CustomScpExplorer.cpp 397 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927
  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. #define WM_PASTE_FILES (WM_WINSCP_USER + 14)
  55. static const int SessionPanelCount = 3;
  56. //---------------------------------------------------------------------------
  57. class TMutexGuard
  58. {
  59. public:
  60. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  61. int Timeout = 5000)
  62. {
  63. FMutex = NULL;
  64. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  65. if (WaitResult == WAIT_TIMEOUT)
  66. {
  67. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  68. }
  69. else
  70. {
  71. FMutex = AMutex;
  72. }
  73. }
  74. ~TMutexGuard()
  75. {
  76. if (FMutex != NULL)
  77. {
  78. ReleaseMutex(FMutex);
  79. }
  80. }
  81. private:
  82. HANDLE FMutex;
  83. };
  84. //---------------------------------------------------------------------------
  85. class TWindowLock
  86. {
  87. public:
  88. TWindowLock(TCustomScpExplorerForm * Form) :
  89. FForm(Form)
  90. {
  91. FForm->LockWindow();
  92. }
  93. ~TWindowLock()
  94. {
  95. FForm->UnlockWindow();
  96. }
  97. private:
  98. TCustomScpExplorerForm * FForm;
  99. };
  100. //---------------------------------------------------------------------------
  101. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  102. FForm(Form)
  103. {
  104. FForm->BatchStart(FBatchStorage);
  105. }
  106. //---------------------------------------------------------------------------
  107. TAutoBatch::~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. //---------------------------------------------------------------------------
  112. struct TTransferOperationParam
  113. {
  114. TTransferOperationParam();
  115. UnicodeString TargetDirectory;
  116. bool Temp;
  117. bool DragDrop;
  118. int Options;
  119. TAutoSwitch Queue;
  120. };
  121. //---------------------------------------------------------------------------
  122. TTransferOperationParam::TTransferOperationParam()
  123. {
  124. Temp = false;
  125. DragDrop = false;
  126. Options = 0;
  127. Queue = asAuto;
  128. }
  129. //---------------------------------------------------------------------------
  130. class TTransferPresetNoteData : public TObject
  131. {
  132. public:
  133. UnicodeString Message;
  134. };
  135. //---------------------------------------------------------------------------
  136. class TTerminalNoteData : public TObject
  137. {
  138. public:
  139. TManagedTerminal * Terminal;
  140. };
  141. //---------------------------------------------------------------------------
  142. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  143. FFormRestored(false),
  144. TForm(Owner)
  145. {
  146. AddStartupSequence(L"F");
  147. FInvalid = false;
  148. FCurrentSide = osRemote;
  149. FEverShown = false;
  150. FDocks = new TList();
  151. InitControls();
  152. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  153. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  154. NonVisualDataModule->ScpExplorer = this;
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. FAutoOperation = false;
  159. FOnFileOperationFinished = NULL;
  160. FPrimaryOperation = foNone;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDExtMapFile = NULL;
  169. // CreateMutexW keeps failing with ERROR_NOACCESS
  170. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  171. DebugAssert(FDDExtMutex != NULL);
  172. FDDTargetControl = NULL;
  173. FDelayedDeletionTimer = NULL;
  174. FDelayedDeletionList = new TStringList();
  175. FDDFileList = NULL;
  176. FPendingTempSpaceWarn = false;
  177. FCapturedLog = NULL;
  178. FDragDropOperation = false;
  179. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  180. FAllowTransferPresetAutoSelect = true;
  181. FSessionChanging = false;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FDeletedFiles = NULL;
  195. FMoveToQueue = false;
  196. StandaloneOperation = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FOnSynchronizeAbort = NULL;
  199. FSynchronizeTerminal = NULL;
  200. FNeedSession = false;
  201. FDoNotIdleCurrentTerminal = 0;
  202. FIncrementalSearching = 0;
  203. FIncrementalSearchHaveNext = false;
  204. FQueueFileList.reset(new TQueueFileList());
  205. FProgressSide = osCurrent;
  206. FCalculateSizeOperation = NULL;
  207. FDownloadingFromClipboard = false;
  208. FClipboardFakeMonitorsPendingReset = false;
  209. FEditorManager = new TEditorManager();
  210. FEditorManager->OnFileChange = ExecutedFileChanged;
  211. FEditorManager->OnFileReload = ExecutedFileReload;
  212. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  213. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  214. FLocalEditors = new TList();
  215. FQueueStatus = NULL;
  216. FQueueStatusSection = new TCriticalSection();
  217. FQueueStatusInvalidated = false;
  218. FQueueItemInvalidated = false;
  219. FQueueStatusUpdating = false;
  220. FQueueActedItem = NULL;
  221. FQueueController = new TQueueController(QueueView3);
  222. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  223. FUserActionTimer = new TTimer(this);
  224. FUserActionTimer->Enabled = false;
  225. FUserActionTimer->Interval = 10;
  226. FUserActionTimer->OnTimer = UserActionTimer;
  227. FNotes = new TStringList();
  228. FNoteTimer = new TTimer(this);
  229. FNoteTimer->Enabled = false;
  230. FNoteTimer->OnTimer = NoteTimer;
  231. FOnNoteClick = NULL;
  232. FOle32Library = LoadLibrary(L"Ole32.dll");
  233. FDragMoveCursor = FOle32Library != NULL ?
  234. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  235. UseSystemSettings(this);
  236. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  237. static_cast<TObject*>(GetComponent(fcTransferList)));
  238. DebugAssert(TransferList != NULL);
  239. FTransferListHoverIndex = -1;
  240. TransferList->OnChange = TransferListChange;
  241. TransferList->OnDrawItem = TransferListDrawItem;
  242. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  243. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  244. UseDesktopFont(SessionsPageControl);
  245. UpdateSessionsPageControlHeight();
  246. UseDesktopFont(RemoteDirView);
  247. UseDesktopFont(RemoteDriveView);
  248. UseDesktopFont(QueueView3);
  249. UseDesktopFont(QueueFileList);
  250. UseDesktopFont(QueueLabel);
  251. UseDesktopFont(RemoteStatusBar);
  252. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  253. QueueSplitter->ShowHint = true;
  254. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  255. QueueFileListSplitter->ShowHint = true;
  256. RemotePanelSplitter->ShowHint = true;
  257. UpdateImages();
  258. FCustomCommandMenu = CreateTBXPopupMenu(this);
  259. FCustomCommandLocalFileList = NULL;
  260. FCustomCommandRemoteFileList = NULL;
  261. FSessionColors = new TPngImageList(this);
  262. FSessionColors->ColorDepth = cd32Bit;
  263. AddFixedSessionImages();
  264. SessionsPageControl->Images = FSessionColors;
  265. UpdateQueueLabel();
  266. CheckStoreTransition();
  267. CreateHiddenWindow();
  268. StartUpdates();
  269. // Start loading drive data, so that they are ready once we need them for StartCreationDirectoryMonitorsOnEachDrive
  270. // (necessary for "explorer" only, as "commander" loads it for its drive menu)
  271. DriveInfo->NeedData();
  272. }
  273. //---------------------------------------------------------------------------
  274. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  275. {
  276. FInvalid = true;
  277. if (FClipboardTerminal != NULL)
  278. {
  279. ClipboardClear(); // implies ClipboardStop
  280. }
  281. // this has to be one of the very first things to do
  282. StopUpdateThread();
  283. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  284. TTerminalManager::Instance()->ScpExplorer = NULL;
  285. delete FCustomCommandLocalFileList;
  286. delete FCustomCommandRemoteFileList;
  287. delete FCustomCommandMenu;
  288. delete FTrayIcon;
  289. FTrayIcon = NULL;
  290. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  291. delete FEditorManager;
  292. ForceCloseLocalEditors();
  293. delete FLocalEditors;
  294. HideFileFindDialog();
  295. if (FDelayedDeletionTimer)
  296. {
  297. DoDelayedDeletion(NULL);
  298. SAFE_DESTROY(FDelayedDeletionTimer);
  299. }
  300. SAFE_DESTROY(FDelayedDeletionList);
  301. // sometimes we do not get DDEnd so the list is not released
  302. SAFE_DESTROY(FDDFileList);
  303. DebugAssert(FSynchronizeController == NULL);
  304. CloseHandle(FDDExtMutex);
  305. FDDExtMutex = NULL;
  306. FreeLibrary(FOle32Library);
  307. FOle32Library = NULL;
  308. FDragMoveCursor = NULL;
  309. DebugAssert(!FErrorList);
  310. if (FEverShown)
  311. {
  312. // when window is never shown (like when running command-line operation),
  313. // particularly window site is not restored correctly (BoundsRect value set
  314. // in RestoreForm gets lost during handle allocation), so we do not want
  315. // it to be stored
  316. StoreParams();
  317. }
  318. ManagedSession = NULL;
  319. Queue = NULL;
  320. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  321. NonVisualDataModule->ScpExplorer = NULL;
  322. delete FQueueScrollOnDragOver;
  323. FQueueScrollOnDragOver = NULL;
  324. delete FQueueController;
  325. FQueueController = NULL;
  326. delete FQueueStatusSection;
  327. FQueueStatusSection = NULL;
  328. delete FQueueStatus;
  329. FQueueStatus = NULL;
  330. delete FUserActionTimer;
  331. FUserActionTimer = NULL;
  332. delete FNoteTimer;
  333. delete FNotes;
  334. delete FNoteData;
  335. SAFE_DESTROY(FDocks);
  336. SessionsPageControl->Images = NULL;
  337. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  338. SAFE_DESTROY(FSessionColors);
  339. SAFE_DESTROY(FSessionsDragDropFilesEx);
  340. SAFE_DESTROY(FHistoryMenu[0][0]);
  341. SAFE_DESTROY(FHistoryMenu[0][1]);
  342. SAFE_DESTROY(FHistoryMenu[1][0]);
  343. SAFE_DESTROY(FHistoryMenu[1][1]);
  344. if (FHiddenWindow != NULL)
  345. {
  346. DestroyWindow(FHiddenWindow);
  347. FHiddenWindow = NULL;
  348. }
  349. }
  350. //---------------------------------------------------------------------------
  351. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  352. {
  353. std::unique_ptr<TSessionData> Data;
  354. if (!Session.IsEmpty())
  355. {
  356. bool DefaultsOnly;
  357. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  358. }
  359. TTerminalManager * Manager = TTerminalManager::Instance();
  360. for (int Index = 0; Index < Manager->Count; Index++)
  361. {
  362. TTerminal * Terminal = Manager->Sessions[Index];
  363. if (Session.IsEmpty() ||
  364. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  365. {
  366. if (Path.IsEmpty())
  367. {
  368. Terminal->ClearCaches();
  369. }
  370. else
  371. {
  372. Terminal->DirectoryModified(Path, true);
  373. }
  374. }
  375. }
  376. // We should flag a pending refresh for the background terminals or busy foreground terminals
  377. if (HasActiveTerminal() &&
  378. CanCommandLineFromAnotherInstance() &&
  379. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  380. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  381. {
  382. Terminal->ReloadDirectory();
  383. }
  384. }
  385. //---------------------------------------------------------------------------
  386. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  387. {
  388. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  389. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  390. size_t MessageSize = sizeof(TCopyDataMessage);
  391. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  392. if (Result)
  393. {
  394. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  395. Result = (Message.Version == TCopyDataMessage::Version1);
  396. if (Result)
  397. {
  398. switch (Message.Command)
  399. {
  400. case TCopyDataMessage::CommandCanCommandLine:
  401. Result = CanCommandLineFromAnotherInstance();
  402. if (Result)
  403. {
  404. AppLog(L"Can process command-line from other instance");
  405. }
  406. else
  407. {
  408. AppLog(L"Cannot process command-line from other instance");
  409. }
  410. break;
  411. case TCopyDataMessage::CommandCommandLine:
  412. {
  413. UnicodeString CommandLine(Message.CommandLine);
  414. Result = CommandLineFromAnotherInstance(CommandLine);
  415. }
  416. break;
  417. case TCopyDataMessage::RefreshPanel:
  418. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  419. break;
  420. case TCopyDataMessage::MainWindowCheck:
  421. Result = true;
  422. break;
  423. default:
  424. Result = false;
  425. break;
  426. }
  427. }
  428. }
  429. Message.Result = Result ? 1 : 0;
  430. }
  431. //---------------------------------------------------------------------------
  432. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  433. {
  434. WNDCLASS WindowClass = {0};
  435. WindowClass.lpfnWndProc = DefWindowProc;
  436. WindowClass.hInstance = HInstance;
  437. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  438. FHiddenWindow = NULL;
  439. if (RegisterClass(&WindowClass))
  440. {
  441. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  442. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  443. }
  444. }
  445. //---------------------------------------------------------------------------
  446. bool __fastcall TCustomScpExplorerForm::CanConsole()
  447. {
  448. return
  449. HasActiveTerminal() &&
  450. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  451. }
  452. //---------------------------------------------------------------------------
  453. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  454. {
  455. bool Result = Visible && !NonVisualDataModule->Busy;
  456. return Result;
  457. }
  458. //---------------------------------------------------------------------------
  459. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  460. const UnicodeString & CommandLine)
  461. {
  462. TProgramParams Params(CommandLine);
  463. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  464. if (Result)
  465. {
  466. NonVisualDataModule->StartBusy();
  467. try
  468. {
  469. // this action is initiated from another process,
  470. // so it's likely that our window is not visible,
  471. // and user won't see what is going on
  472. Application->BringToFront();
  473. // reload sessions as we may be asked to open a session
  474. // just stored by another instance
  475. StoredSessions->Reload();
  476. UnicodeString SessionName = Params.Param[1];
  477. std::unique_ptr<TObjectList> DataList(new TObjectList());
  478. try
  479. {
  480. UnicodeString DownloadFile; // unused
  481. int Flags = GetCommandLineParseUrlFlags(&Params);
  482. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  483. }
  484. catch (EAbort &)
  485. {
  486. // Happens when opening session in PuTTY and in other situations,
  487. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  488. // to process the command-line on its own (or to pass it to yet another instance)
  489. }
  490. if (DataList->Count > 0)
  491. {
  492. TTerminalManager * Manager = TTerminalManager::Instance();
  493. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  494. }
  495. }
  496. __finally
  497. {
  498. NonVisualDataModule->EndBusy();
  499. }
  500. }
  501. return Result;
  502. }
  503. //---------------------------------------------------------------------------
  504. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  505. {
  506. TManagedTerminal * Result;
  507. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  508. {
  509. Result = FManagedSession;
  510. }
  511. else
  512. {
  513. Result = NULL;
  514. }
  515. return Result;
  516. }
  517. //---------------------------------------------------------------------------
  518. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  519. {
  520. if (FManagedSession != value)
  521. {
  522. SessionChanging();
  523. DoSetManagedSession(value, false);
  524. }
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  528. {
  529. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  530. FManagedSession = value;
  531. {
  532. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  533. FAllowTransferPresetAutoSelect = false;
  534. TAutoFlag SessionChangingFlag(FSessionChanging);
  535. SessionChanged(Replace);
  536. }
  537. if (Terminal != NULL)
  538. {
  539. TransferPresetAutoSelect();
  540. }
  541. // Update app and tab titles, prevented above by FSessionChanging.
  542. UpdateControls();
  543. }
  544. //---------------------------------------------------------------------------
  545. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  546. {
  547. DoSetManagedSession(value, true);
  548. }
  549. //---------------------------------------------------------------------------
  550. void __fastcall TCustomScpExplorerForm::SessionChanging()
  551. {
  552. if (ManagedSession != NULL)
  553. {
  554. UpdateSession(ManagedSession);
  555. }
  556. }
  557. //---------------------------------------------------------------------------
  558. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  559. {
  560. if (ManagedSession != NULL)
  561. {
  562. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  563. }
  564. if (!TTerminalManager::Instance()->Updating)
  565. {
  566. SessionListChanged();
  567. }
  568. if (Terminal != NULL)
  569. {
  570. TObject * State = NULL;
  571. if (WinConfiguration->PreservePanelState)
  572. {
  573. State = Terminal->RemoteExplorerState;
  574. DirView(osRemote)->AnnounceState(State);
  575. }
  576. try
  577. {
  578. if (Replaced)
  579. {
  580. RemoteDirView->ReplaceTerminal(Terminal);
  581. }
  582. else
  583. {
  584. RemoteDirView->Terminal = Terminal;
  585. }
  586. if (Terminal->Active)
  587. {
  588. Terminal->RefreshDirectory();
  589. }
  590. if (State != NULL)
  591. {
  592. DirView(osRemote)->RestoreState(State);
  593. }
  594. }
  595. __finally
  596. {
  597. DirView(osRemote)->AnnounceState(NULL);
  598. }
  599. }
  600. else
  601. {
  602. RemoteDirView->Terminal = NULL;
  603. }
  604. InitStatusBar();
  605. UpdateTransferList();
  606. // Update panels Enable state before refreshing the labels
  607. UpdateControls();
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  611. {
  612. if (Queue != value)
  613. {
  614. if (FQueue != NULL)
  615. {
  616. FQueue->OnListUpdate = NULL;
  617. FQueue->OnQueueItemUpdate = NULL;
  618. }
  619. FQueue = value;
  620. if (FQueue != NULL)
  621. {
  622. DebugAssert(FQueue->OnListUpdate == NULL);
  623. FQueue->OnListUpdate = QueueListUpdate;
  624. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  625. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  626. }
  627. QueueChanged();
  628. }
  629. }
  630. //---------------------------------------------------------------------------
  631. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  632. TListItem * Item)
  633. {
  634. if (FQueueActedItem == Item)
  635. {
  636. FQueueActedItem = NULL;
  637. if ((QueueView3->PopupMenu != NULL) &&
  638. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  639. {
  640. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  641. ReleaseCapture();
  642. }
  643. }
  644. if (Item->Data == FPendingQueueActionItem)
  645. {
  646. FPendingQueueActionItem = NULL;
  647. }
  648. }
  649. //---------------------------------------------------------------------------
  650. bool TCustomScpExplorerForm::IsAnythingQueued()
  651. {
  652. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  656. {
  657. {
  658. TGuard Guard(FQueueStatusSection);
  659. FQueueStatusInvalidated = false;
  660. if (FQueue != NULL)
  661. {
  662. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  663. }
  664. }
  665. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  666. {
  667. FMaxQueueLength = FQueueStatus->Count;
  668. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  669. }
  670. {
  671. TAutoFlag Flag(FQueueStatusUpdating);
  672. FQueueController->UpdateQueueStatus(FQueueStatus);
  673. }
  674. UpdateQueueFileList();
  675. SetQueueProgress();
  676. UpdateQueueView();
  677. bool IsEmpty = !IsAnythingQueued();
  678. if (IsEmpty && (Terminal != NULL))
  679. {
  680. TOnceDoneOperation OnceDoneOperation =
  681. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  682. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  683. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  684. {
  685. FQueue->Enabled = false;
  686. }
  687. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  688. {
  689. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  690. }
  691. }
  692. }
  693. //---------------------------------------------------------------------------
  694. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  695. {
  696. UnicodeString Result;
  697. if (FQueueStatus != NULL)
  698. {
  699. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  700. {
  701. TFileOperationProgressType * ProgressData =
  702. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  703. if ((ProgressData != NULL) && ProgressData->InProgress)
  704. {
  705. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  706. }
  707. }
  708. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  709. {
  710. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  711. }
  712. }
  713. return Result;
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  717. {
  718. ComponentVisible[fcQueueView] =
  719. !IsLocalBrowserMode() &&
  720. ((WinConfiguration->QueueView.Show == qvShow) ||
  721. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  722. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  723. }
  724. //---------------------------------------------------------------------------
  725. void __fastcall TCustomScpExplorerForm::QueueChanged()
  726. {
  727. if (FQueueStatus != NULL)
  728. {
  729. // UpdateFileList implementation relies on the status being recreated when session changes
  730. delete FQueueStatus;
  731. FQueueStatus = NULL;
  732. }
  733. UpdateQueueStatus(true);
  734. }
  735. //---------------------------------------------------------------------------
  736. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  737. {
  738. if (FQueue == Queue)
  739. {
  740. FQueueStatusInvalidated = true;
  741. }
  742. }
  743. //---------------------------------------------------------------------------
  744. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  745. TQueueItem * Item)
  746. {
  747. if (FQueue == Queue)
  748. {
  749. TGuard Guard(FQueueStatusSection);
  750. // this may be running in parallel with QueueChanged
  751. if (FQueueStatus != NULL)
  752. {
  753. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  754. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  755. {
  756. FRefreshLocalDirectory = (QueueItem == NULL) ||
  757. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  758. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  759. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  760. }
  761. if (QueueItem != NULL)
  762. {
  763. QueueItem->UserData = (void*)true;
  764. FQueueItemInvalidated = true;
  765. }
  766. }
  767. }
  768. }
  769. //---------------------------------------------------------------------------
  770. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  771. {
  772. // during standalone editing, we have no way to see/control queue,
  773. // so we have to always popup prompts automatically
  774. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  775. }
  776. //---------------------------------------------------------------------------
  777. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  778. {
  779. if (FQueueStatus != NULL)
  780. {
  781. bool QueueAutoPopup = IsQueueAutoPopup();
  782. bool NeedRefresh = FQueueController->NeedRefresh();
  783. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  784. FQueueItemInvalidated = false;
  785. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  786. bool Updated = false;
  787. TQueueItemProxy * QueueItem;
  788. bool WasUserAction;
  789. for (int Index = 0; Index < Limit; Index++)
  790. {
  791. bool Update = false;
  792. QueueItem = FQueueStatus->Items[Index];
  793. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  794. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  795. (FPendingQueueActionItem == NULL))
  796. {
  797. FPendingQueueActionItem = QueueItem;
  798. FUserActionTimer->Enabled = true;
  799. }
  800. if ((bool)QueueItem->UserData)
  801. {
  802. QueueItem->UserData = (void*)false;
  803. QueueItem->Update();
  804. Updated = true;
  805. Update = true;
  806. }
  807. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  808. {
  809. Update = true;
  810. }
  811. if (Update)
  812. {
  813. FQueueController->RefreshQueueItem(QueueItem);
  814. }
  815. }
  816. if (Updated)
  817. {
  818. NonVisualDataModule->UpdateNonVisibleActions();
  819. SetQueueProgress();
  820. UpdateQueueFileList();
  821. }
  822. }
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  826. {
  827. if (FTaskbarList != NULL)
  828. {
  829. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  830. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  831. }
  832. }
  833. //---------------------------------------------------------------------------
  834. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  835. {
  836. if (Progress >= 0)
  837. {
  838. if (FTaskbarList != NULL)
  839. {
  840. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  841. }
  842. }
  843. else
  844. {
  845. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  846. }
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  850. {
  851. int OverallProgress;
  852. // FProgressForm is null when this is called from SetQueueProgress
  853. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  854. {
  855. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  856. }
  857. else if (!ProgressData->IsIndeterminate())
  858. {
  859. OverallProgress = ProgressData->OverallProgress();
  860. }
  861. else
  862. {
  863. OverallProgress = -1;
  864. }
  865. SetTaskbarListProgressValue(OverallProgress);
  866. }
  867. //---------------------------------------------------------------------------
  868. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  869. {
  870. TTerminalManager::Instance()->QueueStatusUpdated();
  871. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  872. {
  873. // Do not animate task bar button, when items are queued, but processing is disabled
  874. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  875. {
  876. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  877. {
  878. TFileOperationProgressType * ProgressData = NULL;
  879. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  880. {
  881. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  882. }
  883. if ((ProgressData != NULL) &&
  884. ProgressData->InProgress)
  885. {
  886. SetTaskbarListProgressValue(ProgressData);
  887. }
  888. else
  889. {
  890. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  891. }
  892. }
  893. else
  894. {
  895. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  896. }
  897. }
  898. else
  899. {
  900. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  901. }
  902. }
  903. UpdateQueueLabel();
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  907. {
  908. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  909. if (FQueueStatus != NULL)
  910. {
  911. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  912. if (ActiveAndPendingPrimaryCount > 0)
  913. {
  914. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  915. }
  916. }
  917. QueueLabel->Caption = Caption;
  918. }
  919. //---------------------------------------------------------------------------
  920. void TCustomScpExplorerForm::CheckStoreTransition()
  921. {
  922. if (WinConfiguration->StoreTransition == stInit)
  923. {
  924. if (IsUWP())
  925. {
  926. WinConfiguration->StoreTransition = stStoreFresh;
  927. AppLog(L"Fresh UWP/store installation");
  928. }
  929. else
  930. {
  931. WinConfiguration->StoreTransition = stStandard;
  932. AppLog(L"Fresh standard installation");
  933. }
  934. }
  935. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  936. {
  937. WinConfiguration->StoreTransition = stStoreMigrated;
  938. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  939. AppLog(L"Standard installation migrated to store installation");
  940. }
  941. if (WinConfiguration->StoreTransition == stStoreMigrated)
  942. {
  943. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  944. Toolbar->Parent = MessageDock;
  945. Toolbar->FullSize = true;
  946. Toolbar->Color = Application->HintColor;
  947. TTBXLabelItem * LabelItem;
  948. LabelItem = new TTBXLabelItem(Toolbar);
  949. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  950. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  951. Toolbar->Items->Add(LabelItem);
  952. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  953. LinkItem->FontSettings->Color = LinkColor;
  954. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  955. LinkItem->OnClick = StoreTransitionLinkClick;
  956. Toolbar->Items->Add(LinkItem);
  957. AddMenuSeparator(Toolbar->Items);
  958. TTBCustomItem * CloseItem = new TTBXItem(Toolbar);
  959. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  960. CloseItem->OnClick = StoreTransitionCloseClick;
  961. Toolbar->Items->Add(CloseItem);
  962. GiveTBItemPriority(CloseItem);
  963. }
  964. }
  965. //---------------------------------------------------------------------------
  966. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  967. {
  968. ShowHelp(HELP_STORE_TRANSITION);
  969. }
  970. //---------------------------------------------------------------------------
  971. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  972. {
  973. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  974. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  975. delete Toolbar;
  976. WinConfiguration->StoreTransition = stStoreAcknowledged;
  977. UpdateControls();
  978. }
  979. //---------------------------------------------------------------------------
  980. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  981. {
  982. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  983. static_cast<TComponent*>(GetComponent(fcTransferList)));
  984. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  985. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  986. TransferList->Strings->BeginUpdate();
  987. try
  988. {
  989. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  990. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  991. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  992. if (FTransferDropDownHint.IsEmpty())
  993. {
  994. FTransferDropDownHint = TransferDropDown->Hint;
  995. }
  996. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  997. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  998. TransferDropDown->Text = StripHotkey(Name);
  999. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  1000. (FTransferDropDownHint, StripHotkey(Name),
  1001. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  1002. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  1003. // update the label, otherwise when it is updated only on the first draw
  1004. // of the list, it is drawn "bold" for some reason
  1005. FTransferListHoverIndex = TransferList->ItemIndex;
  1006. UpdateTransferLabel();
  1007. }
  1008. __finally
  1009. {
  1010. TransferList->Strings->EndUpdate();
  1011. }
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1015. {
  1016. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1017. DebugAssert(Toolbar != NULL);
  1018. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1019. }
  1020. //---------------------------------------------------------------------------
  1021. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1022. {
  1023. TForm::UpdateActions();
  1024. if (ComponentVisible[fcCustomCommandsBand])
  1025. {
  1026. UpdateCustomCommandsToolbar();
  1027. }
  1028. }
  1029. //---------------------------------------------------------------------------
  1030. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1031. {
  1032. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1033. }
  1034. //---------------------------------------------------------------------------
  1035. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1036. {
  1037. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1038. {
  1039. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1040. // Without this, the panel turns gray and unused part of header turns black
  1041. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1042. }
  1043. }
  1044. //---------------------------------------------------------------------------
  1045. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1046. {
  1047. Color = GetBtnFaceColor();
  1048. DebugAssert(Configuration && RemoteDirView);
  1049. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1050. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1051. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1052. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1053. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1054. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1055. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1056. UpdateRowSelect(RemoteDirView);
  1057. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1058. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1059. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1060. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1061. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1062. UpdateSessionsPageControlHeight();
  1063. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1064. UpdateToolbarDisplayMode();
  1065. UpdateTransferList();
  1066. if (Terminal != NULL)
  1067. {
  1068. TransferPresetAutoSelect();
  1069. }
  1070. UpdateQueueView();
  1071. UpdateControls();
  1072. // this can be called even before constuctor finishes.
  1073. if (FEditorManager != NULL)
  1074. {
  1075. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1076. }
  1077. // this can be called even before constuctor finishes.
  1078. if (FLocalEditors != NULL)
  1079. {
  1080. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1081. {
  1082. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1083. }
  1084. }
  1085. if (ComponentVisible[fcCustomCommandsBand])
  1086. {
  1087. UpdateCustomCommandsToolbar();
  1088. }
  1089. // show only when keeping queue items forever,
  1090. // otherwise, it's enough to have it in the context menu
  1091. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1092. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1093. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1094. {
  1095. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1096. FFileColorsCurrent = WinConfiguration->FileColors;
  1097. FileColorsChanged();
  1098. }
  1099. }
  1100. //---------------------------------------------------------------------------
  1101. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1102. {
  1103. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1104. DirView->Invalidate();
  1105. }
  1106. //---------------------------------------------------------------------------
  1107. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1108. {
  1109. DoFileColorsChanged(RemoteDirView);
  1110. }
  1111. //---------------------------------------------------------------------------
  1112. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1113. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1114. void * /*Arg*/)
  1115. {
  1116. if (Token != NULL)
  1117. {
  1118. TForm * Editor = dynamic_cast<TForm*>(Token);
  1119. DebugAssert(Editor != NULL);
  1120. ReconfigureEditorForm(Editor);
  1121. }
  1122. }
  1123. //---------------------------------------------------------------------------
  1124. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1125. {
  1126. WinConfiguration->DDTransferConfirmation = asOn;
  1127. }
  1128. //---------------------------------------------------------------------------
  1129. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1130. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1131. {
  1132. // noop
  1133. }
  1134. //---------------------------------------------------------------------------
  1135. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1136. {
  1137. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1141. {
  1142. if (DoNotShowAgain)
  1143. {
  1144. if (DragDrop)
  1145. {
  1146. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1147. {
  1148. PopupTrayBalloon(
  1149. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1150. }
  1151. WinConfiguration->DDTransferConfirmation = asOff;
  1152. }
  1153. else
  1154. {
  1155. WinConfiguration->ConfirmTransferring = false;
  1156. }
  1157. }
  1158. else
  1159. {
  1160. // User explicitly unchecked "do not show again",
  1161. // so show him the dialog the next time
  1162. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1163. {
  1164. WinConfiguration->DDTransferConfirmation = asOn;
  1165. }
  1166. }
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1170. TTransferDirection Direction, TTransferType Type, bool Temp,
  1171. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1172. bool Confirm, bool DragDrop, int Options)
  1173. {
  1174. bool Result = true;
  1175. DebugAssert(HasActiveTerminal());
  1176. // these parameters are known in advance
  1177. int Params =
  1178. FLAGMASK(Type == ttMove, cpDelete);
  1179. bool ToTemp = (Temp && (Direction == tdToLocal));
  1180. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1181. {
  1182. bool DisableNewerOnly =
  1183. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1184. ToTemp;
  1185. Options |=
  1186. FLAGMASK(ToTemp, coTemp) |
  1187. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1188. coDoNotShowAgain;
  1189. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1190. int CopyParamAttrs = (Direction == tdToRemote ?
  1191. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1192. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1193. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1194. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1195. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1196. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1197. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1198. if (Result)
  1199. {
  1200. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1201. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1202. }
  1203. }
  1204. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1205. {
  1206. Configuration->Usage->Inc(L"TransfersOnBackground");
  1207. // these parameter are known only after transfer dialog
  1208. Params |=
  1209. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1210. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1211. Result = false;
  1212. ClearTransferSourceSelection(Direction);
  1213. }
  1214. return Result;
  1215. }
  1216. //---------------------------------------------------------------------------
  1217. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1218. {
  1219. if (FOnFileOperationFinished != NULL)
  1220. {
  1221. FOnFileOperationFinished(Side, UnicodeString(), true);
  1222. }
  1223. else
  1224. {
  1225. if (HasDirView[Side])
  1226. {
  1227. DirView(Side)->SelectAll(smNone);
  1228. }
  1229. }
  1230. }
  1231. //---------------------------------------------------------------------------
  1232. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1233. {
  1234. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1235. ClearOperationSelection(Side);
  1236. }
  1237. //---------------------------------------------------------------------------
  1238. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1239. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1240. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1241. int Params)
  1242. {
  1243. DebugAssert(Queue != NULL);
  1244. bool SingleFile = false;
  1245. if (FileList->Count == 1)
  1246. {
  1247. if (Direction == tdToRemote)
  1248. {
  1249. UnicodeString FileName = FileList->Strings[0];
  1250. SingleFile = FileExists(ApiPath(FileName));
  1251. }
  1252. else
  1253. {
  1254. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1255. SingleFile = !File->IsDirectory;
  1256. }
  1257. }
  1258. TQueueItem * QueueItem;
  1259. if (Direction == tdToRemote)
  1260. {
  1261. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1262. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1263. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1264. }
  1265. else
  1266. {
  1267. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1268. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1269. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1270. }
  1271. AddQueueItem(Queue, QueueItem, Terminal);
  1272. }
  1273. //---------------------------------------------------------------------------
  1274. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1275. {
  1276. if (Queue->IsEmpty)
  1277. {
  1278. Terminal->QueueOperationStart = Now();
  1279. }
  1280. Queue->AddItem(QueueItem);
  1281. }
  1282. //---------------------------------------------------------------------------
  1283. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1284. {
  1285. }
  1286. //---------------------------------------------------------------------------
  1287. void __fastcall TCustomScpExplorerForm::InitControls()
  1288. {
  1289. DebugAssert(FDocks != NULL);
  1290. for (int Index = 0; Index < ComponentCount; Index++)
  1291. {
  1292. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1293. if ((Dock != NULL) && (Dock->Tag == 0))
  1294. {
  1295. FDocks->Add(Dock);
  1296. }
  1297. }
  1298. CollectItemsWithTextDisplayMode(this);
  1299. }
  1300. //---------------------------------------------------------------------------
  1301. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1302. {
  1303. ConfigurationChanged();
  1304. QueuePanel->Height =
  1305. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1306. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1307. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1308. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1309. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1310. QueueFileList->Height =
  1311. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1312. if (QueueFileList->Visible)
  1313. {
  1314. AdjustQueueLayout();
  1315. }
  1316. UpdateCustomCommandsToolbar();
  1317. }
  1318. //---------------------------------------------------------------------------
  1319. void __fastcall TCustomScpExplorerForm::StoreParams()
  1320. {
  1321. int APixelsPerInch = GetControlPixelsPerInch(this);
  1322. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1323. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1324. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1325. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1326. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1327. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1328. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1329. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1330. }
  1331. //---------------------------------------------------------------------------
  1332. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1333. {
  1334. if (!FFormRestored)
  1335. {
  1336. FFormRestored = true;
  1337. RestoreFormParams();
  1338. }
  1339. TForm::CreateParams(Params);
  1340. }
  1341. //---------------------------------------------------------------------------
  1342. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1343. {
  1344. DebugAssert(FDocks != NULL);
  1345. for (int Index = 0; Index < FDocks->Count; Index++)
  1346. {
  1347. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1348. }
  1349. }
  1350. //---------------------------------------------------------------------------
  1351. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1352. {
  1353. SetDockAllowDrag(true);
  1354. while (!ButtonsStr.IsEmpty())
  1355. {
  1356. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1357. UnicodeString Name = CutToChar(S, L'=', true);
  1358. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1359. {
  1360. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1361. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1362. {
  1363. while (!S.IsEmpty())
  1364. {
  1365. UnicodeString S2 = CutToChar(S, L',', true);
  1366. UnicodeString Name = CutToChar(S2, L':', true);
  1367. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1368. {
  1369. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1370. if (SameText(GetToolbarItemName(Item), Name))
  1371. {
  1372. // forward compatibility
  1373. UnicodeString S3 = CutToChar(S2, L':', true);
  1374. bool Visible = StrToIntDef(S3, 0);
  1375. Item->Visible = Visible;
  1376. }
  1377. }
  1378. }
  1379. }
  1380. }
  1381. }
  1382. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1383. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1384. }
  1385. //---------------------------------------------------------------------------
  1386. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1387. {
  1388. UnicodeString Result;
  1389. SetDockAllowDrag(true);
  1390. Result = ::GetToolbarsLayoutStr(this);
  1391. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1392. return Result;
  1393. }
  1394. //---------------------------------------------------------------------------
  1395. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1396. {
  1397. UnicodeString Result;
  1398. if (Item->Action != NULL)
  1399. {
  1400. Result = Item->Action->Name;
  1401. Result = RemoveSuffix(Result, L"Action", true);
  1402. }
  1403. else
  1404. {
  1405. Result = Item->Name;
  1406. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1407. Result = RemoveSuffix(Result, L"Item", true);
  1408. }
  1409. return Result;
  1410. }
  1411. //---------------------------------------------------------------------------
  1412. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1413. {
  1414. UnicodeString Result;
  1415. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1416. {
  1417. UnicodeString ToolbarStr;
  1418. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1419. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1420. {
  1421. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1422. {
  1423. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1424. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1425. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1426. // and save the changes here only.
  1427. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1428. {
  1429. UnicodeString Name = GetToolbarItemName(Item);
  1430. DebugAssert(Name.Pos(L"TBX") == 0);
  1431. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1432. }
  1433. }
  1434. }
  1435. if (!ToolbarStr.IsEmpty())
  1436. {
  1437. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1438. }
  1439. }
  1440. return Result;
  1441. }
  1442. //---------------------------------------------------------------------------
  1443. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1444. {
  1445. DebugAssert(FProgressForm == NULL);
  1446. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1447. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1448. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1449. FProgressForm->DeleteLocalToRecycleBin =
  1450. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1451. FProgressForm->DeleteRemoteToRecycleBin =
  1452. HasActiveTerminal() &&
  1453. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1454. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1455. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1456. LockWindow();
  1457. // Actually, do not know what hides the progress once the operation finishes
  1458. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1459. SetTaskbarListProgressState(TBPF_NORMAL);
  1460. }
  1461. //---------------------------------------------------------------------------
  1462. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1463. {
  1464. SAFE_DESTROY(FProgressForm);
  1465. UnlockWindow();
  1466. SetQueueProgress();
  1467. }
  1468. //---------------------------------------------------------------------------
  1469. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1470. TFileOperationProgressType & ProgressData)
  1471. {
  1472. // Download to temporary local directory
  1473. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1474. {
  1475. bool Continue = true;
  1476. FPendingTempSpaceWarn = false;
  1477. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1478. if (!Continue)
  1479. {
  1480. Abort();
  1481. }
  1482. }
  1483. // operation is being executed and we still didn't show up progress form
  1484. if (ProgressData.InProgress && !FProgressForm)
  1485. {
  1486. CreateProgressForm(NULL);
  1487. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1488. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1489. }
  1490. // operation is finished (or terminated), so we hide progress form
  1491. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1492. {
  1493. DestroyProgressForm();
  1494. if (ProgressData.Operation == foCalculateSize)
  1495. {
  1496. // When calculating size before transfer, the abort caused by
  1497. // cancel flag set due to "MoveToQueue" below,
  1498. // is not propagated back to us in ExecuteFileOperation,
  1499. // as it is silently swallowed.
  1500. // So we explicitly (re)throw it here.
  1501. if (FMoveToQueue)
  1502. {
  1503. Abort();
  1504. }
  1505. }
  1506. else
  1507. {
  1508. if ((ProgressData.Cancel == csContinue) &&
  1509. !FAutoOperation)
  1510. {
  1511. OperationComplete(ProgressData.StartTime);
  1512. }
  1513. }
  1514. }
  1515. if (FProgressForm &&
  1516. // Can happen only during synchronization, when ending one of the operations
  1517. ProgressData.InProgress)
  1518. {
  1519. FProgressForm->SetProgressData(ProgressData);
  1520. DebugAssert(ProgressData.InProgress);
  1521. SetTaskbarListProgressValue(&ProgressData);
  1522. if (FProgressForm->Cancel > csContinue)
  1523. {
  1524. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1525. if (FProgressForm->Cancel == csCancelFile)
  1526. {
  1527. FProgressForm->ClearCancel();
  1528. }
  1529. // cancel cancels even the move
  1530. FMoveToQueue = false;
  1531. }
  1532. else if (FProgressForm->MoveToQueue)
  1533. {
  1534. FMoveToQueue = true;
  1535. ProgressData.SetCancelAtLeast(csCancel);
  1536. }
  1537. if ((FTransferResumeList != NULL) &&
  1538. ProgressData.InProgress &&
  1539. ProgressData.IsTransfer() &&
  1540. !ProgressData.FullFileName.IsEmpty())
  1541. {
  1542. if ((FTransferResumeList->Count == 0) ||
  1543. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1544. {
  1545. // note that we do not recognize directories from files here
  1546. FTransferResumeList->Add(ProgressData.FullFileName);
  1547. }
  1548. }
  1549. }
  1550. }
  1551. //---------------------------------------------------------------------------
  1552. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1553. const TDateTime & StartTime)
  1554. {
  1555. if (GUIConfiguration->BeepOnFinish &&
  1556. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1557. {
  1558. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1559. DWORD Sound;
  1560. if (!ExtractFileExt(BeepSound).IsEmpty())
  1561. {
  1562. Sound = SND_FILENAME;
  1563. }
  1564. else
  1565. {
  1566. Sound = SND_ALIAS;
  1567. }
  1568. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1569. }
  1570. }
  1571. //---------------------------------------------------------------------------
  1572. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1573. TFileOperationProgressType & ProgressData)
  1574. {
  1575. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1576. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1577. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1578. FileOperationProgress(ProgressData);
  1579. }
  1580. //---------------------------------------------------------------------------
  1581. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1582. {
  1583. UnicodeString Result;
  1584. if (FProgressForm != NULL)
  1585. {
  1586. Result = FProgressForm->ProgressStr();
  1587. }
  1588. return Result;
  1589. }
  1590. //---------------------------------------------------------------------------
  1591. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1592. bool DragDrop)
  1593. {
  1594. return (!DragDrop && (DropSourceControl == NULL)) ||
  1595. (DropSourceControl == DirView(osOther));
  1596. }
  1597. //---------------------------------------------------------------------------
  1598. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1599. {
  1600. TCustomDirView * ADirView = DirView(Side);
  1601. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1602. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1603. // this can happen when local drive is unplugged in the middle of the operation
  1604. if (Item != NULL)
  1605. {
  1606. if (Unselect)
  1607. {
  1608. Item->Selected = false;
  1609. }
  1610. ADirView->MakeProgressVisible(Item);
  1611. }
  1612. return Item;
  1613. }
  1614. //---------------------------------------------------------------------------
  1615. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1616. TFileOperation Operation, TOperationSide Side,
  1617. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1618. TOnceDoneOperation & OnceDoneOperation)
  1619. {
  1620. if (!FAutoOperation)
  1621. {
  1622. // no selection on "/upload", form serves only as event handler
  1623. // (it is not displayed)
  1624. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1625. ((Operation == FPrimaryOperation) ||
  1626. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1627. {
  1628. TOperationSide DViewSide;
  1629. if ((FProgressSide != osCurrent) &&
  1630. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1631. {
  1632. // assume the operation is over the focused panel
  1633. DViewSide = FProgressSide;
  1634. }
  1635. else
  1636. {
  1637. DViewSide = Side;
  1638. }
  1639. if (FOnFileOperationFinished != NULL)
  1640. {
  1641. FOnFileOperationFinished(DViewSide, FileName, Success);
  1642. }
  1643. else
  1644. {
  1645. VisualiseOperationFinished(DViewSide, FileName, Success);
  1646. }
  1647. }
  1648. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1649. {
  1650. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1651. {
  1652. if (Side == osLocal)
  1653. {
  1654. Configuration->Usage->Inc(L"UploadedFiles");
  1655. }
  1656. else
  1657. {
  1658. Configuration->Usage->Inc(L"DownloadedFiles");
  1659. }
  1660. }
  1661. }
  1662. }
  1663. if (Success && (FSynchronizeController != NULL))
  1664. {
  1665. DebugAssert(!IsSideLocalBrowser(osRemote));
  1666. if (Operation == foCopy)
  1667. {
  1668. DebugAssert(Side == osLocal);
  1669. FSynchronizeController->LogOperation(soUpload, FileName);
  1670. }
  1671. else if (Operation == foDelete)
  1672. {
  1673. DebugAssert(Side == osRemote);
  1674. FSynchronizeController->LogOperation(soDelete, FileName);
  1675. }
  1676. }
  1677. if (FProgressForm)
  1678. {
  1679. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1680. }
  1681. }
  1682. //---------------------------------------------------------------------------
  1683. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1684. TFileOperation Operation, TOperationSide Side,
  1685. bool Temp, const UnicodeString & FileName, Boolean Success,
  1686. TOnceDoneOperation & OnceDoneOperation)
  1687. {
  1688. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1689. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1690. OnceDoneOperation);
  1691. }
  1692. //---------------------------------------------------------------------------
  1693. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1694. {
  1695. return false;
  1696. }
  1697. //---------------------------------------------------------------------------
  1698. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1699. {
  1700. return false;
  1701. }
  1702. //---------------------------------------------------------------------------
  1703. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1704. {
  1705. return false;
  1706. }
  1707. //---------------------------------------------------------------------------
  1708. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1709. {
  1710. return NULL;
  1711. }
  1712. //---------------------------------------------------------------------------
  1713. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1714. {
  1715. DebugAssert(GetSide(Side) == osRemote);
  1716. DebugUsedParam(Side);
  1717. return RemoteDirView;
  1718. }
  1719. //---------------------------------------------------------------------------
  1720. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1721. {
  1722. DebugAssert(GetSide(Side) == osRemote);
  1723. DebugUsedParam(Side);
  1724. return RemoteDriveView;
  1725. }
  1726. //---------------------------------------------------------------------------
  1727. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1728. {
  1729. DebugAssert(GetSide(Side) == osRemote);
  1730. DebugUsedParam(Side);
  1731. return HasActiveTerminal();
  1732. }
  1733. //---------------------------------------------------------------------------
  1734. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1735. TOperationSide Side, int FilesOnly)
  1736. {
  1737. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1738. }
  1739. //---------------------------------------------------------------------------
  1740. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1741. TOperationSide Side, int FilesOnly)
  1742. {
  1743. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1744. }
  1745. //---------------------------------------------------------------------------
  1746. struct THistoryItemData
  1747. {
  1748. short int Side;
  1749. short int Index;
  1750. };
  1751. //---------------------------------------------------------------------------
  1752. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1753. {
  1754. DirView(Side)->HistoryGo(Index);
  1755. }
  1756. //---------------------------------------------------------------------------
  1757. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1758. {
  1759. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1760. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1761. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1762. }
  1763. //---------------------------------------------------------------------------
  1764. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1765. bool Back)
  1766. {
  1767. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1768. {
  1769. TCustomDirView * DView = DirView(Side);
  1770. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1771. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1772. if (ICount > 10)
  1773. {
  1774. ICount = 10;
  1775. }
  1776. Menu->Items->Clear();
  1777. THistoryItemData Data;
  1778. Data.Side = Side;
  1779. for (short int i = 1; i <= ICount; i++)
  1780. {
  1781. TTBCustomItem * Item = new TTBXItem(Menu);
  1782. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1783. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1784. Item->Hint = DView->HistoryPath[Data.Index];
  1785. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1786. Item->Tag = *reinterpret_cast<int*>(&Data);
  1787. Item->OnClick = HistoryItemClick;
  1788. Menu->Items->Add(Item);
  1789. }
  1790. }
  1791. }
  1792. //---------------------------------------------------------------------------
  1793. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1794. TOperationSide Side, bool Back)
  1795. {
  1796. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1797. {
  1798. // workaround
  1799. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1800. // operator new allocates memory in Pascal code, but calls inline
  1801. // contructor in C++, leading in problems, the function does
  1802. // both in Pascal code
  1803. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1804. UpdateHistoryMenu(Side, Back);
  1805. }
  1806. return FHistoryMenu[Side == osLocal][Back];
  1807. }
  1808. //---------------------------------------------------------------------------
  1809. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1810. TCustomDirView *Sender)
  1811. {
  1812. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1813. UpdateHistoryMenu(Side, true);
  1814. UpdateHistoryMenu(Side, false);
  1815. }
  1816. //---------------------------------------------------------------------------
  1817. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1818. {
  1819. // remote custom commands can be executed only if the server supports shell commands
  1820. // or have secondary shell
  1821. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1822. }
  1823. //---------------------------------------------------------------------------
  1824. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1825. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1826. {
  1827. // -1 = hidden, 0 = disabled, 1 = enabled
  1828. int Result;
  1829. TFileCustomCommand RemoteCustomCommand;
  1830. TLocalCustomCommand LocalCustomCommand;
  1831. TFileCustomCommand * NonInteractiveCustomCommand =
  1832. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1833. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1834. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1835. if (FLAGCLEAR(Command.Params, ccLocal))
  1836. {
  1837. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1838. // custom command that does not operate with files can be executed anytime ...
  1839. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1840. {
  1841. // ... but do not show such command in remote file menu
  1842. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1843. {
  1844. Result = AllowedState;
  1845. }
  1846. else
  1847. {
  1848. Result = -1;
  1849. }
  1850. }
  1851. else
  1852. {
  1853. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1854. {
  1855. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1856. }
  1857. else
  1858. {
  1859. Result = -1;
  1860. }
  1861. }
  1862. }
  1863. else
  1864. {
  1865. // custom command that does not operate with files can be executed anytime
  1866. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1867. {
  1868. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1869. }
  1870. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1871. {
  1872. if ((ListType == ccltAll) || (ListType == ccltFile))
  1873. {
  1874. // special case is "diff"-style command that can be executed over any side,
  1875. // if we have both sides
  1876. Result =
  1877. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1878. // directly (instead of EnableSelectedOperation) to pass
  1879. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1880. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1881. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1882. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1883. }
  1884. else if (ListType == ccltBoth)
  1885. {
  1886. DebugAssert(!IsLocalBrowserMode());
  1887. Result = 1;
  1888. }
  1889. else
  1890. {
  1891. Result = -1;
  1892. }
  1893. }
  1894. else
  1895. {
  1896. if ((ListType == ccltAll) ||
  1897. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1898. {
  1899. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1900. }
  1901. else
  1902. {
  1903. Result = -1;
  1904. }
  1905. }
  1906. if ((Result > 0) &&
  1907. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1908. !HasActiveTerminal())
  1909. {
  1910. Result = 0;
  1911. }
  1912. }
  1913. return Result;
  1914. }
  1915. //---------------------------------------------------------------------------
  1916. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1917. TStrings * FileList, const TCustomCommandType & ACommand,
  1918. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1919. {
  1920. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1921. {
  1922. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1923. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1924. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1925. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1926. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1927. bool Capture =
  1928. FLAGSET(ACommand.Params, ccShowResults) ||
  1929. FLAGSET(ACommand.Params, ccCopyResults) ||
  1930. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1931. TCaptureOutputEvent OutputEvent = NULL;
  1932. DebugAssert(FCapturedLog == NULL);
  1933. if (Capture)
  1934. {
  1935. FCapturedLog = new TStringList();
  1936. OutputEvent = TerminalCaptureLog;
  1937. }
  1938. try
  1939. {
  1940. if (!RemoteCustomCommand.IsFileCommand(Command))
  1941. {
  1942. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1943. OutputEvent);
  1944. }
  1945. else
  1946. {
  1947. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1948. }
  1949. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1950. {
  1951. if (FLAGSET(ACommand.Params, ccCopyResults))
  1952. {
  1953. CopyToClipboard(FCapturedLog);
  1954. }
  1955. if (FLAGSET(ACommand.Params, ccShowResults))
  1956. {
  1957. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1958. }
  1959. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1960. {
  1961. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1962. }
  1963. }
  1964. }
  1965. __finally
  1966. {
  1967. SAFE_DESTROY(FCapturedLog);
  1968. }
  1969. }
  1970. }
  1971. //---------------------------------------------------------------------------
  1972. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1973. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1974. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1975. {
  1976. DebugAssert(!IsLocalBrowserMode());
  1977. TStrings * LocalFileList = NULL;
  1978. TStrings * RemoteFileList = NULL;
  1979. TStrings * RemoteFileListFull = NULL;
  1980. try
  1981. {
  1982. if (LocalFileCommand)
  1983. {
  1984. if (ALocalFileList == NULL)
  1985. {
  1986. DebugAssert(HasDirView[osLocal]);
  1987. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1988. // directly (instead of EnableSelectedOperation) to pass
  1989. // false to FocusedFileOnlyWhenFocused
  1990. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1991. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1992. }
  1993. else
  1994. {
  1995. LocalFileList = ALocalFileList;
  1996. }
  1997. if (FileListCommand)
  1998. {
  1999. if (LocalFileList->Count != 1)
  2000. {
  2001. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  2002. }
  2003. }
  2004. else
  2005. {
  2006. if ((LocalFileList->Count != 1) &&
  2007. (FileList->Count != 1) &&
  2008. (LocalFileList->Count != FileList->Count))
  2009. {
  2010. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2011. }
  2012. }
  2013. }
  2014. UnicodeString RootTempDir;
  2015. UnicodeString TempDir;
  2016. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2017. if (!RemoteFiles)
  2018. {
  2019. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2020. }
  2021. try
  2022. {
  2023. TDateTimes RemoteFileTimes;
  2024. if (RemoteFiles)
  2025. {
  2026. RemoteFileList = FileList;
  2027. }
  2028. else
  2029. {
  2030. RemoteFileList = new TStringList();
  2031. TMakeLocalFileListParams MakeFileListParam;
  2032. MakeFileListParam.FileList = RemoteFileList;
  2033. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2034. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2035. MakeFileListParam.Recursive =
  2036. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2037. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2038. if (!MakeFileListParam.Recursive)
  2039. {
  2040. RemoteFileTimes.clear();
  2041. RemoteFileListFull = new TStringList();
  2042. MakeFileListParam.FileList = RemoteFileListFull;
  2043. MakeFileListParam.Recursive = true;
  2044. MakeFileListParam.IncludeDirs = false;
  2045. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2046. }
  2047. else
  2048. {
  2049. RemoteFileListFull = RemoteFileList;
  2050. }
  2051. }
  2052. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2053. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2054. if (!NonBlocking)
  2055. {
  2056. int Count;
  2057. if (FileListCommand)
  2058. {
  2059. Count = 1;
  2060. }
  2061. else if (LocalFileCommand)
  2062. {
  2063. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2064. }
  2065. else
  2066. {
  2067. Count = RemoteFileList->Count;
  2068. }
  2069. Progress.Start(foCustomCommand, osRemote, Count);
  2070. DebugAssert(FProgressForm != NULL);
  2071. FProgressForm->ReadOnly = true;
  2072. }
  2073. try
  2074. {
  2075. if (FileListCommand)
  2076. {
  2077. UnicodeString LocalFile;
  2078. // MakeFileList does not delimit filenames
  2079. UnicodeString FileList = MakeFileList(RemoteFileList);
  2080. if (LocalFileCommand)
  2081. {
  2082. DebugAssert(LocalFileList->Count == 1);
  2083. LocalFile = LocalFileList->Strings[0];
  2084. }
  2085. TLocalCustomCommand CustomCommand(Data,
  2086. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2087. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2088. if (NonBlocking)
  2089. {
  2090. DebugAssert(!POutput);
  2091. ExecuteShellChecked(ShellCommand);
  2092. }
  2093. else
  2094. {
  2095. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2096. }
  2097. }
  2098. else if (LocalFileCommand)
  2099. {
  2100. if (LocalFileList->Count == 1)
  2101. {
  2102. UnicodeString LocalFile = LocalFileList->Strings[0];
  2103. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2104. {
  2105. UnicodeString FileName = RemoteFileList->Strings[Index];
  2106. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2107. Progress.SetFile(FileNameForProgress);
  2108. TLocalCustomCommand CustomCommand(Data,
  2109. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2110. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2111. TOnceDoneOperation OnceDoneOperation;
  2112. // Do not unselect anything, as it won't work with two panels anyway
  2113. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2114. }
  2115. }
  2116. else if (RemoteFileList->Count == 1)
  2117. {
  2118. UnicodeString FileName = RemoteFileList->Strings[0];
  2119. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2120. {
  2121. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2122. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2123. Progress.SetFile(LocalFileNameOnly);
  2124. TLocalCustomCommand CustomCommand(
  2125. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2126. FileName, LocalFileName, L"");
  2127. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2128. TOnceDoneOperation OnceDoneOperation;
  2129. // Do not unselect anything, as it won't work with two panels anyway
  2130. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2131. }
  2132. }
  2133. else
  2134. {
  2135. if (LocalFileList->Count != RemoteFileList->Count)
  2136. {
  2137. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2138. }
  2139. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2140. {
  2141. UnicodeString FileName = RemoteFileList->Strings[Index];
  2142. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2143. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2144. Progress.SetFile(LocalFileNameOnly); // sic
  2145. TLocalCustomCommand CustomCommand(
  2146. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2147. FileName, LocalFileName, L"");
  2148. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2149. TOnceDoneOperation OnceDoneOperation;
  2150. // Do not unselect anything, as it won't work with two panels anyway
  2151. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2152. }
  2153. }
  2154. }
  2155. else
  2156. {
  2157. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2158. {
  2159. UnicodeString FileName = RemoteFileList->Strings[Index];
  2160. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2161. Progress.SetFile(FileNameForProgress);
  2162. TLocalCustomCommand CustomCommand(Data,
  2163. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2164. FileName, L"", L"");
  2165. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2166. TOnceDoneOperation OnceDoneOperation;
  2167. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2168. }
  2169. }
  2170. }
  2171. __finally
  2172. {
  2173. if (!NonBlocking)
  2174. {
  2175. Progress.Stop();
  2176. }
  2177. }
  2178. DebugAssert(!FAutoOperation);
  2179. if (!RemoteFiles)
  2180. {
  2181. TempDir = IncludeTrailingBackslash(TempDir);
  2182. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2183. {
  2184. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2185. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2186. // Skip directories, process only nested files.
  2187. // The check is redundant as FileAge fails for directories anyway.
  2188. !DirectoryExists(FileName))
  2189. {
  2190. UnicodeString RemoteDir =
  2191. UnixExtractFileDir(
  2192. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2193. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2194. TDateTime NewTime;
  2195. if (FileAge(FileName, NewTime) &&
  2196. (NewTime != RemoteFileTimes[Index]))
  2197. {
  2198. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2199. TemporaryFileCopyParam(CopyParam);
  2200. CopyParam.FileMask = L"";
  2201. FAutoOperation = true;
  2202. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2203. TemporaryFilesList->Add(FileName);
  2204. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2205. }
  2206. }
  2207. }
  2208. }
  2209. }
  2210. __finally
  2211. {
  2212. FAutoOperation = false;
  2213. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2214. {
  2215. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2216. }
  2217. }
  2218. }
  2219. __finally
  2220. {
  2221. if (RemoteFileList != FileList)
  2222. {
  2223. delete RemoteFileList;
  2224. }
  2225. if (LocalFileList != ALocalFileList)
  2226. {
  2227. delete LocalFileList;
  2228. }
  2229. if (RemoteFileListFull != RemoteFileList)
  2230. {
  2231. delete RemoteFileListFull;
  2232. }
  2233. }
  2234. }
  2235. //---------------------------------------------------------------------------
  2236. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2237. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2238. bool FileListCommand, UnicodeString * POutput)
  2239. {
  2240. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2241. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2242. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2243. {
  2244. UnicodeString FileName = SelectedFileList->Strings[Index];
  2245. if (DirectoryExists(FileName))
  2246. {
  2247. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2248. {
  2249. LocalFileList->Add(FileName);
  2250. }
  2251. if (FLAGSET(ACommand.Params, ccRecursive))
  2252. {
  2253. TMakeLocalFileListParams MakeFileListParam;
  2254. MakeFileListParam.FileList = LocalFileList.get();
  2255. MakeFileListParam.FileTimes = NULL;
  2256. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2257. MakeFileListParam.Recursive = true;
  2258. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2259. }
  2260. }
  2261. else
  2262. {
  2263. LocalFileList->Add(FileName);
  2264. }
  2265. }
  2266. UnicodeString RemotePath;
  2267. if (Terminal != NULL)
  2268. {
  2269. RemotePath = Terminal->CurrentDirectory;
  2270. }
  2271. if (FileListCommand)
  2272. {
  2273. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2274. TLocalCustomCommand CustomCommand(
  2275. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2276. L"", L"", FileList);
  2277. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2278. }
  2279. else
  2280. {
  2281. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2282. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2283. DebugAssert(FProgressForm != NULL);
  2284. FProgressForm->ReadOnly = true;
  2285. try
  2286. {
  2287. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2288. {
  2289. UnicodeString FileName = LocalFileList->Strings[Index];
  2290. Progress.SetFile(FileName);
  2291. TLocalCustomCommand CustomCommand(
  2292. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2293. FileName, L"", L"");
  2294. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2295. TOnceDoneOperation OnceDoneOperation;
  2296. Progress.Finish(FileName, true, OnceDoneOperation);
  2297. }
  2298. }
  2299. __finally
  2300. {
  2301. Progress.Stop();
  2302. }
  2303. }
  2304. }
  2305. //---------------------------------------------------------------------------
  2306. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2307. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2308. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2309. {
  2310. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2311. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2312. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2313. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2314. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2315. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2316. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2317. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2318. std::unique_ptr<UnicodeString> POutput;
  2319. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2320. FLAGSET(ACommand.Params, ccCopyResults))
  2321. {
  2322. POutput.reset(new UnicodeString());
  2323. }
  2324. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2325. FProgressSide = FCurrentSide;
  2326. if (!LocalCustomCommand.IsFileCommand(Command))
  2327. {
  2328. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2329. }
  2330. // remote files?
  2331. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2332. {
  2333. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2334. }
  2335. // local files
  2336. else
  2337. {
  2338. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2339. }
  2340. if (POutput.get() != NULL)
  2341. {
  2342. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2343. int P = POutput->Pos(sLineBreak);
  2344. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2345. {
  2346. POutput->SetLength(P - 1);
  2347. }
  2348. // Copy even empty output to clipboard
  2349. if (FLAGSET(ACommand.Params, ccCopyResults))
  2350. {
  2351. CopyToClipboard(*POutput);
  2352. }
  2353. // But do not show an empty message box.
  2354. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2355. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2356. !POutput->IsEmpty())
  2357. {
  2358. TClipboardHandler ClipboardHandler;
  2359. ClipboardHandler.Text = *POutput;
  2360. TMessageParams Params;
  2361. TQueryButtonAlias Aliases[1];
  2362. Aliases[0].Button = qaRetry;
  2363. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2364. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2365. Params.Aliases = Aliases;
  2366. Params.AliasesCount = LENOF(Aliases);
  2367. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2368. }
  2369. }
  2370. }
  2371. //---------------------------------------------------------------------------
  2372. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2373. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2374. {
  2375. TCustomCommandData Data;
  2376. UnicodeString Site;
  2377. UnicodeString RemotePath;
  2378. if (HasActiveTerminal())
  2379. {
  2380. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2381. Data = TCustomCommandData(SessionData.get());
  2382. Site = Terminal->SessionData->SessionKey;
  2383. RemotePath = Terminal->CurrentDirectory;
  2384. }
  2385. UnicodeString HelpKeyword = ACommand.HomePage;
  2386. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2387. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2388. {
  2389. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2390. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2391. {
  2392. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2393. }
  2394. else
  2395. {
  2396. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2397. }
  2398. if (!DoCustomCommandOptionsDialog(
  2399. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2400. {
  2401. Abort();
  2402. }
  2403. }
  2404. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2405. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2406. {
  2407. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2408. }
  2409. else
  2410. {
  2411. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2412. }
  2413. }
  2414. //---------------------------------------------------------------------------
  2415. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2416. const TCustomCommandType & Command)
  2417. {
  2418. DebugAssert(FCustomCommandLocalFileList != NULL);
  2419. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2420. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2421. TStrings * LocalFileList = new TStringList();
  2422. TStrings * RemoteFileList = new TStringList();
  2423. try
  2424. {
  2425. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2426. {
  2427. LocalFileList->Clear();
  2428. LocalFileList->AddObject(
  2429. FCustomCommandLocalFileList->Strings[Index],
  2430. FCustomCommandLocalFileList->Objects[Index]);
  2431. RemoteFileList->Clear();
  2432. RemoteFileList->AddObject(
  2433. FCustomCommandRemoteFileList->Strings[Index],
  2434. FCustomCommandRemoteFileList->Objects[Index]);
  2435. CustomCommand(RemoteFileList, Command, LocalFileList);
  2436. }
  2437. }
  2438. __finally
  2439. {
  2440. delete LocalFileList;
  2441. delete RemoteFileList;
  2442. }
  2443. }
  2444. //---------------------------------------------------------------------------
  2445. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2446. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2447. {
  2448. delete FCustomCommandLocalFileList;
  2449. delete FCustomCommandRemoteFileList;
  2450. // takeover ownership,
  2451. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2452. FCustomCommandLocalFileList = LocalFileList;
  2453. FCustomCommandRemoteFileList = RemoteFileList;
  2454. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2455. if (Button != NULL)
  2456. {
  2457. FCustomCommandMenu->Items->Clear();
  2458. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2459. MenuPopup(FCustomCommandMenu, Button);
  2460. }
  2461. else
  2462. {
  2463. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2464. }
  2465. }
  2466. //---------------------------------------------------------------------------
  2467. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2468. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2469. {
  2470. DebugAssert(FCapturedLog != NULL);
  2471. if ((OutputType == cotOutput) || (OutputType == cotError))
  2472. {
  2473. FCapturedLog->Add(AddedLine);
  2474. }
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2478. TOperationSide Side)
  2479. {
  2480. return
  2481. (Side == osOther) &&
  2482. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2483. }
  2484. //---------------------------------------------------------------------------
  2485. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2486. TOperationSide Side, TTBXCustomItem * Item,
  2487. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2488. {
  2489. TCustomDirView * DView = DirView(Side);
  2490. bool IsDefault = false;
  2491. if (DView->ItemFocused != NULL)
  2492. {
  2493. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2494. TResolvedDoubleClickAction DefaultAction =
  2495. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2496. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2497. }
  2498. TTBItemOptions O = Item->Options;
  2499. if (IsDefault)
  2500. {
  2501. Item->Options = O << tboDefault;
  2502. }
  2503. else
  2504. {
  2505. Item->Options = O >> tboDefault;
  2506. }
  2507. }
  2508. //---------------------------------------------------------------------------
  2509. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2510. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2511. {
  2512. TCustomDirView * DView = DirView(Side);
  2513. TListItem * Item = DView->ItemFocused;
  2514. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2515. EnableFocusedOperation[Side])
  2516. {
  2517. TPoint ClientPoint;
  2518. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2519. TPoint(0, 0) : MousePos;
  2520. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2521. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2522. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2523. }
  2524. }
  2525. //---------------------------------------------------------------------------
  2526. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2527. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2528. {
  2529. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2530. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2531. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2532. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2533. Handled = true;
  2534. }
  2535. //---------------------------------------------------------------------------
  2536. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2537. {
  2538. }
  2539. //---------------------------------------------------------------------------
  2540. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2541. {
  2542. DebugAssert(FErrorList == NULL);
  2543. if (WinConfiguration->ContinueOnError)
  2544. {
  2545. FErrorList = new TStringList();
  2546. Configuration->Usage->Inc(L"ContinuationsOnError");
  2547. }
  2548. NonVisualDataModule->StartBusy();
  2549. }
  2550. //---------------------------------------------------------------------------
  2551. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2552. {
  2553. NonVisualDataModule->EndBusy();
  2554. if (FErrorList)
  2555. {
  2556. HandleErrorList(FErrorList);
  2557. }
  2558. }
  2559. //---------------------------------------------------------------------------
  2560. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2561. const TCopyParamType & CopyParam)
  2562. {
  2563. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2564. {
  2565. Configuration->Usage->Inc(L"FileMaskUses");
  2566. }
  2567. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2568. {
  2569. Configuration->Usage->Inc(L"OperationMaskUses");
  2570. }
  2571. }
  2572. //---------------------------------------------------------------------------
  2573. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2574. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2575. {
  2576. DebugAssert(!IsLocalBrowserMode());
  2577. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2578. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2579. TTransferOperationParam DefaultParam;
  2580. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2581. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2582. switch (Param.Queue)
  2583. {
  2584. case asOn:
  2585. CopyParam.Queue = true;
  2586. break;
  2587. case asOff:
  2588. CopyParam.Queue = false;
  2589. break;
  2590. case asAuto:
  2591. default:
  2592. // keep default
  2593. break;
  2594. }
  2595. bool Result =
  2596. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2597. if (Result)
  2598. {
  2599. DebugAssert(Terminal);
  2600. bool SelectionRestored = false;
  2601. TCustomDirView * DView = NULL;
  2602. if (HasDirView[Side])
  2603. {
  2604. DView = DirView(Side);
  2605. DView->SaveSelection();
  2606. DView->SaveSelectedNames();
  2607. }
  2608. UpdateCopyParamCounters(CopyParam);
  2609. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2610. DebugAssert(FTransferResumeList == NULL);
  2611. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2612. FMoveToQueue = false;
  2613. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2614. try
  2615. {
  2616. TStrings * PermanentFileList;
  2617. std::unique_ptr<TStrings> PermanentFileListOwner;
  2618. try
  2619. {
  2620. if (Side == osLocal)
  2621. {
  2622. PermanentFileList = FileList;
  2623. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2624. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2625. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2626. if (Operation == foMove)
  2627. {
  2628. ReloadLocalDirectory();
  2629. if (DView != NULL)
  2630. {
  2631. DView->RestoreSelection();
  2632. }
  2633. SelectionRestored = true;
  2634. }
  2635. }
  2636. else
  2637. {
  2638. // Clone the file list as it may refer to current directory files,
  2639. // which get destroyed, when the source directory is reloaded after foMove operation.
  2640. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2641. // But for a hotfix, we are not going to do such a big change.
  2642. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2643. PermanentFileList = PermanentFileListOwner.get();
  2644. try
  2645. {
  2646. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2647. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2648. }
  2649. __finally
  2650. {
  2651. if (Operation == foMove)
  2652. {
  2653. if (DView != NULL)
  2654. {
  2655. DView->RestoreSelection();
  2656. }
  2657. SelectionRestored = true;
  2658. }
  2659. ReloadLocalDirectory(Param.TargetDirectory);
  2660. }
  2661. }
  2662. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2663. if (FMoveToQueue)
  2664. {
  2665. Abort();
  2666. }
  2667. }
  2668. catch (EAbort &)
  2669. {
  2670. if (FMoveToQueue)
  2671. {
  2672. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2673. DebugAssert(CopyParam.TransferSkipList == NULL);
  2674. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2675. if (TransferResumeList->Count > 0)
  2676. {
  2677. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2678. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2679. }
  2680. CopyParam.TransferSkipList = TransferResumeList.release();
  2681. // not really needed, just to keep it consistent with TransferResumeList
  2682. FTransferResumeList = NULL;
  2683. FMoveToQueue = false;
  2684. Configuration->Usage->Inc("MovesToBackground");
  2685. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2686. ClearTransferSourceSelection(Direction);
  2687. }
  2688. throw;
  2689. }
  2690. }
  2691. __finally
  2692. {
  2693. if (!SelectionRestored && (DView != NULL))
  2694. {
  2695. DView->DiscardSavedSelection();
  2696. }
  2697. FTransferResumeList = NULL;
  2698. }
  2699. }
  2700. return Result;
  2701. }
  2702. //---------------------------------------------------------------------------
  2703. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2704. TOperationSide Side, TStrings * FileList, void * Param)
  2705. {
  2706. DebugAssert(FileList->Count);
  2707. // We deliberately do not toggle alternative flag (Param), but use OR,
  2708. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2709. // shortcut of CurrentDeleteAlternativeAction
  2710. bool Alternative =
  2711. bool(Param) || UseAlternativeFunction();
  2712. bool Recycle;
  2713. if (IsSideLocalBrowser(Side))
  2714. {
  2715. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2716. }
  2717. else
  2718. {
  2719. Recycle =
  2720. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2721. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2722. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2723. }
  2724. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2725. if (!Result)
  2726. {
  2727. UnicodeString Query;
  2728. if (FileList->Count == 1)
  2729. {
  2730. if (IsSideLocalBrowser(Side))
  2731. {
  2732. Query = ExtractFileName(FileList->Strings[0]);
  2733. }
  2734. else
  2735. {
  2736. Query = UnixExtractFileName(FileList->Strings[0]);
  2737. }
  2738. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2739. }
  2740. else
  2741. {
  2742. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2743. }
  2744. TMessageParams Params(mpNeverAskAgainCheck);
  2745. Params.ImageName = L"Delete file";
  2746. unsigned int Answer =
  2747. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2748. if (Answer == qaNeverAskAgain)
  2749. {
  2750. Result = true;
  2751. if (Recycle)
  2752. {
  2753. WinConfiguration->ConfirmRecycling = false;
  2754. }
  2755. else
  2756. {
  2757. WinConfiguration->ConfirmDeleting = false;
  2758. }
  2759. }
  2760. else
  2761. {
  2762. Result = (Answer == qaOK);
  2763. }
  2764. }
  2765. if (Result)
  2766. {
  2767. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2768. }
  2769. return Result;
  2770. }
  2771. //---------------------------------------------------------------------------
  2772. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2773. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2774. {
  2775. TAutoBatch AutoBatch(this);
  2776. bool Result;
  2777. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2778. {
  2779. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2780. }
  2781. else if (Operation == foRename)
  2782. {
  2783. DebugAssert(DirView(Side)->ItemFocused);
  2784. DirView(Side)->ItemFocused->EditCaption();
  2785. Result = true;
  2786. }
  2787. else if (Operation == foDelete)
  2788. {
  2789. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2790. }
  2791. else if (Operation == foSetProperties)
  2792. {
  2793. DirView(osRemote)->SaveSelectedNames();
  2794. Result = SetProperties(Side, FileList);
  2795. }
  2796. else if (Operation == foCustomCommand)
  2797. {
  2798. DebugAssert(Param);
  2799. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2800. DirView(osRemote)->SaveSelectedNames();
  2801. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2802. CustomCommand(FileList, *Command, NULL);
  2803. Result = true;
  2804. }
  2805. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2806. {
  2807. DebugAssert(!IsSideLocalBrowser(Side));
  2808. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2809. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2810. }
  2811. else if (Operation == foLock)
  2812. {
  2813. DebugAssert(!IsSideLocalBrowser(Side));
  2814. LockFiles(FileList, true);
  2815. Result = true;
  2816. }
  2817. else if (Operation == foUnlock)
  2818. {
  2819. DebugAssert(!IsSideLocalBrowser(Side));
  2820. LockFiles(FileList, false);
  2821. Result = true;
  2822. }
  2823. else
  2824. {
  2825. DebugFail();
  2826. }
  2827. return Result;
  2828. }
  2829. //---------------------------------------------------------------------------
  2830. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2831. {
  2832. if (Side == osCurrent)
  2833. {
  2834. Side = FCurrentSide;
  2835. }
  2836. return Side;
  2837. }
  2838. //---------------------------------------------------------------------------
  2839. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2840. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2841. {
  2842. Side = GetSide(Side);
  2843. bool FullPath = IsSideLocalBrowser(Side);
  2844. TCustomDirView * ADirView = DirView(Side);
  2845. std::unique_ptr<TStringList> FileList(new TStringList());
  2846. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2847. bool ReloadProperties =
  2848. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2849. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2850. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2851. if (Result && ReloadProperties)
  2852. {
  2853. std::unique_ptr<TStringList> FileList2(new TStringList());
  2854. FileList->CaseSensitive = true;
  2855. FileList->Sort();
  2856. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2857. {
  2858. TListItem * Item = ADirView->Items->Item[Index];
  2859. UnicodeString FileName = ADirView->ItemFileName(Item);
  2860. int Index2;
  2861. if (FileList->Find(FileName, Index2))
  2862. {
  2863. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2864. }
  2865. }
  2866. LoadFilesProperties(FileList2.get());
  2867. }
  2868. return Result;
  2869. }
  2870. //---------------------------------------------------------------------------
  2871. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2872. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2873. bool NoConfirmation, void * Param)
  2874. {
  2875. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2876. {
  2877. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2878. {
  2879. DebugAssert(!IsLocalBrowserMode());
  2880. if (GetSide(Side) == osLocal)
  2881. {
  2882. Configuration->Usage->Inc(L"UploadsCommand");
  2883. }
  2884. else
  2885. {
  2886. Configuration->Usage->Inc(L"DownloadsCommand");
  2887. }
  2888. }
  2889. }
  2890. }
  2891. //---------------------------------------------------------------------------
  2892. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2893. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2894. {
  2895. TTransferOperationParam Param;
  2896. if ((WinConfiguration->Interface != ifCommander) ||
  2897. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2898. {
  2899. Flags &= ~cocShortCutHint;
  2900. }
  2901. TCustomDirView * DView = DirView(Side);
  2902. Param.Options =
  2903. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2904. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2905. if (FLAGSET(Flags, cocQueue))
  2906. {
  2907. Param.Queue = asOn;
  2908. }
  2909. else if (FLAGSET(Flags, cocNonQueue))
  2910. {
  2911. Param.Queue = asOff;
  2912. }
  2913. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2914. }
  2915. //---------------------------------------------------------------------------
  2916. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2917. {
  2918. int Count = ErrorList->Count;
  2919. try
  2920. {
  2921. if (ErrorList->Count)
  2922. {
  2923. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2924. if (MessageDialog(Message, qtError,
  2925. qaOK | qaCancel, HELP_NONE) == qaOK)
  2926. {
  2927. unsigned int Answer;
  2928. int Index = 0;
  2929. do
  2930. {
  2931. DebugAssert(Index >= 0 && Index < Count);
  2932. TQueryButtonAlias Aliases[2];
  2933. Aliases[0].Button = qaYes;
  2934. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2935. Aliases[1].Button = qaNo;
  2936. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2937. TMessageParams Params;
  2938. Params.Aliases = Aliases;
  2939. Params.AliasesCount = LENOF(Aliases);
  2940. int No = Index + 1;
  2941. UnicodeString Message = ErrorList->Strings[Index];
  2942. UnicodeString AMainInstructions;
  2943. UnicodeString Details;
  2944. if (ExtractMainInstructions(Message, AMainInstructions))
  2945. {
  2946. Details = Message;
  2947. Message = AMainInstructions;
  2948. }
  2949. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2950. Answer = MoreMessageDialog(
  2951. Message,
  2952. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2953. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2954. qaOK, HELP_NONE, &Params);
  2955. if (Answer == qaNo)
  2956. {
  2957. Index++;
  2958. }
  2959. if (Answer == qaYes)
  2960. {
  2961. Index--;
  2962. }
  2963. }
  2964. while (Answer != qaOK);
  2965. }
  2966. }
  2967. }
  2968. __finally
  2969. {
  2970. TStrings * List = ErrorList;
  2971. ErrorList = NULL;
  2972. for (int i = 0; i < Count; i++)
  2973. {
  2974. delete List->Objects[i];
  2975. }
  2976. delete List;
  2977. }
  2978. }
  2979. //---------------------------------------------------------------------------
  2980. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2981. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2982. {
  2983. // needed for checking filemasks, as there's no directory object
  2984. // associated with the file object
  2985. File->FullFileName = FullFileName;
  2986. TFileMasks::TParams MaskParams;
  2987. MaskParams.Size = File->Size;
  2988. MaskParams.Modification = File->Modification;
  2989. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2990. }
  2991. //---------------------------------------------------------------------------
  2992. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2993. {
  2994. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2995. Side = GetSide(Side);
  2996. TCustomDirView * DView = DirView(Side);
  2997. TListItem * FocusedItem = DView->ItemFocused;
  2998. UnicodeString Name;
  2999. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  3000. {
  3001. Name = DView->ItemFileName(FocusedItem);
  3002. }
  3003. else
  3004. {
  3005. Name = LoadStr(NEW_FILE);
  3006. }
  3007. UnicodeString Names = Name;
  3008. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3009. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3010. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  3011. {
  3012. while (!Names.IsEmpty())
  3013. {
  3014. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3015. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3016. UnicodeString TargetFileName;
  3017. UnicodeString LocalFileName;
  3018. UnicodeString RootTempDir;
  3019. UnicodeString TempDir;
  3020. UnicodeString RemoteDirectory;
  3021. bool ExistingFile = false;
  3022. if (!IsSideLocalBrowser(Side))
  3023. {
  3024. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3025. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3026. if ((File.get() != NULL) &&
  3027. !File->IsDirectory)
  3028. {
  3029. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3030. ExistingFile = true;
  3031. }
  3032. File.reset(NULL);
  3033. if (!ExistingFile)
  3034. {
  3035. RemoteDirectory = UnixExtractFilePath(Name);
  3036. TemporaryDirectoryForRemoteFiles(
  3037. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3038. TargetFileName = UnixExtractFileName(Name);
  3039. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3040. LocalFileName = TempDir +
  3041. // We probably do not want to trim the VMS version here
  3042. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3043. }
  3044. }
  3045. else
  3046. {
  3047. if (ExtractFilePath(Name).IsEmpty())
  3048. {
  3049. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3050. }
  3051. else
  3052. {
  3053. LocalFileName = ExpandFileName(Name);
  3054. }
  3055. TargetFileName = ExtractFileName(Name);
  3056. }
  3057. if (!ExistingFile)
  3058. {
  3059. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3060. if (NewFile)
  3061. {
  3062. int File = FileCreate(ApiPath(LocalFileName));
  3063. if (File < 0)
  3064. {
  3065. if (!RootTempDir.IsEmpty())
  3066. {
  3067. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3068. }
  3069. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3070. }
  3071. else
  3072. {
  3073. FileClose(File);
  3074. }
  3075. }
  3076. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3077. const TEditorData * ExternalEditor = NULL;
  3078. TFileMasks::TParams MaskParams; // size not known
  3079. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3080. false, MaskParams);
  3081. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3082. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3083. }
  3084. }
  3085. }
  3086. }
  3087. //---------------------------------------------------------------------------
  3088. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3089. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3090. {
  3091. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3092. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3093. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3094. return
  3095. ((ExecuteFileBy == efInternalEditor)) ||
  3096. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3097. }
  3098. //---------------------------------------------------------------------------
  3099. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3100. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3101. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3102. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3103. {
  3104. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3105. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3106. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3107. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3108. Side = GetSide(Side);
  3109. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3110. if (!IsSideLocalBrowser(Side))
  3111. {
  3112. Data->Terminal = Terminal;
  3113. Data->Queue = Queue;
  3114. Data->SessionData = CloneCurrentSessionData();
  3115. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3116. Data->RemoteDirectory = RemoteDirectory;
  3117. Data->SessionName = Terminal->SessionData->SessionName;
  3118. Data->LocalRootDirectory = LocalRootDirectory;
  3119. Data->OriginalFileName = OriginalFileName;
  3120. Data->Command = L""; // will be changed later for external editor
  3121. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3122. if (WinConfiguration->EditorCheckNotModified)
  3123. {
  3124. Data->SourceTimestamp = SourceTimestamp;
  3125. }
  3126. }
  3127. if (ExecuteFileBy == efInternalEditor)
  3128. {
  3129. if (!IsSideLocalBrowser(Side))
  3130. {
  3131. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3132. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3133. TForm * Editor;
  3134. try
  3135. {
  3136. Editor =
  3137. ShowEditorForm(
  3138. FileName, this,
  3139. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3140. SaveAllInternalEditors, AnyInternalEditorModified,
  3141. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3142. }
  3143. catch(...)
  3144. {
  3145. if (!LocalRootDirectory.IsEmpty())
  3146. {
  3147. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3148. }
  3149. throw;
  3150. }
  3151. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3152. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3153. }
  3154. else
  3155. {
  3156. DebugAssert(!StandaloneOperation);
  3157. TForm * Editor =
  3158. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3159. SaveAllInternalEditors, AnyInternalEditorModified,
  3160. L"", false, SessionColor, -1, NewFile);
  3161. FLocalEditors->Add(Editor);
  3162. }
  3163. }
  3164. else
  3165. {
  3166. HANDLE Process;
  3167. if (ExecuteFileBy == efExternalEditor)
  3168. {
  3169. UnicodeString Program, Params, Dir;
  3170. Data->Command = ExternalEditor->ExternalEditor;
  3171. ReformatFileNameCommand(Data->Command);
  3172. SplitCommand(Data->Command, Program, Params, Dir);
  3173. Params = ExpandFileNameCommand(Params, FileName);
  3174. Program = ExpandEnvironmentVariables(Program);
  3175. if (!ExecuteShell(Program, Params, Process))
  3176. {
  3177. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3178. }
  3179. }
  3180. else
  3181. {
  3182. DebugAssert(!IsSideLocalBrowser(Side));
  3183. if (!ExecuteShell(FileName, L"", Process))
  3184. {
  3185. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3186. }
  3187. }
  3188. if (IsSideLocalBrowser(Side) ||
  3189. ((ExecuteFileBy == efShell) &&
  3190. !WinConfiguration->Editor.SDIShellEditor) ||
  3191. ((ExecuteFileBy == efExternalEditor) &&
  3192. !ExternalEditor->SDIExternalEditor))
  3193. {
  3194. // no need for handle
  3195. if (Process != NULL)
  3196. {
  3197. DebugCheck(CloseHandle(Process));
  3198. }
  3199. Process = INVALID_HANDLE_VALUE;
  3200. }
  3201. else
  3202. {
  3203. if (Process == NULL)
  3204. {
  3205. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3206. }
  3207. }
  3208. if (!IsSideLocalBrowser(Side))
  3209. {
  3210. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3211. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3212. }
  3213. }
  3214. }
  3215. //---------------------------------------------------------------------------
  3216. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3217. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3218. void * /*Arg*/)
  3219. {
  3220. if (Token != NULL)
  3221. {
  3222. EditorFormFileSave(static_cast<TForm *>(Token));
  3223. }
  3224. }
  3225. //---------------------------------------------------------------------------
  3226. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3227. {
  3228. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3229. {
  3230. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3231. }
  3232. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3233. }
  3234. //---------------------------------------------------------------------------
  3235. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3236. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3237. void * Arg)
  3238. {
  3239. if ((Token != NULL) &&
  3240. IsEditorFormModified(static_cast<TForm *>(Token)))
  3241. {
  3242. *static_cast<bool *>(Arg) = true;
  3243. }
  3244. }
  3245. //---------------------------------------------------------------------------
  3246. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3247. TObject * /*Sender*/, bool & Modified)
  3248. {
  3249. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3250. {
  3251. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3252. {
  3253. Modified = true;
  3254. }
  3255. }
  3256. if (!Modified)
  3257. {
  3258. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3259. }
  3260. }
  3261. //---------------------------------------------------------------------------
  3262. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3263. {
  3264. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3265. }
  3266. //---------------------------------------------------------------------------
  3267. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3268. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3269. UnicodeString & Result, UnicodeString & RootDirectory)
  3270. {
  3271. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3272. {
  3273. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3274. Result = RootDirectory;
  3275. }
  3276. else
  3277. {
  3278. RootDirectory = L"";
  3279. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3280. Result = IncludeTrailingBackslash(Result);
  3281. }
  3282. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3283. {
  3284. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3285. }
  3286. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3287. {
  3288. UnicodeString ARemoteDirectory = RemoteDirectory;
  3289. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3290. {
  3291. ARemoteDirectory.Delete(1, 1);
  3292. }
  3293. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3294. }
  3295. if (!ForceDirectories(ApiPath(Result)))
  3296. {
  3297. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3298. }
  3299. }
  3300. //---------------------------------------------------------------------------
  3301. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3302. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3303. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3304. {
  3305. DebugAssert(!IsLocalBrowserMode());
  3306. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3307. if (ForceText)
  3308. {
  3309. CopyParam.TransferMode = tmAscii;
  3310. }
  3311. TemporaryFileCopyParam(CopyParam);
  3312. if (TempDir.IsEmpty())
  3313. {
  3314. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3315. }
  3316. DebugAssert(!FAutoOperation);
  3317. FAutoOperation = AutoOperation;
  3318. Terminal->ExceptionOnFail = true;
  3319. try
  3320. {
  3321. try
  3322. {
  3323. // turn off confirmations, as for MDI editors we may possibly download
  3324. // the same file over
  3325. Terminal->CopyToLocal(
  3326. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3327. if (GetTargetNames)
  3328. {
  3329. for (int i = 0; i < FileList->Count; i++)
  3330. {
  3331. FileList->Strings[i] =
  3332. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3333. }
  3334. }
  3335. }
  3336. catch(...)
  3337. {
  3338. if (!RootTempDir.IsEmpty())
  3339. {
  3340. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3341. }
  3342. throw;
  3343. }
  3344. }
  3345. __finally
  3346. {
  3347. FAutoOperation = false;
  3348. Terminal->ExceptionOnFail = false;
  3349. }
  3350. }
  3351. //---------------------------------------------------------------------------
  3352. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3353. {
  3354. // Do not waste time checking for default editor list the next time,
  3355. // or testing if default editor is not notepad.
  3356. bool TryNextTime = false;
  3357. UnicodeString UsageState;
  3358. if (!WinConfiguration->EditorList->IsDefaultList())
  3359. {
  3360. UsageState = "H";
  3361. }
  3362. else
  3363. {
  3364. UnicodeString Executable;
  3365. UnicodeString ExecutableDescription;
  3366. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3367. {
  3368. UnicodeString Message =
  3369. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3370. unsigned int Answer =
  3371. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3372. if (Answer != qaOK)
  3373. {
  3374. UsageState = "R";
  3375. }
  3376. else
  3377. {
  3378. UsageState = "A";
  3379. TEditorData EditorData;
  3380. EditorData.Editor = edExternal;
  3381. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3382. EditorData.ExternalEditorOptionsAutodetect();
  3383. TEditorList EditorList;
  3384. EditorList = *WinConfiguration->EditorList;
  3385. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3386. WinConfiguration->EditorList = &EditorList;
  3387. }
  3388. }
  3389. }
  3390. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3391. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3392. }
  3393. //---------------------------------------------------------------------------
  3394. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3395. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3396. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3397. {
  3398. if (ExecuteFileBy == efDefaultEditor)
  3399. {
  3400. if (!WinConfiguration->OfferedEditorAutoConfig)
  3401. {
  3402. EditorAutoConfig();
  3403. }
  3404. const TEditorPreferences * Editor =
  3405. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3406. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3407. {
  3408. ExecuteFileBy = efInternalEditor;
  3409. ExternalEditor = NULL;
  3410. }
  3411. else if (Editor->Data->Editor == edOpen)
  3412. {
  3413. ExecuteFileBy = efShell;
  3414. ExternalEditor = NULL;
  3415. }
  3416. else
  3417. {
  3418. ExecuteFileBy = efExternalEditor;
  3419. ExternalEditor = Editor->Data;
  3420. }
  3421. }
  3422. }
  3423. //---------------------------------------------------------------------------
  3424. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3425. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3426. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3427. {
  3428. UnicodeString OriginalFileName;
  3429. UnicodeString LocalRootDirectory;
  3430. UnicodeString RemoteDirectory;
  3431. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3432. IsSideLocalBrowser(Side), MaskParams);
  3433. UnicodeString Counter;
  3434. UnicodeString LocalFileName;
  3435. bool Handled = false;
  3436. if (!IsSideLocalBrowser(Side))
  3437. {
  3438. // We need to trim VMS version here, so that we use name without version
  3439. // when uploading back to create a new version of the file
  3440. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3441. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3442. TObject * Token = NULL;
  3443. UnicodeString LocalDirectory;
  3444. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3445. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3446. LocalDirectory))
  3447. {
  3448. if (Token != NULL)
  3449. {
  3450. TForm * Form = dynamic_cast<TForm *>(Token);
  3451. if (Form->WindowState == wsMinimized)
  3452. {
  3453. ShowWindow(Form->Handle, SW_RESTORE);
  3454. }
  3455. else
  3456. {
  3457. Form->SetFocus();
  3458. }
  3459. Handled = true;
  3460. }
  3461. else
  3462. {
  3463. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3464. }
  3465. }
  3466. if (!Handled)
  3467. {
  3468. TStringList * FileList1 = new TStringList();
  3469. try
  3470. {
  3471. FileList1->AddObject(FullFileName, Object);
  3472. TemporarilyDownloadFiles(FileList1,
  3473. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3474. LocalRootDirectory, LocalDirectory, true, true, false);
  3475. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3476. }
  3477. __finally
  3478. {
  3479. delete FileList1;
  3480. }
  3481. switch (ExecuteFileBy)
  3482. {
  3483. case efShell:
  3484. Counter = "RemoteFilesExecuted";
  3485. break;
  3486. case efInternalEditor:
  3487. Counter = "RemoteFilesOpenedInInternalEditor";
  3488. break;
  3489. case efExternalEditor:
  3490. Counter = "RemoteFilesOpenedInExternalEditor";
  3491. break;
  3492. default:
  3493. DebugFail();
  3494. }
  3495. }
  3496. }
  3497. else
  3498. {
  3499. LocalFileName = FullFileName;
  3500. OriginalFileName = ExtractFileName(FullFileName);
  3501. switch (ExecuteFileBy)
  3502. {
  3503. case efShell:
  3504. Counter = "LocalFilesExecuted";
  3505. break;
  3506. case efInternalEditor:
  3507. Counter = "LocalFilesOpenedInInternalEditor";
  3508. break;
  3509. case efExternalEditor:
  3510. Counter = "LocalFilesOpenedInExternalEditor";
  3511. break;
  3512. default:
  3513. DebugFail();
  3514. }
  3515. }
  3516. if (!Handled)
  3517. {
  3518. Configuration->Usage->Inc(Counter);
  3519. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3520. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3521. }
  3522. }
  3523. //---------------------------------------------------------------------------
  3524. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3525. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3526. bool AllSelected, bool OnFocused)
  3527. {
  3528. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3529. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3530. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3531. Side = GetSide(Side);
  3532. TCustomDirView * DView = DirView(Side);
  3533. TStrings * FileList =
  3534. AllSelected ?
  3535. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3536. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3537. try
  3538. {
  3539. DebugAssert(AllSelected || (FileList->Count == 1));
  3540. for (int i = 0; i < FileList->Count; i++)
  3541. {
  3542. UnicodeString ListFileName = FileList->Strings[i];
  3543. UnicodeString FileNameOnly =
  3544. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3545. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3546. if (!DView->ItemIsDirectory(Item))
  3547. {
  3548. UnicodeString FullFileName;
  3549. if (!IsSideLocalBrowser(Side))
  3550. {
  3551. FullFileName = DirView(Side)->Path + ListFileName;
  3552. }
  3553. else
  3554. {
  3555. FullFileName = ListFileName;
  3556. }
  3557. TObject * Object = FileList->Objects[i];
  3558. TFileMasks::TParams MaskParams;
  3559. MaskParams.Size = DView->ItemFileSize(Item);
  3560. TDateTimePrecision Precision;
  3561. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3562. std::unique_ptr<TRemoteFile> File;
  3563. if (!IsSideLocalBrowser(Side))
  3564. {
  3565. File.reset(Terminal->TryReadFile(FullFileName));
  3566. if (File.get() != NULL)
  3567. {
  3568. File->FullFileName = FullFileName;
  3569. Object = File.get();
  3570. MaskParams.Size = File->Size;
  3571. MaskParams.Modification = File->Modification;
  3572. }
  3573. }
  3574. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3575. }
  3576. }
  3577. }
  3578. __finally
  3579. {
  3580. delete FileList;
  3581. }
  3582. }
  3583. //---------------------------------------------------------------------------
  3584. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3585. {
  3586. CopyParam.FileNameCase = ncNoChange;
  3587. CopyParam.PreserveRights = false;
  3588. CopyParam.PreserveReadOnly = false;
  3589. CopyParam.ReplaceInvalidChars = true;
  3590. CopyParam.IncludeFileMask = TFileMasks();
  3591. CopyParam.NewerOnly = false;
  3592. CopyParam.FileMask = L"";
  3593. }
  3594. //---------------------------------------------------------------------------
  3595. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3596. {
  3597. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3598. }
  3599. //---------------------------------------------------------------------------
  3600. class TEditorUploadQueueItem : public TUploadQueueItem
  3601. {
  3602. public:
  3603. __fastcall TEditorUploadQueueItem(
  3604. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  3605. const TCopyParamType * CopyParam, int Params) :
  3606. TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, true, false)
  3607. {
  3608. }
  3609. protected:
  3610. virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  3611. {
  3612. TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
  3613. TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
  3614. }
  3615. };
  3616. //---------------------------------------------------------------------------
  3617. void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
  3618. {
  3619. if (WinConfiguration->EditorCheckNotModified) // optimization
  3620. {
  3621. UnicodeString RemoteFilePath;
  3622. {
  3623. TGuard Guard(FEditorManager->Section);
  3624. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3625. if (DebugAlwaysTrue(Data != NULL) &&
  3626. EditorCheckNotModified(Data))
  3627. {
  3628. RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3629. }
  3630. }
  3631. if (!RemoteFilePath.IsEmpty())
  3632. {
  3633. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
  3634. if (File.get() != NULL)
  3635. {
  3636. TGuard Guard(FEditorManager->Section);
  3637. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3638. if (DebugAlwaysTrue(Data != NULL))
  3639. {
  3640. Data->SourceTimestamp = File->Modification;
  3641. AppLogFmt(L"Uploaded edited remote file timestamp: %s: ", (StandardTimestamp(Data->SourceTimestamp)));
  3642. }
  3643. }
  3644. }
  3645. }
  3646. }
  3647. //---------------------------------------------------------------------------
  3648. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3649. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3650. {
  3651. TTerminalManager * Manager = TTerminalManager::Instance();
  3652. if ((Manager->ActiveTerminal == Data->Terminal) &&
  3653. Manager->ScheduleTerminalReconnnect(Data->Terminal))
  3654. {
  3655. AppLog(L"Scheduled session reconnect, will retry the upload later");
  3656. Retry = true;
  3657. }
  3658. else if (!IsActiveTerminal(Data->Terminal))
  3659. {
  3660. if (!NonVisualDataModule->Busy)
  3661. {
  3662. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3663. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3664. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3665. if (Data->Terminal == NULL)
  3666. {
  3667. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3668. if (SameSiteTerminal != NULL)
  3669. {
  3670. UnicodeString Message =
  3671. FMTLOAD(EDIT_SESSION_REATTACH,
  3672. (FileNameOnly, Data->SessionName, FileNameOnly));
  3673. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3674. {
  3675. Data->Terminal = SameSiteTerminal;
  3676. Data->Queue = Manager->FindQueueForTerminal(SameSiteTerminal);
  3677. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3678. // We might also overwrite session data
  3679. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3680. }
  3681. else
  3682. {
  3683. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3684. Abort();
  3685. }
  3686. }
  3687. }
  3688. // Foreground session should reconnect itself (except for those disconnected explicitly by the user)
  3689. else if (DebugAlwaysTrue(Terminal->Disconnected || (Terminal != Data->Terminal)))
  3690. {
  3691. UnicodeString Message =
  3692. MainInstructions(
  3693. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3694. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3695. {
  3696. if (Terminal->Disconnected)
  3697. {
  3698. Manager->ActiveSession = Terminal;
  3699. ReconnectSession();
  3700. DebugAssert(Terminal == Data->Terminal);
  3701. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3702. }
  3703. else
  3704. {
  3705. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3706. }
  3707. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3708. }
  3709. else
  3710. {
  3711. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3712. Abort();
  3713. }
  3714. }
  3715. }
  3716. if (!IsActiveTerminal(Data->Terminal))
  3717. {
  3718. Configuration->Usage->Inc(L"EditInactiveSession");
  3719. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3720. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3721. (ExtractFileName(FileName), Data->SessionName)));
  3722. }
  3723. }
  3724. if (!Retry && EditorCheckNotModified(Data))
  3725. {
  3726. if (NonVisualDataModule->Busy)
  3727. {
  3728. Retry = true;
  3729. }
  3730. else
  3731. {
  3732. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3733. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3734. if (File.get() != NULL)
  3735. {
  3736. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3737. if (File->Modification != Data->SourceTimestamp)
  3738. {
  3739. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3740. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3741. {
  3742. Abort();
  3743. }
  3744. }
  3745. }
  3746. }
  3747. }
  3748. if (!Retry)
  3749. {
  3750. TStrings * FileList = new TStringList();
  3751. try
  3752. {
  3753. FileList->Add(FileName);
  3754. // Consider using the same settings (preset) as when the file was downloaded.
  3755. // More over this does not reflect the actual session that will do the upload.
  3756. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3757. TemporaryFileCopyParam(CopyParam);
  3758. if (Data->ForceText)
  3759. {
  3760. CopyParam.TransferMode = tmAscii;
  3761. }
  3762. // so i do not need to worry if masking algorithm works in all cases
  3763. // ("" means "copy file name", no masking is actually done)
  3764. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3765. {
  3766. CopyParam.FileMask = L"";
  3767. }
  3768. else
  3769. {
  3770. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3771. }
  3772. int Params = cpNoConfirmation | cpTemporary;
  3773. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3774. {
  3775. DebugAssert(Data->Queue != NULL);
  3776. TQueueItem * QueueItem =
  3777. new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
  3778. QueueItem->CompleteEvent = UploadCompleteEvent;
  3779. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3780. }
  3781. else
  3782. {
  3783. if (NonVisualDataModule->Busy)
  3784. {
  3785. Retry = true;
  3786. }
  3787. else
  3788. {
  3789. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3790. SetEvent(UploadCompleteEvent);
  3791. }
  3792. }
  3793. }
  3794. __finally
  3795. {
  3796. delete FileList;
  3797. }
  3798. }
  3799. }
  3800. //---------------------------------------------------------------------------
  3801. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3802. const UnicodeString & FileName, TEditedFileData * Data)
  3803. {
  3804. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3805. DebugAssert(!NonVisualDataModule->Busy);
  3806. if (!IsActiveTerminal(Data->Terminal))
  3807. {
  3808. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3809. (ExtractFileName(FileName), Data->SessionName)));
  3810. }
  3811. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3812. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3813. NonVisualDataModule->StartBusy();
  3814. try
  3815. {
  3816. std::unique_ptr<TRemoteFile> File;
  3817. UnicodeString RemoteFileName =
  3818. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3819. Terminal->ExceptionOnFail = true;
  3820. try
  3821. {
  3822. File.reset(Terminal->ReadFile(RemoteFileName));
  3823. if (!File->HaveFullFileName)
  3824. {
  3825. File->FullFileName = RemoteFileName;
  3826. }
  3827. }
  3828. __finally
  3829. {
  3830. Terminal->ExceptionOnFail = false;
  3831. }
  3832. std::unique_ptr<TStrings> FileList(new TStringList());
  3833. FileList->AddObject(RemoteFileName, File.get());
  3834. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3835. UnicodeString TempDir = ExtractFilePath(FileName);
  3836. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3837. if (EditorCheckNotModified(Data))
  3838. {
  3839. Data->SourceTimestamp = File->Modification;
  3840. }
  3841. // sanity check, the target file name should be still the same
  3842. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3843. }
  3844. __finally
  3845. {
  3846. NonVisualDataModule->EndBusy();
  3847. // it actually may not exist anymore...
  3848. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3849. }
  3850. }
  3851. //---------------------------------------------------------------------------
  3852. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3853. const TEditedFileData * Data, bool & KeepOpen)
  3854. {
  3855. // Command is set for external editors only (not for "shell" open).
  3856. if (!Data->Command.IsEmpty())
  3857. {
  3858. bool AnyFound = false;
  3859. bool AnyMDI = false;
  3860. bool AnyNonMDI = false;
  3861. bool AnyDetect = false;
  3862. TEditorList * EditorList = new TEditorList();
  3863. try
  3864. {
  3865. *EditorList = *WinConfiguration->EditorList;
  3866. for (int i = 0; i < EditorList->Count; i++)
  3867. {
  3868. const TEditorPreferences * Editor = EditorList->Editors[i];
  3869. if ((Editor->Data->Editor == edExternal) &&
  3870. (Editor->Data->ExternalEditor == Data->Command))
  3871. {
  3872. AnyFound = true;
  3873. if (Editor->Data->SDIExternalEditor)
  3874. {
  3875. AnyNonMDI = true;
  3876. if (Editor->Data->DetectMDIExternalEditor)
  3877. {
  3878. AnyDetect = true;
  3879. }
  3880. }
  3881. else
  3882. {
  3883. AnyMDI = true;
  3884. }
  3885. }
  3886. }
  3887. bool EnableMDI = false;
  3888. bool DisableDetect = false;
  3889. if (AnyMDI)
  3890. {
  3891. KeepOpen = true;
  3892. if (AnyNonMDI)
  3893. {
  3894. // there is at least one instance of the editor with MDI support enabled,
  3895. // and one with disabled, enable it for all instances
  3896. EnableMDI = true;
  3897. }
  3898. }
  3899. else if (AnyFound && !AnyDetect)
  3900. {
  3901. // at least once instance found but all have MDI autodetection disabled
  3902. // => close the file (default action)
  3903. }
  3904. else
  3905. {
  3906. // no instance of the editor has MDI support enabled
  3907. TMessageParams Params;
  3908. if (AnyFound)
  3909. {
  3910. // there is at least one instance of the editor
  3911. Params.Params |= mpNeverAskAgainCheck;
  3912. }
  3913. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3914. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3915. switch (Answer)
  3916. {
  3917. case qaNeverAskAgain:
  3918. DisableDetect = true;
  3919. break;
  3920. case qaNo:
  3921. EnableMDI = true;
  3922. KeepOpen = true;
  3923. break;
  3924. }
  3925. }
  3926. if (AnyFound && (EnableMDI || DisableDetect))
  3927. {
  3928. bool Changed = false;
  3929. for (int i = 0; i < EditorList->Count; i++)
  3930. {
  3931. const TEditorPreferences * Editor = EditorList->Editors[i];
  3932. if ((Editor->Data->Editor == edExternal) &&
  3933. (Editor->Data->ExternalEditor == Data->Command) &&
  3934. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3935. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3936. {
  3937. Changed = true;
  3938. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3939. if (EnableMDI)
  3940. {
  3941. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3942. }
  3943. if (DisableDetect)
  3944. {
  3945. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3946. }
  3947. EditorList->Change(i, UpdatedEditor);
  3948. }
  3949. }
  3950. if (Changed)
  3951. {
  3952. WinConfiguration->EditorList = EditorList;
  3953. }
  3954. }
  3955. }
  3956. __finally
  3957. {
  3958. delete EditorList;
  3959. }
  3960. }
  3961. else
  3962. {
  3963. // "open" case
  3964. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3965. qaOK, HELP_APP_EARLY_CLOSED);
  3966. }
  3967. }
  3968. //---------------------------------------------------------------------------
  3969. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3970. {
  3971. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3972. }
  3973. //---------------------------------------------------------------------------
  3974. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3975. {
  3976. SideEnter(osRemote);
  3977. }
  3978. //---------------------------------------------------------------------------
  3979. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3980. {
  3981. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3982. SideEnter(osRemote);
  3983. }
  3984. //---------------------------------------------------------------------------
  3985. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3986. {
  3987. FCurrentSide = Side;
  3988. if (Visible)
  3989. {
  3990. UpdateControls();
  3991. }
  3992. }
  3993. //---------------------------------------------------------------------------
  3994. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3995. {
  3996. VisualiseOperationFinished(Side, FileName, Success);
  3997. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3998. {
  3999. FDeletedFiles->Add(FileName);
  4000. }
  4001. }
  4002. //---------------------------------------------------------------------------
  4003. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  4004. TStrings * FileList, bool Alternative)
  4005. {
  4006. TCustomDirView * DView = DirView(Side);
  4007. DView->SaveSelection();
  4008. DView->SaveSelectedNames();
  4009. DebugAssert(!FAlternativeDelete);
  4010. FAlternativeDelete = Alternative;
  4011. try
  4012. {
  4013. if (!IsSideLocalBrowser(Side))
  4014. {
  4015. DebugAssert(Terminal != NULL);
  4016. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  4017. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  4018. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  4019. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  4020. int Params = FLAGMASK(Alternative, dfAlternative);
  4021. try
  4022. {
  4023. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  4024. DebugAssert(FDeletedFiles == NULL);
  4025. FDeletedFiles = DeletedFiles.get();
  4026. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  4027. FOnFileOperationFinished = FileDeleted;
  4028. FMoveToQueue = false;
  4029. Terminal->DeleteFiles(FileList, Params);
  4030. // Probably not needed for deleting, just for consistency with transfer code
  4031. if (FMoveToQueue)
  4032. {
  4033. Abort();
  4034. }
  4035. }
  4036. catch (EAbort &)
  4037. {
  4038. if (FMoveToQueue)
  4039. {
  4040. DeletedFiles->Sorted = true;
  4041. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  4042. {
  4043. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  4044. {
  4045. // We should always be deleting the first item => what can be used to optimize this code, if needed
  4046. DebugAssert(Index == 0);
  4047. PermanentFileList->Delete(Index);
  4048. Index--;
  4049. }
  4050. }
  4051. Configuration->Usage->Inc("MovesToBackgroundDelete");
  4052. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  4053. AddQueueItem(Queue, QueueItem, Terminal);
  4054. ClearOperationSelection(osRemote);
  4055. }
  4056. }
  4057. }
  4058. else
  4059. {
  4060. if (IsLocalBrowserMode())
  4061. {
  4062. Configuration->Usage->Inc(L"LocalLocalDeletes");
  4063. }
  4064. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  4065. FProgressSide = FCurrentSide;
  4066. try
  4067. {
  4068. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  4069. }
  4070. __finally
  4071. {
  4072. ReloadLocalDirectory();
  4073. }
  4074. }
  4075. FAlternativeDelete = false;
  4076. }
  4077. catch(...)
  4078. {
  4079. FAlternativeDelete = false;
  4080. DView->DiscardSavedSelection();
  4081. throw;
  4082. }
  4083. DView->RestoreSelection();
  4084. }
  4085. //---------------------------------------------------------------------------
  4086. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4087. {
  4088. DebugAssert(Terminal);
  4089. DebugAssert(!IsLocalBrowserMode());
  4090. RemoteDirView->SaveSelection();
  4091. RemoteDirView->SaveSelectedNames();
  4092. try
  4093. {
  4094. if (Lock)
  4095. {
  4096. Terminal->LockFiles(FileList);
  4097. }
  4098. else
  4099. {
  4100. Terminal->UnlockFiles(FileList);
  4101. }
  4102. }
  4103. catch(...)
  4104. {
  4105. RemoteDirView->DiscardSavedSelection();
  4106. throw;
  4107. }
  4108. RemoteDirView->RestoreSelection();
  4109. }
  4110. //---------------------------------------------------------------------------
  4111. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4112. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4113. bool NoConfirmation, bool Move)
  4114. {
  4115. DebugAssert(Terminal != NULL);
  4116. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4117. DebugAssert(!IsLocalBrowserMode());
  4118. if (Session == NULL)
  4119. {
  4120. Session = Terminal;
  4121. }
  4122. if (Session == Terminal)
  4123. {
  4124. if (RemoteDriveView->DropTarget != NULL)
  4125. {
  4126. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4127. }
  4128. else if (RemoteDirView->DropTarget != NULL)
  4129. {
  4130. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4131. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4132. }
  4133. else
  4134. {
  4135. Target = RemoteDirView->Path;
  4136. }
  4137. }
  4138. else
  4139. {
  4140. Target = GetSessionPath(Session, osRemote);
  4141. }
  4142. Target = UnixIncludeTrailingBackslash(Target);
  4143. if (FileList->Count == 1)
  4144. {
  4145. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4146. }
  4147. else
  4148. {
  4149. FileMask = AnyMask;
  4150. }
  4151. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4152. bool Result = true;
  4153. if (!NoConfirmation)
  4154. {
  4155. bool Multi = (FileList->Count > 1);
  4156. if (Move)
  4157. {
  4158. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4159. }
  4160. else
  4161. {
  4162. std::unique_ptr<TStrings> Sessions(new TStringList);
  4163. std::unique_ptr<TStrings> Directories(new TStringList);
  4164. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4165. for (int Index = 0; Index < SessionList->Count; Index++)
  4166. {
  4167. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4168. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4169. {
  4170. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4171. Directories->Add(GetSessionPath(ASession, osRemote));
  4172. }
  4173. }
  4174. TDirectRemoteCopy AllowDirectCopy;
  4175. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4176. {
  4177. DebugAssert(DirectCopy);
  4178. AllowDirectCopy = drcAllow;
  4179. }
  4180. else if (Terminal->IsCapable[fcSecondaryShell])
  4181. {
  4182. DebugAssert(DirectCopy);
  4183. AllowDirectCopy = drcConfirmCommandSession;
  4184. }
  4185. else
  4186. {
  4187. DebugAssert(!DirectCopy);
  4188. AllowDirectCopy = drcDisallow;
  4189. }
  4190. void * ASession = Session;
  4191. Result = DoRemoteCopyDialog(
  4192. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4193. Session = static_cast<TManagedTerminal *>(ASession);
  4194. }
  4195. }
  4196. return Result;
  4197. }
  4198. //---------------------------------------------------------------------------
  4199. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4200. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4201. {
  4202. DebugAssert(!IsLocalBrowserMode());
  4203. bool DirectCopy;
  4204. UnicodeString Target, FileMask;
  4205. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4206. if (Result)
  4207. {
  4208. if (!Move && !DirectCopy)
  4209. {
  4210. Configuration->Usage->Inc("RemoteCopyTemp");
  4211. UnicodeString RootTempDir;
  4212. UnicodeString TempDir;
  4213. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4214. TStrings * TemporaryFilesList = new TStringList();
  4215. try
  4216. {
  4217. TMakeLocalFileListParams MakeFileListParam;
  4218. MakeFileListParam.FileList = TemporaryFilesList;
  4219. MakeFileListParam.FileTimes = NULL;
  4220. MakeFileListParam.IncludeDirs = true;
  4221. MakeFileListParam.Recursive = false;
  4222. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4223. TTerminalManager::Instance()->ActiveSession = Session;
  4224. if (TemporaryFilesList->Count > 0)
  4225. {
  4226. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4227. CopyParam.FileMask = FileMask;
  4228. DebugAssert(!FAutoOperation);
  4229. FAutoOperation = true;
  4230. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4231. }
  4232. }
  4233. __finally
  4234. {
  4235. delete TemporaryFilesList;
  4236. FAutoOperation = false;
  4237. if (!RootTempDir.IsEmpty())
  4238. {
  4239. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4240. }
  4241. }
  4242. }
  4243. else
  4244. {
  4245. RemoteDirView->SaveSelection();
  4246. RemoteDirView->SaveSelectedNames();
  4247. try
  4248. {
  4249. if (Move)
  4250. {
  4251. Configuration->Usage->Inc("RemoteMove");
  4252. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4253. }
  4254. else
  4255. {
  4256. Configuration->Usage->Inc("RemoteCopyDirect");
  4257. DebugAssert(DirectCopy);
  4258. DebugAssert(Session == Terminal);
  4259. if (Terminal->IsCapable[fcRemoteCopy] ||
  4260. Terminal->CommandSessionOpened ||
  4261. CommandSessionFallback())
  4262. {
  4263. Terminal->CopyFiles(FileList, Target, FileMask);
  4264. }
  4265. }
  4266. }
  4267. catch(...)
  4268. {
  4269. RemoteDirView->DiscardSavedSelection();
  4270. throw;
  4271. }
  4272. RemoteDirView->RestoreSelection();
  4273. }
  4274. }
  4275. return Result;
  4276. }
  4277. //---------------------------------------------------------------------------
  4278. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4279. const UnicodeString & Path, TRemoteProperties & Properties)
  4280. {
  4281. DebugAssert(!IsLocalBrowserMode());
  4282. Properties.Valid = Properties.Valid << vpEncrypt;
  4283. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4284. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4285. }
  4286. //---------------------------------------------------------------------------
  4287. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4288. {
  4289. Side = GetSide(Side);
  4290. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4291. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4292. UnicodeString Name = LoadStr(NEW_FOLDER);
  4293. int AllowedChanges =
  4294. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4295. bool SaveSettings = false;
  4296. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4297. {
  4298. TWindowLock Lock(this);
  4299. if (!IsSideLocalBrowser(Side))
  4300. {
  4301. if (SaveSettings)
  4302. {
  4303. GUIConfiguration->NewDirectoryProperties = Properties;
  4304. }
  4305. CreateRemoteDirectory(Name, Properties);
  4306. }
  4307. else
  4308. {
  4309. DirView(Side)->CreateDirectory(Name);
  4310. }
  4311. }
  4312. }
  4313. //---------------------------------------------------------------------------
  4314. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4315. {
  4316. TWindowLock Lock(this);
  4317. DirView(Side)->ExecuteHomeDirectory();
  4318. }
  4319. //---------------------------------------------------------------------------
  4320. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4321. {
  4322. DoOpenDirectoryDialog(odBrowse, Side);
  4323. }
  4324. //---------------------------------------------------------------------------
  4325. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4326. {
  4327. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4328. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4329. bool Result = !Path.IsEmpty();
  4330. if (Result)
  4331. {
  4332. TryOpenDirectory(Side, Path);
  4333. }
  4334. return Result;
  4335. }
  4336. //---------------------------------------------------------------------------
  4337. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4338. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4339. bool & Close)
  4340. {
  4341. // terminal can be already closed (e.g. dropped connection)
  4342. if (Terminal != NULL)
  4343. {
  4344. try
  4345. {
  4346. TCalculateSizeParams Params;
  4347. Params.Stats = &Stats;
  4348. Terminal->CalculateFilesSize(FileList, Size, Params);
  4349. }
  4350. catch(...)
  4351. {
  4352. if (!Terminal->Active)
  4353. {
  4354. Close = true;
  4355. }
  4356. throw;
  4357. }
  4358. }
  4359. }
  4360. //---------------------------------------------------------------------------
  4361. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4362. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4363. bool & Close)
  4364. {
  4365. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4366. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4367. {
  4368. Configuration->Usage->Inc(L"ChecksumCalculated");
  4369. try
  4370. {
  4371. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4372. }
  4373. catch(...)
  4374. {
  4375. if (!Terminal->Active)
  4376. {
  4377. Close = true;
  4378. }
  4379. throw;
  4380. }
  4381. }
  4382. }
  4383. //---------------------------------------------------------------------------
  4384. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4385. {
  4386. if (Terminal->LoadFilesProperties(FileList))
  4387. {
  4388. RemoteDirView->Invalidate();
  4389. }
  4390. }
  4391. //---------------------------------------------------------------------------
  4392. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4393. {
  4394. bool Result;
  4395. if (!IsSideLocalBrowser(Side))
  4396. {
  4397. TRemoteTokenList * GroupList = NULL;
  4398. TRemoteTokenList * UserList = NULL;
  4399. try
  4400. {
  4401. TRemoteProperties CurrentProperties;
  4402. LoadFilesProperties(FileList);
  4403. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4404. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4405. if (CapableGroupChanging || CapableOwnerChanging)
  4406. {
  4407. if (CapableGroupChanging)
  4408. {
  4409. GroupList = Terminal->Groups->Duplicate();
  4410. }
  4411. if (CapableOwnerChanging)
  4412. {
  4413. UserList = Terminal->Users->Duplicate();
  4414. }
  4415. TRemoteDirectory * Files = Terminal->Files;
  4416. int Count = Files->Count;
  4417. if (Count > 100)
  4418. {
  4419. Count = 100;
  4420. }
  4421. for (int Index = 0; Index < Count; Index++)
  4422. {
  4423. TRemoteFile * File = Files->Files[Index];
  4424. if (CapableGroupChanging)
  4425. {
  4426. GroupList->AddUnique(File->Group);
  4427. }
  4428. if (CapableOwnerChanging)
  4429. {
  4430. UserList->AddUnique(File->Owner);
  4431. }
  4432. }
  4433. // if we haven't collected tokens for all files in current directory,
  4434. // make sure we collect them at least for all selected files.
  4435. // (note that so far the files in FileList has to be from current direcotry)
  4436. if (Count < Files->Count)
  4437. {
  4438. for (int Index = 0; Index < FileList->Count; Index++)
  4439. {
  4440. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4441. if (CapableGroupChanging)
  4442. {
  4443. GroupList->AddUnique(File->Group);
  4444. }
  4445. if (CapableOwnerChanging)
  4446. {
  4447. UserList->AddUnique(File->Owner);
  4448. }
  4449. }
  4450. }
  4451. }
  4452. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4453. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4454. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4455. {
  4456. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4457. {
  4458. CapableAclChanging = false;
  4459. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4460. }
  4461. }
  4462. int Flags = 0;
  4463. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4464. if (CapableAclChanging) Flags |= cpAcl;
  4465. if (CapableOwnerChanging) Flags |= cpOwner;
  4466. if (CapableGroupChanging) Flags |= cpGroup;
  4467. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4468. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4469. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4470. {
  4471. CalculateChecksumEvent = CalculateChecksum;
  4472. }
  4473. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4474. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4475. TRemoteProperties NewProperties = CurrentProperties;
  4476. Result =
  4477. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4478. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4479. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4480. CalculateSize, CalculateChecksumEvent);
  4481. if (Result)
  4482. {
  4483. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4484. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4485. }
  4486. }
  4487. __finally
  4488. {
  4489. delete GroupList;
  4490. delete UserList;
  4491. }
  4492. }
  4493. else
  4494. {
  4495. DirView(Side)->DisplayPropertiesMenu();
  4496. Result = true;
  4497. }
  4498. return Result;
  4499. }
  4500. //---------------------------------------------------------------------------
  4501. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4502. {
  4503. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4504. {
  4505. FIgnoreNextDialogChar = Key;
  4506. }
  4507. Key = 0;
  4508. }
  4509. //---------------------------------------------------------------------------
  4510. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4511. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4512. {
  4513. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4514. if (Command != NULL)
  4515. {
  4516. KeyProcessed(Key, Shift);
  4517. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4518. {
  4519. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4520. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4521. false, false, const_cast<TCustomCommandType *>(Command));
  4522. }
  4523. }
  4524. }
  4525. //---------------------------------------------------------------------------
  4526. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4527. {
  4528. if (!NonVisualDataModule->Busy)
  4529. {
  4530. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4531. {
  4532. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4533. }
  4534. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4535. {
  4536. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4537. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4538. {
  4539. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4540. // But so far it's not a problem, as it does what we want.
  4541. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4542. if (((Action->ShortCut == KeyShortCut) ||
  4543. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4544. AllowedAction(Action, aaShortCut))
  4545. {
  4546. // Has to be called before the action as the dialog char is already in queue.
  4547. // So when the action consumes message queue, we already need to have the
  4548. // FIgnoreNextDialogChar set
  4549. KeyProcessed(Key, Shift);
  4550. // Reset reference to previous component (when menu/toolbar was clicked).
  4551. // Needed to detect that action was invoked by keyboard shortcut
  4552. // in TNonVisualDataModule::ExplorerActionsExecute
  4553. Action->ActionComponent = NULL;
  4554. Action->Execute();
  4555. return;
  4556. }
  4557. }
  4558. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4559. {
  4560. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4561. {
  4562. KeyProcessed(Key, Shift);
  4563. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4564. return;
  4565. }
  4566. }
  4567. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4568. {
  4569. KeyProcessed(Key, Shift);
  4570. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4571. }
  4572. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4573. if (IsCustomShortCut(CustomShortCut))
  4574. {
  4575. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4576. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4577. if (WinConfiguration->SharedBookmarks != NULL)
  4578. {
  4579. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4580. if ((Bookmark != NULL) &&
  4581. OpenBookmark(FCurrentSide, Bookmark))
  4582. {
  4583. KeyProcessed(Key, Shift);
  4584. }
  4585. }
  4586. }
  4587. }
  4588. }
  4589. TForm::KeyDown(Key, Shift);
  4590. }
  4591. //---------------------------------------------------------------------------
  4592. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4593. {
  4594. bool OpenedTerminal = false;
  4595. UnicodeString ProtocolName;
  4596. UnicodeString ProtocolNameHint;
  4597. UnicodeString SecurityProtocolName;
  4598. UnicodeString DurationHint;
  4599. // See the comment in TScpCommanderForm::UpdateControls()
  4600. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4601. if (ActiveTerminal != NULL)
  4602. {
  4603. // Active is not enough here
  4604. if (ActiveTerminal->Status == ssOpened)
  4605. {
  4606. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4607. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4608. if (FileSystemInfo.ProtocolName.IsEmpty())
  4609. {
  4610. ProtocolName = SessionInfo.ProtocolName;
  4611. }
  4612. else
  4613. {
  4614. ProtocolName = FileSystemInfo.ProtocolName;
  4615. }
  4616. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4617. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4618. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4619. OpenedTerminal = true;
  4620. }
  4621. else if (ActiveTerminal->Status > ssClosed)
  4622. {
  4623. ProtocolName = LoadStr(STATUS_CONNECTING);
  4624. }
  4625. else
  4626. {
  4627. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4628. }
  4629. }
  4630. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4631. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4632. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4633. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4634. if (ProtocolStatusPanel->Caption != ProtocolName)
  4635. {
  4636. ProtocolStatusPanel->Caption = ProtocolName;
  4637. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4638. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4639. }
  4640. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4641. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4642. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4643. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4644. SecurityStatusPanel->Enabled = SecurityEnabled;
  4645. UnicodeString SecurityProtocolHint;
  4646. if (SecurityEnabled)
  4647. {
  4648. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4649. }
  4650. else
  4651. {
  4652. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4653. }
  4654. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4655. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4656. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4657. DurationStatusPanel->Hint = DurationHint;
  4658. UpdateStatusBar();
  4659. }
  4660. //---------------------------------------------------------------------------
  4661. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4662. {
  4663. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4664. DebugAssert(SessionStatusBar != NULL);
  4665. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4666. if (!FNote.IsEmpty())
  4667. {
  4668. NoteStatusPanel->Caption = FNote;
  4669. }
  4670. else
  4671. {
  4672. UpdateStatusPanelText(NoteStatusPanel);
  4673. }
  4674. NoteStatusPanel->Hint = FNoteHints;
  4675. UnicodeString LoginTime;
  4676. // See the comment in TScpCommanderForm::UpdateControls()
  4677. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4678. // Active is not enough here
  4679. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4680. {
  4681. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4682. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4683. }
  4684. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4685. DurationStatusPanel->Caption = LoginTime;
  4686. }
  4687. //---------------------------------------------------------------------------
  4688. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4689. {
  4690. Panel->Caption = L"";
  4691. }
  4692. //---------------------------------------------------------------------------
  4693. void __fastcall TCustomScpExplorerForm::Idle()
  4694. {
  4695. if (FShowing ||
  4696. // Particularly for
  4697. // - Standalone editing
  4698. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4699. // while "keeping remote directory up to date".
  4700. StandaloneOperation)
  4701. {
  4702. FEditorManager->Check();
  4703. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4704. // that needs to know if queue view is visible (and it may be closed after queue update)
  4705. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4706. }
  4707. if (FShowing)
  4708. {
  4709. if (!NonVisualDataModule->Busy &&
  4710. // Menu is opened or drag&drop is going on
  4711. (Mouse->Capture == NULL))
  4712. {
  4713. if (FRefreshRemoteDirectory)
  4714. {
  4715. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4716. {
  4717. Terminal->RefreshDirectory();
  4718. }
  4719. FRefreshRemoteDirectory = false;
  4720. }
  4721. if (FRefreshLocalDirectory)
  4722. {
  4723. ReloadLocalDirectory();
  4724. FRefreshLocalDirectory = false;
  4725. }
  4726. if (WinConfiguration->RefreshRemotePanel)
  4727. {
  4728. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4729. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4730. {
  4731. RemoteDirView->ReloadDirectory();
  4732. }
  4733. }
  4734. }
  4735. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4736. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4737. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4738. // And the Alt key can be released even when other window or application is active, so handling the key up in explorer window won't do either.
  4739. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4740. MenuToolbar->Idle();
  4741. }
  4742. if (FShowing || StandaloneOperation)
  4743. {
  4744. if (FQueueStatusInvalidated)
  4745. {
  4746. UpdateQueueStatus(false);
  4747. }
  4748. RefreshQueueItems();
  4749. }
  4750. if (FShowing)
  4751. {
  4752. UpdateStatusBar();
  4753. }
  4754. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4755. {
  4756. FClipboardFakeMonitorsPendingReset = false;
  4757. FClipboardFakeMonitors.reset(NULL);
  4758. }
  4759. FIgnoreNextDialogChar = 0;
  4760. }
  4761. //---------------------------------------------------------------------------
  4762. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4763. {
  4764. try
  4765. {
  4766. FUserActionTimer->Enabled = false;
  4767. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4768. {
  4769. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4770. {
  4771. FPendingQueueActionItem->ProcessUserAction();
  4772. }
  4773. }
  4774. }
  4775. __finally
  4776. {
  4777. FPendingQueueActionItem = NULL;
  4778. }
  4779. }
  4780. //---------------------------------------------------------------------------
  4781. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4782. {
  4783. if (WinConfiguration->MinimizeToTray)
  4784. {
  4785. UpdateTrayIcon();
  4786. FTrayIcon->Visible = true;
  4787. if (Visible)
  4788. {
  4789. ShowWindow(Handle, SW_HIDE);
  4790. }
  4791. }
  4792. }
  4793. //---------------------------------------------------------------------------
  4794. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4795. {
  4796. // WORKAROUND
  4797. // When restoring maximized window from minimization,
  4798. // rarely some controls do not align properly.
  4799. // Two instances seen (both for Commander):
  4800. // - When restoring, window is temporarily narrower (not maximized),
  4801. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4802. // Once maximized already, top dock shinks back, but the session PageControl,
  4803. // do not align up, leaving space between TopDock and PageControl.
  4804. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4805. for (int Index = 0; Index < ControlCount; Index++)
  4806. {
  4807. RealignControl(Controls[Index]);
  4808. }
  4809. if (FTrayIcon->Visible)
  4810. {
  4811. FTrayIcon->Visible = false;
  4812. if (Visible)
  4813. {
  4814. ShowWindow(Handle, SW_SHOW);
  4815. }
  4816. }
  4817. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4818. {
  4819. FNeedSession = false;
  4820. NonVisualDataModule->StartBusy();
  4821. try
  4822. {
  4823. NeedSession(true);
  4824. }
  4825. __finally
  4826. {
  4827. NonVisualDataModule->EndBusy();
  4828. }
  4829. }
  4830. }
  4831. //---------------------------------------------------------------------------
  4832. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4833. {
  4834. FTrayIcon->Hint = GetMainForm()->Caption;
  4835. }
  4836. //---------------------------------------------------------------------------
  4837. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4838. {
  4839. UpdateTrayIcon();
  4840. }
  4841. //---------------------------------------------------------------------------
  4842. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4843. {
  4844. // workaround
  4845. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4846. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4847. // But WM_ACTIVATEAPP message can be received even when
  4848. // the main window is minimized to the tray and internal editor window is focused
  4849. // (after another application was previously active)
  4850. if (::IsIconic(Handle))
  4851. {
  4852. if (!IsAppIconic())
  4853. {
  4854. SetAppIconic(true);
  4855. }
  4856. }
  4857. ::ApplicationRestore();
  4858. Application->BringToFront();
  4859. }
  4860. //---------------------------------------------------------------------------
  4861. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4862. {
  4863. RestoreApp();
  4864. }
  4865. //---------------------------------------------------------------------------
  4866. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4867. {
  4868. if (OpenInNewWindow())
  4869. {
  4870. ExecuteNewInstance(SessionUrl);
  4871. }
  4872. else
  4873. {
  4874. TTerminalManager::Instance()->NewSession(SessionUrl);
  4875. }
  4876. }
  4877. //---------------------------------------------------------------------------
  4878. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4879. {
  4880. NewSession();
  4881. }
  4882. //---------------------------------------------------------------------------
  4883. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4884. {
  4885. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4886. StoredSessions->NewSession(SessionName, SessionData);
  4887. // modified only, explicit
  4888. StoredSessions->Save(false, true);
  4889. // encode session name because of slashes in hierarchical sessions
  4890. return EncodeUrlString(SessionName);
  4891. }
  4892. //---------------------------------------------------------------------------
  4893. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4894. {
  4895. // current working directories become defaults here, what is not right
  4896. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4897. return SaveHiddenDuplicateSession(SessionData.get());
  4898. }
  4899. //---------------------------------------------------------------------------
  4900. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  4901. {
  4902. if (OpenInNewWindow())
  4903. {
  4904. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4905. }
  4906. else
  4907. {
  4908. // current working directories become defaults here, what is not right
  4909. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4910. TTerminalManager * Manager = TTerminalManager::Instance();
  4911. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4912. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4913. // We definitelly want these
  4914. ASession->Disconnected = ManagedSession->Disconnected;
  4915. // Not sure about these two
  4916. ASession->Permanent = ManagedSession->Permanent;
  4917. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4918. Manager->ActiveSession = ASession;
  4919. }
  4920. }
  4921. //---------------------------------------------------------------------------
  4922. void __fastcall TCustomScpExplorerForm::RenameTab()
  4923. {
  4924. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4925. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  4926. // When submitting the default name, do not fix it
  4927. (Name != ManagedSession->SessionData->SessionName))
  4928. {
  4929. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4930. TSessionData::ValidateName(Name);
  4931. // This is inconsistent with how color (for example) is handled.
  4932. ManagedSession->SessionData->Name = Name;
  4933. UpdateControls();
  4934. // Add/Remove distinguishing paths from sessions of the same name.
  4935. SessionListChanged();
  4936. }
  4937. }
  4938. //---------------------------------------------------------------------------
  4939. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4940. {
  4941. DebugAssert(Queue != NULL);
  4942. bool Result = Queue->IsEmpty;
  4943. if (!Result)
  4944. {
  4945. SetFocus();
  4946. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4947. }
  4948. return Result;
  4949. }
  4950. //---------------------------------------------------------------------------
  4951. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4952. {
  4953. return CanCloseQueue(FQueue);
  4954. }
  4955. //---------------------------------------------------------------------------
  4956. void __fastcall TCustomScpExplorerForm::CloseTab()
  4957. {
  4958. if (CanCloseQueue())
  4959. {
  4960. TTerminalManager::Instance()->FreeActiveTerminal();
  4961. }
  4962. }
  4963. //---------------------------------------------------------------------------
  4964. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4965. {
  4966. if (CanCloseQueue())
  4967. {
  4968. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4969. }
  4970. }
  4971. //---------------------------------------------------------------------------
  4972. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4973. {
  4974. DetachTerminal(Terminal);
  4975. RemoteDirView->Terminal = Terminal;
  4976. UpdateRemotePathComboBox(false);
  4977. InitStatusBar();
  4978. UpdateControls();
  4979. }
  4980. //---------------------------------------------------------------------------
  4981. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4982. {
  4983. InitStatusBar();
  4984. }
  4985. //---------------------------------------------------------------------------
  4986. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4987. {
  4988. if (DebugAlwaysTrue(!Terminal->Active))
  4989. {
  4990. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4991. }
  4992. }
  4993. //---------------------------------------------------------------------------
  4994. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4995. {
  4996. if (OpenInNewWindow())
  4997. {
  4998. // encode session name because of slashes in hierarchical sessions
  4999. ExecuteNewInstance(EncodeUrlString(Data->Name));
  5000. }
  5001. else
  5002. {
  5003. TTerminalManager * Manager = TTerminalManager::Instance();
  5004. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  5005. Manager->ActiveSession = Terminal;
  5006. }
  5007. }
  5008. //---------------------------------------------------------------------------
  5009. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  5010. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  5011. {
  5012. TTerminalManager * Manager = TTerminalManager::Instance();
  5013. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5014. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  5015. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  5016. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  5017. {
  5018. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  5019. // FirstSession can be null, if none of the workspace sites exist anymore
  5020. // or if all workspace sessions are not suported by the interface
  5021. // (when trying to open all local-local workspace in "explorer" interface)
  5022. if (FirstSession != NULL)
  5023. {
  5024. if (!ConnectFirstTerminal)
  5025. {
  5026. FirstSession->Disconnected = true;
  5027. FirstSession->DisconnectedTemporarily = true;
  5028. }
  5029. Manager->ActiveSession = FirstSession;
  5030. }
  5031. }
  5032. }
  5033. //---------------------------------------------------------------------------
  5034. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  5035. {
  5036. if (OpenInNewWindow())
  5037. {
  5038. ExecuteNewInstance(Name);
  5039. }
  5040. else
  5041. {
  5042. DoOpenFolderOrWorkspace(Name, true, false);
  5043. }
  5044. }
  5045. //---------------------------------------------------------------------------
  5046. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  5047. bool &CanClose)
  5048. {
  5049. // See the comment in CloseApp()
  5050. if (NonVisualDataModule->Busy)
  5051. {
  5052. CanClose = false;
  5053. }
  5054. else
  5055. {
  5056. TTerminalManager * Manager = TTerminalManager::Instance();
  5057. int ActiveSessions = 0;
  5058. for (int Index = 0; Index < Manager->Count; Index++)
  5059. {
  5060. if (Manager->Sessions[Index]->Active)
  5061. {
  5062. ActiveSessions++;
  5063. }
  5064. }
  5065. // Confirm, when there's at least one active remote session or
  5066. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  5067. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  5068. WinConfiguration->ConfirmClosingSession)
  5069. {
  5070. unsigned int Result;
  5071. TMessageParams Params(mpNeverAskAgainCheck);
  5072. UnicodeString Message;
  5073. int Answers = qaOK | qaCancel;
  5074. // The current session is the only active session
  5075. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5076. {
  5077. if (!WinConfiguration->AutoSaveWorkspace)
  5078. {
  5079. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5080. }
  5081. else
  5082. {
  5083. Message = LoadStr(CLOSE_SESSION);
  5084. }
  5085. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5086. }
  5087. // Multiple active sessions or one active session, but it's not the current one
  5088. else if (ActiveSessions > 0)
  5089. {
  5090. if (!WinConfiguration->AutoSaveWorkspace)
  5091. {
  5092. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5093. }
  5094. else
  5095. {
  5096. Message = LoadStr(CLOSE_SESSIONS);
  5097. }
  5098. }
  5099. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5100. else
  5101. {
  5102. DebugAlwaysTrue(Manager->Count > 1);
  5103. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5104. Message = LoadStr(CLOSE_WORKSPACE);
  5105. }
  5106. UnicodeString Note;
  5107. if (WinConfiguration->AutoSaveWorkspace)
  5108. {
  5109. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5110. }
  5111. else
  5112. {
  5113. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5114. Answers = qaYes | qaNo | qaCancel;
  5115. }
  5116. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5117. SetFocus();
  5118. Result = MessageDialog(Message, qtConfirmation,
  5119. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5120. if (Result == qaNeverAskAgain)
  5121. {
  5122. WinConfiguration->ConfirmClosingSession = false;
  5123. }
  5124. if (Result == qaNo)
  5125. {
  5126. CanClose = SaveWorkspace(true);
  5127. // note that the workspace will be saved redundatly again from FormClose
  5128. }
  5129. else
  5130. {
  5131. CanClose =
  5132. (Result == qaOK) ||
  5133. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5134. (Result == qaNeverAskAgain);
  5135. }
  5136. }
  5137. if (CanClose && HasActiveTerminal())
  5138. {
  5139. CanClose = CanCloseQueue();
  5140. }
  5141. }
  5142. if (CanClose)
  5143. {
  5144. CanClose =
  5145. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5146. FEditorManager->CloseExternalFilesWithoutProcess();
  5147. if (CanClose)
  5148. {
  5149. while (CanClose && (FLocalEditors->Count > 0))
  5150. {
  5151. int PrevCount = FLocalEditors->Count;
  5152. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5153. CanClose = (FLocalEditors->Count < PrevCount);
  5154. }
  5155. if (CanClose)
  5156. {
  5157. CanClose = FEditorManager->Empty(true);
  5158. if (!CanClose)
  5159. {
  5160. SetFocus();
  5161. CanClose =
  5162. (MessageDialog(
  5163. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5164. }
  5165. }
  5166. }
  5167. }
  5168. }
  5169. //---------------------------------------------------------------------------
  5170. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5171. {
  5172. TForm * Form = dynamic_cast<TForm *>(Sender);
  5173. DebugAssert(Form != NULL);
  5174. Form->Close();
  5175. }
  5176. //---------------------------------------------------------------------------
  5177. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5178. {
  5179. TForm * Form = dynamic_cast<TForm *>(Sender);
  5180. delete Form;
  5181. }
  5182. //---------------------------------------------------------------------------
  5183. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5184. {
  5185. while (FLocalEditors->Count > 0)
  5186. {
  5187. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5188. }
  5189. }
  5190. //---------------------------------------------------------------------------
  5191. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5192. TObject *Sender)
  5193. {
  5194. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5195. try
  5196. {
  5197. SetProperties(osRemote, FileList);
  5198. }
  5199. __finally
  5200. {
  5201. delete FileList;
  5202. }
  5203. }
  5204. //---------------------------------------------------------------------------
  5205. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5206. {
  5207. if (value)
  5208. {
  5209. if (Component == fcCustomCommandsBand)
  5210. {
  5211. UpdateCustomCommandsToolbar();
  5212. }
  5213. if (Component == fcQueueFileList)
  5214. {
  5215. DebugAssert(!QueueFileListSplitter->Visible);
  5216. AdjustQueueLayout();
  5217. UpdateQueueFileList();
  5218. }
  5219. }
  5220. }
  5221. //---------------------------------------------------------------------------
  5222. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5223. {
  5224. int MinHeightLimit =
  5225. GetStaticQueuePanelComponentsHeight() +
  5226. QueueFileListSplitter->Height +
  5227. QueueFileList->Height +
  5228. GetMinQueueViewHeight();
  5229. if (QueuePanel->ClientHeight < MinHeightLimit)
  5230. {
  5231. QueuePanel->ClientHeight = MinHeightLimit;
  5232. }
  5233. }
  5234. //---------------------------------------------------------------------------
  5235. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5236. {
  5237. TControl * Control = GetComponent(Component);
  5238. DebugAssert(Control);
  5239. bool Changed = (Control->Visible != value);
  5240. if (Changed)
  5241. {
  5242. ComponentShowing(Component, value);
  5243. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5244. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5245. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5246. if (value)
  5247. {
  5248. int RemainingHeight = Control->Parent->ClientHeight;
  5249. int RemainingWidth = Control->Parent->ClientWidth;
  5250. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5251. {
  5252. TControl * ChildControl = Control->Parent->Controls[i];
  5253. if (ChildControl->Visible)
  5254. {
  5255. switch (ChildControl->Align)
  5256. {
  5257. case alTop:
  5258. case alBottom:
  5259. RemainingHeight -= ChildControl->Height;
  5260. break;
  5261. case alLeft:
  5262. case alRight:
  5263. RemainingWidth -= ChildControl->Width;
  5264. break;
  5265. }
  5266. }
  5267. }
  5268. int Reserve = ScaleByTextHeight(this, 32);
  5269. // queue in explorer, trees in commander
  5270. if (Control->Height > RemainingHeight - Reserve)
  5271. {
  5272. Control->Height = RemainingHeight / 2;
  5273. }
  5274. if (Control->Width > RemainingWidth - Reserve)
  5275. {
  5276. Control->Width = RemainingWidth / 2;
  5277. }
  5278. }
  5279. Control->Visible = value;
  5280. if (WasFocused && Visible)
  5281. {
  5282. DirView(osCurrent)->SetFocus();
  5283. }
  5284. FixControlsPlacement();
  5285. }
  5286. }
  5287. //---------------------------------------------------------------------------
  5288. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5289. {
  5290. TControl * Control = GetComponent(Component);
  5291. if (Control == NULL)
  5292. {
  5293. return false;
  5294. }
  5295. else
  5296. {
  5297. return Control->Visible;
  5298. }
  5299. }
  5300. //---------------------------------------------------------------------------
  5301. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5302. {
  5303. bool Result;
  5304. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5305. {
  5306. Result = !IsUWP();
  5307. }
  5308. else
  5309. {
  5310. Result = true;
  5311. }
  5312. return Result;
  5313. }
  5314. //---------------------------------------------------------------------------
  5315. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5316. {
  5317. if (DirView->ItemFocused != NULL)
  5318. {
  5319. DirView->ItemFocused->MakeVisible(false);
  5320. }
  5321. }
  5322. //---------------------------------------------------------------------------
  5323. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5324. {
  5325. MakeFocusedItemVisible(RemoteDirView);
  5326. QueueSplitter->Visible = QueuePanel->Visible;
  5327. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5328. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5329. TControl * TopControlsOrder[] =
  5330. { TopDock, MessageDock, SessionsPageControl };
  5331. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5332. TControl * QueueControlsOrder[] =
  5333. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5334. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5335. }
  5336. //---------------------------------------------------------------------------
  5337. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5338. {
  5339. switch (Component) {
  5340. case fcStatusBar: return RemoteStatusBar;
  5341. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5342. case fcQueueView: return QueuePanel;
  5343. case fcQueueToolbar: return QueueDock;
  5344. case fcRemoteTree: return RemoteDrivePanel;
  5345. case fcSessionsTabs: return SessionsPageControl;
  5346. case fcQueueFileList: return QueueFileList;
  5347. default: return NULL;
  5348. }
  5349. }
  5350. //---------------------------------------------------------------------------
  5351. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5352. TObject *Sender, TListColumn *Column, TPoint &Point)
  5353. {
  5354. DebugAssert(NonVisualDataModule && Column && Sender);
  5355. NonVisualDataModule->ListColumn = Column;
  5356. TPopupMenu * DirViewColumnMenu;
  5357. if (Sender == DirView(osOther))
  5358. {
  5359. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5360. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5361. bool SizeColumn = (Column->Index == uvSize);
  5362. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5363. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5364. }
  5365. else
  5366. {
  5367. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5368. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5369. bool SizeColumn = (Column->Index == dvSize);
  5370. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5371. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5372. }
  5373. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5374. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5375. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5376. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5377. }
  5378. //---------------------------------------------------------------------------
  5379. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5380. TObject *Sender, TListItem *Item, bool &AllowExec)
  5381. {
  5382. DoDirViewExecFile(Sender, Item, AllowExec);
  5383. }
  5384. //---------------------------------------------------------------------------
  5385. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5386. TListItem * Item, bool & AllowExec)
  5387. {
  5388. DebugAssert(Sender && Item && Configuration);
  5389. DebugAssert(AllowExec);
  5390. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5391. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5392. bool Remote = !IsSideLocalBrowser(Side);
  5393. DebugAssert(Item == ADirView->ItemFocused);
  5394. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5395. TResolvedDoubleClickAction Action;
  5396. if (FForceExecution)
  5397. {
  5398. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5399. }
  5400. else
  5401. {
  5402. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5403. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5404. }
  5405. if (Action == rdcaCopy)
  5406. {
  5407. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5408. UnlockWindow();
  5409. try
  5410. {
  5411. if (IsLocalBrowserMode())
  5412. {
  5413. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5414. }
  5415. else
  5416. {
  5417. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5418. }
  5419. }
  5420. __finally
  5421. {
  5422. LockWindow();
  5423. }
  5424. AllowExec = false;
  5425. }
  5426. else if (Action == rdcaEdit)
  5427. {
  5428. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5429. {
  5430. ExecuteFile(Side, efDefaultEditor);
  5431. AllowExec = false;
  5432. }
  5433. }
  5434. else if (Action == rdcaOpen)
  5435. {
  5436. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5437. {
  5438. ExecuteFile(osRemote, efShell);
  5439. AllowExec = false;
  5440. }
  5441. }
  5442. }
  5443. //---------------------------------------------------------------------------
  5444. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5445. {
  5446. return ((Side == osRemote) || (Side == osCurrent));
  5447. }
  5448. //---------------------------------------------------------------------------
  5449. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5450. {
  5451. DebugFail();
  5452. }
  5453. //---------------------------------------------------------------------------
  5454. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5455. {
  5456. DebugFail();
  5457. }
  5458. //---------------------------------------------------------------------------
  5459. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5460. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5461. {
  5462. TSynchronizeParamType Params;
  5463. Params.LocalDirectory = LocalDirectory;
  5464. Params.RemoteDirectory = RemoteDirectory;
  5465. int UnusedParams =
  5466. (GUIConfiguration->SynchronizeParams &
  5467. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5468. TTerminal::spCaseSensitive));
  5469. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5470. Params.Options = GUIConfiguration->SynchronizeOptions;
  5471. bool SaveSettings = false;
  5472. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5473. &DoSynchronizeTooManyDirectories);
  5474. DebugAssert(FSynchronizeController == NULL);
  5475. FSynchronizeController = &Controller;
  5476. bool Result;
  5477. try
  5478. {
  5479. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5480. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5481. int Options =
  5482. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5483. DebugAssert(FOnFeedSynchronizeError == NULL);
  5484. DebugAssert(FOnSynchronizeAbort == NULL);
  5485. DebugAssert(FSynchronizeTerminal == NULL);
  5486. FSynchronizeTerminal = Terminal;
  5487. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5488. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5489. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5490. if (Result)
  5491. {
  5492. if (SaveSettings)
  5493. {
  5494. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5495. GUIConfiguration->SynchronizeOptions = Params.Options;
  5496. }
  5497. else
  5498. {
  5499. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5500. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5501. FLAGSET(Params.Options, soSynchronize))
  5502. {
  5503. GUIConfiguration->SynchronizeOptions =
  5504. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5505. soSynchronize;
  5506. }
  5507. }
  5508. LocalDirectory = Params.LocalDirectory;
  5509. RemoteDirectory = Params.RemoteDirectory;
  5510. }
  5511. }
  5512. __finally
  5513. {
  5514. FSynchronizeController = NULL;
  5515. DebugAssert(FOnFeedSynchronizeError == NULL);
  5516. FOnFeedSynchronizeError = NULL;
  5517. DebugAssert(FOnSynchronizeAbort == NULL);
  5518. FOnSynchronizeAbort = NULL;
  5519. FSynchronizeTerminal = NULL;
  5520. }
  5521. return Result;
  5522. }
  5523. //---------------------------------------------------------------------------
  5524. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5525. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5526. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5527. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5528. TSynchronizeOptions * Options, bool Full)
  5529. {
  5530. // Keep-up-to-date mode
  5531. if (Terminal->Status == ssOpened)
  5532. {
  5533. try
  5534. {
  5535. int PParams = Params.Params;
  5536. if (!Full)
  5537. {
  5538. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5539. if (FLAGSET(Params.Options, soRecurse))
  5540. {
  5541. PParams |= TTerminal::spSubDirs;
  5542. }
  5543. }
  5544. else
  5545. {
  5546. // if keepuptodate is non-recursive,
  5547. // full sync before has to be non-recursive as well
  5548. if (FLAGCLEAR(Params.Options, soRecurse))
  5549. {
  5550. PParams |= TTerminal::spNoRecurse;
  5551. }
  5552. }
  5553. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5554. PParams, Checklist, Options);
  5555. }
  5556. catch (Exception & E)
  5557. {
  5558. if (!Terminal->Active)
  5559. {
  5560. ShowExtendedExceptionEx(Terminal, &E);
  5561. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5562. if (!Terminal->Active)
  5563. {
  5564. throw;
  5565. }
  5566. }
  5567. else
  5568. {
  5569. if (FLAGSET(Params.Options, soContinueOnError) &&
  5570. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5571. {
  5572. // noop, already logged in MoreMessageDialog
  5573. }
  5574. else
  5575. {
  5576. // We get mostly EAbort here, so this is noop.
  5577. // Exception is an error when listing directories while looking for differences.
  5578. ShowExtendedExceptionEx(Terminal, &E);
  5579. throw;
  5580. }
  5581. }
  5582. }
  5583. }
  5584. }
  5585. //---------------------------------------------------------------------------
  5586. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5587. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5588. const UnicodeString ErrorStr)
  5589. {
  5590. if (!Directory.IsEmpty())
  5591. {
  5592. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5593. }
  5594. else
  5595. {
  5596. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5597. }
  5598. }
  5599. //---------------------------------------------------------------------------
  5600. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5601. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5602. {
  5603. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5604. {
  5605. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5606. }
  5607. else
  5608. {
  5609. TMessageParams Params(mpNeverAskAgainCheck);
  5610. unsigned int Result = MessageDialog(
  5611. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5612. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5613. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5614. {
  5615. MaxDirectories *= 2;
  5616. if (Result == qaNeverAskAgain)
  5617. {
  5618. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5619. }
  5620. }
  5621. else
  5622. {
  5623. Abort();
  5624. }
  5625. }
  5626. }
  5627. //---------------------------------------------------------------------------
  5628. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5629. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5630. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5631. TSynchronizeOptions * Options)
  5632. {
  5633. // Keep-up-to-date mode
  5634. DebugAssert(!FAutoOperation);
  5635. void * BatchStorage;
  5636. BatchStart(BatchStorage);
  5637. FAutoOperation = true;
  5638. bool AnyOperation = false;
  5639. TDateTime StartTime = Now();
  5640. TSynchronizeChecklist * AChecklist = NULL;
  5641. try
  5642. {
  5643. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5644. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5645. {
  5646. FSynchronizeProgressForm->Start();
  5647. }
  5648. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5649. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5650. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5651. Options);
  5652. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5653. SAFE_DESTROY(FSynchronizeProgressForm);
  5654. AnyOperation = (AChecklist->CheckedCount > 0);
  5655. if (AnyOperation)
  5656. {
  5657. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5658. CreateProgressForm(&SynchronizeProgress);
  5659. try
  5660. {
  5661. Terminal->SynchronizeApply(
  5662. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5663. SynchronizeProcessedItem, NULL, NULL, NULL);
  5664. }
  5665. __finally
  5666. {
  5667. DestroyProgressForm();
  5668. }
  5669. }
  5670. }
  5671. __finally
  5672. {
  5673. if (Checklist == NULL)
  5674. {
  5675. delete AChecklist;
  5676. }
  5677. else
  5678. {
  5679. *Checklist = AChecklist;
  5680. }
  5681. FAutoOperation = false;
  5682. SAFE_DESTROY(FSynchronizeProgressForm);
  5683. BatchEnd(BatchStorage);
  5684. ReloadLocalDirectory();
  5685. if (AnyOperation)
  5686. {
  5687. OperationComplete(StartTime);
  5688. }
  5689. }
  5690. }
  5691. //---------------------------------------------------------------------------
  5692. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5693. {
  5694. // can be called from command line
  5695. return Visible &&
  5696. ((DirView(osRemote)->SelCount > 0) ||
  5697. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5698. }
  5699. //---------------------------------------------------------------------------
  5700. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5701. {
  5702. LogSynchronizeEvent(Terminal, Message);
  5703. }
  5704. //---------------------------------------------------------------------------
  5705. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5706. int Params, TSynchronizeOptions & Options)
  5707. {
  5708. DebugAssert(!IsLocalBrowserMode());
  5709. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5710. {
  5711. Options.Filter = new TStringList();
  5712. Options.Filter->CaseSensitive = false;
  5713. Options.Filter->Duplicates = Types::dupAccept;
  5714. if (DirView(osRemote)->SelCount > 0)
  5715. {
  5716. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5717. }
  5718. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5719. {
  5720. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5721. }
  5722. Options.Filter->Sort();
  5723. }
  5724. }
  5725. //---------------------------------------------------------------------------
  5726. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5727. {
  5728. TCopyParamType Defaults;
  5729. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5730. ConfigStorage->AccessMode = smRead;
  5731. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5732. {
  5733. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5734. }
  5735. ConfigStorage.reset(NULL);
  5736. std::unique_ptr<TStringList> Options(new TStringList());
  5737. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5738. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5739. UnicodeString Result;
  5740. if (Options->Count > 0)
  5741. {
  5742. Result =
  5743. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5744. }
  5745. return Result;
  5746. }
  5747. //---------------------------------------------------------------------------
  5748. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5749. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5750. {
  5751. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5752. UnicodeString AdditionalParams =
  5753. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5754. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5755. SerializeCopyParamForCommandLine(CopyParams),
  5756. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5757. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5758. ExecuteNewInstance(SessionName, AdditionalParams);
  5759. }
  5760. //---------------------------------------------------------------------------
  5761. struct TSynchronizeParams
  5762. {
  5763. UnicodeString LocalDirectory;
  5764. UnicodeString RemoteDirectory;
  5765. TSynchronizeMode Mode;
  5766. TSynchronizeChecklist * Checklist;
  5767. int Params;
  5768. TCopyParamType * CopyParam;
  5769. TDateTime CollectElapsed;
  5770. TDateTime * StartTime;
  5771. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5772. };
  5773. //---------------------------------------------------------------------------
  5774. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5775. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5776. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5777. {
  5778. DebugAssert(!FAutoOperation);
  5779. void * BatchStorage;
  5780. BatchStart(BatchStorage);
  5781. FAutoOperation = true;
  5782. TFileOperationStatistics Statistics;
  5783. TDateTime Start = Now();
  5784. try
  5785. {
  5786. Params.OnProcessedItem = OnProcessedItem;
  5787. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5788. CreateProgressForm(&SynchronizeProgress);
  5789. Terminal->SynchronizeApply(
  5790. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5791. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5792. &Statistics);
  5793. }
  5794. __finally
  5795. {
  5796. Params.OnProcessedItem = NULL;
  5797. FAutoOperation = false;
  5798. DestroyProgressForm();
  5799. BatchEnd(BatchStorage);
  5800. ReloadLocalDirectory();
  5801. }
  5802. if (WinConfiguration->SynchronizeSummary)
  5803. {
  5804. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5805. // The statistics should be 0 anyway in this case
  5806. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5807. {
  5808. Message += L"\n";
  5809. if (Statistics.FilesUploaded > 0)
  5810. {
  5811. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5812. }
  5813. if (Statistics.FilesDownloaded > 0)
  5814. {
  5815. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5816. }
  5817. if (Statistics.FilesDeletedLocal > 0)
  5818. {
  5819. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5820. }
  5821. if (Statistics.FilesDeletedRemote > 0)
  5822. {
  5823. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5824. }
  5825. }
  5826. TDateTime Elapsed = (Now() - Start);
  5827. Message +=
  5828. L"\n" +
  5829. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5830. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5831. TMessageParams Params(mpNeverAskAgainCheck);
  5832. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5833. if (Result == qaNeverAskAgain)
  5834. {
  5835. WinConfiguration->SynchronizeSummary = false;
  5836. }
  5837. }
  5838. }
  5839. //---------------------------------------------------------------------------
  5840. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5841. {
  5842. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5843. {
  5844. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5845. }
  5846. // Not set in keep-up-to-date mode - Synchronize() method
  5847. if (Token != NULL)
  5848. {
  5849. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5850. if (Params.OnProcessedItem != NULL)
  5851. {
  5852. Params.OnProcessedItem(NULL, ChecklistItem);
  5853. }
  5854. }
  5855. }
  5856. //---------------------------------------------------------------------------
  5857. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5858. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5859. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5860. {
  5861. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5862. *Params.StartTime = Now();
  5863. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5864. }
  5865. //---------------------------------------------------------------------------
  5866. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5867. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5868. {
  5869. // terminal can be already closed (e.g. dropped connection)
  5870. if (Terminal != NULL)
  5871. {
  5872. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5873. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5874. }
  5875. }
  5876. //---------------------------------------------------------------------------
  5877. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5878. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5879. {
  5880. DebugAssert(!IsLocalBrowserMode());
  5881. TAutoBatch AutoBatch(this);
  5882. TAutoFlag AutoOperationFlag(FAutoOperation);
  5883. if (Side == osRemote)
  5884. {
  5885. std::unique_ptr<TStrings> FileList(new TStringList());
  5886. FileList->AddObject(FileName, RemoteFile);
  5887. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5888. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5889. RemoteDirView->SaveSelection();
  5890. RemoteDirView->SaveSelectedNames();
  5891. try
  5892. {
  5893. Terminal->MoveFiles(FileList.get(), Target, FileMask, false);
  5894. }
  5895. catch(...)
  5896. {
  5897. RemoteDirView->DiscardSavedSelection();
  5898. throw;
  5899. }
  5900. RemoteDirView->RestoreSelection();
  5901. }
  5902. else if (DebugAlwaysTrue(Side == osLocal))
  5903. {
  5904. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5905. {
  5906. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5907. }
  5908. UnicodeString Directory = ExtractFileDir(FileName);
  5909. ReloadLocalDirectory(Directory);
  5910. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5911. if (!SamePaths(Directory, NewDirectory))
  5912. {
  5913. ReloadLocalDirectory(NewDirectory);
  5914. }
  5915. }
  5916. }
  5917. //---------------------------------------------------------------------------
  5918. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5919. {
  5920. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5921. if (Side == osLocal)
  5922. {
  5923. if (Action == TSynchronizeChecklist::saDownloadNew)
  5924. {
  5925. OpenFolderInExplorer(LocalPath);
  5926. }
  5927. else
  5928. {
  5929. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5930. }
  5931. }
  5932. else if (DebugAlwaysTrue(Side == osRemote))
  5933. {
  5934. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5935. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5936. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5937. if (!LocalPath.IsEmpty())
  5938. {
  5939. SessionData->LocalDirectory = LocalPath;
  5940. }
  5941. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5942. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5943. }
  5944. }
  5945. //---------------------------------------------------------------------------
  5946. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5947. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5948. const TCopyParamType * CopyParams)
  5949. {
  5950. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5951. UnicodeString AdditionalParams =
  5952. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5953. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5954. SerializeCopyParamForCommandLine(CopyParams),
  5955. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5956. LocalDirectory, RemoteDirectory, Mode, Params));
  5957. ExecuteNewInstance(SessionName, AdditionalParams);
  5958. }
  5959. //---------------------------------------------------------------------------
  5960. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5961. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5962. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5963. {
  5964. int Result;
  5965. bool SaveSettings = false;
  5966. int Options =
  5967. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5968. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5969. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5970. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5971. bool Continue =
  5972. (UseDefaults == 0) ||
  5973. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5974. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5975. if (Continue)
  5976. {
  5977. Configuration->Usage->Inc(L"Synchronizations");
  5978. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5979. UpdateCopyParamCounters(CopyParam);
  5980. TSynchronizeOptions SynchronizeOptions;
  5981. GetSynchronizeOptions(Params, SynchronizeOptions);
  5982. if (SaveSettings)
  5983. {
  5984. GUIConfiguration->SynchronizeParams = Params;
  5985. }
  5986. else
  5987. {
  5988. SaveMode = false;
  5989. }
  5990. TDateTime StartTime = Now();
  5991. TSynchronizeChecklist * Checklist = NULL;
  5992. try
  5993. {
  5994. DebugAssert(!FAutoOperation);
  5995. FAutoOperation = true;
  5996. try
  5997. {
  5998. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5999. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  6000. int Files = -1;
  6001. if (DataList->Count >= 1)
  6002. {
  6003. Files = StrToIntDef(DataList->Strings[0], Files);
  6004. }
  6005. else
  6006. {
  6007. DataList->Add(UnicodeString());
  6008. }
  6009. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  6010. FSynchronizeProgressForm->Start();
  6011. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  6012. static_cast<TTerminal::TSynchronizeMode>(Mode),
  6013. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  6014. &SynchronizeOptions);
  6015. if (Terminal->SessionData->CacheDirectories)
  6016. {
  6017. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  6018. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  6019. }
  6020. }
  6021. __finally
  6022. {
  6023. FAutoOperation = false;
  6024. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  6025. SAFE_DESTROY(FSynchronizeProgressForm);
  6026. }
  6027. TSynchronizeParams SynchronizeParams;
  6028. SynchronizeParams.LocalDirectory = LocalDirectory;
  6029. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  6030. SynchronizeParams.Mode = Mode;
  6031. SynchronizeParams.CopyParam = &CopyParam;
  6032. SynchronizeParams.Params = Params;
  6033. SynchronizeParams.Checklist = Checklist;
  6034. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  6035. SynchronizeParams.StartTime = &StartTime;
  6036. SynchronizeParams.OnProcessedItem = NULL;
  6037. Result = Checklist->Count;
  6038. if (Checklist->Count > 0)
  6039. {
  6040. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  6041. {
  6042. if (!DoSynchronizeChecklistDialog(
  6043. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  6044. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  6045. {
  6046. Result = -1;
  6047. }
  6048. }
  6049. else
  6050. {
  6051. FullSynchronize(SynchronizeParams, NULL, NULL);
  6052. }
  6053. }
  6054. else
  6055. {
  6056. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  6057. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  6058. }
  6059. }
  6060. __finally
  6061. {
  6062. delete Checklist;
  6063. }
  6064. OperationComplete(StartTime);
  6065. }
  6066. else
  6067. {
  6068. Result = -1;
  6069. }
  6070. return Result;
  6071. }
  6072. //---------------------------------------------------------------------------
  6073. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6074. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6075. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6076. {
  6077. if (Collect)
  6078. {
  6079. DebugAssert(FSynchronizeProgressForm != NULL);
  6080. if (!FSynchronizeProgressForm->Started)
  6081. {
  6082. FSynchronizeProgressForm->Start();
  6083. }
  6084. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6085. SetTaskbarListProgressValue(CompareProgress);
  6086. }
  6087. else
  6088. {
  6089. DebugAssert(FSynchronizeProgressForm == NULL);
  6090. DebugAssert(FProgressForm != NULL);
  6091. }
  6092. }
  6093. //---------------------------------------------------------------------------
  6094. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6095. {
  6096. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6097. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6098. if (File.get() != NULL)
  6099. {
  6100. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6101. Application->ShowMainForm = false;
  6102. Application->Run();
  6103. }
  6104. }
  6105. //---------------------------------------------------------------------------
  6106. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6107. {
  6108. DebugFail();
  6109. }
  6110. //---------------------------------------------------------------------------
  6111. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6112. {
  6113. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6114. SessionData->Assign(ManagedSession->SessionData);
  6115. UpdateSessionData(SessionData.get());
  6116. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6117. if (IsActiveTerminal(ManagedSession))
  6118. {
  6119. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6120. }
  6121. if (!ManagedSession->SessionData->HasSessionName())
  6122. {
  6123. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6124. // once IsWorkspace is cleared
  6125. SessionData->Name = UnicodeString();
  6126. }
  6127. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6128. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6129. SessionData->IsWorkspace = false;
  6130. return SessionData.release();
  6131. }
  6132. //---------------------------------------------------------------------------
  6133. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6134. {
  6135. TSessionData * SessionData = CloneCurrentSessionData();
  6136. try
  6137. {
  6138. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6139. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6140. }
  6141. __finally
  6142. {
  6143. delete SessionData;
  6144. }
  6145. }
  6146. //---------------------------------------------------------------------------
  6147. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6148. {
  6149. TTerminalManager * Manager = TTerminalManager::Instance();
  6150. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6151. if (DebugAlwaysTrue(ManagedSession != NULL))
  6152. {
  6153. // Update ManagedSession->StateData
  6154. UpdateSession(ManagedSession);
  6155. }
  6156. Manager->SaveWorkspace(DataList.get());
  6157. return DataList.release();
  6158. }
  6159. //---------------------------------------------------------------------------
  6160. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6161. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6162. {
  6163. WinConfiguration->LastWorkspace = Name;
  6164. if (!SavePasswords)
  6165. {
  6166. for (int Index = 0; Index < DataList->Count; Index++)
  6167. {
  6168. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6169. if (SessionData->Link.IsEmpty())
  6170. {
  6171. SessionData->ClearSessionPasswords();
  6172. }
  6173. }
  6174. }
  6175. StoredSessions->NewWorkspace(Name, DataList);
  6176. // modified only
  6177. StoredSessions->Save(false, Explicit);
  6178. }
  6179. //---------------------------------------------------------------------------
  6180. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6181. {
  6182. return
  6183. DefaultStr(WinConfiguration->LastWorkspace,
  6184. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6185. }
  6186. //---------------------------------------------------------------------------
  6187. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6188. {
  6189. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6190. bool AnyNonStoredSessionWithPassword = false;
  6191. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6192. bool AllNonStoredSessionsAnonymous = true;
  6193. TTerminalManager * Manager = TTerminalManager::Instance();
  6194. for (int Index = 0; Index < DataList->Count; Index++)
  6195. {
  6196. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6197. if (SessionData->Link.IsEmpty())
  6198. {
  6199. if (SessionData->HasAnySessionPassword())
  6200. {
  6201. AnyNonStoredSessionWithPassword = true;
  6202. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6203. {
  6204. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6205. }
  6206. }
  6207. if (!SameText(SessionData->UserName, AnonymousUserName))
  6208. {
  6209. AllNonStoredSessionsAnonymous = false;
  6210. }
  6211. }
  6212. }
  6213. bool SavePasswords;
  6214. bool * PSavePasswords;
  6215. bool NotRecommendedSavingPasswords =
  6216. !CustomWinConfiguration->UseMasterPassword &&
  6217. !AllNonStoredSessionsAnonymous;
  6218. if (Configuration->DisablePasswordStoring ||
  6219. !AnyNonStoredSessionWithPassword)
  6220. {
  6221. PSavePasswords = NULL;
  6222. SavePasswords = false;
  6223. }
  6224. else
  6225. {
  6226. PSavePasswords = &SavePasswords;
  6227. SavePasswords =
  6228. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6229. !NotRecommendedSavingPasswords;
  6230. }
  6231. UnicodeString Name = WorkspaceName();
  6232. bool CreateShortcut = false;
  6233. bool Result =
  6234. DoSaveWorkspaceDialog(
  6235. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6236. EnableAutoSave);
  6237. if (Result)
  6238. {
  6239. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6240. if (CreateShortcut)
  6241. {
  6242. TOperationVisualizer Visualizer;
  6243. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6244. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6245. }
  6246. if (EnableAutoSave)
  6247. {
  6248. WinConfiguration->AutoSaveWorkspace = true;
  6249. WinConfiguration->AutoWorkspace = Name;
  6250. if (PSavePasswords != NULL)
  6251. {
  6252. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6253. }
  6254. }
  6255. }
  6256. return Result;
  6257. }
  6258. //---------------------------------------------------------------------------
  6259. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6260. {
  6261. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6262. DebugAssert(Session == ManagedSession);
  6263. if (!IsLocalBrowserMode())
  6264. {
  6265. SAFE_DESTROY(Session->RemoteExplorerState);
  6266. if (WinConfiguration->PreservePanelState)
  6267. {
  6268. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6269. }
  6270. }
  6271. UpdateSessionData(Session->StateData);
  6272. }
  6273. //---------------------------------------------------------------------------
  6274. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6275. {
  6276. // Keep in sync with TSessionData::CopyStateData
  6277. DebugAssert(Data != NULL);
  6278. // This is inconsistent with how color (for example) is handled.
  6279. Data->Name = ManagedSession->SessionData->Name;
  6280. if (!IsLocalBrowserMode())
  6281. {
  6282. // cannot use RemoteDirView->Path, because it is empty if connection
  6283. // was already closed
  6284. // also only peek, we may not be connected at all atm
  6285. // so make sure we do not try retrieving current directory from the server
  6286. // (particularly with FTP)
  6287. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6288. if (!ACurrentDirectory.IsEmpty())
  6289. {
  6290. Data->RemoteDirectory = ACurrentDirectory;
  6291. }
  6292. }
  6293. Data->Color = SessionColor;
  6294. }
  6295. //---------------------------------------------------------------------------
  6296. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6297. {
  6298. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6299. DebugAssert(Toolbar != NULL);
  6300. for (int i = 0; i < Toolbar->Items->Count; i++)
  6301. {
  6302. TTBXCustomDropDownItem * DropDownItem;
  6303. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6304. if (DropDownItem != NULL)
  6305. {
  6306. ToolbarItemResize(DropDownItem,
  6307. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6308. Toolbar->NonClientWidth);
  6309. break;
  6310. }
  6311. }
  6312. }
  6313. //---------------------------------------------------------------------------
  6314. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6315. {
  6316. Item->EditWidth = Width;
  6317. }
  6318. //---------------------------------------------------------------------------
  6319. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6320. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6321. {
  6322. for (int i = 0; i < Toolbar->Items->Count; i++)
  6323. {
  6324. TTBXCustomDropDownItem * DropDownItem;
  6325. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6326. if (DropDownItem != NULL)
  6327. {
  6328. ASize.x -= DropDownItem->EditWidth;
  6329. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6330. }
  6331. }
  6332. }
  6333. //---------------------------------------------------------------------------
  6334. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6335. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6336. {
  6337. if (WinConfiguration->DDWarnLackOfTempSpace)
  6338. {
  6339. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6340. if (!ADrive.IsEmpty())
  6341. {
  6342. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6343. DebugAssert(RequiredSpace >= 0);
  6344. __int64 RequiredWithReserve;
  6345. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6346. if (FreeSpace < RequiredWithReserve)
  6347. {
  6348. unsigned int Result;
  6349. TMessageParams Params(mpNeverAskAgainCheck);
  6350. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6351. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6352. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6353. if (Result == qaNeverAskAgain)
  6354. {
  6355. WinConfiguration->DDWarnLackOfTempSpace = false;
  6356. }
  6357. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6358. }
  6359. }
  6360. }
  6361. }
  6362. //---------------------------------------------------------------------------
  6363. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6364. {
  6365. DoOpenDirectoryDialog(odAddBookmark, Side);
  6366. }
  6367. //---------------------------------------------------------------------------
  6368. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6369. {
  6370. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6371. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6372. try
  6373. {
  6374. TCustomDirView * DView = DirView(Side);
  6375. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6376. {
  6377. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6378. }
  6379. }
  6380. catch (...)
  6381. {
  6382. delete VisitedDirectories;
  6383. throw;
  6384. }
  6385. return VisitedDirectories;
  6386. }
  6387. //---------------------------------------------------------------------------
  6388. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6389. TOpenDirectoryMode Mode, TOperationSide Side)
  6390. {
  6391. bool Continue = true;
  6392. if (Mode == odAddBookmark)
  6393. {
  6394. TMessageParams Params(mpNeverAskAgainCheck);
  6395. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6396. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6397. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6398. unsigned int Answer =
  6399. MessageDialog(Message,
  6400. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6401. if (Answer == qaNeverAskAgain)
  6402. {
  6403. Continue = true;
  6404. WinConfiguration->UseSharedBookmarks = true;
  6405. }
  6406. else if (Answer == qaOK)
  6407. {
  6408. Continue = true;
  6409. WinConfiguration->UseSharedBookmarks = false;
  6410. }
  6411. else
  6412. {
  6413. Continue = false;
  6414. }
  6415. }
  6416. if (Continue)
  6417. {
  6418. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6419. try
  6420. {
  6421. UnicodeString Name = DirView(Side)->PathName;
  6422. bool Repeat;
  6423. do
  6424. {
  6425. Repeat = false;
  6426. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6427. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6428. // do not allow switching to location profiles,
  6429. // if we are not connected
  6430. HasDirView[osLocal] && (Terminal != NULL)))
  6431. {
  6432. TWindowLock Lock(this);
  6433. if (!TryOpenDirectory(Side, Name))
  6434. {
  6435. Repeat = true;
  6436. Mode = odBrowse;
  6437. }
  6438. }
  6439. }
  6440. while (Repeat);
  6441. }
  6442. __finally
  6443. {
  6444. delete VisitedDirectories;
  6445. }
  6446. }
  6447. }
  6448. //---------------------------------------------------------------------------
  6449. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6450. {
  6451. DebugAssert(!Terminal->CommandSessionOpened);
  6452. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6453. }
  6454. //---------------------------------------------------------------------------
  6455. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6456. {
  6457. bool Result = Terminal->IsCapable[Capability] ||
  6458. Terminal->CommandSessionOpened;
  6459. if (!Result)
  6460. {
  6461. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6462. if (!GUIConfiguration->ConfirmCommandSession)
  6463. {
  6464. Result = true;
  6465. }
  6466. else
  6467. {
  6468. TMessageParams Params(mpNeverAskAgainCheck);
  6469. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6470. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6471. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6472. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6473. if (Answer == qaNeverAskAgain)
  6474. {
  6475. GUIConfiguration->ConfirmCommandSession = false;
  6476. Result = true;
  6477. }
  6478. else if (Answer == qaOK)
  6479. {
  6480. Result = true;
  6481. }
  6482. }
  6483. if (Result)
  6484. {
  6485. Result = CommandSessionFallback();
  6486. }
  6487. }
  6488. return Result;
  6489. }
  6490. //---------------------------------------------------------------------------
  6491. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6492. {
  6493. if (EnsureCommandSessionFallback(fcAnyCommand))
  6494. {
  6495. DoConsoleDialog(Terminal, Command);
  6496. }
  6497. }
  6498. //---------------------------------------------------------------------------
  6499. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6500. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6501. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6502. {
  6503. if (!IsSideLocalBrowser(osRemote) &&
  6504. IsFileControl(DropSourceControl, osRemote) &&
  6505. (FDDExtMapFile != NULL))
  6506. {
  6507. Accept = true;
  6508. }
  6509. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6510. DebugAssert(FDDTargetControl != NULL);
  6511. }
  6512. //---------------------------------------------------------------------------
  6513. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6514. _di_IDataObject DataObj, int KeyState,
  6515. const TPoint & Point, int & Effect, bool & Accept)
  6516. {
  6517. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6518. }
  6519. //---------------------------------------------------------------------------
  6520. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6521. _di_IDataObject DataObj, int KeyState,
  6522. const TPoint & Point, int & Effect, bool & Accept)
  6523. {
  6524. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6525. }
  6526. //---------------------------------------------------------------------------
  6527. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6528. TObject * Sender)
  6529. {
  6530. DebugUsedParam(Sender);
  6531. DebugAssert(FDDTargetControl == Sender);
  6532. FDDTargetControl = NULL;
  6533. }
  6534. //---------------------------------------------------------------------------
  6535. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6536. {
  6537. FileControlDDDragLeave(SessionsPageControl);
  6538. }
  6539. //---------------------------------------------------------------------------
  6540. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6541. {
  6542. FileControlDDDragLeave(QueueView3);
  6543. }
  6544. //---------------------------------------------------------------------------
  6545. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6546. {
  6547. DebugAssert(!IsSideLocalBrowser(Side));
  6548. DebugUsedParam(Side);
  6549. bool Edit = false;
  6550. TRemoteFile * File = NULL;
  6551. UnicodeString FileName;
  6552. UnicodeString PointTo;
  6553. bool SymbolicLink = true;
  6554. if (RemoteDirView->ItemFocused)
  6555. {
  6556. DebugAssert(RemoteDirView->ItemFocused->Data);
  6557. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6558. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6559. if (Edit)
  6560. {
  6561. FileName = File->FileName;
  6562. PointTo = File->LinkTo;
  6563. }
  6564. else if (!File->IsParentDirectory)
  6565. {
  6566. PointTo = File->FileName;
  6567. }
  6568. }
  6569. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6570. Terminal->IsCapable[fcHardLink]))
  6571. {
  6572. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6573. if (Edit)
  6574. {
  6575. DebugAssert(File->FileName == FileName);
  6576. int Params = dfNoRecursive;
  6577. Terminal->ExceptionOnFail = true;
  6578. try
  6579. {
  6580. Terminal->DeleteFile(L"", File, &Params);
  6581. }
  6582. __finally
  6583. {
  6584. Terminal->ExceptionOnFail = false;
  6585. }
  6586. }
  6587. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6588. }
  6589. }
  6590. //---------------------------------------------------------------------------
  6591. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6592. {
  6593. return
  6594. (IsSideLocalBrowser(Side) ||
  6595. (HasActiveTerminal() &&
  6596. Terminal->ResolvingSymlinks &&
  6597. Terminal->IsCapable[fcSymbolicLink]));
  6598. }
  6599. //---------------------------------------------------------------------------
  6600. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6601. {
  6602. return
  6603. !IsSideLocalBrowser(FCurrentSide) &&
  6604. (RemoteDirView->ItemFocused != NULL) &&
  6605. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6606. Terminal->SessionData->ResolveSymlinks;
  6607. }
  6608. //---------------------------------------------------------------------------
  6609. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6610. {
  6611. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6612. FForceExecution = true;
  6613. try
  6614. {
  6615. DirView(osCurrent)->ExecuteCurrentFile();
  6616. }
  6617. __finally
  6618. {
  6619. FForceExecution = false;
  6620. }
  6621. }
  6622. //---------------------------------------------------------------------------
  6623. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6624. {
  6625. TEditorData ExternalEditor;
  6626. ExternalEditor.Editor = edExternal;
  6627. bool Remember = false;
  6628. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6629. {
  6630. if (Remember)
  6631. {
  6632. TEditorList * EditorList = new TEditorList;
  6633. try
  6634. {
  6635. *EditorList = *WinConfiguration->EditorList;
  6636. bool Found = false;
  6637. int i = 0;
  6638. while (!Found && (i < EditorList->Count))
  6639. {
  6640. const TEditorPreferences * Editor = EditorList->Editors[i];
  6641. if ((Editor->Data->Editor == edExternal) &&
  6642. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6643. {
  6644. Found = true;
  6645. }
  6646. i++;
  6647. }
  6648. if (!Found)
  6649. {
  6650. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6651. WinConfiguration->EditorList = EditorList;
  6652. }
  6653. }
  6654. __finally
  6655. {
  6656. delete EditorList;
  6657. }
  6658. }
  6659. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6660. }
  6661. }
  6662. //---------------------------------------------------------------------------
  6663. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6664. {
  6665. if (FFileFindTerminal == ATerminal)
  6666. {
  6667. FFileFindTerminal = NULL;
  6668. HideFileFindDialog();
  6669. }
  6670. if (FClipboardTerminal == ATerminal)
  6671. {
  6672. ClipboardClear(); // implies ClipboardStop
  6673. }
  6674. if ((FOnSynchronizeAbort != NULL) &&
  6675. (ATerminal == FManagedSession))
  6676. {
  6677. FOnSynchronizeAbort(NULL);
  6678. }
  6679. }
  6680. //---------------------------------------------------------------------------
  6681. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6682. {
  6683. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6684. DetachTerminal(Sender);
  6685. }
  6686. //---------------------------------------------------------------------------
  6687. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6688. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6689. {
  6690. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6691. DebugAssert(Terminal != NULL);
  6692. if (Data->Terminal == Terminal)
  6693. {
  6694. Data->Terminal = NULL;
  6695. }
  6696. }
  6697. //---------------------------------------------------------------------------
  6698. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6699. {
  6700. return NULL;
  6701. }
  6702. //---------------------------------------------------------------------------
  6703. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6704. {
  6705. UpdateControls();
  6706. SessionColor = TColor(0);
  6707. UpdateRemotePathComboBox(false);
  6708. NeedSession(false);
  6709. }
  6710. //---------------------------------------------------------------------------
  6711. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6712. {
  6713. try
  6714. {
  6715. // Cache, as the login dialog can change its value.
  6716. // See also DoShow. Also see TTerminalManager::NewSession.
  6717. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6718. try
  6719. {
  6720. if (ShowLogin)
  6721. {
  6722. bool ReloadSessions = !Startup;
  6723. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6724. }
  6725. }
  6726. __finally
  6727. {
  6728. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6729. // (so there was no chance for the user to open any session yet)
  6730. if ((ShowLogin || !Startup) &&
  6731. !WinConfiguration->KeepOpenWhenNoSession &&
  6732. // this rules out the implicit Commander's local-local browser
  6733. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6734. {
  6735. TerminateApplication();
  6736. }
  6737. }
  6738. }
  6739. catch (EAbort &)
  6740. {
  6741. // swallow
  6742. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6743. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6744. }
  6745. }
  6746. //---------------------------------------------------------------------------
  6747. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6748. {
  6749. TTerminalManager * Manager = TTerminalManager::Instance();
  6750. DebugAssert(!Manager->Updating);
  6751. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6752. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6753. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6754. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6755. try
  6756. {
  6757. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6758. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6759. {
  6760. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6761. }
  6762. for (int Index = 0; Index <= Manager->Count; Index++)
  6763. {
  6764. TThemeTabSheet * TabSheet;
  6765. if (Index >= SessionsPageControl->PageCount)
  6766. {
  6767. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6768. TabSheet->PageControl = SessionsPageControl;
  6769. }
  6770. else
  6771. {
  6772. TabSheet = SessionsPageControl->Pages[Index];
  6773. }
  6774. bool IsSessionTab = (Index < Manager->Count);
  6775. if (IsSessionTab)
  6776. {
  6777. TTerminal * Terminal = Manager->Sessions[Index];
  6778. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6779. TabSheet->CaptionTruncation = tttEllipsis;
  6780. UpdateSessionTab(TabSheet);
  6781. }
  6782. else
  6783. {
  6784. TabSheet->Tag = 0; // not really needed
  6785. TabSheet->Shadowed = false;
  6786. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6787. TabSheet->CaptionTruncation = tttNoText;
  6788. // 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)
  6789. UpdateNewTabTab();
  6790. }
  6791. }
  6792. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6793. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6794. {
  6795. SessionsPageControl->UpdateTabsCaptionTruncation();
  6796. }
  6797. }
  6798. __finally
  6799. {
  6800. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6801. }
  6802. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6803. }
  6804. //---------------------------------------------------------------------------
  6805. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6806. {
  6807. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6808. }
  6809. //---------------------------------------------------------------------------
  6810. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6811. {
  6812. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6813. UnicodeString TabCaption;
  6814. if (WinConfiguration->SelectiveToolbarText)
  6815. {
  6816. TabCaption = GetNewTabTabCaption();
  6817. }
  6818. TabSheet->BaseCaption = TabCaption;
  6819. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6820. }
  6821. //---------------------------------------------------------------------------
  6822. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6823. {
  6824. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6825. }
  6826. //---------------------------------------------------------------------------
  6827. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6828. {
  6829. DebugFail();
  6830. return UnicodeString();
  6831. }
  6832. //---------------------------------------------------------------------------
  6833. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6834. {
  6835. if (DebugAlwaysTrue(TabSheet != NULL))
  6836. {
  6837. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6838. if (DebugAlwaysTrue(ASession != NULL))
  6839. {
  6840. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6841. if (ASession->LocalBrowser)
  6842. {
  6843. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6844. }
  6845. else
  6846. {
  6847. TabSheet->ImageIndex = AddSessionColor(Color);
  6848. }
  6849. TTerminalManager * Manager = TTerminalManager::Instance();
  6850. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6851. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6852. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6853. }
  6854. }
  6855. }
  6856. //---------------------------------------------------------------------------
  6857. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6858. {
  6859. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6860. }
  6861. //---------------------------------------------------------------------------
  6862. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6863. {
  6864. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6865. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6866. bool Result = (Session != NULL);
  6867. if (Result)
  6868. {
  6869. TTerminalManager::Instance()->ActiveSession = Session;
  6870. }
  6871. else
  6872. {
  6873. try
  6874. {
  6875. NewTab();
  6876. }
  6877. __finally
  6878. {
  6879. SessionListChanged();
  6880. }
  6881. FSessionsPageControlNewTabTime = Now();
  6882. }
  6883. return Result;
  6884. }
  6885. //---------------------------------------------------------------------------
  6886. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6887. {
  6888. TWindowLock Lock(this);
  6889. SessionTabSwitched();
  6890. }
  6891. //---------------------------------------------------------------------------
  6892. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6893. {
  6894. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6895. DebugAssert(TransferList != NULL);
  6896. UnicodeString Name;
  6897. if (TransferList->ItemIndex <= 0)
  6898. {
  6899. Name = L"";
  6900. }
  6901. else
  6902. {
  6903. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6904. }
  6905. if (FCopyParamAutoSelected.IsEmpty())
  6906. {
  6907. // if previous preset was not autoselected, make new preset the "default"
  6908. FCopyParamDefault = Name;
  6909. }
  6910. GUIConfiguration->CopyParamCurrent = Name;
  6911. }
  6912. //---------------------------------------------------------------------------
  6913. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6914. {
  6915. // sanity check
  6916. bool ExistingPreset =
  6917. (FTransferListHoverIndex >= 0) &&
  6918. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6919. DebugAssert(ExistingPreset);
  6920. if (ExistingPreset)
  6921. {
  6922. HDC DC = GetDC(0);
  6923. TCanvas * Canvas = new TCanvas();
  6924. try
  6925. {
  6926. Canvas->Handle = DC;
  6927. Canvas->Font = GetToolbarFont(this);
  6928. UnicodeString Name;
  6929. if (FTransferListHoverIndex == 0)
  6930. {
  6931. Name = L"";
  6932. }
  6933. else
  6934. {
  6935. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6936. }
  6937. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6938. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6939. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6940. static_cast<TObject*>(GetComponent(fcTransferList)));
  6941. UnicodeString InfoStr =
  6942. GUIConfiguration->CopyParamPreset[Name].
  6943. GetInfoStr(L"; ",
  6944. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6945. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6946. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6947. {
  6948. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6949. {
  6950. InfoStr.SetLength(InfoStr.Length() - 1);
  6951. }
  6952. InfoStr += Ellipsis;
  6953. }
  6954. // UpdateCaption does not cause invalidation of whole submenu, while
  6955. // setting Caption property does.
  6956. // also it probably does not resize the label, even if necessary
  6957. // (we do not want that anyway)
  6958. TransferLabel->UpdateCaption(InfoStr);
  6959. }
  6960. __finally
  6961. {
  6962. Canvas->Handle = NULL;
  6963. ReleaseDC(0, DC);
  6964. delete Canvas;
  6965. }
  6966. }
  6967. }
  6968. //---------------------------------------------------------------------------
  6969. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6970. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6971. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6972. {
  6973. if (FTransferListHoverIndex != AHoverIndex)
  6974. {
  6975. FTransferListHoverIndex = AHoverIndex;
  6976. UpdateTransferLabel();
  6977. }
  6978. }
  6979. //---------------------------------------------------------------------------
  6980. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6981. {
  6982. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6983. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6984. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6985. {
  6986. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6987. {
  6988. if (!NonVisualDataModule->Busy)
  6989. {
  6990. OpenDirectory(GetSide(osCurrent));
  6991. }
  6992. Message.Result = 1;
  6993. }
  6994. else
  6995. {
  6996. TForm::Dispatch(&Message);
  6997. }
  6998. }
  6999. else
  7000. {
  7001. TForm::Dispatch(&Message);
  7002. }
  7003. }
  7004. //---------------------------------------------------------------------------
  7005. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  7006. {
  7007. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  7008. if ((FIgnoreNextDialogChar != 0) &&
  7009. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  7010. {
  7011. Message.Result = 1;
  7012. }
  7013. else
  7014. {
  7015. TForm::Dispatch(&Message);
  7016. }
  7017. FIgnoreNextDialogChar = 0;
  7018. }
  7019. //---------------------------------------------------------------------------
  7020. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  7021. {
  7022. // The four low-order bits are used internally by Windows
  7023. unsigned int Cmd = (Message.WParam & 0xFFF0);
  7024. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  7025. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  7026. // 61730 - restore thru double click - undocumented
  7027. // 61490 - maximize thru double click - undocumented
  7028. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  7029. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  7030. {
  7031. SysResizing(Cmd);
  7032. }
  7033. TForm::Dispatch(&Message);
  7034. }
  7035. //---------------------------------------------------------------------------
  7036. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  7037. {
  7038. // We were actually never able to make ENDSESSION_CRITICAL happen.
  7039. // Also there no point returning TRUE as we are not able to
  7040. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  7041. // Hence the process termination might be safer :)
  7042. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  7043. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  7044. {
  7045. Message.Result = FALSE;
  7046. }
  7047. else
  7048. {
  7049. Message.Result = TRUE;
  7050. }
  7051. // Do not call default handling as that triggers OnCloseQuery,
  7052. // where our implementation will popup confirmation dialogs, what we do not want,
  7053. // as per Vista guidelines:
  7054. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  7055. }
  7056. //---------------------------------------------------------------------------
  7057. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  7058. {
  7059. if (Message.EndSession && IsApplicationMinimized())
  7060. {
  7061. // WORKAROUND
  7062. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  7063. // when it receives WM_ENDSESSION,
  7064. // but that sometimes (particularly when application is minimized) cause crashes.
  7065. // Crash popups message that blocks log off.
  7066. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  7067. // so we call ExitProcess() immediately, not even trying to cleanup.
  7068. // It still causes beep, so there's likely some popup, but it does not block
  7069. // log off.
  7070. ExitProcess(0);
  7071. }
  7072. TForm::Dispatch(&Message);
  7073. }
  7074. #ifdef _DEBUG
  7075. //---------------------------------------------------------------------------
  7076. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7077. {
  7078. TForm::Dispatch(&Message);
  7079. }
  7080. #endif
  7081. //---------------------------------------------------------------------------
  7082. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7083. {
  7084. }
  7085. //---------------------------------------------------------------------------
  7086. void __fastcall TCustomScpExplorerForm::DoShow()
  7087. {
  7088. // Called from TCustomScpExplorerForm::CMShowingChanged
  7089. // only now are the controls resized finally, so the size constraints
  7090. // will not conflict with possibly very small window size
  7091. RestoreFormParams();
  7092. // This indicates that the form was rescaled after parameters were loaded and
  7093. // as most probably the parameters were actually saved with the current form scaling
  7094. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7095. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7096. // and no rounding problems will occur.
  7097. // See also RestoreForm.
  7098. // (we should be safe to call RestoreParams unconditionally)
  7099. if (PixelsPerInch != Screen->PixelsPerInch)
  7100. {
  7101. RestoreParams();
  7102. }
  7103. FixControlsPlacement();
  7104. if (Position == poDefaultPosOnly)
  7105. {
  7106. CutFormToDesktop(this);
  7107. }
  7108. TForm::DoShow();
  7109. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7110. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7111. // This was previously in NeedSession, where other related code is.
  7112. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7113. // unpleasantly shows briefly.
  7114. if ((ManagedSession == NULL) &&
  7115. !WinConfiguration->ShowLoginWhenNoSession &&
  7116. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7117. // This detects if workspace was saved the last time the main widow was closed
  7118. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7119. {
  7120. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7121. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7122. }
  7123. // Before main window displays
  7124. if (ManagedSession == NULL)
  7125. {
  7126. StartingWithoutSession();
  7127. }
  7128. UpdatePixelsPerInchMainWindowCounter();
  7129. FShowing = true;
  7130. }
  7131. //---------------------------------------------------------------------------
  7132. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7133. {
  7134. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7135. }
  7136. //---------------------------------------------------------------------------
  7137. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7138. {
  7139. SessionListChanged();
  7140. InitStatusBar();
  7141. UpdateControls();
  7142. }
  7143. //---------------------------------------------------------------------------
  7144. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7145. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7146. TNotifyEvent OnBalloonClick, TObject * UserData)
  7147. {
  7148. bool Do;
  7149. UnicodeString Message;
  7150. if (E == NULL)
  7151. {
  7152. Message = Str;
  7153. Do = true;
  7154. }
  7155. else
  7156. {
  7157. Do = ExceptionMessage(E, Message);
  7158. }
  7159. if (Do && WinConfiguration->BalloonNotifications)
  7160. {
  7161. UnicodeString Title;
  7162. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7163. {
  7164. Message = TrimLeft(Message);
  7165. }
  7166. else
  7167. {
  7168. Message = UnformatMessage(Message);
  7169. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7170. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7171. Title = LoadResourceString(Captions[Type]);
  7172. if (Terminal != NULL)
  7173. {
  7174. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7175. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7176. }
  7177. }
  7178. if (Seconds == 0)
  7179. {
  7180. Seconds = WinConfiguration->NotificationsTimeout;
  7181. }
  7182. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7183. }
  7184. }
  7185. //---------------------------------------------------------------------------
  7186. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7187. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7188. UnicodeString HelpKeyword, const TMessageParams * Params,
  7189. TTerminal * Terminal)
  7190. {
  7191. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7192. (FOnFeedSynchronizeError != NULL)) &&
  7193. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7194. {
  7195. if (FOnFeedSynchronizeError != NULL)
  7196. {
  7197. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7198. }
  7199. else
  7200. {
  7201. DebugAssert(FErrorList != NULL);
  7202. TStringList * MoreMessagesCopy = NULL;
  7203. if (MoreMessages)
  7204. {
  7205. MoreMessagesCopy = new TStringList();
  7206. MoreMessagesCopy->Assign(MoreMessages);
  7207. }
  7208. FErrorList->AddObject(Message, MoreMessagesCopy);
  7209. }
  7210. PopupTrayBalloon(Terminal, Message, Type);
  7211. return ContinueAnswer(Answers);
  7212. }
  7213. else
  7214. {
  7215. bool UseBalloon = FTrayIcon->Visible;
  7216. if (UseBalloon)
  7217. {
  7218. PopupTrayBalloon(Terminal, Message, Type);
  7219. }
  7220. unsigned int Result;
  7221. try
  7222. {
  7223. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7224. }
  7225. __finally
  7226. {
  7227. // cancel only balloon we popped up, otherwise we may cancel notification
  7228. // balloon that was there before the message dialog
  7229. // (such as "dd confirmation opt in balloon)
  7230. if (UseBalloon)
  7231. {
  7232. FTrayIcon->CancelBalloon();
  7233. }
  7234. }
  7235. return Result;
  7236. }
  7237. }
  7238. //---------------------------------------------------------------------------
  7239. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7240. TTerminal * Terminal, Exception * E)
  7241. {
  7242. if (FTrayIcon->Visible)
  7243. {
  7244. PopupTrayBalloon(Terminal, L"", qtError, E);
  7245. }
  7246. // particularly prevent opening new session from jump list,
  7247. // while exception is shown
  7248. NonVisualDataModule->StartBusy();
  7249. try
  7250. {
  7251. ShowExtendedExceptionEx(Terminal, E);
  7252. }
  7253. __finally
  7254. {
  7255. NonVisualDataModule->EndBusy();
  7256. FTrayIcon->CancelBalloon();
  7257. }
  7258. }
  7259. //---------------------------------------------------------------------------
  7260. void __fastcall TCustomScpExplorerForm::SessionReady()
  7261. {
  7262. InitStatusBar();
  7263. // cannot rely on active page being page for active terminal,
  7264. // as it can happen that active page is the "new session" page
  7265. // (e.g. when reconnecting active terminal, while login dialog
  7266. // invoked from "new session" page is modal)
  7267. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7268. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7269. }
  7270. //---------------------------------------------------------------------------
  7271. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7272. TTerminal * Terminal, Exception * E)
  7273. {
  7274. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7275. if (!Terminal->Active)
  7276. {
  7277. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7278. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7279. {
  7280. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7281. }
  7282. }
  7283. }
  7284. //---------------------------------------------------------------------------
  7285. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7286. UnicodeString Message, TQueryType Type,
  7287. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7288. {
  7289. if ((E == NULL) ||
  7290. ExceptionMessage(E, Message))
  7291. {
  7292. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7293. if (Important)
  7294. {
  7295. Seconds *= 5;
  7296. }
  7297. UnicodeString NoteMessage(UnformatMessage(Message));
  7298. if (Terminal != NULL)
  7299. {
  7300. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7301. NoteMessage = FORMAT(L"%s: %s",
  7302. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7303. }
  7304. if (WinConfiguration->BalloonNotifications)
  7305. {
  7306. AddNote(NoteMessage);
  7307. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7308. }
  7309. else
  7310. {
  7311. FlashOnBackground();
  7312. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7313. }
  7314. }
  7315. }
  7316. //---------------------------------------------------------------------------
  7317. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7318. {
  7319. RestoreApp();
  7320. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7321. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7322. !NonVisualDataModule->Busy)
  7323. {
  7324. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7325. TTerminalManager::Instance()->ActiveSession = Terminal;
  7326. if (!ComponentVisible[fcQueueView])
  7327. {
  7328. ToggleQueueVisibility();
  7329. GoToQueue();
  7330. }
  7331. }
  7332. }
  7333. //---------------------------------------------------------------------------
  7334. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7335. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7336. {
  7337. UnicodeString Message;
  7338. TNotifyEvent OnClick = NULL;
  7339. TObject * UserData = NULL;
  7340. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7341. switch (Event)
  7342. {
  7343. case qeEmptyButMonitored:
  7344. if ((ATerminal != Terminal) || QueueInvisible)
  7345. {
  7346. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7347. OnClick = QueueEmptyNoteClicked;
  7348. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7349. TerminalNoteData->Terminal = ATerminal;
  7350. UserData = TerminalNoteData;
  7351. }
  7352. break;
  7353. case qeEmpty:
  7354. OperationComplete(ATerminal->QueueOperationStart);
  7355. break;
  7356. case qePendingUserAction:
  7357. if ((ATerminal != Terminal) ||
  7358. (QueueInvisible && !IsQueueAutoPopup()))
  7359. {
  7360. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7361. }
  7362. break;
  7363. default:
  7364. DebugFail();
  7365. }
  7366. if (!Message.IsEmpty())
  7367. {
  7368. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7369. }
  7370. }
  7371. //---------------------------------------------------------------------------
  7372. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7373. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7374. {
  7375. if (FDDExtMapFile != NULL)
  7376. {
  7377. CloseHandle(FDDExtMapFile);
  7378. FDDExtMapFile = NULL;
  7379. }
  7380. if (WinConfiguration->DDFakeFile)
  7381. {
  7382. DDFakeFileInitDrag(FileList, Created);
  7383. }
  7384. }
  7385. //---------------------------------------------------------------------------
  7386. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7387. {
  7388. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7389. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7390. {
  7391. FFakeFileDropTarget = FileName;
  7392. }
  7393. }
  7394. //---------------------------------------------------------------------------
  7395. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7396. {
  7397. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7398. if (!ForceDirectories(ApiPath(Result)))
  7399. {
  7400. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7401. }
  7402. FileSetAttr(ApiPath(Result), faHidden);
  7403. return Result;
  7404. }
  7405. //---------------------------------------------------------------------------
  7406. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7407. bool & Created)
  7408. {
  7409. FFakeFileDropTarget = UnicodeString();
  7410. FDragFakeDirectory = CreateFakeTransferDirectory();
  7411. FileList->AddItem(NULL, FDragFakeDirectory);
  7412. Created = true;
  7413. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7414. {
  7415. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7416. }
  7417. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7418. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7419. {
  7420. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7421. TDragExtCommStruct* CommStruct;
  7422. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7423. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7424. DebugAssert(CommStruct != NULL);
  7425. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7426. CommStruct->Dragging = true;
  7427. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7428. LENOF(CommStruct->DropDest));
  7429. NULL_TERMINATE(CommStruct->DropDest);
  7430. UnmapViewOfFile(CommStruct);
  7431. }
  7432. }
  7433. //---------------------------------------------------------------------------
  7434. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7435. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7436. {
  7437. bool Result;
  7438. DebugAssert(!IsLocalBrowserMode());
  7439. if (Sender == RemoteDirView)
  7440. {
  7441. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7442. }
  7443. else
  7444. {
  7445. DebugAssert(Sender == RemoteDriveView);
  7446. TStrings * FileList = RemoteDriveView->DragFileList();
  7447. try
  7448. {
  7449. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7450. }
  7451. __finally
  7452. {
  7453. delete FileList;
  7454. }
  7455. }
  7456. if (FDDTargetControl == RemoteDriveView)
  7457. {
  7458. RemoteDriveView->UpdateDropTarget();
  7459. }
  7460. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7461. // or from tree view to dir view.
  7462. // foMove happens when file/dir is dragged from remote tree view outside of
  7463. // application via dragex
  7464. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7465. (DropSourceControl == RemoteDriveView))
  7466. {
  7467. RemoteDriveView->UpdateDropSource();
  7468. }
  7469. return Result;
  7470. }
  7471. //---------------------------------------------------------------------------
  7472. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7473. {
  7474. DebugAssert(!IsLocalBrowserMode());
  7475. // This also handles drops of remote files to queue.
  7476. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7477. SAFE_DESTROY(FDDFileList);
  7478. if (!FFakeFileDropTarget.IsEmpty())
  7479. {
  7480. RemoveDir(ApiPath(FFakeFileDropTarget));
  7481. }
  7482. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7483. {
  7484. try
  7485. {
  7486. TDragResult DDResult = (Sender == RemoteDirView) ?
  7487. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7488. // Focus is moved to the target application,
  7489. // but as we are going to present the UI, we need to steal the focus back.
  7490. // This most likely won't work though (windows does not allow application
  7491. // to steal focus most of the time)
  7492. Application->BringToFront();
  7493. // On older version of Windows we never got drMove here, see also comment below.
  7494. // On Windows 10, we get the "move".
  7495. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7496. {
  7497. UnicodeString TargetDirectory;
  7498. TFileOperation Operation;
  7499. // drInvalid may mean drMove, see comment below
  7500. switch (DDResult)
  7501. {
  7502. case drCopy:
  7503. Operation = foCopy;
  7504. break;
  7505. case drMove:
  7506. Operation = foMove;
  7507. break;
  7508. default:
  7509. DebugFail();
  7510. case drInvalid:
  7511. // prefer "copy" for safety
  7512. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7513. break;
  7514. }
  7515. TTransferOperationParam Param;
  7516. UnicodeString CounterName;
  7517. bool ForceQueue;
  7518. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7519. {
  7520. // we get drInvalid both if d&d was intercepted by ddext,
  7521. // and when users drops on no-drop location.
  7522. // we tell the difference by existence of response from ddext,
  7523. // so we ignore absence of response in this case
  7524. if (DDResult != drInvalid)
  7525. {
  7526. // here we know that the extension is installed,
  7527. // as it is checked as soon as drag&drop starts from
  7528. // RemoteFileControlDDCreateDragFileList
  7529. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7530. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7531. {
  7532. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7533. }
  7534. else
  7535. {
  7536. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7537. }
  7538. }
  7539. }
  7540. else
  7541. {
  7542. // download using fake file
  7543. Param.Temp = false;
  7544. Param.DragDrop = true;
  7545. if (ForceQueue)
  7546. {
  7547. Param.Queue = asOn;
  7548. }
  7549. if (Sender == RemoteDirView)
  7550. {
  7551. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7552. }
  7553. if (RemoteFileControlFileOperation(Sender, Operation,
  7554. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7555. {
  7556. Configuration->Usage->Inc(CounterName);
  7557. }
  7558. }
  7559. }
  7560. }
  7561. __finally
  7562. {
  7563. CloseHandle(FDDExtMapFile);
  7564. FDDExtMapFile = NULL;
  7565. delete FDragFakeMonitors;
  7566. FDragFakeMonitors = NULL;
  7567. RemoveDir(ApiPath(FDragFakeDirectory));
  7568. FDragFakeDirectory = L"";
  7569. FFakeFileDropTarget = UnicodeString();
  7570. }
  7571. }
  7572. if (FDragDropSshTerminate.get() != NULL)
  7573. {
  7574. FDragDropSshTerminate->Rethrow();
  7575. }
  7576. }
  7577. //---------------------------------------------------------------------------
  7578. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7579. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7580. {
  7581. // Remember drop effect so we know (when user drops files), if we copy or move
  7582. FLastDropEffect = dwEffect;
  7583. }
  7584. //---------------------------------------------------------------------------
  7585. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7586. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7587. {
  7588. bool Result;
  7589. if (FDDTargetControl == QueueView3)
  7590. {
  7591. Directory = DefaultDownloadTargetDirectory();
  7592. Result = true;
  7593. CounterName = L"DownloadsDragDropQueue";
  7594. ForceQueue = true;
  7595. }
  7596. else if (!FFakeFileDropTarget.IsEmpty())
  7597. {
  7598. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7599. Result = true;
  7600. ForceQueue = false;
  7601. CounterName = L"DownloadsDragDropFakeFile";
  7602. }
  7603. else
  7604. {
  7605. ForceQueue = false;
  7606. Enabled = false;
  7607. try
  7608. {
  7609. int Timer = 0;
  7610. Result = false;
  7611. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7612. {
  7613. {
  7614. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7615. TDragExtCommStruct* CommStruct;
  7616. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7617. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7618. DebugAssert(CommStruct != NULL);
  7619. Result = !CommStruct->Dragging;
  7620. if (Result)
  7621. {
  7622. Directory = ExtractFilePath(CommStruct->DropDest);
  7623. CounterName = L"DownloadsDragDropExternalExt";
  7624. }
  7625. UnmapViewOfFile(CommStruct);
  7626. }
  7627. if (!Result)
  7628. {
  7629. Sleep(50);
  7630. Timer += 50;
  7631. Application->ProcessMessages();
  7632. }
  7633. }
  7634. }
  7635. __finally
  7636. {
  7637. Enabled = true;
  7638. }
  7639. }
  7640. return Result;
  7641. }
  7642. //---------------------------------------------------------------------------
  7643. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7644. const UnicodeString TempDir, int SecDelay)
  7645. {
  7646. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7647. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7648. if (FDelayedDeletionTimer == NULL)
  7649. {
  7650. DebugAssert(HandleAllocated());
  7651. FDelayedDeletionTimer = new TTimer(this);
  7652. FDelayedDeletionTimer->Interval = 10000;
  7653. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7654. }
  7655. else
  7656. {
  7657. FDelayedDeletionTimer->Enabled = true;
  7658. }
  7659. }
  7660. //---------------------------------------------------------------------------
  7661. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7662. {
  7663. DebugAssert(FDelayedDeletionList != NULL);
  7664. TDateTime N = Now();
  7665. TDateTime Alert;
  7666. UnicodeString Directory;
  7667. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7668. {
  7669. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7670. if ((N >= Alert) || (Sender == NULL))
  7671. {
  7672. Directory = FDelayedDeletionList->Strings[Index];
  7673. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7674. {
  7675. FDelayedDeletionList->Delete(Index);
  7676. }
  7677. }
  7678. }
  7679. if (FDelayedDeletionList->Count == 0)
  7680. {
  7681. FDelayedDeletionTimer->Enabled = false;
  7682. }
  7683. }
  7684. //---------------------------------------------------------------------------
  7685. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7686. {
  7687. DebugAssert(!IsLocalBrowserMode());
  7688. if (IsFileControl(FDDTargetControl, osRemote) ||
  7689. (FDDTargetControl == SessionsPageControl))
  7690. {
  7691. TManagedTerminal * TargetTerminal = NULL;
  7692. TFileOperation Operation = foNone;
  7693. if (FDDTargetControl == SessionsPageControl)
  7694. {
  7695. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7696. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7697. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7698. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7699. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7700. {
  7701. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7702. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7703. {
  7704. Operation = foRemoteMove;
  7705. }
  7706. else
  7707. {
  7708. Operation = foRemoteCopy;
  7709. }
  7710. }
  7711. }
  7712. else
  7713. {
  7714. if (FLastDropEffect == DROPEFFECT_MOVE)
  7715. {
  7716. Operation = foRemoteMove;
  7717. }
  7718. else if (FLastDropEffect == DROPEFFECT_COPY)
  7719. {
  7720. Operation = foRemoteCopy;
  7721. }
  7722. }
  7723. if (Operation != foNone)
  7724. {
  7725. RemoteFileControlFileOperation(DropSourceControl,
  7726. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7727. }
  7728. // abort drag&drop
  7729. Abort();
  7730. }
  7731. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7732. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7733. (FDDTargetControl != QueueView3))
  7734. {
  7735. DebugAssert(!FDragTempDir.IsEmpty());
  7736. TTransferType Type;
  7737. UnicodeString TempDir = FDragTempDir;
  7738. // We clear FDragTempDir before calling
  7739. // just in case it fail (raises exception)
  7740. FDragTempDir = L"";
  7741. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7742. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7743. // empty directory parameter means temp directory -> don't display it!
  7744. UnicodeString TargetDir = L"";
  7745. int Options = 0;
  7746. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7747. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7748. {
  7749. Abort();
  7750. }
  7751. // TargetDir is set when dropped on local file control
  7752. // (this was workaround for legacy dirview event handling, now it should be
  7753. // made prettier)
  7754. if (TargetDir.IsEmpty())
  7755. {
  7756. TargetDir = TempDir;
  7757. if (ForceDirectories(ApiPath(TargetDir)))
  7758. {
  7759. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7760. FPendingTempSpaceWarn = true;
  7761. try
  7762. {
  7763. FDragDropOperation = true;
  7764. int Params = cpTemporary |
  7765. (Type == ttMove ? cpDelete : 0);
  7766. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7767. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7768. }
  7769. __finally
  7770. {
  7771. FDragDropOperation = false;
  7772. FPendingTempSpaceWarn = false;
  7773. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7774. }
  7775. }
  7776. else
  7777. {
  7778. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7779. }
  7780. }
  7781. }
  7782. }
  7783. //---------------------------------------------------------------------------
  7784. void __fastcall TCustomScpExplorerForm::DDDownload(
  7785. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7786. {
  7787. DebugAssert(!IsLocalBrowserMode());
  7788. TAutoBatch AutoBatch(this);
  7789. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7790. UpdateCopyParamCounters(*CopyParam);
  7791. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7792. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7793. {
  7794. RemoteDriveView->UpdateDropSource();
  7795. }
  7796. }
  7797. //---------------------------------------------------------------------------
  7798. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7799. {
  7800. public:
  7801. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7802. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7803. {
  7804. }
  7805. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7806. {
  7807. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7808. TDataObjectFilesEx::AllowData(FormatEtc);
  7809. }
  7810. };
  7811. //---------------------------------------------------------------------------
  7812. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7813. TObject * Sender, TDataObject *& DataObject)
  7814. {
  7815. if (FDDExtMapFile != NULL)
  7816. {
  7817. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7818. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7819. {
  7820. Abort();
  7821. }
  7822. if (FileList->Count > 0)
  7823. {
  7824. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7825. if (!FilesObject->IsValid(true, true))
  7826. {
  7827. FilesObject->_Release();
  7828. }
  7829. else
  7830. {
  7831. DataObject = FilesObject;
  7832. }
  7833. }
  7834. }
  7835. }
  7836. //---------------------------------------------------------------------------
  7837. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7838. {
  7839. DebugFail();
  7840. }
  7841. //---------------------------------------------------------------------------
  7842. void __fastcall TCustomScpExplorerForm::GoToTree()
  7843. {
  7844. ComponentVisible[fcRemoteTree] = true;
  7845. DriveView(osRemote)->SetFocus();
  7846. }
  7847. //---------------------------------------------------------------------------
  7848. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7849. TPanelExport Export)
  7850. {
  7851. TCustomDirView * DirView = this->DirView(Side);
  7852. std::unique_ptr<TStrings> ExportData(new TStringList());
  7853. switch (Export)
  7854. {
  7855. case pePath:
  7856. ExportData->Add(DirView->PathName);
  7857. break;
  7858. case peFileList:
  7859. case peFullFileList:
  7860. {
  7861. bool FullPath = (Export == peFullFileList);
  7862. DirView->CreateFileList(false, FullPath, ExportData.get());
  7863. for (int Index = 0; Index < ExportData->Count; Index++)
  7864. {
  7865. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7866. {
  7867. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7868. }
  7869. }
  7870. }
  7871. break;
  7872. default:
  7873. DebugFail();
  7874. }
  7875. return ExportData.release();
  7876. }
  7877. //---------------------------------------------------------------------------
  7878. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7879. TPanelExport Export, TPanelExportDestination Destination)
  7880. {
  7881. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7882. PanelExportStore(Side, Export, Destination, ExportData.get());
  7883. }
  7884. //---------------------------------------------------------------------------
  7885. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7886. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7887. TStrings * ExportData)
  7888. {
  7889. if (Destination == pedClipboard)
  7890. {
  7891. TInstantOperationVisualizer Visualizer;
  7892. CopyToClipboard(ExportData);
  7893. }
  7894. else
  7895. {
  7896. DebugFail();
  7897. }
  7898. }
  7899. //---------------------------------------------------------------------------
  7900. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7901. {
  7902. TCustomDirView * DirView = this->DirView(Side);
  7903. UnicodeString Mask = DirView->Mask;
  7904. if (DoFilterMaskDialog(DirView, Mask))
  7905. {
  7906. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7907. Configuration->Usage->Inc(L"Filters");
  7908. }
  7909. }
  7910. //---------------------------------------------------------------------------
  7911. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7912. {
  7913. return FQueueController->DefaultOperation();
  7914. }
  7915. //---------------------------------------------------------------------------
  7916. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7917. TQueueOperation Operation, void ** Param)
  7918. {
  7919. switch (Operation)
  7920. {
  7921. case qoPreferences:
  7922. return true;
  7923. case qoGoTo:
  7924. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7925. case qoOnceEmpty:
  7926. return IsAnythingQueued();
  7927. default:
  7928. return FQueueController->AllowOperation(Operation, Param);
  7929. }
  7930. }
  7931. //---------------------------------------------------------------------------
  7932. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7933. {
  7934. if (DebugAlwaysTrue(QueueView3->Visible))
  7935. {
  7936. QueueView3->SetFocus();
  7937. }
  7938. }
  7939. //---------------------------------------------------------------------------
  7940. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7941. TQueueOperation Operation, void * Param)
  7942. {
  7943. if (Operation == qoGoTo)
  7944. {
  7945. GoToQueue();
  7946. }
  7947. else if (Operation == qoPreferences)
  7948. {
  7949. PreferencesDialog(pmQueue);
  7950. }
  7951. else
  7952. {
  7953. FQueueController->ExecuteOperation(Operation, Param);
  7954. }
  7955. }
  7956. //---------------------------------------------------------------------------
  7957. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7958. {
  7959. return (Queue != NULL) && Queue->Enabled;
  7960. }
  7961. //---------------------------------------------------------------------------
  7962. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7963. {
  7964. DebugAssert(Queue != NULL);
  7965. if (Queue != NULL)
  7966. {
  7967. Queue->Enabled = !Queue->Enabled;
  7968. }
  7969. }
  7970. //---------------------------------------------------------------------------
  7971. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7972. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7973. {
  7974. FQueueActedItem = QueueView3->ItemFocused;
  7975. }
  7976. //---------------------------------------------------------------------------
  7977. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7978. {
  7979. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  7980. }
  7981. //---------------------------------------------------------------------------
  7982. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7983. {
  7984. return
  7985. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7986. (QueueDock->Visible ? QueueDock->Height : 0) +
  7987. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7988. }
  7989. //---------------------------------------------------------------------------
  7990. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7991. {
  7992. return ScaleByTextHeight(this, 48);
  7993. }
  7994. //---------------------------------------------------------------------------
  7995. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7996. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7997. {
  7998. // when queue is hidden by double-clicking splitter, stray attempt to
  7999. // resize the panel with strange value arrives, make sure it is ignored
  8000. if (ComponentVisible[fcQueueView])
  8001. {
  8002. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  8003. if (NewSize > MaxHeightLimit)
  8004. {
  8005. NewSize = MaxHeightLimit;
  8006. }
  8007. int MinHeightLimit =
  8008. GetStaticQueuePanelComponentsHeight() +
  8009. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  8010. GetMinQueueViewHeight();
  8011. if (NewSize < MinHeightLimit)
  8012. {
  8013. NewSize = MinHeightLimit;
  8014. }
  8015. }
  8016. else
  8017. {
  8018. Accept = false;
  8019. }
  8020. }
  8021. //---------------------------------------------------------------------------
  8022. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  8023. {
  8024. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  8025. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  8026. if (QueueViewHeight < GetMinQueueViewHeight())
  8027. {
  8028. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  8029. }
  8030. }
  8031. //---------------------------------------------------------------------------
  8032. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  8033. TDragObject *& /*DragObject*/)
  8034. {
  8035. FQueueActedItem = QueueView3->ItemFocused;
  8036. FQueueScrollOnDragOver->StartDrag();
  8037. }
  8038. //---------------------------------------------------------------------------
  8039. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  8040. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  8041. {
  8042. FQueueScrollOnDragOver->EndDrag();
  8043. }
  8044. //---------------------------------------------------------------------------
  8045. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  8046. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  8047. {
  8048. Accept = true;
  8049. if (Source == QueueView3)
  8050. {
  8051. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  8052. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  8053. if (Accept)
  8054. {
  8055. TQueueItemProxy * QueueItem;
  8056. TQueueItemProxy * DestQueueItem;
  8057. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8058. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  8059. Accept = (QueueItem != DestQueueItem) &&
  8060. (QueueItem->Status == TQueueItem::qsPending) &&
  8061. (DestQueueItem->Status == TQueueItem::qsPending);
  8062. }
  8063. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  8064. }
  8065. }
  8066. //---------------------------------------------------------------------------
  8067. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  8068. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8069. {
  8070. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8071. {
  8072. TQueueItemProxy * QueueItem;
  8073. TQueueItemProxy * DestQueueItem;
  8074. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8075. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8076. QueueItem->Move(DestQueueItem);
  8077. }
  8078. }
  8079. //---------------------------------------------------------------------------
  8080. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8081. {
  8082. if ((QueueView3->ItemFocused == NULL) &&
  8083. (QueueView3->Items->Count > 0))
  8084. {
  8085. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8086. }
  8087. QueueLabelUpdateStatus();
  8088. }
  8089. //---------------------------------------------------------------------------
  8090. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8091. {
  8092. QueueLabelUpdateStatus();
  8093. }
  8094. //---------------------------------------------------------------------------
  8095. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8096. {
  8097. QueueLabel->UpdateStatus();
  8098. }
  8099. //---------------------------------------------------------------------------
  8100. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8101. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8102. {
  8103. if (Selected)
  8104. {
  8105. NonVisualDataModule->UpdateNonVisibleActions();
  8106. }
  8107. }
  8108. //---------------------------------------------------------------------------
  8109. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8110. {
  8111. TDragDropFilesEx * Result = NULL;
  8112. if (Sender == SessionsPageControl)
  8113. {
  8114. Result = FSessionsDragDropFilesEx;
  8115. }
  8116. else if (Sender == QueueView3)
  8117. {
  8118. Result = FQueueDragDropFilesEx;
  8119. }
  8120. else
  8121. {
  8122. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8123. if (DirView != NULL)
  8124. {
  8125. Result = DirView->DragDropFilesEx;
  8126. }
  8127. else
  8128. {
  8129. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8130. if (DriveView != NULL)
  8131. {
  8132. Result = DriveView->DragDropFilesEx;
  8133. }
  8134. }
  8135. }
  8136. DebugAssert(Result != NULL);
  8137. return Result;
  8138. }
  8139. //---------------------------------------------------------------------------
  8140. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8141. {
  8142. return (DView->SelCount == DView->FilesCount);
  8143. }
  8144. //---------------------------------------------------------------------------
  8145. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8146. {
  8147. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8148. }
  8149. //---------------------------------------------------------------------------
  8150. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8151. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8152. {
  8153. DebugAssert(!IsLocalBrowserMode());
  8154. TFileOperation Operation;
  8155. switch (Effect)
  8156. {
  8157. case DROPEFFECT_MOVE:
  8158. Operation = foMove;
  8159. break;
  8160. case DROPEFFECT_COPY:
  8161. // occures on WinXP (reported by user)
  8162. default:
  8163. Operation = foCopy;
  8164. break;
  8165. };
  8166. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8167. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8168. if (DragDropFilesEx->FileList->Count > 0)
  8169. {
  8170. TStrings * FileList = new TStringList();
  8171. try
  8172. {
  8173. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8174. {
  8175. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8176. }
  8177. FDragDropOperation = true;
  8178. TTransferOperationParam Param;
  8179. Param.TargetDirectory = TargetPath;
  8180. // upload, no temp dirs
  8181. Param.Temp = false;
  8182. Param.DragDrop = DragDrop;
  8183. Param.Options =
  8184. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8185. if (ForceQueue)
  8186. {
  8187. Param.Queue = asOn;
  8188. }
  8189. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8190. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8191. {
  8192. if (IsFileControl(DropSourceControl, osLocal))
  8193. {
  8194. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8195. }
  8196. else
  8197. {
  8198. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8199. }
  8200. }
  8201. }
  8202. __finally
  8203. {
  8204. FDragDropOperation = false;
  8205. delete FileList;
  8206. }
  8207. }
  8208. }
  8209. //---------------------------------------------------------------------------
  8210. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8211. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8212. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8213. {
  8214. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8215. }
  8216. //---------------------------------------------------------------------------
  8217. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8218. TObject * Sender, int grfKeyState, int & dwEffect)
  8219. {
  8220. DebugAssert(!IsLocalBrowserMode());
  8221. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8222. if ((dwEffect != DROPEFFECT_NONE) &&
  8223. IsFileControl(DropSourceControl, osRemote))
  8224. {
  8225. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8226. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8227. (RemoteDirView->DropTarget == NULL))
  8228. {
  8229. dwEffect = DROPEFFECT_NONE;
  8230. }
  8231. else
  8232. {
  8233. if (dwEffect == DROPEFFECT_COPY)
  8234. {
  8235. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8236. // currently we support copying always (at least via temporary directory);
  8237. // remove associated checks once this all proves stable and working
  8238. bool CopyCapable = true;
  8239. // if we do not support neither of operations, there's no discussion
  8240. if (!MoveCapable && !CopyCapable)
  8241. {
  8242. dwEffect = DROPEFFECT_NONE;
  8243. }
  8244. else
  8245. {
  8246. // The default effect inside remote panel is move,
  8247. // unless we do not support it, but support copy
  8248. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8249. {
  8250. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8251. }
  8252. else
  8253. {
  8254. // with ctrl-down, we want copy unless it is not supported
  8255. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8256. }
  8257. }
  8258. }
  8259. }
  8260. }
  8261. }
  8262. //---------------------------------------------------------------------------
  8263. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8264. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8265. {
  8266. DebugAssert(!IsLocalBrowserMode());
  8267. if (FDDTotalSize >= 0)
  8268. {
  8269. if (File->IsDirectory)
  8270. {
  8271. FDDTotalSize = -1;
  8272. }
  8273. else
  8274. {
  8275. FDDTotalSize += File->Size;
  8276. }
  8277. }
  8278. DebugAssert(!FDragTempDir.IsEmpty());
  8279. // TODO: this is quite ineffective
  8280. // TODO: what if invalid character replacement is disabled?
  8281. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8282. UnicodeString TransferFileName;
  8283. if (Sender == RemoteDriveView)
  8284. {
  8285. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8286. }
  8287. else
  8288. {
  8289. TransferFileName = File->FileName;
  8290. }
  8291. FDDFileList->AddObject(TransferFileName, File);
  8292. }
  8293. //---------------------------------------------------------------------------
  8294. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8295. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8296. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8297. {
  8298. // sometimes we do not get DDEnd so the list is not released
  8299. SAFE_DESTROY(FDDFileList);
  8300. FDDFileList = new TStringList();
  8301. FDragTempDir = WinConfiguration->TemporaryDir();
  8302. FDDTotalSize = 0;
  8303. FDragDropSshTerminate.reset(NULL);
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8307. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8308. HRESULT & Result)
  8309. {
  8310. if (Result == DRAGDROP_S_DROP)
  8311. {
  8312. try
  8313. {
  8314. GlobalDragImageList->HideDragImage();
  8315. try
  8316. {
  8317. RemoteFileControlDDTargetDrop();
  8318. }
  8319. catch(ESshTerminate & E)
  8320. {
  8321. FDragDropSshTerminate.reset(E.Clone());
  8322. }
  8323. }
  8324. catch (Exception &E)
  8325. {
  8326. // If downloading fails we need to cancel drag&drop, otherwise
  8327. // Explorer shows error
  8328. // But by the way exception probably never reach this point as
  8329. // it's catched on way.
  8330. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8331. Result = DRAGDROP_S_CANCEL;
  8332. ShowExtendedException(Terminal, &E);
  8333. }
  8334. }
  8335. }
  8336. //---------------------------------------------------------------------------
  8337. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8338. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8339. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8340. {
  8341. TFileMasks::TParams MaskParams;
  8342. MaskParams.Size = Size;
  8343. MaskParams.Modification = Modification;
  8344. // this does not re-parse the mask if it is the same as the last time
  8345. FDirViewMatchMask = Masks;
  8346. bool ImplicitMatch;
  8347. Matches =
  8348. // Local and RecurseInclude parameters have no effect as there's no path.
  8349. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8350. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8351. (AllowImplicitMatches || !ImplicitMatch);
  8352. }
  8353. //---------------------------------------------------------------------------
  8354. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8355. TObject * Sender, TListItem * Item, WORD & Indexes)
  8356. {
  8357. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8358. UnicodeString Ext;
  8359. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8360. {
  8361. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8362. }
  8363. else
  8364. {
  8365. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8366. }
  8367. if (SameText(Ext, Configuration->PartialExt))
  8368. {
  8369. Indexes |= oiPartial;
  8370. }
  8371. }
  8372. //---------------------------------------------------------------------------
  8373. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8374. {
  8375. return
  8376. DirViewEnabled(osCurrent) &&
  8377. DirView(osCurrent)->CanPasteFromClipBoard();
  8378. }
  8379. //---------------------------------------------------------------------------
  8380. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8381. {
  8382. bool Result = false;
  8383. if (CanPasteToDirViewFromClipBoard())
  8384. {
  8385. Result = true;
  8386. }
  8387. else
  8388. {
  8389. UnicodeString ClipboardText;
  8390. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8391. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8392. {
  8393. if (StoredSessions->IsUrl(ClipboardText))
  8394. {
  8395. Result = true;
  8396. }
  8397. else
  8398. {
  8399. Result = DirViewEnabled(osCurrent);
  8400. }
  8401. }
  8402. }
  8403. return Result;
  8404. }
  8405. //---------------------------------------------------------------------------
  8406. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8407. {
  8408. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8409. if (DoesClipboardContainOurFiles())
  8410. {
  8411. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8412. {
  8413. DebugAssert(!IsLocalBrowserMode());
  8414. TTerminalManager * Manager = TTerminalManager::Instance();
  8415. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8416. Manager->ActiveSession = FClipboardTerminal;
  8417. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8418. }
  8419. }
  8420. else if (CanPasteToDirViewFromClipBoard())
  8421. {
  8422. DirView(osCurrent)->PasteFromClipBoard();
  8423. }
  8424. else
  8425. {
  8426. UnicodeString ClipboardText;
  8427. if (NonEmptyTextFromClipboard(ClipboardText))
  8428. {
  8429. if (StoredSessions->IsUrl(ClipboardText))
  8430. {
  8431. NewSession(ClipboardText);
  8432. }
  8433. else
  8434. {
  8435. DirView(osCurrent)->Path = ClipboardText;
  8436. }
  8437. }
  8438. }
  8439. }
  8440. //---------------------------------------------------------------------------
  8441. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8442. {
  8443. // TBD
  8444. }
  8445. //---------------------------------------------------------------------------
  8446. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8447. {
  8448. TCustomDirView * ADirView = DirView(Side);
  8449. ADirView->SelectAll(Mode);
  8450. ADirView->SetFocus();
  8451. }
  8452. //---------------------------------------------------------------------------
  8453. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8454. {
  8455. TCustomDirView * ADirView = DirView(Side);
  8456. TFileFilter Filter;
  8457. DefaultFileFilter(Filter);
  8458. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8459. {
  8460. Configuration->Usage->Inc(L"MaskSelections");
  8461. ADirView->SelectFiles(Filter, Select);
  8462. ADirView->SetFocus();
  8463. }
  8464. }
  8465. //---------------------------------------------------------------------------
  8466. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8467. {
  8468. TCustomDirView * ADirView = DirView(Side);
  8469. ADirView->RestoreSelectedNames();
  8470. ADirView->SetFocus();
  8471. }
  8472. //---------------------------------------------------------------------------
  8473. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8474. {
  8475. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8476. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8477. {
  8478. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8479. UnicodeString Ext;
  8480. if (!IsSideLocalBrowser(osCurrent))
  8481. {
  8482. Ext = UnixExtractFileExt(FileName);
  8483. }
  8484. else
  8485. {
  8486. Ext = ExtractFileExt(FileName);
  8487. }
  8488. if (Ext.IsEmpty())
  8489. {
  8490. Ext = L".";
  8491. }
  8492. TFileFilter Filter;
  8493. Filter.Masks = FORMAT(L"*%s", (Ext));
  8494. Filter.Directories = false;
  8495. CurrentDirView->SelectFiles(Filter, Select);
  8496. }
  8497. }
  8498. //---------------------------------------------------------------------------
  8499. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8500. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8501. {
  8502. UnicodeString Result;
  8503. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8504. {
  8505. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8506. }
  8507. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8508. {
  8509. // noop
  8510. }
  8511. else
  8512. {
  8513. Result =
  8514. FMTLOAD(FILE_INFO_FORMAT,
  8515. (FormatBytes(FileInfo.SelectedSize),
  8516. FormatBytes(FileInfo.FilesSize),
  8517. FormatNumber(FileInfo.SelectedCount),
  8518. FormatNumber(FileInfo.FilesCount)));
  8519. }
  8520. return Result;
  8521. }
  8522. //---------------------------------------------------------------------------
  8523. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8524. TTBXStatusPanel * Panel, TOperationSide Side)
  8525. {
  8526. if (Panel->Index == 1)
  8527. {
  8528. ToggleShowHiddenFiles();
  8529. }
  8530. else if (Panel->Index == 2)
  8531. {
  8532. Filter(Side);
  8533. }
  8534. }
  8535. //---------------------------------------------------------------------------
  8536. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8537. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8538. {
  8539. DebugAssert(!StatusBar->SimplePanel);
  8540. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8541. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8542. }
  8543. //---------------------------------------------------------------------------
  8544. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8545. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8546. {
  8547. DebugAssert(StatusBar->Panels->Count >= 3);
  8548. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8549. if (FileInfo.HiddenCount > 0)
  8550. {
  8551. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8552. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8553. }
  8554. else
  8555. {
  8556. HiddenFilesPanel->ViewPriority = 0;
  8557. // not really necessary, just to cleanup no-longer-valid data
  8558. HiddenFilesPanel->Caption = L"";
  8559. }
  8560. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8561. if (FileInfo.FilteredCount > 0)
  8562. {
  8563. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8564. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8565. }
  8566. else
  8567. {
  8568. FilteredFilesPanel->ViewPriority = 0;
  8569. // not really necessary, just to cleanup no-longer-valid data
  8570. FilteredFilesPanel->Caption = L"";
  8571. }
  8572. }
  8573. //---------------------------------------------------------------------------
  8574. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8575. TObject * /*Sender*/)
  8576. {
  8577. if (DirView(osOther)->Enabled)
  8578. {
  8579. DirView(osOther)->SetFocus();
  8580. }
  8581. }
  8582. //---------------------------------------------------------------------------
  8583. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8584. {
  8585. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8586. switch (Config.Show)
  8587. {
  8588. case qvShow:
  8589. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8590. {
  8591. Config.Show = qvHide;
  8592. }
  8593. else
  8594. {
  8595. Config.Show = Config.LastHideShow;
  8596. }
  8597. break;
  8598. case qvHideWhenEmpty:
  8599. if (ComponentVisible[fcQueueView])
  8600. {
  8601. Config.Show = qvHide;
  8602. }
  8603. else
  8604. {
  8605. Config.LastHideShow = Config.Show;
  8606. Config.Show = qvShow;
  8607. }
  8608. break;
  8609. case qvHide:
  8610. Config.LastHideShow = Config.Show;
  8611. Config.Show = qvShow;
  8612. break;
  8613. }
  8614. WinConfiguration->QueueView = Config;
  8615. }
  8616. //---------------------------------------------------------------------------
  8617. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8618. {
  8619. UnicodeString Result;
  8620. if (Terminal != NULL)
  8621. {
  8622. switch (WinConfiguration->PathInCaption)
  8623. {
  8624. case picShort:
  8625. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8626. break;
  8627. case picFull:
  8628. Result = Terminal->CurrentDirectory;
  8629. break;
  8630. }
  8631. }
  8632. return Result;
  8633. }
  8634. //---------------------------------------------------------------------------
  8635. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8636. {
  8637. TColor Result = GetWindowColor(FSessionColor);
  8638. return Result;
  8639. }
  8640. //---------------------------------------------------------------------------
  8641. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8642. {
  8643. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8644. return Result;
  8645. }
  8646. //---------------------------------------------------------------------------
  8647. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8648. {
  8649. if (!FSessionChanging)
  8650. {
  8651. TTerminalManager::Instance()->UpdateAppTitle();
  8652. }
  8653. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8654. // background to gray on Windows 7 (works on Windows 10).
  8655. // See also EnableControl
  8656. if (Showing)
  8657. {
  8658. if (ManagedSession != NULL)
  8659. {
  8660. // Update path when it changes
  8661. if ((SessionsPageControl->ActivePage != NULL) &&
  8662. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8663. {
  8664. UpdateSessionTab(SessionsPageControl->ActivePage);
  8665. }
  8666. }
  8667. NonVisualDataModule->ReconnectSessionAction->Update();
  8668. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8669. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8670. CenterReconnectToolbar();
  8671. bool HasTerminal = HasActiveTerminal();
  8672. RemoteDirView->Enabled = HasTerminal;
  8673. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8674. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8675. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8676. RemoteDriveView->Color = RemoteDirView->Color;
  8677. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8678. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8679. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8680. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8681. QueueFileList->Enabled = QueueView3->Enabled;
  8682. QueueFileList->Color = QueueView3->Color;
  8683. QueueFileList->Font->Color = QueueView3->Font->Color;
  8684. QueueLabelUpdateStatus();
  8685. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8686. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8687. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8688. {
  8689. UpdateDarkMode();
  8690. }
  8691. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8692. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8693. }
  8694. }
  8695. //---------------------------------------------------------------------------
  8696. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8697. {
  8698. UpdateControls();
  8699. }
  8700. //---------------------------------------------------------------------------
  8701. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8702. TObject * Sender)
  8703. {
  8704. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8705. DebugAssert(DirView != NULL);
  8706. DoDirViewLoaded(DirView);
  8707. TransferPresetAutoSelect();
  8708. }
  8709. //---------------------------------------------------------------------------
  8710. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8711. {
  8712. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8713. // first run after installation
  8714. if (double(Updates.LastCheck) == 0)
  8715. {
  8716. AppLog(L"First run, scheduling updates check for the next run");
  8717. // make sure next time there will be an update (if enabled)
  8718. Updates.LastCheck = TDateTime(1);
  8719. WinConfiguration->Updates = Updates;
  8720. }
  8721. else
  8722. {
  8723. TDateTime Period;
  8724. if (IsUWP() && !IsOfficialPackage())
  8725. {
  8726. Period = DefaultUpdatesPeriod;
  8727. AppLog(L"Thirdparty UWP package, using default updates check period");
  8728. }
  8729. else
  8730. {
  8731. Period = Updates.Period;
  8732. }
  8733. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8734. if (double(Period) > 0)
  8735. {
  8736. TDateTime Interval = Now() - Updates.LastCheck;
  8737. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8738. if (Interval >= Period)
  8739. {
  8740. StartUpdateThread(UpdatesChecked);
  8741. }
  8742. }
  8743. }
  8744. }
  8745. //---------------------------------------------------------------------------
  8746. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8747. {
  8748. AppLog(L"Updates check done");
  8749. UnicodeString Message;
  8750. bool New;
  8751. TQueryType Type;
  8752. GetUpdatesMessage(Message, New, Type, false);
  8753. if (!Message.IsEmpty())
  8754. {
  8755. AppLogFmt(L"Updates check message: %s", (Message));
  8756. if (New)
  8757. {
  8758. AppLog(L"New version detected");
  8759. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8760. }
  8761. if (!New && (Type != qtWarning))
  8762. {
  8763. AppLog(L"Posting non-critical updates note");
  8764. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8765. }
  8766. else
  8767. {
  8768. AppLog(L"Posting new version or critical message notification");
  8769. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8770. }
  8771. if (New)
  8772. {
  8773. Configuration->Usage->Inc(L"UpdateNotifications");
  8774. }
  8775. }
  8776. }
  8777. //---------------------------------------------------------------------------
  8778. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8779. {
  8780. RestoreApp();
  8781. bool CanDisplay = !NonVisualDataModule->Busy;
  8782. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8783. {
  8784. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8785. }
  8786. if (CanDisplay)
  8787. {
  8788. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8789. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8790. NonVisualDataModule->StartBusy();
  8791. try
  8792. {
  8793. CheckForUpdates(true);
  8794. }
  8795. __finally
  8796. {
  8797. NonVisualDataModule->EndBusy();
  8798. }
  8799. }
  8800. else
  8801. {
  8802. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8803. }
  8804. }
  8805. //---------------------------------------------------------------------------
  8806. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8807. TCopyParamRuleData & Data)
  8808. {
  8809. DebugAssert(Terminal != NULL);
  8810. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8811. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8812. Data.RemoteDirectory = RemoteDirView->PathName;
  8813. }
  8814. //---------------------------------------------------------------------------
  8815. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8816. {
  8817. // Terminal can be null when we are changing local directory implicitly,
  8818. // because it has been deleted, while no session is connected
  8819. // (Login dialog is open)
  8820. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8821. {
  8822. DebugAssert(!IsLocalBrowserMode());
  8823. TCopyParamRuleData Data;
  8824. GetTransferPresetAutoSelectData(Data);
  8825. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8826. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8827. if (CopyParamIndex < 0)
  8828. {
  8829. // there is no preset that matches autoselection
  8830. // set preset that we consider "default"
  8831. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8832. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8833. }
  8834. else
  8835. {
  8836. // there is preset matching autoselection
  8837. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8838. if (CopyParamName == FCopyParamAutoSelected)
  8839. {
  8840. // autoselected the same preset as the last time
  8841. // make no change (i.e. preserve custom user preset, if any)
  8842. }
  8843. else
  8844. {
  8845. // autoselected the different preset then the last time (or there
  8846. // was default preset set)
  8847. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8848. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8849. }
  8850. }
  8851. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8852. {
  8853. if (CopyParamIndex >= 0)
  8854. {
  8855. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8856. }
  8857. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8858. try
  8859. {
  8860. int Fmt =
  8861. (CopyParamIndex < 0) ?
  8862. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8863. COPY_PARAM_AUTOSELECTED;
  8864. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8865. Data->Message = MainInstructions(Message);
  8866. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8867. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8868. CopyParamAttrs);
  8869. if (CopyParamIndex >= 0)
  8870. {
  8871. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8872. Info = FORMAT(L"%s\n \n%s", (Info,
  8873. FMTLOAD(COPY_PARAM_RULE,
  8874. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8875. }
  8876. Data->Message += L"\n\n" + Info;
  8877. if (WinConfiguration->CopyParamAutoSelectNotice)
  8878. {
  8879. TransferPresetNoteMessage(Data, true);
  8880. }
  8881. else
  8882. {
  8883. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8884. Data = NULL; // ownership passed
  8885. }
  8886. }
  8887. __finally
  8888. {
  8889. delete Data;
  8890. }
  8891. }
  8892. }
  8893. }
  8894. //---------------------------------------------------------------------------
  8895. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8896. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8897. {
  8898. DebugAssert(NoteData != NULL);
  8899. DebugAssert(!IsLocalBrowserMode());
  8900. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8901. TQueryButtonAlias Aliases[1];
  8902. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8903. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8904. Params.Aliases = Aliases;
  8905. Params.AliasesCount = LENOF(Aliases);
  8906. unsigned int Result =
  8907. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8908. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8909. switch (Result)
  8910. {
  8911. case qaNeverAskAgain:
  8912. DebugAssert(AllowNeverAskAgain);
  8913. WinConfiguration->CopyParamAutoSelectNotice = false;
  8914. break;
  8915. case qaIgnore:
  8916. PreferencesDialog(pmPresets);
  8917. break;
  8918. }
  8919. }
  8920. //---------------------------------------------------------------------------
  8921. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8922. {
  8923. // as of now useless, as this is used for notes only, never for balloons, ...
  8924. RestoreApp();
  8925. // .. and we should never be busy here
  8926. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8927. {
  8928. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8929. }
  8930. }
  8931. //---------------------------------------------------------------------------
  8932. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8933. TPreferencesMode APreferencesMode)
  8934. {
  8935. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8936. TCopyParamRuleData Data;
  8937. if (Terminal != NULL)
  8938. {
  8939. PreferencesData.reset(new TPreferencesDialogData());
  8940. GetTransferPresetAutoSelectData(Data);
  8941. PreferencesData->CopyParamRuleData = &Data;
  8942. }
  8943. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8944. }
  8945. //---------------------------------------------------------------------------
  8946. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8947. const TCustomCommandType & Command)
  8948. {
  8949. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8950. {
  8951. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8952. }
  8953. }
  8954. //---------------------------------------------------------------------------
  8955. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8956. {
  8957. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8958. TCustomCommandType Command;
  8959. // make sure we use local custom command when remote are not supported
  8960. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8961. {
  8962. Command = FLastCustomCommand;
  8963. }
  8964. else
  8965. {
  8966. Command.Params = Command.Params | ccLocal;
  8967. }
  8968. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8969. FEditingFocusedAdHocCommand = OnFocused;
  8970. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8971. int Options =
  8972. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8973. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8974. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8975. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8976. {
  8977. FLastCustomCommand = Command;
  8978. UpdateCustomCommandsToolbar();
  8979. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8980. }
  8981. }
  8982. //---------------------------------------------------------------------------
  8983. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8984. {
  8985. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8986. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8987. DebugAssert(State > 0);
  8988. if (State <= 0)
  8989. {
  8990. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8991. }
  8992. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8993. }
  8994. //---------------------------------------------------------------------------
  8995. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8996. TCustomCommandType & Command, int & State)
  8997. {
  8998. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8999. if (Result)
  9000. {
  9001. Command = FLastCustomCommand;
  9002. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9003. }
  9004. return Result;
  9005. }
  9006. //---------------------------------------------------------------------------
  9007. void __fastcall TCustomScpExplorerForm::BeforeAction()
  9008. {
  9009. ResetIncrementalSearch();
  9010. }
  9011. //---------------------------------------------------------------------------
  9012. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  9013. {
  9014. DebugAssert(ComponentVisible[Component]);
  9015. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  9016. }
  9017. //---------------------------------------------------------------------------
  9018. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  9019. {
  9020. // when queue panel is resized here directly, the status bar is stretched
  9021. // over whole space the panel occupied
  9022. PostComponentHide(fcQueueView);
  9023. }
  9024. //---------------------------------------------------------------------------
  9025. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  9026. {
  9027. ComponentVisible[fcQueueFileList] = false;
  9028. }
  9029. //---------------------------------------------------------------------------
  9030. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  9031. {
  9032. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  9033. RefreshColorMode();
  9034. ResetSysDarkTheme();
  9035. ConfigurationChanged();
  9036. ConfigureInterface();
  9037. // Should be called for all controls
  9038. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  9039. }
  9040. //---------------------------------------------------------------------------
  9041. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  9042. {
  9043. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  9044. if (!FInvalid &&
  9045. (Message.LParam != 0) &&
  9046. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  9047. {
  9048. ThemeChanged();
  9049. }
  9050. TForm::Dispatch(&Message);
  9051. }
  9052. //---------------------------------------------------------------------------
  9053. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  9054. {
  9055. TMessage * M = static_cast<TMessage*>(Message);
  9056. switch (M->Msg)
  9057. {
  9058. case CM_DIALOGCHAR:
  9059. CMDialogChar(*M);
  9060. break;
  9061. case WM_APPCOMMAND:
  9062. WMAppCommand(*M);
  9063. break;
  9064. case WM_SYSCOMMAND:
  9065. WMSysCommand(*M);
  9066. break;
  9067. case WM_QUERYENDSESSION:
  9068. WMQueryEndSession(*M);
  9069. break;
  9070. case WM_ENDSESSION:
  9071. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9072. break;
  9073. #ifdef _DEBUG
  9074. case WM_WINDOWPOSCHANGED:
  9075. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9076. break;
  9077. case CM_CUSTOMSTYLECHANGED:
  9078. TForm::Dispatch(Message);
  9079. break;
  9080. #endif
  9081. case WM_COMPONENT_HIDE:
  9082. {
  9083. Byte Component = static_cast<Byte>(M->WParam);
  9084. // sanity check
  9085. if (ComponentVisible[Component])
  9086. {
  9087. // special treatment
  9088. if (Component == fcQueueView)
  9089. {
  9090. ToggleQueueVisibility();
  9091. DebugAssert(!ComponentVisible[fcQueueView]);
  9092. }
  9093. else
  9094. {
  9095. ComponentVisible[Component] = false;
  9096. }
  9097. }
  9098. }
  9099. break;
  9100. case WM_COPYDATA:
  9101. WMCopyData(*M);
  9102. break;
  9103. case WM_MANAGES_CAPTION:
  9104. // caption managed in TTerminalManager::UpdateAppTitle()
  9105. M->Result = 1;
  9106. break;
  9107. case WM_WANTS_MOUSEWHEEL:
  9108. M->Result = 1;
  9109. break;
  9110. case CM_SHOWINGCHANGED:
  9111. CMShowingChanged(*M);
  9112. break;
  9113. case WM_CLOSE:
  9114. WMClose(*M);
  9115. break;
  9116. case WM_DPICHANGED:
  9117. WMDpiChanged(*M);
  9118. break;
  9119. case CM_DPICHANGED:
  9120. CMDpiChanged(*M);
  9121. break;
  9122. case WM_SETTINGCHANGE:
  9123. WMSettingChange(*M);
  9124. break;
  9125. case CM_DIALOGKEY:
  9126. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9127. break;
  9128. case WM_PASTE_FILES:
  9129. PasteFiles();
  9130. break;
  9131. default:
  9132. TForm::Dispatch(Message);
  9133. break;
  9134. }
  9135. }
  9136. //---------------------------------------------------------------------------
  9137. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9138. {
  9139. // noop
  9140. }
  9141. //---------------------------------------------------------------------------
  9142. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9143. {
  9144. TForm::Dispatch(&Message);
  9145. FSessionColors->Clear();
  9146. AddFixedSessionImages();
  9147. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9148. UpdateImages();
  9149. }
  9150. //---------------------------------------------------------------------------
  9151. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9152. {
  9153. TForm::Dispatch(&Message);
  9154. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9155. UpdatePixelsPerInchMainWindowCounter();
  9156. }
  9157. //---------------------------------------------------------------------------
  9158. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9159. {
  9160. // Cannot close window while we are busy.
  9161. // We cannot test this in FormCloseQuery as that is called also from
  9162. // Close(), which is called by CloseApplicationAction. So we can be busy
  9163. // there already even, when it is legitimate to close the application.
  9164. // Possibly a better place to handle this would be WMSysCommand.
  9165. if (NonVisualDataModule->Busy)
  9166. {
  9167. Message.Result = 1;
  9168. }
  9169. else
  9170. {
  9171. TForm::Dispatch(&Message);
  9172. }
  9173. }
  9174. //---------------------------------------------------------------------------
  9175. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9176. {
  9177. if (!FShowingChanged)
  9178. {
  9179. AddStartupSequence(L"D");
  9180. FShowingChanged = true;
  9181. }
  9182. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9183. bool NoSession = (ManagedSession == NULL);
  9184. // This calls TCustomScpExplorerForm::DoShow
  9185. TForm::Dispatch(&Message);
  9186. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9187. if (Showing && !FStarted)
  9188. {
  9189. FStarted = true;
  9190. InterfaceStarted();
  9191. }
  9192. if (Showing && NoSession)
  9193. {
  9194. // When we are starting minimized (i.e. from an installer),
  9195. // postpone showing Login dialog until we get restored.
  9196. // Otherwise the Login dialog (and Authentication window) show restored
  9197. // over invisible (minimized) main window.
  9198. if (WindowState == wsMinimized)
  9199. {
  9200. FNeedSession = true;
  9201. }
  9202. else
  9203. {
  9204. // This happens before application ever goes idle, so the toolbars would
  9205. // stay enabled (initial state) until the Login dialog is dismissed.
  9206. UpdateActions();
  9207. NonVisualDataModule->StartBusy();
  9208. try
  9209. {
  9210. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9211. // otherwise the dialog does not receive focus.
  9212. // With Commander interface the ProcessMessages is called already
  9213. // by TDriveView, but with Explorer interface, we need to call it explicily
  9214. Application->ProcessMessages();
  9215. // do not reload sessions, they have been loaded just now (optimization)
  9216. NeedSession(true);
  9217. }
  9218. __finally
  9219. {
  9220. NonVisualDataModule->EndBusy();
  9221. }
  9222. }
  9223. }
  9224. }
  9225. //---------------------------------------------------------------------------
  9226. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9227. {
  9228. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9229. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9230. }
  9231. //---------------------------------------------------------------------------
  9232. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9233. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9234. int & MaxHeight)
  9235. {
  9236. // workaround for bug in TWinControl.CalcConstraints
  9237. // Check for empty rect (restore from iconic state) is done there only after
  9238. // call to AdjustClientRect, which enlarges the rect (for forms).
  9239. TRect R = GetClientRect();
  9240. // when restoring from iconic state, place no restrictions
  9241. if (IsRectEmpty(R))
  9242. {
  9243. MinWidth = 0;
  9244. MinHeight = 0;
  9245. MaxWidth = 32000;
  9246. MaxHeight = 32000;
  9247. }
  9248. }
  9249. //---------------------------------------------------------------------------
  9250. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9251. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9252. {
  9253. // terminal can be already closed (e.g. dropped connection)
  9254. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9255. {
  9256. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9257. try
  9258. {
  9259. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9260. }
  9261. catch(...)
  9262. {
  9263. if (!Terminal->Active)
  9264. {
  9265. Close = true;
  9266. }
  9267. throw;
  9268. }
  9269. }
  9270. }
  9271. //---------------------------------------------------------------------------
  9272. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9273. {
  9274. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9275. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9276. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9277. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9278. {
  9279. OnGetSpaceAvailable = GetSpaceAvailable;
  9280. }
  9281. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9282. OnGetSpaceAvailable);
  9283. }
  9284. //---------------------------------------------------------------------------
  9285. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9286. {
  9287. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9288. Terminal->FillSessionDataForCode(Data.get());
  9289. return Data.release();
  9290. }
  9291. //---------------------------------------------------------------------------
  9292. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9293. {
  9294. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9295. DoGenerateUrlDialog(Data.get(), Paths);
  9296. }
  9297. //---------------------------------------------------------------------------
  9298. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9299. {
  9300. GenerateUrl(NULL);
  9301. }
  9302. //---------------------------------------------------------------------------
  9303. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9304. {
  9305. std::unique_ptr<TStrings> Paths(new TStringList());
  9306. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9307. GenerateUrl(Paths.get());
  9308. }
  9309. //---------------------------------------------------------------------------
  9310. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9311. {
  9312. FSessionColor = value;
  9313. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9314. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9315. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9316. DebugAssert(ColorItem != NULL);
  9317. ColorItem->Color = C;
  9318. NonVisualDataModule->ColorMenuItem->Color = C;
  9319. UpdateControls();
  9320. }
  9321. //---------------------------------------------------------------------------
  9322. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9323. {
  9324. if (value != FSessionColor)
  9325. {
  9326. UpdateSessionColor(value);
  9327. // Is null when called from LastTerminalClosed
  9328. if (Terminal != NULL)
  9329. {
  9330. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9331. }
  9332. }
  9333. }
  9334. //---------------------------------------------------------------------------
  9335. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9336. {
  9337. bool Result = FNoteTimer->Enabled;
  9338. if (Result)
  9339. {
  9340. // cannot cancel note too early
  9341. bool NotEarly =
  9342. (Now() - FNoteShown >
  9343. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9344. if (Force || NotEarly)
  9345. {
  9346. FNoteTimer->Enabled = false;
  9347. FNote = L"";
  9348. SAFE_DESTROY(FNoteData);
  9349. FOnNoteClick = NULL;
  9350. FNoteHints = FNotes->Text;
  9351. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9352. UpdateStatusBar();
  9353. }
  9354. }
  9355. return Result;
  9356. }
  9357. //---------------------------------------------------------------------------
  9358. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9359. {
  9360. DebugAssert(FNoteTimer->Enabled);
  9361. CancelNote(true);
  9362. }
  9363. //---------------------------------------------------------------------------
  9364. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9365. {
  9366. FNoteHints = FNotes->Text.TrimRight();
  9367. }
  9368. //---------------------------------------------------------------------------
  9369. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9370. {
  9371. int P = Note.Pos(L"\n");
  9372. if (P > 0)
  9373. {
  9374. Note.SetLength(P - 1);
  9375. }
  9376. FNotes->Add(FORMAT(L"[%s] %s",
  9377. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9378. while (FNotes->Count > 10)
  9379. {
  9380. FNotes->Delete(0);
  9381. }
  9382. if (UpdateNow)
  9383. {
  9384. UpdateNoteHints();
  9385. UpdateStatusBar();
  9386. }
  9387. }
  9388. //---------------------------------------------------------------------------
  9389. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9390. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9391. {
  9392. int P = Note.Pos(L"\n");
  9393. if (P > 0)
  9394. {
  9395. Note.SetLength(P - 1);
  9396. }
  9397. UpdateNoteHints();
  9398. FNote = Note;
  9399. SAFE_DESTROY(FNoteData);
  9400. FNoteData = NoteData;
  9401. FOnNoteClick = OnNoteClick;
  9402. AddNote(Note, false);
  9403. UpdateStatusBar();
  9404. FNoteShown = Now();
  9405. FNoteTimer->Enabled = false;
  9406. if (Seconds == 0)
  9407. {
  9408. Seconds = WinConfiguration->NotificationsTimeout;
  9409. }
  9410. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9411. FNoteTimer->Enabled = true;
  9412. }
  9413. //---------------------------------------------------------------------------
  9414. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9415. {
  9416. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9417. }
  9418. //---------------------------------------------------------------------------
  9419. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9420. {
  9421. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9422. {
  9423. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9424. }
  9425. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9426. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9427. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9428. 0, NULL, NULL);
  9429. }
  9430. //---------------------------------------------------------------------------
  9431. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9432. {
  9433. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9434. UnicodeString Note =
  9435. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9436. PostNote(Note, 0, NULL, NULL);
  9437. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9438. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9439. }
  9440. //---------------------------------------------------------------------------
  9441. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9442. {
  9443. WinConfiguration->FormatSizeBytes = Style;
  9444. }
  9445. //---------------------------------------------------------------------------
  9446. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9447. {
  9448. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9449. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9450. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9451. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9452. }
  9453. //---------------------------------------------------------------------------
  9454. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9455. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9456. {
  9457. if (Panel->Index == 0)
  9458. {
  9459. if (FOnNoteClick != NULL)
  9460. {
  9461. // prevent the user data from being freed by possible call
  9462. // to CancelNote or PostNote during call to OnNoteClick
  9463. std::unique_ptr<TObject> NoteData(FNoteData);
  9464. TNotifyEvent OnNoteClick = FOnNoteClick;
  9465. FNoteData = NULL;
  9466. // need to cancel the note as we are going to delete its user data
  9467. CancelNote(true);
  9468. OnNoteClick(NoteData.get());
  9469. }
  9470. }
  9471. if (HasActiveTerminal() &&
  9472. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9473. {
  9474. FileSystemInfo();
  9475. }
  9476. }
  9477. //---------------------------------------------------------------------------
  9478. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9479. {
  9480. // workaround:
  9481. // 1) for unknown reason, disabling window, while minimized,
  9482. // prevents it from restoring, even if it was enabled again meanwhile
  9483. // 2) when disabling the main window, while another has focus
  9484. // minimize is no longer possible
  9485. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9486. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9487. // Shouldn't we use IsApplicationMinimized() here?
  9488. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9489. {
  9490. Enabled = false;
  9491. }
  9492. FLockLevel++;
  9493. }
  9494. //---------------------------------------------------------------------------
  9495. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9496. {
  9497. DebugAssert(FLockLevel > 0);
  9498. FLockLevel--;
  9499. if (FLockLevel == 0)
  9500. {
  9501. DebugAssert(FLockSuspendLevel == 0);
  9502. Enabled = true;
  9503. }
  9504. }
  9505. //---------------------------------------------------------------------------
  9506. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9507. {
  9508. // We need to make sure that window is enabled when the last modal window closes
  9509. // otherwise focus is not restored correctly.
  9510. // So we re-enable the window when modal window opens and
  9511. // disable it back after is closes
  9512. if (FLockLevel > 0)
  9513. {
  9514. // while we have nesting counter, we know that we never be called
  9515. // recursivelly as Application->OnModalBegin is called only
  9516. // for the top-level modal window
  9517. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9518. {
  9519. // won't be disabled when conditions in LockWindow() were not satisfied
  9520. FDisabledOnLockSuspend = !Enabled;
  9521. // When minimized to tray (or actually when set to SW_HIDE),
  9522. // setting Enabled makes the window focusable even when there's
  9523. // modal window over it
  9524. if (!FTrayIcon->Visible)
  9525. {
  9526. Enabled = true;
  9527. }
  9528. }
  9529. FLockSuspendLevel++;
  9530. }
  9531. }
  9532. //---------------------------------------------------------------------------
  9533. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9534. {
  9535. if (FLockSuspendLevel > 0)
  9536. {
  9537. DebugAssert(FLockLevel > 0);
  9538. FLockSuspendLevel--;
  9539. // see comment in SuspendWindowLock
  9540. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9541. {
  9542. // Note that window can be enabled here, when we were minized to tray when
  9543. // was SuspendWindowLock() called.
  9544. // We should possibly do the same check as in LockWindow(),
  9545. // if it is ever possible that the consitions change between
  9546. // SuspendWindowLock() and ResumeWindowLock()
  9547. Enabled = !FDisabledOnLockSuspend;
  9548. }
  9549. }
  9550. }
  9551. //---------------------------------------------------------------------------
  9552. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9553. {
  9554. TTBXComboBoxItem * RemotePathComboBox =
  9555. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9556. if (!TextOnly)
  9557. {
  9558. TStrings * Items = RemotePathComboBox->Strings;
  9559. Items->BeginUpdate();
  9560. try
  9561. {
  9562. Items->Clear();
  9563. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9564. {
  9565. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9566. while (!IsUnixRootPath(APath))
  9567. {
  9568. int P = APath.LastDelimiter(L'/');
  9569. DebugAssert(P >= 0);
  9570. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9571. APath.SetLength(P - 1);
  9572. }
  9573. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9574. }
  9575. }
  9576. __finally
  9577. {
  9578. Items->EndUpdate();
  9579. }
  9580. }
  9581. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9582. // Setting ItemIndex to -1 does not reset its text
  9583. if (RemotePathComboBox->Strings->Count == 0)
  9584. {
  9585. RemotePathComboBox->Text = L"";
  9586. }
  9587. }
  9588. //---------------------------------------------------------------------------
  9589. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9590. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9591. {
  9592. if (AIndex < 0)
  9593. {
  9594. AIndex = Sender->ItemIndex;
  9595. }
  9596. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9597. }
  9598. //---------------------------------------------------------------------------
  9599. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9600. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9601. int & ImageIndex)
  9602. {
  9603. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9604. }
  9605. //---------------------------------------------------------------------------
  9606. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9607. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9608. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9609. {
  9610. if (!IsLocalBrowserMode())
  9611. {
  9612. ARect.Left += (10 * AIndex);
  9613. }
  9614. }
  9615. //---------------------------------------------------------------------------
  9616. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9617. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9618. {
  9619. AWidth += (10 * AIndex);
  9620. }
  9621. //---------------------------------------------------------------------------
  9622. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9623. {
  9624. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9625. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9626. int Index = RemotePathComboBox->ItemIndex;
  9627. while (Index < RemotePathComboBox->Strings->Count - 1)
  9628. {
  9629. APath = UnixExtractFileDir(APath);
  9630. Index++;
  9631. }
  9632. // VanDyke style paths
  9633. if (APath.IsEmpty())
  9634. {
  9635. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9636. APath = ROOTDIRECTORY;
  9637. }
  9638. if (RemoteDirView->Path != APath)
  9639. {
  9640. RemoteDirView->Path = APath;
  9641. }
  9642. }
  9643. //---------------------------------------------------------------------------
  9644. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9645. {
  9646. DoRemotePathComboBoxItemClick(Sender);
  9647. }
  9648. //---------------------------------------------------------------------------
  9649. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9650. {
  9651. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9652. DebugUsedParam(Sender);
  9653. UpdateRemotePathComboBox(true);
  9654. }
  9655. //---------------------------------------------------------------------------
  9656. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9657. {
  9658. DoRemotePathComboBoxCancel(Sender);
  9659. }
  9660. //---------------------------------------------------------------------------
  9661. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9662. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9663. {
  9664. ResetIncrementalSearch();
  9665. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9666. DebugAssert(DirView != NULL);
  9667. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9668. {
  9669. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9670. // OnEditing is called also from TCustomListView::CanEdit
  9671. if (Edit != NULL)
  9672. {
  9673. EditSelectBaseName(Edit);
  9674. }
  9675. }
  9676. }
  9677. //---------------------------------------------------------------------------
  9678. //---------------------------------------------------------------------------
  9679. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9680. {
  9681. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9682. Result->AutoDetectDnD = false;
  9683. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9684. Result->RenderDataOn = rdoEnterAndDropSync;
  9685. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9686. return Result;
  9687. }
  9688. //---------------------------------------------------------------------------
  9689. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9690. {
  9691. if (IsWin10Build(2004))
  9692. {
  9693. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9694. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9695. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9696. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9697. }
  9698. }
  9699. //---------------------------------------------------------------------------
  9700. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9701. {
  9702. TForm::CreateWnd();
  9703. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9704. UpdateDarkMode();
  9705. if (FSessionsDragDropFilesEx == NULL)
  9706. {
  9707. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9708. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9709. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9710. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9711. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9712. }
  9713. if (FQueueDragDropFilesEx == NULL)
  9714. {
  9715. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9716. // No need to set OnDragOver as we do not have any restrictions
  9717. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9718. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9719. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9720. }
  9721. }
  9722. //---------------------------------------------------------------------------
  9723. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9724. {
  9725. TForm::DestroyWnd();
  9726. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9727. FQueueDragDropFilesEx->DragDropControl = NULL;
  9728. }
  9729. //---------------------------------------------------------------------------
  9730. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9731. {
  9732. SideEnter(FCurrentSide);
  9733. FEverShown = true;
  9734. }
  9735. //---------------------------------------------------------------------------
  9736. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9737. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9738. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9739. {
  9740. if (!NonVisualDataModule->Busy)
  9741. {
  9742. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9743. Configuration->Usage->Inc(L"FileFinds");
  9744. LockWindow(true);
  9745. NonVisualDataModule->StartBusy();
  9746. WinConfiguration->LockedInterface = true;
  9747. try
  9748. {
  9749. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9750. }
  9751. __finally
  9752. {
  9753. WinConfiguration->LockedInterface = false;
  9754. NonVisualDataModule->EndBusy();
  9755. UnlockWindow();
  9756. }
  9757. }
  9758. }
  9759. //---------------------------------------------------------------------------
  9760. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9761. {
  9762. if (!NonVisualDataModule->Busy)
  9763. {
  9764. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9765. SetFocus();
  9766. RemoteDirView->Path = UnixExtractFilePath(Path);
  9767. UnicodeString FileName = UnixExtractFileName(Path);
  9768. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9769. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9770. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9771. if (Item == NULL)
  9772. {
  9773. ReloadDirectory(osRemote);
  9774. Item = RemoteDirView->FindFileItem(FileName);
  9775. }
  9776. if (Item != NULL)
  9777. {
  9778. RemoteDirView->ItemFocused = Item;
  9779. RemoteDirView->ItemFocused->MakeVisible(false);
  9780. RemoteDirView->SetFocus();
  9781. }
  9782. }
  9783. }
  9784. //---------------------------------------------------------------------------
  9785. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9786. {
  9787. bool Result = !NonVisualDataModule->Busy;
  9788. if (Result)
  9789. {
  9790. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9791. }
  9792. return Result;
  9793. }
  9794. //---------------------------------------------------------------------------
  9795. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9796. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9797. {
  9798. if (CanOperateOnFoundFiles(ATerminal))
  9799. {
  9800. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9801. FOnFileOperationFinished = OnFileOperationFinished;
  9802. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9803. }
  9804. }
  9805. //---------------------------------------------------------------------------
  9806. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9807. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9808. {
  9809. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9810. }
  9811. //---------------------------------------------------------------------------
  9812. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9813. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9814. {
  9815. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9816. }
  9817. //---------------------------------------------------------------------------
  9818. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9819. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9820. {
  9821. if (CanOperateOnFoundFiles(ATerminal))
  9822. {
  9823. for (int Index = 0; Index < FileList->Count; Index++)
  9824. {
  9825. UnicodeString FileName = FileList->Strings[Index];
  9826. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9827. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9828. OnFileOperationFinished(osRemote, FileName, true);
  9829. }
  9830. }
  9831. }
  9832. //---------------------------------------------------------------------------
  9833. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9834. {
  9835. FFileFindTerminal = Terminal;
  9836. ShowFileFindDialog(
  9837. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9838. DoDownloadFoundFiles, DoEditFoundFiles);
  9839. }
  9840. //---------------------------------------------------------------------------
  9841. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9842. {
  9843. FTaskbarList = TaskbarList;
  9844. }
  9845. //---------------------------------------------------------------------------
  9846. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9847. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9848. {
  9849. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9850. if (Index >= 0)
  9851. {
  9852. if (Button == mbLeft)
  9853. {
  9854. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9855. // If switching tab (switching session) takes long enough for user
  9856. // to actually release the button, "mouse down" is still raised,
  9857. // but we do not get "mouse up" event, so dragging is not cancelled,
  9858. // prevent that by not beginning dragging in the first place.
  9859. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9860. if (IsKeyPressed(Key))
  9861. {
  9862. // when user clicks the "+", we get mouse down only after the session
  9863. // is closed, when new session tab is already on X:Y, so dragging
  9864. // starts, prevent that
  9865. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9866. {
  9867. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9868. if (Terminal != NULL)
  9869. {
  9870. SessionsPageControl->BeginDrag(false);
  9871. }
  9872. }
  9873. }
  9874. }
  9875. else if (Button == mbMiddle)
  9876. {
  9877. // ignore middle-click for "New session tab" and the last local-local tab
  9878. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9879. if ((Session != NULL) && CanCloseSession(Session))
  9880. {
  9881. CloseSessionTab(Index);
  9882. }
  9883. }
  9884. }
  9885. }
  9886. //---------------------------------------------------------------------------
  9887. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9888. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9889. {
  9890. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9891. // do not allow dropping on the "+" tab
  9892. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9893. if ((TargetTerminal != NULL) &&
  9894. (SessionsPageControl->ActivePage->PageIndex != Index))
  9895. {
  9896. Configuration->Usage->Inc(L"SessionTabMoves");
  9897. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9898. // but we want to actually prevent that to avoid flicker
  9899. SessionsPageControl->ActivePage->PageIndex = Index;
  9900. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9901. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9902. }
  9903. }
  9904. //---------------------------------------------------------------------------
  9905. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9906. TObject * Sender, TObject * Source, int X, int Y,
  9907. TDragState /*State*/, bool & Accept)
  9908. {
  9909. Accept = (Sender == Source);
  9910. if (Accept)
  9911. {
  9912. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9913. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9914. // do not allow dragging to the "+" tab
  9915. Accept = (Terminal != NULL);
  9916. }
  9917. }
  9918. //---------------------------------------------------------------------------
  9919. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9920. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9921. {
  9922. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9923. if (Index < 0)
  9924. {
  9925. Effect = DROPEFFECT_NONE;
  9926. }
  9927. else
  9928. {
  9929. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9930. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9931. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9932. {
  9933. Effect = DROPEFFECT_NONE;
  9934. }
  9935. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9936. {
  9937. Effect = DROPEFFECT_COPY;
  9938. }
  9939. }
  9940. }
  9941. //---------------------------------------------------------------------------
  9942. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9943. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9944. {
  9945. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9946. // do not allow dropping on local-local, disconnected and the "+" tab
  9947. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9948. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9949. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9950. {
  9951. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9952. if (!IsFileControl(DropSourceControl, osRemote))
  9953. {
  9954. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9955. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9956. // Why don't we use Terminal->CurrentDirectory directly?
  9957. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9958. // do not force queue, drag drop
  9959. false, true);
  9960. }
  9961. }
  9962. }
  9963. //---------------------------------------------------------------------------
  9964. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9965. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9966. {
  9967. // Downloads are handled in RemoteFileControlDDEnd
  9968. if (!IsFileControl(DropSourceControl, osRemote))
  9969. {
  9970. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9971. Terminal->CurrentDirectory,
  9972. // force queue, drag drop
  9973. true, true);
  9974. }
  9975. }
  9976. //---------------------------------------------------------------------------
  9977. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9978. {
  9979. FShowing = false;
  9980. // Do not save empty workspace
  9981. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9982. {
  9983. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9984. UnicodeString Name = WorkspaceName();
  9985. DoSaveWorkspace(
  9986. Name, DataList.get(),
  9987. !Configuration->DisablePasswordStoring &&
  9988. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9989. WinConfiguration->LastStoredSession = Name;
  9990. }
  9991. }
  9992. //---------------------------------------------------------------------------
  9993. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9994. {
  9995. TManagedTerminal * ManagedTerminal =
  9996. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9997. if (ManagedTerminal != NULL)
  9998. {
  9999. ManagedTerminal->DirectoryLoaded = Now();
  10000. }
  10001. }
  10002. //---------------------------------------------------------------------------
  10003. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  10004. TListItem * /*Item*/, bool /*Selected*/)
  10005. {
  10006. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  10007. switch (DirView->LastSelectMethod)
  10008. {
  10009. case smKeyboard:
  10010. Configuration->Usage->Inc(L"KeyboardSelections");
  10011. break;
  10012. case smMouse:
  10013. Configuration->Usage->Inc(L"MouseSelections");
  10014. break;
  10015. }
  10016. }
  10017. //---------------------------------------------------------------------------
  10018. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  10019. {
  10020. if (Color != 0)
  10021. {
  10022. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  10023. }
  10024. else
  10025. {
  10026. return FSessionTabImageIndex;
  10027. }
  10028. }
  10029. //---------------------------------------------------------------------------
  10030. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  10031. {
  10032. TPngImageCollectionItem * Item =
  10033. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  10034. FSessionColors->AddPng(Item->PngImage);
  10035. return FSessionColors->Count - 1;
  10036. }
  10037. //---------------------------------------------------------------------------
  10038. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  10039. {
  10040. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  10041. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  10042. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  10043. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  10044. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  10045. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  10046. }
  10047. //---------------------------------------------------------------------------
  10048. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  10049. {
  10050. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  10051. }
  10052. //---------------------------------------------------------------------------
  10053. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  10054. {
  10055. switch (Side)
  10056. {
  10057. case osLocal:
  10058. return FNewLocalTabTabImageIndex;
  10059. case osRemote:
  10060. case osCurrent:
  10061. return FNewRemoteTabTabImageIndex;
  10062. default:
  10063. DebugFail();
  10064. return -1;
  10065. }
  10066. }
  10067. //---------------------------------------------------------------------------
  10068. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  10069. {
  10070. for (int Index = 0; Index < Control->ControlCount; Index++)
  10071. {
  10072. TControl * AControl = Control->Controls[Index];
  10073. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10074. if (WinControl != NULL)
  10075. {
  10076. CollectItemsWithTextDisplayMode(WinControl);
  10077. }
  10078. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10079. if (Toolbar != NULL)
  10080. {
  10081. // we care for top-level items only
  10082. TTBCustomItem * Items = Toolbar->Items;
  10083. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10084. {
  10085. TTBCustomItem * Item = Items->Items[ItemIndex];
  10086. if (((Item->DisplayMode == nbdmImageAndText) ||
  10087. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10088. EligibleForImageDisplayMode(Item))
  10089. {
  10090. FItemsWithTextDisplayMode.insert(Item);
  10091. }
  10092. }
  10093. }
  10094. }
  10095. }
  10096. //---------------------------------------------------------------------------
  10097. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10098. {
  10099. return true;
  10100. }
  10101. //---------------------------------------------------------------------------
  10102. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10103. {
  10104. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10105. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10106. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10107. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10108. bool Result = true;
  10109. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10110. {
  10111. TTBCustomItem * Item = *i;
  10112. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10113. if (Label != NULL)
  10114. {
  10115. // optimization
  10116. if (Label->Visible == SelectiveToolbarText)
  10117. {
  10118. Result = false;
  10119. }
  10120. else
  10121. {
  10122. Label->Visible = SelectiveToolbarText;
  10123. }
  10124. }
  10125. else
  10126. {
  10127. // optimization
  10128. if (Item->DisplayMode == DisplayMode)
  10129. {
  10130. Result = false;
  10131. }
  10132. else
  10133. {
  10134. Item->DisplayMode = DisplayMode;
  10135. }
  10136. }
  10137. ++i;
  10138. }
  10139. UpdateNewTabTab();
  10140. return Result;
  10141. }
  10142. //---------------------------------------------------------------------------
  10143. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10144. {
  10145. DebugFail();
  10146. }
  10147. //---------------------------------------------------------------------------
  10148. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10149. {
  10150. // Among other, a lock level is non zero, while directory is being loaded,
  10151. // even when it happens as a result of dirview navigation,
  10152. // i.e. when TNonVisualDataModule is NOT FBusy.
  10153. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10154. // Among other this prevents a panel auto update to occur while
  10155. // directory is changing.
  10156. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10157. }
  10158. //---------------------------------------------------------------------------
  10159. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10160. {
  10161. // While the window is disabled, we still seem to process menu shortcuts at least,
  10162. // so stop it at least here.
  10163. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10164. bool Result;
  10165. if (Allowed == aaUpdate)
  10166. {
  10167. Result = true;
  10168. }
  10169. else
  10170. {
  10171. Result = !NonVisualDataModule->Busy;
  10172. if (Result && (Allowed == aaShortCut))
  10173. {
  10174. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10175. Action->Update();
  10176. Result = Action->Enabled;
  10177. }
  10178. }
  10179. return Result;
  10180. }
  10181. //---------------------------------------------------------------------------
  10182. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10183. {
  10184. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10185. }
  10186. //---------------------------------------------------------------------------
  10187. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10188. {
  10189. // This is somewhat redundant to LockWindow() call from
  10190. // TTerminalManager::TerminalReadDirectoryProgress.
  10191. // But disabling window is known not to block keyboard shorcuts
  10192. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10193. if (Busy > 0)
  10194. {
  10195. if (NonVisualDataModule->Busy)
  10196. {
  10197. State = false;
  10198. }
  10199. else
  10200. {
  10201. NonVisualDataModule->StartBusy();
  10202. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10203. LockWindow();
  10204. }
  10205. }
  10206. else if (Busy < 0)
  10207. {
  10208. UnlockWindow();
  10209. NonVisualDataModule->EndBusy();
  10210. }
  10211. else
  10212. {
  10213. State = NonVisualDataModule->Busy;
  10214. }
  10215. }
  10216. //---------------------------------------------------------------------------
  10217. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10218. {
  10219. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10220. if (Index >= 0)
  10221. {
  10222. TPopupMenu * PopupMenu = NULL;
  10223. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10224. if (Session != NULL)
  10225. {
  10226. SessionsPageControl->ActivePageIndex = Index;
  10227. if (DebugAlwaysTrue(SessionTabSwitched()))
  10228. {
  10229. // copied from TControl.WMContextMenu
  10230. SendCancelMode(SessionsPageControl);
  10231. // explicit popup instead of using PopupMenu property
  10232. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10233. // while connecting yet not-connected session and hence
  10234. // allowing an access to commands over not-completely connected session
  10235. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10236. }
  10237. }
  10238. else
  10239. {
  10240. if (SupportsLocalBrowser())
  10241. {
  10242. PopupMenu = NonVisualDataModule->NewTabPopup;
  10243. }
  10244. }
  10245. if (PopupMenu != NULL)
  10246. {
  10247. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10248. PopupMenu->PopupComponent = SessionsPageControl;
  10249. PopupMenu->Popup(Point.x, Point.y);
  10250. }
  10251. }
  10252. Handled = true;
  10253. }
  10254. //---------------------------------------------------------------------------
  10255. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10256. {
  10257. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10258. }
  10259. //---------------------------------------------------------------------------
  10260. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10261. {
  10262. Configuration->Usage->Inc(L"PasswordChanges");
  10263. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10264. ChangePasswordData->DisableAuthentationsExceptPassword();
  10265. ChangePasswordData->ChangePassword = true;
  10266. std::unique_ptr<TTerminal> ChangePasswordSession(
  10267. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10268. ChangePasswordSession->AutoReadDirectory = false;
  10269. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10270. {
  10271. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10272. }
  10273. else
  10274. {
  10275. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10276. }
  10277. }
  10278. //---------------------------------------------------------------------------
  10279. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10280. {
  10281. // No nice way to assert SSH2
  10282. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10283. }
  10284. //---------------------------------------------------------------------------
  10285. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10286. {
  10287. TOperationVisualizer OperationVisualizer;
  10288. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10289. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10290. }
  10291. //---------------------------------------------------------------------------
  10292. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10293. {
  10294. TForm::ChangeScale(M, D);
  10295. int APixelsPerInch = GetControlPixelsPerInch(this);
  10296. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10297. try
  10298. {
  10299. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10300. }
  10301. __finally
  10302. {
  10303. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10304. }
  10305. }
  10306. //---------------------------------------------------------------------------
  10307. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10308. {
  10309. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10310. }
  10311. //---------------------------------------------------------------------
  10312. class TPublicControl : public TControl
  10313. {
  10314. friend class TCustomScpExplorerForm;
  10315. };
  10316. //---------------------------------------------------------------------------
  10317. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10318. {
  10319. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10320. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10321. }
  10322. //---------------------------------------------------------------------------
  10323. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10324. {
  10325. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10326. OpenBookmark(Side, Bookmark);
  10327. }
  10328. //---------------------------------------------------------------------------
  10329. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10330. {
  10331. DoBookmarkClick(osLocal, Sender);
  10332. }
  10333. //---------------------------------------------------------------------------
  10334. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10335. {
  10336. DoBookmarkClick(osRemote, Sender);
  10337. }
  10338. //---------------------------------------------------------------------------
  10339. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10340. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10341. {
  10342. if (BookmarkList != NULL)
  10343. {
  10344. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10345. if (IsLocalBrowserMode())
  10346. {
  10347. Side = osLocal;
  10348. }
  10349. if (!WinConfiguration->UseLocationProfiles)
  10350. {
  10351. std::unique_ptr<TStringList> Directories(new TStringList());
  10352. Directories->CaseSensitive = (Side == osRemote);
  10353. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10354. {
  10355. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10356. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10357. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10358. {
  10359. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10360. Item->Caption = EscapeHotkey(Directory);
  10361. Item->ShortCut = Bookmark->ShortCut;
  10362. Item->OnClick = OnBookmarkClick;
  10363. Item->Tag = reinterpret_cast<int>(Bookmark);
  10364. Directories->Add(Directory);
  10365. Menu->Add(Item.release());
  10366. }
  10367. }
  10368. }
  10369. else
  10370. {
  10371. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10372. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10373. {
  10374. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10375. if (!Bookmark->Node.IsEmpty())
  10376. {
  10377. Folders->Add(Bookmark->Node);
  10378. }
  10379. }
  10380. for (int Index = 0; Index < Folders->Count; Index++)
  10381. {
  10382. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10383. Item->Caption = Folders->Strings[Index];
  10384. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10385. Folders->Objects[Index] = Item.get();
  10386. Menu->Add(Item.release());
  10387. }
  10388. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10389. {
  10390. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10391. TTBCustomItem * Parent;
  10392. if (!Bookmark->Node.IsEmpty())
  10393. {
  10394. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10395. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10396. }
  10397. else
  10398. {
  10399. Parent = Menu;
  10400. }
  10401. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10402. Item->Caption = Bookmark->Name;
  10403. Item->ShortCut = Bookmark->ShortCut;
  10404. Item->OnClick = OnBookmarkClick;
  10405. Item->Tag = reinterpret_cast<int>(Bookmark);
  10406. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10407. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10408. {
  10409. // No hint for location profiles that are actually mere "bookmarks"
  10410. }
  10411. else
  10412. {
  10413. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10414. UnicodeString LongHint;
  10415. if (!Bookmark->Local.IsEmpty())
  10416. {
  10417. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10418. }
  10419. if (!Bookmark->Remote.IsEmpty())
  10420. {
  10421. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10422. }
  10423. AddToList(Hint, LongHint, L"|");
  10424. Item->Hint = Hint;
  10425. }
  10426. Parent->Add(Item.release());
  10427. }
  10428. }
  10429. AddMenuSeparator(Menu);
  10430. }
  10431. }
  10432. //---------------------------------------------------------------------------
  10433. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10434. {
  10435. Menu->Clear();
  10436. std::unique_ptr<TTBCustomItem> Item;
  10437. Item.reset(new TTBXItem(Owner));
  10438. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10439. Item->Options = Item->Options << tboDefault;
  10440. Menu->Add(Item.release());
  10441. AddMenuSeparator(Menu);
  10442. if (Terminal != NULL)
  10443. {
  10444. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10445. }
  10446. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10447. Item.reset(new TTBXItem(Owner));
  10448. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10449. Menu->Add(Item.release());
  10450. }
  10451. //---------------------------------------------------------------------------
  10452. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10453. {
  10454. bool Result = true;
  10455. bool Remote = !IsSideLocalBrowser(Side);
  10456. try
  10457. {
  10458. UnicodeString APath = Path;
  10459. if (Remote)
  10460. {
  10461. Terminal->ExceptionOnFail = true;
  10462. }
  10463. else
  10464. {
  10465. APath = ExpandEnvironmentVariables(Path);
  10466. }
  10467. try
  10468. {
  10469. DirView(Side)->Path = APath;
  10470. }
  10471. __finally
  10472. {
  10473. if (Remote)
  10474. {
  10475. Terminal->ExceptionOnFail = false;
  10476. }
  10477. }
  10478. }
  10479. catch (Exception & E)
  10480. {
  10481. if (!Remote || Terminal->Active)
  10482. {
  10483. ShowExtendedExceptionEx(Terminal, &E);
  10484. Result = false;
  10485. }
  10486. else
  10487. {
  10488. throw;
  10489. }
  10490. }
  10491. return Result;
  10492. }
  10493. //---------------------------------------------------------------------------
  10494. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10495. {
  10496. // Make sure there some feedback even when loading tiny local folders
  10497. TInstantOperationVisualizer Visualizer;
  10498. DirView(Side)->ReloadDirectory();
  10499. }
  10500. //---------------------------------------------------------------------------
  10501. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10502. {
  10503. if (Index == SessionsPageControl->TabIndex)
  10504. {
  10505. CloseTab();
  10506. }
  10507. else
  10508. {
  10509. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10510. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10511. TTerminalManager * Manager = TTerminalManager::Instance();
  10512. if (!Terminal->Active)
  10513. {
  10514. Manager->FreeTerminal(Terminal);
  10515. }
  10516. else
  10517. {
  10518. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10519. if (CanCloseQueue(Queue))
  10520. {
  10521. Manager->FreeTerminal(Terminal);
  10522. }
  10523. }
  10524. }
  10525. }
  10526. //---------------------------------------------------------------------------
  10527. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10528. {
  10529. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10530. if (Session == NULL)
  10531. {
  10532. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10533. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10534. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10535. //P = SessionsPageControl->ClientToScreen(P);
  10536. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10537. }
  10538. else
  10539. {
  10540. CloseSessionTab(Index);
  10541. }
  10542. }
  10543. //---------------------------------------------------------------------------
  10544. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10545. {
  10546. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10547. {
  10548. TInstantOperationVisualizer Visualizer;
  10549. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10550. // before we create a new data object
  10551. ClipboardClear();
  10552. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10553. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10554. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10555. // Intermediate variable to WORKAROUND a crash in compiler
  10556. TObjectList * ClipboardFakeMonitors =
  10557. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10558. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10559. if (FClipboardDragDropFilesEx.get() == NULL)
  10560. {
  10561. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10562. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10563. }
  10564. FClipboardDragDropFilesEx->FileList->Clear();
  10565. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10566. FClipboardDragDropFilesEx->CopyToClipboard();
  10567. FClipboardTerminal = Terminal;
  10568. // Need full paths, as cwd can be different once files are pasted
  10569. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10570. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10571. {
  10572. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10573. }
  10574. }
  10575. }
  10576. //---------------------------------------------------------------------------
  10577. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10578. {
  10579. if (OpenClipboard(0))
  10580. {
  10581. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10582. CloseClipboard();
  10583. }
  10584. }
  10585. //---------------------------------------------------------------------------
  10586. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10587. {
  10588. return !FClipboardFakeDirectory.IsEmpty();
  10589. }
  10590. //---------------------------------------------------------------------------
  10591. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10592. {
  10593. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10594. FClipboardFakeDirectory = UnicodeString();
  10595. FClipboardTerminal = NULL;
  10596. if (FDownloadingFromClipboard)
  10597. {
  10598. // We are called by the monitor, so attempt to release it would deadlock
  10599. FClipboardFakeMonitorsPendingReset = true;
  10600. }
  10601. else
  10602. {
  10603. FClipboardFakeMonitors.reset(NULL);
  10604. }
  10605. }
  10606. //---------------------------------------------------------------------------
  10607. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10608. {
  10609. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10610. {
  10611. ClipboardStop();
  10612. }
  10613. }
  10614. //---------------------------------------------------------------------------
  10615. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10616. {
  10617. TTerminalManager * Manager = TTerminalManager::Instance();
  10618. Manager->ActiveSession = FClipboardTerminal;
  10619. TTransferOperationParam Params;
  10620. Params.TargetDirectory = TargetDirectory;
  10621. Params.DragDrop = DragDrop;
  10622. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10623. }
  10624. //---------------------------------------------------------------------------
  10625. void TCustomScpExplorerForm::PasteFiles()
  10626. {
  10627. // Guard against possible race conditions
  10628. if (!FClipboardPasteTarget.IsEmpty())
  10629. {
  10630. // ClipboardFakeCreated might be called while we are here, so make sure we finish operating on the same path we started with
  10631. UnicodeString Target = FClipboardPasteTarget;
  10632. FClipboardPasteTarget = EmptyStr;
  10633. // Can fail as it can be e.g. locked by AV, so we retry that later
  10634. bool Removed = RemoveDir(ApiPath(Target));
  10635. try
  10636. {
  10637. if (!NonVisualDataModule->Busy)
  10638. {
  10639. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10640. FClipboardFakeMonitorsPendingReset = false;
  10641. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10642. TAutoFlag Flag(FDownloadingFromClipboard);
  10643. ClipboardDownload(ExtractFilePath(Target), NoConfirmation, true);
  10644. }
  10645. }
  10646. __finally
  10647. {
  10648. if (!Removed)
  10649. {
  10650. RemoveDir(ApiPath(Target));
  10651. }
  10652. }
  10653. }
  10654. }
  10655. //---------------------------------------------------------------------------
  10656. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10657. {
  10658. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10659. // (as it pumps a Windows message queue while being shutted down)
  10660. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10661. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10662. {
  10663. AppLogFmt(L"Fake clipboard directory pasted to \"%s\"", (FileName));
  10664. FClipboardPasteTarget = FileName;
  10665. // Particularly when pasting to external HDD, the fake directory is strangely recreated if removed here in the monitor callback.
  10666. // Postponing removing (or actually whole pasting operation) seems to help.
  10667. PostMessage(Handle, WM_PASTE_FILES, 0, 0);
  10668. }
  10669. }
  10670. //---------------------------------------------------------------------------
  10671. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10672. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10673. {
  10674. TFileMasks::TParams MaskParams;
  10675. MaskParams.Size = Size;
  10676. MaskParams.Modification = Modification;
  10677. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10678. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10679. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10680. if (!Local && Directory)
  10681. {
  10682. FileName = UnixExcludeTrailingBackslash(FileName);
  10683. }
  10684. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10685. {
  10686. bool ImplicitMatch;
  10687. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10688. !ImplicitMatch)
  10689. {
  10690. Color = Iter->Color;
  10691. break;
  10692. }
  10693. }
  10694. }
  10695. //---------------------------------------------------------------------------
  10696. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10697. {
  10698. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10699. if (!DirView->IsEditing())
  10700. {
  10701. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10702. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10703. // while searching
  10704. if (Key == VK_BACK)
  10705. {
  10706. if (!FIncrementalSearch.IsEmpty())
  10707. {
  10708. if (FIncrementalSearch.Length() == 1)
  10709. {
  10710. ResetIncrementalSearch();
  10711. }
  10712. else
  10713. {
  10714. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10715. IncrementalSearch(NewText, false, false);
  10716. }
  10717. Key = 0;
  10718. }
  10719. }
  10720. }
  10721. }
  10722. //---------------------------------------------------------------------------
  10723. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10724. {
  10725. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10726. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10727. {
  10728. // Filter control sequences.
  10729. // When not searching yet, prefer use of the space for toggling file selection
  10730. // (so we cannot incrementally search for a string starting with a space).
  10731. if ((Key > VK_SPACE) ||
  10732. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10733. {
  10734. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10735. Key = 0;
  10736. }
  10737. }
  10738. }
  10739. //---------------------------------------------------------------------------
  10740. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10741. {
  10742. if (!FIncrementalSearch.IsEmpty())
  10743. {
  10744. FIncrementalSearch = L"";
  10745. DirView(osCurrent)->UpdateStatusBar();
  10746. }
  10747. }
  10748. //---------------------------------------------------------------------------
  10749. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10750. {
  10751. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10752. if (Item == NULL)
  10753. {
  10754. MessageBeep(MB_ICONHAND);
  10755. }
  10756. else
  10757. {
  10758. TCustomDirView * ADirView = DirView(osCurrent);
  10759. {
  10760. TAutoNestingCounter Guard(FIncrementalSearching);
  10761. ADirView->FocusItem(Item);
  10762. if (ADirView->NortonLike == nlOff)
  10763. {
  10764. if ((ADirView->Selected != Item) ||
  10765. (ADirView->SelCount > 1))
  10766. {
  10767. ADirView->ClearSelection();
  10768. ADirView->Selected = Item;
  10769. }
  10770. }
  10771. }
  10772. FIncrementalSearch = Text;
  10773. Item->MakeVisible(false);
  10774. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10775. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10776. ADirView->UpdateStatusBar();
  10777. }
  10778. }
  10779. //---------------------------------------------------------------------------
  10780. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10781. {
  10782. int Index = Item->Index;
  10783. if (!Reverse)
  10784. {
  10785. Index++;
  10786. if (Index >= Item->Owner->Count)
  10787. {
  10788. Index = 0;
  10789. }
  10790. }
  10791. else
  10792. {
  10793. Index--;
  10794. if (Index < 0)
  10795. {
  10796. Index = Item->Owner->Count - 1;
  10797. }
  10798. }
  10799. return Item->Owner->Item[Index];
  10800. }
  10801. //---------------------------------------------------------------------------
  10802. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10803. {
  10804. TCustomDirView * ADirView = DirView(osCurrent);
  10805. if (ADirView->Items->Count == 0)
  10806. {
  10807. return NULL;
  10808. }
  10809. else
  10810. {
  10811. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10812. TListItem * Item = CurrentItem;
  10813. if (SkipCurrent)
  10814. {
  10815. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10816. }
  10817. while (true)
  10818. {
  10819. bool Matches = false;
  10820. switch (WinConfiguration->PanelSearch)
  10821. {
  10822. case isNameStartOnly:
  10823. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10824. break;
  10825. case isName:
  10826. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10827. break;
  10828. case isAll:
  10829. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10830. int Index = 0;
  10831. while ((Index < ColCount) && !Matches)
  10832. {
  10833. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10834. if (Visible)
  10835. {
  10836. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10837. }
  10838. Index++;
  10839. }
  10840. break;
  10841. }
  10842. if (Matches)
  10843. {
  10844. return Item;
  10845. }
  10846. Item = GetNextFile(Item, Reverse);
  10847. if (Item == CurrentItem)
  10848. {
  10849. return NULL;
  10850. }
  10851. }
  10852. }
  10853. }
  10854. //---------------------------------------------------------------------------
  10855. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10856. {
  10857. ResetIncrementalSearch();
  10858. }
  10859. //---------------------------------------------------------------------------
  10860. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10861. {
  10862. if (Message.CharCode == VK_TAB)
  10863. {
  10864. if (!FIncrementalSearch.IsEmpty())
  10865. {
  10866. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10867. bool Reverse = Shift.Contains(ssShift);
  10868. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10869. Message.Result = 1;
  10870. return;
  10871. }
  10872. }
  10873. else if (Message.CharCode == VK_ESCAPE)
  10874. {
  10875. if (!FIncrementalSearch.IsEmpty())
  10876. {
  10877. ResetIncrementalSearch();
  10878. Message.Result = 1;
  10879. return;
  10880. }
  10881. }
  10882. TForm::Dispatch(&Message);
  10883. }
  10884. //---------------------------------------------------------------------------
  10885. void __fastcall TCustomScpExplorerForm::Deactivate()
  10886. {
  10887. TForm::Deactivate();
  10888. // E.g. when switching to an internal editor window
  10889. ResetIncrementalSearch();
  10890. }
  10891. //---------------------------------------------------------------------------
  10892. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10893. {
  10894. // When switching to another application
  10895. ResetIncrementalSearch();
  10896. }
  10897. //---------------------------------------------------------------------------
  10898. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10899. {
  10900. ResetIncrementalSearch();
  10901. }
  10902. //---------------------------------------------------------------------------
  10903. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10904. {
  10905. if (FIncrementalSearching <= 0)
  10906. {
  10907. ResetIncrementalSearch();
  10908. }
  10909. }
  10910. //---------------------------------------------------------------------------
  10911. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10912. {
  10913. CountClicksForWindowPrint(this);
  10914. }
  10915. //---------------------------------------------------------------------------
  10916. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10917. {
  10918. CenterReconnectToolbar();
  10919. }
  10920. //---------------------------------------------------------------------------
  10921. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10922. {
  10923. if (RemoteDirView->ItemFocused != NULL)
  10924. {
  10925. RemoteDirView->ItemFocused->Selected = true;
  10926. }
  10927. }
  10928. //---------------------------------------------------------------------------
  10929. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10930. {
  10931. if (!FQueueStatusUpdating)
  10932. {
  10933. bool Refresh;
  10934. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10935. {
  10936. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10937. }
  10938. else
  10939. {
  10940. if (FQueueFileList->GetCount() > 0)
  10941. {
  10942. FQueueFileList->Clear();
  10943. Refresh = true;
  10944. }
  10945. }
  10946. int Count = FQueueFileList->GetCount();
  10947. if (QueueFileList->Items->Count != Count)
  10948. {
  10949. DebugAssert(Refresh);
  10950. QueueFileList->Items->Count = Count;
  10951. }
  10952. if (Refresh)
  10953. {
  10954. QueueFileList->Invalidate();
  10955. }
  10956. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10957. }
  10958. }
  10959. //---------------------------------------------------------------------------
  10960. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10961. {
  10962. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10963. }
  10964. //---------------------------------------------------------------------------
  10965. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10966. {
  10967. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10968. // See TSynchronizeChecklistDialog::UpdateTimer
  10969. if (QueueFileList->Items->Count > 0)
  10970. {
  10971. QueueFileList->Items->Item[0]->MakeVisible(false);
  10972. }
  10973. UpdateQueueFileList();
  10974. }
  10975. //---------------------------------------------------------------------------
  10976. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10977. {
  10978. Active = QueueView3->Focused() || QueueFileList->Focused();
  10979. }
  10980. //---------------------------------------------------------------------------
  10981. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10982. {
  10983. QueueLabelUpdateStatus();
  10984. }
  10985. //---------------------------------------------------------------------------
  10986. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10987. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10988. {
  10989. int State = FQueueFileList->GetState(Item->Index);
  10990. if (State == qfsProcessed)
  10991. {
  10992. Sender->Canvas->Font->Color = clGrayText;
  10993. }
  10994. }
  10995. //---------------------------------------------------------------------------
  10996. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10997. {
  10998. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10999. QueueFileList->Column[0]->Width =
  11000. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  11001. }
  11002. //---------------------------------------------------------------------------
  11003. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  11004. {
  11005. QueueFileListColumnAutoSize();
  11006. }
  11007. //---------------------------------------------------------------------------
  11008. void __fastcall TCustomScpExplorerForm::CloseApp()
  11009. {
  11010. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  11011. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  11012. // So we clear the flag here.
  11013. DebugAssert(NonVisualDataModule->Busy);
  11014. NonVisualDataModule->EndBusy();
  11015. try
  11016. {
  11017. Close();
  11018. }
  11019. __finally
  11020. {
  11021. NonVisualDataModule->StartBusy();
  11022. }
  11023. }
  11024. //---------------------------------------------------------------------------
  11025. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  11026. {
  11027. return (Terminal != NULL) && Terminal->Active;
  11028. }
  11029. //---------------------------------------------------------------------------
  11030. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  11031. {
  11032. return (ManagedSession != NULL);
  11033. }
  11034. //---------------------------------------------------------------------------
  11035. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  11036. {
  11037. return IsActiveTerminal(Terminal);
  11038. }
  11039. //---------------------------------------------------------------------------
  11040. void TCustomScpExplorerForm::LocalLocalCopy(
  11041. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  11042. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  11043. {
  11044. DebugFail();
  11045. }
  11046. //---------------------------------------------------------------------------
  11047. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  11048. {
  11049. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  11050. }
  11051. //---------------------------------------------------------------------------
  11052. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  11053. {
  11054. // 1) Changing tab list triggers the OnResize.
  11055. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  11056. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  11057. {
  11058. // UpdateTabsCaptionTruncation should be enough
  11059. SessionListChanged(true);
  11060. }
  11061. }
  11062. //---------------------------------------------------------------------------
  11063. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  11064. {
  11065. if (ASession == ManagedSession)
  11066. {
  11067. UpdateSession(ASession);
  11068. }
  11069. TSessionData * Data = ASession->StateData;
  11070. UnicodeString Result;
  11071. if (Side == osLocal)
  11072. {
  11073. Result = Data->LocalDirectory;
  11074. }
  11075. else if (DebugAlwaysTrue(Side == osOther))
  11076. {
  11077. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  11078. }
  11079. return Result;
  11080. }
  11081. //---------------------------------------------------------------------------
  11082. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  11083. {
  11084. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  11085. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  11086. if (Title == Result)
  11087. {
  11088. Result = EmptyStr;
  11089. }
  11090. else
  11091. {
  11092. Result += L"\n";
  11093. }
  11094. return Result;
  11095. }
  11096. //---------------------------------------------------------------------------
  11097. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11098. {
  11099. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11100. if (!ASession->Active)
  11101. {
  11102. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11103. }
  11104. else
  11105. {
  11106. Result += GetSessionPath(ASession, osRemote);
  11107. }
  11108. return Result;
  11109. }
  11110. //---------------------------------------------------------------------------
  11111. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11112. {
  11113. return LoadStr(NEW_REMOTE_TAB_HINT);
  11114. }
  11115. //---------------------------------------------------------------------------
  11116. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11117. {
  11118. if (Index >= 0)
  11119. {
  11120. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11121. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11122. if (TabSession == NULL)
  11123. {
  11124. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11125. if (NewTabTabCaption != TabSheet->Caption)
  11126. {
  11127. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11128. }
  11129. }
  11130. else
  11131. {
  11132. TTerminalManager * Manager = TTerminalManager::Instance();
  11133. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11134. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11135. }
  11136. }
  11137. }
  11138. //---------------------------------------------------------------------------
  11139. typedef std::vector<TListItem *> TListItemsVector;
  11140. struct TCalculateSizeOperation
  11141. {
  11142. TCalculateSizeStats Stats;
  11143. TListItemsVector ListItems;
  11144. size_t Index;
  11145. };
  11146. //---------------------------------------------------------------------------
  11147. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11148. {
  11149. TCalculateSizeOperation CalculateSizeOperation;
  11150. Side = GetSide(Side);
  11151. TCustomDirView * ADirView = DirView(Side);
  11152. bool LocalSide = IsSideLocalBrowser(Side);
  11153. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11154. std::unique_ptr<TStrings> FileList(new TStringList());
  11155. for (int Index = 0; Index < AllFileList->Count; Index++)
  11156. {
  11157. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11158. if (ADirView->ItemIsDirectory(Item))
  11159. {
  11160. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11161. CalculateSizeOperation.ListItems.push_back(Item);
  11162. }
  11163. }
  11164. CalculateSizeOperation.Index = 0;
  11165. __int64 Size = 0;
  11166. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  11167. FCalculateSizeOperation = &CalculateSizeOperation;
  11168. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11169. FOnFileOperationFinished = DirectorySizeCalculated;
  11170. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  11171. FPrimaryOperation = foCalculateSize;
  11172. TCalculatedSizes CalculatedSizes;
  11173. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11174. if (LocalSide)
  11175. {
  11176. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  11177. FProgressSide = Side;
  11178. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11179. }
  11180. else
  11181. {
  11182. TCalculateSizeParams Params;
  11183. Params.Stats = &CalculateSizeOperation.Stats;
  11184. Params.UseCache = true;
  11185. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11186. }
  11187. }
  11188. //---------------------------------------------------------------------------
  11189. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11190. {
  11191. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11192. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11193. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11194. // optimization
  11195. bool GotExpectedItem =
  11196. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11197. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11198. int Index = -1;
  11199. if (DebugAlwaysTrue(GotExpectedItem))
  11200. {
  11201. Index = FCalculateSizeOperation->Index;
  11202. FCalculateSizeOperation->Index++;
  11203. }
  11204. else
  11205. {
  11206. TListItemsVector::iterator I =
  11207. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11208. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11209. {
  11210. Index = I - FCalculateSizeOperation->ListItems.begin();
  11211. }
  11212. }
  11213. if (DebugAlwaysTrue(Index >= 0) && Success)
  11214. {
  11215. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11216. ADirView->SetItemCalculatedSize(Item, Size);
  11217. }
  11218. }
  11219. //---------------------------------------------------------------------------
  11220. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11221. {
  11222. Accept = false;
  11223. }
  11224. //---------------------------------------------------------------------------