CustomScpExplorer.cpp 379 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. FInvalid = false;
  146. FCurrentSide = osRemote;
  147. FEverShown = false;
  148. FDocks = new TList();
  149. InitControls();
  150. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  151. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  152. NonVisualDataModule->ScpExplorer = this;
  153. RestoreParams();
  154. ConfigurationChanged();
  155. RemoteDirView->Invalidate();
  156. FAutoOperation = false;
  157. FOnFileOperationFinished = NULL;
  158. FPrimaryOperation = foNone;
  159. FForceExecution = false;
  160. FIgnoreNextDialogChar = 0;
  161. FErrorList = NULL;
  162. FSynchronizeProgressForm = NULL;
  163. FProgressForm = NULL;
  164. FRefreshLocalDirectory = false;
  165. FRefreshRemoteDirectory = false;
  166. FDDExtMapFile = NULL;
  167. // CreateMutexW keeps failing with ERROR_NOACCESS
  168. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  169. DebugAssert(FDDExtMutex != NULL);
  170. FDDTargetControl = NULL;
  171. FDelayedDeletionTimer = NULL;
  172. FDelayedDeletionList = new TStringList();
  173. FDDFileList = NULL;
  174. FPendingTempSpaceWarn = false;
  175. FCapturedLog = NULL;
  176. FDragDropOperation = false;
  177. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  178. FAllowTransferPresetAutoSelect = true;
  179. FSessionChanging = false;
  180. FCopyParamDefault = L"";
  181. FSynchronizeController = NULL;
  182. FPendingQueueActionItem = NULL;
  183. FLockLevel = 0;
  184. FLockSuspendLevel = 0;
  185. FDisabledOnLockSuspend = false;
  186. FAlternativeDelete = false;
  187. FTrayIcon = new ::TTrayIcon(0);
  188. FTrayIcon->OnClick = TrayIconClick;
  189. FMaxQueueLength = 0;
  190. FLastContextPopupScreenPoint = TPoint(-1, -1);
  191. FTransferResumeList = NULL;
  192. FMoveToQueue = false;
  193. StandaloneOperation = false;
  194. FOnFeedSynchronizeError = NULL;
  195. FOnSynchronizeAbort = NULL;
  196. FSynchronizeTerminal = NULL;
  197. FNeedSession = false;
  198. FDoNotIdleCurrentTerminal = 0;
  199. FIncrementalSearching = 0;
  200. FIncrementalSearchHaveNext = false;
  201. FQueueFileList.reset(new TQueueFileList());
  202. FProgressSide = osCurrent;
  203. FCalculateSizeOperation = NULL;
  204. FEditorManager = new TEditorManager();
  205. FEditorManager->OnFileChange = ExecutedFileChanged;
  206. FEditorManager->OnFileReload = ExecutedFileReload;
  207. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  208. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  209. FLocalEditors = new TList();
  210. FQueueStatus = NULL;
  211. FQueueStatusSection = new TCriticalSection();
  212. FQueueStatusInvalidated = false;
  213. FQueueItemInvalidated = false;
  214. FQueueStatusUpdating = false;
  215. FQueueActedItem = NULL;
  216. FQueueController = new TQueueController(QueueView3);
  217. FUserActionTimer = new TTimer(this);
  218. FUserActionTimer->Enabled = false;
  219. FUserActionTimer->Interval = 10;
  220. FUserActionTimer->OnTimer = UserActionTimer;
  221. FNotes = new TStringList();
  222. FNoteTimer = new TTimer(this);
  223. FNoteTimer->Enabled = false;
  224. FNoteTimer->OnTimer = NoteTimer;
  225. FOnNoteClick = NULL;
  226. FOle32Library = LoadLibrary(L"Ole32.dll");
  227. FDragMoveCursor = FOle32Library != NULL ?
  228. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  229. UseSystemSettings(this);
  230. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  231. static_cast<TObject*>(GetComponent(fcTransferList)));
  232. DebugAssert(TransferList != NULL);
  233. FTransferListHoverIndex = -1;
  234. TransferList->OnChange = TransferListChange;
  235. TransferList->OnDrawItem = TransferListDrawItem;
  236. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  237. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  238. UseDesktopFont(SessionsPageControl);
  239. UpdateSessionsPageControlHeight();
  240. UseDesktopFont(RemoteDirView);
  241. UseDesktopFont(RemoteDriveView);
  242. UseDesktopFont(QueueView3);
  243. UseDesktopFont(QueueFileList);
  244. UseDesktopFont(QueueLabel);
  245. UseDesktopFont(RemoteStatusBar);
  246. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  247. QueueSplitter->ShowHint = true;
  248. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  249. QueueFileListSplitter->ShowHint = true;
  250. RemotePanelSplitter->ShowHint = true;
  251. UpdateImages();
  252. FCustomCommandMenu = CreateTBXPopupMenu(this);
  253. FCustomCommandLocalFileList = NULL;
  254. FCustomCommandRemoteFileList = NULL;
  255. FSessionColors = new TPngImageList(this);
  256. FSessionColors->ColorDepth = cd32Bit;
  257. AddFixedSessionImages();
  258. SessionsPageControl->Images = FSessionColors;
  259. UpdateQueueLabel();
  260. CreateHiddenWindow();
  261. StartUpdates();
  262. }
  263. //---------------------------------------------------------------------------
  264. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  265. {
  266. FInvalid = true;
  267. // this has to be one of the very first things to do
  268. StopUpdateThread();
  269. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  270. TTerminalManager::Instance()->ScpExplorer = NULL;
  271. delete FCustomCommandLocalFileList;
  272. delete FCustomCommandRemoteFileList;
  273. delete FCustomCommandMenu;
  274. delete FTrayIcon;
  275. FTrayIcon = NULL;
  276. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  277. delete FEditorManager;
  278. ForceCloseLocalEditors();
  279. delete FLocalEditors;
  280. HideFileFindDialog();
  281. if (FDelayedDeletionTimer)
  282. {
  283. DoDelayedDeletion(NULL);
  284. SAFE_DESTROY(FDelayedDeletionTimer);
  285. }
  286. SAFE_DESTROY(FDelayedDeletionList);
  287. // sometimes we do not get DDEnd so the list is not released
  288. SAFE_DESTROY(FDDFileList);
  289. DebugAssert(FSynchronizeController == NULL);
  290. CloseHandle(FDDExtMutex);
  291. FDDExtMutex = NULL;
  292. FreeLibrary(FOle32Library);
  293. FOle32Library = NULL;
  294. FDragMoveCursor = NULL;
  295. DebugAssert(!FErrorList);
  296. if (FEverShown)
  297. {
  298. // when window is never shown (like when running command-line operation),
  299. // particularly window site is not restored correctly (BoundsRect value set
  300. // in RestoreForm gets lost during handle allocation), so we do not want
  301. // it to be stored
  302. StoreParams();
  303. }
  304. ManagedSession = NULL;
  305. Queue = NULL;
  306. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  307. NonVisualDataModule->ScpExplorer = NULL;
  308. delete FQueueController;
  309. FQueueController = NULL;
  310. delete FQueueStatusSection;
  311. FQueueStatusSection = NULL;
  312. delete FQueueStatus;
  313. FQueueStatus = NULL;
  314. delete FUserActionTimer;
  315. FUserActionTimer = NULL;
  316. delete FNoteTimer;
  317. delete FNotes;
  318. delete FNoteData;
  319. SAFE_DESTROY(FDocks);
  320. SessionsPageControl->Images = NULL;
  321. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  322. SAFE_DESTROY(FSessionColors);
  323. SAFE_DESTROY(FSessionsDragDropFilesEx);
  324. SAFE_DESTROY(FHistoryMenu[0][0]);
  325. SAFE_DESTROY(FHistoryMenu[0][1]);
  326. SAFE_DESTROY(FHistoryMenu[1][0]);
  327. SAFE_DESTROY(FHistoryMenu[1][1]);
  328. if (FHiddenWindow != NULL)
  329. {
  330. DestroyWindow(FHiddenWindow);
  331. FHiddenWindow = NULL;
  332. }
  333. }
  334. //---------------------------------------------------------------------------
  335. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  336. {
  337. std::unique_ptr<TSessionData> Data;
  338. if (!Session.IsEmpty())
  339. {
  340. bool DefaultsOnly;
  341. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  342. }
  343. TTerminalManager * Manager = TTerminalManager::Instance();
  344. for (int Index = 0; Index < Manager->Count; Index++)
  345. {
  346. TTerminal * Terminal = Manager->Sessions[Index];
  347. if (Session.IsEmpty() ||
  348. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  349. {
  350. if (Path.IsEmpty())
  351. {
  352. Terminal->ClearCaches();
  353. }
  354. else
  355. {
  356. Terminal->DirectoryModified(Path, true);
  357. }
  358. }
  359. }
  360. // We should flag a pending refresh for the background terminals or busy foreground terminals
  361. if (HasActiveTerminal() &&
  362. CanCommandLineFromAnotherInstance() &&
  363. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  364. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  365. {
  366. Terminal->ReloadDirectory();
  367. }
  368. }
  369. //---------------------------------------------------------------------------
  370. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  371. {
  372. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  373. size_t MessageSize = sizeof(TCopyDataMessage);
  374. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  375. if (Result)
  376. {
  377. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  378. Result = (Message.Version == TCopyDataMessage::Version1);
  379. if (Result)
  380. {
  381. switch (Message.Command)
  382. {
  383. case TCopyDataMessage::CommandCanCommandLine:
  384. Result = CanCommandLineFromAnotherInstance();
  385. break;
  386. case TCopyDataMessage::CommandCommandLine:
  387. {
  388. UnicodeString CommandLine(Message.CommandLine);
  389. Result = CommandLineFromAnotherInstance(CommandLine);
  390. }
  391. break;
  392. case TCopyDataMessage::RefreshPanel:
  393. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  394. break;
  395. case TCopyDataMessage::MainWindowCheck:
  396. Result = true;
  397. break;
  398. default:
  399. Result = false;
  400. break;
  401. }
  402. }
  403. }
  404. Message.Result = Result ? 1 : 0;
  405. }
  406. //---------------------------------------------------------------------------
  407. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  408. {
  409. WNDCLASS WindowClass = {0};
  410. WindowClass.lpfnWndProc = DefWindowProc;
  411. WindowClass.hInstance = HInstance;
  412. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  413. FHiddenWindow = NULL;
  414. if (RegisterClass(&WindowClass))
  415. {
  416. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  417. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  418. }
  419. }
  420. //---------------------------------------------------------------------------
  421. bool __fastcall TCustomScpExplorerForm::CanConsole()
  422. {
  423. return
  424. HasActiveTerminal() &&
  425. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  426. }
  427. //---------------------------------------------------------------------------
  428. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  429. {
  430. bool Result = Visible && !NonVisualDataModule->Busy;
  431. return Result;
  432. }
  433. //---------------------------------------------------------------------------
  434. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  435. const UnicodeString & CommandLine)
  436. {
  437. TProgramParams Params(CommandLine);
  438. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  439. if (Result)
  440. {
  441. NonVisualDataModule->StartBusy();
  442. try
  443. {
  444. // this action is initiated from another process,
  445. // so it's likely that our window is not visible,
  446. // and user won't see what is going on
  447. Application->BringToFront();
  448. // reload sessions as we may be asked to open a session
  449. // just stored by another instance
  450. StoredSessions->Reload();
  451. UnicodeString SessionName = Params.Param[1];
  452. std::unique_ptr<TObjectList> DataList(new TObjectList());
  453. try
  454. {
  455. UnicodeString DownloadFile; // unused
  456. int Flags = GetCommandLineParseUrlFlags(&Params);
  457. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  458. }
  459. catch (EAbort &)
  460. {
  461. // Happens when opening session in PuTTY and in other situations,
  462. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  463. // to process the command-line on its own (or to pass it to yet another instance)
  464. }
  465. if (DataList->Count > 0)
  466. {
  467. TTerminalManager * Manager = TTerminalManager::Instance();
  468. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  469. }
  470. }
  471. __finally
  472. {
  473. NonVisualDataModule->EndBusy();
  474. }
  475. }
  476. return Result;
  477. }
  478. //---------------------------------------------------------------------------
  479. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  480. {
  481. TManagedTerminal * Result;
  482. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  483. {
  484. Result = FManagedSession;
  485. }
  486. else
  487. {
  488. Result = NULL;
  489. }
  490. return Result;
  491. }
  492. //---------------------------------------------------------------------------
  493. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  494. {
  495. if (FManagedSession != value)
  496. {
  497. SessionChanging();
  498. DoSetManagedSession(value, false);
  499. }
  500. }
  501. //---------------------------------------------------------------------------
  502. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  503. {
  504. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  505. FManagedSession = value;
  506. {
  507. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  508. FAllowTransferPresetAutoSelect = false;
  509. TAutoFlag SessionChangingFlag(FSessionChanging);
  510. SessionChanged(Replace);
  511. }
  512. if (Terminal != NULL)
  513. {
  514. TransferPresetAutoSelect();
  515. }
  516. // Update app and tab titles, prevented above by FSessionChanging.
  517. UpdateControls();
  518. }
  519. //---------------------------------------------------------------------------
  520. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  521. {
  522. DoSetManagedSession(value, true);
  523. }
  524. //---------------------------------------------------------------------------
  525. void __fastcall TCustomScpExplorerForm::SessionChanging()
  526. {
  527. if (ManagedSession != NULL)
  528. {
  529. UpdateSession(ManagedSession);
  530. }
  531. }
  532. //---------------------------------------------------------------------------
  533. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  534. {
  535. if (Terminal != NULL)
  536. {
  537. UpdateSessionColor((TColor)Terminal->StateData->Color);
  538. }
  539. if (!TTerminalManager::Instance()->Updating)
  540. {
  541. SessionListChanged();
  542. }
  543. if (Terminal != NULL)
  544. {
  545. TObject * State = NULL;
  546. if (WinConfiguration->PreservePanelState)
  547. {
  548. State = Terminal->RemoteExplorerState;
  549. DirView(osRemote)->AnnounceState(State);
  550. }
  551. try
  552. {
  553. if (Replaced)
  554. {
  555. RemoteDirView->ReplaceTerminal(Terminal);
  556. }
  557. else
  558. {
  559. RemoteDirView->Terminal = Terminal;
  560. }
  561. if (Terminal->Active)
  562. {
  563. Terminal->RefreshDirectory();
  564. }
  565. if (State != NULL)
  566. {
  567. DirView(osRemote)->RestoreState(State);
  568. }
  569. }
  570. __finally
  571. {
  572. DirView(osRemote)->AnnounceState(NULL);
  573. }
  574. }
  575. else
  576. {
  577. RemoteDirView->Terminal = NULL;
  578. }
  579. InitStatusBar();
  580. UpdateTransferList();
  581. // Update panels Enable state before refreshing the labels
  582. UpdateControls();
  583. }
  584. //---------------------------------------------------------------------------
  585. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  586. {
  587. if (Queue != value)
  588. {
  589. if (FQueue != NULL)
  590. {
  591. FQueue->OnListUpdate = NULL;
  592. FQueue->OnQueueItemUpdate = NULL;
  593. }
  594. FQueue = value;
  595. if (FQueue != NULL)
  596. {
  597. DebugAssert(FQueue->OnListUpdate == NULL);
  598. FQueue->OnListUpdate = QueueListUpdate;
  599. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  600. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  601. }
  602. QueueChanged();
  603. }
  604. }
  605. //---------------------------------------------------------------------------
  606. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  607. TListItem * Item)
  608. {
  609. if (FQueueActedItem == Item)
  610. {
  611. FQueueActedItem = NULL;
  612. if ((QueueView3->PopupMenu != NULL) &&
  613. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  614. {
  615. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  616. ReleaseCapture();
  617. }
  618. }
  619. if (Item->Data == FPendingQueueActionItem)
  620. {
  621. FPendingQueueActionItem = NULL;
  622. }
  623. }
  624. //---------------------------------------------------------------------------
  625. bool TCustomScpExplorerForm::IsAnythingQueued()
  626. {
  627. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  628. }
  629. //---------------------------------------------------------------------------
  630. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  631. {
  632. {
  633. TGuard Guard(FQueueStatusSection);
  634. FQueueStatusInvalidated = false;
  635. if (FQueue != NULL)
  636. {
  637. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  638. }
  639. }
  640. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  641. {
  642. FMaxQueueLength = FQueueStatus->Count;
  643. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  644. }
  645. {
  646. TAutoFlag Flag(FQueueStatusUpdating);
  647. FQueueController->UpdateQueueStatus(FQueueStatus);
  648. }
  649. UpdateQueueFileList();
  650. SetQueueProgress();
  651. UpdateQueueView();
  652. bool IsEmpty = !IsAnythingQueued();
  653. if (IsEmpty && (Terminal != NULL))
  654. {
  655. TOnceDoneOperation OnceDoneOperation =
  656. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  657. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  658. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  659. {
  660. FQueue->Enabled = false;
  661. }
  662. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  663. {
  664. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  665. }
  666. }
  667. }
  668. //---------------------------------------------------------------------------
  669. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  670. {
  671. UnicodeString Result;
  672. if (FQueueStatus != NULL)
  673. {
  674. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  675. {
  676. TFileOperationProgressType * ProgressData =
  677. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  678. if ((ProgressData != NULL) && ProgressData->InProgress)
  679. {
  680. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  681. }
  682. }
  683. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  684. {
  685. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  686. }
  687. }
  688. return Result;
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  692. {
  693. ComponentVisible[fcQueueView] =
  694. !IsLocalBrowserMode() &&
  695. ((WinConfiguration->QueueView.Show == qvShow) ||
  696. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  697. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  698. }
  699. //---------------------------------------------------------------------------
  700. void __fastcall TCustomScpExplorerForm::QueueChanged()
  701. {
  702. if (FQueueStatus != NULL)
  703. {
  704. // UpdateFileList implementation relies on the status being recreated when session changes
  705. delete FQueueStatus;
  706. FQueueStatus = NULL;
  707. }
  708. UpdateQueueStatus(true);
  709. }
  710. //---------------------------------------------------------------------------
  711. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  712. {
  713. if (FQueue == Queue)
  714. {
  715. FQueueStatusInvalidated = true;
  716. }
  717. }
  718. //---------------------------------------------------------------------------
  719. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  720. TQueueItem * Item)
  721. {
  722. if (FQueue == Queue)
  723. {
  724. TGuard Guard(FQueueStatusSection);
  725. // this may be running in parallel with QueueChanged
  726. if (FQueueStatus != NULL)
  727. {
  728. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  729. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  730. {
  731. FRefreshLocalDirectory = (QueueItem == NULL) ||
  732. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  733. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  734. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  735. }
  736. if (QueueItem != NULL)
  737. {
  738. QueueItem->UserData = (void*)true;
  739. FQueueItemInvalidated = true;
  740. }
  741. }
  742. }
  743. }
  744. //---------------------------------------------------------------------------
  745. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  746. {
  747. // during standalone editing, we have no way to see/control queue,
  748. // so we have to always popup prompts automatically
  749. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  750. }
  751. //---------------------------------------------------------------------------
  752. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  753. {
  754. if (FQueueStatus != NULL)
  755. {
  756. bool QueueAutoPopup = IsQueueAutoPopup();
  757. bool NeedRefresh = FQueueController->NeedRefresh();
  758. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  759. FQueueItemInvalidated = false;
  760. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  761. bool Updated = false;
  762. TQueueItemProxy * QueueItem;
  763. bool WasUserAction;
  764. for (int Index = 0; Index < Limit; Index++)
  765. {
  766. bool Update = false;
  767. QueueItem = FQueueStatus->Items[Index];
  768. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  769. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  770. (FPendingQueueActionItem == NULL))
  771. {
  772. FPendingQueueActionItem = QueueItem;
  773. FUserActionTimer->Enabled = true;
  774. }
  775. if ((bool)QueueItem->UserData)
  776. {
  777. QueueItem->UserData = (void*)false;
  778. QueueItem->Update();
  779. Updated = true;
  780. Update = true;
  781. }
  782. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  783. {
  784. Update = true;
  785. }
  786. if (Update)
  787. {
  788. FQueueController->RefreshQueueItem(QueueItem);
  789. }
  790. }
  791. if (Updated)
  792. {
  793. NonVisualDataModule->UpdateNonVisibleActions();
  794. SetQueueProgress();
  795. UpdateQueueFileList();
  796. }
  797. }
  798. }
  799. //---------------------------------------------------------------------------
  800. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  801. {
  802. if (FTaskbarList != NULL)
  803. {
  804. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  805. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  806. }
  807. }
  808. //---------------------------------------------------------------------------
  809. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  810. {
  811. if (Progress >= 0)
  812. {
  813. if (FTaskbarList != NULL)
  814. {
  815. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  816. }
  817. }
  818. else
  819. {
  820. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  821. }
  822. }
  823. //---------------------------------------------------------------------------
  824. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  825. {
  826. int OverallProgress;
  827. // FProgressForm is null when this is called from SetQueueProgress
  828. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  829. {
  830. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  831. }
  832. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  833. {
  834. OverallProgress = ProgressData->OverallProgress();
  835. }
  836. else
  837. {
  838. OverallProgress = -1;
  839. }
  840. SetTaskbarListProgressValue(OverallProgress);
  841. }
  842. //---------------------------------------------------------------------------
  843. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  844. {
  845. TTerminalManager::Instance()->QueueStatusUpdated();
  846. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  847. {
  848. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  849. {
  850. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  851. {
  852. TFileOperationProgressType * ProgressData = NULL;
  853. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  854. {
  855. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  856. }
  857. if ((ProgressData != NULL) &&
  858. ProgressData->InProgress)
  859. {
  860. SetTaskbarListProgressValue(ProgressData);
  861. }
  862. else
  863. {
  864. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  865. }
  866. }
  867. else
  868. {
  869. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  870. }
  871. }
  872. else
  873. {
  874. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  875. }
  876. }
  877. UpdateQueueLabel();
  878. }
  879. //---------------------------------------------------------------------------
  880. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  881. {
  882. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  883. if (FQueueStatus != NULL)
  884. {
  885. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  886. if (ActiveAndPendingPrimaryCount > 0)
  887. {
  888. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  889. }
  890. }
  891. QueueLabel->Caption = Caption;
  892. }
  893. //---------------------------------------------------------------------------
  894. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  895. {
  896. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  897. static_cast<TComponent*>(GetComponent(fcTransferList)));
  898. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  899. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  900. TransferList->Strings->BeginUpdate();
  901. try
  902. {
  903. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  904. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  905. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  906. if (FTransferDropDownHint.IsEmpty())
  907. {
  908. FTransferDropDownHint = TransferDropDown->Hint;
  909. }
  910. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  911. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  912. TransferDropDown->Text = StripHotkey(Name);
  913. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  914. (FTransferDropDownHint, StripHotkey(Name),
  915. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  916. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  917. // update the label, otherwise when it is updated only on the first draw
  918. // of the list, it is drawn "bold" for some reason
  919. FTransferListHoverIndex = TransferList->ItemIndex;
  920. UpdateTransferLabel();
  921. }
  922. __finally
  923. {
  924. TransferList->Strings->EndUpdate();
  925. }
  926. }
  927. //---------------------------------------------------------------------------
  928. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  929. {
  930. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  931. DebugAssert(Toolbar != NULL);
  932. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  933. }
  934. //---------------------------------------------------------------------------
  935. void __fastcall TCustomScpExplorerForm::UpdateActions()
  936. {
  937. TForm::UpdateActions();
  938. if (ComponentVisible[fcCustomCommandsBand])
  939. {
  940. UpdateCustomCommandsToolbar();
  941. }
  942. }
  943. //---------------------------------------------------------------------------
  944. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  945. {
  946. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  947. }
  948. //---------------------------------------------------------------------------
  949. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  950. {
  951. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  952. {
  953. DirView->RowSelect = WinConfiguration->FullRowSelect;
  954. // selection is not redrawn automatically when RowSelect changes
  955. DirView->Invalidate();
  956. }
  957. }
  958. //---------------------------------------------------------------------------
  959. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  960. {
  961. Color = GetBtnFaceColor();
  962. DebugAssert(Configuration && RemoteDirView);
  963. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  964. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  965. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  966. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  967. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  968. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  969. UpdateRowSelect(RemoteDirView);
  970. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  971. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  972. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  973. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  974. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  975. UpdateSessionsPageControlHeight();
  976. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  977. UpdateToolbarDisplayMode();
  978. UpdateTransferList();
  979. if (Terminal != NULL)
  980. {
  981. TransferPresetAutoSelect();
  982. }
  983. UpdateQueueView();
  984. UpdateControls();
  985. // this can be called even before constuctor finishes.
  986. if (FEditorManager != NULL)
  987. {
  988. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  989. }
  990. // this can be called even before constuctor finishes.
  991. if (FLocalEditors != NULL)
  992. {
  993. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  994. {
  995. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  996. }
  997. }
  998. if (ComponentVisible[fcCustomCommandsBand])
  999. {
  1000. UpdateCustomCommandsToolbar();
  1001. }
  1002. // show only when keeping queue items forever,
  1003. // otherwise, its enough to have in in the context menu
  1004. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1005. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1006. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1007. {
  1008. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1009. FFileColorsCurrent = WinConfiguration->FileColors;
  1010. FileColorsChanged();
  1011. }
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1015. {
  1016. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1017. DirView->Invalidate();
  1018. }
  1019. //---------------------------------------------------------------------------
  1020. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1021. {
  1022. DoFileColorsChanged(RemoteDirView);
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1026. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1027. void * /*Arg*/)
  1028. {
  1029. if (Token != NULL)
  1030. {
  1031. TForm * Editor = dynamic_cast<TForm*>(Token);
  1032. DebugAssert(Editor != NULL);
  1033. ReconfigureEditorForm(Editor);
  1034. }
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1038. {
  1039. WinConfiguration->DDTransferConfirmation = asOn;
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1043. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1044. {
  1045. // noop
  1046. }
  1047. //---------------------------------------------------------------------------
  1048. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1049. {
  1050. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1051. }
  1052. //---------------------------------------------------------------------------
  1053. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1054. {
  1055. if (DoNotShowAgain)
  1056. {
  1057. if (DragDrop)
  1058. {
  1059. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1060. {
  1061. PopupTrayBalloon(
  1062. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1063. }
  1064. WinConfiguration->DDTransferConfirmation = asOff;
  1065. }
  1066. else
  1067. {
  1068. WinConfiguration->ConfirmTransferring = false;
  1069. }
  1070. }
  1071. else
  1072. {
  1073. // User explicitly unchecked "do not show again",
  1074. // so show him the dialog the next time
  1075. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1076. {
  1077. WinConfiguration->DDTransferConfirmation = asOn;
  1078. }
  1079. }
  1080. }
  1081. //---------------------------------------------------------------------------
  1082. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1083. TTransferDirection Direction, TTransferType Type, bool Temp,
  1084. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1085. bool Confirm, bool DragDrop, int Options)
  1086. {
  1087. bool Result = true;
  1088. DebugAssert(HasActiveTerminal());
  1089. // these parameters are known in advance
  1090. int Params =
  1091. FLAGMASK(Type == ttMove, cpDelete);
  1092. bool ToTemp = (Temp && (Direction == tdToLocal));
  1093. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1094. {
  1095. bool DisableNewerOnly =
  1096. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1097. ToTemp;
  1098. Options |=
  1099. FLAGMASK(ToTemp, coTemp) |
  1100. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1101. coDoNotShowAgain;
  1102. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1103. int CopyParamAttrs = (Direction == tdToRemote ?
  1104. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1105. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1106. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1107. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1108. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1109. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1110. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1111. if (Result)
  1112. {
  1113. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1114. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1115. }
  1116. }
  1117. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1118. {
  1119. Configuration->Usage->Inc(L"TransfersOnBackground");
  1120. // these parameter are known only after transfer dialog
  1121. Params |=
  1122. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1123. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1124. Result = false;
  1125. ClearTransferSourceSelection(Direction);
  1126. }
  1127. return Result;
  1128. }
  1129. //---------------------------------------------------------------------------
  1130. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1131. {
  1132. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1133. if (FOnFileOperationFinished != NULL)
  1134. {
  1135. FOnFileOperationFinished(Side, UnicodeString(), true);
  1136. }
  1137. else
  1138. {
  1139. if (HasDirView[Side])
  1140. {
  1141. DirView(Side)->SelectAll(smNone);
  1142. }
  1143. }
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1147. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1148. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1149. int Params)
  1150. {
  1151. DebugAssert(Queue != NULL);
  1152. bool SingleFile = false;
  1153. if (FileList->Count == 1)
  1154. {
  1155. if (Direction == tdToRemote)
  1156. {
  1157. UnicodeString FileName = FileList->Strings[0];
  1158. SingleFile = FileExists(ApiPath(FileName));
  1159. }
  1160. else
  1161. {
  1162. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1163. SingleFile = !File->IsDirectory;
  1164. }
  1165. }
  1166. TQueueItem * QueueItem;
  1167. if (Direction == tdToRemote)
  1168. {
  1169. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1170. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1171. }
  1172. else
  1173. {
  1174. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1175. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1176. }
  1177. AddQueueItem(Queue, QueueItem, Terminal);
  1178. }
  1179. //---------------------------------------------------------------------------
  1180. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1181. {
  1182. if (Queue->IsEmpty)
  1183. {
  1184. Terminal->QueueOperationStart = Now();
  1185. }
  1186. Queue->AddItem(QueueItem);
  1187. }
  1188. //---------------------------------------------------------------------------
  1189. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1190. {
  1191. }
  1192. //---------------------------------------------------------------------------
  1193. void __fastcall TCustomScpExplorerForm::InitControls()
  1194. {
  1195. DebugAssert(FDocks != NULL);
  1196. for (int Index = 0; Index < ComponentCount; Index++)
  1197. {
  1198. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1199. if ((Dock != NULL) && (Dock->Tag == 0))
  1200. {
  1201. FDocks->Add(Dock);
  1202. }
  1203. }
  1204. CollectItemsWithTextDisplayMode(this);
  1205. }
  1206. //---------------------------------------------------------------------------
  1207. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1208. {
  1209. ConfigurationChanged();
  1210. QueuePanel->Height =
  1211. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1212. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1213. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1214. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1215. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1216. QueueFileList->Height =
  1217. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1218. if (QueueFileList->Visible)
  1219. {
  1220. AdjustQueueLayout();
  1221. }
  1222. UpdateCustomCommandsToolbar();
  1223. }
  1224. //---------------------------------------------------------------------------
  1225. void __fastcall TCustomScpExplorerForm::StoreParams()
  1226. {
  1227. int APixelsPerInch = GetControlPixelsPerInch(this);
  1228. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1229. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1230. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1231. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1232. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1233. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1234. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1235. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1236. }
  1237. //---------------------------------------------------------------------------
  1238. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1239. {
  1240. if (!FFormRestored)
  1241. {
  1242. FFormRestored = true;
  1243. RestoreFormParams();
  1244. }
  1245. TForm::CreateParams(Params);
  1246. }
  1247. //---------------------------------------------------------------------------
  1248. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1249. {
  1250. DebugAssert(FDocks != NULL);
  1251. for (int Index = 0; Index < FDocks->Count; Index++)
  1252. {
  1253. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1254. }
  1255. }
  1256. //---------------------------------------------------------------------------
  1257. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1258. {
  1259. SetDockAllowDrag(true);
  1260. while (!ButtonsStr.IsEmpty())
  1261. {
  1262. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1263. UnicodeString Name = CutToChar(S, L'=', true);
  1264. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1265. {
  1266. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1267. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1268. {
  1269. while (!S.IsEmpty())
  1270. {
  1271. UnicodeString S2 = CutToChar(S, L',', true);
  1272. UnicodeString Name = CutToChar(S2, L':', true);
  1273. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1274. {
  1275. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1276. if (SameText(GetToolbarItemName(Item), Name))
  1277. {
  1278. // forward compatibility
  1279. UnicodeString S3 = CutToChar(S2, L':', true);
  1280. bool Visible = StrToIntDef(S3, 0);
  1281. Item->Visible = Visible;
  1282. }
  1283. }
  1284. }
  1285. }
  1286. }
  1287. }
  1288. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1289. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1290. }
  1291. //---------------------------------------------------------------------------
  1292. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1293. {
  1294. UnicodeString Result;
  1295. SetDockAllowDrag(true);
  1296. Result = ::GetToolbarsLayoutStr(this);
  1297. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1298. return Result;
  1299. }
  1300. //---------------------------------------------------------------------------
  1301. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1302. {
  1303. UnicodeString Result;
  1304. if (Item->Action != NULL)
  1305. {
  1306. Result = Item->Action->Name;
  1307. Result = RemoveSuffix(Result, L"Action", true);
  1308. }
  1309. else
  1310. {
  1311. Result = Item->Name;
  1312. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1313. Result = RemoveSuffix(Result, L"Item", true);
  1314. }
  1315. return Result;
  1316. }
  1317. //---------------------------------------------------------------------------
  1318. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1319. {
  1320. UnicodeString Result;
  1321. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1322. {
  1323. UnicodeString ToolbarStr;
  1324. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1325. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1326. {
  1327. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1328. {
  1329. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1330. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1331. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1332. // and save the changes here only.
  1333. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1334. {
  1335. UnicodeString Name = GetToolbarItemName(Item);
  1336. DebugAssert(Name.Pos(L"TBX") == 0);
  1337. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1338. }
  1339. }
  1340. }
  1341. if (!ToolbarStr.IsEmpty())
  1342. {
  1343. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1344. }
  1345. }
  1346. return Result;
  1347. }
  1348. //---------------------------------------------------------------------------
  1349. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1350. {
  1351. DebugAssert(FProgressForm == NULL);
  1352. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1353. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1354. FProgressForm->DeleteLocalToRecycleBin =
  1355. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1356. FProgressForm->DeleteRemoteToRecycleBin =
  1357. HasActiveTerminal() &&
  1358. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1359. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1360. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1361. LockWindow();
  1362. // Actually, do not know what hides the progress once the operation finishes
  1363. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1364. SetTaskbarListProgressState(TBPF_NORMAL);
  1365. }
  1366. //---------------------------------------------------------------------------
  1367. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1368. {
  1369. UnlockWindow();
  1370. SAFE_DESTROY(FProgressForm);
  1371. SetQueueProgress();
  1372. }
  1373. //---------------------------------------------------------------------------
  1374. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1375. TFileOperationProgressType & ProgressData)
  1376. {
  1377. // Download to temporary local directory
  1378. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1379. {
  1380. bool Continue = true;
  1381. FPendingTempSpaceWarn = false;
  1382. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1383. if (!Continue)
  1384. {
  1385. Abort();
  1386. }
  1387. }
  1388. // operation is being executed and we still didn't show up progress form
  1389. if (ProgressData.InProgress && !FProgressForm)
  1390. {
  1391. CreateProgressForm(NULL);
  1392. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1393. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1394. }
  1395. // operation is finished (or terminated), so we hide progress form
  1396. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1397. {
  1398. DestroyProgressForm();
  1399. if (ProgressData.Operation == foCalculateSize)
  1400. {
  1401. // When calculating size before transfer, the abort caused by
  1402. // cancel flag set due to "MoveToQueue" below,
  1403. // is not propagated back to us in ExecuteFileOperation,
  1404. // as it is silently swallowed.
  1405. // So we explicitly (re)throw it here.
  1406. if (FMoveToQueue)
  1407. {
  1408. Abort();
  1409. }
  1410. }
  1411. else
  1412. {
  1413. if ((ProgressData.Cancel == csContinue) &&
  1414. !FAutoOperation)
  1415. {
  1416. OperationComplete(ProgressData.StartTime);
  1417. }
  1418. }
  1419. }
  1420. if (FProgressForm &&
  1421. // Can happen only during synchronization, when ending one of the operations
  1422. ProgressData.InProgress)
  1423. {
  1424. FProgressForm->SetProgressData(ProgressData);
  1425. DebugAssert(ProgressData.InProgress);
  1426. SetTaskbarListProgressValue(&ProgressData);
  1427. if (FProgressForm->Cancel > csContinue)
  1428. {
  1429. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1430. if (FProgressForm->Cancel == csCancelFile)
  1431. {
  1432. FProgressForm->ClearCancel();
  1433. }
  1434. // cancel cancels even the move
  1435. FMoveToQueue = false;
  1436. }
  1437. else if (FProgressForm->MoveToQueue)
  1438. {
  1439. FMoveToQueue = true;
  1440. ProgressData.SetCancelAtLeast(csCancel);
  1441. }
  1442. if ((FTransferResumeList != NULL) &&
  1443. ProgressData.InProgress &&
  1444. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1445. !ProgressData.FullFileName.IsEmpty())
  1446. {
  1447. if ((FTransferResumeList->Count == 0) ||
  1448. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1449. {
  1450. // note that we do not recognize directories from files here
  1451. FTransferResumeList->Add(ProgressData.FullFileName);
  1452. }
  1453. }
  1454. }
  1455. }
  1456. //---------------------------------------------------------------------------
  1457. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1458. const TDateTime & StartTime)
  1459. {
  1460. if (GUIConfiguration->BeepOnFinish &&
  1461. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1462. {
  1463. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1464. DWORD Sound;
  1465. if (!ExtractFileExt(BeepSound).IsEmpty())
  1466. {
  1467. Sound = SND_FILENAME;
  1468. }
  1469. else
  1470. {
  1471. Sound = SND_ALIAS;
  1472. }
  1473. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1474. }
  1475. }
  1476. //---------------------------------------------------------------------------
  1477. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1478. TFileOperationProgressType & ProgressData)
  1479. {
  1480. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1481. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1482. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1483. FileOperationProgress(ProgressData);
  1484. }
  1485. //---------------------------------------------------------------------------
  1486. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1487. {
  1488. UnicodeString Result;
  1489. if (FProgressForm != NULL)
  1490. {
  1491. Result = FProgressForm->ProgressStr();
  1492. }
  1493. return Result;
  1494. }
  1495. //---------------------------------------------------------------------------
  1496. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1497. bool DragDrop)
  1498. {
  1499. return (!DragDrop && (DropSourceControl == NULL)) ||
  1500. (DropSourceControl == DirView(osOther));
  1501. }
  1502. //---------------------------------------------------------------------------
  1503. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1504. {
  1505. TCustomDirView * ADirView = DirView(Side);
  1506. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1507. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1508. // this can happen when local drive is unplugged in the middle of the operation
  1509. if (Item != NULL)
  1510. {
  1511. if (Unselect)
  1512. {
  1513. Item->Selected = false;
  1514. }
  1515. ADirView->MakeProgressVisible(Item);
  1516. }
  1517. return Item;
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1521. TFileOperation Operation, TOperationSide Side,
  1522. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1523. TOnceDoneOperation & OnceDoneOperation)
  1524. {
  1525. if (!FAutoOperation)
  1526. {
  1527. // no selection on "/upload", form serves only as event handler
  1528. // (it is not displayed)
  1529. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1530. ((Operation == FPrimaryOperation) ||
  1531. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1532. {
  1533. TOperationSide DViewSide;
  1534. if ((FProgressSide != osCurrent) &&
  1535. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1536. {
  1537. // assume the operation is over the focused panel
  1538. DViewSide = FProgressSide;
  1539. }
  1540. else
  1541. {
  1542. DViewSide = Side;
  1543. }
  1544. if (FOnFileOperationFinished != NULL)
  1545. {
  1546. FOnFileOperationFinished(DViewSide, FileName, Success);
  1547. }
  1548. else
  1549. {
  1550. VisualiseOperationFinished(DViewSide, FileName, Success);
  1551. }
  1552. }
  1553. if ((Operation == foCopy) || (Operation == foMove))
  1554. {
  1555. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1556. {
  1557. if (Side == osLocal)
  1558. {
  1559. Configuration->Usage->Inc(L"UploadedFiles");
  1560. }
  1561. else
  1562. {
  1563. Configuration->Usage->Inc(L"DownloadedFiles");
  1564. }
  1565. }
  1566. }
  1567. }
  1568. if (Success && (FSynchronizeController != NULL))
  1569. {
  1570. DebugAssert(!IsSideLocalBrowser(osRemote));
  1571. if (Operation == foCopy)
  1572. {
  1573. DebugAssert(Side == osLocal);
  1574. FSynchronizeController->LogOperation(soUpload, FileName);
  1575. }
  1576. else if (Operation == foDelete)
  1577. {
  1578. DebugAssert(Side == osRemote);
  1579. FSynchronizeController->LogOperation(soDelete, FileName);
  1580. }
  1581. }
  1582. if (FProgressForm)
  1583. {
  1584. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1585. }
  1586. }
  1587. //---------------------------------------------------------------------------
  1588. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1589. TFileOperation Operation, TOperationSide Side,
  1590. bool Temp, const UnicodeString & FileName, Boolean Success,
  1591. TOnceDoneOperation & OnceDoneOperation)
  1592. {
  1593. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1594. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1595. OnceDoneOperation);
  1596. }
  1597. //---------------------------------------------------------------------------
  1598. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1599. {
  1600. return false;
  1601. }
  1602. //---------------------------------------------------------------------------
  1603. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1604. {
  1605. return false;
  1606. }
  1607. //---------------------------------------------------------------------------
  1608. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1609. {
  1610. return false;
  1611. }
  1612. //---------------------------------------------------------------------------
  1613. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1614. {
  1615. return NULL;
  1616. }
  1617. //---------------------------------------------------------------------------
  1618. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1619. {
  1620. DebugAssert(GetSide(Side) == osRemote);
  1621. DebugUsedParam(Side);
  1622. return RemoteDirView;
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1626. {
  1627. DebugAssert(GetSide(Side) == osRemote);
  1628. DebugUsedParam(Side);
  1629. return RemoteDriveView;
  1630. }
  1631. //---------------------------------------------------------------------------
  1632. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1633. {
  1634. DebugAssert(GetSide(Side) == osRemote);
  1635. DebugUsedParam(Side);
  1636. return HasActiveTerminal();
  1637. }
  1638. //---------------------------------------------------------------------------
  1639. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1640. TOperationSide Side, int FilesOnly)
  1641. {
  1642. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1643. }
  1644. //---------------------------------------------------------------------------
  1645. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1646. TOperationSide Side, int FilesOnly)
  1647. {
  1648. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1649. }
  1650. //---------------------------------------------------------------------------
  1651. struct THistoryItemData
  1652. {
  1653. short int Side;
  1654. short int Index;
  1655. };
  1656. //---------------------------------------------------------------------------
  1657. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1658. {
  1659. DirView(Side)->HistoryGo(Index);
  1660. }
  1661. //---------------------------------------------------------------------------
  1662. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1663. {
  1664. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1665. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1666. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1667. }
  1668. //---------------------------------------------------------------------------
  1669. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1670. bool Back)
  1671. {
  1672. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1673. {
  1674. TCustomDirView * DView = DirView(Side);
  1675. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1676. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1677. if (ICount > 10)
  1678. {
  1679. ICount = 10;
  1680. }
  1681. Menu->Items->Clear();
  1682. THistoryItemData Data;
  1683. Data.Side = Side;
  1684. for (short int i = 1; i <= ICount; i++)
  1685. {
  1686. TTBCustomItem * Item = new TTBXItem(Menu);
  1687. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1688. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1689. Item->Hint = DView->HistoryPath[Data.Index];
  1690. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1691. Item->Tag = *reinterpret_cast<int*>(&Data);
  1692. Item->OnClick = HistoryItemClick;
  1693. Menu->Items->Add(Item);
  1694. }
  1695. }
  1696. }
  1697. //---------------------------------------------------------------------------
  1698. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1699. TOperationSide Side, bool Back)
  1700. {
  1701. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1702. {
  1703. // workaround
  1704. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1705. // operator new allocates memory in Pascal code, but calls inline
  1706. // contructor in C++, leading in problems, the function does
  1707. // both in Pascal code
  1708. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1709. UpdateHistoryMenu(Side, Back);
  1710. }
  1711. return FHistoryMenu[Side == osLocal][Back];
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1715. TCustomDirView *Sender)
  1716. {
  1717. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1718. UpdateHistoryMenu(Side, true);
  1719. UpdateHistoryMenu(Side, false);
  1720. }
  1721. //---------------------------------------------------------------------------
  1722. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1723. {
  1724. // remote custom commands can be executed only if the server supports shell commands
  1725. // or have secondary shell
  1726. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1727. }
  1728. //---------------------------------------------------------------------------
  1729. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1730. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1731. {
  1732. // -1 = hidden, 0 = disabled, 1 = enabled
  1733. int Result;
  1734. TFileCustomCommand RemoteCustomCommand;
  1735. TLocalCustomCommand LocalCustomCommand;
  1736. TFileCustomCommand * NonInteractiveCustomCommand =
  1737. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1738. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1739. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1740. if (FLAGCLEAR(Command.Params, ccLocal))
  1741. {
  1742. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1743. // custom command that does not operate with files can be executed anytime ...
  1744. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1745. {
  1746. // ... but do not show such command in remote file menu
  1747. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1748. {
  1749. Result = AllowedState;
  1750. }
  1751. else
  1752. {
  1753. Result = -1;
  1754. }
  1755. }
  1756. else
  1757. {
  1758. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1759. {
  1760. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1761. }
  1762. else
  1763. {
  1764. Result = -1;
  1765. }
  1766. }
  1767. }
  1768. else
  1769. {
  1770. // custom command that does not operate with files can be executed anytime
  1771. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1772. {
  1773. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1774. }
  1775. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1776. {
  1777. if ((ListType == ccltAll) || (ListType == ccltFile))
  1778. {
  1779. // special case is "diff"-style command that can be executed over any side,
  1780. // if we have both sides
  1781. Result =
  1782. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1783. // directly (instead of EnableSelectedOperation) to pass
  1784. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1785. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1786. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1787. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1788. }
  1789. else if (ListType == ccltBoth)
  1790. {
  1791. DebugAssert(!IsLocalBrowserMode());
  1792. Result = 1;
  1793. }
  1794. else
  1795. {
  1796. Result = -1;
  1797. }
  1798. }
  1799. else
  1800. {
  1801. if ((ListType == ccltAll) ||
  1802. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1803. {
  1804. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1805. }
  1806. else
  1807. {
  1808. Result = -1;
  1809. }
  1810. }
  1811. if ((Result > 0) &&
  1812. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1813. !HasActiveTerminal())
  1814. {
  1815. Result = 0;
  1816. }
  1817. }
  1818. return Result;
  1819. }
  1820. //---------------------------------------------------------------------------
  1821. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1822. TStrings * FileList, const TCustomCommandType & ACommand,
  1823. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1824. {
  1825. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1826. {
  1827. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1828. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1829. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1830. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1831. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1832. bool Capture =
  1833. FLAGSET(ACommand.Params, ccShowResults) ||
  1834. FLAGSET(ACommand.Params, ccCopyResults) ||
  1835. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1836. TCaptureOutputEvent OutputEvent = NULL;
  1837. DebugAssert(FCapturedLog == NULL);
  1838. if (Capture)
  1839. {
  1840. FCapturedLog = new TStringList();
  1841. OutputEvent = TerminalCaptureLog;
  1842. }
  1843. try
  1844. {
  1845. if (!RemoteCustomCommand.IsFileCommand(Command))
  1846. {
  1847. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1848. OutputEvent);
  1849. }
  1850. else
  1851. {
  1852. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1853. }
  1854. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1855. {
  1856. if (FLAGSET(ACommand.Params, ccCopyResults))
  1857. {
  1858. CopyToClipboard(FCapturedLog);
  1859. }
  1860. if (FLAGSET(ACommand.Params, ccShowResults))
  1861. {
  1862. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1863. }
  1864. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1865. {
  1866. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1867. }
  1868. }
  1869. }
  1870. __finally
  1871. {
  1872. SAFE_DESTROY(FCapturedLog);
  1873. }
  1874. }
  1875. }
  1876. //---------------------------------------------------------------------------
  1877. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1878. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1879. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1880. {
  1881. DebugAssert(!IsLocalBrowserMode());
  1882. TStrings * LocalFileList = NULL;
  1883. TStrings * RemoteFileList = NULL;
  1884. TStrings * RemoteFileListFull = NULL;
  1885. try
  1886. {
  1887. if (LocalFileCommand)
  1888. {
  1889. if (ALocalFileList == NULL)
  1890. {
  1891. DebugAssert(HasDirView[osLocal]);
  1892. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1893. // directly (instead of EnableSelectedOperation) to pass
  1894. // false to FocusedFileOnlyWhenFocused
  1895. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1896. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1897. }
  1898. else
  1899. {
  1900. LocalFileList = ALocalFileList;
  1901. }
  1902. if (FileListCommand)
  1903. {
  1904. if (LocalFileList->Count != 1)
  1905. {
  1906. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1907. }
  1908. }
  1909. else
  1910. {
  1911. if ((LocalFileList->Count != 1) &&
  1912. (FileList->Count != 1) &&
  1913. (LocalFileList->Count != FileList->Count))
  1914. {
  1915. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1916. }
  1917. }
  1918. }
  1919. UnicodeString RootTempDir;
  1920. UnicodeString TempDir;
  1921. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1922. if (!RemoteFiles)
  1923. {
  1924. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1925. }
  1926. try
  1927. {
  1928. TDateTimes RemoteFileTimes;
  1929. if (RemoteFiles)
  1930. {
  1931. RemoteFileList = FileList;
  1932. }
  1933. else
  1934. {
  1935. RemoteFileList = new TStringList();
  1936. TMakeLocalFileListParams MakeFileListParam;
  1937. MakeFileListParam.FileList = RemoteFileList;
  1938. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1939. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1940. MakeFileListParam.Recursive =
  1941. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1942. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1943. if (!MakeFileListParam.Recursive)
  1944. {
  1945. RemoteFileTimes.clear();
  1946. RemoteFileListFull = new TStringList();
  1947. MakeFileListParam.FileList = RemoteFileListFull;
  1948. MakeFileListParam.Recursive = true;
  1949. MakeFileListParam.IncludeDirs = false;
  1950. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1951. }
  1952. else
  1953. {
  1954. RemoteFileListFull = RemoteFileList;
  1955. }
  1956. }
  1957. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1958. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1959. if (!NonBlocking)
  1960. {
  1961. int Count;
  1962. if (FileListCommand)
  1963. {
  1964. Count = 1;
  1965. }
  1966. else if (LocalFileCommand)
  1967. {
  1968. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1969. }
  1970. else
  1971. {
  1972. Count = RemoteFileList->Count;
  1973. }
  1974. Progress.Start(foCustomCommand, osRemote, Count);
  1975. DebugAssert(FProgressForm != NULL);
  1976. FProgressForm->ReadOnly = true;
  1977. }
  1978. try
  1979. {
  1980. if (FileListCommand)
  1981. {
  1982. UnicodeString LocalFile;
  1983. // MakeFileList does not delimit filenames
  1984. UnicodeString FileList = MakeFileList(RemoteFileList);
  1985. if (LocalFileCommand)
  1986. {
  1987. DebugAssert(LocalFileList->Count == 1);
  1988. LocalFile = LocalFileList->Strings[0];
  1989. }
  1990. TLocalCustomCommand CustomCommand(Data,
  1991. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1992. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1993. if (NonBlocking)
  1994. {
  1995. DebugAssert(!POutput);
  1996. ExecuteShellChecked(ShellCommand);
  1997. }
  1998. else
  1999. {
  2000. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2001. }
  2002. }
  2003. else if (LocalFileCommand)
  2004. {
  2005. if (LocalFileList->Count == 1)
  2006. {
  2007. UnicodeString LocalFile = LocalFileList->Strings[0];
  2008. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2009. {
  2010. UnicodeString FileName = RemoteFileList->Strings[Index];
  2011. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2012. Progress.SetFile(FileNameForProgress);
  2013. TLocalCustomCommand CustomCommand(Data,
  2014. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2015. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2016. TOnceDoneOperation OnceDoneOperation;
  2017. // Do not unselect anything, as it won't work with two panels anyway
  2018. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2019. }
  2020. }
  2021. else if (RemoteFileList->Count == 1)
  2022. {
  2023. UnicodeString FileName = RemoteFileList->Strings[0];
  2024. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2025. {
  2026. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2027. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2028. Progress.SetFile(LocalFileNameOnly);
  2029. TLocalCustomCommand CustomCommand(
  2030. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2031. FileName, LocalFileName, L"");
  2032. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2033. TOnceDoneOperation OnceDoneOperation;
  2034. // Do not unselect anything, as it won't work with two panels anyway
  2035. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2036. }
  2037. }
  2038. else
  2039. {
  2040. if (LocalFileList->Count != RemoteFileList->Count)
  2041. {
  2042. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2043. }
  2044. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2045. {
  2046. UnicodeString FileName = RemoteFileList->Strings[Index];
  2047. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2048. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2049. Progress.SetFile(LocalFileNameOnly); // sic
  2050. TLocalCustomCommand CustomCommand(
  2051. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2052. FileName, LocalFileName, L"");
  2053. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2054. TOnceDoneOperation OnceDoneOperation;
  2055. // Do not unselect anything, as it won't work with two panels anyway
  2056. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2057. }
  2058. }
  2059. }
  2060. else
  2061. {
  2062. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2063. {
  2064. UnicodeString FileName = RemoteFileList->Strings[Index];
  2065. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2066. Progress.SetFile(FileNameForProgress);
  2067. TLocalCustomCommand CustomCommand(Data,
  2068. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2069. FileName, L"", L"");
  2070. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2071. TOnceDoneOperation OnceDoneOperation;
  2072. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2073. }
  2074. }
  2075. }
  2076. __finally
  2077. {
  2078. if (!NonBlocking)
  2079. {
  2080. Progress.Stop();
  2081. }
  2082. }
  2083. DebugAssert(!FAutoOperation);
  2084. if (!RemoteFiles)
  2085. {
  2086. TempDir = IncludeTrailingBackslash(TempDir);
  2087. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2088. {
  2089. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2090. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2091. // Skip directories, process only nested files.
  2092. // The check is redundant as FileAge fails for directories anyway.
  2093. !DirectoryExists(FileName))
  2094. {
  2095. UnicodeString RemoteDir =
  2096. UnixExtractFileDir(
  2097. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2098. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2099. TDateTime NewTime;
  2100. if (FileAge(FileName, NewTime) &&
  2101. (NewTime != RemoteFileTimes[Index]))
  2102. {
  2103. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2104. TemporaryFileCopyParam(CopyParam);
  2105. CopyParam.FileMask = L"";
  2106. FAutoOperation = true;
  2107. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2108. TemporaryFilesList->Add(FileName);
  2109. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2110. }
  2111. }
  2112. }
  2113. }
  2114. }
  2115. __finally
  2116. {
  2117. FAutoOperation = false;
  2118. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2119. {
  2120. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2121. }
  2122. }
  2123. }
  2124. __finally
  2125. {
  2126. if (RemoteFileList != FileList)
  2127. {
  2128. delete RemoteFileList;
  2129. }
  2130. if (LocalFileList != ALocalFileList)
  2131. {
  2132. delete LocalFileList;
  2133. }
  2134. if (RemoteFileListFull != RemoteFileList)
  2135. {
  2136. delete RemoteFileListFull;
  2137. }
  2138. }
  2139. }
  2140. //---------------------------------------------------------------------------
  2141. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2142. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2143. bool FileListCommand, UnicodeString * POutput)
  2144. {
  2145. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2146. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2147. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2148. {
  2149. UnicodeString FileName = SelectedFileList->Strings[Index];
  2150. if (DirectoryExists(FileName))
  2151. {
  2152. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2153. {
  2154. LocalFileList->Add(FileName);
  2155. }
  2156. if (FLAGSET(ACommand.Params, ccRecursive))
  2157. {
  2158. TMakeLocalFileListParams MakeFileListParam;
  2159. MakeFileListParam.FileList = LocalFileList.get();
  2160. MakeFileListParam.FileTimes = NULL;
  2161. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2162. MakeFileListParam.Recursive = true;
  2163. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2164. }
  2165. }
  2166. else
  2167. {
  2168. LocalFileList->Add(FileName);
  2169. }
  2170. }
  2171. UnicodeString RemotePath;
  2172. if (Terminal != NULL)
  2173. {
  2174. RemotePath = Terminal->CurrentDirectory;
  2175. }
  2176. if (FileListCommand)
  2177. {
  2178. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2179. TLocalCustomCommand CustomCommand(
  2180. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2181. L"", L"", FileList);
  2182. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2183. }
  2184. else
  2185. {
  2186. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2187. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2188. DebugAssert(FProgressForm != NULL);
  2189. FProgressForm->ReadOnly = true;
  2190. try
  2191. {
  2192. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2193. {
  2194. UnicodeString FileName = LocalFileList->Strings[Index];
  2195. Progress.SetFile(FileName);
  2196. TLocalCustomCommand CustomCommand(
  2197. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2198. FileName, L"", L"");
  2199. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2200. TOnceDoneOperation OnceDoneOperation;
  2201. Progress.Finish(FileName, true, OnceDoneOperation);
  2202. }
  2203. }
  2204. __finally
  2205. {
  2206. Progress.Stop();
  2207. }
  2208. }
  2209. }
  2210. //---------------------------------------------------------------------------
  2211. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2212. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2213. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2214. {
  2215. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2216. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2217. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2218. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2219. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2220. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2221. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2222. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2223. std::unique_ptr<UnicodeString> POutput;
  2224. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2225. FLAGSET(ACommand.Params, ccCopyResults))
  2226. {
  2227. POutput.reset(new UnicodeString());
  2228. }
  2229. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2230. FProgressSide = FCurrentSide;
  2231. if (!LocalCustomCommand.IsFileCommand(Command))
  2232. {
  2233. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2234. }
  2235. // remote files?
  2236. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2237. {
  2238. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2239. }
  2240. // local files
  2241. else
  2242. {
  2243. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2244. }
  2245. if (POutput.get() != NULL)
  2246. {
  2247. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2248. int P = POutput->Pos(sLineBreak);
  2249. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2250. {
  2251. POutput->SetLength(P - 1);
  2252. }
  2253. // Copy even empty output to clipboard
  2254. if (FLAGSET(ACommand.Params, ccCopyResults))
  2255. {
  2256. CopyToClipboard(*POutput);
  2257. }
  2258. // But do not show an empty message box.
  2259. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2260. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2261. !POutput->IsEmpty())
  2262. {
  2263. TClipboardHandler ClipboardHandler;
  2264. ClipboardHandler.Text = *POutput;
  2265. TMessageParams Params;
  2266. TQueryButtonAlias Aliases[1];
  2267. Aliases[0].Button = qaRetry;
  2268. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2269. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2270. Params.Aliases = Aliases;
  2271. Params.AliasesCount = LENOF(Aliases);
  2272. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2273. }
  2274. }
  2275. }
  2276. //---------------------------------------------------------------------------
  2277. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2278. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2279. {
  2280. TCustomCommandData Data;
  2281. UnicodeString Site;
  2282. UnicodeString RemotePath;
  2283. if (HasActiveTerminal())
  2284. {
  2285. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2286. Data = TCustomCommandData(SessionData.get());
  2287. Site = Terminal->SessionData->SessionKey;
  2288. RemotePath = Terminal->CurrentDirectory;
  2289. }
  2290. UnicodeString HelpKeyword = ACommand.HomePage;
  2291. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2292. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2293. {
  2294. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2295. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2296. {
  2297. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2298. }
  2299. else
  2300. {
  2301. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2302. }
  2303. if (!DoCustomCommandOptionsDialog(
  2304. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2305. {
  2306. Abort();
  2307. }
  2308. }
  2309. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2310. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2311. {
  2312. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2313. }
  2314. else
  2315. {
  2316. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2317. }
  2318. }
  2319. //---------------------------------------------------------------------------
  2320. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2321. const TCustomCommandType & Command)
  2322. {
  2323. DebugAssert(FCustomCommandLocalFileList != NULL);
  2324. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2325. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2326. TStrings * LocalFileList = new TStringList();
  2327. TStrings * RemoteFileList = new TStringList();
  2328. try
  2329. {
  2330. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2331. {
  2332. LocalFileList->Clear();
  2333. LocalFileList->AddObject(
  2334. FCustomCommandLocalFileList->Strings[Index],
  2335. FCustomCommandLocalFileList->Objects[Index]);
  2336. RemoteFileList->Clear();
  2337. RemoteFileList->AddObject(
  2338. FCustomCommandRemoteFileList->Strings[Index],
  2339. FCustomCommandRemoteFileList->Objects[Index]);
  2340. CustomCommand(RemoteFileList, Command, LocalFileList);
  2341. }
  2342. }
  2343. __finally
  2344. {
  2345. delete LocalFileList;
  2346. delete RemoteFileList;
  2347. }
  2348. }
  2349. //---------------------------------------------------------------------------
  2350. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2351. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2352. {
  2353. delete FCustomCommandLocalFileList;
  2354. delete FCustomCommandRemoteFileList;
  2355. // takeover ownership,
  2356. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2357. FCustomCommandLocalFileList = LocalFileList;
  2358. FCustomCommandRemoteFileList = RemoteFileList;
  2359. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2360. if (Button != NULL)
  2361. {
  2362. FCustomCommandMenu->Items->Clear();
  2363. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2364. MenuPopup(FCustomCommandMenu, Button);
  2365. }
  2366. else
  2367. {
  2368. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2369. }
  2370. }
  2371. //---------------------------------------------------------------------------
  2372. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2373. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2374. {
  2375. DebugAssert(FCapturedLog != NULL);
  2376. if ((OutputType == cotOutput) || (OutputType == cotError))
  2377. {
  2378. FCapturedLog->Add(AddedLine);
  2379. }
  2380. }
  2381. //---------------------------------------------------------------------------
  2382. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2383. TOperationSide Side)
  2384. {
  2385. return
  2386. (Side == osOther) &&
  2387. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2388. }
  2389. //---------------------------------------------------------------------------
  2390. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2391. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2392. {
  2393. TTBItemOptions O;
  2394. O = Item->Options;
  2395. TCustomDirView * DView = DirView(Side);
  2396. if ((DView->ItemFocused != NULL) &&
  2397. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2398. // when resolving links is disabled, default action is to enter the directory,
  2399. // no matter what DoubleClickAction is configured to
  2400. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2401. // Can only Edit files, but can Open/Copy even directories
  2402. ((DoubleClickAction != dcaEdit) ||
  2403. !DView->ItemIsDirectory(DView->ItemFocused)))
  2404. {
  2405. Item->Options = O << tboDefault;
  2406. }
  2407. else
  2408. {
  2409. Item->Options = O >> tboDefault;
  2410. }
  2411. }
  2412. //---------------------------------------------------------------------------
  2413. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2414. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2415. {
  2416. TCustomDirView * DView = DirView(Side);
  2417. TListItem * Item = DView->ItemFocused;
  2418. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2419. EnableFocusedOperation[Side])
  2420. {
  2421. TPoint ClientPoint;
  2422. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2423. TPoint(0, 0) : MousePos;
  2424. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2425. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2426. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2427. }
  2428. }
  2429. //---------------------------------------------------------------------------
  2430. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2431. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2432. {
  2433. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2434. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2435. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2436. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2437. Handled = true;
  2438. }
  2439. //---------------------------------------------------------------------------
  2440. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2441. {
  2442. }
  2443. //---------------------------------------------------------------------------
  2444. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2445. {
  2446. DebugAssert(FErrorList == NULL);
  2447. if (WinConfiguration->ContinueOnError)
  2448. {
  2449. FErrorList = new TStringList();
  2450. Configuration->Usage->Inc(L"ContinuationsOnError");
  2451. }
  2452. NonVisualDataModule->StartBusy();
  2453. }
  2454. //---------------------------------------------------------------------------
  2455. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2456. {
  2457. NonVisualDataModule->EndBusy();
  2458. if (FErrorList)
  2459. {
  2460. HandleErrorList(FErrorList);
  2461. }
  2462. }
  2463. //---------------------------------------------------------------------------
  2464. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2465. const TCopyParamType & CopyParam)
  2466. {
  2467. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2468. {
  2469. Configuration->Usage->Inc(L"FileMaskUses");
  2470. }
  2471. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2472. {
  2473. Configuration->Usage->Inc(L"OperationMaskUses");
  2474. }
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2478. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2479. {
  2480. DebugAssert(!IsLocalBrowserMode());
  2481. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2482. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2483. TTransferOperationParam DefaultParam;
  2484. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2485. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2486. switch (Param.Queue)
  2487. {
  2488. case asOn:
  2489. CopyParam.Queue = true;
  2490. break;
  2491. case asOff:
  2492. CopyParam.Queue = false;
  2493. break;
  2494. case asAuto:
  2495. default:
  2496. // keep default
  2497. break;
  2498. }
  2499. bool Result =
  2500. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2501. if (Result)
  2502. {
  2503. DebugAssert(Terminal);
  2504. bool SelectionRestored = false;
  2505. TCustomDirView * DView = NULL;
  2506. if (HasDirView[Side])
  2507. {
  2508. DView = DirView(Side);
  2509. DView->SaveSelection();
  2510. DView->SaveSelectedNames();
  2511. }
  2512. UpdateCopyParamCounters(CopyParam);
  2513. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2514. DebugAssert(FTransferResumeList == NULL);
  2515. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2516. FMoveToQueue = false;
  2517. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2518. try
  2519. {
  2520. TStrings * PermanentFileList;
  2521. std::unique_ptr<TStrings> PermanentFileListOwner;
  2522. try
  2523. {
  2524. if (Side == osLocal)
  2525. {
  2526. PermanentFileList = FileList;
  2527. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2528. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2529. if (Operation == foMove)
  2530. {
  2531. ReloadLocalDirectory();
  2532. if (DView != NULL)
  2533. {
  2534. DView->RestoreSelection();
  2535. }
  2536. SelectionRestored = true;
  2537. }
  2538. }
  2539. else
  2540. {
  2541. // Clone the file list as it may refer to current directory files,
  2542. // which get destroyed, when the source directory is reloaded after foMove operation.
  2543. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2544. // But for a hotfix, we are not going to do such a big change.
  2545. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2546. PermanentFileList = PermanentFileListOwner.get();
  2547. try
  2548. {
  2549. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2550. }
  2551. __finally
  2552. {
  2553. if (Operation == foMove)
  2554. {
  2555. if (DView != NULL)
  2556. {
  2557. DView->RestoreSelection();
  2558. }
  2559. SelectionRestored = true;
  2560. }
  2561. ReloadLocalDirectory(Param.TargetDirectory);
  2562. }
  2563. }
  2564. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2565. if (FMoveToQueue)
  2566. {
  2567. Abort();
  2568. }
  2569. }
  2570. catch (EAbort &)
  2571. {
  2572. if (FMoveToQueue)
  2573. {
  2574. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2575. DebugAssert(CopyParam.TransferSkipList == NULL);
  2576. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2577. if (TransferResumeList->Count > 0)
  2578. {
  2579. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2580. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2581. }
  2582. CopyParam.TransferSkipList = TransferResumeList.release();
  2583. // not really needed, just to keep it consistent with TransferResumeList
  2584. FTransferResumeList = NULL;
  2585. FMoveToQueue = false;
  2586. Configuration->Usage->Inc("MovesToBackground");
  2587. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2588. ClearTransferSourceSelection(Direction);
  2589. }
  2590. throw;
  2591. }
  2592. }
  2593. __finally
  2594. {
  2595. if (!SelectionRestored && (DView != NULL))
  2596. {
  2597. DView->DiscardSavedSelection();
  2598. }
  2599. FTransferResumeList = NULL;
  2600. }
  2601. }
  2602. return Result;
  2603. }
  2604. //---------------------------------------------------------------------------
  2605. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2606. TOperationSide Side, TStrings * FileList, void * Param)
  2607. {
  2608. DebugAssert(FileList->Count);
  2609. // We deliberately do not toggle alternative flag (Param), but use OR,
  2610. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2611. // shortcut of CurrentDeleteAlternativeAction
  2612. bool Alternative =
  2613. bool(Param) || UseAlternativeFunction();
  2614. bool Recycle;
  2615. if (IsSideLocalBrowser(Side))
  2616. {
  2617. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2618. }
  2619. else
  2620. {
  2621. Recycle =
  2622. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2623. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2624. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2625. }
  2626. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2627. if (!Result)
  2628. {
  2629. UnicodeString Query;
  2630. if (FileList->Count == 1)
  2631. {
  2632. if (IsSideLocalBrowser(Side))
  2633. {
  2634. Query = ExtractFileName(FileList->Strings[0]);
  2635. }
  2636. else
  2637. {
  2638. Query = UnixExtractFileName(FileList->Strings[0]);
  2639. }
  2640. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2641. }
  2642. else
  2643. {
  2644. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2645. }
  2646. TMessageParams Params(mpNeverAskAgainCheck);
  2647. Params.ImageName = L"Delete file";
  2648. unsigned int Answer =
  2649. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2650. if (Answer == qaNeverAskAgain)
  2651. {
  2652. Result = true;
  2653. if (Recycle)
  2654. {
  2655. WinConfiguration->ConfirmRecycling = false;
  2656. }
  2657. else
  2658. {
  2659. WinConfiguration->ConfirmDeleting = false;
  2660. }
  2661. }
  2662. else
  2663. {
  2664. Result = (Answer == qaOK);
  2665. }
  2666. }
  2667. if (Result)
  2668. {
  2669. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2670. }
  2671. return Result;
  2672. }
  2673. //---------------------------------------------------------------------------
  2674. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2675. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2676. {
  2677. TAutoBatch AutoBatch(this);
  2678. bool Result;
  2679. if ((Operation == foCopy) || (Operation == foMove))
  2680. {
  2681. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2682. }
  2683. else if (Operation == foRename)
  2684. {
  2685. DebugAssert(DirView(Side)->ItemFocused);
  2686. DirView(Side)->ItemFocused->EditCaption();
  2687. Result = true;
  2688. }
  2689. else if (Operation == foDelete)
  2690. {
  2691. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2692. }
  2693. else if (Operation == foSetProperties)
  2694. {
  2695. DirView(osRemote)->SaveSelectedNames();
  2696. Result = SetProperties(Side, FileList);
  2697. }
  2698. else if (Operation == foCustomCommand)
  2699. {
  2700. DebugAssert(Param);
  2701. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2702. DirView(osRemote)->SaveSelectedNames();
  2703. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2704. CustomCommand(FileList, *Command, NULL);
  2705. Result = true;
  2706. }
  2707. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2708. {
  2709. DebugAssert(!IsSideLocalBrowser(Side));
  2710. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2711. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2712. }
  2713. else if (Operation == foLock)
  2714. {
  2715. DebugAssert(!IsSideLocalBrowser(Side));
  2716. LockFiles(FileList, true);
  2717. Result = true;
  2718. }
  2719. else if (Operation == foUnlock)
  2720. {
  2721. DebugAssert(!IsSideLocalBrowser(Side));
  2722. LockFiles(FileList, false);
  2723. Result = true;
  2724. }
  2725. else
  2726. {
  2727. DebugFail();
  2728. }
  2729. return Result;
  2730. }
  2731. //---------------------------------------------------------------------------
  2732. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2733. {
  2734. if (Side == osCurrent)
  2735. {
  2736. Side = FCurrentSide;
  2737. }
  2738. return Side;
  2739. }
  2740. //---------------------------------------------------------------------------
  2741. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2742. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2743. {
  2744. Side = GetSide(Side);
  2745. bool Result;
  2746. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2747. try
  2748. {
  2749. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2750. }
  2751. __finally
  2752. {
  2753. delete FileList;
  2754. }
  2755. return Result;
  2756. }
  2757. //---------------------------------------------------------------------------
  2758. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2759. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2760. bool NoConfirmation, void * Param)
  2761. {
  2762. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2763. {
  2764. if ((Operation == foCopy) || (Operation == foMove))
  2765. {
  2766. DebugAssert(!IsLocalBrowserMode());
  2767. if (GetSide(Side) == osLocal)
  2768. {
  2769. Configuration->Usage->Inc(L"UploadsCommand");
  2770. }
  2771. else
  2772. {
  2773. Configuration->Usage->Inc(L"DownloadsCommand");
  2774. }
  2775. }
  2776. }
  2777. }
  2778. //---------------------------------------------------------------------------
  2779. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2780. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2781. {
  2782. TTransferOperationParam Param;
  2783. if ((WinConfiguration->Interface != ifCommander) ||
  2784. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2785. {
  2786. Flags &= ~cocShortCutHint;
  2787. }
  2788. TCustomDirView * DView = DirView(Side);
  2789. Param.Options =
  2790. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2791. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2792. if (FLAGSET(Flags, cocQueue))
  2793. {
  2794. Param.Queue = asOn;
  2795. }
  2796. else if (FLAGSET(Flags, cocNonQueue))
  2797. {
  2798. Param.Queue = asOff;
  2799. }
  2800. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2801. }
  2802. //---------------------------------------------------------------------------
  2803. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2804. {
  2805. int Count = ErrorList->Count;
  2806. try
  2807. {
  2808. if (ErrorList->Count)
  2809. {
  2810. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2811. if (MessageDialog(Message, qtError,
  2812. qaOK | qaCancel, HELP_NONE) == qaOK)
  2813. {
  2814. unsigned int Answer;
  2815. int Index = 0;
  2816. do
  2817. {
  2818. DebugAssert(Index >= 0 && Index < Count);
  2819. TQueryButtonAlias Aliases[2];
  2820. Aliases[0].Button = qaYes;
  2821. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2822. Aliases[1].Button = qaNo;
  2823. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2824. TMessageParams Params;
  2825. Params.Aliases = Aliases;
  2826. Params.AliasesCount = LENOF(Aliases);
  2827. int No = Index + 1;
  2828. UnicodeString Message = ErrorList->Strings[Index];
  2829. UnicodeString AMainInstructions;
  2830. UnicodeString Details;
  2831. if (ExtractMainInstructions(Message, AMainInstructions))
  2832. {
  2833. Details = Message;
  2834. Message = AMainInstructions;
  2835. }
  2836. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2837. Answer = MoreMessageDialog(
  2838. Message,
  2839. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2840. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2841. qaOK, HELP_NONE, &Params);
  2842. if (Answer == qaNo)
  2843. {
  2844. Index++;
  2845. }
  2846. if (Answer == qaYes)
  2847. {
  2848. Index--;
  2849. }
  2850. }
  2851. while (Answer != qaOK);
  2852. }
  2853. }
  2854. }
  2855. __finally
  2856. {
  2857. TStrings * List = ErrorList;
  2858. ErrorList = NULL;
  2859. for (int i = 0; i < Count; i++)
  2860. {
  2861. delete List->Objects[i];
  2862. }
  2863. delete List;
  2864. }
  2865. }
  2866. //---------------------------------------------------------------------------
  2867. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2868. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2869. {
  2870. // needed for checking filemasks, as there's no directory object
  2871. // associated with the file object
  2872. File->FullFileName = FullFileName;
  2873. TFileMasks::TParams MaskParams;
  2874. MaskParams.Size = File->Size;
  2875. MaskParams.Modification = File->Modification;
  2876. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2877. }
  2878. //---------------------------------------------------------------------------
  2879. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2880. {
  2881. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2882. Side = GetSide(Side);
  2883. TCustomDirView * DView = DirView(Side);
  2884. TListItem * FocusedItem = DView->ItemFocused;
  2885. UnicodeString Name;
  2886. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2887. {
  2888. Name = DView->ItemFileName(FocusedItem);
  2889. }
  2890. else
  2891. {
  2892. Name = LoadStr(NEW_FILE);
  2893. }
  2894. UnicodeString Names = Name;
  2895. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2896. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2897. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2898. {
  2899. while (!Names.IsEmpty())
  2900. {
  2901. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2902. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2903. UnicodeString TargetFileName;
  2904. UnicodeString LocalFileName;
  2905. UnicodeString RootTempDir;
  2906. UnicodeString TempDir;
  2907. UnicodeString RemoteDirectory;
  2908. bool ExistingFile = false;
  2909. if (!IsSideLocalBrowser(Side))
  2910. {
  2911. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2912. TRemoteFile * File = NULL;
  2913. if (Terminal->FileExists(Name, &File) &&
  2914. !File->IsDirectory)
  2915. {
  2916. try
  2917. {
  2918. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2919. ExistingFile = true;
  2920. }
  2921. __finally
  2922. {
  2923. delete File;
  2924. }
  2925. }
  2926. if (!ExistingFile)
  2927. {
  2928. RemoteDirectory = UnixExtractFilePath(Name);
  2929. TemporaryDirectoryForRemoteFiles(
  2930. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2931. TargetFileName = UnixExtractFileName(Name);
  2932. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2933. LocalFileName = TempDir +
  2934. // We probably do not want to trim the VMS version here
  2935. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2936. }
  2937. }
  2938. else
  2939. {
  2940. if (ExtractFilePath(Name).IsEmpty())
  2941. {
  2942. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2943. }
  2944. else
  2945. {
  2946. LocalFileName = ExpandFileName(Name);
  2947. }
  2948. TargetFileName = ExtractFileName(Name);
  2949. }
  2950. if (!ExistingFile)
  2951. {
  2952. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2953. if (NewFile)
  2954. {
  2955. int File = FileCreate(ApiPath(LocalFileName));
  2956. if (File < 0)
  2957. {
  2958. if (!RootTempDir.IsEmpty())
  2959. {
  2960. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2961. }
  2962. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2963. }
  2964. else
  2965. {
  2966. FileClose(File);
  2967. }
  2968. }
  2969. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2970. const TEditorData * ExternalEditor = NULL;
  2971. TFileMasks::TParams MaskParams; // size not known
  2972. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2973. false, MaskParams);
  2974. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2975. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2976. }
  2977. }
  2978. }
  2979. }
  2980. //---------------------------------------------------------------------------
  2981. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2982. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2983. {
  2984. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2985. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2986. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2987. return
  2988. ((ExecuteFileBy == efInternalEditor)) ||
  2989. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2990. }
  2991. //---------------------------------------------------------------------------
  2992. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2993. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2994. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2995. UnicodeString RemoteDirectory, bool NewFile)
  2996. {
  2997. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2998. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2999. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3000. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3001. Side = GetSide(Side);
  3002. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3003. if (!IsSideLocalBrowser(Side))
  3004. {
  3005. Data->Terminal = Terminal;
  3006. Data->Queue = Queue;
  3007. Data->SessionData = CloneCurrentSessionData();
  3008. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3009. Data->RemoteDirectory = RemoteDirectory;
  3010. Data->SessionName = Terminal->SessionData->SessionName;
  3011. Data->LocalRootDirectory = LocalRootDirectory;
  3012. Data->OriginalFileName = OriginalFileName;
  3013. Data->Command = L""; // will be changed later for external editor
  3014. }
  3015. if (ExecuteFileBy == efInternalEditor)
  3016. {
  3017. if (!IsSideLocalBrowser(Side))
  3018. {
  3019. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3020. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3021. TForm * Editor;
  3022. try
  3023. {
  3024. TNotifyEvent OnFileChanged = NULL;
  3025. TNotifyEvent OnSaveAll = NULL;
  3026. TAnyModifiedEvent OnAnyModified = NULL;
  3027. // Edited files are uploaded in background queue, what we do not support with encrypted files
  3028. if (Terminal->IsCapable[fcBackgroundTransfers])
  3029. {
  3030. OnFileChanged = FEditorManager->FileChanged;
  3031. OnSaveAll = SaveAllInternalEditors;
  3032. OnAnyModified = AnyInternalEditorModified;
  3033. }
  3034. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  3035. FEditorManager->FileReload, FEditorManager->FileClosed,
  3036. OnSaveAll, OnAnyModified,
  3037. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3038. }
  3039. catch(...)
  3040. {
  3041. if (!LocalRootDirectory.IsEmpty())
  3042. {
  3043. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3044. }
  3045. throw;
  3046. }
  3047. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3048. }
  3049. else
  3050. {
  3051. DebugAssert(!StandaloneOperation);
  3052. TForm * Editor =
  3053. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3054. SaveAllInternalEditors, AnyInternalEditorModified,
  3055. L"", false, SessionColor, -1, NewFile);
  3056. FLocalEditors->Add(Editor);
  3057. }
  3058. }
  3059. else
  3060. {
  3061. HANDLE Process;
  3062. if (ExecuteFileBy == efExternalEditor)
  3063. {
  3064. UnicodeString Program, Params, Dir;
  3065. Data->Command = ExternalEditor->ExternalEditor;
  3066. ReformatFileNameCommand(Data->Command);
  3067. SplitCommand(Data->Command, Program, Params, Dir);
  3068. Params = ExpandFileNameCommand(Params, FileName);
  3069. Program = ExpandEnvironmentVariables(Program);
  3070. if (!ExecuteShell(Program, Params, Process))
  3071. {
  3072. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3073. }
  3074. }
  3075. else
  3076. {
  3077. DebugAssert(!IsSideLocalBrowser(Side));
  3078. if (!ExecuteShell(FileName, L"", Process))
  3079. {
  3080. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3081. }
  3082. }
  3083. if (IsSideLocalBrowser(Side) ||
  3084. ((ExecuteFileBy == efShell) &&
  3085. !WinConfiguration->Editor.SDIShellEditor) ||
  3086. ((ExecuteFileBy == efExternalEditor) &&
  3087. !ExternalEditor->SDIExternalEditor))
  3088. {
  3089. // no need for handle
  3090. if (Process != NULL)
  3091. {
  3092. DebugCheck(CloseHandle(Process));
  3093. }
  3094. Process = INVALID_HANDLE_VALUE;
  3095. }
  3096. else
  3097. {
  3098. if (Process == NULL)
  3099. {
  3100. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3101. }
  3102. }
  3103. if (!IsSideLocalBrowser(Side))
  3104. {
  3105. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3106. }
  3107. }
  3108. }
  3109. //---------------------------------------------------------------------------
  3110. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3111. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3112. void * /*Arg*/)
  3113. {
  3114. if (Token != NULL)
  3115. {
  3116. EditorFormFileSave(static_cast<TForm *>(Token));
  3117. }
  3118. }
  3119. //---------------------------------------------------------------------------
  3120. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3121. {
  3122. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3123. {
  3124. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3125. }
  3126. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3127. }
  3128. //---------------------------------------------------------------------------
  3129. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3130. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3131. void * Arg)
  3132. {
  3133. if ((Token != NULL) &&
  3134. IsEditorFormModified(static_cast<TForm *>(Token)))
  3135. {
  3136. *static_cast<bool *>(Arg) = true;
  3137. }
  3138. }
  3139. //---------------------------------------------------------------------------
  3140. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3141. TObject * /*Sender*/, bool & Modified)
  3142. {
  3143. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3144. {
  3145. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3146. {
  3147. Modified = true;
  3148. }
  3149. }
  3150. if (!Modified)
  3151. {
  3152. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3153. }
  3154. }
  3155. //---------------------------------------------------------------------------
  3156. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3157. {
  3158. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3159. }
  3160. //---------------------------------------------------------------------------
  3161. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3162. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3163. UnicodeString & Result, UnicodeString & RootDirectory)
  3164. {
  3165. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3166. {
  3167. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3168. Result = RootDirectory;
  3169. }
  3170. else
  3171. {
  3172. RootDirectory = L"";
  3173. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3174. Result = IncludeTrailingBackslash(Result);
  3175. }
  3176. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3177. {
  3178. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3179. }
  3180. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3181. {
  3182. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3183. {
  3184. RemoteDirectory.Delete(1, 1);
  3185. }
  3186. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3187. }
  3188. if (!ForceDirectories(ApiPath(Result)))
  3189. {
  3190. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3191. }
  3192. }
  3193. //---------------------------------------------------------------------------
  3194. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3195. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3196. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3197. {
  3198. DebugAssert(!IsLocalBrowserMode());
  3199. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3200. if (ForceText)
  3201. {
  3202. CopyParam.TransferMode = tmAscii;
  3203. }
  3204. TemporaryFileCopyParam(CopyParam);
  3205. if (AllFiles)
  3206. {
  3207. CopyParam.IncludeFileMask = TFileMasks();
  3208. }
  3209. if (TempDir.IsEmpty())
  3210. {
  3211. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3212. }
  3213. DebugAssert(!FAutoOperation);
  3214. FAutoOperation = AutoOperation;
  3215. Terminal->ExceptionOnFail = true;
  3216. try
  3217. {
  3218. try
  3219. {
  3220. // turn off confirmations, as for MDI editors we may possibly download
  3221. // the same file over
  3222. Terminal->CopyToLocal(
  3223. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3224. if (GetTargetNames)
  3225. {
  3226. for (int i = 0; i < FileList->Count; i++)
  3227. {
  3228. FileList->Strings[i] =
  3229. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3230. }
  3231. }
  3232. }
  3233. catch(...)
  3234. {
  3235. if (!RootTempDir.IsEmpty())
  3236. {
  3237. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3238. }
  3239. throw;
  3240. }
  3241. }
  3242. __finally
  3243. {
  3244. FAutoOperation = false;
  3245. Terminal->ExceptionOnFail = false;
  3246. }
  3247. }
  3248. //---------------------------------------------------------------------------
  3249. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3250. {
  3251. // Do not waste time checking for default editor list the next time,
  3252. // or testing if default editor is not notepad.
  3253. bool TryNextTime = false;
  3254. UnicodeString UsageState;
  3255. if (!WinConfiguration->EditorList->IsDefaultList())
  3256. {
  3257. UsageState = "H";
  3258. }
  3259. else
  3260. {
  3261. UnicodeString Executable;
  3262. UnicodeString ExecutableDescription;
  3263. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3264. {
  3265. UnicodeString Message =
  3266. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3267. unsigned int Answer =
  3268. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3269. if (Answer != qaOK)
  3270. {
  3271. UsageState = "R";
  3272. }
  3273. else
  3274. {
  3275. UsageState = "A";
  3276. TEditorData EditorData;
  3277. EditorData.Editor = edExternal;
  3278. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3279. EditorData.ExternalEditorOptionsAutodetect();
  3280. TEditorList EditorList;
  3281. EditorList = *WinConfiguration->EditorList;
  3282. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3283. WinConfiguration->EditorList = &EditorList;
  3284. }
  3285. }
  3286. }
  3287. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3288. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3289. }
  3290. //---------------------------------------------------------------------------
  3291. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3292. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3293. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3294. {
  3295. if (ExecuteFileBy == efDefaultEditor)
  3296. {
  3297. if (!WinConfiguration->OfferedEditorAutoConfig)
  3298. {
  3299. EditorAutoConfig();
  3300. }
  3301. const TEditorPreferences * Editor =
  3302. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3303. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3304. {
  3305. ExecuteFileBy = efInternalEditor;
  3306. ExternalEditor = NULL;
  3307. }
  3308. else if (Editor->Data->Editor == edOpen)
  3309. {
  3310. ExecuteFileBy = efShell;
  3311. ExternalEditor = NULL;
  3312. }
  3313. else
  3314. {
  3315. ExecuteFileBy = efExternalEditor;
  3316. ExternalEditor = Editor->Data;
  3317. }
  3318. }
  3319. }
  3320. //---------------------------------------------------------------------------
  3321. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3322. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3323. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3324. {
  3325. UnicodeString OriginalFileName;
  3326. UnicodeString LocalRootDirectory;
  3327. UnicodeString RemoteDirectory;
  3328. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3329. IsSideLocalBrowser(Side), MaskParams);
  3330. UnicodeString Counter;
  3331. UnicodeString LocalFileName;
  3332. bool Handled = false;
  3333. if (!IsSideLocalBrowser(Side))
  3334. {
  3335. // We need to trim VMS version here, so that we use name without version
  3336. // when uploading back to create a new version of the file
  3337. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3338. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3339. TObject * Token = NULL;
  3340. UnicodeString LocalDirectory;
  3341. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3342. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3343. LocalDirectory))
  3344. {
  3345. if (Token != NULL)
  3346. {
  3347. TForm * Form = dynamic_cast<TForm *>(Token);
  3348. if (Form->WindowState == wsMinimized)
  3349. {
  3350. ShowWindow(Form->Handle, SW_RESTORE);
  3351. }
  3352. else
  3353. {
  3354. Form->SetFocus();
  3355. }
  3356. Handled = true;
  3357. }
  3358. else
  3359. {
  3360. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3361. }
  3362. }
  3363. if (!Handled)
  3364. {
  3365. TStringList * FileList1 = new TStringList();
  3366. try
  3367. {
  3368. FileList1->AddObject(FullFileName, Object);
  3369. TemporarilyDownloadFiles(FileList1,
  3370. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3371. LocalRootDirectory, LocalDirectory, true, true, true);
  3372. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3373. }
  3374. __finally
  3375. {
  3376. delete FileList1;
  3377. }
  3378. switch (ExecuteFileBy)
  3379. {
  3380. case efShell:
  3381. Counter = "RemoteFilesExecuted";
  3382. break;
  3383. case efInternalEditor:
  3384. Counter = "RemoteFilesOpenedInInternalEditor";
  3385. break;
  3386. case efExternalEditor:
  3387. Counter = "RemoteFilesOpenedInExternalEditor";
  3388. break;
  3389. default:
  3390. DebugFail();
  3391. }
  3392. }
  3393. }
  3394. else
  3395. {
  3396. LocalFileName = FullFileName;
  3397. OriginalFileName = ExtractFileName(FullFileName);
  3398. switch (ExecuteFileBy)
  3399. {
  3400. case efShell:
  3401. Counter = "LocalFilesExecuted";
  3402. break;
  3403. case efInternalEditor:
  3404. Counter = "LocalFilesOpenedInInternalEditor";
  3405. break;
  3406. case efExternalEditor:
  3407. Counter = "LocalFilesOpenedInExternalEditor";
  3408. break;
  3409. default:
  3410. DebugFail();
  3411. }
  3412. }
  3413. if (!Handled)
  3414. {
  3415. Configuration->Usage->Inc(Counter);
  3416. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3417. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3418. }
  3419. }
  3420. //---------------------------------------------------------------------------
  3421. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3422. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3423. bool AllSelected, bool OnFocused)
  3424. {
  3425. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3426. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3427. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3428. Side = GetSide(Side);
  3429. TCustomDirView * DView = DirView(Side);
  3430. TStrings * FileList =
  3431. AllSelected ?
  3432. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3433. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3434. try
  3435. {
  3436. DebugAssert(AllSelected || (FileList->Count == 1));
  3437. for (int i = 0; i < FileList->Count; i++)
  3438. {
  3439. UnicodeString ListFileName = FileList->Strings[i];
  3440. UnicodeString FileNameOnly =
  3441. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3442. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3443. if (!DView->ItemIsDirectory(Item))
  3444. {
  3445. UnicodeString FullFileName;
  3446. if (!IsSideLocalBrowser(Side))
  3447. {
  3448. FullFileName = DirView(Side)->Path + ListFileName;
  3449. }
  3450. else
  3451. {
  3452. FullFileName = ListFileName;
  3453. }
  3454. TFileMasks::TParams MaskParams;
  3455. MaskParams.Size = DView->ItemFileSize(Item);
  3456. TDateTimePrecision Precision;
  3457. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3458. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3459. FileList->Objects[i], MaskParams);
  3460. }
  3461. }
  3462. }
  3463. __finally
  3464. {
  3465. delete FileList;
  3466. }
  3467. }
  3468. //---------------------------------------------------------------------------
  3469. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3470. {
  3471. CopyParam.FileNameCase = ncNoChange;
  3472. CopyParam.PreserveRights = false;
  3473. CopyParam.PreserveReadOnly = false;
  3474. CopyParam.ReplaceInvalidChars = true;
  3475. CopyParam.IncludeFileMask = TFileMasks();
  3476. CopyParam.NewerOnly = false;
  3477. CopyParam.FileMask = L"";
  3478. }
  3479. //---------------------------------------------------------------------------
  3480. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3481. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3482. {
  3483. TTerminalManager * Manager = TTerminalManager::Instance();
  3484. if (!IsActiveTerminal(Data->Terminal))
  3485. {
  3486. if (!NonVisualDataModule->Busy)
  3487. {
  3488. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3489. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3490. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3491. if (Data->Terminal == NULL)
  3492. {
  3493. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3494. if (SameSiteTerminal != NULL)
  3495. {
  3496. UnicodeString Message =
  3497. FMTLOAD(EDIT_SESSION_REATTACH,
  3498. (FileNameOnly, Data->SessionName, FileNameOnly));
  3499. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3500. {
  3501. Data->Terminal = Terminal;
  3502. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3503. Data->SessionName = Terminal->SessionData->SessionName;
  3504. // We might also overwrite session data
  3505. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3506. }
  3507. else
  3508. {
  3509. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3510. Abort();
  3511. }
  3512. }
  3513. }
  3514. // foreground session should reconnect itself
  3515. else if (Terminal != Data->Terminal)
  3516. {
  3517. UnicodeString Message =
  3518. MainInstructions(
  3519. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3520. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3521. {
  3522. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3523. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3524. }
  3525. else
  3526. {
  3527. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3528. Abort();
  3529. }
  3530. }
  3531. }
  3532. if (!IsActiveTerminal(Data->Terminal))
  3533. {
  3534. Configuration->Usage->Inc(L"EditInactiveSession");
  3535. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3536. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3537. (ExtractFileName(FileName), Data->SessionName)));
  3538. }
  3539. }
  3540. TStrings * FileList = new TStringList();
  3541. try
  3542. {
  3543. FileList->Add(FileName);
  3544. // consider using the same settings (preset) as when the file was downloaded
  3545. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3546. TemporaryFileCopyParam(CopyParam);
  3547. if (Data->ForceText)
  3548. {
  3549. CopyParam.TransferMode = tmAscii;
  3550. }
  3551. // so i do not need to worry if masking algorithm works in all cases
  3552. // ("" means "copy file name", no masking is actually done)
  3553. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3554. {
  3555. CopyParam.FileMask = L"";
  3556. }
  3557. else
  3558. {
  3559. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3560. }
  3561. DebugAssert(Data->Queue != NULL);
  3562. int Params = cpNoConfirmation | cpTemporary;
  3563. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3564. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3565. QueueItem->CompleteEvent = UploadCompleteEvent;
  3566. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3567. }
  3568. __finally
  3569. {
  3570. delete FileList;
  3571. }
  3572. }
  3573. //---------------------------------------------------------------------------
  3574. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3575. const UnicodeString FileName, const TEditedFileData * Data)
  3576. {
  3577. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3578. DebugAssert(!NonVisualDataModule->Busy);
  3579. if (!IsActiveTerminal(Data->Terminal))
  3580. {
  3581. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3582. (ExtractFileName(FileName), Data->SessionName)));
  3583. }
  3584. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3585. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3586. NonVisualDataModule->StartBusy();
  3587. try
  3588. {
  3589. std::unique_ptr<TRemoteFile> File;
  3590. UnicodeString RemoteFileName =
  3591. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3592. Terminal->ExceptionOnFail = true;
  3593. try
  3594. {
  3595. TRemoteFile * AFile = NULL;
  3596. Terminal->ReadFile(RemoteFileName, AFile);
  3597. File.reset(AFile);
  3598. if (!File->HaveFullFileName)
  3599. {
  3600. File->FullFileName = RemoteFileName;
  3601. }
  3602. }
  3603. __finally
  3604. {
  3605. Terminal->ExceptionOnFail = false;
  3606. }
  3607. std::unique_ptr<TStrings> FileList(new TStringList());
  3608. FileList->AddObject(RemoteFileName, File.get());
  3609. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3610. UnicodeString TempDir = ExtractFilePath(FileName);
  3611. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3612. TempDir, true, true, true);
  3613. // sanity check, the target file name should be still the same
  3614. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3615. }
  3616. __finally
  3617. {
  3618. NonVisualDataModule->EndBusy();
  3619. // it actually may not exist anymore...
  3620. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3621. }
  3622. }
  3623. //---------------------------------------------------------------------------
  3624. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3625. const TEditedFileData * Data, bool & KeepOpen)
  3626. {
  3627. // Command is set for external editors only (not for "shell" open).
  3628. if (!Data->Command.IsEmpty())
  3629. {
  3630. bool AnyFound = false;
  3631. bool AnyMDI = false;
  3632. bool AnyNonMDI = false;
  3633. bool AnyDetect = false;
  3634. TEditorList * EditorList = new TEditorList();
  3635. try
  3636. {
  3637. *EditorList = *WinConfiguration->EditorList;
  3638. for (int i = 0; i < EditorList->Count; i++)
  3639. {
  3640. const TEditorPreferences * Editor = EditorList->Editors[i];
  3641. if ((Editor->Data->Editor == edExternal) &&
  3642. (Editor->Data->ExternalEditor == Data->Command))
  3643. {
  3644. AnyFound = true;
  3645. if (Editor->Data->SDIExternalEditor)
  3646. {
  3647. AnyNonMDI = true;
  3648. if (Editor->Data->DetectMDIExternalEditor)
  3649. {
  3650. AnyDetect = true;
  3651. }
  3652. }
  3653. else
  3654. {
  3655. AnyMDI = true;
  3656. }
  3657. }
  3658. }
  3659. bool EnableMDI = false;
  3660. bool DisableDetect = false;
  3661. if (AnyMDI)
  3662. {
  3663. KeepOpen = true;
  3664. if (AnyNonMDI)
  3665. {
  3666. // there is at least one instance of the editor with MDI support enabled,
  3667. // and one with disabled, enable it for all instances
  3668. EnableMDI = true;
  3669. }
  3670. }
  3671. else if (AnyFound && !AnyDetect)
  3672. {
  3673. // at least once instance found but all have MDI autodetection disabled
  3674. // => close the file (default action)
  3675. }
  3676. else
  3677. {
  3678. // no instance of the editor has MDI support enabled
  3679. TMessageParams Params;
  3680. if (AnyFound)
  3681. {
  3682. // there is at least one instance of the editor
  3683. Params.Params |= mpNeverAskAgainCheck;
  3684. }
  3685. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3686. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3687. switch (Answer)
  3688. {
  3689. case qaNeverAskAgain:
  3690. DisableDetect = true;
  3691. break;
  3692. case qaNo:
  3693. EnableMDI = true;
  3694. KeepOpen = true;
  3695. break;
  3696. }
  3697. }
  3698. if (AnyFound && (EnableMDI || DisableDetect))
  3699. {
  3700. bool Changed = false;
  3701. for (int i = 0; i < EditorList->Count; i++)
  3702. {
  3703. const TEditorPreferences * Editor = EditorList->Editors[i];
  3704. if ((Editor->Data->Editor == edExternal) &&
  3705. (Editor->Data->ExternalEditor == Data->Command) &&
  3706. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3707. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3708. {
  3709. Changed = true;
  3710. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3711. if (EnableMDI)
  3712. {
  3713. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3714. }
  3715. if (DisableDetect)
  3716. {
  3717. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3718. }
  3719. EditorList->Change(i, UpdatedEditor);
  3720. }
  3721. }
  3722. if (Changed)
  3723. {
  3724. WinConfiguration->EditorList = EditorList;
  3725. }
  3726. }
  3727. }
  3728. __finally
  3729. {
  3730. delete EditorList;
  3731. }
  3732. }
  3733. else
  3734. {
  3735. // "open" case
  3736. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3737. qaOK, HELP_APP_EARLY_CLOSED);
  3738. }
  3739. }
  3740. //---------------------------------------------------------------------------
  3741. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3742. {
  3743. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3744. }
  3745. //---------------------------------------------------------------------------
  3746. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3747. {
  3748. SideEnter(osRemote);
  3749. }
  3750. //---------------------------------------------------------------------------
  3751. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3752. {
  3753. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3754. SideEnter(osRemote);
  3755. }
  3756. //---------------------------------------------------------------------------
  3757. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3758. {
  3759. FCurrentSide = Side;
  3760. if (Visible)
  3761. {
  3762. UpdateControls();
  3763. }
  3764. }
  3765. //---------------------------------------------------------------------------
  3766. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3767. TStrings * FileList, bool Alternative)
  3768. {
  3769. TCustomDirView * DView = DirView(Side);
  3770. DView->SaveSelection();
  3771. DView->SaveSelectedNames();
  3772. DebugAssert(!FAlternativeDelete);
  3773. FAlternativeDelete = Alternative;
  3774. try
  3775. {
  3776. if (!IsSideLocalBrowser(Side))
  3777. {
  3778. DebugAssert(Terminal != NULL);
  3779. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3780. }
  3781. else
  3782. {
  3783. if (IsLocalBrowserMode())
  3784. {
  3785. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3786. }
  3787. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3788. FProgressSide = FCurrentSide;
  3789. try
  3790. {
  3791. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3792. }
  3793. __finally
  3794. {
  3795. ReloadLocalDirectory();
  3796. }
  3797. }
  3798. FAlternativeDelete = false;
  3799. }
  3800. catch(...)
  3801. {
  3802. FAlternativeDelete = false;
  3803. DView->DiscardSavedSelection();
  3804. throw;
  3805. }
  3806. DView->RestoreSelection();
  3807. }
  3808. //---------------------------------------------------------------------------
  3809. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3810. {
  3811. DebugAssert(Terminal);
  3812. DebugAssert(!IsLocalBrowserMode());
  3813. RemoteDirView->SaveSelection();
  3814. RemoteDirView->SaveSelectedNames();
  3815. try
  3816. {
  3817. if (Lock)
  3818. {
  3819. Terminal->LockFiles(FileList);
  3820. }
  3821. else
  3822. {
  3823. Terminal->UnlockFiles(FileList);
  3824. }
  3825. }
  3826. catch(...)
  3827. {
  3828. RemoteDirView->DiscardSavedSelection();
  3829. throw;
  3830. }
  3831. RemoteDirView->RestoreSelection();
  3832. }
  3833. //---------------------------------------------------------------------------
  3834. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3835. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3836. bool NoConfirmation, bool Move)
  3837. {
  3838. DebugAssert(Terminal != NULL);
  3839. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3840. DebugAssert(!IsLocalBrowserMode());
  3841. if (Session == NULL)
  3842. {
  3843. Session = Terminal;
  3844. }
  3845. if (Session == Terminal)
  3846. {
  3847. if (RemoteDriveView->DropTarget != NULL)
  3848. {
  3849. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3850. }
  3851. else if (RemoteDirView->DropTarget != NULL)
  3852. {
  3853. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3854. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3855. }
  3856. else
  3857. {
  3858. Target = RemoteDirView->Path;
  3859. }
  3860. }
  3861. else
  3862. {
  3863. Target = GetSessionPath(Session, osRemote);
  3864. }
  3865. Target = UnixIncludeTrailingBackslash(Target);
  3866. if (FileList->Count == 1)
  3867. {
  3868. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3869. }
  3870. else
  3871. {
  3872. FileMask = AnyMask;
  3873. }
  3874. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3875. bool Result = true;
  3876. if (!NoConfirmation)
  3877. {
  3878. bool Multi = (FileList->Count > 1);
  3879. if (Move)
  3880. {
  3881. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3882. }
  3883. else
  3884. {
  3885. std::unique_ptr<TStrings> Sessions(new TStringList);
  3886. std::unique_ptr<TStrings> Directories(new TStringList);
  3887. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3888. for (int Index = 0; Index < SessionList->Count; Index++)
  3889. {
  3890. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3891. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3892. {
  3893. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3894. Directories->Add(GetSessionPath(ASession, osRemote));
  3895. }
  3896. }
  3897. TDirectRemoteCopy AllowDirectCopy;
  3898. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3899. {
  3900. DebugAssert(DirectCopy);
  3901. AllowDirectCopy = drcAllow;
  3902. }
  3903. else if (Terminal->IsCapable[fcSecondaryShell])
  3904. {
  3905. DebugAssert(DirectCopy);
  3906. AllowDirectCopy = drcConfirmCommandSession;
  3907. }
  3908. else
  3909. {
  3910. DebugAssert(!DirectCopy);
  3911. AllowDirectCopy = drcDisallow;
  3912. }
  3913. void * ASession = Session;
  3914. Result = DoRemoteCopyDialog(
  3915. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3916. Session = static_cast<TManagedTerminal *>(ASession);
  3917. }
  3918. }
  3919. return Result;
  3920. }
  3921. //---------------------------------------------------------------------------
  3922. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3923. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3924. {
  3925. DebugAssert(!IsLocalBrowserMode());
  3926. bool DirectCopy;
  3927. UnicodeString Target, FileMask;
  3928. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3929. if (Result)
  3930. {
  3931. if (!Move && !DirectCopy)
  3932. {
  3933. Configuration->Usage->Inc("RemoteCopyTemp");
  3934. UnicodeString RootTempDir;
  3935. UnicodeString TempDir;
  3936. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3937. TStrings * TemporaryFilesList = new TStringList();
  3938. try
  3939. {
  3940. TMakeLocalFileListParams MakeFileListParam;
  3941. MakeFileListParam.FileList = TemporaryFilesList;
  3942. MakeFileListParam.FileTimes = NULL;
  3943. MakeFileListParam.IncludeDirs = true;
  3944. MakeFileListParam.Recursive = false;
  3945. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  3946. TTerminalManager::Instance()->ActiveSession = Session;
  3947. if (TemporaryFilesList->Count > 0)
  3948. {
  3949. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3950. CopyParam.FileMask = FileMask;
  3951. DebugAssert(!FAutoOperation);
  3952. FAutoOperation = true;
  3953. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3954. }
  3955. }
  3956. __finally
  3957. {
  3958. delete TemporaryFilesList;
  3959. FAutoOperation = false;
  3960. if (!RootTempDir.IsEmpty())
  3961. {
  3962. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3963. }
  3964. }
  3965. }
  3966. else
  3967. {
  3968. RemoteDirView->SaveSelection();
  3969. RemoteDirView->SaveSelectedNames();
  3970. try
  3971. {
  3972. if (Move)
  3973. {
  3974. Configuration->Usage->Inc("RemoteMove");
  3975. Terminal->MoveFiles(FileList, Target, FileMask);
  3976. }
  3977. else
  3978. {
  3979. Configuration->Usage->Inc("RemoteCopyDirect");
  3980. DebugAssert(DirectCopy);
  3981. DebugAssert(Session == Terminal);
  3982. if (Terminal->IsCapable[fcRemoteCopy] ||
  3983. Terminal->CommandSessionOpened ||
  3984. CommandSessionFallback())
  3985. {
  3986. Terminal->CopyFiles(FileList, Target, FileMask);
  3987. }
  3988. }
  3989. }
  3990. catch(...)
  3991. {
  3992. RemoteDirView->DiscardSavedSelection();
  3993. throw;
  3994. }
  3995. RemoteDirView->RestoreSelection();
  3996. }
  3997. }
  3998. return Result;
  3999. }
  4000. //---------------------------------------------------------------------------
  4001. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4002. const UnicodeString & Path, TRemoteProperties & Properties)
  4003. {
  4004. DebugAssert(!IsLocalBrowserMode());
  4005. Properties.Valid = Properties.Valid << vpEncrypt;
  4006. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4007. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4008. }
  4009. //---------------------------------------------------------------------------
  4010. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4011. {
  4012. Side = GetSide(Side);
  4013. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4014. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4015. UnicodeString Name = LoadStr(NEW_FOLDER);
  4016. int AllowedChanges =
  4017. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4018. bool SaveSettings = false;
  4019. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4020. {
  4021. TWindowLock Lock(this);
  4022. if (!IsSideLocalBrowser(Side))
  4023. {
  4024. if (SaveSettings)
  4025. {
  4026. GUIConfiguration->NewDirectoryProperties = Properties;
  4027. }
  4028. CreateRemoteDirectory(Name, Properties);
  4029. }
  4030. else
  4031. {
  4032. DirView(Side)->CreateDirectory(Name);
  4033. }
  4034. }
  4035. }
  4036. //---------------------------------------------------------------------------
  4037. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4038. {
  4039. TWindowLock Lock(this);
  4040. DirView(Side)->ExecuteHomeDirectory();
  4041. }
  4042. //---------------------------------------------------------------------------
  4043. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4044. {
  4045. DoOpenDirectoryDialog(odBrowse, Side);
  4046. }
  4047. //---------------------------------------------------------------------------
  4048. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4049. {
  4050. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4051. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4052. bool Result = !Path.IsEmpty();
  4053. if (Result)
  4054. {
  4055. // While we might get here when the session is closed (from location profiles),
  4056. // it's not a problem as the Path setter is noop then
  4057. DirView(Side)->Path = Path;
  4058. }
  4059. return Result;
  4060. }
  4061. //---------------------------------------------------------------------------
  4062. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4063. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4064. bool & Close)
  4065. {
  4066. // terminal can be already closed (e.g. dropped connection)
  4067. if (Terminal != NULL)
  4068. {
  4069. try
  4070. {
  4071. TCalculateSizeParams Params;
  4072. Params.Stats = &Stats;
  4073. Terminal->CalculateFilesSize(FileList, Size, Params);
  4074. }
  4075. catch(...)
  4076. {
  4077. if (!Terminal->Active)
  4078. {
  4079. Close = true;
  4080. }
  4081. throw;
  4082. }
  4083. }
  4084. }
  4085. //---------------------------------------------------------------------------
  4086. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4087. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4088. bool & Close)
  4089. {
  4090. // terminal can be already closed (e.g. dropped connection)
  4091. if (Terminal != NULL)
  4092. {
  4093. Configuration->Usage->Inc(L"ChecksumCalculated");
  4094. try
  4095. {
  4096. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4097. }
  4098. catch(...)
  4099. {
  4100. if (!Terminal->Active)
  4101. {
  4102. Close = true;
  4103. }
  4104. throw;
  4105. }
  4106. }
  4107. }
  4108. //---------------------------------------------------------------------------
  4109. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4110. {
  4111. bool Result;
  4112. if (!IsSideLocalBrowser(Side))
  4113. {
  4114. TRemoteTokenList * GroupList = NULL;
  4115. TRemoteTokenList * UserList = NULL;
  4116. try
  4117. {
  4118. TRemoteProperties CurrentProperties;
  4119. if (Terminal->LoadFilesProperties(FileList))
  4120. {
  4121. RemoteDirView->Invalidate();
  4122. }
  4123. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4124. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4125. if (CapableGroupChanging || CapableOwnerChanging)
  4126. {
  4127. if (CapableGroupChanging)
  4128. {
  4129. GroupList = Terminal->Groups->Duplicate();
  4130. }
  4131. if (CapableOwnerChanging)
  4132. {
  4133. UserList = Terminal->Users->Duplicate();
  4134. }
  4135. TRemoteDirectory * Files = Terminal->Files;
  4136. int Count = Files->Count;
  4137. if (Count > 100)
  4138. {
  4139. Count = 100;
  4140. }
  4141. for (int Index = 0; Index < Count; Index++)
  4142. {
  4143. TRemoteFile * File = Files->Files[Index];
  4144. if (CapableGroupChanging)
  4145. {
  4146. GroupList->AddUnique(File->Group);
  4147. }
  4148. if (CapableOwnerChanging)
  4149. {
  4150. UserList->AddUnique(File->Owner);
  4151. }
  4152. }
  4153. // if we haven't collected tokens for all files in current directory,
  4154. // make sure we collect them at least for all selected files.
  4155. // (note that so far the files in FileList has to be from current direcotry)
  4156. if (Count < Files->Count)
  4157. {
  4158. for (int Index = 0; Index < FileList->Count; Index++)
  4159. {
  4160. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4161. if (CapableGroupChanging)
  4162. {
  4163. GroupList->AddUnique(File->Group);
  4164. }
  4165. if (CapableOwnerChanging)
  4166. {
  4167. UserList->AddUnique(File->Owner);
  4168. }
  4169. }
  4170. }
  4171. }
  4172. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4173. int Flags = 0;
  4174. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4175. if (CapableOwnerChanging) Flags |= cpOwner;
  4176. if (CapableGroupChanging) Flags |= cpGroup;
  4177. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4178. if (Terminal->IsCapable[fcCalculatingChecksum])
  4179. {
  4180. CalculateChecksumEvent = CalculateChecksum;
  4181. }
  4182. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4183. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4184. TRemoteProperties NewProperties = CurrentProperties;
  4185. Result =
  4186. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4187. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4188. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4189. CalculateSize, CalculateChecksumEvent);
  4190. if (Result)
  4191. {
  4192. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4193. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4194. }
  4195. }
  4196. __finally
  4197. {
  4198. delete GroupList;
  4199. delete UserList;
  4200. }
  4201. }
  4202. else
  4203. {
  4204. DirView(Side)->DisplayPropertiesMenu();
  4205. Result = true;
  4206. }
  4207. return Result;
  4208. }
  4209. //---------------------------------------------------------------------------
  4210. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4211. {
  4212. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4213. {
  4214. FIgnoreNextDialogChar = Key;
  4215. }
  4216. Key = 0;
  4217. }
  4218. //---------------------------------------------------------------------------
  4219. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4220. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4221. {
  4222. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4223. if (Command != NULL)
  4224. {
  4225. KeyProcessed(Key, Shift);
  4226. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4227. {
  4228. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4229. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4230. false, false, const_cast<TCustomCommandType *>(Command));
  4231. }
  4232. }
  4233. }
  4234. //---------------------------------------------------------------------------
  4235. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4236. {
  4237. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4238. {
  4239. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4240. }
  4241. if (!DirView(osCurrent)->IsEditing())
  4242. {
  4243. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4244. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4245. {
  4246. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4247. // But so far it's not a problem, as it does what we want.
  4248. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4249. if (((Action->ShortCut == KeyShortCut) ||
  4250. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4251. AllowedAction(Action, aaShortCut))
  4252. {
  4253. // Has to be called before the action as the dialog char is already in queue.
  4254. // So when the action consumes message queue, we already need to have the
  4255. // FIgnoreNextDialogChar set
  4256. KeyProcessed(Key, Shift);
  4257. // Reset reference to previous component (when menu/toolbar was clicked).
  4258. // Needed to detect that action was invoked by keyboard shortcut
  4259. // in TNonVisualDataModule::ExplorerActionsExecute
  4260. Action->ActionComponent = NULL;
  4261. Action->Execute();
  4262. return;
  4263. }
  4264. }
  4265. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4266. {
  4267. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4268. {
  4269. KeyProcessed(Key, Shift);
  4270. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4271. return;
  4272. }
  4273. }
  4274. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4275. {
  4276. KeyProcessed(Key, Shift);
  4277. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4278. }
  4279. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4280. if (IsCustomShortCut(CustomShortCut))
  4281. {
  4282. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4283. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4284. if (WinConfiguration->SharedBookmarks != NULL)
  4285. {
  4286. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4287. if ((Bookmark != NULL) &&
  4288. OpenBookmark(FCurrentSide, Bookmark))
  4289. {
  4290. KeyProcessed(Key, Shift);
  4291. }
  4292. }
  4293. }
  4294. }
  4295. TForm::KeyDown(Key, Shift);
  4296. }
  4297. //---------------------------------------------------------------------------
  4298. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4299. {
  4300. bool OpenedTerminal = false;
  4301. UnicodeString ProtocolName;
  4302. UnicodeString ProtocolNameHint;
  4303. UnicodeString SecurityProtocolName;
  4304. UnicodeString DurationHint;
  4305. // See the comment in TScpCommanderForm::UpdateControls()
  4306. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4307. if (ActiveTerminal != NULL)
  4308. {
  4309. // Active is not enough here
  4310. if (ActiveTerminal->Status == ssOpened)
  4311. {
  4312. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4313. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4314. if (FileSystemInfo.ProtocolName.IsEmpty())
  4315. {
  4316. ProtocolName = SessionInfo.ProtocolName;
  4317. }
  4318. else
  4319. {
  4320. ProtocolName = FileSystemInfo.ProtocolName;
  4321. }
  4322. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4323. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4324. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4325. OpenedTerminal = true;
  4326. }
  4327. else if (ActiveTerminal->Status > ssClosed)
  4328. {
  4329. ProtocolName = LoadStr(STATUS_CONNECTING);
  4330. }
  4331. else
  4332. {
  4333. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4334. }
  4335. }
  4336. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4337. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4338. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4339. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4340. if (ProtocolStatusPanel->Caption != ProtocolName)
  4341. {
  4342. ProtocolStatusPanel->Caption = ProtocolName;
  4343. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4344. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4345. }
  4346. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4347. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4348. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4349. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4350. SecurityStatusPanel->Enabled = SecurityEnabled;
  4351. UnicodeString SecurityProtocolHint;
  4352. if (SecurityEnabled)
  4353. {
  4354. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4355. }
  4356. else
  4357. {
  4358. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4359. }
  4360. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4361. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4362. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4363. DurationStatusPanel->Hint = DurationHint;
  4364. UpdateStatusBar();
  4365. }
  4366. //---------------------------------------------------------------------------
  4367. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4368. {
  4369. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4370. DebugAssert(SessionStatusBar != NULL);
  4371. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4372. if (!FNote.IsEmpty())
  4373. {
  4374. NoteStatusPanel->Caption = FNote;
  4375. }
  4376. else
  4377. {
  4378. UpdateStatusPanelText(NoteStatusPanel);
  4379. }
  4380. NoteStatusPanel->Hint = FNoteHints;
  4381. UnicodeString LoginTime;
  4382. // See the comment in TScpCommanderForm::UpdateControls()
  4383. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4384. // Active is not enough here
  4385. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4386. {
  4387. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4388. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4389. }
  4390. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4391. DurationStatusPanel->Caption = LoginTime;
  4392. }
  4393. //---------------------------------------------------------------------------
  4394. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4395. {
  4396. Panel->Caption = L"";
  4397. }
  4398. //---------------------------------------------------------------------------
  4399. void __fastcall TCustomScpExplorerForm::Idle()
  4400. {
  4401. if (FShowing ||
  4402. // Particularly to detect closed connection and automatically reconnect it while waiting for changes
  4403. // while "keeping remote directory up to date"
  4404. StandaloneOperation)
  4405. {
  4406. FEditorManager->Check();
  4407. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4408. // that needs to know if queue view is visible (and it may be closed after queue update)
  4409. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4410. }
  4411. if (FShowing)
  4412. {
  4413. if (!NonVisualDataModule->Busy &&
  4414. // Menu is opened or drag&drop is going on
  4415. (Mouse->Capture == NULL))
  4416. {
  4417. if (FRefreshRemoteDirectory)
  4418. {
  4419. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4420. {
  4421. Terminal->RefreshDirectory();
  4422. }
  4423. FRefreshRemoteDirectory = false;
  4424. }
  4425. if (FRefreshLocalDirectory)
  4426. {
  4427. ReloadLocalDirectory();
  4428. FRefreshLocalDirectory = false;
  4429. }
  4430. if (WinConfiguration->RefreshRemotePanel)
  4431. {
  4432. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4433. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4434. {
  4435. RemoteDirView->ReloadDirectory();
  4436. }
  4437. }
  4438. }
  4439. }
  4440. if (FShowing || StandaloneOperation)
  4441. {
  4442. if (FQueueStatusInvalidated)
  4443. {
  4444. UpdateQueueStatus(false);
  4445. }
  4446. RefreshQueueItems();
  4447. }
  4448. if (FShowing)
  4449. {
  4450. UpdateStatusBar();
  4451. }
  4452. FIgnoreNextDialogChar = 0;
  4453. }
  4454. //---------------------------------------------------------------------------
  4455. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4456. {
  4457. try
  4458. {
  4459. FUserActionTimer->Enabled = false;
  4460. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4461. {
  4462. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4463. {
  4464. FPendingQueueActionItem->ProcessUserAction();
  4465. }
  4466. }
  4467. }
  4468. __finally
  4469. {
  4470. FPendingQueueActionItem = NULL;
  4471. }
  4472. }
  4473. //---------------------------------------------------------------------------
  4474. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4475. {
  4476. if (WinConfiguration->MinimizeToTray)
  4477. {
  4478. UpdateTrayIcon();
  4479. FTrayIcon->Visible = true;
  4480. if (Visible)
  4481. {
  4482. ShowWindow(Handle, SW_HIDE);
  4483. }
  4484. }
  4485. }
  4486. //---------------------------------------------------------------------------
  4487. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4488. {
  4489. // WORKAROUND
  4490. // When restoring maximized window from minimization,
  4491. // rarely some controls do not align properly.
  4492. // Two instances seen (both for Commander):
  4493. // - When restoring, window is temporarily narrower (not maximized),
  4494. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4495. // Once maximized already, top dock shinks back, but the session PageControl,
  4496. // do not align up, leaving space between TopDock and PageControl.
  4497. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4498. for (int Index = 0; Index < ControlCount; Index++)
  4499. {
  4500. RealignControl(Controls[Index]);
  4501. }
  4502. if (FTrayIcon->Visible)
  4503. {
  4504. FTrayIcon->Visible = false;
  4505. if (Visible)
  4506. {
  4507. ShowWindow(Handle, SW_SHOW);
  4508. }
  4509. }
  4510. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4511. {
  4512. FNeedSession = false;
  4513. NonVisualDataModule->StartBusy();
  4514. try
  4515. {
  4516. NeedSession(true);
  4517. }
  4518. __finally
  4519. {
  4520. NonVisualDataModule->EndBusy();
  4521. }
  4522. }
  4523. }
  4524. //---------------------------------------------------------------------------
  4525. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4526. {
  4527. FTrayIcon->Hint = GetMainForm()->Caption;
  4528. }
  4529. //---------------------------------------------------------------------------
  4530. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4531. {
  4532. UpdateTrayIcon();
  4533. }
  4534. //---------------------------------------------------------------------------
  4535. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4536. {
  4537. // workaround
  4538. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4539. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4540. // But WM_ACTIVATEAPP message can be received even when
  4541. // the main window is minimized to the tray and internal editor window is focused
  4542. // (after another application was previously active)
  4543. if (::IsIconic(Handle))
  4544. {
  4545. if (!IsAppIconic())
  4546. {
  4547. SetAppIconic(true);
  4548. }
  4549. }
  4550. ::ApplicationRestore();
  4551. Application->BringToFront();
  4552. }
  4553. //---------------------------------------------------------------------------
  4554. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4555. {
  4556. RestoreApp();
  4557. }
  4558. //---------------------------------------------------------------------------
  4559. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4560. {
  4561. if (OpenInNewWindow())
  4562. {
  4563. ExecuteNewInstance(SessionUrl);
  4564. }
  4565. else
  4566. {
  4567. TTerminalManager::Instance()->NewSession(SessionUrl);
  4568. }
  4569. }
  4570. //---------------------------------------------------------------------------
  4571. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4572. {
  4573. NewSession();
  4574. }
  4575. //---------------------------------------------------------------------------
  4576. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4577. {
  4578. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4579. StoredSessions->NewSession(SessionName, SessionData);
  4580. // modified only, explicit
  4581. StoredSessions->Save(false, true);
  4582. // encode session name because of slashes in hierarchical sessions
  4583. return EncodeUrlString(SessionName);
  4584. }
  4585. //---------------------------------------------------------------------------
  4586. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4587. {
  4588. // current working directories become defaults here, what is not right
  4589. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4590. return SaveHiddenDuplicateSession(SessionData.get());
  4591. }
  4592. //---------------------------------------------------------------------------
  4593. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4594. {
  4595. if (OpenInNewWindow())
  4596. {
  4597. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4598. }
  4599. else
  4600. {
  4601. // current working directories become defaults here, what is not right
  4602. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4603. TTerminalManager * Manager = TTerminalManager::Instance();
  4604. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4605. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4606. // We definitelly want these
  4607. ASession->Disconnected = ManagedSession->Disconnected;
  4608. // Not sure about these two
  4609. ASession->Permanent = ManagedSession->Permanent;
  4610. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4611. Manager->ActiveSession = ASession;
  4612. }
  4613. }
  4614. //---------------------------------------------------------------------------
  4615. void __fastcall TCustomScpExplorerForm::RenameSession()
  4616. {
  4617. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4618. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4619. // When submitting the default name, do not fix it
  4620. (Name != ManagedSession->SessionData->SessionName))
  4621. {
  4622. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4623. TSessionData::ValidateName(Name);
  4624. // This is inconsistent with how color (for example) is handled.
  4625. ManagedSession->SessionData->Name = Name;
  4626. UpdateControls();
  4627. // Add/Remove distinguishing paths from sessions of the same name.
  4628. SessionListChanged();
  4629. }
  4630. }
  4631. //---------------------------------------------------------------------------
  4632. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4633. {
  4634. DebugAssert(Queue != NULL);
  4635. bool Result = Queue->IsEmpty;
  4636. if (!Result)
  4637. {
  4638. SetFocus();
  4639. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4640. }
  4641. return Result;
  4642. }
  4643. //---------------------------------------------------------------------------
  4644. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4645. {
  4646. return CanCloseQueue(FQueue);
  4647. }
  4648. //---------------------------------------------------------------------------
  4649. void __fastcall TCustomScpExplorerForm::CloseSession()
  4650. {
  4651. if (CanCloseQueue())
  4652. {
  4653. TTerminalManager::Instance()->FreeActiveTerminal();
  4654. }
  4655. }
  4656. //---------------------------------------------------------------------------
  4657. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4658. {
  4659. if (CanCloseQueue())
  4660. {
  4661. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4662. }
  4663. }
  4664. //---------------------------------------------------------------------------
  4665. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4666. {
  4667. DetachTerminal(Terminal);
  4668. RemoteDirView->Terminal = Terminal;
  4669. UpdateRemotePathComboBox(false);
  4670. InitStatusBar();
  4671. UpdateControls();
  4672. }
  4673. //---------------------------------------------------------------------------
  4674. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4675. {
  4676. InitStatusBar();
  4677. }
  4678. //---------------------------------------------------------------------------
  4679. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4680. {
  4681. if (DebugAlwaysTrue(!Terminal->Active))
  4682. {
  4683. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4684. }
  4685. }
  4686. //---------------------------------------------------------------------------
  4687. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4688. {
  4689. if (OpenInNewWindow())
  4690. {
  4691. // encode session name because of slashes in hierarchical sessions
  4692. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4693. }
  4694. else
  4695. {
  4696. TTerminalManager * Manager = TTerminalManager::Instance();
  4697. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4698. Manager->ActiveSession = Terminal;
  4699. }
  4700. }
  4701. //---------------------------------------------------------------------------
  4702. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4703. {
  4704. TTerminalManager * Manager = TTerminalManager::Instance();
  4705. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4706. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4707. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4708. // FirstSession can be null, if some of the
  4709. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4710. {
  4711. FirstSession->Disconnected = true;
  4712. FirstSession->DisconnectedTemporarily = true;
  4713. }
  4714. Manager->ActiveSession = FirstSession;
  4715. }
  4716. //---------------------------------------------------------------------------
  4717. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4718. {
  4719. if (OpenInNewWindow())
  4720. {
  4721. ExecuteNewInstance(Name);
  4722. }
  4723. else
  4724. {
  4725. DoOpenFolderOrWorkspace(Name, true);
  4726. }
  4727. }
  4728. //---------------------------------------------------------------------------
  4729. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4730. bool &CanClose)
  4731. {
  4732. // See the comment in CloseApp()
  4733. if (NonVisualDataModule->Busy)
  4734. {
  4735. CanClose = false;
  4736. }
  4737. else
  4738. {
  4739. TTerminalManager * Manager = TTerminalManager::Instance();
  4740. int ActiveSessions = 0;
  4741. for (int Index = 0; Index < Manager->Count; Index++)
  4742. {
  4743. if (Manager->Sessions[Index]->Active)
  4744. {
  4745. ActiveSessions++;
  4746. }
  4747. }
  4748. // Confirm, when there's at least one active remote session or
  4749. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4750. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4751. WinConfiguration->ConfirmClosingSession)
  4752. {
  4753. unsigned int Result;
  4754. TMessageParams Params(mpNeverAskAgainCheck);
  4755. UnicodeString Message;
  4756. int Answers = qaOK | qaCancel;
  4757. // The current session is the only active session
  4758. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4759. {
  4760. if (!WinConfiguration->AutoSaveWorkspace)
  4761. {
  4762. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4763. }
  4764. else
  4765. {
  4766. Message = LoadStr(CLOSE_SESSION);
  4767. }
  4768. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4769. }
  4770. // Multiple active sessions or one active session, but it's not the current one
  4771. else if (ActiveSessions > 0)
  4772. {
  4773. if (!WinConfiguration->AutoSaveWorkspace)
  4774. {
  4775. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4776. }
  4777. else
  4778. {
  4779. Message = LoadStr(CLOSE_SESSIONS);
  4780. }
  4781. }
  4782. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4783. else
  4784. {
  4785. DebugAlwaysTrue(Manager->Count > 1);
  4786. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4787. Message = LoadStr(CLOSE_WORKSPACE);
  4788. }
  4789. UnicodeString Note;
  4790. if (WinConfiguration->AutoSaveWorkspace)
  4791. {
  4792. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4793. }
  4794. else
  4795. {
  4796. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4797. Answers = qaYes | qaNo | qaCancel;
  4798. }
  4799. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4800. SetFocus();
  4801. Result = MessageDialog(Message, qtConfirmation,
  4802. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4803. if (Result == qaNeverAskAgain)
  4804. {
  4805. WinConfiguration->ConfirmClosingSession = false;
  4806. }
  4807. if (Result == qaNo)
  4808. {
  4809. CanClose = SaveWorkspace(true);
  4810. // note that the workspace will be saved redundatly again from FormClose
  4811. }
  4812. else
  4813. {
  4814. CanClose =
  4815. (Result == qaOK) ||
  4816. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4817. (Result == qaNeverAskAgain);
  4818. }
  4819. }
  4820. if (CanClose && HasActiveTerminal())
  4821. {
  4822. CanClose = CanCloseQueue();
  4823. }
  4824. }
  4825. if (CanClose)
  4826. {
  4827. CanClose =
  4828. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4829. FEditorManager->CloseExternalFilesWithoutProcess();
  4830. if (CanClose)
  4831. {
  4832. while (CanClose && (FLocalEditors->Count > 0))
  4833. {
  4834. int PrevCount = FLocalEditors->Count;
  4835. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4836. CanClose = (FLocalEditors->Count < PrevCount);
  4837. }
  4838. if (CanClose)
  4839. {
  4840. CanClose = FEditorManager->Empty(true);
  4841. if (!CanClose)
  4842. {
  4843. SetFocus();
  4844. CanClose =
  4845. (MessageDialog(
  4846. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4847. }
  4848. }
  4849. }
  4850. }
  4851. }
  4852. //---------------------------------------------------------------------------
  4853. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4854. {
  4855. TForm * Form = dynamic_cast<TForm *>(Sender);
  4856. DebugAssert(Form != NULL);
  4857. Form->Close();
  4858. }
  4859. //---------------------------------------------------------------------------
  4860. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4861. {
  4862. TForm * Form = dynamic_cast<TForm *>(Sender);
  4863. delete Form;
  4864. }
  4865. //---------------------------------------------------------------------------
  4866. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4867. {
  4868. while (FLocalEditors->Count > 0)
  4869. {
  4870. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4871. }
  4872. }
  4873. //---------------------------------------------------------------------------
  4874. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4875. TObject *Sender)
  4876. {
  4877. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4878. try
  4879. {
  4880. SetProperties(osRemote, FileList);
  4881. }
  4882. __finally
  4883. {
  4884. delete FileList;
  4885. }
  4886. }
  4887. //---------------------------------------------------------------------------
  4888. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4889. {
  4890. if (value)
  4891. {
  4892. if (Component == fcCustomCommandsBand)
  4893. {
  4894. UpdateCustomCommandsToolbar();
  4895. }
  4896. if (Component == fcQueueFileList)
  4897. {
  4898. DebugAssert(!QueueFileListSplitter->Visible);
  4899. AdjustQueueLayout();
  4900. UpdateQueueFileList();
  4901. }
  4902. }
  4903. }
  4904. //---------------------------------------------------------------------------
  4905. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4906. {
  4907. int MinHeightLimit =
  4908. GetStaticQueuePanelComponentsHeight() +
  4909. QueueFileListSplitter->Height +
  4910. QueueFileList->Height +
  4911. GetMinQueueViewHeight();
  4912. if (QueuePanel->ClientHeight < MinHeightLimit)
  4913. {
  4914. QueuePanel->ClientHeight = MinHeightLimit;
  4915. }
  4916. }
  4917. //---------------------------------------------------------------------------
  4918. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4919. {
  4920. TControl * Control = GetComponent(Component);
  4921. DebugAssert(Control);
  4922. bool Changed = (Control->Visible != value);
  4923. if (Changed)
  4924. {
  4925. ComponentShowing(Component, value);
  4926. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4927. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4928. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4929. if (value)
  4930. {
  4931. int RemainingHeight = Control->Parent->ClientHeight;
  4932. int RemainingWidth = Control->Parent->ClientWidth;
  4933. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4934. {
  4935. TControl * ChildControl = Control->Parent->Controls[i];
  4936. if (ChildControl->Visible)
  4937. {
  4938. switch (ChildControl->Align)
  4939. {
  4940. case alTop:
  4941. case alBottom:
  4942. RemainingHeight -= ChildControl->Height;
  4943. break;
  4944. case alLeft:
  4945. case alRight:
  4946. RemainingWidth -= ChildControl->Width;
  4947. break;
  4948. }
  4949. }
  4950. }
  4951. int Reserve = ScaleByTextHeight(this, 32);
  4952. // queue in explorer, trees in commander
  4953. if (Control->Height > RemainingHeight - Reserve)
  4954. {
  4955. Control->Height = RemainingHeight / 2;
  4956. }
  4957. if (Control->Width > RemainingWidth - Reserve)
  4958. {
  4959. Control->Width = RemainingWidth / 2;
  4960. }
  4961. }
  4962. Control->Visible = value;
  4963. if (WasFocused && Visible)
  4964. {
  4965. DirView(osCurrent)->SetFocus();
  4966. }
  4967. FixControlsPlacement();
  4968. }
  4969. }
  4970. //---------------------------------------------------------------------------
  4971. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4972. {
  4973. TControl * Control = GetComponent(Component);
  4974. if (Control == NULL)
  4975. {
  4976. return false;
  4977. }
  4978. else
  4979. {
  4980. return Control->Visible;
  4981. }
  4982. }
  4983. //---------------------------------------------------------------------------
  4984. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4985. {
  4986. bool Result;
  4987. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4988. {
  4989. Result = !IsUWP();
  4990. }
  4991. else
  4992. {
  4993. Result = true;
  4994. }
  4995. return Result;
  4996. }
  4997. //---------------------------------------------------------------------------
  4998. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  4999. {
  5000. if (DirView->ItemFocused != NULL)
  5001. {
  5002. DirView->ItemFocused->MakeVisible(false);
  5003. }
  5004. }
  5005. //---------------------------------------------------------------------------
  5006. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5007. {
  5008. MakeFocusedItemVisible(RemoteDirView);
  5009. QueueSplitter->Visible = QueuePanel->Visible;
  5010. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5011. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5012. TControl * QueueControlsOrder[] =
  5013. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5014. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5015. }
  5016. //---------------------------------------------------------------------------
  5017. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5018. {
  5019. switch (Component) {
  5020. case fcStatusBar: return RemoteStatusBar;
  5021. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5022. case fcQueueView: return QueuePanel;
  5023. case fcQueueToolbar: return QueueDock;
  5024. case fcRemoteTree: return RemoteDrivePanel;
  5025. case fcSessionsTabs: return SessionsPageControl;
  5026. case fcQueueFileList: return QueueFileList;
  5027. default: return NULL;
  5028. }
  5029. }
  5030. //---------------------------------------------------------------------------
  5031. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5032. TObject *Sender, TListColumn *Column, TPoint &Point)
  5033. {
  5034. DebugAssert(NonVisualDataModule && Column && Sender);
  5035. NonVisualDataModule->ListColumn = Column;
  5036. TPopupMenu * DirViewColumnMenu;
  5037. if (Sender == DirView(osOther))
  5038. {
  5039. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5040. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5041. (Column->Index == uvName);
  5042. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5043. (Column->Index == uvSize);
  5044. }
  5045. else
  5046. {
  5047. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5048. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5049. (Column->Index == dvName);
  5050. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5051. (Column->Index == dvSize);
  5052. }
  5053. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5054. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5055. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5056. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5057. }
  5058. //---------------------------------------------------------------------------
  5059. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5060. TObject *Sender, TListItem *Item, bool &AllowExec)
  5061. {
  5062. DoDirViewExecFile(Sender, Item, AllowExec);
  5063. }
  5064. //---------------------------------------------------------------------------
  5065. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5066. TListItem * Item, bool & AllowExec)
  5067. {
  5068. DebugAssert(Sender && Item && Configuration);
  5069. DebugAssert(AllowExec);
  5070. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5071. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5072. bool Remote = !IsSideLocalBrowser(Side);
  5073. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  5074. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  5075. // on files only (not directories)
  5076. // and only when symlinks are resolved (apply to remote panel only)
  5077. if (!ADirView->ItemIsDirectory(Item) &&
  5078. (ResolvedSymlinks || FForceExecution))
  5079. {
  5080. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5081. !FForceExecution && ResolvedSymlinks)
  5082. {
  5083. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5084. {
  5085. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5086. UnlockWindow();
  5087. try
  5088. {
  5089. if (IsLocalBrowserMode())
  5090. {
  5091. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5092. }
  5093. else
  5094. {
  5095. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5096. }
  5097. }
  5098. __finally
  5099. {
  5100. LockWindow();
  5101. }
  5102. AllowExec = false;
  5103. }
  5104. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5105. {
  5106. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5107. {
  5108. ExecuteFile(Side, efDefaultEditor);
  5109. AllowExec = false;
  5110. }
  5111. }
  5112. else
  5113. {
  5114. DebugFail();
  5115. }
  5116. }
  5117. // if we have not done anything special, fall back to default behavior
  5118. if (AllowExec)
  5119. {
  5120. if (Remote && !WinConfiguration->DisableOpenEdit)
  5121. {
  5122. ExecuteFile(osRemote, efShell);
  5123. AllowExec = false;
  5124. }
  5125. }
  5126. }
  5127. }
  5128. //---------------------------------------------------------------------------
  5129. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5130. {
  5131. return ((Side == osRemote) || (Side == osCurrent));
  5132. }
  5133. //---------------------------------------------------------------------------
  5134. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5135. {
  5136. DebugFail();
  5137. }
  5138. //---------------------------------------------------------------------------
  5139. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5140. {
  5141. DebugFail();
  5142. }
  5143. //---------------------------------------------------------------------------
  5144. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5145. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5146. {
  5147. TSynchronizeParamType Params;
  5148. Params.LocalDirectory = LocalDirectory;
  5149. Params.RemoteDirectory = RemoteDirectory;
  5150. int UnusedParams =
  5151. (GUIConfiguration->SynchronizeParams &
  5152. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5153. TTerminal::spCaseSensitive));
  5154. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5155. Params.Options = GUIConfiguration->SynchronizeOptions;
  5156. bool SaveSettings = false;
  5157. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5158. &DoSynchronizeTooManyDirectories);
  5159. DebugAssert(FSynchronizeController == NULL);
  5160. FSynchronizeController = &Controller;
  5161. bool Result;
  5162. try
  5163. {
  5164. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5165. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5166. int Options =
  5167. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5168. DebugAssert(FOnFeedSynchronizeError == NULL);
  5169. DebugAssert(FOnSynchronizeAbort == NULL);
  5170. DebugAssert(FSynchronizeTerminal == NULL);
  5171. FSynchronizeTerminal = Terminal;
  5172. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5173. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5174. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5175. if (Result)
  5176. {
  5177. if (SaveSettings)
  5178. {
  5179. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5180. GUIConfiguration->SynchronizeOptions = Params.Options;
  5181. }
  5182. else
  5183. {
  5184. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5185. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5186. FLAGSET(Params.Options, soSynchronize))
  5187. {
  5188. GUIConfiguration->SynchronizeOptions =
  5189. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5190. soSynchronize;
  5191. }
  5192. }
  5193. LocalDirectory = Params.LocalDirectory;
  5194. RemoteDirectory = Params.RemoteDirectory;
  5195. }
  5196. }
  5197. __finally
  5198. {
  5199. FSynchronizeController = NULL;
  5200. DebugAssert(FOnFeedSynchronizeError == NULL);
  5201. FOnFeedSynchronizeError = NULL;
  5202. DebugAssert(FOnSynchronizeAbort == NULL);
  5203. FOnSynchronizeAbort = NULL;
  5204. FSynchronizeTerminal = NULL;
  5205. }
  5206. return Result;
  5207. }
  5208. //---------------------------------------------------------------------------
  5209. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5210. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5211. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5212. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5213. TSynchronizeOptions * Options, bool Full)
  5214. {
  5215. // Keep-up-to-date mode
  5216. if (Terminal->Status == ssOpened)
  5217. {
  5218. try
  5219. {
  5220. int PParams = Params.Params;
  5221. if (!Full)
  5222. {
  5223. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5224. if (FLAGSET(Params.Options, soRecurse))
  5225. {
  5226. PParams |= TTerminal::spSubDirs;
  5227. }
  5228. }
  5229. else
  5230. {
  5231. // if keepuptodate is non-recursive,
  5232. // full sync before has to be non-recursive as well
  5233. if (FLAGCLEAR(Params.Options, soRecurse))
  5234. {
  5235. PParams |= TTerminal::spNoRecurse;
  5236. }
  5237. }
  5238. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5239. PParams, Checklist, Options);
  5240. }
  5241. catch (Exception & E)
  5242. {
  5243. if (!Terminal->Active)
  5244. {
  5245. ShowExtendedExceptionEx(Terminal, &E);
  5246. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5247. if (!Terminal->Active)
  5248. {
  5249. throw;
  5250. }
  5251. }
  5252. else
  5253. {
  5254. if (FLAGSET(Params.Options, soContinueOnError) &&
  5255. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5256. {
  5257. // noop, already logged in MoreMessageDialog
  5258. }
  5259. else
  5260. {
  5261. // We get mostly EAbort here, so this is noop.
  5262. // Exception is an error when listing directories while looking for differences.
  5263. ShowExtendedExceptionEx(Terminal, &E);
  5264. throw;
  5265. }
  5266. }
  5267. }
  5268. }
  5269. }
  5270. //---------------------------------------------------------------------------
  5271. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5272. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5273. const UnicodeString ErrorStr)
  5274. {
  5275. if (!Directory.IsEmpty())
  5276. {
  5277. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5278. }
  5279. else
  5280. {
  5281. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5282. }
  5283. }
  5284. //---------------------------------------------------------------------------
  5285. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5286. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5287. {
  5288. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5289. {
  5290. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5291. }
  5292. else
  5293. {
  5294. TMessageParams Params(mpNeverAskAgainCheck);
  5295. unsigned int Result = MessageDialog(
  5296. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5297. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5298. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5299. {
  5300. MaxDirectories *= 2;
  5301. if (Result == qaNeverAskAgain)
  5302. {
  5303. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5304. }
  5305. }
  5306. else
  5307. {
  5308. Abort();
  5309. }
  5310. }
  5311. }
  5312. //---------------------------------------------------------------------------
  5313. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5314. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5315. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5316. TSynchronizeOptions * Options)
  5317. {
  5318. // Keep-up-to-date mode
  5319. DebugAssert(!FAutoOperation);
  5320. void * BatchStorage;
  5321. BatchStart(BatchStorage);
  5322. FAutoOperation = true;
  5323. bool AnyOperation = false;
  5324. TDateTime StartTime = Now();
  5325. TSynchronizeChecklist * AChecklist = NULL;
  5326. try
  5327. {
  5328. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5329. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5330. {
  5331. FSynchronizeProgressForm->Start();
  5332. }
  5333. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5334. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5335. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5336. Options);
  5337. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5338. SAFE_DESTROY(FSynchronizeProgressForm);
  5339. AnyOperation = (AChecklist->CheckedCount > 0);
  5340. if (AnyOperation)
  5341. {
  5342. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5343. CreateProgressForm(&SynchronizeProgress);
  5344. try
  5345. {
  5346. Terminal->SynchronizeApply(
  5347. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5348. SynchronizeProcessedItem, NULL, NULL, NULL);
  5349. }
  5350. __finally
  5351. {
  5352. DestroyProgressForm();
  5353. }
  5354. }
  5355. }
  5356. __finally
  5357. {
  5358. if (Checklist == NULL)
  5359. {
  5360. delete AChecklist;
  5361. }
  5362. else
  5363. {
  5364. *Checklist = AChecklist;
  5365. }
  5366. FAutoOperation = false;
  5367. SAFE_DESTROY(FSynchronizeProgressForm);
  5368. BatchEnd(BatchStorage);
  5369. ReloadLocalDirectory();
  5370. if (AnyOperation)
  5371. {
  5372. OperationComplete(StartTime);
  5373. }
  5374. }
  5375. }
  5376. //---------------------------------------------------------------------------
  5377. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5378. {
  5379. // can be called from command line
  5380. return Visible &&
  5381. ((DirView(osRemote)->SelCount > 0) ||
  5382. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5383. }
  5384. //---------------------------------------------------------------------------
  5385. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5386. {
  5387. LogSynchronizeEvent(Terminal, Message);
  5388. }
  5389. //---------------------------------------------------------------------------
  5390. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5391. int Params, TSynchronizeOptions & Options)
  5392. {
  5393. DebugAssert(!IsLocalBrowserMode());
  5394. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5395. {
  5396. Options.Filter = new TStringList();
  5397. Options.Filter->CaseSensitive = false;
  5398. Options.Filter->Duplicates = Types::dupAccept;
  5399. if (DirView(osRemote)->SelCount > 0)
  5400. {
  5401. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5402. }
  5403. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5404. {
  5405. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5406. }
  5407. Options.Filter->Sort();
  5408. }
  5409. }
  5410. //---------------------------------------------------------------------------
  5411. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5412. {
  5413. TCopyParamType Defaults;
  5414. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5415. ConfigStorage->AccessMode = smRead;
  5416. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5417. {
  5418. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5419. }
  5420. ConfigStorage.reset(NULL);
  5421. std::unique_ptr<TStringList> Options(new TStringList());
  5422. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5423. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5424. UnicodeString Result;
  5425. if (Options->Count > 0)
  5426. {
  5427. Result =
  5428. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5429. }
  5430. return Result;
  5431. }
  5432. //---------------------------------------------------------------------------
  5433. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5434. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5435. {
  5436. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5437. UnicodeString AdditionalParams =
  5438. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5439. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5440. SerializeCopyParamForCommandLine(CopyParams),
  5441. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5442. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5443. ExecuteNewInstance(SessionName, AdditionalParams);
  5444. }
  5445. //---------------------------------------------------------------------------
  5446. struct TSynchronizeParams
  5447. {
  5448. UnicodeString LocalDirectory;
  5449. UnicodeString RemoteDirectory;
  5450. TSynchronizeMode Mode;
  5451. TSynchronizeChecklist * Checklist;
  5452. int Params;
  5453. TCopyParamType * CopyParam;
  5454. TDateTime CollectElapsed;
  5455. TDateTime * StartTime;
  5456. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5457. };
  5458. //---------------------------------------------------------------------------
  5459. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5460. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5461. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5462. {
  5463. DebugAssert(!FAutoOperation);
  5464. void * BatchStorage;
  5465. BatchStart(BatchStorage);
  5466. FAutoOperation = true;
  5467. TFileOperationStatistics Statistics;
  5468. TDateTime Start = Now();
  5469. try
  5470. {
  5471. Params.OnProcessedItem = OnProcessedItem;
  5472. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5473. CreateProgressForm(&SynchronizeProgress);
  5474. Terminal->SynchronizeApply(
  5475. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5476. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5477. &Statistics);
  5478. }
  5479. __finally
  5480. {
  5481. Params.OnProcessedItem = NULL;
  5482. FAutoOperation = false;
  5483. DestroyProgressForm();
  5484. BatchEnd(BatchStorage);
  5485. ReloadLocalDirectory();
  5486. }
  5487. if (WinConfiguration->SynchronizeSummary)
  5488. {
  5489. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5490. // The statistics should be 0 anyway in this case
  5491. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5492. {
  5493. Message += L"\n";
  5494. if (Statistics.FilesUploaded > 0)
  5495. {
  5496. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5497. }
  5498. if (Statistics.FilesDownloaded > 0)
  5499. {
  5500. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5501. }
  5502. if (Statistics.FilesDeletedLocal > 0)
  5503. {
  5504. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5505. }
  5506. if (Statistics.FilesDeletedRemote > 0)
  5507. {
  5508. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5509. }
  5510. }
  5511. TDateTime Elapsed = (Now() - Start);
  5512. Message +=
  5513. L"\n" +
  5514. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5515. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5516. TMessageParams Params(mpNeverAskAgainCheck);
  5517. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5518. if (Result == qaNeverAskAgain)
  5519. {
  5520. WinConfiguration->SynchronizeSummary = false;
  5521. }
  5522. }
  5523. }
  5524. //---------------------------------------------------------------------------
  5525. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5526. {
  5527. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5528. {
  5529. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5530. }
  5531. // Not set in keep-up-to-date mode - Synchronize() method
  5532. if (Token != NULL)
  5533. {
  5534. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5535. if (Params.OnProcessedItem != NULL)
  5536. {
  5537. Params.OnProcessedItem(NULL, ChecklistItem);
  5538. }
  5539. }
  5540. }
  5541. //---------------------------------------------------------------------------
  5542. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5543. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5544. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5545. {
  5546. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5547. *Params.StartTime = Now();
  5548. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5549. }
  5550. //---------------------------------------------------------------------------
  5551. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5552. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5553. {
  5554. // terminal can be already closed (e.g. dropped connection)
  5555. if (Terminal != NULL)
  5556. {
  5557. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5558. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5559. }
  5560. }
  5561. //---------------------------------------------------------------------------
  5562. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5563. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5564. {
  5565. DebugAssert(!IsLocalBrowserMode());
  5566. TAutoBatch AutoBatch(this);
  5567. TAutoFlag AutoOperationFlag(FAutoOperation);
  5568. if (Side == osRemote)
  5569. {
  5570. std::unique_ptr<TStrings> FileList(new TStringList());
  5571. FileList->AddObject(FileName, RemoteFile);
  5572. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5573. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5574. RemoteDirView->SaveSelection();
  5575. RemoteDirView->SaveSelectedNames();
  5576. try
  5577. {
  5578. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5579. }
  5580. catch(...)
  5581. {
  5582. RemoteDirView->DiscardSavedSelection();
  5583. throw;
  5584. }
  5585. RemoteDirView->RestoreSelection();
  5586. }
  5587. else if (DebugAlwaysTrue(Side == osLocal))
  5588. {
  5589. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5590. {
  5591. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5592. }
  5593. UnicodeString Directory = ExtractFileDir(FileName);
  5594. ReloadLocalDirectory(Directory);
  5595. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5596. if (!SamePaths(Directory, NewDirectory))
  5597. {
  5598. ReloadLocalDirectory(NewDirectory);
  5599. }
  5600. }
  5601. }
  5602. //---------------------------------------------------------------------------
  5603. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5604. {
  5605. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5606. if (Side == osLocal)
  5607. {
  5608. if (Action == TSynchronizeChecklist::saDownloadNew)
  5609. {
  5610. OpenFolderInExplorer(LocalPath);
  5611. }
  5612. else
  5613. {
  5614. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5615. }
  5616. }
  5617. else if (DebugAlwaysTrue(Side == osRemote))
  5618. {
  5619. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5620. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5621. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5622. if (!LocalPath.IsEmpty())
  5623. {
  5624. SessionData->LocalDirectory = LocalPath;
  5625. }
  5626. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5627. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5628. }
  5629. }
  5630. //---------------------------------------------------------------------------
  5631. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5632. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5633. const TCopyParamType * CopyParams)
  5634. {
  5635. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5636. UnicodeString AdditionalParams =
  5637. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5638. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5639. SerializeCopyParamForCommandLine(CopyParams),
  5640. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5641. LocalDirectory, RemoteDirectory, Mode, Params));
  5642. ExecuteNewInstance(SessionName, AdditionalParams);
  5643. }
  5644. //---------------------------------------------------------------------------
  5645. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5646. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5647. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5648. {
  5649. int Result;
  5650. bool SaveSettings = false;
  5651. int Options =
  5652. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5653. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5654. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5655. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5656. bool Continue =
  5657. (UseDefaults == 0) ||
  5658. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5659. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5660. if (Continue)
  5661. {
  5662. Configuration->Usage->Inc(L"Synchronizations");
  5663. UpdateCopyParamCounters(CopyParam);
  5664. TSynchronizeOptions SynchronizeOptions;
  5665. GetSynchronizeOptions(Params, SynchronizeOptions);
  5666. if (SaveSettings)
  5667. {
  5668. GUIConfiguration->SynchronizeParams = Params;
  5669. }
  5670. else
  5671. {
  5672. SaveMode = false;
  5673. }
  5674. TDateTime StartTime = Now();
  5675. TSynchronizeChecklist * Checklist = NULL;
  5676. try
  5677. {
  5678. DebugAssert(!FAutoOperation);
  5679. FAutoOperation = true;
  5680. try
  5681. {
  5682. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5683. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5684. int Files = -1;
  5685. if (DataList->Count >= 1)
  5686. {
  5687. Files = StrToIntDef(DataList->Strings[0], Files);
  5688. }
  5689. else
  5690. {
  5691. DataList->Add(UnicodeString());
  5692. }
  5693. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5694. FSynchronizeProgressForm->Start();
  5695. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5696. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5697. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5698. &SynchronizeOptions);
  5699. if (Terminal->SessionData->CacheDirectories)
  5700. {
  5701. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5702. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5703. }
  5704. }
  5705. __finally
  5706. {
  5707. FAutoOperation = false;
  5708. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5709. SAFE_DESTROY(FSynchronizeProgressForm);
  5710. }
  5711. TSynchronizeParams SynchronizeParams;
  5712. SynchronizeParams.LocalDirectory = LocalDirectory;
  5713. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5714. SynchronizeParams.Mode = Mode;
  5715. SynchronizeParams.CopyParam = &CopyParam;
  5716. SynchronizeParams.Params = Params;
  5717. SynchronizeParams.Checklist = Checklist;
  5718. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5719. SynchronizeParams.StartTime = &StartTime;
  5720. SynchronizeParams.OnProcessedItem = NULL;
  5721. Result = Checklist->Count;
  5722. if (Checklist->Count > 0)
  5723. {
  5724. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5725. {
  5726. if (!DoSynchronizeChecklistDialog(
  5727. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5728. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5729. {
  5730. Result = -1;
  5731. }
  5732. }
  5733. else
  5734. {
  5735. FullSynchronize(SynchronizeParams, NULL, NULL);
  5736. }
  5737. }
  5738. else
  5739. {
  5740. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5741. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5742. }
  5743. }
  5744. __finally
  5745. {
  5746. delete Checklist;
  5747. }
  5748. OperationComplete(StartTime);
  5749. }
  5750. else
  5751. {
  5752. Result = -1;
  5753. }
  5754. return Result;
  5755. }
  5756. //---------------------------------------------------------------------------
  5757. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5758. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5759. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5760. {
  5761. if (Collect)
  5762. {
  5763. DebugAssert(FSynchronizeProgressForm != NULL);
  5764. if (!FSynchronizeProgressForm->Started)
  5765. {
  5766. FSynchronizeProgressForm->Start();
  5767. }
  5768. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5769. SetTaskbarListProgressValue(CompareProgress);
  5770. }
  5771. else
  5772. {
  5773. DebugAssert(FSynchronizeProgressForm == NULL);
  5774. DebugAssert(FProgressForm != NULL);
  5775. }
  5776. }
  5777. //---------------------------------------------------------------------------
  5778. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5779. {
  5780. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5781. TRemoteFile * File = NULL;
  5782. Terminal->ReadFile(FullFileName, File);
  5783. if (File != NULL)
  5784. {
  5785. std::unique_ptr<TRemoteFile> FileOwner(File);
  5786. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5787. Application->ShowMainForm = false;
  5788. Application->Run();
  5789. }
  5790. }
  5791. //---------------------------------------------------------------------------
  5792. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5793. {
  5794. DebugFail();
  5795. }
  5796. //---------------------------------------------------------------------------
  5797. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5798. {
  5799. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5800. SessionData->Assign(ManagedSession->SessionData);
  5801. UpdateSessionData(SessionData.get());
  5802. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5803. if (IsActiveTerminal(ManagedSession))
  5804. {
  5805. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5806. }
  5807. if (!ManagedSession->SessionData->HasSessionName())
  5808. {
  5809. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5810. // once IsWorkspace is cleared
  5811. SessionData->Name = UnicodeString();
  5812. }
  5813. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5814. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5815. SessionData->IsWorkspace = false;
  5816. return SessionData.release();
  5817. }
  5818. //---------------------------------------------------------------------------
  5819. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5820. {
  5821. TSessionData * SessionData = CloneCurrentSessionData();
  5822. try
  5823. {
  5824. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5825. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5826. }
  5827. __finally
  5828. {
  5829. delete SessionData;
  5830. }
  5831. }
  5832. //---------------------------------------------------------------------------
  5833. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5834. {
  5835. TTerminalManager * Manager = TTerminalManager::Instance();
  5836. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5837. if (DebugAlwaysTrue(ManagedSession != NULL))
  5838. {
  5839. // Update ManagedSession->StateData
  5840. UpdateSession(ManagedSession);
  5841. }
  5842. Manager->SaveWorkspace(DataList.get());
  5843. return DataList.release();
  5844. }
  5845. //---------------------------------------------------------------------------
  5846. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5847. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5848. {
  5849. WinConfiguration->LastWorkspace = Name;
  5850. if (!SavePasswords)
  5851. {
  5852. for (int Index = 0; Index < DataList->Count; Index++)
  5853. {
  5854. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5855. if (SessionData->Link.IsEmpty())
  5856. {
  5857. SessionData->ClearSessionPasswords();
  5858. }
  5859. }
  5860. }
  5861. StoredSessions->NewWorkspace(Name, DataList);
  5862. // modified only
  5863. StoredSessions->Save(false, Explicit);
  5864. }
  5865. //---------------------------------------------------------------------------
  5866. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5867. {
  5868. return
  5869. DefaultStr(WinConfiguration->LastWorkspace,
  5870. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5871. }
  5872. //---------------------------------------------------------------------------
  5873. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5874. {
  5875. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5876. bool AnyNonStoredSessionWithPassword = false;
  5877. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5878. bool AllNonStoredSessionsAnonymous = true;
  5879. TTerminalManager * Manager = TTerminalManager::Instance();
  5880. for (int Index = 0; Index < DataList->Count; Index++)
  5881. {
  5882. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5883. if (SessionData->Link.IsEmpty())
  5884. {
  5885. if (SessionData->HasAnySessionPassword())
  5886. {
  5887. AnyNonStoredSessionWithPassword = true;
  5888. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5889. {
  5890. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5891. }
  5892. }
  5893. if (!SameText(SessionData->UserName, AnonymousUserName))
  5894. {
  5895. AllNonStoredSessionsAnonymous = false;
  5896. }
  5897. }
  5898. }
  5899. bool SavePasswords;
  5900. bool * PSavePasswords;
  5901. bool NotRecommendedSavingPasswords =
  5902. !CustomWinConfiguration->UseMasterPassword &&
  5903. !AllNonStoredSessionsAnonymous;
  5904. if (Configuration->DisablePasswordStoring ||
  5905. !AnyNonStoredSessionWithPassword)
  5906. {
  5907. PSavePasswords = NULL;
  5908. SavePasswords = false;
  5909. }
  5910. else
  5911. {
  5912. PSavePasswords = &SavePasswords;
  5913. SavePasswords =
  5914. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5915. !NotRecommendedSavingPasswords;
  5916. }
  5917. UnicodeString Name = WorkspaceName();
  5918. bool CreateShortcut = false;
  5919. bool Result =
  5920. DoSaveWorkspaceDialog(
  5921. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5922. EnableAutoSave);
  5923. if (Result)
  5924. {
  5925. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5926. if (CreateShortcut)
  5927. {
  5928. TOperationVisualizer Visualizer;
  5929. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5930. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5931. }
  5932. if (EnableAutoSave)
  5933. {
  5934. WinConfiguration->AutoSaveWorkspace = true;
  5935. WinConfiguration->AutoWorkspace = Name;
  5936. if (PSavePasswords != NULL)
  5937. {
  5938. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5939. }
  5940. }
  5941. }
  5942. return Result;
  5943. }
  5944. //---------------------------------------------------------------------------
  5945. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  5946. {
  5947. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  5948. DebugAssert(Session == ManagedSession);
  5949. if (!IsLocalBrowserMode())
  5950. {
  5951. SAFE_DESTROY(Session->RemoteExplorerState);
  5952. if (WinConfiguration->PreservePanelState)
  5953. {
  5954. Session->RemoteExplorerState = RemoteDirView->SaveState();
  5955. }
  5956. }
  5957. UpdateSessionData(Session->StateData);
  5958. }
  5959. //---------------------------------------------------------------------------
  5960. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5961. {
  5962. // Keep in sync with TSessionData::CopyStateData
  5963. DebugAssert(Data != NULL);
  5964. // This is inconsistent with how color (for example) is handled.
  5965. Data->Name = ManagedSession->SessionData->Name;
  5966. if (!IsLocalBrowserMode())
  5967. {
  5968. // cannot use RemoteDirView->Path, because it is empty if connection
  5969. // was already closed
  5970. // also only peek, we may not be connected at all atm
  5971. // so make sure we do not try retrieving current directory from the server
  5972. // (particularly with FTP)
  5973. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5974. if (!ACurrentDirectory.IsEmpty())
  5975. {
  5976. Data->RemoteDirectory = ACurrentDirectory;
  5977. }
  5978. }
  5979. Data->Color = SessionColor;
  5980. }
  5981. //---------------------------------------------------------------------------
  5982. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5983. {
  5984. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5985. DebugAssert(Toolbar != NULL);
  5986. for (int i = 0; i < Toolbar->Items->Count; i++)
  5987. {
  5988. TTBXCustomDropDownItem * DropDownItem;
  5989. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5990. if (DropDownItem != NULL)
  5991. {
  5992. ToolbarItemResize(DropDownItem,
  5993. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5994. Toolbar->NonClientWidth);
  5995. break;
  5996. }
  5997. }
  5998. }
  5999. //---------------------------------------------------------------------------
  6000. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6001. {
  6002. Item->EditWidth = Width;
  6003. }
  6004. //---------------------------------------------------------------------------
  6005. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6006. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6007. {
  6008. for (int i = 0; i < Toolbar->Items->Count; i++)
  6009. {
  6010. TTBXCustomDropDownItem * DropDownItem;
  6011. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6012. if (DropDownItem != NULL)
  6013. {
  6014. ASize.x -= DropDownItem->EditWidth;
  6015. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6016. }
  6017. }
  6018. }
  6019. //---------------------------------------------------------------------------
  6020. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6021. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6022. {
  6023. if (WinConfiguration->DDWarnLackOfTempSpace)
  6024. {
  6025. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6026. if (!ADrive.IsEmpty())
  6027. {
  6028. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6029. DebugAssert(RequiredSpace >= 0);
  6030. __int64 RequiredWithReserve;
  6031. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6032. if (FreeSpace < RequiredWithReserve)
  6033. {
  6034. unsigned int Result;
  6035. TMessageParams Params(mpNeverAskAgainCheck);
  6036. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6037. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6038. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6039. if (Result == qaNeverAskAgain)
  6040. {
  6041. WinConfiguration->DDWarnLackOfTempSpace = false;
  6042. }
  6043. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6044. }
  6045. }
  6046. }
  6047. }
  6048. //---------------------------------------------------------------------------
  6049. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6050. {
  6051. DoOpenDirectoryDialog(odAddBookmark, Side);
  6052. }
  6053. //---------------------------------------------------------------------------
  6054. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6055. {
  6056. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6057. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6058. try
  6059. {
  6060. TCustomDirView * DView = DirView(Side);
  6061. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6062. {
  6063. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6064. }
  6065. }
  6066. catch (...)
  6067. {
  6068. delete VisitedDirectories;
  6069. throw;
  6070. }
  6071. return VisitedDirectories;
  6072. }
  6073. //---------------------------------------------------------------------------
  6074. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6075. TOpenDirectoryMode Mode, TOperationSide Side)
  6076. {
  6077. bool Continue = true;
  6078. if (Mode == odAddBookmark)
  6079. {
  6080. TMessageParams Params(mpNeverAskAgainCheck);
  6081. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6082. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6083. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6084. unsigned int Answer =
  6085. MessageDialog(Message,
  6086. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6087. if (Answer == qaNeverAskAgain)
  6088. {
  6089. Continue = true;
  6090. WinConfiguration->UseSharedBookmarks = true;
  6091. }
  6092. else if (Answer == qaOK)
  6093. {
  6094. Continue = true;
  6095. WinConfiguration->UseSharedBookmarks = false;
  6096. }
  6097. else
  6098. {
  6099. Continue = false;
  6100. }
  6101. }
  6102. if (Continue)
  6103. {
  6104. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6105. try
  6106. {
  6107. UnicodeString Name = DirView(Side)->PathName;
  6108. bool Repeat;
  6109. do
  6110. {
  6111. Repeat = false;
  6112. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6113. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6114. // do not allow switching to location profiles,
  6115. // if we are not connected
  6116. HasDirView[osLocal] && (Terminal != NULL)))
  6117. {
  6118. TWindowLock Lock(this);
  6119. if (!TryOpenDirectory(Side, Name))
  6120. {
  6121. Repeat = true;
  6122. Mode = odBrowse;
  6123. }
  6124. }
  6125. }
  6126. while (Repeat);
  6127. }
  6128. __finally
  6129. {
  6130. delete VisitedDirectories;
  6131. }
  6132. }
  6133. }
  6134. //---------------------------------------------------------------------------
  6135. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6136. {
  6137. DebugAssert(!Terminal->CommandSessionOpened);
  6138. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6139. }
  6140. //---------------------------------------------------------------------------
  6141. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6142. {
  6143. bool Result = Terminal->IsCapable[Capability] ||
  6144. Terminal->CommandSessionOpened;
  6145. if (!Result)
  6146. {
  6147. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6148. if (!GUIConfiguration->ConfirmCommandSession)
  6149. {
  6150. Result = true;
  6151. }
  6152. else
  6153. {
  6154. TMessageParams Params(mpNeverAskAgainCheck);
  6155. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6156. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6157. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6158. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6159. if (Answer == qaNeverAskAgain)
  6160. {
  6161. GUIConfiguration->ConfirmCommandSession = false;
  6162. Result = true;
  6163. }
  6164. else if (Answer == qaOK)
  6165. {
  6166. Result = true;
  6167. }
  6168. }
  6169. if (Result)
  6170. {
  6171. Result = CommandSessionFallback();
  6172. }
  6173. }
  6174. return Result;
  6175. }
  6176. //---------------------------------------------------------------------------
  6177. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6178. {
  6179. if (EnsureCommandSessionFallback(fcAnyCommand))
  6180. {
  6181. DoConsoleDialog(Terminal, Command);
  6182. }
  6183. }
  6184. //---------------------------------------------------------------------------
  6185. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6186. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6187. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6188. {
  6189. if (!IsSideLocalBrowser(osRemote) &&
  6190. IsFileControl(DropSourceControl, osRemote) &&
  6191. (FDDExtMapFile != NULL))
  6192. {
  6193. Accept = true;
  6194. }
  6195. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6196. DebugAssert(FDDTargetControl != NULL);
  6197. }
  6198. //---------------------------------------------------------------------------
  6199. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6200. _di_IDataObject DataObj, int KeyState,
  6201. const TPoint & Point, int & Effect, bool & Accept)
  6202. {
  6203. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6204. }
  6205. //---------------------------------------------------------------------------
  6206. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6207. _di_IDataObject DataObj, int KeyState,
  6208. const TPoint & Point, int & Effect, bool & Accept)
  6209. {
  6210. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6211. }
  6212. //---------------------------------------------------------------------------
  6213. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6214. TObject * Sender)
  6215. {
  6216. DebugUsedParam(Sender);
  6217. DebugAssert(FDDTargetControl == Sender);
  6218. FDDTargetControl = NULL;
  6219. }
  6220. //---------------------------------------------------------------------------
  6221. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6222. {
  6223. FileControlDDDragLeave(SessionsPageControl);
  6224. }
  6225. //---------------------------------------------------------------------------
  6226. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6227. {
  6228. FileControlDDDragLeave(QueueView3);
  6229. }
  6230. //---------------------------------------------------------------------------
  6231. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6232. {
  6233. DebugAssert(!IsSideLocalBrowser(Side));
  6234. DebugUsedParam(Side);
  6235. bool Edit = false;
  6236. TRemoteFile * File = NULL;
  6237. UnicodeString FileName;
  6238. UnicodeString PointTo;
  6239. bool SymbolicLink = true;
  6240. if (RemoteDirView->ItemFocused)
  6241. {
  6242. DebugAssert(RemoteDirView->ItemFocused->Data);
  6243. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6244. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6245. if (Edit)
  6246. {
  6247. FileName = File->FileName;
  6248. PointTo = File->LinkTo;
  6249. }
  6250. else
  6251. {
  6252. PointTo = File->FileName;
  6253. }
  6254. }
  6255. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6256. Terminal->IsCapable[fcHardLink]))
  6257. {
  6258. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6259. if (Edit)
  6260. {
  6261. DebugAssert(File->FileName == FileName);
  6262. int Params = dfNoRecursive;
  6263. Terminal->ExceptionOnFail = true;
  6264. try
  6265. {
  6266. Terminal->DeleteFile(L"", File, &Params);
  6267. }
  6268. __finally
  6269. {
  6270. Terminal->ExceptionOnFail = false;
  6271. }
  6272. }
  6273. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6274. }
  6275. }
  6276. //---------------------------------------------------------------------------
  6277. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6278. {
  6279. return
  6280. (IsSideLocalBrowser(Side) ||
  6281. (HasActiveTerminal() &&
  6282. Terminal->ResolvingSymlinks &&
  6283. Terminal->IsCapable[fcSymbolicLink]));
  6284. }
  6285. //---------------------------------------------------------------------------
  6286. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6287. {
  6288. return
  6289. !IsSideLocalBrowser(FCurrentSide) &&
  6290. (RemoteDirView->ItemFocused != NULL) &&
  6291. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6292. Terminal->SessionData->ResolveSymlinks;
  6293. }
  6294. //---------------------------------------------------------------------------
  6295. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6296. {
  6297. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6298. FForceExecution = true;
  6299. try
  6300. {
  6301. DirView(osCurrent)->ExecuteCurrentFile();
  6302. }
  6303. __finally
  6304. {
  6305. FForceExecution = false;
  6306. }
  6307. }
  6308. //---------------------------------------------------------------------------
  6309. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6310. {
  6311. TEditorData ExternalEditor;
  6312. ExternalEditor.Editor = edExternal;
  6313. bool Remember = false;
  6314. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6315. {
  6316. if (Remember)
  6317. {
  6318. TEditorList * EditorList = new TEditorList;
  6319. try
  6320. {
  6321. *EditorList = *WinConfiguration->EditorList;
  6322. bool Found = false;
  6323. int i = 0;
  6324. while (!Found && (i < EditorList->Count))
  6325. {
  6326. const TEditorPreferences * Editor = EditorList->Editors[i];
  6327. if ((Editor->Data->Editor == edExternal) &&
  6328. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6329. {
  6330. Found = true;
  6331. }
  6332. i++;
  6333. }
  6334. if (!Found)
  6335. {
  6336. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6337. WinConfiguration->EditorList = EditorList;
  6338. }
  6339. }
  6340. __finally
  6341. {
  6342. delete EditorList;
  6343. }
  6344. }
  6345. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6346. }
  6347. }
  6348. //---------------------------------------------------------------------------
  6349. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6350. {
  6351. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6352. if (FFileFindTerminal == ATerminal)
  6353. {
  6354. FFileFindTerminal = NULL;
  6355. HideFileFindDialog();
  6356. }
  6357. if (FClipboardTerminal == ATerminal)
  6358. {
  6359. ClipboardClear(); // implies ClipboardStop
  6360. }
  6361. if ((FOnSynchronizeAbort != NULL) &&
  6362. (ATerminal == FManagedSession))
  6363. {
  6364. FOnSynchronizeAbort(NULL);
  6365. }
  6366. }
  6367. //---------------------------------------------------------------------------
  6368. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6369. {
  6370. DetachTerminal(Sender);
  6371. }
  6372. //---------------------------------------------------------------------------
  6373. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6374. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6375. {
  6376. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6377. DebugAssert(Terminal != NULL);
  6378. if (Data->Terminal == Terminal)
  6379. {
  6380. Data->Terminal = NULL;
  6381. }
  6382. }
  6383. //---------------------------------------------------------------------------
  6384. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6385. {
  6386. return NULL;
  6387. }
  6388. //---------------------------------------------------------------------------
  6389. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6390. {
  6391. UpdateControls();
  6392. SessionColor = TColor(0);
  6393. UpdateRemotePathComboBox(false);
  6394. NeedSession(false);
  6395. }
  6396. //---------------------------------------------------------------------------
  6397. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6398. {
  6399. try
  6400. {
  6401. // Cache, as the login dialog can change its value.
  6402. // See also DoShow. Also see TTerminalManager::NewSession.
  6403. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6404. try
  6405. {
  6406. if (ShowLogin)
  6407. {
  6408. bool ReloadSessions = !Startup;
  6409. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6410. }
  6411. }
  6412. __finally
  6413. {
  6414. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6415. // (so there was no chance for the user to open any session yet)
  6416. if ((ShowLogin || !Startup) &&
  6417. !WinConfiguration->KeepOpenWhenNoSession &&
  6418. // this rules out the implicit Commander's local-local browser
  6419. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6420. {
  6421. TerminateApplication();
  6422. }
  6423. }
  6424. }
  6425. catch (EAbort &)
  6426. {
  6427. // swallow
  6428. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6429. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6430. }
  6431. }
  6432. //---------------------------------------------------------------------------
  6433. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6434. {
  6435. TTerminalManager * Manager = TTerminalManager::Instance();
  6436. DebugAssert(!Manager->Updating);
  6437. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6438. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6439. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6440. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6441. try
  6442. {
  6443. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6444. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6445. {
  6446. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6447. }
  6448. for (int Index = 0; Index <= Manager->Count; Index++)
  6449. {
  6450. TThemeTabSheet * TabSheet;
  6451. if (Index >= SessionsPageControl->PageCount)
  6452. {
  6453. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6454. TabSheet->PageControl = SessionsPageControl;
  6455. }
  6456. else
  6457. {
  6458. TabSheet = SessionsPageControl->Pages[Index];
  6459. }
  6460. bool IsSessionTab = (Index < Manager->Count);
  6461. if (IsSessionTab)
  6462. {
  6463. TTerminal * Terminal = Manager->Sessions[Index];
  6464. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6465. TabSheet->CaptionTruncation = tttEllipsis;
  6466. UpdateSessionTab(TabSheet);
  6467. }
  6468. else
  6469. {
  6470. TabSheet->Tag = 0; // not really needed
  6471. TabSheet->Shadowed = false;
  6472. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6473. TabSheet->CaptionTruncation = tttNoText;
  6474. // 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)
  6475. UpdateNewTabTab();
  6476. }
  6477. }
  6478. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6479. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6480. {
  6481. SessionsPageControl->UpdateTabsCaptionTruncation();
  6482. }
  6483. }
  6484. __finally
  6485. {
  6486. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6487. }
  6488. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6489. }
  6490. //---------------------------------------------------------------------------
  6491. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6492. {
  6493. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6494. }
  6495. //---------------------------------------------------------------------------
  6496. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6497. {
  6498. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6499. UnicodeString TabCaption;
  6500. if (WinConfiguration->SelectiveToolbarText)
  6501. {
  6502. TabCaption = GetNewTabTabCaption();
  6503. }
  6504. TabSheet->BaseCaption = TabCaption;
  6505. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6506. }
  6507. //---------------------------------------------------------------------------
  6508. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6509. {
  6510. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6511. }
  6512. //---------------------------------------------------------------------------
  6513. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6514. {
  6515. DebugFail();
  6516. return UnicodeString();
  6517. }
  6518. //---------------------------------------------------------------------------
  6519. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6520. {
  6521. if (DebugAlwaysTrue(TabSheet != NULL))
  6522. {
  6523. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6524. if (DebugAlwaysTrue(ASession != NULL))
  6525. {
  6526. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6527. if (ASession->LocalBrowser)
  6528. {
  6529. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6530. }
  6531. else
  6532. {
  6533. TabSheet->ImageIndex = AddSessionColor(Color);
  6534. }
  6535. TTerminalManager * Manager = TTerminalManager::Instance();
  6536. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6537. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6538. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6539. }
  6540. }
  6541. }
  6542. //---------------------------------------------------------------------------
  6543. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6544. {
  6545. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6546. }
  6547. //---------------------------------------------------------------------------
  6548. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6549. {
  6550. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6551. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6552. bool Result = (Session != NULL);
  6553. if (Result)
  6554. {
  6555. TTerminalManager::Instance()->ActiveSession = Session;
  6556. }
  6557. else
  6558. {
  6559. try
  6560. {
  6561. NewTab();
  6562. }
  6563. __finally
  6564. {
  6565. SessionListChanged();
  6566. }
  6567. FSessionsPageControlNewTabTime = Now();
  6568. }
  6569. return Result;
  6570. }
  6571. //---------------------------------------------------------------------------
  6572. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6573. {
  6574. SessionTabSwitched();
  6575. }
  6576. //---------------------------------------------------------------------------
  6577. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6578. {
  6579. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6580. DebugAssert(TransferList != NULL);
  6581. UnicodeString Name;
  6582. if (TransferList->ItemIndex <= 0)
  6583. {
  6584. Name = L"";
  6585. }
  6586. else
  6587. {
  6588. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6589. }
  6590. if (FCopyParamAutoSelected.IsEmpty())
  6591. {
  6592. // if previous preset was not autoselected, make new preset the "default"
  6593. FCopyParamDefault = Name;
  6594. }
  6595. GUIConfiguration->CopyParamCurrent = Name;
  6596. }
  6597. //---------------------------------------------------------------------------
  6598. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6599. {
  6600. // sanity check
  6601. bool ExistingPreset =
  6602. (FTransferListHoverIndex >= 0) &&
  6603. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6604. DebugAssert(ExistingPreset);
  6605. if (ExistingPreset)
  6606. {
  6607. HDC DC = GetDC(0);
  6608. TCanvas * Canvas = new TCanvas();
  6609. try
  6610. {
  6611. Canvas->Handle = DC;
  6612. Canvas->Font = GetToolbarFont(this);
  6613. UnicodeString Name;
  6614. if (FTransferListHoverIndex == 0)
  6615. {
  6616. Name = L"";
  6617. }
  6618. else
  6619. {
  6620. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6621. }
  6622. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6623. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6624. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6625. static_cast<TObject*>(GetComponent(fcTransferList)));
  6626. UnicodeString InfoStr =
  6627. GUIConfiguration->CopyParamPreset[Name].
  6628. GetInfoStr(L"; ",
  6629. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6630. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6631. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6632. {
  6633. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6634. {
  6635. InfoStr.SetLength(InfoStr.Length() - 1);
  6636. }
  6637. InfoStr += Ellipsis;
  6638. }
  6639. // UpdateCaption does not cause invalidation of whole submenu, while
  6640. // setting Caption property does.
  6641. // also it probably does not resize the label, even if necessary
  6642. // (we do not want that anyway)
  6643. TransferLabel->UpdateCaption(InfoStr);
  6644. }
  6645. __finally
  6646. {
  6647. Canvas->Handle = NULL;
  6648. ReleaseDC(0, DC);
  6649. delete Canvas;
  6650. }
  6651. }
  6652. }
  6653. //---------------------------------------------------------------------------
  6654. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6655. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6656. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6657. {
  6658. if (FTransferListHoverIndex != AHoverIndex)
  6659. {
  6660. FTransferListHoverIndex = AHoverIndex;
  6661. UpdateTransferLabel();
  6662. }
  6663. }
  6664. //---------------------------------------------------------------------------
  6665. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6666. {
  6667. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6668. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6669. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6670. {
  6671. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6672. {
  6673. if (!NonVisualDataModule->Busy)
  6674. {
  6675. OpenDirectory(GetSide(osCurrent));
  6676. }
  6677. Message.Result = 1;
  6678. }
  6679. else
  6680. {
  6681. TForm::Dispatch(&Message);
  6682. }
  6683. }
  6684. else
  6685. {
  6686. TForm::Dispatch(&Message);
  6687. }
  6688. }
  6689. //---------------------------------------------------------------------------
  6690. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6691. {
  6692. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6693. if ((FIgnoreNextDialogChar != 0) &&
  6694. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6695. {
  6696. Message.Result = 1;
  6697. }
  6698. else
  6699. {
  6700. TForm::Dispatch(&Message);
  6701. }
  6702. FIgnoreNextDialogChar = 0;
  6703. }
  6704. //---------------------------------------------------------------------------
  6705. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6706. {
  6707. // The four low-order bits are used internally by Windows
  6708. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6709. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6710. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6711. // 61730 - restore thru double click - undocumented
  6712. // 61490 - maximize thru double click - undocumented
  6713. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6714. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6715. {
  6716. SysResizing(Cmd);
  6717. }
  6718. TForm::Dispatch(&Message);
  6719. }
  6720. //---------------------------------------------------------------------------
  6721. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6722. {
  6723. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6724. // Also there no point returning TRUE as we are not able to
  6725. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6726. // Hence the process termination might be safer :)
  6727. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6728. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6729. {
  6730. Message.Result = FALSE;
  6731. }
  6732. else
  6733. {
  6734. Message.Result = TRUE;
  6735. }
  6736. // Do not call default handling as that triggers OnCloseQuery,
  6737. // where our implementation will popup confirmation dialogs, what we do not want,
  6738. // as per Vista guidelines:
  6739. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6740. }
  6741. //---------------------------------------------------------------------------
  6742. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6743. {
  6744. if (Message.EndSession && IsApplicationMinimized())
  6745. {
  6746. // WORKAROUND
  6747. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6748. // when it receives WM_ENDSESSION,
  6749. // but that sometimes (particularly when application is minimized) cause crashes.
  6750. // Crash popups message that blocks log off.
  6751. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6752. // so we call ExitProcess() immediately, not even trying to cleanup.
  6753. // It still causes beep, so there's likely some popup, but it does not block
  6754. // log off.
  6755. ExitProcess(0);
  6756. }
  6757. TForm::Dispatch(&Message);
  6758. }
  6759. #ifdef _DEBUG
  6760. //---------------------------------------------------------------------------
  6761. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6762. {
  6763. TForm::Dispatch(&Message);
  6764. }
  6765. #endif
  6766. //---------------------------------------------------------------------------
  6767. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6768. {
  6769. }
  6770. //---------------------------------------------------------------------------
  6771. void __fastcall TCustomScpExplorerForm::DoShow()
  6772. {
  6773. // Called from TCustomScpExplorerForm::CMShowingChanged
  6774. // only now are the controls resized finally, so the size constraints
  6775. // will not conflict with possibly very small window size
  6776. RestoreFormParams();
  6777. // This indicates that the form was rescaled after parameters were loaded and
  6778. // as most probably the parameters were actually saved with the current form scaling
  6779. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6780. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6781. // and no rounding problems will occur.
  6782. // See also RestoreForm.
  6783. // (we should be safe to call RestoreParams unconditionally)
  6784. if (PixelsPerInch != Screen->PixelsPerInch)
  6785. {
  6786. RestoreParams();
  6787. }
  6788. FixControlsPlacement();
  6789. if (Position == poDefaultPosOnly)
  6790. {
  6791. CutFormToDesktop(this);
  6792. }
  6793. TForm::DoShow();
  6794. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6795. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6796. // This was previously in NeedSession, where other related code is.
  6797. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6798. // unpleasantly shows briefly.
  6799. if ((ManagedSession == NULL) &&
  6800. !WinConfiguration->ShowLoginWhenNoSession &&
  6801. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6802. // This detects if workspace was saved the last time the main widow was closed
  6803. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6804. {
  6805. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6806. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6807. }
  6808. // Before main window displays
  6809. if (ManagedSession == NULL)
  6810. {
  6811. StartingWithoutSession();
  6812. }
  6813. UpdatePixelsPerInchMainWindowCounter();
  6814. FShowing = true;
  6815. }
  6816. //---------------------------------------------------------------------------
  6817. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6818. {
  6819. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6820. }
  6821. //---------------------------------------------------------------------------
  6822. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6823. {
  6824. SessionListChanged();
  6825. InitStatusBar();
  6826. UpdateControls();
  6827. }
  6828. //---------------------------------------------------------------------------
  6829. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6830. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6831. TNotifyEvent OnBalloonClick, TObject * UserData)
  6832. {
  6833. bool Do;
  6834. UnicodeString Message;
  6835. if (E == NULL)
  6836. {
  6837. Message = Str;
  6838. Do = true;
  6839. }
  6840. else
  6841. {
  6842. Do = ExceptionMessage(E, Message);
  6843. }
  6844. if (Do && WinConfiguration->BalloonNotifications)
  6845. {
  6846. UnicodeString Title;
  6847. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6848. {
  6849. Message = TrimLeft(Message);
  6850. }
  6851. else
  6852. {
  6853. Message = UnformatMessage(Message);
  6854. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6855. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6856. Title = LoadResourceString(Captions[Type]);
  6857. if (Terminal != NULL)
  6858. {
  6859. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6860. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6861. }
  6862. }
  6863. if (Seconds == 0)
  6864. {
  6865. Seconds = WinConfiguration->NotificationsTimeout;
  6866. }
  6867. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6868. }
  6869. }
  6870. //---------------------------------------------------------------------------
  6871. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6872. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6873. UnicodeString HelpKeyword, const TMessageParams * Params,
  6874. TTerminal * Terminal)
  6875. {
  6876. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6877. (FOnFeedSynchronizeError != NULL)) &&
  6878. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6879. {
  6880. if (FOnFeedSynchronizeError != NULL)
  6881. {
  6882. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6883. }
  6884. else
  6885. {
  6886. DebugAssert(FErrorList != NULL);
  6887. TStringList * MoreMessagesCopy = NULL;
  6888. if (MoreMessages)
  6889. {
  6890. MoreMessagesCopy = new TStringList();
  6891. MoreMessagesCopy->Assign(MoreMessages);
  6892. }
  6893. FErrorList->AddObject(Message, MoreMessagesCopy);
  6894. }
  6895. PopupTrayBalloon(Terminal, Message, Type);
  6896. return ContinueAnswer(Answers);
  6897. }
  6898. else
  6899. {
  6900. bool UseBalloon = FTrayIcon->Visible;
  6901. if (UseBalloon)
  6902. {
  6903. PopupTrayBalloon(Terminal, Message, Type);
  6904. }
  6905. unsigned int Result;
  6906. try
  6907. {
  6908. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6909. }
  6910. __finally
  6911. {
  6912. // cancel only balloon we popped up, otherwise we may cancel notification
  6913. // balloon that was there before the message dialog
  6914. // (such as "dd confirmation opt in balloon)
  6915. if (UseBalloon)
  6916. {
  6917. FTrayIcon->CancelBalloon();
  6918. }
  6919. }
  6920. return Result;
  6921. }
  6922. }
  6923. //---------------------------------------------------------------------------
  6924. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6925. TTerminal * Terminal, Exception * E)
  6926. {
  6927. if (FTrayIcon->Visible)
  6928. {
  6929. PopupTrayBalloon(Terminal, L"", qtError, E);
  6930. }
  6931. // particularly prevent opening new session from jump list,
  6932. // while exception is shown
  6933. NonVisualDataModule->StartBusy();
  6934. try
  6935. {
  6936. ShowExtendedExceptionEx(Terminal, E);
  6937. }
  6938. __finally
  6939. {
  6940. NonVisualDataModule->EndBusy();
  6941. FTrayIcon->CancelBalloon();
  6942. }
  6943. }
  6944. //---------------------------------------------------------------------------
  6945. void __fastcall TCustomScpExplorerForm::SessionReady()
  6946. {
  6947. InitStatusBar();
  6948. // cannot rely on active page being page for active terminal,
  6949. // as it can happen that active page is the "new session" page
  6950. // (e.g. when reconnecting active terminal, while login dialog
  6951. // invoked from "new session" page is modal)
  6952. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  6953. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  6954. }
  6955. //---------------------------------------------------------------------------
  6956. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6957. TTerminal * Terminal, Exception * E)
  6958. {
  6959. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6960. if (!Terminal->Active)
  6961. {
  6962. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6963. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6964. {
  6965. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6966. }
  6967. }
  6968. }
  6969. //---------------------------------------------------------------------------
  6970. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6971. UnicodeString Message, TQueryType Type,
  6972. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6973. {
  6974. if ((E == NULL) ||
  6975. ExceptionMessage(E, Message))
  6976. {
  6977. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6978. if (Important)
  6979. {
  6980. Seconds *= 5;
  6981. }
  6982. UnicodeString NoteMessage(UnformatMessage(Message));
  6983. if (Terminal != NULL)
  6984. {
  6985. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6986. NoteMessage = FORMAT(L"%s: %s",
  6987. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  6988. }
  6989. if (WinConfiguration->BalloonNotifications)
  6990. {
  6991. AddNote(NoteMessage);
  6992. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6993. }
  6994. else
  6995. {
  6996. FlashOnBackground();
  6997. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6998. }
  6999. }
  7000. }
  7001. //---------------------------------------------------------------------------
  7002. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7003. {
  7004. RestoreApp();
  7005. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7006. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7007. !NonVisualDataModule->Busy)
  7008. {
  7009. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7010. TTerminalManager::Instance()->ActiveSession = Terminal;
  7011. if (!ComponentVisible[fcQueueView])
  7012. {
  7013. ToggleQueueVisibility();
  7014. GoToQueue();
  7015. }
  7016. }
  7017. }
  7018. //---------------------------------------------------------------------------
  7019. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7020. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7021. {
  7022. UnicodeString Message;
  7023. TNotifyEvent OnClick = NULL;
  7024. TObject * UserData = NULL;
  7025. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7026. switch (Event)
  7027. {
  7028. case qeEmptyButMonitored:
  7029. if ((ATerminal != Terminal) || QueueInvisible)
  7030. {
  7031. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7032. OnClick = QueueEmptyNoteClicked;
  7033. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7034. TerminalNoteData->Terminal = ATerminal;
  7035. UserData = TerminalNoteData;
  7036. }
  7037. break;
  7038. case qeEmpty:
  7039. OperationComplete(ATerminal->QueueOperationStart);
  7040. break;
  7041. case qePendingUserAction:
  7042. if ((ATerminal != Terminal) ||
  7043. (QueueInvisible && !IsQueueAutoPopup()))
  7044. {
  7045. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7046. }
  7047. break;
  7048. default:
  7049. DebugFail();
  7050. }
  7051. if (!Message.IsEmpty())
  7052. {
  7053. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7054. }
  7055. }
  7056. //---------------------------------------------------------------------------
  7057. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7058. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7059. {
  7060. if (FDDExtMapFile != NULL)
  7061. {
  7062. CloseHandle(FDDExtMapFile);
  7063. FDDExtMapFile = NULL;
  7064. }
  7065. if (WinConfiguration->DDFakeFile)
  7066. {
  7067. DDFakeFileInitDrag(FileList, Created);
  7068. }
  7069. }
  7070. //---------------------------------------------------------------------------
  7071. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7072. {
  7073. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7074. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7075. {
  7076. FFakeFileDropTarget = FileName;
  7077. }
  7078. }
  7079. //---------------------------------------------------------------------------
  7080. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7081. {
  7082. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7083. if (!ForceDirectories(ApiPath(Result)))
  7084. {
  7085. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7086. }
  7087. FileSetAttr(ApiPath(Result), faHidden);
  7088. return Result;
  7089. }
  7090. //---------------------------------------------------------------------------
  7091. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7092. bool & Created)
  7093. {
  7094. FFakeFileDropTarget = UnicodeString();
  7095. FDragFakeDirectory = CreateFakeTransferDirectory();
  7096. FileList->AddItem(NULL, FDragFakeDirectory);
  7097. Created = true;
  7098. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7099. {
  7100. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7101. }
  7102. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7103. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7104. {
  7105. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7106. TDragExtCommStruct* CommStruct;
  7107. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7108. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7109. DebugAssert(CommStruct != NULL);
  7110. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7111. CommStruct->Dragging = true;
  7112. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7113. LENOF(CommStruct->DropDest));
  7114. NULL_TERMINATE(CommStruct->DropDest);
  7115. UnmapViewOfFile(CommStruct);
  7116. }
  7117. }
  7118. //---------------------------------------------------------------------------
  7119. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7120. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7121. {
  7122. bool Result;
  7123. DebugAssert(!IsLocalBrowserMode());
  7124. if (Sender == RemoteDirView)
  7125. {
  7126. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7127. }
  7128. else
  7129. {
  7130. DebugAssert(Sender == RemoteDriveView);
  7131. TStrings * FileList = RemoteDriveView->DragFileList();
  7132. try
  7133. {
  7134. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7135. }
  7136. __finally
  7137. {
  7138. delete FileList;
  7139. }
  7140. }
  7141. if (FDDTargetControl == RemoteDriveView)
  7142. {
  7143. RemoteDriveView->UpdateDropTarget();
  7144. }
  7145. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7146. // or from tree view to dir view.
  7147. // foMove happens when file/dir is dragged from remote tree view outside of
  7148. // application via dragex
  7149. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7150. (DropSourceControl == RemoteDriveView))
  7151. {
  7152. RemoteDriveView->UpdateDropSource();
  7153. }
  7154. return Result;
  7155. }
  7156. //---------------------------------------------------------------------------
  7157. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7158. {
  7159. DebugAssert(!IsLocalBrowserMode());
  7160. // This also handles drops of remote files to queue.
  7161. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7162. SAFE_DESTROY(FDDFileList);
  7163. if (!FFakeFileDropTarget.IsEmpty())
  7164. {
  7165. RemoveDir(ApiPath(FFakeFileDropTarget));
  7166. }
  7167. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7168. {
  7169. try
  7170. {
  7171. TDragResult DDResult = (Sender == RemoteDirView) ?
  7172. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7173. // Focus is moved to the target application,
  7174. // but as we are going to present the UI, we need to steal the focus back.
  7175. // This most likely won't work though (windows does not allow application
  7176. // to steal focus most of the time)
  7177. Application->BringToFront();
  7178. // On older version of Windows we never got drMove here, see also comment below.
  7179. // On Windows 10, we get the "move".
  7180. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7181. {
  7182. UnicodeString TargetDirectory;
  7183. TFileOperation Operation;
  7184. // drInvalid may mean drMove, see comment below
  7185. switch (DDResult)
  7186. {
  7187. case drCopy:
  7188. Operation = foCopy;
  7189. break;
  7190. case drMove:
  7191. Operation = foMove;
  7192. break;
  7193. default:
  7194. DebugFail();
  7195. case drInvalid:
  7196. // prefer "copy" for safety
  7197. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7198. break;
  7199. }
  7200. TTransferOperationParam Param;
  7201. UnicodeString CounterName;
  7202. bool ForceQueue;
  7203. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7204. {
  7205. // we get drInvalid both if d&d was intercepted by ddext,
  7206. // and when users drops on no-drop location.
  7207. // we tell the difference by existence of response from ddext,
  7208. // so we ignore absence of response in this case
  7209. if (DDResult != drInvalid)
  7210. {
  7211. // here we know that the extension is installed,
  7212. // as it is checked as soon as drag&drop starts from
  7213. // RemoteFileControlDDCreateDragFileList
  7214. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7215. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7216. {
  7217. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7218. }
  7219. else
  7220. {
  7221. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7222. }
  7223. }
  7224. }
  7225. else
  7226. {
  7227. // download using fake file
  7228. Param.Temp = false;
  7229. Param.DragDrop = true;
  7230. if (ForceQueue)
  7231. {
  7232. Param.Queue = asOn;
  7233. }
  7234. if (Sender == RemoteDirView)
  7235. {
  7236. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7237. }
  7238. if (RemoteFileControlFileOperation(Sender, Operation,
  7239. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7240. {
  7241. Configuration->Usage->Inc(CounterName);
  7242. }
  7243. }
  7244. }
  7245. }
  7246. __finally
  7247. {
  7248. CloseHandle(FDDExtMapFile);
  7249. FDDExtMapFile = NULL;
  7250. delete FDragFakeMonitors;
  7251. FDragFakeMonitors = NULL;
  7252. RemoveDir(ApiPath(FDragFakeDirectory));
  7253. FDragFakeDirectory = L"";
  7254. FFakeFileDropTarget = UnicodeString();
  7255. }
  7256. }
  7257. if (FDragDropSshTerminate.get() != NULL)
  7258. {
  7259. FDragDropSshTerminate->Rethrow();
  7260. }
  7261. }
  7262. //---------------------------------------------------------------------------
  7263. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7264. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7265. {
  7266. // Remember drop effect so we know (when user drops files), if we copy or move
  7267. FLastDropEffect = dwEffect;
  7268. }
  7269. //---------------------------------------------------------------------------
  7270. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7271. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7272. {
  7273. bool Result;
  7274. if (FDDTargetControl == QueueView3)
  7275. {
  7276. Directory = DefaultDownloadTargetDirectory();
  7277. Result = true;
  7278. CounterName = L"DownloadsDragDropQueue";
  7279. ForceQueue = true;
  7280. }
  7281. else if (!FFakeFileDropTarget.IsEmpty())
  7282. {
  7283. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7284. Result = true;
  7285. ForceQueue = false;
  7286. CounterName = L"DownloadsDragDropFakeFile";
  7287. }
  7288. else
  7289. {
  7290. ForceQueue = false;
  7291. Enabled = false;
  7292. try
  7293. {
  7294. int Timer = 0;
  7295. Result = false;
  7296. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7297. {
  7298. {
  7299. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7300. TDragExtCommStruct* CommStruct;
  7301. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7302. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7303. DebugAssert(CommStruct != NULL);
  7304. Result = !CommStruct->Dragging;
  7305. if (Result)
  7306. {
  7307. Directory = ExtractFilePath(CommStruct->DropDest);
  7308. CounterName = L"DownloadsDragDropExternalExt";
  7309. }
  7310. UnmapViewOfFile(CommStruct);
  7311. }
  7312. if (!Result)
  7313. {
  7314. Sleep(50);
  7315. Timer += 50;
  7316. Application->ProcessMessages();
  7317. }
  7318. }
  7319. }
  7320. __finally
  7321. {
  7322. Enabled = true;
  7323. }
  7324. }
  7325. return Result;
  7326. }
  7327. //---------------------------------------------------------------------------
  7328. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7329. const UnicodeString TempDir, int SecDelay)
  7330. {
  7331. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7332. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7333. if (FDelayedDeletionTimer == NULL)
  7334. {
  7335. DebugAssert(HandleAllocated());
  7336. FDelayedDeletionTimer = new TTimer(this);
  7337. FDelayedDeletionTimer->Interval = 10000;
  7338. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7339. }
  7340. else
  7341. {
  7342. FDelayedDeletionTimer->Enabled = true;
  7343. }
  7344. }
  7345. //---------------------------------------------------------------------------
  7346. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7347. {
  7348. DebugAssert(FDelayedDeletionList != NULL);
  7349. TDateTime N = Now();
  7350. TDateTime Alert;
  7351. UnicodeString Directory;
  7352. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7353. {
  7354. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7355. if ((N >= Alert) || (Sender == NULL))
  7356. {
  7357. Directory = FDelayedDeletionList->Strings[Index];
  7358. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7359. {
  7360. FDelayedDeletionList->Delete(Index);
  7361. }
  7362. }
  7363. }
  7364. if (FDelayedDeletionList->Count == 0)
  7365. {
  7366. FDelayedDeletionTimer->Enabled = false;
  7367. }
  7368. }
  7369. //---------------------------------------------------------------------------
  7370. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7371. {
  7372. DebugAssert(!IsLocalBrowserMode());
  7373. if (IsFileControl(FDDTargetControl, osRemote) ||
  7374. (FDDTargetControl == SessionsPageControl))
  7375. {
  7376. TManagedTerminal * TargetTerminal = NULL;
  7377. TFileOperation Operation = foNone;
  7378. if (FDDTargetControl == SessionsPageControl)
  7379. {
  7380. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7381. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7382. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7383. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7384. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7385. {
  7386. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7387. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7388. {
  7389. Operation = foRemoteMove;
  7390. }
  7391. else
  7392. {
  7393. Operation = foRemoteCopy;
  7394. }
  7395. }
  7396. }
  7397. else
  7398. {
  7399. if (FLastDropEffect == DROPEFFECT_MOVE)
  7400. {
  7401. Operation = foRemoteMove;
  7402. }
  7403. else if (FLastDropEffect == DROPEFFECT_COPY)
  7404. {
  7405. Operation = foRemoteCopy;
  7406. }
  7407. }
  7408. if (Operation != foNone)
  7409. {
  7410. RemoteFileControlFileOperation(DropSourceControl,
  7411. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7412. }
  7413. // abort drag&drop
  7414. Abort();
  7415. }
  7416. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7417. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7418. (FDDTargetControl != QueueView3))
  7419. {
  7420. DebugAssert(!FDragTempDir.IsEmpty());
  7421. TTransferType Type;
  7422. UnicodeString TempDir = FDragTempDir;
  7423. // We clear FDragTempDir before calling
  7424. // just in case it fail (raises exception)
  7425. FDragTempDir = L"";
  7426. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7427. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7428. // empty directory parameter means temp directory -> don't display it!
  7429. UnicodeString TargetDir = L"";
  7430. int Options = 0;
  7431. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7432. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7433. {
  7434. Abort();
  7435. }
  7436. // TargetDir is set when dropped on local file control
  7437. // (this was workaround for legacy dirview event handling, now it should be
  7438. // made prettier)
  7439. if (TargetDir.IsEmpty())
  7440. {
  7441. TargetDir = TempDir;
  7442. if (ForceDirectories(ApiPath(TargetDir)))
  7443. {
  7444. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7445. FPendingTempSpaceWarn = true;
  7446. try
  7447. {
  7448. FDragDropOperation = true;
  7449. int Params = cpTemporary |
  7450. (Type == ttMove ? cpDelete : 0);
  7451. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7452. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7453. }
  7454. __finally
  7455. {
  7456. FDragDropOperation = false;
  7457. FPendingTempSpaceWarn = false;
  7458. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7459. }
  7460. }
  7461. else
  7462. {
  7463. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7464. }
  7465. }
  7466. }
  7467. }
  7468. //---------------------------------------------------------------------------
  7469. void __fastcall TCustomScpExplorerForm::DDDownload(
  7470. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7471. {
  7472. DebugAssert(!IsLocalBrowserMode());
  7473. TAutoBatch AutoBatch(this);
  7474. UpdateCopyParamCounters(*CopyParam);
  7475. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7476. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7477. {
  7478. RemoteDriveView->UpdateDropSource();
  7479. }
  7480. }
  7481. //---------------------------------------------------------------------------
  7482. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7483. {
  7484. public:
  7485. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7486. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7487. {
  7488. }
  7489. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7490. {
  7491. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7492. TDataObjectFilesEx::AllowData(FormatEtc);
  7493. }
  7494. };
  7495. //---------------------------------------------------------------------------
  7496. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7497. TObject * Sender, TDataObject *& DataObject)
  7498. {
  7499. if (FDDExtMapFile != NULL)
  7500. {
  7501. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7502. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7503. {
  7504. Abort();
  7505. }
  7506. if (FileList->Count > 0)
  7507. {
  7508. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7509. if (!FilesObject->IsValid(true, true))
  7510. {
  7511. FilesObject->_Release();
  7512. }
  7513. else
  7514. {
  7515. DataObject = FilesObject;
  7516. }
  7517. }
  7518. }
  7519. }
  7520. //---------------------------------------------------------------------------
  7521. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7522. {
  7523. DebugFail();
  7524. }
  7525. //---------------------------------------------------------------------------
  7526. void __fastcall TCustomScpExplorerForm::GoToTree()
  7527. {
  7528. ComponentVisible[fcRemoteTree] = true;
  7529. DriveView(osRemote)->SetFocus();
  7530. }
  7531. //---------------------------------------------------------------------------
  7532. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7533. TPanelExport Export)
  7534. {
  7535. TCustomDirView * DirView = this->DirView(Side);
  7536. std::unique_ptr<TStrings> ExportData(new TStringList());
  7537. switch (Export)
  7538. {
  7539. case pePath:
  7540. ExportData->Add(DirView->PathName);
  7541. break;
  7542. case peFileList:
  7543. case peFullFileList:
  7544. {
  7545. bool FullPath = (Export == peFullFileList);
  7546. DirView->CreateFileList(false, FullPath, ExportData.get());
  7547. for (int Index = 0; Index < ExportData->Count; Index++)
  7548. {
  7549. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7550. {
  7551. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7552. }
  7553. }
  7554. }
  7555. break;
  7556. default:
  7557. DebugFail();
  7558. }
  7559. return ExportData.release();
  7560. }
  7561. //---------------------------------------------------------------------------
  7562. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7563. TPanelExport Export, TPanelExportDestination Destination)
  7564. {
  7565. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7566. PanelExportStore(Side, Export, Destination, ExportData.get());
  7567. }
  7568. //---------------------------------------------------------------------------
  7569. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7570. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7571. TStrings * ExportData)
  7572. {
  7573. if (Destination == pedClipboard)
  7574. {
  7575. TInstantOperationVisualizer Visualizer;
  7576. CopyToClipboard(ExportData);
  7577. }
  7578. else
  7579. {
  7580. DebugFail();
  7581. }
  7582. }
  7583. //---------------------------------------------------------------------------
  7584. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7585. {
  7586. TCustomDirView * DirView = this->DirView(Side);
  7587. UnicodeString Mask = DirView->Mask;
  7588. if (DoFilterMaskDialog(DirView, Mask))
  7589. {
  7590. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7591. Configuration->Usage->Inc(L"Filters");
  7592. }
  7593. }
  7594. //---------------------------------------------------------------------------
  7595. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7596. {
  7597. return FQueueController->DefaultOperation();
  7598. }
  7599. //---------------------------------------------------------------------------
  7600. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7601. TQueueOperation Operation, void ** Param)
  7602. {
  7603. switch (Operation)
  7604. {
  7605. case qoPreferences:
  7606. return true;
  7607. case qoGoTo:
  7608. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7609. case qoOnceEmpty:
  7610. return IsAnythingQueued();
  7611. default:
  7612. return FQueueController->AllowOperation(Operation, Param);
  7613. }
  7614. }
  7615. //---------------------------------------------------------------------------
  7616. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7617. {
  7618. if (DebugAlwaysTrue(QueueView3->Visible))
  7619. {
  7620. QueueView3->SetFocus();
  7621. }
  7622. }
  7623. //---------------------------------------------------------------------------
  7624. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7625. TQueueOperation Operation, void * Param)
  7626. {
  7627. if (Operation == qoGoTo)
  7628. {
  7629. GoToQueue();
  7630. }
  7631. else if (Operation == qoPreferences)
  7632. {
  7633. PreferencesDialog(pmQueue);
  7634. }
  7635. else
  7636. {
  7637. FQueueController->ExecuteOperation(Operation, Param);
  7638. }
  7639. }
  7640. //---------------------------------------------------------------------------
  7641. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7642. {
  7643. return (Queue != NULL) && Queue->Enabled;
  7644. }
  7645. //---------------------------------------------------------------------------
  7646. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7647. {
  7648. DebugAssert(Queue != NULL);
  7649. if (Queue != NULL)
  7650. {
  7651. Queue->Enabled = !Queue->Enabled;
  7652. }
  7653. }
  7654. //---------------------------------------------------------------------------
  7655. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7656. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7657. {
  7658. FQueueActedItem = QueueView3->ItemFocused;
  7659. }
  7660. //---------------------------------------------------------------------------
  7661. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7662. {
  7663. return TopDock->Height + QueueSplitter->Height;
  7664. }
  7665. //---------------------------------------------------------------------------
  7666. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7667. {
  7668. return
  7669. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7670. (QueueDock->Visible ? QueueDock->Height : 0) +
  7671. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7672. }
  7673. //---------------------------------------------------------------------------
  7674. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7675. {
  7676. return ScaleByTextHeight(this, 48);
  7677. }
  7678. //---------------------------------------------------------------------------
  7679. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7680. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7681. {
  7682. // when queue is hidden by double-clicking splitter, stray attempt to
  7683. // resize the panel with strange value arrives, make sure it is ignored
  7684. if (ComponentVisible[fcQueueView])
  7685. {
  7686. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7687. if (NewSize > MaxHeightLimit)
  7688. {
  7689. NewSize = MaxHeightLimit;
  7690. }
  7691. int MinHeightLimit =
  7692. GetStaticQueuePanelComponentsHeight() +
  7693. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7694. GetMinQueueViewHeight();
  7695. if (NewSize < MinHeightLimit)
  7696. {
  7697. NewSize = MinHeightLimit;
  7698. }
  7699. }
  7700. else
  7701. {
  7702. Accept = false;
  7703. }
  7704. }
  7705. //---------------------------------------------------------------------------
  7706. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7707. {
  7708. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7709. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7710. if (QueueViewHeight < GetMinQueueViewHeight())
  7711. {
  7712. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7713. }
  7714. }
  7715. //---------------------------------------------------------------------------
  7716. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7717. TDragObject *& /*DragObject*/)
  7718. {
  7719. FQueueActedItem = QueueView3->ItemFocused;
  7720. }
  7721. //---------------------------------------------------------------------------
  7722. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7723. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7724. {
  7725. Accept = true;
  7726. if (Source == QueueView3)
  7727. {
  7728. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7729. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7730. if (Accept)
  7731. {
  7732. TQueueItemProxy * QueueItem;
  7733. TQueueItemProxy * DestQueueItem;
  7734. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7735. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7736. Accept = (QueueItem != DestQueueItem) &&
  7737. (QueueItem->Status == TQueueItem::qsPending) &&
  7738. (DestQueueItem->Status == TQueueItem::qsPending);
  7739. }
  7740. }
  7741. }
  7742. //---------------------------------------------------------------------------
  7743. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7744. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7745. {
  7746. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7747. {
  7748. TQueueItemProxy * QueueItem;
  7749. TQueueItemProxy * DestQueueItem;
  7750. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7751. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7752. QueueItem->Move(DestQueueItem);
  7753. }
  7754. }
  7755. //---------------------------------------------------------------------------
  7756. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7757. {
  7758. if ((QueueView3->ItemFocused == NULL) &&
  7759. (QueueView3->Items->Count > 0))
  7760. {
  7761. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7762. }
  7763. QueueLabelUpdateStatus();
  7764. }
  7765. //---------------------------------------------------------------------------
  7766. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7767. {
  7768. QueueLabelUpdateStatus();
  7769. }
  7770. //---------------------------------------------------------------------------
  7771. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7772. {
  7773. QueueLabel->UpdateStatus();
  7774. }
  7775. //---------------------------------------------------------------------------
  7776. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7777. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7778. {
  7779. if (Selected)
  7780. {
  7781. NonVisualDataModule->UpdateNonVisibleActions();
  7782. }
  7783. }
  7784. //---------------------------------------------------------------------------
  7785. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7786. {
  7787. TDragDropFilesEx * Result = NULL;
  7788. if (Sender == SessionsPageControl)
  7789. {
  7790. Result = FSessionsDragDropFilesEx;
  7791. }
  7792. else if (Sender == QueueView3)
  7793. {
  7794. Result = FQueueDragDropFilesEx;
  7795. }
  7796. else
  7797. {
  7798. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7799. if (DirView != NULL)
  7800. {
  7801. Result = DirView->DragDropFilesEx;
  7802. }
  7803. else
  7804. {
  7805. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7806. if (DriveView != NULL)
  7807. {
  7808. Result = DriveView->DragDropFilesEx;
  7809. }
  7810. }
  7811. }
  7812. DebugAssert(Result != NULL);
  7813. return Result;
  7814. }
  7815. //---------------------------------------------------------------------------
  7816. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7817. {
  7818. return (DView->SelCount == DView->FilesCount);
  7819. }
  7820. //---------------------------------------------------------------------------
  7821. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7822. {
  7823. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7824. }
  7825. //---------------------------------------------------------------------------
  7826. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7827. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7828. {
  7829. DebugAssert(!IsLocalBrowserMode());
  7830. TFileOperation Operation;
  7831. switch (Effect)
  7832. {
  7833. case DROPEFFECT_MOVE:
  7834. Operation = foMove;
  7835. break;
  7836. case DROPEFFECT_COPY:
  7837. // occures on WinXP (reported by user)
  7838. default:
  7839. Operation = foCopy;
  7840. break;
  7841. };
  7842. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7843. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7844. if (DragDropFilesEx->FileList->Count > 0)
  7845. {
  7846. TStrings * FileList = new TStringList();
  7847. try
  7848. {
  7849. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7850. {
  7851. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7852. }
  7853. FDragDropOperation = true;
  7854. TTransferOperationParam Param;
  7855. Param.TargetDirectory = TargetPath;
  7856. // upload, no temp dirs
  7857. Param.Temp = false;
  7858. Param.DragDrop = DragDrop;
  7859. Param.Options =
  7860. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7861. if (ForceQueue)
  7862. {
  7863. Param.Queue = asOn;
  7864. }
  7865. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7866. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7867. {
  7868. if (IsFileControl(DropSourceControl, osLocal))
  7869. {
  7870. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7871. }
  7872. else
  7873. {
  7874. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7875. }
  7876. }
  7877. }
  7878. __finally
  7879. {
  7880. FDragDropOperation = false;
  7881. delete FileList;
  7882. }
  7883. }
  7884. }
  7885. //---------------------------------------------------------------------------
  7886. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7887. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7888. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7889. {
  7890. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7891. }
  7892. //---------------------------------------------------------------------------
  7893. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7894. TObject * Sender, int grfKeyState, int & dwEffect)
  7895. {
  7896. DebugAssert(!IsLocalBrowserMode());
  7897. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7898. if ((dwEffect != DROPEFFECT_NONE) &&
  7899. IsFileControl(DropSourceControl, osRemote))
  7900. {
  7901. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7902. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7903. (RemoteDirView->DropTarget == NULL))
  7904. {
  7905. dwEffect = DROPEFFECT_NONE;
  7906. }
  7907. else
  7908. {
  7909. if (dwEffect == DROPEFFECT_COPY)
  7910. {
  7911. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  7912. // currently we support copying always (at least via temporary directory);
  7913. // remove associated checks once this all proves stable and working
  7914. bool CopyCapable = true;
  7915. // if we do not support neither of operations, there's no discussion
  7916. if (!MoveCapable && !CopyCapable)
  7917. {
  7918. dwEffect = DROPEFFECT_NONE;
  7919. }
  7920. else
  7921. {
  7922. // The default effect inside remote panel is move,
  7923. // unless we do not support it, but support copy
  7924. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7925. {
  7926. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7927. }
  7928. else
  7929. {
  7930. // with ctrl-down, we want copy unless it is not supported
  7931. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7932. }
  7933. }
  7934. }
  7935. }
  7936. }
  7937. }
  7938. //---------------------------------------------------------------------------
  7939. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7940. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7941. {
  7942. DebugAssert(!IsLocalBrowserMode());
  7943. if (FDDTotalSize >= 0)
  7944. {
  7945. if (File->IsDirectory)
  7946. {
  7947. FDDTotalSize = -1;
  7948. }
  7949. else
  7950. {
  7951. FDDTotalSize += File->Size;
  7952. }
  7953. }
  7954. DebugAssert(!FDragTempDir.IsEmpty());
  7955. // TODO: this is quite ineffective
  7956. // TODO: what if invalid character replacement is disabled?
  7957. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7958. UnicodeString TransferFileName;
  7959. if (Sender == RemoteDriveView)
  7960. {
  7961. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7962. }
  7963. else
  7964. {
  7965. TransferFileName = File->FileName;
  7966. }
  7967. FDDFileList->AddObject(TransferFileName, File);
  7968. }
  7969. //---------------------------------------------------------------------------
  7970. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7971. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7972. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7973. {
  7974. // sometimes we do not get DDEnd so the list is not released
  7975. SAFE_DESTROY(FDDFileList);
  7976. FDDFileList = new TStringList();
  7977. FDragTempDir = WinConfiguration->TemporaryDir();
  7978. FDDTotalSize = 0;
  7979. FDragDropSshTerminate.reset(NULL);
  7980. }
  7981. //---------------------------------------------------------------------------
  7982. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7983. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7984. HRESULT & Result)
  7985. {
  7986. if (Result == DRAGDROP_S_DROP)
  7987. {
  7988. try
  7989. {
  7990. GlobalDragImageList->HideDragImage();
  7991. try
  7992. {
  7993. RemoteFileControlDDTargetDrop();
  7994. }
  7995. catch(ESshTerminate & E)
  7996. {
  7997. FDragDropSshTerminate.reset(E.Clone());
  7998. }
  7999. }
  8000. catch (Exception &E)
  8001. {
  8002. // If downloading fails we need to cancel drag&drop, otherwise
  8003. // Explorer shows error
  8004. // But by the way exception probably never reach this point as
  8005. // it's catched on way.
  8006. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8007. Result = DRAGDROP_S_CANCEL;
  8008. ShowExtendedException(Terminal, &E);
  8009. }
  8010. }
  8011. }
  8012. //---------------------------------------------------------------------------
  8013. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8014. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8015. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8016. {
  8017. TFileMasks::TParams MaskParams;
  8018. MaskParams.Size = Size;
  8019. MaskParams.Modification = Modification;
  8020. // this does not re-parse the mask if it is the same as the last time
  8021. FDirViewMatchMask = Masks;
  8022. bool ImplicitMatch;
  8023. Matches =
  8024. // RecurseInclude parameter has no effect as the Path is empty
  8025. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  8026. (AllowImplicitMatches || !ImplicitMatch);
  8027. }
  8028. //---------------------------------------------------------------------------
  8029. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8030. TObject * Sender, TListItem * Item, WORD & Indexes)
  8031. {
  8032. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8033. UnicodeString Ext;
  8034. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8035. {
  8036. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8037. }
  8038. else
  8039. {
  8040. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8041. }
  8042. if (SameText(Ext, Configuration->PartialExt))
  8043. {
  8044. Indexes |= oiPartial;
  8045. }
  8046. }
  8047. //---------------------------------------------------------------------------
  8048. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8049. {
  8050. return
  8051. DirViewEnabled(osCurrent) &&
  8052. DirView(osCurrent)->CanPasteFromClipBoard();
  8053. }
  8054. //---------------------------------------------------------------------------
  8055. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8056. {
  8057. bool Result = false;
  8058. if (CanPasteToDirViewFromClipBoard())
  8059. {
  8060. Result = true;
  8061. }
  8062. else
  8063. {
  8064. UnicodeString ClipboardText;
  8065. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8066. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8067. {
  8068. if (StoredSessions->IsUrl(ClipboardText))
  8069. {
  8070. Result = true;
  8071. }
  8072. else
  8073. {
  8074. Result = DirViewEnabled(osCurrent);
  8075. }
  8076. }
  8077. }
  8078. return Result;
  8079. }
  8080. //---------------------------------------------------------------------------
  8081. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8082. {
  8083. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8084. if (DoesClipboardContainOurFiles())
  8085. {
  8086. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8087. {
  8088. DebugAssert(!IsLocalBrowserMode());
  8089. TTerminalManager * Manager = TTerminalManager::Instance();
  8090. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8091. Manager->ActiveSession = FClipboardTerminal;
  8092. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8093. }
  8094. }
  8095. else if (CanPasteToDirViewFromClipBoard())
  8096. {
  8097. DirView(osCurrent)->PasteFromClipBoard();
  8098. }
  8099. else
  8100. {
  8101. UnicodeString ClipboardText;
  8102. if (NonEmptyTextFromClipboard(ClipboardText))
  8103. {
  8104. if (StoredSessions->IsUrl(ClipboardText))
  8105. {
  8106. NewSession(ClipboardText);
  8107. }
  8108. else
  8109. {
  8110. DirView(osCurrent)->Path = ClipboardText;
  8111. }
  8112. }
  8113. }
  8114. }
  8115. //---------------------------------------------------------------------------
  8116. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8117. {
  8118. // TBD
  8119. }
  8120. //---------------------------------------------------------------------------
  8121. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8122. {
  8123. TCustomDirView * ADirView = DirView(Side);
  8124. ADirView->SelectAll(Mode);
  8125. ADirView->SetFocus();
  8126. }
  8127. //---------------------------------------------------------------------------
  8128. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8129. {
  8130. TCustomDirView * ADirView = DirView(Side);
  8131. TFileFilter Filter;
  8132. DefaultFileFilter(Filter);
  8133. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8134. {
  8135. Configuration->Usage->Inc(L"MaskSelections");
  8136. ADirView->SelectFiles(Filter, Select);
  8137. ADirView->SetFocus();
  8138. }
  8139. }
  8140. //---------------------------------------------------------------------------
  8141. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8142. {
  8143. TCustomDirView * ADirView = DirView(Side);
  8144. ADirView->RestoreSelectedNames();
  8145. ADirView->SetFocus();
  8146. }
  8147. //---------------------------------------------------------------------------
  8148. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8149. {
  8150. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8151. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8152. {
  8153. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8154. UnicodeString Ext;
  8155. if (!IsSideLocalBrowser(osCurrent))
  8156. {
  8157. Ext = UnixExtractFileExt(FileName);
  8158. }
  8159. else
  8160. {
  8161. Ext = ExtractFileExt(FileName);
  8162. }
  8163. if (Ext.IsEmpty())
  8164. {
  8165. Ext = L".";
  8166. }
  8167. TFileFilter Filter;
  8168. Filter.Masks = FORMAT(L"*%s", (Ext));
  8169. Filter.Directories = false;
  8170. CurrentDirView->SelectFiles(Filter, Select);
  8171. }
  8172. }
  8173. //---------------------------------------------------------------------------
  8174. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8175. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8176. {
  8177. UnicodeString Result;
  8178. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8179. {
  8180. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8181. }
  8182. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8183. {
  8184. // noop
  8185. }
  8186. else
  8187. {
  8188. Result =
  8189. FMTLOAD(FILE_INFO_FORMAT,
  8190. (FormatBytes(FileInfo.SelectedSize),
  8191. FormatBytes(FileInfo.FilesSize),
  8192. FormatNumber(FileInfo.SelectedCount),
  8193. FormatNumber(FileInfo.FilesCount)));
  8194. }
  8195. return Result;
  8196. }
  8197. //---------------------------------------------------------------------------
  8198. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8199. TTBXStatusPanel * Panel, TOperationSide Side)
  8200. {
  8201. if (Panel->Index == 1)
  8202. {
  8203. ToggleShowHiddenFiles();
  8204. }
  8205. else if (Panel->Index == 2)
  8206. {
  8207. Filter(Side);
  8208. }
  8209. }
  8210. //---------------------------------------------------------------------------
  8211. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8212. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8213. {
  8214. DebugAssert(!StatusBar->SimplePanel);
  8215. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8216. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8217. }
  8218. //---------------------------------------------------------------------------
  8219. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8220. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8221. {
  8222. DebugAssert(StatusBar->Panels->Count >= 3);
  8223. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8224. if (FileInfo.HiddenCount > 0)
  8225. {
  8226. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8227. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8228. }
  8229. else
  8230. {
  8231. HiddenFilesPanel->ViewPriority = 0;
  8232. // not really necessary, just to cleanup no-longer-valid data
  8233. HiddenFilesPanel->Caption = L"";
  8234. }
  8235. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8236. if (FileInfo.FilteredCount > 0)
  8237. {
  8238. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8239. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8240. }
  8241. else
  8242. {
  8243. FilteredFilesPanel->ViewPriority = 0;
  8244. // not really necessary, just to cleanup no-longer-valid data
  8245. FilteredFilesPanel->Caption = L"";
  8246. }
  8247. }
  8248. //---------------------------------------------------------------------------
  8249. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8250. TObject * /*Sender*/)
  8251. {
  8252. if (DirView(osOther)->Enabled)
  8253. {
  8254. DirView(osOther)->SetFocus();
  8255. }
  8256. }
  8257. //---------------------------------------------------------------------------
  8258. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8259. {
  8260. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8261. switch (Config.Show)
  8262. {
  8263. case qvShow:
  8264. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8265. {
  8266. Config.Show = qvHide;
  8267. }
  8268. else
  8269. {
  8270. Config.Show = Config.LastHideShow;
  8271. }
  8272. break;
  8273. case qvHideWhenEmpty:
  8274. if (ComponentVisible[fcQueueView])
  8275. {
  8276. Config.Show = qvHide;
  8277. }
  8278. else
  8279. {
  8280. Config.LastHideShow = Config.Show;
  8281. Config.Show = qvShow;
  8282. }
  8283. break;
  8284. case qvHide:
  8285. Config.LastHideShow = Config.Show;
  8286. Config.Show = qvShow;
  8287. break;
  8288. }
  8289. WinConfiguration->QueueView = Config;
  8290. }
  8291. //---------------------------------------------------------------------------
  8292. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8293. {
  8294. UnicodeString Result;
  8295. if (Terminal != NULL)
  8296. {
  8297. switch (WinConfiguration->PathInCaption)
  8298. {
  8299. case picShort:
  8300. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8301. break;
  8302. case picFull:
  8303. Result = Terminal->CurrentDirectory;
  8304. break;
  8305. }
  8306. }
  8307. return Result;
  8308. }
  8309. //---------------------------------------------------------------------------
  8310. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8311. {
  8312. TColor Result = GetWindowColor(FSessionColor);
  8313. return Result;
  8314. }
  8315. //---------------------------------------------------------------------------
  8316. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8317. {
  8318. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8319. return Result;
  8320. }
  8321. //---------------------------------------------------------------------------
  8322. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8323. {
  8324. if (!FSessionChanging)
  8325. {
  8326. TTerminalManager::Instance()->UpdateAppTitle();
  8327. }
  8328. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8329. // background to gray on Windows 7 (works on Windows 10).
  8330. // See also EnableControl
  8331. if (Showing)
  8332. {
  8333. if (ManagedSession != NULL)
  8334. {
  8335. // Update path when it changes
  8336. if ((SessionsPageControl->ActivePage != NULL) &&
  8337. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8338. {
  8339. UpdateSessionTab(SessionsPageControl->ActivePage);
  8340. }
  8341. }
  8342. NonVisualDataModule->ReconnectSessionAction->Update();
  8343. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8344. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8345. CenterReconnectToolbar();
  8346. bool HasTerminal = HasActiveTerminal();
  8347. RemoteDirView->Enabled = HasTerminal;
  8348. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8349. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8350. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8351. RemoteDriveView->Color = RemoteDirView->Color;
  8352. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8353. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8354. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8355. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8356. QueueFileList->Enabled = QueueView3->Enabled;
  8357. QueueFileList->Color = QueueView3->Color;
  8358. QueueFileList->Font->Color = QueueView3->Font->Color;
  8359. QueueLabelUpdateStatus();
  8360. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8361. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8362. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8363. {
  8364. UpdateDarkMode();
  8365. }
  8366. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8367. }
  8368. }
  8369. //---------------------------------------------------------------------------
  8370. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8371. {
  8372. UpdateControls();
  8373. }
  8374. //---------------------------------------------------------------------------
  8375. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8376. TObject * Sender)
  8377. {
  8378. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8379. DebugAssert(DirView != NULL);
  8380. DoDirViewLoaded(DirView);
  8381. TransferPresetAutoSelect();
  8382. }
  8383. //---------------------------------------------------------------------------
  8384. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8385. {
  8386. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8387. // first run after installation
  8388. if (double(Updates.LastCheck) == 0)
  8389. {
  8390. // make sure next time there will be an update (if enabled)
  8391. Updates.LastCheck = TDateTime(1);
  8392. WinConfiguration->Updates = Updates;
  8393. }
  8394. else if ((double(Updates.Period) > 0) &&
  8395. (Now() - Updates.LastCheck >= Updates.Period))
  8396. {
  8397. TThreadMethod OnUpdatesChecked = NULL;
  8398. if (!IsUWP())
  8399. {
  8400. OnUpdatesChecked = UpdatesChecked;
  8401. }
  8402. StartUpdateThread(OnUpdatesChecked);
  8403. }
  8404. }
  8405. //---------------------------------------------------------------------------
  8406. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8407. {
  8408. UnicodeString Message;
  8409. bool New;
  8410. TQueryType Type;
  8411. GetUpdatesMessage(Message, New, Type, false);
  8412. if (!Message.IsEmpty())
  8413. {
  8414. if (New)
  8415. {
  8416. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8417. }
  8418. if (!New && (Type != qtWarning))
  8419. {
  8420. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8421. }
  8422. else
  8423. {
  8424. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8425. }
  8426. if (New)
  8427. {
  8428. Configuration->Usage->Inc(L"UpdateNotifications");
  8429. }
  8430. }
  8431. }
  8432. //---------------------------------------------------------------------------
  8433. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8434. {
  8435. RestoreApp();
  8436. bool CanDisplay = !NonVisualDataModule->Busy;
  8437. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8438. {
  8439. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8440. }
  8441. if (CanDisplay)
  8442. {
  8443. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8444. CheckForUpdates(true);
  8445. }
  8446. else
  8447. {
  8448. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8449. }
  8450. }
  8451. //---------------------------------------------------------------------------
  8452. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8453. TCopyParamRuleData & Data)
  8454. {
  8455. DebugAssert(Terminal != NULL);
  8456. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8457. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8458. Data.RemoteDirectory = RemoteDirView->PathName;
  8459. }
  8460. //---------------------------------------------------------------------------
  8461. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8462. {
  8463. // Terminal can be null when we are changing local directory implicitly,
  8464. // because it has been deleted, while no session is connected
  8465. // (Login dialog is open)
  8466. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8467. {
  8468. DebugAssert(!IsLocalBrowserMode());
  8469. TCopyParamRuleData Data;
  8470. GetTransferPresetAutoSelectData(Data);
  8471. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8472. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8473. if (CopyParamIndex < 0)
  8474. {
  8475. // there is no preset that matches autoselection
  8476. // set preset that we consider "default"
  8477. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8478. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8479. }
  8480. else
  8481. {
  8482. // there is preset matching autoselection
  8483. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8484. if (CopyParamName == FCopyParamAutoSelected)
  8485. {
  8486. // autoselected the same preset as the last time
  8487. // make no change (i.e. preserve custom user preset, if any)
  8488. }
  8489. else
  8490. {
  8491. // autoselected the different preset then the last time (or there
  8492. // was default preset set)
  8493. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8494. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8495. }
  8496. }
  8497. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8498. {
  8499. if (CopyParamIndex >= 0)
  8500. {
  8501. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8502. }
  8503. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8504. try
  8505. {
  8506. int Fmt =
  8507. (CopyParamIndex < 0) ?
  8508. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8509. COPY_PARAM_AUTOSELECTED;
  8510. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8511. Data->Message = MainInstructions(Message);
  8512. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8513. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8514. CopyParamAttrs);
  8515. if (CopyParamIndex >= 0)
  8516. {
  8517. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8518. Info = FORMAT(L"%s\n \n%s", (Info,
  8519. FMTLOAD(COPY_PARAM_RULE,
  8520. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8521. }
  8522. Data->Message += L"\n\n" + Info;
  8523. if (WinConfiguration->CopyParamAutoSelectNotice)
  8524. {
  8525. TransferPresetNoteMessage(Data, true);
  8526. }
  8527. else
  8528. {
  8529. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8530. Data = NULL; // ownership passed
  8531. }
  8532. }
  8533. __finally
  8534. {
  8535. delete Data;
  8536. }
  8537. }
  8538. }
  8539. }
  8540. //---------------------------------------------------------------------------
  8541. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8542. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8543. {
  8544. DebugAssert(NoteData != NULL);
  8545. DebugAssert(!IsLocalBrowserMode());
  8546. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8547. TQueryButtonAlias Aliases[1];
  8548. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8549. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8550. Params.Aliases = Aliases;
  8551. Params.AliasesCount = LENOF(Aliases);
  8552. unsigned int Result =
  8553. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8554. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8555. switch (Result)
  8556. {
  8557. case qaNeverAskAgain:
  8558. DebugAssert(AllowNeverAskAgain);
  8559. WinConfiguration->CopyParamAutoSelectNotice = false;
  8560. break;
  8561. case qaIgnore:
  8562. PreferencesDialog(pmPresets);
  8563. break;
  8564. }
  8565. }
  8566. //---------------------------------------------------------------------------
  8567. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8568. {
  8569. // as of now useless, as this is used for notes only, never for balloons, ...
  8570. RestoreApp();
  8571. // .. and we should never be busy here
  8572. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8573. {
  8574. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8575. }
  8576. }
  8577. //---------------------------------------------------------------------------
  8578. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8579. TPreferencesMode APreferencesMode)
  8580. {
  8581. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8582. TCopyParamRuleData Data;
  8583. if (Terminal != NULL)
  8584. {
  8585. PreferencesData.reset(new TPreferencesDialogData());
  8586. GetTransferPresetAutoSelectData(Data);
  8587. PreferencesData->CopyParamRuleData = &Data;
  8588. }
  8589. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8590. }
  8591. //---------------------------------------------------------------------------
  8592. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8593. const TCustomCommandType & Command)
  8594. {
  8595. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8596. {
  8597. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8598. }
  8599. }
  8600. //---------------------------------------------------------------------------
  8601. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8602. {
  8603. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8604. TCustomCommandType Command;
  8605. // make sure we use local custom command when remote are not supported
  8606. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8607. {
  8608. Command = FLastCustomCommand;
  8609. }
  8610. else
  8611. {
  8612. Command.Params = Command.Params | ccLocal;
  8613. }
  8614. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8615. FEditingFocusedAdHocCommand = OnFocused;
  8616. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8617. int Options =
  8618. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8619. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8620. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8621. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8622. {
  8623. FLastCustomCommand = Command;
  8624. UpdateCustomCommandsToolbar();
  8625. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8626. }
  8627. }
  8628. //---------------------------------------------------------------------------
  8629. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8630. {
  8631. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8632. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8633. DebugAssert(State > 0);
  8634. if (State <= 0)
  8635. {
  8636. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8637. }
  8638. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8639. }
  8640. //---------------------------------------------------------------------------
  8641. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8642. TCustomCommandType & Command, int & State)
  8643. {
  8644. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8645. if (Result)
  8646. {
  8647. Command = FLastCustomCommand;
  8648. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8649. }
  8650. return Result;
  8651. }
  8652. //---------------------------------------------------------------------------
  8653. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8654. {
  8655. ResetIncrementalSearch();
  8656. }
  8657. //---------------------------------------------------------------------------
  8658. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8659. {
  8660. DebugAssert(ComponentVisible[Component]);
  8661. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8662. }
  8663. //---------------------------------------------------------------------------
  8664. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8665. {
  8666. // when queue panel is resized here directly, the status bar is stretched
  8667. // over whole space the panel occupied
  8668. PostComponentHide(fcQueueView);
  8669. }
  8670. //---------------------------------------------------------------------------
  8671. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8672. {
  8673. ComponentVisible[fcQueueFileList] = false;
  8674. }
  8675. //---------------------------------------------------------------------------
  8676. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8677. {
  8678. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8679. RefreshColorMode();
  8680. ResetSysDarkTheme();
  8681. ConfigurationChanged();
  8682. ConfigureInterface();
  8683. // Should be called for all controls
  8684. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8685. }
  8686. //---------------------------------------------------------------------------
  8687. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8688. {
  8689. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8690. if (!FInvalid &&
  8691. (Message.LParam != 0) &&
  8692. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8693. {
  8694. ThemeChanged();
  8695. }
  8696. TForm::Dispatch(&Message);
  8697. }
  8698. //---------------------------------------------------------------------------
  8699. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8700. {
  8701. TMessage * M = static_cast<TMessage*>(Message);
  8702. switch (M->Msg)
  8703. {
  8704. case CM_DIALOGCHAR:
  8705. CMDialogChar(*M);
  8706. break;
  8707. case WM_APPCOMMAND:
  8708. WMAppCommand(*M);
  8709. break;
  8710. case WM_SYSCOMMAND:
  8711. WMSysCommand(*M);
  8712. break;
  8713. case WM_QUERYENDSESSION:
  8714. WMQueryEndSession(*M);
  8715. break;
  8716. case WM_ENDSESSION:
  8717. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8718. break;
  8719. #ifdef _DEBUG
  8720. case WM_WINDOWPOSCHANGED:
  8721. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8722. break;
  8723. case CM_CUSTOMSTYLECHANGED:
  8724. TForm::Dispatch(Message);
  8725. break;
  8726. #endif
  8727. case WM_COMPONENT_HIDE:
  8728. {
  8729. Byte Component = static_cast<Byte>(M->WParam);
  8730. // sanity check
  8731. if (ComponentVisible[Component])
  8732. {
  8733. // special treatment
  8734. if (Component == fcQueueView)
  8735. {
  8736. ToggleQueueVisibility();
  8737. DebugAssert(!ComponentVisible[fcQueueView]);
  8738. }
  8739. else
  8740. {
  8741. ComponentVisible[Component] = false;
  8742. }
  8743. }
  8744. }
  8745. break;
  8746. case WM_COPYDATA:
  8747. WMCopyData(*M);
  8748. break;
  8749. case WM_MANAGES_CAPTION:
  8750. // caption managed in TTerminalManager::UpdateAppTitle()
  8751. M->Result = 1;
  8752. break;
  8753. case WM_WANTS_MOUSEWHEEL:
  8754. M->Result = 1;
  8755. break;
  8756. case CM_SHOWINGCHANGED:
  8757. CMShowingChanged(*M);
  8758. break;
  8759. case WM_CLOSE:
  8760. WMClose(*M);
  8761. break;
  8762. case WM_DPICHANGED:
  8763. WMDpiChanged(*M);
  8764. break;
  8765. case CM_DPICHANGED:
  8766. CMDpiChanged(*M);
  8767. break;
  8768. case WM_SETTINGCHANGE:
  8769. WMSettingChange(*M);
  8770. break;
  8771. case CM_DIALOGKEY:
  8772. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8773. break;
  8774. default:
  8775. TForm::Dispatch(Message);
  8776. break;
  8777. }
  8778. }
  8779. //---------------------------------------------------------------------------
  8780. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8781. {
  8782. // noop
  8783. }
  8784. //---------------------------------------------------------------------------
  8785. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8786. {
  8787. TForm::Dispatch(&Message);
  8788. FSessionColors->Clear();
  8789. AddFixedSessionImages();
  8790. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8791. UpdateImages();
  8792. }
  8793. //---------------------------------------------------------------------------
  8794. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8795. {
  8796. TForm::Dispatch(&Message);
  8797. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8798. UpdatePixelsPerInchMainWindowCounter();
  8799. }
  8800. //---------------------------------------------------------------------------
  8801. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8802. {
  8803. // Cannot close window while we are busy.
  8804. // We cannot test this in FormCloseQuery as that is called also from
  8805. // Close(), which is called by CloseApplicationAction. So we can be busy
  8806. // there already even, when it is legitimate to close the application.
  8807. // Possibly a better place to handle this would be WMSysCommand.
  8808. if (NonVisualDataModule->Busy)
  8809. {
  8810. Message.Result = 1;
  8811. }
  8812. else
  8813. {
  8814. TForm::Dispatch(&Message);
  8815. }
  8816. }
  8817. //---------------------------------------------------------------------------
  8818. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8819. {
  8820. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8821. bool NoSession = (ManagedSession == NULL);
  8822. // This calls TCustomScpExplorerForm::DoShow
  8823. TForm::Dispatch(&Message);
  8824. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8825. if (Showing && !FStarted)
  8826. {
  8827. FStarted = true;
  8828. InterfaceStarted();
  8829. }
  8830. if (Showing && NoSession)
  8831. {
  8832. // When we are starting minimized (i.e. from an installer),
  8833. // postpone showing Login dialog until we get restored.
  8834. // Otherwise the Login dialog (and Authentication window) show restored
  8835. // over invisible (minimized) main window.
  8836. if (WindowState == wsMinimized)
  8837. {
  8838. FNeedSession = true;
  8839. }
  8840. else
  8841. {
  8842. // This happens before application ever goes idle, so the toolbars would
  8843. // stay enabled (initial state) until the Login dialog is dismissed.
  8844. UpdateActions();
  8845. NonVisualDataModule->StartBusy();
  8846. try
  8847. {
  8848. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8849. // otherwise the dialog does not receive focus.
  8850. // With Commander interface the ProcessMessages is called already
  8851. // by TDriveView, but with Explorer interface, we need to call it explicily
  8852. Application->ProcessMessages();
  8853. // do not reload sessions, they have been loaded just now (optimization)
  8854. NeedSession(true);
  8855. }
  8856. __finally
  8857. {
  8858. NonVisualDataModule->EndBusy();
  8859. }
  8860. }
  8861. }
  8862. }
  8863. //---------------------------------------------------------------------------
  8864. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8865. {
  8866. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8867. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8868. }
  8869. //---------------------------------------------------------------------------
  8870. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8871. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8872. int & MaxHeight)
  8873. {
  8874. // workaround for bug in TWinControl.CalcConstraints
  8875. // Check for empty rect (restore from iconic state) is done there only after
  8876. // call to AdjustClientRect, which enlarges the rect (for forms).
  8877. TRect R = GetClientRect();
  8878. // when restoring from iconic state, place no restrictions
  8879. if (IsRectEmpty(R))
  8880. {
  8881. MinWidth = 0;
  8882. MinHeight = 0;
  8883. MaxWidth = 32000;
  8884. MaxHeight = 32000;
  8885. }
  8886. }
  8887. //---------------------------------------------------------------------------
  8888. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8889. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8890. {
  8891. // terminal can be already closed (e.g. dropped connection)
  8892. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8893. {
  8894. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8895. try
  8896. {
  8897. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8898. }
  8899. catch(...)
  8900. {
  8901. if (!Terminal->Active)
  8902. {
  8903. Close = true;
  8904. }
  8905. throw;
  8906. }
  8907. }
  8908. }
  8909. //---------------------------------------------------------------------------
  8910. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8911. {
  8912. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8913. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8914. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8915. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8916. {
  8917. OnGetSpaceAvailable = GetSpaceAvailable;
  8918. }
  8919. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8920. OnGetSpaceAvailable);
  8921. }
  8922. //---------------------------------------------------------------------------
  8923. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8924. {
  8925. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8926. Terminal->FillSessionDataForCode(Data.get());
  8927. return Data.release();
  8928. }
  8929. //---------------------------------------------------------------------------
  8930. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8931. {
  8932. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8933. DoGenerateUrlDialog(Data.get(), Paths);
  8934. }
  8935. //---------------------------------------------------------------------------
  8936. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8937. {
  8938. GenerateUrl(NULL);
  8939. }
  8940. //---------------------------------------------------------------------------
  8941. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8942. {
  8943. std::unique_ptr<TStrings> Paths(new TStringList());
  8944. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8945. GenerateUrl(Paths.get());
  8946. }
  8947. //---------------------------------------------------------------------------
  8948. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8949. {
  8950. FSessionColor = value;
  8951. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8952. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8953. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8954. DebugAssert(ColorItem != NULL);
  8955. ColorItem->Color = C;
  8956. NonVisualDataModule->ColorMenuItem->Color = C;
  8957. UpdateControls();
  8958. }
  8959. //---------------------------------------------------------------------------
  8960. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8961. {
  8962. if (value != FSessionColor)
  8963. {
  8964. UpdateSessionColor(value);
  8965. // Is null when called from LastTerminalClosed
  8966. if (Terminal != NULL)
  8967. {
  8968. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8969. }
  8970. }
  8971. }
  8972. //---------------------------------------------------------------------------
  8973. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8974. {
  8975. bool Result = FNoteTimer->Enabled;
  8976. if (Result)
  8977. {
  8978. // cannot cancel note too early
  8979. bool NotEarly =
  8980. (Now() - FNoteShown >
  8981. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8982. if (Force || NotEarly)
  8983. {
  8984. FNoteTimer->Enabled = false;
  8985. FNote = L"";
  8986. SAFE_DESTROY(FNoteData);
  8987. FOnNoteClick = NULL;
  8988. FNoteHints = FNotes->Text;
  8989. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8990. UpdateStatusBar();
  8991. }
  8992. }
  8993. return Result;
  8994. }
  8995. //---------------------------------------------------------------------------
  8996. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8997. {
  8998. DebugAssert(FNoteTimer->Enabled);
  8999. CancelNote(true);
  9000. }
  9001. //---------------------------------------------------------------------------
  9002. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9003. {
  9004. FNoteHints = FNotes->Text.TrimRight();
  9005. }
  9006. //---------------------------------------------------------------------------
  9007. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9008. {
  9009. int P = Note.Pos(L"\n");
  9010. if (P > 0)
  9011. {
  9012. Note.SetLength(P - 1);
  9013. }
  9014. FNotes->Add(FORMAT(L"[%s] %s",
  9015. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9016. while (FNotes->Count > 10)
  9017. {
  9018. FNotes->Delete(0);
  9019. }
  9020. if (UpdateNow)
  9021. {
  9022. UpdateNoteHints();
  9023. UpdateStatusBar();
  9024. }
  9025. }
  9026. //---------------------------------------------------------------------------
  9027. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9028. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9029. {
  9030. int P = Note.Pos(L"\n");
  9031. if (P > 0)
  9032. {
  9033. Note.SetLength(P - 1);
  9034. }
  9035. UpdateNoteHints();
  9036. FNote = Note;
  9037. SAFE_DESTROY(FNoteData);
  9038. FNoteData = NoteData;
  9039. FOnNoteClick = OnNoteClick;
  9040. AddNote(Note, false);
  9041. UpdateStatusBar();
  9042. FNoteShown = Now();
  9043. FNoteTimer->Enabled = false;
  9044. if (Seconds == 0)
  9045. {
  9046. Seconds = WinConfiguration->NotificationsTimeout;
  9047. }
  9048. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9049. FNoteTimer->Enabled = true;
  9050. }
  9051. //---------------------------------------------------------------------------
  9052. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9053. {
  9054. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9055. }
  9056. //---------------------------------------------------------------------------
  9057. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9058. {
  9059. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9060. {
  9061. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9062. }
  9063. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9064. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9065. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9066. 0, NULL, NULL);
  9067. }
  9068. //---------------------------------------------------------------------------
  9069. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9070. {
  9071. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9072. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9073. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9074. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9075. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9076. }
  9077. //---------------------------------------------------------------------------
  9078. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9079. {
  9080. WinConfiguration->FormatSizeBytes = Style;
  9081. }
  9082. //---------------------------------------------------------------------------
  9083. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9084. {
  9085. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9086. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9087. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9088. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9089. }
  9090. //---------------------------------------------------------------------------
  9091. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9092. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9093. {
  9094. if (Panel->Index == 0)
  9095. {
  9096. if (FOnNoteClick != NULL)
  9097. {
  9098. // prevent the user data from being freed by possible call
  9099. // to CancelNote or PostNote during call to OnNoteClick
  9100. std::unique_ptr<TObject> NoteData(FNoteData);
  9101. TNotifyEvent OnNoteClick = FOnNoteClick;
  9102. FNoteData = NULL;
  9103. // need to cancel the note as we are going to delete its user data
  9104. CancelNote(true);
  9105. OnNoteClick(NoteData.get());
  9106. }
  9107. }
  9108. if (HasActiveTerminal() &&
  9109. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9110. {
  9111. FileSystemInfo();
  9112. }
  9113. }
  9114. //---------------------------------------------------------------------------
  9115. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9116. {
  9117. // workaround:
  9118. // 1) for unknown reason, disabling window, while minimized,
  9119. // prevents it from restoring, even if it was enabled again meanwhile
  9120. // 2) when disabling the main window, while another has focus
  9121. // minimize is no longer possible
  9122. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9123. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9124. // Shouldn't we use IsApplicationMinimized() here?
  9125. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9126. {
  9127. Enabled = false;
  9128. }
  9129. FLockLevel++;
  9130. }
  9131. //---------------------------------------------------------------------------
  9132. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9133. {
  9134. DebugAssert(FLockLevel > 0);
  9135. FLockLevel--;
  9136. if (FLockLevel == 0)
  9137. {
  9138. DebugAssert(FLockSuspendLevel == 0);
  9139. Enabled = true;
  9140. }
  9141. }
  9142. //---------------------------------------------------------------------------
  9143. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9144. {
  9145. // We need to make sure that window is enabled when the last modal window closes
  9146. // otherwise focus is not restored correctly.
  9147. // So we re-enable the window when modal window opens and
  9148. // disable it back after is closes
  9149. if (FLockLevel > 0)
  9150. {
  9151. // while we have nesting counter, we know that we never be called
  9152. // recursivelly as Application->OnModalBegin is called only
  9153. // for the top-level modal window
  9154. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9155. {
  9156. // won't be disabled when conditions in LockWindow() were not satisfied
  9157. FDisabledOnLockSuspend = !Enabled;
  9158. // When minimized to tray (or actually when set to SW_HIDE),
  9159. // setting Enabled makes the window focusable even when there's
  9160. // modal window over it
  9161. if (!FTrayIcon->Visible)
  9162. {
  9163. Enabled = true;
  9164. }
  9165. }
  9166. FLockSuspendLevel++;
  9167. }
  9168. }
  9169. //---------------------------------------------------------------------------
  9170. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9171. {
  9172. if (FLockSuspendLevel > 0)
  9173. {
  9174. DebugAssert(FLockLevel > 0);
  9175. FLockSuspendLevel--;
  9176. // see comment in SuspendWindowLock
  9177. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9178. {
  9179. // Note that window can be enabled here, when we were minized to tray when
  9180. // was SuspendWindowLock() called.
  9181. // We should possibly do the same check as in LockWindow(),
  9182. // if it is ever possible that the consitions change between
  9183. // SuspendWindowLock() and ResumeWindowLock()
  9184. Enabled = !FDisabledOnLockSuspend;
  9185. }
  9186. }
  9187. }
  9188. //---------------------------------------------------------------------------
  9189. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9190. {
  9191. TTBXComboBoxItem * RemotePathComboBox =
  9192. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9193. if (!TextOnly)
  9194. {
  9195. TStrings * Items = RemotePathComboBox->Strings;
  9196. Items->BeginUpdate();
  9197. try
  9198. {
  9199. Items->Clear();
  9200. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9201. {
  9202. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9203. while (!IsUnixRootPath(APath))
  9204. {
  9205. int P = APath.LastDelimiter(L'/');
  9206. DebugAssert(P >= 0);
  9207. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9208. APath.SetLength(P - 1);
  9209. }
  9210. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9211. }
  9212. }
  9213. __finally
  9214. {
  9215. Items->EndUpdate();
  9216. }
  9217. }
  9218. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9219. // Setting ItemIndex to -1 does not reset its text
  9220. if (RemotePathComboBox->Strings->Count == 0)
  9221. {
  9222. RemotePathComboBox->Text = L"";
  9223. }
  9224. }
  9225. //---------------------------------------------------------------------------
  9226. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9227. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9228. {
  9229. if (AIndex < 0)
  9230. {
  9231. AIndex = Sender->ItemIndex;
  9232. }
  9233. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9234. }
  9235. //---------------------------------------------------------------------------
  9236. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9237. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9238. int & ImageIndex)
  9239. {
  9240. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9241. }
  9242. //---------------------------------------------------------------------------
  9243. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9244. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9245. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9246. {
  9247. if (!IsLocalBrowserMode())
  9248. {
  9249. ARect.Left += (10 * AIndex);
  9250. }
  9251. }
  9252. //---------------------------------------------------------------------------
  9253. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9254. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9255. {
  9256. AWidth += (10 * AIndex);
  9257. }
  9258. //---------------------------------------------------------------------------
  9259. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9260. {
  9261. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9262. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9263. int Index = RemotePathComboBox->ItemIndex;
  9264. while (Index < RemotePathComboBox->Strings->Count - 1)
  9265. {
  9266. APath = UnixExtractFileDir(APath);
  9267. Index++;
  9268. }
  9269. // VanDyke style paths
  9270. if (APath.IsEmpty())
  9271. {
  9272. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9273. APath = ROOTDIRECTORY;
  9274. }
  9275. if (RemoteDirView->Path != APath)
  9276. {
  9277. RemoteDirView->Path = APath;
  9278. }
  9279. }
  9280. //---------------------------------------------------------------------------
  9281. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9282. {
  9283. DoRemotePathComboBoxItemClick(Sender);
  9284. }
  9285. //---------------------------------------------------------------------------
  9286. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9287. {
  9288. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9289. DebugUsedParam(Sender);
  9290. UpdateRemotePathComboBox(true);
  9291. }
  9292. //---------------------------------------------------------------------------
  9293. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9294. {
  9295. DoRemotePathComboBoxCancel(Sender);
  9296. }
  9297. //---------------------------------------------------------------------------
  9298. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9299. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9300. {
  9301. ResetIncrementalSearch();
  9302. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9303. DebugAssert(DirView != NULL);
  9304. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9305. {
  9306. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9307. // OnEditing is called also from TCustomListView::CanEdit
  9308. if (Edit != NULL)
  9309. {
  9310. EditSelectBaseName(Edit);
  9311. }
  9312. }
  9313. }
  9314. //---------------------------------------------------------------------------
  9315. //---------------------------------------------------------------------------
  9316. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9317. {
  9318. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9319. Result->AutoDetectDnD = false;
  9320. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9321. Result->RenderDataOn = rdoEnterAndDropSync;
  9322. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9323. return Result;
  9324. }
  9325. //---------------------------------------------------------------------------
  9326. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9327. {
  9328. if (IsWin10Build(2004))
  9329. {
  9330. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9331. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9332. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9333. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9334. }
  9335. }
  9336. //---------------------------------------------------------------------------
  9337. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9338. {
  9339. TForm::CreateWnd();
  9340. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9341. UpdateDarkMode();
  9342. if (FSessionsDragDropFilesEx == NULL)
  9343. {
  9344. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9345. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9346. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9347. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9348. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9349. }
  9350. if (FQueueDragDropFilesEx == NULL)
  9351. {
  9352. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9353. // No need to set OnDragOver as we do not have any restrictions
  9354. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9355. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9356. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9357. }
  9358. }
  9359. //---------------------------------------------------------------------------
  9360. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9361. {
  9362. TForm::DestroyWnd();
  9363. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9364. FQueueDragDropFilesEx->DragDropControl = NULL;
  9365. }
  9366. //---------------------------------------------------------------------------
  9367. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9368. {
  9369. SideEnter(FCurrentSide);
  9370. FEverShown = true;
  9371. }
  9372. //---------------------------------------------------------------------------
  9373. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9374. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9375. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9376. {
  9377. if (!NonVisualDataModule->Busy)
  9378. {
  9379. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9380. Configuration->Usage->Inc(L"FileFinds");
  9381. LockWindow(true);
  9382. NonVisualDataModule->StartBusy();
  9383. WinConfiguration->LockedInterface = true;
  9384. try
  9385. {
  9386. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9387. }
  9388. __finally
  9389. {
  9390. WinConfiguration->LockedInterface = false;
  9391. NonVisualDataModule->EndBusy();
  9392. UnlockWindow();
  9393. }
  9394. }
  9395. }
  9396. //---------------------------------------------------------------------------
  9397. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9398. {
  9399. if (!NonVisualDataModule->Busy)
  9400. {
  9401. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9402. SetFocus();
  9403. RemoteDirView->Path = UnixExtractFilePath(Path);
  9404. UnicodeString FileName = UnixExtractFileName(Path);
  9405. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9406. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9407. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9408. if (Item == NULL)
  9409. {
  9410. ReloadDirectory(osRemote);
  9411. Item = RemoteDirView->FindFileItem(FileName);
  9412. }
  9413. if (Item != NULL)
  9414. {
  9415. RemoteDirView->ItemFocused = Item;
  9416. RemoteDirView->ItemFocused->MakeVisible(false);
  9417. RemoteDirView->SetFocus();
  9418. }
  9419. }
  9420. }
  9421. //---------------------------------------------------------------------------
  9422. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9423. {
  9424. bool Result = !NonVisualDataModule->Busy;
  9425. if (Result)
  9426. {
  9427. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9428. }
  9429. return Result;
  9430. }
  9431. //---------------------------------------------------------------------------
  9432. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9433. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9434. {
  9435. if (CanOperateOnFoundFiles(ATerminal))
  9436. {
  9437. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9438. FOnFileOperationFinished = OnFileOperationFinished;
  9439. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9440. }
  9441. }
  9442. //---------------------------------------------------------------------------
  9443. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9444. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9445. {
  9446. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9447. }
  9448. //---------------------------------------------------------------------------
  9449. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9450. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9451. {
  9452. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9453. }
  9454. //---------------------------------------------------------------------------
  9455. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9456. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9457. {
  9458. if (CanOperateOnFoundFiles(ATerminal))
  9459. {
  9460. for (int Index = 0; Index < FileList->Count; Index++)
  9461. {
  9462. UnicodeString FileName = FileList->Strings[Index];
  9463. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9464. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9465. OnFileOperationFinished(osRemote, FileName, true);
  9466. }
  9467. }
  9468. }
  9469. //---------------------------------------------------------------------------
  9470. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9471. {
  9472. FFileFindTerminal = Terminal;
  9473. ShowFileFindDialog(
  9474. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9475. DoDownloadFoundFiles, DoEditFoundFiles);
  9476. }
  9477. //---------------------------------------------------------------------------
  9478. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9479. {
  9480. FTaskbarList = TaskbarList;
  9481. }
  9482. //---------------------------------------------------------------------------
  9483. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9484. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9485. {
  9486. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9487. if (Index >= 0)
  9488. {
  9489. if (Button == mbLeft)
  9490. {
  9491. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9492. // If switching tab (switching session) takes long enough for user
  9493. // to actually release the button, "mouse down" is still raised,
  9494. // but we do not get "mouse up" event, so dragging is not cancelled,
  9495. // prevent that by not beginning dragging in the first place.
  9496. if (IsKeyPressed(VK_LBUTTON))
  9497. {
  9498. // when user clicks the "+", we get mouse down only after the session
  9499. // is closed, when new session tab is already on X:Y, so dragging
  9500. // starts, prevent that
  9501. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9502. {
  9503. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9504. if (Terminal != NULL)
  9505. {
  9506. SessionsPageControl->BeginDrag(false);
  9507. }
  9508. }
  9509. }
  9510. }
  9511. else if (Button == mbMiddle)
  9512. {
  9513. // ignore middle-click for "New session tab" and the last local-local tab
  9514. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9515. if ((Session != NULL) && CanCloseSession(Session))
  9516. {
  9517. CloseSessionTab(Index);
  9518. }
  9519. }
  9520. }
  9521. }
  9522. //---------------------------------------------------------------------------
  9523. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9524. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9525. {
  9526. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9527. // do not allow dropping on the "+" tab
  9528. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9529. if ((TargetTerminal != NULL) &&
  9530. (SessionsPageControl->ActivePage->PageIndex != Index))
  9531. {
  9532. Configuration->Usage->Inc(L"SessionTabMoves");
  9533. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9534. // but we want to actually prevent that to avoid flicker
  9535. SessionsPageControl->ActivePage->PageIndex = Index;
  9536. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9537. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9538. }
  9539. }
  9540. //---------------------------------------------------------------------------
  9541. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9542. TObject * Sender, TObject * Source, int X, int Y,
  9543. TDragState /*State*/, bool & Accept)
  9544. {
  9545. Accept = (Sender == Source);
  9546. if (Accept)
  9547. {
  9548. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9549. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9550. // do not allow dragging to the "+" tab
  9551. Accept = (Terminal != NULL);
  9552. }
  9553. }
  9554. //---------------------------------------------------------------------------
  9555. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9556. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9557. {
  9558. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9559. if (Index < 0)
  9560. {
  9561. Effect = DROPEFFECT_NONE;
  9562. }
  9563. else
  9564. {
  9565. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9566. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9567. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9568. {
  9569. Effect = DROPEFFECT_NONE;
  9570. }
  9571. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9572. {
  9573. Effect = DROPEFFECT_COPY;
  9574. }
  9575. }
  9576. }
  9577. //---------------------------------------------------------------------------
  9578. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9579. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9580. {
  9581. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9582. // do not allow dropping on local-local, disconnected and the "+" tab
  9583. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9584. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9585. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9586. {
  9587. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9588. if (!IsFileControl(DropSourceControl, osRemote))
  9589. {
  9590. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9591. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9592. // Why don't we use Terminal->CurrentDirectory directly?
  9593. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9594. // do not force queue, drag drop
  9595. false, true);
  9596. }
  9597. }
  9598. }
  9599. //---------------------------------------------------------------------------
  9600. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9601. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9602. {
  9603. // Downloads are handled in RemoteFileControlDDEnd
  9604. if (!IsFileControl(DropSourceControl, osRemote))
  9605. {
  9606. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9607. Terminal->CurrentDirectory,
  9608. // force queue, drag drop
  9609. true, true);
  9610. }
  9611. }
  9612. //---------------------------------------------------------------------------
  9613. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9614. {
  9615. FShowing = false;
  9616. // Do not save empty workspace
  9617. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9618. {
  9619. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9620. UnicodeString Name = WorkspaceName();
  9621. DoSaveWorkspace(
  9622. Name, DataList.get(),
  9623. !Configuration->DisablePasswordStoring &&
  9624. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9625. WinConfiguration->LastStoredSession = Name;
  9626. }
  9627. }
  9628. //---------------------------------------------------------------------------
  9629. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9630. {
  9631. TManagedTerminal * ManagedTerminal =
  9632. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9633. if (ManagedTerminal != NULL)
  9634. {
  9635. ManagedTerminal->DirectoryLoaded = Now();
  9636. }
  9637. }
  9638. //---------------------------------------------------------------------------
  9639. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9640. TListItem * /*Item*/, bool /*Selected*/)
  9641. {
  9642. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9643. switch (DirView->LastSelectMethod)
  9644. {
  9645. case smKeyboard:
  9646. Configuration->Usage->Inc(L"KeyboardSelections");
  9647. break;
  9648. case smMouse:
  9649. Configuration->Usage->Inc(L"MouseSelections");
  9650. break;
  9651. }
  9652. }
  9653. //---------------------------------------------------------------------------
  9654. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9655. {
  9656. if (Color != 0)
  9657. {
  9658. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9659. }
  9660. else
  9661. {
  9662. return FSessionTabImageIndex;
  9663. }
  9664. }
  9665. //---------------------------------------------------------------------------
  9666. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9667. {
  9668. TPngImageCollectionItem * Item =
  9669. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9670. FSessionColors->AddPng(Item->PngImage);
  9671. return FSessionColors->Count - 1;
  9672. }
  9673. //---------------------------------------------------------------------------
  9674. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9675. {
  9676. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9677. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9678. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9679. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9680. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9681. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9682. }
  9683. //---------------------------------------------------------------------------
  9684. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9685. {
  9686. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9687. }
  9688. //---------------------------------------------------------------------------
  9689. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9690. {
  9691. switch (Side)
  9692. {
  9693. case osLocal:
  9694. return FNewLocalTabTabImageIndex;
  9695. case osRemote:
  9696. case osCurrent:
  9697. return FNewRemoteTabTabImageIndex;
  9698. default:
  9699. DebugFail();
  9700. return -1;
  9701. }
  9702. }
  9703. //---------------------------------------------------------------------------
  9704. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9705. {
  9706. for (int Index = 0; Index < Control->ControlCount; Index++)
  9707. {
  9708. TControl * AControl = Control->Controls[Index];
  9709. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9710. if (WinControl != NULL)
  9711. {
  9712. CollectItemsWithTextDisplayMode(WinControl);
  9713. }
  9714. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9715. if (Toolbar != NULL)
  9716. {
  9717. // we care for top-level items only
  9718. TTBCustomItem * Items = Toolbar->Items;
  9719. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9720. {
  9721. TTBCustomItem * Item = Items->Items[ItemIndex];
  9722. if (((Item->DisplayMode == nbdmImageAndText) ||
  9723. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9724. EligibleForImageDisplayMode(Item))
  9725. {
  9726. FItemsWithTextDisplayMode.insert(Item);
  9727. }
  9728. }
  9729. }
  9730. }
  9731. }
  9732. //---------------------------------------------------------------------------
  9733. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9734. {
  9735. return true;
  9736. }
  9737. //---------------------------------------------------------------------------
  9738. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9739. {
  9740. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9741. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9742. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9743. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9744. bool Result = true;
  9745. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9746. {
  9747. TTBCustomItem * Item = *i;
  9748. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9749. if (Label != NULL)
  9750. {
  9751. // optimization
  9752. if (Label->Visible == SelectiveToolbarText)
  9753. {
  9754. Result = false;
  9755. }
  9756. else
  9757. {
  9758. Label->Visible = SelectiveToolbarText;
  9759. }
  9760. }
  9761. else
  9762. {
  9763. // optimization
  9764. if (Item->DisplayMode == DisplayMode)
  9765. {
  9766. Result = false;
  9767. }
  9768. else
  9769. {
  9770. Item->DisplayMode = DisplayMode;
  9771. }
  9772. }
  9773. ++i;
  9774. }
  9775. UpdateNewTabTab();
  9776. return Result;
  9777. }
  9778. //---------------------------------------------------------------------------
  9779. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9780. {
  9781. DebugFail();
  9782. }
  9783. //---------------------------------------------------------------------------
  9784. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9785. {
  9786. // Among other, a lock level is non zero, while directory is being loaded,
  9787. // even when it happens as a result of dirview navigation,
  9788. // i.e. when TNonVisualDataModule is NOT FBusy.
  9789. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9790. // Among other this prevents a panel auto update to occur while
  9791. // directory is changing.
  9792. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9793. }
  9794. //---------------------------------------------------------------------------
  9795. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9796. {
  9797. // While the window is disabled, we still seem to process menu shortcuts at least,
  9798. // so stop it at least here.
  9799. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9800. bool Result;
  9801. if (Allowed == aaUpdate)
  9802. {
  9803. Result = true;
  9804. }
  9805. else
  9806. {
  9807. Result = !NonVisualDataModule->Busy;
  9808. if (Result && (Allowed == aaShortCut))
  9809. {
  9810. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9811. Action->Update();
  9812. Result = Action->Enabled;
  9813. }
  9814. }
  9815. return Result;
  9816. }
  9817. //---------------------------------------------------------------------------
  9818. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9819. {
  9820. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9821. }
  9822. //---------------------------------------------------------------------------
  9823. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9824. {
  9825. // This is somewhat redundant to LockWindow() call from
  9826. // TTerminalManager::TerminalReadDirectoryProgress.
  9827. // But disabling window is known not to block keyboard shorcuts
  9828. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9829. if (Busy > 0)
  9830. {
  9831. if (NonVisualDataModule->Busy)
  9832. {
  9833. State = false;
  9834. }
  9835. else
  9836. {
  9837. NonVisualDataModule->StartBusy();
  9838. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9839. LockWindow();
  9840. }
  9841. }
  9842. else if (Busy < 0)
  9843. {
  9844. UnlockWindow();
  9845. NonVisualDataModule->EndBusy();
  9846. }
  9847. else
  9848. {
  9849. State = NonVisualDataModule->Busy;
  9850. }
  9851. }
  9852. //---------------------------------------------------------------------------
  9853. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9854. {
  9855. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9856. if (Index >= 0)
  9857. {
  9858. TPopupMenu * PopupMenu = NULL;
  9859. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9860. if (Session != NULL)
  9861. {
  9862. SessionsPageControl->ActivePageIndex = Index;
  9863. if (DebugAlwaysTrue(SessionTabSwitched()))
  9864. {
  9865. // copied from TControl.WMContextMenu
  9866. SendCancelMode(SessionsPageControl);
  9867. // explicit popup instead of using PopupMenu property
  9868. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9869. // while connecting yet not-connected session and hence
  9870. // allowing an access to commands over not-completelly connected session
  9871. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9872. }
  9873. }
  9874. else
  9875. {
  9876. if (SupportsLocalBrowser())
  9877. {
  9878. PopupMenu = NonVisualDataModule->NewTabPopup;
  9879. }
  9880. }
  9881. if (PopupMenu != NULL)
  9882. {
  9883. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9884. PopupMenu->PopupComponent = SessionsPageControl;
  9885. PopupMenu->Popup(Point.x, Point.y);
  9886. }
  9887. }
  9888. Handled = true;
  9889. }
  9890. //---------------------------------------------------------------------------
  9891. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9892. {
  9893. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9894. }
  9895. //---------------------------------------------------------------------------
  9896. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9897. {
  9898. Configuration->Usage->Inc(L"PasswordChanges");
  9899. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9900. ChangePasswordData->DisableAuthentationsExceptPassword();
  9901. ChangePasswordData->ChangePassword = true;
  9902. std::unique_ptr<TTerminal> ChangePasswordSession(
  9903. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9904. ChangePasswordSession->AutoReadDirectory = false;
  9905. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9906. {
  9907. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9908. }
  9909. else
  9910. {
  9911. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9912. }
  9913. }
  9914. //---------------------------------------------------------------------------
  9915. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9916. {
  9917. // No nice way to assert SSH2
  9918. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9919. }
  9920. //---------------------------------------------------------------------------
  9921. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9922. {
  9923. TOperationVisualizer OperationVisualizer;
  9924. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9925. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9926. }
  9927. //---------------------------------------------------------------------------
  9928. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9929. {
  9930. TForm::ChangeScale(M, D);
  9931. int APixelsPerInch = GetControlPixelsPerInch(this);
  9932. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9933. try
  9934. {
  9935. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9936. }
  9937. __finally
  9938. {
  9939. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9940. }
  9941. }
  9942. //---------------------------------------------------------------------------
  9943. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9944. {
  9945. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9946. }
  9947. //---------------------------------------------------------------------
  9948. class TPublicControl : public TControl
  9949. {
  9950. friend class TCustomScpExplorerForm;
  9951. };
  9952. //---------------------------------------------------------------------------
  9953. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9954. {
  9955. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9956. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9957. }
  9958. //---------------------------------------------------------------------------
  9959. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9960. {
  9961. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9962. OpenBookmark(Side, Bookmark);
  9963. }
  9964. //---------------------------------------------------------------------------
  9965. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9966. {
  9967. DoBookmarkClick(osLocal, Sender);
  9968. }
  9969. //---------------------------------------------------------------------------
  9970. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9971. {
  9972. DoBookmarkClick(osRemote, Sender);
  9973. }
  9974. //---------------------------------------------------------------------------
  9975. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9976. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9977. {
  9978. if (BookmarkList != NULL)
  9979. {
  9980. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9981. if (IsLocalBrowserMode())
  9982. {
  9983. Side = osLocal;
  9984. }
  9985. if (!WinConfiguration->UseLocationProfiles)
  9986. {
  9987. std::unique_ptr<TStringList> Directories(new TStringList());
  9988. Directories->CaseSensitive = (Side == osRemote);
  9989. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9990. {
  9991. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9992. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9993. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9994. {
  9995. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9996. Item->Caption = EscapeHotkey(Directory);
  9997. Item->ShortCut = Bookmark->ShortCut;
  9998. Item->OnClick = OnBookmarkClick;
  9999. Item->Tag = reinterpret_cast<int>(Bookmark);
  10000. Directories->Add(Directory);
  10001. Menu->Add(Item.release());
  10002. }
  10003. }
  10004. }
  10005. else
  10006. {
  10007. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10008. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10009. {
  10010. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10011. if (!Bookmark->Node.IsEmpty())
  10012. {
  10013. Folders->Add(Bookmark->Node);
  10014. }
  10015. }
  10016. for (int Index = 0; Index < Folders->Count; Index++)
  10017. {
  10018. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10019. Item->Caption = Folders->Strings[Index];
  10020. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10021. Folders->Objects[Index] = Item.get();
  10022. Menu->Add(Item.release());
  10023. }
  10024. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10025. {
  10026. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10027. TTBCustomItem * Parent;
  10028. if (!Bookmark->Node.IsEmpty())
  10029. {
  10030. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10031. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10032. }
  10033. else
  10034. {
  10035. Parent = Menu;
  10036. }
  10037. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10038. Item->Caption = Bookmark->Name;
  10039. Item->ShortCut = Bookmark->ShortCut;
  10040. Item->OnClick = OnBookmarkClick;
  10041. Item->Tag = reinterpret_cast<int>(Bookmark);
  10042. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10043. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10044. {
  10045. // No hint for location profiles that are actually mere "bookmarks"
  10046. }
  10047. else
  10048. {
  10049. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10050. UnicodeString LongHint;
  10051. if (!Bookmark->Local.IsEmpty())
  10052. {
  10053. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10054. }
  10055. if (!Bookmark->Remote.IsEmpty())
  10056. {
  10057. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10058. }
  10059. AddToList(Hint, LongHint, L"|");
  10060. Item->Hint = Hint;
  10061. }
  10062. Parent->Add(Item.release());
  10063. }
  10064. }
  10065. AddMenuSeparator(Menu);
  10066. }
  10067. }
  10068. //---------------------------------------------------------------------------
  10069. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10070. {
  10071. Menu->Clear();
  10072. std::unique_ptr<TTBCustomItem> Item;
  10073. Item.reset(new TTBXItem(Owner));
  10074. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10075. Item->Options = Item->Options << tboDefault;
  10076. Menu->Add(Item.release());
  10077. AddMenuSeparator(Menu);
  10078. if (Terminal != NULL)
  10079. {
  10080. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10081. }
  10082. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10083. Item.reset(new TTBXItem(Owner));
  10084. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10085. Menu->Add(Item.release());
  10086. }
  10087. //---------------------------------------------------------------------------
  10088. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10089. {
  10090. bool Result = true;
  10091. bool Remote = !IsSideLocalBrowser(Side);
  10092. try
  10093. {
  10094. if (Remote)
  10095. {
  10096. Terminal->ExceptionOnFail = true;
  10097. }
  10098. try
  10099. {
  10100. DirView(Side)->Path = Path;
  10101. }
  10102. __finally
  10103. {
  10104. if (Remote)
  10105. {
  10106. Terminal->ExceptionOnFail = false;
  10107. }
  10108. }
  10109. }
  10110. catch (Exception & E)
  10111. {
  10112. if (!Remote || Terminal->Active)
  10113. {
  10114. ShowExtendedExceptionEx(Terminal, &E);
  10115. Result = false;
  10116. }
  10117. else
  10118. {
  10119. throw;
  10120. }
  10121. }
  10122. return Result;
  10123. }
  10124. //---------------------------------------------------------------------------
  10125. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10126. {
  10127. // Make sure there some feedback even when loading tiny local folders
  10128. TInstantOperationVisualizer Visualizer;
  10129. DirView(Side)->ReloadDirectory();
  10130. }
  10131. //---------------------------------------------------------------------------
  10132. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10133. {
  10134. if (Index == SessionsPageControl->TabIndex)
  10135. {
  10136. CloseSession();
  10137. }
  10138. else
  10139. {
  10140. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10141. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10142. TTerminalManager * Manager = TTerminalManager::Instance();
  10143. if (!Terminal->Active)
  10144. {
  10145. Manager->FreeTerminal(Terminal);
  10146. }
  10147. else
  10148. {
  10149. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10150. if (CanCloseQueue(Queue))
  10151. {
  10152. Manager->FreeTerminal(Terminal);
  10153. }
  10154. }
  10155. }
  10156. }
  10157. //---------------------------------------------------------------------------
  10158. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10159. {
  10160. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10161. if (Session == NULL)
  10162. {
  10163. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10164. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10165. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10166. //P = SessionsPageControl->ClientToScreen(P);
  10167. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10168. }
  10169. else
  10170. {
  10171. CloseSessionTab(Index);
  10172. }
  10173. }
  10174. //---------------------------------------------------------------------------
  10175. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10176. {
  10177. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10178. {
  10179. TInstantOperationVisualizer Visualizer;
  10180. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10181. // before we create a new data object
  10182. ClipboardClear();
  10183. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10184. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10185. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10186. // Intermediate variable to WORKAROUND a crash in compiler
  10187. TObjectList * ClipboardFakeMonitors =
  10188. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10189. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10190. if (FClipboardDragDropFilesEx.get() == NULL)
  10191. {
  10192. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10193. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10194. }
  10195. FClipboardDragDropFilesEx->FileList->Clear();
  10196. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10197. FClipboardDragDropFilesEx->CopyToClipboard();
  10198. FClipboardTerminal = Terminal;
  10199. // Need full paths, as cwd can be different once files are pasted
  10200. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10201. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10202. {
  10203. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10204. }
  10205. }
  10206. }
  10207. //---------------------------------------------------------------------------
  10208. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10209. {
  10210. if (OpenClipboard(0))
  10211. {
  10212. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10213. CloseClipboard();
  10214. }
  10215. }
  10216. //---------------------------------------------------------------------------
  10217. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10218. {
  10219. return !FClipboardFakeDirectory.IsEmpty();
  10220. }
  10221. //---------------------------------------------------------------------------
  10222. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10223. {
  10224. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10225. FClipboardFakeDirectory = UnicodeString();
  10226. FClipboardTerminal = NULL;
  10227. // Also called in Idle()
  10228. FClipboardFakeMonitors.reset(NULL);
  10229. }
  10230. //---------------------------------------------------------------------------
  10231. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10232. {
  10233. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10234. {
  10235. ClipboardStop();
  10236. }
  10237. }
  10238. //---------------------------------------------------------------------------
  10239. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10240. {
  10241. TTerminalManager * Manager = TTerminalManager::Instance();
  10242. Manager->ActiveSession = FClipboardTerminal;
  10243. TTransferOperationParam Params;
  10244. Params.TargetDirectory = TargetDirectory;
  10245. Params.DragDrop = DragDrop;
  10246. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10247. }
  10248. //---------------------------------------------------------------------------
  10249. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10250. {
  10251. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10252. // (as it pumps a Windows message queue while being shutted down)
  10253. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10254. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10255. {
  10256. // Can fail as it can be e.g. locked by AV, so we retry that later
  10257. bool Removed = RemoveDir(ApiPath(FileName));
  10258. try
  10259. {
  10260. if (!NonVisualDataModule->Busy)
  10261. {
  10262. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10263. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10264. }
  10265. }
  10266. __finally
  10267. {
  10268. if (!Removed)
  10269. {
  10270. RemoveDir(ApiPath(FileName));
  10271. }
  10272. }
  10273. }
  10274. }
  10275. //---------------------------------------------------------------------------
  10276. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10277. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10278. {
  10279. TFileMasks::TParams MaskParams;
  10280. MaskParams.Size = Size;
  10281. MaskParams.Modification = Modification;
  10282. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10283. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10284. {
  10285. bool ImplicitMatch;
  10286. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10287. !ImplicitMatch)
  10288. {
  10289. Color = Iter->Color;
  10290. break;
  10291. }
  10292. }
  10293. }
  10294. //---------------------------------------------------------------------------
  10295. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10296. {
  10297. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10298. if (!DirView->IsEditing())
  10299. {
  10300. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10301. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10302. // while searching
  10303. if (Key == VK_BACK)
  10304. {
  10305. if (!FIncrementalSearch.IsEmpty())
  10306. {
  10307. if (FIncrementalSearch.Length() == 1)
  10308. {
  10309. ResetIncrementalSearch();
  10310. }
  10311. else
  10312. {
  10313. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10314. IncrementalSearch(NewText, false, false);
  10315. }
  10316. Key = 0;
  10317. }
  10318. }
  10319. }
  10320. }
  10321. //---------------------------------------------------------------------------
  10322. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10323. {
  10324. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10325. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10326. {
  10327. // Filter control sequences.
  10328. // When not searching yet, prefer use of the space for toggling file selection
  10329. // (so we cannot incrementally search for a string starting with a space).
  10330. if ((Key > VK_SPACE) ||
  10331. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10332. {
  10333. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10334. Key = 0;
  10335. }
  10336. }
  10337. }
  10338. //---------------------------------------------------------------------------
  10339. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10340. {
  10341. if (!FIncrementalSearch.IsEmpty())
  10342. {
  10343. FIncrementalSearch = L"";
  10344. DirView(osCurrent)->UpdateStatusBar();
  10345. }
  10346. }
  10347. //---------------------------------------------------------------------------
  10348. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10349. {
  10350. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10351. if (Item == NULL)
  10352. {
  10353. MessageBeep(MB_ICONHAND);
  10354. }
  10355. else
  10356. {
  10357. TCustomDirView * ADirView = DirView(osCurrent);
  10358. {
  10359. TAutoNestingCounter Guard(FIncrementalSearching);
  10360. ADirView->FocusItem(Item);
  10361. if (ADirView->NortonLike == nlOff)
  10362. {
  10363. if ((ADirView->Selected != Item) ||
  10364. (ADirView->SelCount > 1))
  10365. {
  10366. ADirView->ClearSelection();
  10367. ADirView->Selected = Item;
  10368. }
  10369. }
  10370. }
  10371. FIncrementalSearch = Text;
  10372. Item->MakeVisible(false);
  10373. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10374. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10375. ADirView->UpdateStatusBar();
  10376. }
  10377. }
  10378. //---------------------------------------------------------------------------
  10379. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10380. {
  10381. int Index = Item->Index;
  10382. if (!Reverse)
  10383. {
  10384. Index++;
  10385. if (Index >= Item->Owner->Count)
  10386. {
  10387. Index = 0;
  10388. }
  10389. }
  10390. else
  10391. {
  10392. Index--;
  10393. if (Index < 0)
  10394. {
  10395. Index = Item->Owner->Count - 1;
  10396. }
  10397. }
  10398. return Item->Owner->Item[Index];
  10399. }
  10400. //---------------------------------------------------------------------------
  10401. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10402. {
  10403. TCustomDirView * ADirView = DirView(osCurrent);
  10404. if (ADirView->Items->Count == 0)
  10405. {
  10406. return NULL;
  10407. }
  10408. else
  10409. {
  10410. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10411. TListItem * Item = CurrentItem;
  10412. if (SkipCurrent)
  10413. {
  10414. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10415. }
  10416. while (true)
  10417. {
  10418. bool Matches = false;
  10419. switch (WinConfiguration->PanelSearch)
  10420. {
  10421. case isNameStartOnly:
  10422. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10423. break;
  10424. case isName:
  10425. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10426. break;
  10427. case isAll:
  10428. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10429. int Index = 0;
  10430. while ((Index < ColCount) && !Matches)
  10431. {
  10432. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10433. if (Visible)
  10434. {
  10435. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10436. }
  10437. Index++;
  10438. }
  10439. break;
  10440. }
  10441. if (Matches)
  10442. {
  10443. return Item;
  10444. }
  10445. Item = GetNextFile(Item, Reverse);
  10446. if (Item == CurrentItem)
  10447. {
  10448. return NULL;
  10449. }
  10450. }
  10451. }
  10452. }
  10453. //---------------------------------------------------------------------------
  10454. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10455. {
  10456. ResetIncrementalSearch();
  10457. }
  10458. //---------------------------------------------------------------------------
  10459. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10460. {
  10461. if (Message.CharCode == VK_TAB)
  10462. {
  10463. if (!FIncrementalSearch.IsEmpty())
  10464. {
  10465. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10466. bool Reverse = Shift.Contains(ssShift);
  10467. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10468. Message.Result = 1;
  10469. return;
  10470. }
  10471. }
  10472. else if (Message.CharCode == VK_ESCAPE)
  10473. {
  10474. if (!FIncrementalSearch.IsEmpty())
  10475. {
  10476. ResetIncrementalSearch();
  10477. Message.Result = 1;
  10478. return;
  10479. }
  10480. }
  10481. TForm::Dispatch(&Message);
  10482. }
  10483. //---------------------------------------------------------------------------
  10484. void __fastcall TCustomScpExplorerForm::Deactivate()
  10485. {
  10486. TForm::Deactivate();
  10487. // E.g. when switching to an internal editor window
  10488. ResetIncrementalSearch();
  10489. }
  10490. //---------------------------------------------------------------------------
  10491. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10492. {
  10493. // When switching to another application
  10494. ResetIncrementalSearch();
  10495. }
  10496. //---------------------------------------------------------------------------
  10497. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10498. {
  10499. ResetIncrementalSearch();
  10500. }
  10501. //---------------------------------------------------------------------------
  10502. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10503. {
  10504. if (FIncrementalSearching <= 0)
  10505. {
  10506. ResetIncrementalSearch();
  10507. }
  10508. }
  10509. //---------------------------------------------------------------------------
  10510. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10511. {
  10512. CountClicksForWindowPrint(this);
  10513. }
  10514. //---------------------------------------------------------------------------
  10515. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10516. {
  10517. CenterReconnectToolbar();
  10518. }
  10519. //---------------------------------------------------------------------------
  10520. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10521. {
  10522. if (RemoteDirView->ItemFocused != NULL)
  10523. {
  10524. RemoteDirView->ItemFocused->Selected = true;
  10525. }
  10526. }
  10527. //---------------------------------------------------------------------------
  10528. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10529. {
  10530. if (!FQueueStatusUpdating)
  10531. {
  10532. bool Refresh;
  10533. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10534. {
  10535. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10536. }
  10537. else
  10538. {
  10539. if (FQueueFileList->GetCount() > 0)
  10540. {
  10541. FQueueFileList->Clear();
  10542. Refresh = true;
  10543. }
  10544. }
  10545. int Count = FQueueFileList->GetCount();
  10546. if (QueueFileList->Items->Count != Count)
  10547. {
  10548. DebugAssert(Refresh);
  10549. QueueFileList->Items->Count = Count;
  10550. }
  10551. if (Refresh)
  10552. {
  10553. QueueFileList->Invalidate();
  10554. }
  10555. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10556. }
  10557. }
  10558. //---------------------------------------------------------------------------
  10559. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10560. {
  10561. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10562. }
  10563. //---------------------------------------------------------------------------
  10564. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10565. {
  10566. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10567. // See TSynchronizeChecklistDialog::UpdateTimer
  10568. if (QueueFileList->Items->Count > 0)
  10569. {
  10570. QueueFileList->Items->Item[0]->MakeVisible(false);
  10571. }
  10572. UpdateQueueFileList();
  10573. }
  10574. //---------------------------------------------------------------------------
  10575. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10576. {
  10577. Active = QueueView3->Focused() || QueueFileList->Focused();
  10578. }
  10579. //---------------------------------------------------------------------------
  10580. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10581. {
  10582. QueueLabelUpdateStatus();
  10583. }
  10584. //---------------------------------------------------------------------------
  10585. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10586. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10587. {
  10588. int State = FQueueFileList->GetState(Item->Index);
  10589. if (State == qfsProcessed)
  10590. {
  10591. Sender->Canvas->Font->Color = clGrayText;
  10592. }
  10593. }
  10594. //---------------------------------------------------------------------------
  10595. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10596. {
  10597. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10598. QueueFileList->Column[0]->Width =
  10599. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10600. }
  10601. //---------------------------------------------------------------------------
  10602. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10603. {
  10604. QueueFileListColumnAutoSize();
  10605. }
  10606. //---------------------------------------------------------------------------
  10607. void __fastcall TCustomScpExplorerForm::CloseApp()
  10608. {
  10609. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10610. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10611. // So we clear the flag here.
  10612. DebugAssert(NonVisualDataModule->Busy);
  10613. NonVisualDataModule->EndBusy();
  10614. try
  10615. {
  10616. Close();
  10617. }
  10618. __finally
  10619. {
  10620. NonVisualDataModule->StartBusy();
  10621. }
  10622. }
  10623. //---------------------------------------------------------------------------
  10624. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10625. {
  10626. return (Terminal != NULL) && Terminal->Active;
  10627. }
  10628. //---------------------------------------------------------------------------
  10629. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10630. {
  10631. return (ManagedSession != NULL);
  10632. }
  10633. //---------------------------------------------------------------------------
  10634. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10635. {
  10636. return IsActiveTerminal(Terminal);
  10637. }
  10638. //---------------------------------------------------------------------------
  10639. void TCustomScpExplorerForm::LocalLocalCopy(
  10640. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10641. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10642. {
  10643. DebugFail();
  10644. }
  10645. //---------------------------------------------------------------------------
  10646. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10647. {
  10648. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10649. }
  10650. //---------------------------------------------------------------------------
  10651. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10652. {
  10653. // Changing tab list triggers the OnResize.
  10654. if (!FUpdatingSessionTabs)
  10655. {
  10656. // UpdateTabsCaptionTruncation should be enough
  10657. SessionListChanged(true);
  10658. }
  10659. }
  10660. //---------------------------------------------------------------------------
  10661. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10662. {
  10663. if (ASession == ManagedSession)
  10664. {
  10665. UpdateSession(ASession);
  10666. }
  10667. TSessionData * Data = ASession->StateData;
  10668. UnicodeString Result;
  10669. if (Side == osLocal)
  10670. {
  10671. Result = Data->LocalDirectory;
  10672. }
  10673. else if (DebugAlwaysTrue(Side == osOther))
  10674. {
  10675. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10676. }
  10677. return Result;
  10678. }
  10679. //---------------------------------------------------------------------------
  10680. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10681. {
  10682. UnicodeString Result;
  10683. if (!ASession->Active)
  10684. {
  10685. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10686. }
  10687. else
  10688. {
  10689. Result = GetSessionPath(ASession, osRemote);
  10690. }
  10691. return Result;
  10692. }
  10693. //---------------------------------------------------------------------------
  10694. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10695. {
  10696. return LoadStr(NEW_REMOTE_TAB_HINT);
  10697. }
  10698. //---------------------------------------------------------------------------
  10699. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10700. {
  10701. if (Index >= 0)
  10702. {
  10703. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10704. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10705. if (TabSession == NULL)
  10706. {
  10707. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10708. if (NewTabTabCaption != TabSheet->Caption)
  10709. {
  10710. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10711. }
  10712. }
  10713. else
  10714. {
  10715. TTerminalManager * Manager = TTerminalManager::Instance();
  10716. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10717. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10718. }
  10719. }
  10720. }
  10721. //---------------------------------------------------------------------------
  10722. typedef std::vector<TListItem *> TListItemsVector;
  10723. struct TCalculateSizeOperation
  10724. {
  10725. TCalculateSizeStats Stats;
  10726. TListItemsVector ListItems;
  10727. size_t Index;
  10728. };
  10729. //---------------------------------------------------------------------------
  10730. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10731. {
  10732. TCalculateSizeOperation CalculateSizeOperation;
  10733. TOperationSide Side = GetSide(osCurrent);
  10734. TCustomDirView * ADirView = DirView(Side);
  10735. bool FullPath = IsSideLocalBrowser(Side);
  10736. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10737. std::unique_ptr<TStrings> FileList(new TStringList());
  10738. for (int Index = 0; Index < AllFileList->Count; Index++)
  10739. {
  10740. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10741. if (ADirView->ItemIsDirectory(Item))
  10742. {
  10743. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10744. CalculateSizeOperation.ListItems.push_back(Item);
  10745. }
  10746. }
  10747. CalculateSizeOperation.Index = 0;
  10748. __int64 Size = 0;
  10749. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10750. FCalculateSizeOperation = &CalculateSizeOperation;
  10751. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10752. FOnFileOperationFinished = DirectorySizeCalculated;
  10753. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10754. FPrimaryOperation = foCalculateSize;
  10755. TCalculatedSizes CalculatedSizes;
  10756. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10757. if (Side == osLocal)
  10758. {
  10759. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10760. }
  10761. else
  10762. {
  10763. TCalculateSizeParams Params;
  10764. Params.Stats = &CalculateSizeOperation.Stats;
  10765. Params.UseCache = true;
  10766. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10767. }
  10768. }
  10769. //---------------------------------------------------------------------------
  10770. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10771. {
  10772. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10773. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10774. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10775. // optimization
  10776. bool GotExpectedItem =
  10777. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10778. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10779. int Index = -1;
  10780. if (DebugAlwaysTrue(GotExpectedItem))
  10781. {
  10782. Index = FCalculateSizeOperation->Index;
  10783. FCalculateSizeOperation->Index++;
  10784. }
  10785. else
  10786. {
  10787. TListItemsVector::iterator I =
  10788. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  10789. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  10790. {
  10791. Index = I - FCalculateSizeOperation->ListItems.begin();
  10792. }
  10793. }
  10794. if (DebugAlwaysTrue(Index >= 0))
  10795. {
  10796. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  10797. ADirView->SetItemCalculatedSize(Item, Size);
  10798. }
  10799. }