CustomScpExplorer.cpp 378 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435
  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. FStandaloneEditing = false;
  194. FOnFeedSynchronizeError = NULL;
  195. FNeedSession = false;
  196. FDoNotIdleCurrentTerminal = 0;
  197. FIncrementalSearching = 0;
  198. FIncrementalSearchHaveNext = false;
  199. FQueueFileList.reset(new TQueueFileList());
  200. FProgressSide = osCurrent;
  201. FCalculateSizeOperation = NULL;
  202. FEditorManager = new TEditorManager();
  203. FEditorManager->OnFileChange = ExecutedFileChanged;
  204. FEditorManager->OnFileReload = ExecutedFileReload;
  205. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  206. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  207. FLocalEditors = new TList();
  208. FQueueStatus = NULL;
  209. FQueueStatusSection = new TCriticalSection();
  210. FQueueStatusInvalidated = false;
  211. FQueueItemInvalidated = false;
  212. FQueueStatusUpdating = false;
  213. FQueueActedItem = NULL;
  214. FQueueController = new TQueueController(QueueView3);
  215. FUserActionTimer = new TTimer(this);
  216. FUserActionTimer->Enabled = false;
  217. FUserActionTimer->Interval = 10;
  218. FUserActionTimer->OnTimer = UserActionTimer;
  219. FNotes = new TStringList();
  220. FNoteTimer = new TTimer(this);
  221. FNoteTimer->Enabled = false;
  222. FNoteTimer->OnTimer = NoteTimer;
  223. FOnNoteClick = NULL;
  224. FOle32Library = LoadLibrary(L"Ole32.dll");
  225. FDragMoveCursor = FOle32Library != NULL ?
  226. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  227. UseSystemSettings(this);
  228. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  229. static_cast<TObject*>(GetComponent(fcTransferList)));
  230. DebugAssert(TransferList != NULL);
  231. FTransferListHoverIndex = -1;
  232. TransferList->OnChange = TransferListChange;
  233. TransferList->OnDrawItem = TransferListDrawItem;
  234. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  235. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  236. UseDesktopFont(SessionsPageControl);
  237. UpdateSessionsPageControlHeight();
  238. UseDesktopFont(RemoteDirView);
  239. UseDesktopFont(RemoteDriveView);
  240. UseDesktopFont(QueueView3);
  241. UseDesktopFont(QueueFileList);
  242. UseDesktopFont(QueueLabel);
  243. UseDesktopFont(RemoteStatusBar);
  244. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  245. QueueSplitter->ShowHint = true;
  246. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  247. QueueFileListSplitter->ShowHint = true;
  248. RemotePanelSplitter->ShowHint = true;
  249. UpdateImages();
  250. FCustomCommandMenu = CreateTBXPopupMenu(this);
  251. FCustomCommandLocalFileList = NULL;
  252. FCustomCommandRemoteFileList = NULL;
  253. FSessionColors = new TPngImageList(this);
  254. FSessionColors->ColorDepth = cd32Bit;
  255. AddFixedSessionImages();
  256. SessionsPageControl->Images = FSessionColors;
  257. UpdateQueueLabel();
  258. CreateHiddenWindow();
  259. StartUpdates();
  260. }
  261. //---------------------------------------------------------------------------
  262. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  263. {
  264. FInvalid = true;
  265. // this has to be one of the very first things to do
  266. StopUpdateThread();
  267. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  268. TTerminalManager::Instance()->ScpExplorer = NULL;
  269. delete FCustomCommandLocalFileList;
  270. delete FCustomCommandRemoteFileList;
  271. delete FCustomCommandMenu;
  272. delete FTrayIcon;
  273. FTrayIcon = NULL;
  274. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  275. delete FEditorManager;
  276. ForceCloseLocalEditors();
  277. delete FLocalEditors;
  278. HideFileFindDialog();
  279. if (FDelayedDeletionTimer)
  280. {
  281. DoDelayedDeletion(NULL);
  282. SAFE_DESTROY(FDelayedDeletionTimer);
  283. }
  284. SAFE_DESTROY(FDelayedDeletionList);
  285. // sometimes we do not get DDEnd so the list is not released
  286. SAFE_DESTROY(FDDFileList);
  287. DebugAssert(FSynchronizeController == NULL);
  288. CloseHandle(FDDExtMutex);
  289. FDDExtMutex = NULL;
  290. FreeLibrary(FOle32Library);
  291. FOle32Library = NULL;
  292. FDragMoveCursor = NULL;
  293. DebugAssert(!FErrorList);
  294. if (FEverShown)
  295. {
  296. // when window is never shown (like when running command-line operation),
  297. // particularly window site is not restored correctly (BoundsRect value set
  298. // in RestoreForm gets lost during handle allocation), so we do not want
  299. // it to be stored
  300. StoreParams();
  301. }
  302. ManagedSession = NULL;
  303. Queue = NULL;
  304. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  305. NonVisualDataModule->ScpExplorer = NULL;
  306. delete FQueueController;
  307. FQueueController = NULL;
  308. delete FQueueStatusSection;
  309. FQueueStatusSection = NULL;
  310. delete FQueueStatus;
  311. FQueueStatus = NULL;
  312. delete FUserActionTimer;
  313. FUserActionTimer = NULL;
  314. delete FNoteTimer;
  315. delete FNotes;
  316. delete FNoteData;
  317. SAFE_DESTROY(FDocks);
  318. SessionsPageControl->Images = NULL;
  319. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  320. SAFE_DESTROY(FSessionColors);
  321. SAFE_DESTROY(FSessionsDragDropFilesEx);
  322. SAFE_DESTROY(FHistoryMenu[0][0]);
  323. SAFE_DESTROY(FHistoryMenu[0][1]);
  324. SAFE_DESTROY(FHistoryMenu[1][0]);
  325. SAFE_DESTROY(FHistoryMenu[1][1]);
  326. if (FHiddenWindow != NULL)
  327. {
  328. DestroyWindow(FHiddenWindow);
  329. FHiddenWindow = NULL;
  330. }
  331. }
  332. //---------------------------------------------------------------------------
  333. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  334. {
  335. std::unique_ptr<TSessionData> Data;
  336. if (!Session.IsEmpty())
  337. {
  338. bool DefaultsOnly;
  339. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  340. }
  341. TTerminalManager * Manager = TTerminalManager::Instance();
  342. for (int Index = 0; Index < Manager->Count; Index++)
  343. {
  344. TTerminal * Terminal = Manager->Sessions[Index];
  345. if (Session.IsEmpty() ||
  346. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  347. {
  348. if (Path.IsEmpty())
  349. {
  350. Terminal->ClearCaches();
  351. }
  352. else
  353. {
  354. Terminal->DirectoryModified(Path, true);
  355. }
  356. }
  357. }
  358. // We should flag a pending refresh for the background terminals or busy foreground terminals
  359. if (HasActiveTerminal() &&
  360. CanCommandLineFromAnotherInstance() &&
  361. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  362. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  363. {
  364. Terminal->ReloadDirectory();
  365. }
  366. }
  367. //---------------------------------------------------------------------------
  368. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  369. {
  370. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  371. size_t MessageSize = sizeof(TCopyDataMessage);
  372. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  373. if (Result)
  374. {
  375. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  376. Result = (Message.Version == TCopyDataMessage::Version1);
  377. if (Result)
  378. {
  379. switch (Message.Command)
  380. {
  381. case TCopyDataMessage::CommandCanCommandLine:
  382. Result = CanCommandLineFromAnotherInstance();
  383. break;
  384. case TCopyDataMessage::CommandCommandLine:
  385. {
  386. UnicodeString CommandLine(Message.CommandLine);
  387. Result = CommandLineFromAnotherInstance(CommandLine);
  388. }
  389. break;
  390. case TCopyDataMessage::RefreshPanel:
  391. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  392. break;
  393. case TCopyDataMessage::MainWindowCheck:
  394. Result = true;
  395. break;
  396. default:
  397. Result = false;
  398. break;
  399. }
  400. }
  401. }
  402. Message.Result = Result ? 1 : 0;
  403. }
  404. //---------------------------------------------------------------------------
  405. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  406. {
  407. WNDCLASS WindowClass = {0};
  408. WindowClass.lpfnWndProc = DefWindowProc;
  409. WindowClass.hInstance = HInstance;
  410. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  411. FHiddenWindow = NULL;
  412. if (RegisterClass(&WindowClass))
  413. {
  414. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  415. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  416. }
  417. }
  418. //---------------------------------------------------------------------------
  419. bool __fastcall TCustomScpExplorerForm::CanConsole()
  420. {
  421. return
  422. HasActiveTerminal() &&
  423. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  424. }
  425. //---------------------------------------------------------------------------
  426. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  427. {
  428. bool Result = Visible && !NonVisualDataModule->Busy;
  429. return Result;
  430. }
  431. //---------------------------------------------------------------------------
  432. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  433. const UnicodeString & CommandLine)
  434. {
  435. TProgramParams Params(CommandLine);
  436. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  437. if (Result)
  438. {
  439. NonVisualDataModule->StartBusy();
  440. try
  441. {
  442. // this action is initiated from another process,
  443. // so it's likely that our window is not visible,
  444. // and user won't see what is going on
  445. Application->BringToFront();
  446. // reload sessions as we may be asked to open a session
  447. // just stored by another instance
  448. StoredSessions->Reload();
  449. UnicodeString SessionName = Params.Param[1];
  450. std::unique_ptr<TObjectList> DataList(new TObjectList());
  451. try
  452. {
  453. UnicodeString DownloadFile; // unused
  454. int Flags = GetCommandLineParseUrlFlags(&Params);
  455. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  456. }
  457. catch (EAbort &)
  458. {
  459. // Happens when opening session in PuTTY and in other situations,
  460. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  461. // to process the command-line on its own (or to pass it to yet another instance)
  462. }
  463. if (DataList->Count > 0)
  464. {
  465. TTerminalManager * Manager = TTerminalManager::Instance();
  466. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  467. }
  468. }
  469. __finally
  470. {
  471. NonVisualDataModule->EndBusy();
  472. }
  473. }
  474. return Result;
  475. }
  476. //---------------------------------------------------------------------------
  477. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  478. {
  479. TManagedTerminal * Result;
  480. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  481. {
  482. Result = FManagedSession;
  483. }
  484. else
  485. {
  486. Result = NULL;
  487. }
  488. return Result;
  489. }
  490. //---------------------------------------------------------------------------
  491. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  492. {
  493. if (FManagedSession != value)
  494. {
  495. SessionChanging();
  496. DoSetManagedSession(value, false);
  497. }
  498. }
  499. //---------------------------------------------------------------------------
  500. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  501. {
  502. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  503. FManagedSession = value;
  504. {
  505. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  506. FAllowTransferPresetAutoSelect = false;
  507. TAutoFlag SessionChangingFlag(FSessionChanging);
  508. SessionChanged(Replace);
  509. }
  510. if (Terminal != NULL)
  511. {
  512. TransferPresetAutoSelect();
  513. }
  514. // Update app and tab titles, prevented above by FSessionChanging.
  515. UpdateControls();
  516. }
  517. //---------------------------------------------------------------------------
  518. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  519. {
  520. DoSetManagedSession(value, true);
  521. }
  522. //---------------------------------------------------------------------------
  523. void __fastcall TCustomScpExplorerForm::SessionChanging()
  524. {
  525. if (ManagedSession != NULL)
  526. {
  527. UpdateSession(ManagedSession);
  528. }
  529. }
  530. //---------------------------------------------------------------------------
  531. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  532. {
  533. if (Terminal != NULL)
  534. {
  535. UpdateSessionColor((TColor)Terminal->StateData->Color);
  536. }
  537. if (!TTerminalManager::Instance()->Updating)
  538. {
  539. SessionListChanged();
  540. }
  541. if (Terminal != NULL)
  542. {
  543. TObject * State = NULL;
  544. if (WinConfiguration->PreservePanelState)
  545. {
  546. State = Terminal->RemoteExplorerState;
  547. DirView(osRemote)->AnnounceState(State);
  548. }
  549. try
  550. {
  551. if (Replaced)
  552. {
  553. RemoteDirView->ReplaceTerminal(Terminal);
  554. }
  555. else
  556. {
  557. RemoteDirView->Terminal = Terminal;
  558. }
  559. if (Terminal->Active)
  560. {
  561. Terminal->RefreshDirectory();
  562. }
  563. if (State != NULL)
  564. {
  565. DirView(osRemote)->RestoreState(State);
  566. }
  567. }
  568. __finally
  569. {
  570. DirView(osRemote)->AnnounceState(NULL);
  571. }
  572. }
  573. else
  574. {
  575. RemoteDirView->Terminal = NULL;
  576. }
  577. InitStatusBar();
  578. UpdateTransferList();
  579. // Update panels Enable state before refreshing the labels
  580. UpdateControls();
  581. }
  582. //---------------------------------------------------------------------------
  583. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  584. {
  585. if (Queue != value)
  586. {
  587. if (FQueue != NULL)
  588. {
  589. FQueue->OnListUpdate = NULL;
  590. FQueue->OnQueueItemUpdate = NULL;
  591. }
  592. FQueue = value;
  593. if (FQueue != NULL)
  594. {
  595. DebugAssert(FQueue->OnListUpdate == NULL);
  596. FQueue->OnListUpdate = QueueListUpdate;
  597. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  598. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  599. }
  600. QueueChanged();
  601. }
  602. }
  603. //---------------------------------------------------------------------------
  604. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  605. TListItem * Item)
  606. {
  607. if (FQueueActedItem == Item)
  608. {
  609. FQueueActedItem = NULL;
  610. if ((QueueView3->PopupMenu != NULL) &&
  611. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  612. {
  613. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  614. ReleaseCapture();
  615. }
  616. }
  617. if (Item->Data == FPendingQueueActionItem)
  618. {
  619. FPendingQueueActionItem = NULL;
  620. }
  621. }
  622. //---------------------------------------------------------------------------
  623. bool TCustomScpExplorerForm::IsAnythingQueued()
  624. {
  625. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  626. }
  627. //---------------------------------------------------------------------------
  628. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  629. {
  630. {
  631. TGuard Guard(FQueueStatusSection);
  632. FQueueStatusInvalidated = false;
  633. if (FQueue != NULL)
  634. {
  635. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  636. }
  637. }
  638. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  639. {
  640. FMaxQueueLength = FQueueStatus->Count;
  641. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  642. }
  643. {
  644. TAutoFlag Flag(FQueueStatusUpdating);
  645. FQueueController->UpdateQueueStatus(FQueueStatus);
  646. }
  647. UpdateQueueFileList();
  648. SetQueueProgress();
  649. UpdateQueueView();
  650. bool IsEmpty = !IsAnythingQueued();
  651. if (IsEmpty && (Terminal != NULL))
  652. {
  653. TOnceDoneOperation OnceDoneOperation =
  654. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  655. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  656. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  657. {
  658. FQueue->Enabled = false;
  659. }
  660. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  661. {
  662. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  663. }
  664. }
  665. }
  666. //---------------------------------------------------------------------------
  667. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  668. {
  669. UnicodeString Result;
  670. if (FQueueStatus != NULL)
  671. {
  672. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  673. {
  674. TFileOperationProgressType * ProgressData =
  675. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  676. if ((ProgressData != NULL) && ProgressData->InProgress)
  677. {
  678. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  679. }
  680. }
  681. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  682. {
  683. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  684. }
  685. }
  686. return Result;
  687. }
  688. //---------------------------------------------------------------------------
  689. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  690. {
  691. ComponentVisible[fcQueueView] =
  692. !IsLocalBrowserMode() &&
  693. ((WinConfiguration->QueueView.Show == qvShow) ||
  694. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  695. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  696. }
  697. //---------------------------------------------------------------------------
  698. void __fastcall TCustomScpExplorerForm::QueueChanged()
  699. {
  700. if (FQueueStatus != NULL)
  701. {
  702. // UpdateFileList implementation relies on the status being recreated when session changes
  703. delete FQueueStatus;
  704. FQueueStatus = NULL;
  705. }
  706. UpdateQueueStatus(true);
  707. }
  708. //---------------------------------------------------------------------------
  709. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  710. {
  711. if (FQueue == Queue)
  712. {
  713. FQueueStatusInvalidated = true;
  714. }
  715. }
  716. //---------------------------------------------------------------------------
  717. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  718. TQueueItem * Item)
  719. {
  720. if (FQueue == Queue)
  721. {
  722. TGuard Guard(FQueueStatusSection);
  723. // this may be running in parallel with QueueChanged
  724. if (FQueueStatus != NULL)
  725. {
  726. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  727. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  728. {
  729. FRefreshLocalDirectory = (QueueItem == NULL) ||
  730. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  731. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  732. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  733. }
  734. if (QueueItem != NULL)
  735. {
  736. QueueItem->UserData = (void*)true;
  737. FQueueItemInvalidated = true;
  738. }
  739. }
  740. }
  741. }
  742. //---------------------------------------------------------------------------
  743. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  744. {
  745. // during standalone editing, we have no way to see/control queue,
  746. // so we have to always popup prompts automatically
  747. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  748. }
  749. //---------------------------------------------------------------------------
  750. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  751. {
  752. if (FQueueStatus != NULL)
  753. {
  754. bool QueueAutoPopup = IsQueueAutoPopup();
  755. bool NeedRefresh = FQueueController->NeedRefresh();
  756. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  757. FQueueItemInvalidated = false;
  758. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  759. bool Updated = false;
  760. TQueueItemProxy * QueueItem;
  761. bool WasUserAction;
  762. for (int Index = 0; Index < Limit; Index++)
  763. {
  764. bool Update = false;
  765. QueueItem = FQueueStatus->Items[Index];
  766. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  767. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  768. (FPendingQueueActionItem == NULL))
  769. {
  770. FPendingQueueActionItem = QueueItem;
  771. FUserActionTimer->Enabled = true;
  772. }
  773. if ((bool)QueueItem->UserData)
  774. {
  775. QueueItem->UserData = (void*)false;
  776. QueueItem->Update();
  777. Updated = true;
  778. Update = true;
  779. }
  780. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  781. {
  782. Update = true;
  783. }
  784. if (Update)
  785. {
  786. FQueueController->RefreshQueueItem(QueueItem);
  787. }
  788. }
  789. if (Updated)
  790. {
  791. NonVisualDataModule->UpdateNonVisibleActions();
  792. SetQueueProgress();
  793. UpdateQueueFileList();
  794. }
  795. }
  796. }
  797. //---------------------------------------------------------------------------
  798. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  799. {
  800. if (FTaskbarList != NULL)
  801. {
  802. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  803. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  804. }
  805. }
  806. //---------------------------------------------------------------------------
  807. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  808. {
  809. if (Progress >= 0)
  810. {
  811. if (FTaskbarList != NULL)
  812. {
  813. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  814. }
  815. }
  816. else
  817. {
  818. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  819. }
  820. }
  821. //---------------------------------------------------------------------------
  822. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  823. {
  824. int OverallProgress;
  825. // FProgressForm is null when this is called from SetQueueProgress
  826. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  827. {
  828. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  829. }
  830. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  831. {
  832. OverallProgress = ProgressData->OverallProgress();
  833. }
  834. else
  835. {
  836. OverallProgress = -1;
  837. }
  838. SetTaskbarListProgressValue(OverallProgress);
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  842. {
  843. TTerminalManager::Instance()->QueueStatusUpdated();
  844. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  845. {
  846. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  847. {
  848. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  849. {
  850. TFileOperationProgressType * ProgressData = NULL;
  851. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  852. {
  853. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  854. }
  855. if ((ProgressData != NULL) &&
  856. ProgressData->InProgress)
  857. {
  858. SetTaskbarListProgressValue(ProgressData);
  859. }
  860. else
  861. {
  862. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  863. }
  864. }
  865. else
  866. {
  867. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  868. }
  869. }
  870. else
  871. {
  872. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  873. }
  874. }
  875. UpdateQueueLabel();
  876. }
  877. //---------------------------------------------------------------------------
  878. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  879. {
  880. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  881. if (FQueueStatus != NULL)
  882. {
  883. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  884. if (ActiveAndPendingPrimaryCount > 0)
  885. {
  886. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  887. }
  888. }
  889. QueueLabel->Caption = Caption;
  890. }
  891. //---------------------------------------------------------------------------
  892. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  893. {
  894. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  895. static_cast<TComponent*>(GetComponent(fcTransferList)));
  896. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  897. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  898. TransferList->Strings->BeginUpdate();
  899. try
  900. {
  901. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  902. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  903. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  904. if (FTransferDropDownHint.IsEmpty())
  905. {
  906. FTransferDropDownHint = TransferDropDown->Hint;
  907. }
  908. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  909. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  910. TransferDropDown->Text = StripHotkey(Name);
  911. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  912. (FTransferDropDownHint, StripHotkey(Name),
  913. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  914. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  915. // update the label, otherwise when it is updated only on the first draw
  916. // of the list, it is drawn "bold" for some reason
  917. FTransferListHoverIndex = TransferList->ItemIndex;
  918. UpdateTransferLabel();
  919. }
  920. __finally
  921. {
  922. TransferList->Strings->EndUpdate();
  923. }
  924. }
  925. //---------------------------------------------------------------------------
  926. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  927. {
  928. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  929. DebugAssert(Toolbar != NULL);
  930. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  931. }
  932. //---------------------------------------------------------------------------
  933. void __fastcall TCustomScpExplorerForm::UpdateActions()
  934. {
  935. TForm::UpdateActions();
  936. if (ComponentVisible[fcCustomCommandsBand])
  937. {
  938. UpdateCustomCommandsToolbar();
  939. }
  940. }
  941. //---------------------------------------------------------------------------
  942. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  943. {
  944. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  945. }
  946. //---------------------------------------------------------------------------
  947. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  948. {
  949. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  950. {
  951. DirView->RowSelect = WinConfiguration->FullRowSelect;
  952. // selection is not redrawn automatically when RowSelect changes
  953. DirView->Invalidate();
  954. }
  955. }
  956. //---------------------------------------------------------------------------
  957. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  958. {
  959. Color = GetBtnFaceColor();
  960. DebugAssert(Configuration && RemoteDirView);
  961. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  962. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  963. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  964. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  965. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  966. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  967. UpdateRowSelect(RemoteDirView);
  968. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  969. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  970. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  971. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  972. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  973. UpdateSessionsPageControlHeight();
  974. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  975. UpdateToolbarDisplayMode();
  976. UpdateTransferList();
  977. if (Terminal != NULL)
  978. {
  979. TransferPresetAutoSelect();
  980. }
  981. UpdateQueueView();
  982. UpdateControls();
  983. // this can be called even before constuctor finishes.
  984. if (FEditorManager != NULL)
  985. {
  986. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  987. }
  988. // this can be called even before constuctor finishes.
  989. if (FLocalEditors != NULL)
  990. {
  991. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  992. {
  993. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  994. }
  995. }
  996. if (ComponentVisible[fcCustomCommandsBand])
  997. {
  998. UpdateCustomCommandsToolbar();
  999. }
  1000. // show only when keeping queue items forever,
  1001. // otherwise, its enough to have in in the context menu
  1002. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1003. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1004. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1005. {
  1006. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1007. FFileColorsCurrent = WinConfiguration->FileColors;
  1008. FileColorsChanged();
  1009. }
  1010. }
  1011. //---------------------------------------------------------------------------
  1012. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1013. {
  1014. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1015. DirView->Invalidate();
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1019. {
  1020. DoFileColorsChanged(RemoteDirView);
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1024. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1025. void * /*Arg*/)
  1026. {
  1027. if (Token != NULL)
  1028. {
  1029. TForm * Editor = dynamic_cast<TForm*>(Token);
  1030. DebugAssert(Editor != NULL);
  1031. ReconfigureEditorForm(Editor);
  1032. }
  1033. }
  1034. //---------------------------------------------------------------------------
  1035. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1036. {
  1037. WinConfiguration->DDTransferConfirmation = asOn;
  1038. }
  1039. //---------------------------------------------------------------------------
  1040. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1041. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1042. {
  1043. // noop
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1047. {
  1048. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1052. {
  1053. if (DoNotShowAgain)
  1054. {
  1055. if (DragDrop)
  1056. {
  1057. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1058. {
  1059. PopupTrayBalloon(
  1060. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1061. }
  1062. WinConfiguration->DDTransferConfirmation = asOff;
  1063. }
  1064. else
  1065. {
  1066. WinConfiguration->ConfirmTransferring = false;
  1067. }
  1068. }
  1069. else
  1070. {
  1071. // User explicitly unchecked "do not show again",
  1072. // so show him the dialog the next time
  1073. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1074. {
  1075. WinConfiguration->DDTransferConfirmation = asOn;
  1076. }
  1077. }
  1078. }
  1079. //---------------------------------------------------------------------------
  1080. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1081. TTransferDirection Direction, TTransferType Type, bool Temp,
  1082. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1083. bool Confirm, bool DragDrop, int Options)
  1084. {
  1085. bool Result = true;
  1086. DebugAssert(HasActiveTerminal());
  1087. // these parameters are known in advance
  1088. int Params =
  1089. FLAGMASK(Type == ttMove, cpDelete);
  1090. bool ToTemp = (Temp && (Direction == tdToLocal));
  1091. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1092. {
  1093. bool DisableNewerOnly =
  1094. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1095. ToTemp;
  1096. Options |=
  1097. FLAGMASK(ToTemp, coTemp) |
  1098. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1099. coDoNotShowAgain;
  1100. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1101. int CopyParamAttrs = (Direction == tdToRemote ?
  1102. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1103. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1104. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1105. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1106. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1107. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1108. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1109. if (Result)
  1110. {
  1111. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1112. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1113. }
  1114. }
  1115. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1116. {
  1117. Configuration->Usage->Inc(L"TransfersOnBackground");
  1118. // these parameter are known only after transfer dialog
  1119. Params |=
  1120. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1121. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1122. Result = false;
  1123. ClearTransferSourceSelection(Direction);
  1124. }
  1125. return Result;
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1129. {
  1130. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1131. if (FOnFileOperationFinished != NULL)
  1132. {
  1133. FOnFileOperationFinished(Side, UnicodeString(), true);
  1134. }
  1135. else
  1136. {
  1137. if (HasDirView[Side])
  1138. {
  1139. DirView(Side)->SelectAll(smNone);
  1140. }
  1141. }
  1142. }
  1143. //---------------------------------------------------------------------------
  1144. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1145. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1146. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1147. int Params)
  1148. {
  1149. DebugAssert(Queue != NULL);
  1150. bool SingleFile = false;
  1151. if (FileList->Count == 1)
  1152. {
  1153. if (Direction == tdToRemote)
  1154. {
  1155. UnicodeString FileName = FileList->Strings[0];
  1156. SingleFile = FileExists(ApiPath(FileName));
  1157. }
  1158. else
  1159. {
  1160. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1161. SingleFile = !File->IsDirectory;
  1162. }
  1163. }
  1164. TQueueItem * QueueItem;
  1165. if (Direction == tdToRemote)
  1166. {
  1167. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1168. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1169. }
  1170. else
  1171. {
  1172. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1173. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1174. }
  1175. AddQueueItem(Queue, QueueItem, Terminal);
  1176. }
  1177. //---------------------------------------------------------------------------
  1178. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1179. {
  1180. if (Queue->IsEmpty)
  1181. {
  1182. Terminal->QueueOperationStart = Now();
  1183. }
  1184. Queue->AddItem(QueueItem);
  1185. }
  1186. //---------------------------------------------------------------------------
  1187. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1188. {
  1189. }
  1190. //---------------------------------------------------------------------------
  1191. void __fastcall TCustomScpExplorerForm::InitControls()
  1192. {
  1193. DebugAssert(FDocks != NULL);
  1194. for (int Index = 0; Index < ComponentCount; Index++)
  1195. {
  1196. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1197. if ((Dock != NULL) && (Dock->Tag == 0))
  1198. {
  1199. FDocks->Add(Dock);
  1200. }
  1201. }
  1202. CollectItemsWithTextDisplayMode(this);
  1203. }
  1204. //---------------------------------------------------------------------------
  1205. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1206. {
  1207. ConfigurationChanged();
  1208. QueuePanel->Height =
  1209. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1210. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1211. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1212. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1213. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1214. QueueFileList->Height =
  1215. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1216. if (QueueFileList->Visible)
  1217. {
  1218. AdjustQueueLayout();
  1219. }
  1220. UpdateCustomCommandsToolbar();
  1221. }
  1222. //---------------------------------------------------------------------------
  1223. void __fastcall TCustomScpExplorerForm::StoreParams()
  1224. {
  1225. int APixelsPerInch = GetControlPixelsPerInch(this);
  1226. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1227. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1228. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1229. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1230. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1231. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1232. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1233. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1237. {
  1238. if (!FFormRestored)
  1239. {
  1240. FFormRestored = true;
  1241. RestoreFormParams();
  1242. }
  1243. TForm::CreateParams(Params);
  1244. }
  1245. //---------------------------------------------------------------------------
  1246. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1247. {
  1248. DebugAssert(FDocks != NULL);
  1249. for (int Index = 0; Index < FDocks->Count; Index++)
  1250. {
  1251. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1252. }
  1253. }
  1254. //---------------------------------------------------------------------------
  1255. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1256. {
  1257. SetDockAllowDrag(true);
  1258. while (!ButtonsStr.IsEmpty())
  1259. {
  1260. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1261. UnicodeString Name = CutToChar(S, L'=', true);
  1262. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1263. {
  1264. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1265. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1266. {
  1267. while (!S.IsEmpty())
  1268. {
  1269. UnicodeString S2 = CutToChar(S, L',', true);
  1270. UnicodeString Name = CutToChar(S2, L':', true);
  1271. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1272. {
  1273. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1274. if (SameText(GetToolbarItemName(Item), Name))
  1275. {
  1276. // forward compatibility
  1277. UnicodeString S3 = CutToChar(S2, L':', true);
  1278. bool Visible = StrToIntDef(S3, 0);
  1279. Item->Visible = Visible;
  1280. }
  1281. }
  1282. }
  1283. }
  1284. }
  1285. }
  1286. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1287. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1288. }
  1289. //---------------------------------------------------------------------------
  1290. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1291. {
  1292. UnicodeString Result;
  1293. SetDockAllowDrag(true);
  1294. Result = ::GetToolbarsLayoutStr(this);
  1295. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1296. return Result;
  1297. }
  1298. //---------------------------------------------------------------------------
  1299. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1300. {
  1301. UnicodeString Result;
  1302. if (Item->Action != NULL)
  1303. {
  1304. Result = Item->Action->Name;
  1305. Result = RemoveSuffix(Result, L"Action", true);
  1306. }
  1307. else
  1308. {
  1309. Result = Item->Name;
  1310. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1311. Result = RemoveSuffix(Result, L"Item", true);
  1312. }
  1313. return Result;
  1314. }
  1315. //---------------------------------------------------------------------------
  1316. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1317. {
  1318. UnicodeString Result;
  1319. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1320. {
  1321. UnicodeString ToolbarStr;
  1322. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1323. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1324. {
  1325. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1326. {
  1327. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1328. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1329. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1330. // and save the changes here only.
  1331. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1332. {
  1333. UnicodeString Name = GetToolbarItemName(Item);
  1334. DebugAssert(Name.Pos(L"TBX") == 0);
  1335. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1336. }
  1337. }
  1338. }
  1339. if (!ToolbarStr.IsEmpty())
  1340. {
  1341. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1342. }
  1343. }
  1344. return Result;
  1345. }
  1346. //---------------------------------------------------------------------------
  1347. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1348. {
  1349. DebugAssert(FProgressForm == NULL);
  1350. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1351. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1352. FProgressForm->DeleteLocalToRecycleBin =
  1353. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1354. FProgressForm->DeleteRemoteToRecycleBin =
  1355. HasActiveTerminal() &&
  1356. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1357. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1358. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1359. LockWindow();
  1360. // Actually, do not know what hides the progress once the operation finishes
  1361. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1362. SetTaskbarListProgressState(TBPF_NORMAL);
  1363. }
  1364. //---------------------------------------------------------------------------
  1365. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1366. {
  1367. UnlockWindow();
  1368. SAFE_DESTROY(FProgressForm);
  1369. SetQueueProgress();
  1370. }
  1371. //---------------------------------------------------------------------------
  1372. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1373. TFileOperationProgressType & ProgressData)
  1374. {
  1375. // Download to temporary local directory
  1376. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1377. {
  1378. bool Continue = true;
  1379. FPendingTempSpaceWarn = false;
  1380. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1381. if (!Continue)
  1382. {
  1383. Abort();
  1384. }
  1385. }
  1386. // operation is being executed and we still didn't show up progress form
  1387. if (ProgressData.InProgress && !FProgressForm)
  1388. {
  1389. CreateProgressForm(NULL);
  1390. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1391. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1392. }
  1393. // operation is finished (or terminated), so we hide progress form
  1394. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1395. {
  1396. DestroyProgressForm();
  1397. if (ProgressData.Operation == foCalculateSize)
  1398. {
  1399. // When calculating size before transfer, the abort caused by
  1400. // cancel flag set due to "MoveToQueue" below,
  1401. // is not propagated back to us in ExecuteFileOperation,
  1402. // as it is silently swallowed.
  1403. // So we explicitly (re)throw it here.
  1404. if (FMoveToQueue)
  1405. {
  1406. Abort();
  1407. }
  1408. }
  1409. else
  1410. {
  1411. if ((ProgressData.Cancel == csContinue) &&
  1412. !FAutoOperation)
  1413. {
  1414. OperationComplete(ProgressData.StartTime);
  1415. }
  1416. }
  1417. }
  1418. if (FProgressForm &&
  1419. // Can happen only during synchronization, when ending one of the operations
  1420. ProgressData.InProgress)
  1421. {
  1422. FProgressForm->SetProgressData(ProgressData);
  1423. DebugAssert(ProgressData.InProgress);
  1424. SetTaskbarListProgressValue(&ProgressData);
  1425. if (FProgressForm->Cancel > csContinue)
  1426. {
  1427. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1428. if (FProgressForm->Cancel == csCancelFile)
  1429. {
  1430. FProgressForm->ClearCancel();
  1431. }
  1432. // cancel cancels even the move
  1433. FMoveToQueue = false;
  1434. }
  1435. else if (FProgressForm->MoveToQueue)
  1436. {
  1437. FMoveToQueue = true;
  1438. ProgressData.SetCancelAtLeast(csCancel);
  1439. }
  1440. if ((FTransferResumeList != NULL) &&
  1441. ProgressData.InProgress &&
  1442. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1443. !ProgressData.FullFileName.IsEmpty())
  1444. {
  1445. if ((FTransferResumeList->Count == 0) ||
  1446. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1447. {
  1448. // note that we do not recognize directories from files here
  1449. FTransferResumeList->Add(ProgressData.FullFileName);
  1450. }
  1451. }
  1452. }
  1453. }
  1454. //---------------------------------------------------------------------------
  1455. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1456. const TDateTime & StartTime)
  1457. {
  1458. if (GUIConfiguration->BeepOnFinish &&
  1459. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1460. {
  1461. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1462. DWORD Sound;
  1463. if (!ExtractFileExt(BeepSound).IsEmpty())
  1464. {
  1465. Sound = SND_FILENAME;
  1466. }
  1467. else
  1468. {
  1469. Sound = SND_ALIAS;
  1470. }
  1471. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1472. }
  1473. }
  1474. //---------------------------------------------------------------------------
  1475. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1476. TFileOperationProgressType & ProgressData)
  1477. {
  1478. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1479. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1480. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1481. FileOperationProgress(ProgressData);
  1482. }
  1483. //---------------------------------------------------------------------------
  1484. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1485. {
  1486. UnicodeString Result;
  1487. if (FProgressForm != NULL)
  1488. {
  1489. Result = FProgressForm->ProgressStr();
  1490. }
  1491. return Result;
  1492. }
  1493. //---------------------------------------------------------------------------
  1494. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1495. bool DragDrop)
  1496. {
  1497. return (!DragDrop && (DropSourceControl == NULL)) ||
  1498. (DropSourceControl == DirView(osOther));
  1499. }
  1500. //---------------------------------------------------------------------------
  1501. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1502. {
  1503. TCustomDirView * ADirView = DirView(Side);
  1504. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1505. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1506. // this can happen when local drive is unplugged in the middle of the operation
  1507. if (Item != NULL)
  1508. {
  1509. if (Unselect)
  1510. {
  1511. Item->Selected = false;
  1512. }
  1513. ADirView->MakeProgressVisible(Item);
  1514. }
  1515. return Item;
  1516. }
  1517. //---------------------------------------------------------------------------
  1518. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1519. TFileOperation Operation, TOperationSide Side,
  1520. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1521. TOnceDoneOperation & OnceDoneOperation)
  1522. {
  1523. if (!FAutoOperation)
  1524. {
  1525. // no selection on "/upload", form serves only as event handler
  1526. // (it is not displayed)
  1527. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1528. ((Operation == FPrimaryOperation) ||
  1529. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1530. {
  1531. TOperationSide DViewSide;
  1532. if ((FProgressSide != osCurrent) &&
  1533. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1534. {
  1535. // assume the operation is over the focused panel
  1536. DViewSide = FProgressSide;
  1537. }
  1538. else
  1539. {
  1540. DViewSide = Side;
  1541. }
  1542. if (FOnFileOperationFinished != NULL)
  1543. {
  1544. FOnFileOperationFinished(DViewSide, FileName, Success);
  1545. }
  1546. else
  1547. {
  1548. VisualiseOperationFinished(DViewSide, FileName, Success);
  1549. }
  1550. }
  1551. if ((Operation == foCopy) || (Operation == foMove))
  1552. {
  1553. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1554. {
  1555. if (Side == osLocal)
  1556. {
  1557. Configuration->Usage->Inc(L"UploadedFiles");
  1558. }
  1559. else
  1560. {
  1561. Configuration->Usage->Inc(L"DownloadedFiles");
  1562. }
  1563. }
  1564. }
  1565. }
  1566. if (Success && (FSynchronizeController != NULL))
  1567. {
  1568. DebugAssert(!IsSideLocalBrowser(osRemote));
  1569. if (Operation == foCopy)
  1570. {
  1571. DebugAssert(Side == osLocal);
  1572. FSynchronizeController->LogOperation(soUpload, FileName);
  1573. }
  1574. else if (Operation == foDelete)
  1575. {
  1576. DebugAssert(Side == osRemote);
  1577. FSynchronizeController->LogOperation(soDelete, FileName);
  1578. }
  1579. }
  1580. if (FProgressForm)
  1581. {
  1582. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1583. }
  1584. }
  1585. //---------------------------------------------------------------------------
  1586. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1587. TFileOperation Operation, TOperationSide Side,
  1588. bool Temp, const UnicodeString & FileName, Boolean Success,
  1589. TOnceDoneOperation & OnceDoneOperation)
  1590. {
  1591. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1592. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1593. OnceDoneOperation);
  1594. }
  1595. //---------------------------------------------------------------------------
  1596. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1597. {
  1598. return false;
  1599. }
  1600. //---------------------------------------------------------------------------
  1601. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1602. {
  1603. return false;
  1604. }
  1605. //---------------------------------------------------------------------------
  1606. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1607. {
  1608. return false;
  1609. }
  1610. //---------------------------------------------------------------------------
  1611. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1612. {
  1613. return NULL;
  1614. }
  1615. //---------------------------------------------------------------------------
  1616. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1617. {
  1618. DebugAssert(GetSide(Side) == osRemote);
  1619. DebugUsedParam(Side);
  1620. return RemoteDirView;
  1621. }
  1622. //---------------------------------------------------------------------------
  1623. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1624. {
  1625. DebugAssert(GetSide(Side) == osRemote);
  1626. DebugUsedParam(Side);
  1627. return RemoteDriveView;
  1628. }
  1629. //---------------------------------------------------------------------------
  1630. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1631. {
  1632. DebugAssert(GetSide(Side) == osRemote);
  1633. DebugUsedParam(Side);
  1634. return HasActiveTerminal();
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1638. TOperationSide Side, int FilesOnly)
  1639. {
  1640. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1641. }
  1642. //---------------------------------------------------------------------------
  1643. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1644. TOperationSide Side, int FilesOnly)
  1645. {
  1646. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1647. }
  1648. //---------------------------------------------------------------------------
  1649. struct THistoryItemData
  1650. {
  1651. short int Side;
  1652. short int Index;
  1653. };
  1654. //---------------------------------------------------------------------------
  1655. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1656. {
  1657. DirView(Side)->HistoryGo(Index);
  1658. }
  1659. //---------------------------------------------------------------------------
  1660. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1661. {
  1662. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1663. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1664. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1665. }
  1666. //---------------------------------------------------------------------------
  1667. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1668. bool Back)
  1669. {
  1670. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1671. {
  1672. TCustomDirView * DView = DirView(Side);
  1673. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1674. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1675. if (ICount > 10)
  1676. {
  1677. ICount = 10;
  1678. }
  1679. Menu->Items->Clear();
  1680. THistoryItemData Data;
  1681. Data.Side = Side;
  1682. for (short int i = 1; i <= ICount; i++)
  1683. {
  1684. TTBCustomItem * Item = new TTBXItem(Menu);
  1685. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1686. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1687. Item->Hint = DView->HistoryPath[Data.Index];
  1688. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1689. Item->Tag = *reinterpret_cast<int*>(&Data);
  1690. Item->OnClick = HistoryItemClick;
  1691. Menu->Items->Add(Item);
  1692. }
  1693. }
  1694. }
  1695. //---------------------------------------------------------------------------
  1696. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1697. TOperationSide Side, bool Back)
  1698. {
  1699. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1700. {
  1701. // workaround
  1702. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1703. // operator new allocates memory in Pascal code, but calls inline
  1704. // contructor in C++, leading in problems, the function does
  1705. // both in Pascal code
  1706. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1707. UpdateHistoryMenu(Side, Back);
  1708. }
  1709. return FHistoryMenu[Side == osLocal][Back];
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1713. TCustomDirView *Sender)
  1714. {
  1715. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1716. UpdateHistoryMenu(Side, true);
  1717. UpdateHistoryMenu(Side, false);
  1718. }
  1719. //---------------------------------------------------------------------------
  1720. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1721. {
  1722. // remote custom commands can be executed only if the server supports shell commands
  1723. // or have secondary shell
  1724. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1725. }
  1726. //---------------------------------------------------------------------------
  1727. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1728. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1729. {
  1730. // -1 = hidden, 0 = disabled, 1 = enabled
  1731. int Result;
  1732. TFileCustomCommand RemoteCustomCommand;
  1733. TLocalCustomCommand LocalCustomCommand;
  1734. TFileCustomCommand * NonInteractiveCustomCommand =
  1735. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1736. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1737. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1738. if (FLAGCLEAR(Command.Params, ccLocal))
  1739. {
  1740. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1741. // custom command that does not operate with files can be executed anytime ...
  1742. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1743. {
  1744. // ... but do not show such command in remote file menu
  1745. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1746. {
  1747. Result = AllowedState;
  1748. }
  1749. else
  1750. {
  1751. Result = -1;
  1752. }
  1753. }
  1754. else
  1755. {
  1756. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1757. {
  1758. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1759. }
  1760. else
  1761. {
  1762. Result = -1;
  1763. }
  1764. }
  1765. }
  1766. else
  1767. {
  1768. // custom command that does not operate with files can be executed anytime
  1769. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1770. {
  1771. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1772. }
  1773. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1774. {
  1775. if ((ListType == ccltAll) || (ListType == ccltFile))
  1776. {
  1777. // special case is "diff"-style command that can be executed over any side,
  1778. // if we have both sides
  1779. Result =
  1780. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1781. // directly (instead of EnableSelectedOperation) to pass
  1782. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1783. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1784. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1785. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1786. }
  1787. else if (ListType == ccltBoth)
  1788. {
  1789. DebugAssert(!IsLocalBrowserMode());
  1790. Result = 1;
  1791. }
  1792. else
  1793. {
  1794. Result = -1;
  1795. }
  1796. }
  1797. else
  1798. {
  1799. if ((ListType == ccltAll) ||
  1800. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1801. {
  1802. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1803. }
  1804. else
  1805. {
  1806. Result = -1;
  1807. }
  1808. }
  1809. if ((Result > 0) &&
  1810. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1811. !HasActiveTerminal())
  1812. {
  1813. Result = 0;
  1814. }
  1815. }
  1816. return Result;
  1817. }
  1818. //---------------------------------------------------------------------------
  1819. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1820. TStrings * FileList, const TCustomCommandType & ACommand,
  1821. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1822. {
  1823. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1824. {
  1825. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1826. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1827. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1828. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1829. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1830. bool Capture =
  1831. FLAGSET(ACommand.Params, ccShowResults) ||
  1832. FLAGSET(ACommand.Params, ccCopyResults) ||
  1833. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1834. TCaptureOutputEvent OutputEvent = NULL;
  1835. DebugAssert(FCapturedLog == NULL);
  1836. if (Capture)
  1837. {
  1838. FCapturedLog = new TStringList();
  1839. OutputEvent = TerminalCaptureLog;
  1840. }
  1841. try
  1842. {
  1843. if (!RemoteCustomCommand.IsFileCommand(Command))
  1844. {
  1845. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1846. OutputEvent);
  1847. }
  1848. else
  1849. {
  1850. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1851. }
  1852. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1853. {
  1854. if (FLAGSET(ACommand.Params, ccCopyResults))
  1855. {
  1856. CopyToClipboard(FCapturedLog);
  1857. }
  1858. if (FLAGSET(ACommand.Params, ccShowResults))
  1859. {
  1860. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1861. }
  1862. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1863. {
  1864. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1865. }
  1866. }
  1867. }
  1868. __finally
  1869. {
  1870. SAFE_DESTROY(FCapturedLog);
  1871. }
  1872. }
  1873. }
  1874. //---------------------------------------------------------------------------
  1875. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1876. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1877. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1878. {
  1879. DebugAssert(!IsLocalBrowserMode());
  1880. TStrings * LocalFileList = NULL;
  1881. TStrings * RemoteFileList = NULL;
  1882. TStrings * RemoteFileListFull = NULL;
  1883. try
  1884. {
  1885. if (LocalFileCommand)
  1886. {
  1887. if (ALocalFileList == NULL)
  1888. {
  1889. DebugAssert(HasDirView[osLocal]);
  1890. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1891. // directly (instead of EnableSelectedOperation) to pass
  1892. // false to FocusedFileOnlyWhenFocused
  1893. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1894. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1895. }
  1896. else
  1897. {
  1898. LocalFileList = ALocalFileList;
  1899. }
  1900. if (FileListCommand)
  1901. {
  1902. if (LocalFileList->Count != 1)
  1903. {
  1904. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1905. }
  1906. }
  1907. else
  1908. {
  1909. if ((LocalFileList->Count != 1) &&
  1910. (FileList->Count != 1) &&
  1911. (LocalFileList->Count != FileList->Count))
  1912. {
  1913. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1914. }
  1915. }
  1916. }
  1917. UnicodeString RootTempDir;
  1918. UnicodeString TempDir;
  1919. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1920. if (!RemoteFiles)
  1921. {
  1922. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1923. }
  1924. try
  1925. {
  1926. TDateTimes RemoteFileTimes;
  1927. if (RemoteFiles)
  1928. {
  1929. RemoteFileList = FileList;
  1930. }
  1931. else
  1932. {
  1933. RemoteFileList = new TStringList();
  1934. TMakeLocalFileListParams MakeFileListParam;
  1935. MakeFileListParam.FileList = RemoteFileList;
  1936. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1937. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1938. MakeFileListParam.Recursive =
  1939. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1940. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1941. if (!MakeFileListParam.Recursive)
  1942. {
  1943. RemoteFileTimes.clear();
  1944. RemoteFileListFull = new TStringList();
  1945. MakeFileListParam.FileList = RemoteFileListFull;
  1946. MakeFileListParam.Recursive = true;
  1947. MakeFileListParam.IncludeDirs = false;
  1948. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1949. }
  1950. else
  1951. {
  1952. RemoteFileListFull = RemoteFileList;
  1953. }
  1954. }
  1955. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1956. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1957. if (!NonBlocking)
  1958. {
  1959. int Count;
  1960. if (FileListCommand)
  1961. {
  1962. Count = 1;
  1963. }
  1964. else if (LocalFileCommand)
  1965. {
  1966. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1967. }
  1968. else
  1969. {
  1970. Count = RemoteFileList->Count;
  1971. }
  1972. Progress.Start(foCustomCommand, osRemote, Count);
  1973. DebugAssert(FProgressForm != NULL);
  1974. FProgressForm->ReadOnly = true;
  1975. }
  1976. try
  1977. {
  1978. if (FileListCommand)
  1979. {
  1980. UnicodeString LocalFile;
  1981. // MakeFileList does not delimit filenames
  1982. UnicodeString FileList = MakeFileList(RemoteFileList);
  1983. if (LocalFileCommand)
  1984. {
  1985. DebugAssert(LocalFileList->Count == 1);
  1986. LocalFile = LocalFileList->Strings[0];
  1987. }
  1988. TLocalCustomCommand CustomCommand(Data,
  1989. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1990. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1991. if (NonBlocking)
  1992. {
  1993. DebugAssert(!POutput);
  1994. ExecuteShellChecked(ShellCommand);
  1995. }
  1996. else
  1997. {
  1998. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1999. }
  2000. }
  2001. else if (LocalFileCommand)
  2002. {
  2003. if (LocalFileList->Count == 1)
  2004. {
  2005. UnicodeString LocalFile = LocalFileList->Strings[0];
  2006. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2007. {
  2008. UnicodeString FileName = RemoteFileList->Strings[Index];
  2009. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2010. Progress.SetFile(FileNameForProgress);
  2011. TLocalCustomCommand CustomCommand(Data,
  2012. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2013. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2014. TOnceDoneOperation OnceDoneOperation;
  2015. // Do not unselect anything, as it won't work with two panels anyway
  2016. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2017. }
  2018. }
  2019. else if (RemoteFileList->Count == 1)
  2020. {
  2021. UnicodeString FileName = RemoteFileList->Strings[0];
  2022. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2023. {
  2024. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2025. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2026. Progress.SetFile(LocalFileNameOnly);
  2027. TLocalCustomCommand CustomCommand(
  2028. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2029. FileName, LocalFileName, L"");
  2030. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2031. TOnceDoneOperation OnceDoneOperation;
  2032. // Do not unselect anything, as it won't work with two panels anyway
  2033. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2034. }
  2035. }
  2036. else
  2037. {
  2038. if (LocalFileList->Count != RemoteFileList->Count)
  2039. {
  2040. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2041. }
  2042. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2043. {
  2044. UnicodeString FileName = RemoteFileList->Strings[Index];
  2045. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2046. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2047. Progress.SetFile(LocalFileNameOnly); // sic
  2048. TLocalCustomCommand CustomCommand(
  2049. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2050. FileName, LocalFileName, L"");
  2051. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2052. TOnceDoneOperation OnceDoneOperation;
  2053. // Do not unselect anything, as it won't work with two panels anyway
  2054. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2055. }
  2056. }
  2057. }
  2058. else
  2059. {
  2060. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2061. {
  2062. UnicodeString FileName = RemoteFileList->Strings[Index];
  2063. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2064. Progress.SetFile(FileNameForProgress);
  2065. TLocalCustomCommand CustomCommand(Data,
  2066. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2067. FileName, L"", L"");
  2068. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2069. TOnceDoneOperation OnceDoneOperation;
  2070. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2071. }
  2072. }
  2073. }
  2074. __finally
  2075. {
  2076. if (!NonBlocking)
  2077. {
  2078. Progress.Stop();
  2079. }
  2080. }
  2081. DebugAssert(!FAutoOperation);
  2082. if (!RemoteFiles)
  2083. {
  2084. TempDir = IncludeTrailingBackslash(TempDir);
  2085. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2086. {
  2087. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2088. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2089. // Skip directories, process only nested files.
  2090. // The check is redundant as FileAge fails for directories anyway.
  2091. !DirectoryExists(FileName))
  2092. {
  2093. UnicodeString RemoteDir =
  2094. UnixExtractFileDir(
  2095. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2096. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2097. TDateTime NewTime;
  2098. if (FileAge(FileName, NewTime) &&
  2099. (NewTime != RemoteFileTimes[Index]))
  2100. {
  2101. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2102. TemporaryFileCopyParam(CopyParam);
  2103. CopyParam.FileMask = L"";
  2104. FAutoOperation = true;
  2105. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2106. TemporaryFilesList->Add(FileName);
  2107. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2108. }
  2109. }
  2110. }
  2111. }
  2112. }
  2113. __finally
  2114. {
  2115. FAutoOperation = false;
  2116. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2117. {
  2118. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2119. }
  2120. }
  2121. }
  2122. __finally
  2123. {
  2124. if (RemoteFileList != FileList)
  2125. {
  2126. delete RemoteFileList;
  2127. }
  2128. if (LocalFileList != ALocalFileList)
  2129. {
  2130. delete LocalFileList;
  2131. }
  2132. if (RemoteFileListFull != RemoteFileList)
  2133. {
  2134. delete RemoteFileListFull;
  2135. }
  2136. }
  2137. }
  2138. //---------------------------------------------------------------------------
  2139. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2140. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2141. bool FileListCommand, UnicodeString * POutput)
  2142. {
  2143. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2144. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2145. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2146. {
  2147. UnicodeString FileName = SelectedFileList->Strings[Index];
  2148. if (DirectoryExists(FileName))
  2149. {
  2150. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2151. {
  2152. LocalFileList->Add(FileName);
  2153. }
  2154. if (FLAGSET(ACommand.Params, ccRecursive))
  2155. {
  2156. TMakeLocalFileListParams MakeFileListParam;
  2157. MakeFileListParam.FileList = LocalFileList.get();
  2158. MakeFileListParam.FileTimes = NULL;
  2159. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2160. MakeFileListParam.Recursive = true;
  2161. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2162. }
  2163. }
  2164. else
  2165. {
  2166. LocalFileList->Add(FileName);
  2167. }
  2168. }
  2169. UnicodeString RemotePath;
  2170. if (Terminal != NULL)
  2171. {
  2172. RemotePath = Terminal->CurrentDirectory;
  2173. }
  2174. if (FileListCommand)
  2175. {
  2176. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2177. TLocalCustomCommand CustomCommand(
  2178. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2179. L"", L"", FileList);
  2180. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2181. }
  2182. else
  2183. {
  2184. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2185. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2186. DebugAssert(FProgressForm != NULL);
  2187. FProgressForm->ReadOnly = true;
  2188. try
  2189. {
  2190. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2191. {
  2192. UnicodeString FileName = LocalFileList->Strings[Index];
  2193. Progress.SetFile(FileName);
  2194. TLocalCustomCommand CustomCommand(
  2195. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2196. FileName, L"", L"");
  2197. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2198. TOnceDoneOperation OnceDoneOperation;
  2199. Progress.Finish(FileName, true, OnceDoneOperation);
  2200. }
  2201. }
  2202. __finally
  2203. {
  2204. Progress.Stop();
  2205. }
  2206. }
  2207. }
  2208. //---------------------------------------------------------------------------
  2209. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2210. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2211. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2212. {
  2213. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2214. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2215. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2216. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2217. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2218. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2219. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2220. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2221. std::unique_ptr<UnicodeString> POutput;
  2222. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2223. FLAGSET(ACommand.Params, ccCopyResults))
  2224. {
  2225. POutput.reset(new UnicodeString());
  2226. }
  2227. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2228. FProgressSide = FCurrentSide;
  2229. if (!LocalCustomCommand.IsFileCommand(Command))
  2230. {
  2231. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2232. }
  2233. // remote files?
  2234. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2235. {
  2236. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2237. }
  2238. // local files
  2239. else
  2240. {
  2241. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2242. }
  2243. if (POutput.get() != NULL)
  2244. {
  2245. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2246. int P = POutput->Pos(sLineBreak);
  2247. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2248. {
  2249. POutput->SetLength(P - 1);
  2250. }
  2251. // Copy even empty output to clipboard
  2252. if (FLAGSET(ACommand.Params, ccCopyResults))
  2253. {
  2254. CopyToClipboard(*POutput);
  2255. }
  2256. // But do not show an empty message box.
  2257. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2258. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2259. !POutput->IsEmpty())
  2260. {
  2261. TClipboardHandler ClipboardHandler;
  2262. ClipboardHandler.Text = *POutput;
  2263. TMessageParams Params;
  2264. TQueryButtonAlias Aliases[1];
  2265. Aliases[0].Button = qaRetry;
  2266. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2267. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2268. Params.Aliases = Aliases;
  2269. Params.AliasesCount = LENOF(Aliases);
  2270. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2271. }
  2272. }
  2273. }
  2274. //---------------------------------------------------------------------------
  2275. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2276. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2277. {
  2278. TCustomCommandData Data;
  2279. UnicodeString Site;
  2280. UnicodeString RemotePath;
  2281. if (HasActiveTerminal())
  2282. {
  2283. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2284. Data = TCustomCommandData(SessionData.get());
  2285. Site = Terminal->SessionData->SessionKey;
  2286. RemotePath = Terminal->CurrentDirectory;
  2287. }
  2288. UnicodeString HelpKeyword = ACommand.HomePage;
  2289. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2290. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2291. {
  2292. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2293. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2294. {
  2295. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2296. }
  2297. else
  2298. {
  2299. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2300. }
  2301. if (!DoCustomCommandOptionsDialog(
  2302. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2303. {
  2304. Abort();
  2305. }
  2306. }
  2307. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2308. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2309. {
  2310. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2311. }
  2312. else
  2313. {
  2314. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2315. }
  2316. }
  2317. //---------------------------------------------------------------------------
  2318. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2319. const TCustomCommandType & Command)
  2320. {
  2321. DebugAssert(FCustomCommandLocalFileList != NULL);
  2322. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2323. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2324. TStrings * LocalFileList = new TStringList();
  2325. TStrings * RemoteFileList = new TStringList();
  2326. try
  2327. {
  2328. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2329. {
  2330. LocalFileList->Clear();
  2331. LocalFileList->AddObject(
  2332. FCustomCommandLocalFileList->Strings[Index],
  2333. FCustomCommandLocalFileList->Objects[Index]);
  2334. RemoteFileList->Clear();
  2335. RemoteFileList->AddObject(
  2336. FCustomCommandRemoteFileList->Strings[Index],
  2337. FCustomCommandRemoteFileList->Objects[Index]);
  2338. CustomCommand(RemoteFileList, Command, LocalFileList);
  2339. }
  2340. }
  2341. __finally
  2342. {
  2343. delete LocalFileList;
  2344. delete RemoteFileList;
  2345. }
  2346. }
  2347. //---------------------------------------------------------------------------
  2348. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2349. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2350. {
  2351. delete FCustomCommandLocalFileList;
  2352. delete FCustomCommandRemoteFileList;
  2353. // takeover ownership,
  2354. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2355. FCustomCommandLocalFileList = LocalFileList;
  2356. FCustomCommandRemoteFileList = RemoteFileList;
  2357. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2358. if (Button != NULL)
  2359. {
  2360. FCustomCommandMenu->Items->Clear();
  2361. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2362. MenuPopup(FCustomCommandMenu, Button);
  2363. }
  2364. else
  2365. {
  2366. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2367. }
  2368. }
  2369. //---------------------------------------------------------------------------
  2370. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2371. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2372. {
  2373. DebugAssert(FCapturedLog != NULL);
  2374. if ((OutputType == cotOutput) || (OutputType == cotError))
  2375. {
  2376. FCapturedLog->Add(AddedLine);
  2377. }
  2378. }
  2379. //---------------------------------------------------------------------------
  2380. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2381. TOperationSide Side)
  2382. {
  2383. return
  2384. (Side == osOther) &&
  2385. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2386. }
  2387. //---------------------------------------------------------------------------
  2388. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2389. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2390. {
  2391. TTBItemOptions O;
  2392. O = Item->Options;
  2393. TCustomDirView * DView = DirView(Side);
  2394. if ((DView->ItemFocused != NULL) &&
  2395. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2396. // when resolving links is disabled, default action is to enter the directory,
  2397. // no matter what DoubleClickAction is configured to
  2398. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2399. // Can only Edit files, but can Open/Copy even directories
  2400. ((DoubleClickAction != dcaEdit) ||
  2401. !DView->ItemIsDirectory(DView->ItemFocused)))
  2402. {
  2403. Item->Options = O << tboDefault;
  2404. }
  2405. else
  2406. {
  2407. Item->Options = O >> tboDefault;
  2408. }
  2409. }
  2410. //---------------------------------------------------------------------------
  2411. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2412. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2413. {
  2414. TCustomDirView * DView = DirView(Side);
  2415. TListItem * Item = DView->ItemFocused;
  2416. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2417. EnableFocusedOperation[Side])
  2418. {
  2419. TPoint ClientPoint;
  2420. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2421. TPoint(0, 0) : MousePos;
  2422. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2423. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2424. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2425. }
  2426. }
  2427. //---------------------------------------------------------------------------
  2428. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2429. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2430. {
  2431. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2432. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2433. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2434. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2435. Handled = true;
  2436. }
  2437. //---------------------------------------------------------------------------
  2438. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2439. {
  2440. }
  2441. //---------------------------------------------------------------------------
  2442. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2443. {
  2444. DebugAssert(FErrorList == NULL);
  2445. if (WinConfiguration->ContinueOnError)
  2446. {
  2447. FErrorList = new TStringList();
  2448. Configuration->Usage->Inc(L"ContinuationsOnError");
  2449. }
  2450. NonVisualDataModule->StartBusy();
  2451. }
  2452. //---------------------------------------------------------------------------
  2453. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2454. {
  2455. NonVisualDataModule->EndBusy();
  2456. if (FErrorList)
  2457. {
  2458. HandleErrorList(FErrorList);
  2459. }
  2460. }
  2461. //---------------------------------------------------------------------------
  2462. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2463. const TCopyParamType & CopyParam)
  2464. {
  2465. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2466. {
  2467. Configuration->Usage->Inc(L"FileMaskUses");
  2468. }
  2469. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2470. {
  2471. Configuration->Usage->Inc(L"OperationMaskUses");
  2472. }
  2473. }
  2474. //---------------------------------------------------------------------------
  2475. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2476. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2477. {
  2478. DebugAssert(!IsLocalBrowserMode());
  2479. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2480. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2481. TTransferOperationParam DefaultParam;
  2482. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2483. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2484. switch (Param.Queue)
  2485. {
  2486. case asOn:
  2487. CopyParam.Queue = true;
  2488. break;
  2489. case asOff:
  2490. CopyParam.Queue = false;
  2491. break;
  2492. case asAuto:
  2493. default:
  2494. // keep default
  2495. break;
  2496. }
  2497. bool Result =
  2498. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2499. if (Result)
  2500. {
  2501. DebugAssert(Terminal);
  2502. bool SelectionRestored = false;
  2503. TCustomDirView * DView = NULL;
  2504. if (HasDirView[Side])
  2505. {
  2506. DView = DirView(Side);
  2507. DView->SaveSelection();
  2508. DView->SaveSelectedNames();
  2509. }
  2510. UpdateCopyParamCounters(CopyParam);
  2511. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2512. DebugAssert(FTransferResumeList == NULL);
  2513. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2514. FMoveToQueue = false;
  2515. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2516. try
  2517. {
  2518. TStrings * PermanentFileList;
  2519. std::unique_ptr<TStrings> PermanentFileListOwner;
  2520. try
  2521. {
  2522. if (Side == osLocal)
  2523. {
  2524. PermanentFileList = FileList;
  2525. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2526. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2527. if (Operation == foMove)
  2528. {
  2529. ReloadLocalDirectory();
  2530. if (DView != NULL)
  2531. {
  2532. DView->RestoreSelection();
  2533. }
  2534. SelectionRestored = true;
  2535. }
  2536. }
  2537. else
  2538. {
  2539. // Clone the file list as it may refer to current directory files,
  2540. // which get destroyed, when the source directory is reloaded after foMove operation.
  2541. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2542. // But for a hotfix, we are not going to do such a big change.
  2543. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2544. PermanentFileList = PermanentFileListOwner.get();
  2545. try
  2546. {
  2547. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2548. }
  2549. __finally
  2550. {
  2551. if (Operation == foMove)
  2552. {
  2553. if (DView != NULL)
  2554. {
  2555. DView->RestoreSelection();
  2556. }
  2557. SelectionRestored = true;
  2558. }
  2559. ReloadLocalDirectory(Param.TargetDirectory);
  2560. }
  2561. }
  2562. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2563. if (FMoveToQueue)
  2564. {
  2565. Abort();
  2566. }
  2567. }
  2568. catch (EAbort &)
  2569. {
  2570. if (FMoveToQueue)
  2571. {
  2572. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2573. DebugAssert(CopyParam.TransferSkipList == NULL);
  2574. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2575. if (TransferResumeList->Count > 0)
  2576. {
  2577. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2578. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2579. }
  2580. CopyParam.TransferSkipList = TransferResumeList.release();
  2581. // not really needed, just to keep it consistent with TransferResumeList
  2582. FTransferResumeList = NULL;
  2583. FMoveToQueue = false;
  2584. Configuration->Usage->Inc("MovesToBackground");
  2585. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2586. ClearTransferSourceSelection(Direction);
  2587. }
  2588. throw;
  2589. }
  2590. }
  2591. __finally
  2592. {
  2593. if (!SelectionRestored && (DView != NULL))
  2594. {
  2595. DView->DiscardSavedSelection();
  2596. }
  2597. FTransferResumeList = NULL;
  2598. }
  2599. }
  2600. return Result;
  2601. }
  2602. //---------------------------------------------------------------------------
  2603. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2604. TOperationSide Side, TStrings * FileList, void * Param)
  2605. {
  2606. DebugAssert(FileList->Count);
  2607. // We deliberately do not toggle alternative flag (Param), but use OR,
  2608. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2609. // shortcut of CurrentDeleteAlternativeAction
  2610. bool Alternative =
  2611. bool(Param) || UseAlternativeFunction();
  2612. bool Recycle;
  2613. if (IsSideLocalBrowser(Side))
  2614. {
  2615. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2616. }
  2617. else
  2618. {
  2619. Recycle =
  2620. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2621. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2622. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2623. }
  2624. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2625. if (!Result)
  2626. {
  2627. UnicodeString Query;
  2628. if (FileList->Count == 1)
  2629. {
  2630. if (IsSideLocalBrowser(Side))
  2631. {
  2632. Query = ExtractFileName(FileList->Strings[0]);
  2633. }
  2634. else
  2635. {
  2636. Query = UnixExtractFileName(FileList->Strings[0]);
  2637. }
  2638. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2639. }
  2640. else
  2641. {
  2642. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2643. }
  2644. TMessageParams Params(mpNeverAskAgainCheck);
  2645. Params.ImageName = L"Delete file";
  2646. unsigned int Answer =
  2647. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2648. if (Answer == qaNeverAskAgain)
  2649. {
  2650. Result = true;
  2651. if (Recycle)
  2652. {
  2653. WinConfiguration->ConfirmRecycling = false;
  2654. }
  2655. else
  2656. {
  2657. WinConfiguration->ConfirmDeleting = false;
  2658. }
  2659. }
  2660. else
  2661. {
  2662. Result = (Answer == qaOK);
  2663. }
  2664. }
  2665. if (Result)
  2666. {
  2667. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2668. }
  2669. return Result;
  2670. }
  2671. //---------------------------------------------------------------------------
  2672. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2673. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2674. {
  2675. TAutoBatch AutoBatch(this);
  2676. bool Result;
  2677. if ((Operation == foCopy) || (Operation == foMove))
  2678. {
  2679. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2680. }
  2681. else if (Operation == foRename)
  2682. {
  2683. DebugAssert(DirView(Side)->ItemFocused);
  2684. DirView(Side)->ItemFocused->EditCaption();
  2685. Result = true;
  2686. }
  2687. else if (Operation == foDelete)
  2688. {
  2689. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2690. }
  2691. else if (Operation == foSetProperties)
  2692. {
  2693. DirView(osRemote)->SaveSelectedNames();
  2694. Result = SetProperties(Side, FileList);
  2695. }
  2696. else if (Operation == foCustomCommand)
  2697. {
  2698. DebugAssert(Param);
  2699. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2700. DirView(osRemote)->SaveSelectedNames();
  2701. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2702. CustomCommand(FileList, *Command, NULL);
  2703. Result = true;
  2704. }
  2705. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2706. {
  2707. DebugAssert(!IsSideLocalBrowser(Side));
  2708. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2709. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2710. }
  2711. else if (Operation == foLock)
  2712. {
  2713. DebugAssert(!IsSideLocalBrowser(Side));
  2714. LockFiles(FileList, true);
  2715. Result = true;
  2716. }
  2717. else if (Operation == foUnlock)
  2718. {
  2719. DebugAssert(!IsSideLocalBrowser(Side));
  2720. LockFiles(FileList, false);
  2721. Result = true;
  2722. }
  2723. else
  2724. {
  2725. DebugFail();
  2726. }
  2727. return Result;
  2728. }
  2729. //---------------------------------------------------------------------------
  2730. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2731. {
  2732. if (Side == osCurrent)
  2733. {
  2734. Side = FCurrentSide;
  2735. }
  2736. return Side;
  2737. }
  2738. //---------------------------------------------------------------------------
  2739. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2740. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2741. {
  2742. Side = GetSide(Side);
  2743. bool Result;
  2744. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2745. try
  2746. {
  2747. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2748. }
  2749. __finally
  2750. {
  2751. delete FileList;
  2752. }
  2753. return Result;
  2754. }
  2755. //---------------------------------------------------------------------------
  2756. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2757. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2758. bool NoConfirmation, void * Param)
  2759. {
  2760. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2761. {
  2762. if ((Operation == foCopy) || (Operation == foMove))
  2763. {
  2764. DebugAssert(!IsLocalBrowserMode());
  2765. if (GetSide(Side) == osLocal)
  2766. {
  2767. Configuration->Usage->Inc(L"UploadsCommand");
  2768. }
  2769. else
  2770. {
  2771. Configuration->Usage->Inc(L"DownloadsCommand");
  2772. }
  2773. }
  2774. }
  2775. }
  2776. //---------------------------------------------------------------------------
  2777. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2778. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2779. {
  2780. TTransferOperationParam Param;
  2781. if ((WinConfiguration->Interface != ifCommander) ||
  2782. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2783. {
  2784. Flags &= ~cocShortCutHint;
  2785. }
  2786. TCustomDirView * DView = DirView(Side);
  2787. Param.Options =
  2788. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2789. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2790. if (FLAGSET(Flags, cocQueue))
  2791. {
  2792. Param.Queue = asOn;
  2793. }
  2794. else if (FLAGSET(Flags, cocNonQueue))
  2795. {
  2796. Param.Queue = asOff;
  2797. }
  2798. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2799. }
  2800. //---------------------------------------------------------------------------
  2801. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2802. {
  2803. int Count = ErrorList->Count;
  2804. try
  2805. {
  2806. if (ErrorList->Count)
  2807. {
  2808. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2809. if (MessageDialog(Message, qtError,
  2810. qaOK | qaCancel, HELP_NONE) == qaOK)
  2811. {
  2812. unsigned int Answer;
  2813. int Index = 0;
  2814. do
  2815. {
  2816. DebugAssert(Index >= 0 && Index < Count);
  2817. TQueryButtonAlias Aliases[2];
  2818. Aliases[0].Button = qaYes;
  2819. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2820. Aliases[1].Button = qaNo;
  2821. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2822. TMessageParams Params;
  2823. Params.Aliases = Aliases;
  2824. Params.AliasesCount = LENOF(Aliases);
  2825. int No = Index + 1;
  2826. UnicodeString Message = ErrorList->Strings[Index];
  2827. UnicodeString AMainInstructions;
  2828. UnicodeString Details;
  2829. if (ExtractMainInstructions(Message, AMainInstructions))
  2830. {
  2831. Details = Message;
  2832. Message = AMainInstructions;
  2833. }
  2834. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2835. Answer = MoreMessageDialog(
  2836. Message,
  2837. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2838. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2839. qaOK, HELP_NONE, &Params);
  2840. if (Answer == qaNo)
  2841. {
  2842. Index++;
  2843. }
  2844. if (Answer == qaYes)
  2845. {
  2846. Index--;
  2847. }
  2848. }
  2849. while (Answer != qaOK);
  2850. }
  2851. }
  2852. }
  2853. __finally
  2854. {
  2855. TStrings * List = ErrorList;
  2856. ErrorList = NULL;
  2857. for (int i = 0; i < Count; i++)
  2858. {
  2859. delete List->Objects[i];
  2860. }
  2861. delete List;
  2862. }
  2863. }
  2864. //---------------------------------------------------------------------------
  2865. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2866. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2867. {
  2868. // needed for checking filemasks, as there's no directory object
  2869. // associated with the file object
  2870. File->FullFileName = FullFileName;
  2871. TFileMasks::TParams MaskParams;
  2872. MaskParams.Size = File->Size;
  2873. MaskParams.Modification = File->Modification;
  2874. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2875. }
  2876. //---------------------------------------------------------------------------
  2877. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2878. {
  2879. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2880. Side = GetSide(Side);
  2881. TCustomDirView * DView = DirView(Side);
  2882. TListItem * FocusedItem = DView->ItemFocused;
  2883. UnicodeString Name;
  2884. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2885. {
  2886. Name = DView->ItemFileName(FocusedItem);
  2887. }
  2888. else
  2889. {
  2890. Name = LoadStr(NEW_FILE);
  2891. }
  2892. UnicodeString Names = Name;
  2893. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2894. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2895. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2896. {
  2897. while (!Names.IsEmpty())
  2898. {
  2899. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2900. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2901. UnicodeString TargetFileName;
  2902. UnicodeString LocalFileName;
  2903. UnicodeString RootTempDir;
  2904. UnicodeString TempDir;
  2905. UnicodeString RemoteDirectory;
  2906. bool ExistingFile = false;
  2907. if (!IsSideLocalBrowser(Side))
  2908. {
  2909. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2910. TRemoteFile * File = NULL;
  2911. if (Terminal->FileExists(Name, &File) &&
  2912. !File->IsDirectory)
  2913. {
  2914. try
  2915. {
  2916. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2917. ExistingFile = true;
  2918. }
  2919. __finally
  2920. {
  2921. delete File;
  2922. }
  2923. }
  2924. if (!ExistingFile)
  2925. {
  2926. RemoteDirectory = UnixExtractFilePath(Name);
  2927. TemporaryDirectoryForRemoteFiles(
  2928. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2929. TargetFileName = UnixExtractFileName(Name);
  2930. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2931. LocalFileName = TempDir +
  2932. // We probably do not want to trim the VMS version here
  2933. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2934. }
  2935. }
  2936. else
  2937. {
  2938. if (ExtractFilePath(Name).IsEmpty())
  2939. {
  2940. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2941. }
  2942. else
  2943. {
  2944. LocalFileName = ExpandFileName(Name);
  2945. }
  2946. TargetFileName = ExtractFileName(Name);
  2947. }
  2948. if (!ExistingFile)
  2949. {
  2950. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2951. if (NewFile)
  2952. {
  2953. int File = FileCreate(ApiPath(LocalFileName));
  2954. if (File < 0)
  2955. {
  2956. if (!RootTempDir.IsEmpty())
  2957. {
  2958. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2959. }
  2960. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2961. }
  2962. else
  2963. {
  2964. FileClose(File);
  2965. }
  2966. }
  2967. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2968. const TEditorData * ExternalEditor = NULL;
  2969. TFileMasks::TParams MaskParams; // size not known
  2970. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2971. false, MaskParams);
  2972. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2973. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2974. }
  2975. }
  2976. }
  2977. }
  2978. //---------------------------------------------------------------------------
  2979. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2980. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2981. {
  2982. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2983. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2984. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2985. return
  2986. ((ExecuteFileBy == efInternalEditor)) ||
  2987. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2988. }
  2989. //---------------------------------------------------------------------------
  2990. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2991. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2992. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2993. UnicodeString RemoteDirectory, bool NewFile)
  2994. {
  2995. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2996. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2997. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2998. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2999. Side = GetSide(Side);
  3000. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3001. if (!IsSideLocalBrowser(Side))
  3002. {
  3003. Data->Terminal = Terminal;
  3004. Data->Queue = Queue;
  3005. Data->SessionData = CloneCurrentSessionData();
  3006. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3007. Data->RemoteDirectory = RemoteDirectory;
  3008. Data->SessionName = Terminal->SessionData->SessionName;
  3009. Data->LocalRootDirectory = LocalRootDirectory;
  3010. Data->OriginalFileName = OriginalFileName;
  3011. Data->Command = L""; // will be changed later for external editor
  3012. }
  3013. if (ExecuteFileBy == efInternalEditor)
  3014. {
  3015. if (!IsSideLocalBrowser(Side))
  3016. {
  3017. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3018. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3019. TForm * Editor;
  3020. try
  3021. {
  3022. TNotifyEvent OnFileChanged = NULL;
  3023. TNotifyEvent OnSaveAll = NULL;
  3024. TAnyModifiedEvent OnAnyModified = NULL;
  3025. // Edited files are uploaded in background queue, what we do not support with encrypted files
  3026. if (Terminal->IsCapable[fcBackgroundTransfers])
  3027. {
  3028. OnFileChanged = FEditorManager->FileChanged;
  3029. OnSaveAll = SaveAllInternalEditors;
  3030. OnAnyModified = AnyInternalEditorModified;
  3031. }
  3032. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  3033. FEditorManager->FileReload, FEditorManager->FileClosed,
  3034. OnSaveAll, OnAnyModified,
  3035. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3036. }
  3037. catch(...)
  3038. {
  3039. if (!LocalRootDirectory.IsEmpty())
  3040. {
  3041. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3042. }
  3043. throw;
  3044. }
  3045. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3046. }
  3047. else
  3048. {
  3049. DebugAssert(!FStandaloneEditing);
  3050. TForm * Editor =
  3051. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3052. SaveAllInternalEditors, AnyInternalEditorModified,
  3053. L"", false, SessionColor, -1, NewFile);
  3054. FLocalEditors->Add(Editor);
  3055. }
  3056. }
  3057. else
  3058. {
  3059. HANDLE Process;
  3060. if (ExecuteFileBy == efExternalEditor)
  3061. {
  3062. UnicodeString Program, Params, Dir;
  3063. Data->Command = ExternalEditor->ExternalEditor;
  3064. ReformatFileNameCommand(Data->Command);
  3065. SplitCommand(Data->Command, Program, Params, Dir);
  3066. Params = ExpandFileNameCommand(Params, FileName);
  3067. Program = ExpandEnvironmentVariables(Program);
  3068. if (!ExecuteShell(Program, Params, Process))
  3069. {
  3070. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3071. }
  3072. }
  3073. else
  3074. {
  3075. DebugAssert(!IsSideLocalBrowser(Side));
  3076. if (!ExecuteShell(FileName, L"", Process))
  3077. {
  3078. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3079. }
  3080. }
  3081. if (IsSideLocalBrowser(Side) ||
  3082. ((ExecuteFileBy == efShell) &&
  3083. !WinConfiguration->Editor.SDIShellEditor) ||
  3084. ((ExecuteFileBy == efExternalEditor) &&
  3085. !ExternalEditor->SDIExternalEditor))
  3086. {
  3087. // no need for handle
  3088. if (Process != NULL)
  3089. {
  3090. DebugCheck(CloseHandle(Process));
  3091. }
  3092. Process = INVALID_HANDLE_VALUE;
  3093. }
  3094. else
  3095. {
  3096. if (Process == NULL)
  3097. {
  3098. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3099. }
  3100. }
  3101. if (!IsSideLocalBrowser(Side))
  3102. {
  3103. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3104. }
  3105. }
  3106. }
  3107. //---------------------------------------------------------------------------
  3108. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3109. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3110. void * /*Arg*/)
  3111. {
  3112. if (Token != NULL)
  3113. {
  3114. EditorFormFileSave(static_cast<TForm *>(Token));
  3115. }
  3116. }
  3117. //---------------------------------------------------------------------------
  3118. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3119. {
  3120. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3121. {
  3122. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3123. }
  3124. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3125. }
  3126. //---------------------------------------------------------------------------
  3127. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3128. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3129. void * Arg)
  3130. {
  3131. if ((Token != NULL) &&
  3132. IsEditorFormModified(static_cast<TForm *>(Token)))
  3133. {
  3134. *static_cast<bool *>(Arg) = true;
  3135. }
  3136. }
  3137. //---------------------------------------------------------------------------
  3138. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3139. TObject * /*Sender*/, bool & Modified)
  3140. {
  3141. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3142. {
  3143. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3144. {
  3145. Modified = true;
  3146. }
  3147. }
  3148. if (!Modified)
  3149. {
  3150. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3151. }
  3152. }
  3153. //---------------------------------------------------------------------------
  3154. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3155. {
  3156. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3157. }
  3158. //---------------------------------------------------------------------------
  3159. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3160. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3161. UnicodeString & Result, UnicodeString & RootDirectory)
  3162. {
  3163. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3164. {
  3165. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3166. Result = RootDirectory;
  3167. }
  3168. else
  3169. {
  3170. RootDirectory = L"";
  3171. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3172. Result = IncludeTrailingBackslash(Result);
  3173. }
  3174. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3175. {
  3176. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3177. }
  3178. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3179. {
  3180. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3181. {
  3182. RemoteDirectory.Delete(1, 1);
  3183. }
  3184. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3185. }
  3186. if (!ForceDirectories(ApiPath(Result)))
  3187. {
  3188. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3189. }
  3190. }
  3191. //---------------------------------------------------------------------------
  3192. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3193. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3194. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3195. {
  3196. DebugAssert(!IsLocalBrowserMode());
  3197. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3198. if (ForceText)
  3199. {
  3200. CopyParam.TransferMode = tmAscii;
  3201. }
  3202. TemporaryFileCopyParam(CopyParam);
  3203. if (AllFiles)
  3204. {
  3205. CopyParam.IncludeFileMask = TFileMasks();
  3206. }
  3207. if (TempDir.IsEmpty())
  3208. {
  3209. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3210. }
  3211. DebugAssert(!FAutoOperation);
  3212. FAutoOperation = AutoOperation;
  3213. Terminal->ExceptionOnFail = true;
  3214. try
  3215. {
  3216. try
  3217. {
  3218. // turn off confirmations, as for MDI editors we may possibly download
  3219. // the same file over
  3220. Terminal->CopyToLocal(
  3221. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3222. if (GetTargetNames)
  3223. {
  3224. for (int i = 0; i < FileList->Count; i++)
  3225. {
  3226. FileList->Strings[i] =
  3227. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3228. }
  3229. }
  3230. }
  3231. catch(...)
  3232. {
  3233. if (!RootTempDir.IsEmpty())
  3234. {
  3235. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3236. }
  3237. throw;
  3238. }
  3239. }
  3240. __finally
  3241. {
  3242. FAutoOperation = false;
  3243. Terminal->ExceptionOnFail = false;
  3244. }
  3245. }
  3246. //---------------------------------------------------------------------------
  3247. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3248. {
  3249. // Do not waste time checking for default editor list the next time,
  3250. // or testing if default editor is not notepad.
  3251. bool TryNextTime = false;
  3252. UnicodeString UsageState;
  3253. if (!WinConfiguration->EditorList->IsDefaultList())
  3254. {
  3255. UsageState = "H";
  3256. }
  3257. else
  3258. {
  3259. UnicodeString Executable;
  3260. UnicodeString ExecutableDescription;
  3261. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3262. {
  3263. UnicodeString Message =
  3264. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3265. unsigned int Answer =
  3266. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3267. if (Answer != qaOK)
  3268. {
  3269. UsageState = "R";
  3270. }
  3271. else
  3272. {
  3273. UsageState = "A";
  3274. TEditorData EditorData;
  3275. EditorData.Editor = edExternal;
  3276. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3277. EditorData.ExternalEditorOptionsAutodetect();
  3278. TEditorList EditorList;
  3279. EditorList = *WinConfiguration->EditorList;
  3280. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3281. WinConfiguration->EditorList = &EditorList;
  3282. }
  3283. }
  3284. }
  3285. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3286. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3287. }
  3288. //---------------------------------------------------------------------------
  3289. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3290. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3291. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3292. {
  3293. if (ExecuteFileBy == efDefaultEditor)
  3294. {
  3295. if (!WinConfiguration->OfferedEditorAutoConfig)
  3296. {
  3297. EditorAutoConfig();
  3298. }
  3299. const TEditorPreferences * Editor =
  3300. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3301. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3302. {
  3303. ExecuteFileBy = efInternalEditor;
  3304. ExternalEditor = NULL;
  3305. }
  3306. else if (Editor->Data->Editor == edOpen)
  3307. {
  3308. ExecuteFileBy = efShell;
  3309. ExternalEditor = NULL;
  3310. }
  3311. else
  3312. {
  3313. ExecuteFileBy = efExternalEditor;
  3314. ExternalEditor = Editor->Data;
  3315. }
  3316. }
  3317. }
  3318. //---------------------------------------------------------------------------
  3319. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3320. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3321. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3322. {
  3323. UnicodeString OriginalFileName;
  3324. UnicodeString LocalRootDirectory;
  3325. UnicodeString RemoteDirectory;
  3326. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3327. IsSideLocalBrowser(Side), MaskParams);
  3328. UnicodeString Counter;
  3329. UnicodeString LocalFileName;
  3330. bool Handled = false;
  3331. if (!IsSideLocalBrowser(Side))
  3332. {
  3333. // We need to trim VMS version here, so that we use name without version
  3334. // when uploading back to create a new version of the file
  3335. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3336. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3337. TObject * Token = NULL;
  3338. UnicodeString LocalDirectory;
  3339. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3340. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3341. LocalDirectory))
  3342. {
  3343. if (Token != NULL)
  3344. {
  3345. TForm * Form = dynamic_cast<TForm *>(Token);
  3346. if (Form->WindowState == wsMinimized)
  3347. {
  3348. ShowWindow(Form->Handle, SW_RESTORE);
  3349. }
  3350. else
  3351. {
  3352. Form->SetFocus();
  3353. }
  3354. Handled = true;
  3355. }
  3356. else
  3357. {
  3358. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3359. }
  3360. }
  3361. if (!Handled)
  3362. {
  3363. TStringList * FileList1 = new TStringList();
  3364. try
  3365. {
  3366. FileList1->AddObject(FullFileName, Object);
  3367. TemporarilyDownloadFiles(FileList1,
  3368. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3369. LocalRootDirectory, LocalDirectory, true, true, true);
  3370. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3371. }
  3372. __finally
  3373. {
  3374. delete FileList1;
  3375. }
  3376. switch (ExecuteFileBy)
  3377. {
  3378. case efShell:
  3379. Counter = "RemoteFilesExecuted";
  3380. break;
  3381. case efInternalEditor:
  3382. Counter = "RemoteFilesOpenedInInternalEditor";
  3383. break;
  3384. case efExternalEditor:
  3385. Counter = "RemoteFilesOpenedInExternalEditor";
  3386. break;
  3387. default:
  3388. DebugFail();
  3389. }
  3390. }
  3391. }
  3392. else
  3393. {
  3394. LocalFileName = FullFileName;
  3395. OriginalFileName = ExtractFileName(FullFileName);
  3396. switch (ExecuteFileBy)
  3397. {
  3398. case efShell:
  3399. Counter = "LocalFilesExecuted";
  3400. break;
  3401. case efInternalEditor:
  3402. Counter = "LocalFilesOpenedInInternalEditor";
  3403. break;
  3404. case efExternalEditor:
  3405. Counter = "LocalFilesOpenedInExternalEditor";
  3406. break;
  3407. default:
  3408. DebugFail();
  3409. }
  3410. }
  3411. if (!Handled)
  3412. {
  3413. Configuration->Usage->Inc(Counter);
  3414. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3415. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3416. }
  3417. }
  3418. //---------------------------------------------------------------------------
  3419. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3420. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3421. bool AllSelected, bool OnFocused)
  3422. {
  3423. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3424. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3425. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3426. Side = GetSide(Side);
  3427. TCustomDirView * DView = DirView(Side);
  3428. TStrings * FileList =
  3429. AllSelected ?
  3430. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3431. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3432. try
  3433. {
  3434. DebugAssert(AllSelected || (FileList->Count == 1));
  3435. for (int i = 0; i < FileList->Count; i++)
  3436. {
  3437. UnicodeString ListFileName = FileList->Strings[i];
  3438. UnicodeString FileNameOnly =
  3439. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3440. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3441. if (!DView->ItemIsDirectory(Item))
  3442. {
  3443. UnicodeString FullFileName;
  3444. if (!IsSideLocalBrowser(Side))
  3445. {
  3446. FullFileName = DirView(Side)->Path + ListFileName;
  3447. }
  3448. else
  3449. {
  3450. FullFileName = ListFileName;
  3451. }
  3452. TFileMasks::TParams MaskParams;
  3453. MaskParams.Size = DView->ItemFileSize(Item);
  3454. TDateTimePrecision Precision;
  3455. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3456. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3457. FileList->Objects[i], MaskParams);
  3458. }
  3459. }
  3460. }
  3461. __finally
  3462. {
  3463. delete FileList;
  3464. }
  3465. }
  3466. //---------------------------------------------------------------------------
  3467. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3468. {
  3469. CopyParam.FileNameCase = ncNoChange;
  3470. CopyParam.PreserveRights = false;
  3471. CopyParam.PreserveReadOnly = false;
  3472. CopyParam.ReplaceInvalidChars = true;
  3473. CopyParam.IncludeFileMask = TFileMasks();
  3474. CopyParam.NewerOnly = false;
  3475. CopyParam.FileMask = L"";
  3476. }
  3477. //---------------------------------------------------------------------------
  3478. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3479. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3480. {
  3481. TTerminalManager * Manager = TTerminalManager::Instance();
  3482. if (!IsActiveTerminal(Data->Terminal))
  3483. {
  3484. if (!NonVisualDataModule->Busy)
  3485. {
  3486. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3487. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3488. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3489. if (Data->Terminal == NULL)
  3490. {
  3491. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3492. if (SameSiteTerminal != NULL)
  3493. {
  3494. UnicodeString Message =
  3495. FMTLOAD(EDIT_SESSION_REATTACH,
  3496. (FileNameOnly, Data->SessionName, FileNameOnly));
  3497. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3498. {
  3499. Data->Terminal = Terminal;
  3500. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3501. Data->SessionName = Terminal->SessionData->SessionName;
  3502. // We might also overwrite session data
  3503. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3504. }
  3505. else
  3506. {
  3507. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3508. Abort();
  3509. }
  3510. }
  3511. }
  3512. // foreground session should reconnect itself
  3513. else if (Terminal != Data->Terminal)
  3514. {
  3515. UnicodeString Message =
  3516. MainInstructions(
  3517. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3518. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3519. {
  3520. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3521. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3522. }
  3523. else
  3524. {
  3525. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3526. Abort();
  3527. }
  3528. }
  3529. }
  3530. if (!IsActiveTerminal(Data->Terminal))
  3531. {
  3532. Configuration->Usage->Inc(L"EditInactiveSession");
  3533. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3534. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3535. (ExtractFileName(FileName), Data->SessionName)));
  3536. }
  3537. }
  3538. TStrings * FileList = new TStringList();
  3539. try
  3540. {
  3541. FileList->Add(FileName);
  3542. // consider using the same settings (preset) as when the file was downloaded
  3543. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3544. TemporaryFileCopyParam(CopyParam);
  3545. if (Data->ForceText)
  3546. {
  3547. CopyParam.TransferMode = tmAscii;
  3548. }
  3549. // so i do not need to worry if masking algorithm works in all cases
  3550. // ("" means "copy file name", no masking is actually done)
  3551. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3552. {
  3553. CopyParam.FileMask = L"";
  3554. }
  3555. else
  3556. {
  3557. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3558. }
  3559. DebugAssert(Data->Queue != NULL);
  3560. int Params = cpNoConfirmation | cpTemporary;
  3561. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3562. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3563. QueueItem->CompleteEvent = UploadCompleteEvent;
  3564. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3565. }
  3566. __finally
  3567. {
  3568. delete FileList;
  3569. }
  3570. }
  3571. //---------------------------------------------------------------------------
  3572. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3573. const UnicodeString FileName, const TEditedFileData * Data)
  3574. {
  3575. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3576. DebugAssert(!NonVisualDataModule->Busy);
  3577. if (!IsActiveTerminal(Data->Terminal))
  3578. {
  3579. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3580. (ExtractFileName(FileName), Data->SessionName)));
  3581. }
  3582. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3583. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3584. NonVisualDataModule->StartBusy();
  3585. try
  3586. {
  3587. std::unique_ptr<TRemoteFile> File;
  3588. UnicodeString RemoteFileName =
  3589. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3590. Terminal->ExceptionOnFail = true;
  3591. try
  3592. {
  3593. TRemoteFile * AFile = NULL;
  3594. Terminal->ReadFile(RemoteFileName, AFile);
  3595. File.reset(AFile);
  3596. if (!File->HaveFullFileName)
  3597. {
  3598. File->FullFileName = RemoteFileName;
  3599. }
  3600. }
  3601. __finally
  3602. {
  3603. Terminal->ExceptionOnFail = false;
  3604. }
  3605. std::unique_ptr<TStrings> FileList(new TStringList());
  3606. FileList->AddObject(RemoteFileName, File.get());
  3607. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3608. UnicodeString TempDir = ExtractFilePath(FileName);
  3609. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3610. TempDir, true, true, true);
  3611. // sanity check, the target file name should be still the same
  3612. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3613. }
  3614. __finally
  3615. {
  3616. NonVisualDataModule->EndBusy();
  3617. // it actually may not exist anymore...
  3618. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3619. }
  3620. }
  3621. //---------------------------------------------------------------------------
  3622. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3623. const TEditedFileData * Data, bool & KeepOpen)
  3624. {
  3625. // Command is set for external editors only (not for "shell" open).
  3626. if (!Data->Command.IsEmpty())
  3627. {
  3628. bool AnyFound = false;
  3629. bool AnyMDI = false;
  3630. bool AnyNonMDI = false;
  3631. bool AnyDetect = false;
  3632. TEditorList * EditorList = new TEditorList();
  3633. try
  3634. {
  3635. *EditorList = *WinConfiguration->EditorList;
  3636. for (int i = 0; i < EditorList->Count; i++)
  3637. {
  3638. const TEditorPreferences * Editor = EditorList->Editors[i];
  3639. if ((Editor->Data->Editor == edExternal) &&
  3640. (Editor->Data->ExternalEditor == Data->Command))
  3641. {
  3642. AnyFound = true;
  3643. if (Editor->Data->SDIExternalEditor)
  3644. {
  3645. AnyNonMDI = true;
  3646. if (Editor->Data->DetectMDIExternalEditor)
  3647. {
  3648. AnyDetect = true;
  3649. }
  3650. }
  3651. else
  3652. {
  3653. AnyMDI = true;
  3654. }
  3655. }
  3656. }
  3657. bool EnableMDI = false;
  3658. bool DisableDetect = false;
  3659. if (AnyMDI)
  3660. {
  3661. KeepOpen = true;
  3662. if (AnyNonMDI)
  3663. {
  3664. // there is at least one instance of the editor with MDI support enabled,
  3665. // and one with disabled, enable it for all instances
  3666. EnableMDI = true;
  3667. }
  3668. }
  3669. else if (AnyFound && !AnyDetect)
  3670. {
  3671. // at least once instance found but all have MDI autodetection disabled
  3672. // => close the file (default action)
  3673. }
  3674. else
  3675. {
  3676. // no instance of the editor has MDI support enabled
  3677. TMessageParams Params;
  3678. if (AnyFound)
  3679. {
  3680. // there is at least one instance of the editor
  3681. Params.Params |= mpNeverAskAgainCheck;
  3682. }
  3683. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3684. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3685. switch (Answer)
  3686. {
  3687. case qaNeverAskAgain:
  3688. DisableDetect = true;
  3689. break;
  3690. case qaNo:
  3691. EnableMDI = true;
  3692. KeepOpen = true;
  3693. break;
  3694. }
  3695. }
  3696. if (AnyFound && (EnableMDI || DisableDetect))
  3697. {
  3698. bool Changed = false;
  3699. for (int i = 0; i < EditorList->Count; i++)
  3700. {
  3701. const TEditorPreferences * Editor = EditorList->Editors[i];
  3702. if ((Editor->Data->Editor == edExternal) &&
  3703. (Editor->Data->ExternalEditor == Data->Command) &&
  3704. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3705. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3706. {
  3707. Changed = true;
  3708. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3709. if (EnableMDI)
  3710. {
  3711. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3712. }
  3713. if (DisableDetect)
  3714. {
  3715. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3716. }
  3717. EditorList->Change(i, UpdatedEditor);
  3718. }
  3719. }
  3720. if (Changed)
  3721. {
  3722. WinConfiguration->EditorList = EditorList;
  3723. }
  3724. }
  3725. }
  3726. __finally
  3727. {
  3728. delete EditorList;
  3729. }
  3730. }
  3731. else
  3732. {
  3733. // "open" case
  3734. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3735. qaOK, HELP_APP_EARLY_CLOSED);
  3736. }
  3737. }
  3738. //---------------------------------------------------------------------------
  3739. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3740. {
  3741. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3742. }
  3743. //---------------------------------------------------------------------------
  3744. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3745. {
  3746. SideEnter(osRemote);
  3747. }
  3748. //---------------------------------------------------------------------------
  3749. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3750. {
  3751. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3752. SideEnter(osRemote);
  3753. }
  3754. //---------------------------------------------------------------------------
  3755. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3756. {
  3757. FCurrentSide = Side;
  3758. if (Visible)
  3759. {
  3760. UpdateControls();
  3761. }
  3762. }
  3763. //---------------------------------------------------------------------------
  3764. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3765. TStrings * FileList, bool Alternative)
  3766. {
  3767. TCustomDirView * DView = DirView(Side);
  3768. DView->SaveSelection();
  3769. DView->SaveSelectedNames();
  3770. DebugAssert(!FAlternativeDelete);
  3771. FAlternativeDelete = Alternative;
  3772. try
  3773. {
  3774. if (!IsSideLocalBrowser(Side))
  3775. {
  3776. DebugAssert(Terminal != NULL);
  3777. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3778. }
  3779. else
  3780. {
  3781. if (IsLocalBrowserMode())
  3782. {
  3783. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3784. }
  3785. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3786. FProgressSide = FCurrentSide;
  3787. try
  3788. {
  3789. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3790. }
  3791. __finally
  3792. {
  3793. ReloadLocalDirectory();
  3794. }
  3795. }
  3796. FAlternativeDelete = false;
  3797. }
  3798. catch(...)
  3799. {
  3800. FAlternativeDelete = false;
  3801. DView->DiscardSavedSelection();
  3802. throw;
  3803. }
  3804. DView->RestoreSelection();
  3805. }
  3806. //---------------------------------------------------------------------------
  3807. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3808. {
  3809. DebugAssert(Terminal);
  3810. DebugAssert(!IsLocalBrowserMode());
  3811. RemoteDirView->SaveSelection();
  3812. RemoteDirView->SaveSelectedNames();
  3813. try
  3814. {
  3815. if (Lock)
  3816. {
  3817. Terminal->LockFiles(FileList);
  3818. }
  3819. else
  3820. {
  3821. Terminal->UnlockFiles(FileList);
  3822. }
  3823. }
  3824. catch(...)
  3825. {
  3826. RemoteDirView->DiscardSavedSelection();
  3827. throw;
  3828. }
  3829. RemoteDirView->RestoreSelection();
  3830. }
  3831. //---------------------------------------------------------------------------
  3832. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3833. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3834. bool NoConfirmation, bool Move)
  3835. {
  3836. DebugAssert(Terminal != NULL);
  3837. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3838. DebugAssert(!IsLocalBrowserMode());
  3839. if (Session == NULL)
  3840. {
  3841. Session = Terminal;
  3842. }
  3843. if (Session == Terminal)
  3844. {
  3845. if (RemoteDriveView->DropTarget != NULL)
  3846. {
  3847. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3848. }
  3849. else if (RemoteDirView->DropTarget != NULL)
  3850. {
  3851. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3852. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3853. }
  3854. else
  3855. {
  3856. Target = RemoteDirView->Path;
  3857. }
  3858. }
  3859. else
  3860. {
  3861. Target = GetSessionPath(Session, osRemote);
  3862. }
  3863. Target = UnixIncludeTrailingBackslash(Target);
  3864. if (FileList->Count == 1)
  3865. {
  3866. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3867. }
  3868. else
  3869. {
  3870. FileMask = AnyMask;
  3871. }
  3872. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3873. bool Result = true;
  3874. if (!NoConfirmation)
  3875. {
  3876. bool Multi = (FileList->Count > 1);
  3877. if (Move)
  3878. {
  3879. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3880. }
  3881. else
  3882. {
  3883. std::unique_ptr<TStrings> Sessions(new TStringList);
  3884. std::unique_ptr<TStrings> Directories(new TStringList);
  3885. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3886. for (int Index = 0; Index < SessionList->Count; Index++)
  3887. {
  3888. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3889. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3890. {
  3891. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3892. Directories->Add(GetSessionPath(ASession, osRemote));
  3893. }
  3894. }
  3895. TDirectRemoteCopy AllowDirectCopy;
  3896. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3897. {
  3898. DebugAssert(DirectCopy);
  3899. AllowDirectCopy = drcAllow;
  3900. }
  3901. else if (Terminal->IsCapable[fcSecondaryShell])
  3902. {
  3903. DebugAssert(DirectCopy);
  3904. AllowDirectCopy = drcConfirmCommandSession;
  3905. }
  3906. else
  3907. {
  3908. DebugAssert(!DirectCopy);
  3909. AllowDirectCopy = drcDisallow;
  3910. }
  3911. void * ASession = Session;
  3912. Result = DoRemoteCopyDialog(
  3913. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3914. Session = static_cast<TManagedTerminal *>(ASession);
  3915. }
  3916. }
  3917. return Result;
  3918. }
  3919. //---------------------------------------------------------------------------
  3920. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3921. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3922. {
  3923. DebugAssert(!IsLocalBrowserMode());
  3924. bool DirectCopy;
  3925. UnicodeString Target, FileMask;
  3926. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3927. if (Result)
  3928. {
  3929. if (!Move && !DirectCopy)
  3930. {
  3931. Configuration->Usage->Inc("RemoteCopyTemp");
  3932. UnicodeString RootTempDir;
  3933. UnicodeString TempDir;
  3934. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3935. TStrings * TemporaryFilesList = new TStringList();
  3936. try
  3937. {
  3938. TMakeLocalFileListParams MakeFileListParam;
  3939. MakeFileListParam.FileList = TemporaryFilesList;
  3940. MakeFileListParam.FileTimes = NULL;
  3941. MakeFileListParam.IncludeDirs = true;
  3942. MakeFileListParam.Recursive = false;
  3943. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  3944. TTerminalManager::Instance()->ActiveSession = Session;
  3945. if (TemporaryFilesList->Count > 0)
  3946. {
  3947. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3948. CopyParam.FileMask = FileMask;
  3949. DebugAssert(!FAutoOperation);
  3950. FAutoOperation = true;
  3951. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3952. }
  3953. }
  3954. __finally
  3955. {
  3956. delete TemporaryFilesList;
  3957. FAutoOperation = false;
  3958. if (!RootTempDir.IsEmpty())
  3959. {
  3960. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3961. }
  3962. }
  3963. }
  3964. else
  3965. {
  3966. RemoteDirView->SaveSelection();
  3967. RemoteDirView->SaveSelectedNames();
  3968. try
  3969. {
  3970. if (Move)
  3971. {
  3972. Configuration->Usage->Inc("RemoteMove");
  3973. Terminal->MoveFiles(FileList, Target, FileMask);
  3974. }
  3975. else
  3976. {
  3977. Configuration->Usage->Inc("RemoteCopyDirect");
  3978. DebugAssert(DirectCopy);
  3979. DebugAssert(Session == Terminal);
  3980. if (Terminal->IsCapable[fcRemoteCopy] ||
  3981. Terminal->CommandSessionOpened ||
  3982. CommandSessionFallback())
  3983. {
  3984. Terminal->CopyFiles(FileList, Target, FileMask);
  3985. }
  3986. }
  3987. }
  3988. catch(...)
  3989. {
  3990. RemoteDirView->DiscardSavedSelection();
  3991. throw;
  3992. }
  3993. RemoteDirView->RestoreSelection();
  3994. }
  3995. }
  3996. return Result;
  3997. }
  3998. //---------------------------------------------------------------------------
  3999. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4000. const UnicodeString & Path, TRemoteProperties & Properties)
  4001. {
  4002. DebugAssert(!IsLocalBrowserMode());
  4003. Properties.Valid = Properties.Valid << vpEncrypt;
  4004. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4005. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4006. }
  4007. //---------------------------------------------------------------------------
  4008. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4009. {
  4010. Side = GetSide(Side);
  4011. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4012. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4013. UnicodeString Name = LoadStr(NEW_FOLDER);
  4014. int AllowedChanges =
  4015. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4016. bool SaveSettings = false;
  4017. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4018. {
  4019. TWindowLock Lock(this);
  4020. if (!IsSideLocalBrowser(Side))
  4021. {
  4022. if (SaveSettings)
  4023. {
  4024. GUIConfiguration->NewDirectoryProperties = Properties;
  4025. }
  4026. CreateRemoteDirectory(Name, Properties);
  4027. }
  4028. else
  4029. {
  4030. DirView(Side)->CreateDirectory(Name);
  4031. }
  4032. }
  4033. }
  4034. //---------------------------------------------------------------------------
  4035. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4036. {
  4037. TWindowLock Lock(this);
  4038. DirView(Side)->ExecuteHomeDirectory();
  4039. }
  4040. //---------------------------------------------------------------------------
  4041. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4042. {
  4043. DoOpenDirectoryDialog(odBrowse, Side);
  4044. }
  4045. //---------------------------------------------------------------------------
  4046. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4047. {
  4048. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4049. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4050. bool Result = !Path.IsEmpty();
  4051. if (Result)
  4052. {
  4053. // While we might get here when the session is closed (from location profiles),
  4054. // it's not a problem as the Path setter is noop then
  4055. DirView(Side)->Path = Path;
  4056. }
  4057. return Result;
  4058. }
  4059. //---------------------------------------------------------------------------
  4060. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4061. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4062. bool & Close)
  4063. {
  4064. // terminal can be already closed (e.g. dropped connection)
  4065. if (Terminal != NULL)
  4066. {
  4067. try
  4068. {
  4069. TCalculateSizeParams Params;
  4070. Params.Stats = &Stats;
  4071. Terminal->CalculateFilesSize(FileList, Size, Params);
  4072. }
  4073. catch(...)
  4074. {
  4075. if (!Terminal->Active)
  4076. {
  4077. Close = true;
  4078. }
  4079. throw;
  4080. }
  4081. }
  4082. }
  4083. //---------------------------------------------------------------------------
  4084. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4085. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4086. bool & Close)
  4087. {
  4088. // terminal can be already closed (e.g. dropped connection)
  4089. if (Terminal != NULL)
  4090. {
  4091. Configuration->Usage->Inc(L"ChecksumCalculated");
  4092. try
  4093. {
  4094. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4095. }
  4096. catch(...)
  4097. {
  4098. if (!Terminal->Active)
  4099. {
  4100. Close = true;
  4101. }
  4102. throw;
  4103. }
  4104. }
  4105. }
  4106. //---------------------------------------------------------------------------
  4107. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4108. {
  4109. bool Result;
  4110. if (!IsSideLocalBrowser(Side))
  4111. {
  4112. TRemoteTokenList * GroupList = NULL;
  4113. TRemoteTokenList * UserList = NULL;
  4114. try
  4115. {
  4116. TRemoteProperties CurrentProperties;
  4117. if (Terminal->LoadFilesProperties(FileList))
  4118. {
  4119. RemoteDirView->Invalidate();
  4120. }
  4121. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4122. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4123. if (CapableGroupChanging || CapableOwnerChanging)
  4124. {
  4125. if (CapableGroupChanging)
  4126. {
  4127. GroupList = Terminal->Groups->Duplicate();
  4128. }
  4129. if (CapableOwnerChanging)
  4130. {
  4131. UserList = Terminal->Users->Duplicate();
  4132. }
  4133. TRemoteDirectory * Files = Terminal->Files;
  4134. int Count = Files->Count;
  4135. if (Count > 100)
  4136. {
  4137. Count = 100;
  4138. }
  4139. for (int Index = 0; Index < Count; Index++)
  4140. {
  4141. TRemoteFile * File = Files->Files[Index];
  4142. if (CapableGroupChanging)
  4143. {
  4144. GroupList->AddUnique(File->Group);
  4145. }
  4146. if (CapableOwnerChanging)
  4147. {
  4148. UserList->AddUnique(File->Owner);
  4149. }
  4150. }
  4151. // if we haven't collected tokens for all files in current directory,
  4152. // make sure we collect them at least for all selected files.
  4153. // (note that so far the files in FileList has to be from current direcotry)
  4154. if (Count < Files->Count)
  4155. {
  4156. for (int Index = 0; Index < FileList->Count; Index++)
  4157. {
  4158. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4159. if (CapableGroupChanging)
  4160. {
  4161. GroupList->AddUnique(File->Group);
  4162. }
  4163. if (CapableOwnerChanging)
  4164. {
  4165. UserList->AddUnique(File->Owner);
  4166. }
  4167. }
  4168. }
  4169. }
  4170. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4171. int Flags = 0;
  4172. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4173. if (CapableOwnerChanging) Flags |= cpOwner;
  4174. if (CapableGroupChanging) Flags |= cpGroup;
  4175. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4176. if (Terminal->IsCapable[fcCalculatingChecksum])
  4177. {
  4178. CalculateChecksumEvent = CalculateChecksum;
  4179. }
  4180. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4181. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4182. TRemoteProperties NewProperties = CurrentProperties;
  4183. Result =
  4184. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4185. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4186. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4187. CalculateSize, CalculateChecksumEvent);
  4188. if (Result)
  4189. {
  4190. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4191. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4192. }
  4193. }
  4194. __finally
  4195. {
  4196. delete GroupList;
  4197. delete UserList;
  4198. }
  4199. }
  4200. else
  4201. {
  4202. DirView(Side)->DisplayPropertiesMenu();
  4203. Result = true;
  4204. }
  4205. return Result;
  4206. }
  4207. //---------------------------------------------------------------------------
  4208. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4209. {
  4210. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4211. {
  4212. FIgnoreNextDialogChar = Key;
  4213. }
  4214. Key = 0;
  4215. }
  4216. //---------------------------------------------------------------------------
  4217. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4218. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4219. {
  4220. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4221. if (Command != NULL)
  4222. {
  4223. KeyProcessed(Key, Shift);
  4224. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4225. {
  4226. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4227. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4228. false, false, const_cast<TCustomCommandType *>(Command));
  4229. }
  4230. }
  4231. }
  4232. //---------------------------------------------------------------------------
  4233. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4234. {
  4235. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4236. {
  4237. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4238. }
  4239. if (!DirView(osCurrent)->IsEditing())
  4240. {
  4241. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4242. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4243. {
  4244. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4245. // But so far it's not a problem, as it does what we want.
  4246. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4247. if (((Action->ShortCut == KeyShortCut) ||
  4248. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4249. AllowedAction(Action, aaShortCut))
  4250. {
  4251. // Has to be called before the action as the dialog char is already in queue.
  4252. // So when the action consumes message queue, we already need to have the
  4253. // FIgnoreNextDialogChar set
  4254. KeyProcessed(Key, Shift);
  4255. // Reset reference to previous component (when menu/toolbar was clicked).
  4256. // Needed to detect that action was invoked by keyboard shortcut
  4257. // in TNonVisualDataModule::ExplorerActionsExecute
  4258. Action->ActionComponent = NULL;
  4259. Action->Execute();
  4260. return;
  4261. }
  4262. }
  4263. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4264. {
  4265. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4266. {
  4267. KeyProcessed(Key, Shift);
  4268. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4269. return;
  4270. }
  4271. }
  4272. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4273. {
  4274. KeyProcessed(Key, Shift);
  4275. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4276. }
  4277. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4278. if (IsCustomShortCut(CustomShortCut))
  4279. {
  4280. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4281. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4282. if (WinConfiguration->SharedBookmarks != NULL)
  4283. {
  4284. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4285. if ((Bookmark != NULL) &&
  4286. OpenBookmark(FCurrentSide, Bookmark))
  4287. {
  4288. KeyProcessed(Key, Shift);
  4289. }
  4290. }
  4291. }
  4292. }
  4293. TForm::KeyDown(Key, Shift);
  4294. }
  4295. //---------------------------------------------------------------------------
  4296. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4297. {
  4298. bool OpenedTerminal = false;
  4299. UnicodeString ProtocolName;
  4300. UnicodeString ProtocolNameHint;
  4301. UnicodeString SecurityProtocolName;
  4302. UnicodeString DurationHint;
  4303. // See the comment in TScpCommanderForm::UpdateControls()
  4304. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4305. if (ActiveTerminal != NULL)
  4306. {
  4307. // Active is not enough here
  4308. if (ActiveTerminal->Status == ssOpened)
  4309. {
  4310. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4311. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4312. if (FileSystemInfo.ProtocolName.IsEmpty())
  4313. {
  4314. ProtocolName = SessionInfo.ProtocolName;
  4315. }
  4316. else
  4317. {
  4318. ProtocolName = FileSystemInfo.ProtocolName;
  4319. }
  4320. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4321. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4322. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4323. OpenedTerminal = true;
  4324. }
  4325. else if (ActiveTerminal->Status > ssClosed)
  4326. {
  4327. ProtocolName = LoadStr(STATUS_CONNECTING);
  4328. }
  4329. else
  4330. {
  4331. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4332. }
  4333. }
  4334. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4335. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4336. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4337. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4338. if (ProtocolStatusPanel->Caption != ProtocolName)
  4339. {
  4340. ProtocolStatusPanel->Caption = ProtocolName;
  4341. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4342. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4343. }
  4344. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4345. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4346. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4347. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4348. SecurityStatusPanel->Enabled = SecurityEnabled;
  4349. UnicodeString SecurityProtocolHint;
  4350. if (SecurityEnabled)
  4351. {
  4352. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4353. }
  4354. else
  4355. {
  4356. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4357. }
  4358. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4359. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4360. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4361. DurationStatusPanel->Hint = DurationHint;
  4362. UpdateStatusBar();
  4363. }
  4364. //---------------------------------------------------------------------------
  4365. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4366. {
  4367. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4368. DebugAssert(SessionStatusBar != NULL);
  4369. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4370. if (!FNote.IsEmpty())
  4371. {
  4372. NoteStatusPanel->Caption = FNote;
  4373. }
  4374. else
  4375. {
  4376. UpdateStatusPanelText(NoteStatusPanel);
  4377. }
  4378. NoteStatusPanel->Hint = FNoteHints;
  4379. UnicodeString LoginTime;
  4380. // See the comment in TScpCommanderForm::UpdateControls()
  4381. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4382. // Active is not enough here
  4383. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4384. {
  4385. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4386. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4387. }
  4388. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4389. DurationStatusPanel->Caption = LoginTime;
  4390. }
  4391. //---------------------------------------------------------------------------
  4392. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4393. {
  4394. Panel->Caption = L"";
  4395. }
  4396. //---------------------------------------------------------------------------
  4397. void __fastcall TCustomScpExplorerForm::Idle()
  4398. {
  4399. if (FShowing || FStandaloneEditing)
  4400. {
  4401. FEditorManager->Check();
  4402. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4403. // that needs to know if queue view is visible (and it may be closed after queue update)
  4404. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4405. }
  4406. if (FShowing)
  4407. {
  4408. if (!NonVisualDataModule->Busy &&
  4409. // Menu is opened or drag&drop is going on
  4410. (Mouse->Capture == NULL))
  4411. {
  4412. if (FRefreshRemoteDirectory)
  4413. {
  4414. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4415. {
  4416. Terminal->RefreshDirectory();
  4417. }
  4418. FRefreshRemoteDirectory = false;
  4419. }
  4420. if (FRefreshLocalDirectory)
  4421. {
  4422. ReloadLocalDirectory();
  4423. FRefreshLocalDirectory = false;
  4424. }
  4425. if (WinConfiguration->RefreshRemotePanel)
  4426. {
  4427. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4428. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4429. {
  4430. RemoteDirView->ReloadDirectory();
  4431. }
  4432. }
  4433. }
  4434. }
  4435. if (FShowing || FStandaloneEditing)
  4436. {
  4437. if (FQueueStatusInvalidated)
  4438. {
  4439. UpdateQueueStatus(false);
  4440. }
  4441. RefreshQueueItems();
  4442. }
  4443. if (FShowing)
  4444. {
  4445. UpdateStatusBar();
  4446. }
  4447. FIgnoreNextDialogChar = 0;
  4448. }
  4449. //---------------------------------------------------------------------------
  4450. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4451. {
  4452. try
  4453. {
  4454. FUserActionTimer->Enabled = false;
  4455. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4456. {
  4457. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4458. {
  4459. FPendingQueueActionItem->ProcessUserAction();
  4460. }
  4461. }
  4462. }
  4463. __finally
  4464. {
  4465. FPendingQueueActionItem = NULL;
  4466. }
  4467. }
  4468. //---------------------------------------------------------------------------
  4469. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4470. {
  4471. if (WinConfiguration->MinimizeToTray)
  4472. {
  4473. UpdateTrayIcon();
  4474. FTrayIcon->Visible = true;
  4475. if (Visible)
  4476. {
  4477. ShowWindow(Handle, SW_HIDE);
  4478. }
  4479. }
  4480. }
  4481. //---------------------------------------------------------------------------
  4482. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4483. {
  4484. // WORKAROUND
  4485. // When restoring maximized window from minimization,
  4486. // rarely some controls do not align properly.
  4487. // Two instances seen (both for Commander):
  4488. // - When restoring, window is temporarily narrower (not maximized),
  4489. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4490. // Once maximized already, top dock shinks back, but the session PageControl,
  4491. // do not align up, leaving space between TopDock and PageControl.
  4492. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4493. for (int Index = 0; Index < ControlCount; Index++)
  4494. {
  4495. RealignControl(Controls[Index]);
  4496. }
  4497. if (FTrayIcon->Visible)
  4498. {
  4499. FTrayIcon->Visible = false;
  4500. if (Visible)
  4501. {
  4502. ShowWindow(Handle, SW_SHOW);
  4503. }
  4504. }
  4505. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4506. {
  4507. FNeedSession = false;
  4508. NonVisualDataModule->StartBusy();
  4509. try
  4510. {
  4511. NeedSession(true);
  4512. }
  4513. __finally
  4514. {
  4515. NonVisualDataModule->EndBusy();
  4516. }
  4517. }
  4518. }
  4519. //---------------------------------------------------------------------------
  4520. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4521. {
  4522. FTrayIcon->Hint = GetMainForm()->Caption;
  4523. }
  4524. //---------------------------------------------------------------------------
  4525. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4526. {
  4527. UpdateTrayIcon();
  4528. }
  4529. //---------------------------------------------------------------------------
  4530. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4531. {
  4532. // workaround
  4533. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4534. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4535. // But WM_ACTIVATEAPP message can be received even when
  4536. // the main window is minimized to the tray and internal editor window is focused
  4537. // (after another application was previously active)
  4538. if (::IsIconic(Handle))
  4539. {
  4540. if (!IsAppIconic())
  4541. {
  4542. SetAppIconic(true);
  4543. }
  4544. }
  4545. ::ApplicationRestore();
  4546. Application->BringToFront();
  4547. }
  4548. //---------------------------------------------------------------------------
  4549. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4550. {
  4551. RestoreApp();
  4552. }
  4553. //---------------------------------------------------------------------------
  4554. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4555. {
  4556. if (OpenInNewWindow())
  4557. {
  4558. ExecuteNewInstance(SessionUrl);
  4559. }
  4560. else
  4561. {
  4562. TTerminalManager::Instance()->NewSession(SessionUrl);
  4563. }
  4564. }
  4565. //---------------------------------------------------------------------------
  4566. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4567. {
  4568. NewSession();
  4569. }
  4570. //---------------------------------------------------------------------------
  4571. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4572. {
  4573. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4574. StoredSessions->NewSession(SessionName, SessionData);
  4575. // modified only, explicit
  4576. StoredSessions->Save(false, true);
  4577. // encode session name because of slashes in hierarchical sessions
  4578. return EncodeUrlString(SessionName);
  4579. }
  4580. //---------------------------------------------------------------------------
  4581. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4582. {
  4583. // current working directories become defaults here, what is not right
  4584. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4585. return SaveHiddenDuplicateSession(SessionData.get());
  4586. }
  4587. //---------------------------------------------------------------------------
  4588. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4589. {
  4590. if (OpenInNewWindow())
  4591. {
  4592. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4593. }
  4594. else
  4595. {
  4596. // current working directories become defaults here, what is not right
  4597. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4598. TTerminalManager * Manager = TTerminalManager::Instance();
  4599. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4600. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4601. // We definitelly want these
  4602. ASession->Disconnected = ManagedSession->Disconnected;
  4603. // Not sure about these two
  4604. ASession->Permanent = ManagedSession->Permanent;
  4605. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4606. Manager->ActiveSession = ASession;
  4607. }
  4608. }
  4609. //---------------------------------------------------------------------------
  4610. void __fastcall TCustomScpExplorerForm::RenameSession()
  4611. {
  4612. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4613. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4614. // When submitting the default name, do not fix it
  4615. (Name != ManagedSession->SessionData->SessionName))
  4616. {
  4617. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4618. TSessionData::ValidateName(Name);
  4619. // This is inconsistent with how color (for example) is handled.
  4620. ManagedSession->SessionData->Name = Name;
  4621. UpdateControls();
  4622. // Add/Remove distinguishing paths from sessions of the same name.
  4623. SessionListChanged();
  4624. }
  4625. }
  4626. //---------------------------------------------------------------------------
  4627. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4628. {
  4629. DebugAssert(Queue != NULL);
  4630. bool Result = Queue->IsEmpty;
  4631. if (!Result)
  4632. {
  4633. SetFocus();
  4634. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4635. }
  4636. return Result;
  4637. }
  4638. //---------------------------------------------------------------------------
  4639. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4640. {
  4641. return CanCloseQueue(FQueue);
  4642. }
  4643. //---------------------------------------------------------------------------
  4644. void __fastcall TCustomScpExplorerForm::CloseSession()
  4645. {
  4646. if (CanCloseQueue())
  4647. {
  4648. TTerminalManager::Instance()->FreeActiveTerminal();
  4649. }
  4650. }
  4651. //---------------------------------------------------------------------------
  4652. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4653. {
  4654. if (CanCloseQueue())
  4655. {
  4656. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4657. }
  4658. }
  4659. //---------------------------------------------------------------------------
  4660. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4661. {
  4662. DetachTerminal(Terminal);
  4663. RemoteDirView->Terminal = Terminal;
  4664. UpdateRemotePathComboBox(false);
  4665. InitStatusBar();
  4666. UpdateControls();
  4667. }
  4668. //---------------------------------------------------------------------------
  4669. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4670. {
  4671. InitStatusBar();
  4672. }
  4673. //---------------------------------------------------------------------------
  4674. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4675. {
  4676. if (DebugAlwaysTrue(!Terminal->Active))
  4677. {
  4678. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4679. }
  4680. }
  4681. //---------------------------------------------------------------------------
  4682. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4683. {
  4684. if (OpenInNewWindow())
  4685. {
  4686. // encode session name because of slashes in hierarchical sessions
  4687. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4688. }
  4689. else
  4690. {
  4691. TTerminalManager * Manager = TTerminalManager::Instance();
  4692. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4693. Manager->ActiveSession = Terminal;
  4694. }
  4695. }
  4696. //---------------------------------------------------------------------------
  4697. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4698. {
  4699. TTerminalManager * Manager = TTerminalManager::Instance();
  4700. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4701. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4702. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4703. // FirstSession can be null, if some of the
  4704. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4705. {
  4706. FirstSession->Disconnected = true;
  4707. FirstSession->DisconnectedTemporarily = true;
  4708. }
  4709. Manager->ActiveSession = FirstSession;
  4710. }
  4711. //---------------------------------------------------------------------------
  4712. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4713. {
  4714. if (OpenInNewWindow())
  4715. {
  4716. ExecuteNewInstance(Name);
  4717. }
  4718. else
  4719. {
  4720. DoOpenFolderOrWorkspace(Name, true);
  4721. }
  4722. }
  4723. //---------------------------------------------------------------------------
  4724. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4725. bool &CanClose)
  4726. {
  4727. // See the comment in CloseApp()
  4728. if (NonVisualDataModule->Busy)
  4729. {
  4730. CanClose = false;
  4731. }
  4732. else
  4733. {
  4734. TTerminalManager * Manager = TTerminalManager::Instance();
  4735. int ActiveSessions = 0;
  4736. for (int Index = 0; Index < Manager->Count; Index++)
  4737. {
  4738. if (Manager->Sessions[Index]->Active)
  4739. {
  4740. ActiveSessions++;
  4741. }
  4742. }
  4743. // Confirm, when there's at least one active remote session or
  4744. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4745. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4746. WinConfiguration->ConfirmClosingSession)
  4747. {
  4748. unsigned int Result;
  4749. TMessageParams Params(mpNeverAskAgainCheck);
  4750. UnicodeString Message;
  4751. int Answers = qaOK | qaCancel;
  4752. // The current session is the only active session
  4753. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4754. {
  4755. if (!WinConfiguration->AutoSaveWorkspace)
  4756. {
  4757. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4758. }
  4759. else
  4760. {
  4761. Message = LoadStr(CLOSE_SESSION);
  4762. }
  4763. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4764. }
  4765. // Multiple active sessions or one active session, but it's not the current one
  4766. else if (ActiveSessions > 0)
  4767. {
  4768. if (!WinConfiguration->AutoSaveWorkspace)
  4769. {
  4770. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4771. }
  4772. else
  4773. {
  4774. Message = LoadStr(CLOSE_SESSIONS);
  4775. }
  4776. }
  4777. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4778. else
  4779. {
  4780. DebugAlwaysTrue(Manager->Count > 1);
  4781. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4782. Message = LoadStr(CLOSE_WORKSPACE);
  4783. }
  4784. UnicodeString Note;
  4785. if (WinConfiguration->AutoSaveWorkspace)
  4786. {
  4787. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4788. }
  4789. else
  4790. {
  4791. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4792. Answers = qaYes | qaNo | qaCancel;
  4793. }
  4794. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4795. SetFocus();
  4796. Result = MessageDialog(Message, qtConfirmation,
  4797. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4798. if (Result == qaNeverAskAgain)
  4799. {
  4800. WinConfiguration->ConfirmClosingSession = false;
  4801. }
  4802. if (Result == qaNo)
  4803. {
  4804. CanClose = SaveWorkspace(true);
  4805. // note that the workspace will be saved redundatly again from FormClose
  4806. }
  4807. else
  4808. {
  4809. CanClose =
  4810. (Result == qaOK) ||
  4811. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4812. (Result == qaNeverAskAgain);
  4813. }
  4814. }
  4815. if (CanClose && HasActiveTerminal())
  4816. {
  4817. CanClose = CanCloseQueue();
  4818. }
  4819. }
  4820. if (CanClose)
  4821. {
  4822. CanClose =
  4823. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4824. FEditorManager->CloseExternalFilesWithoutProcess();
  4825. if (CanClose)
  4826. {
  4827. while (CanClose && (FLocalEditors->Count > 0))
  4828. {
  4829. int PrevCount = FLocalEditors->Count;
  4830. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4831. CanClose = (FLocalEditors->Count < PrevCount);
  4832. }
  4833. if (CanClose)
  4834. {
  4835. CanClose = FEditorManager->Empty(true);
  4836. if (!CanClose)
  4837. {
  4838. SetFocus();
  4839. CanClose =
  4840. (MessageDialog(
  4841. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4842. }
  4843. }
  4844. }
  4845. }
  4846. }
  4847. //---------------------------------------------------------------------------
  4848. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4849. {
  4850. TForm * Form = dynamic_cast<TForm *>(Sender);
  4851. DebugAssert(Form != NULL);
  4852. Form->Close();
  4853. }
  4854. //---------------------------------------------------------------------------
  4855. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4856. {
  4857. TForm * Form = dynamic_cast<TForm *>(Sender);
  4858. delete Form;
  4859. }
  4860. //---------------------------------------------------------------------------
  4861. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4862. {
  4863. while (FLocalEditors->Count > 0)
  4864. {
  4865. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4866. }
  4867. }
  4868. //---------------------------------------------------------------------------
  4869. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4870. TObject *Sender)
  4871. {
  4872. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4873. try
  4874. {
  4875. SetProperties(osRemote, FileList);
  4876. }
  4877. __finally
  4878. {
  4879. delete FileList;
  4880. }
  4881. }
  4882. //---------------------------------------------------------------------------
  4883. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4884. {
  4885. if (value)
  4886. {
  4887. if (Component == fcCustomCommandsBand)
  4888. {
  4889. UpdateCustomCommandsToolbar();
  4890. }
  4891. if (Component == fcQueueFileList)
  4892. {
  4893. DebugAssert(!QueueFileListSplitter->Visible);
  4894. AdjustQueueLayout();
  4895. UpdateQueueFileList();
  4896. }
  4897. }
  4898. }
  4899. //---------------------------------------------------------------------------
  4900. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4901. {
  4902. int MinHeightLimit =
  4903. GetStaticQueuePanelComponentsHeight() +
  4904. QueueFileListSplitter->Height +
  4905. QueueFileList->Height +
  4906. GetMinQueueViewHeight();
  4907. if (QueuePanel->ClientHeight < MinHeightLimit)
  4908. {
  4909. QueuePanel->ClientHeight = MinHeightLimit;
  4910. }
  4911. }
  4912. //---------------------------------------------------------------------------
  4913. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4914. {
  4915. TControl * Control = GetComponent(Component);
  4916. DebugAssert(Control);
  4917. bool Changed = (Control->Visible != value);
  4918. if (Changed)
  4919. {
  4920. ComponentShowing(Component, value);
  4921. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4922. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4923. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4924. if (value)
  4925. {
  4926. int RemainingHeight = Control->Parent->ClientHeight;
  4927. int RemainingWidth = Control->Parent->ClientWidth;
  4928. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4929. {
  4930. TControl * ChildControl = Control->Parent->Controls[i];
  4931. if (ChildControl->Visible)
  4932. {
  4933. switch (ChildControl->Align)
  4934. {
  4935. case alTop:
  4936. case alBottom:
  4937. RemainingHeight -= ChildControl->Height;
  4938. break;
  4939. case alLeft:
  4940. case alRight:
  4941. RemainingWidth -= ChildControl->Width;
  4942. break;
  4943. }
  4944. }
  4945. }
  4946. int Reserve = ScaleByTextHeight(this, 32);
  4947. // queue in explorer, trees in commander
  4948. if (Control->Height > RemainingHeight - Reserve)
  4949. {
  4950. Control->Height = RemainingHeight / 2;
  4951. }
  4952. if (Control->Width > RemainingWidth - Reserve)
  4953. {
  4954. Control->Width = RemainingWidth / 2;
  4955. }
  4956. }
  4957. Control->Visible = value;
  4958. if (WasFocused && Visible)
  4959. {
  4960. DirView(osCurrent)->SetFocus();
  4961. }
  4962. FixControlsPlacement();
  4963. }
  4964. }
  4965. //---------------------------------------------------------------------------
  4966. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4967. {
  4968. TControl * Control = GetComponent(Component);
  4969. if (Control == NULL)
  4970. {
  4971. return false;
  4972. }
  4973. else
  4974. {
  4975. return Control->Visible;
  4976. }
  4977. }
  4978. //---------------------------------------------------------------------------
  4979. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4980. {
  4981. bool Result;
  4982. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4983. {
  4984. Result = !IsUWP();
  4985. }
  4986. else
  4987. {
  4988. Result = true;
  4989. }
  4990. return Result;
  4991. }
  4992. //---------------------------------------------------------------------------
  4993. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  4994. {
  4995. if (DirView->ItemFocused != NULL)
  4996. {
  4997. DirView->ItemFocused->MakeVisible(false);
  4998. }
  4999. }
  5000. //---------------------------------------------------------------------------
  5001. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5002. {
  5003. MakeFocusedItemVisible(RemoteDirView);
  5004. QueueSplitter->Visible = QueuePanel->Visible;
  5005. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5006. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5007. TControl * QueueControlsOrder[] =
  5008. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5009. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5010. }
  5011. //---------------------------------------------------------------------------
  5012. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5013. {
  5014. switch (Component) {
  5015. case fcStatusBar: return RemoteStatusBar;
  5016. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5017. case fcQueueView: return QueuePanel;
  5018. case fcQueueToolbar: return QueueDock;
  5019. case fcRemoteTree: return RemoteDrivePanel;
  5020. case fcSessionsTabs: return SessionsPageControl;
  5021. case fcQueueFileList: return QueueFileList;
  5022. default: return NULL;
  5023. }
  5024. }
  5025. //---------------------------------------------------------------------------
  5026. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5027. TObject *Sender, TListColumn *Column, TPoint &Point)
  5028. {
  5029. DebugAssert(NonVisualDataModule && Column && Sender);
  5030. NonVisualDataModule->ListColumn = Column;
  5031. TPopupMenu * DirViewColumnMenu;
  5032. if (Sender == DirView(osOther))
  5033. {
  5034. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5035. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5036. (Column->Index == uvName);
  5037. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5038. (Column->Index == uvSize);
  5039. }
  5040. else
  5041. {
  5042. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5043. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5044. (Column->Index == dvName);
  5045. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5046. (Column->Index == dvSize);
  5047. }
  5048. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5049. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5050. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5051. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5052. }
  5053. //---------------------------------------------------------------------------
  5054. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5055. TObject *Sender, TListItem *Item, bool &AllowExec)
  5056. {
  5057. DoDirViewExecFile(Sender, Item, AllowExec);
  5058. }
  5059. //---------------------------------------------------------------------------
  5060. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5061. TListItem * Item, bool & AllowExec)
  5062. {
  5063. DebugAssert(Sender && Item && Configuration);
  5064. DebugAssert(AllowExec);
  5065. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5066. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5067. bool Remote = !IsSideLocalBrowser(Side);
  5068. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  5069. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  5070. // on files only (not directories)
  5071. // and only when symlinks are resolved (apply to remote panel only)
  5072. if (!ADirView->ItemIsDirectory(Item) &&
  5073. (ResolvedSymlinks || FForceExecution))
  5074. {
  5075. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5076. !FForceExecution && ResolvedSymlinks)
  5077. {
  5078. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5079. {
  5080. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5081. UnlockWindow();
  5082. try
  5083. {
  5084. if (IsLocalBrowserMode())
  5085. {
  5086. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5087. }
  5088. else
  5089. {
  5090. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5091. }
  5092. }
  5093. __finally
  5094. {
  5095. LockWindow();
  5096. }
  5097. AllowExec = false;
  5098. }
  5099. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5100. {
  5101. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5102. {
  5103. ExecuteFile(Side, efDefaultEditor);
  5104. AllowExec = false;
  5105. }
  5106. }
  5107. else
  5108. {
  5109. DebugFail();
  5110. }
  5111. }
  5112. // if we have not done anything special, fall back to default behavior
  5113. if (AllowExec)
  5114. {
  5115. if (Remote && !WinConfiguration->DisableOpenEdit)
  5116. {
  5117. ExecuteFile(osRemote, efShell);
  5118. AllowExec = false;
  5119. }
  5120. }
  5121. }
  5122. }
  5123. //---------------------------------------------------------------------------
  5124. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5125. {
  5126. return ((Side == osRemote) || (Side == osCurrent));
  5127. }
  5128. //---------------------------------------------------------------------------
  5129. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5130. {
  5131. DebugFail();
  5132. }
  5133. //---------------------------------------------------------------------------
  5134. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5135. {
  5136. DebugFail();
  5137. }
  5138. //---------------------------------------------------------------------------
  5139. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5140. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5141. {
  5142. TSynchronizeParamType Params;
  5143. Params.LocalDirectory = LocalDirectory;
  5144. Params.RemoteDirectory = RemoteDirectory;
  5145. int UnusedParams =
  5146. (GUIConfiguration->SynchronizeParams &
  5147. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5148. TTerminal::spCaseSensitive));
  5149. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5150. Params.Options = GUIConfiguration->SynchronizeOptions;
  5151. bool SaveSettings = false;
  5152. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5153. &DoSynchronizeTooManyDirectories);
  5154. DebugAssert(FSynchronizeController == NULL);
  5155. FSynchronizeController = &Controller;
  5156. bool Result;
  5157. try
  5158. {
  5159. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5160. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5161. int Options =
  5162. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5163. DebugAssert(FOnFeedSynchronizeError == NULL);
  5164. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5165. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5166. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  5167. if (Result)
  5168. {
  5169. if (SaveSettings)
  5170. {
  5171. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5172. GUIConfiguration->SynchronizeOptions = Params.Options;
  5173. }
  5174. else
  5175. {
  5176. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5177. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5178. FLAGSET(Params.Options, soSynchronize))
  5179. {
  5180. GUIConfiguration->SynchronizeOptions =
  5181. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5182. soSynchronize;
  5183. }
  5184. }
  5185. LocalDirectory = Params.LocalDirectory;
  5186. RemoteDirectory = Params.RemoteDirectory;
  5187. }
  5188. }
  5189. __finally
  5190. {
  5191. FSynchronizeController = NULL;
  5192. DebugAssert(FOnFeedSynchronizeError == NULL);
  5193. FOnFeedSynchronizeError = NULL;
  5194. }
  5195. return Result;
  5196. }
  5197. //---------------------------------------------------------------------------
  5198. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5199. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5200. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5201. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5202. TSynchronizeOptions * Options, bool Full)
  5203. {
  5204. // Keep-up-to-date mode
  5205. if (Terminal->Status == ssOpened)
  5206. {
  5207. try
  5208. {
  5209. int PParams = Params.Params;
  5210. if (!Full)
  5211. {
  5212. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5213. if (FLAGSET(Params.Options, soRecurse))
  5214. {
  5215. PParams |= TTerminal::spSubDirs;
  5216. }
  5217. }
  5218. else
  5219. {
  5220. // if keepuptodate is non-recursive,
  5221. // full sync before has to be non-recursive as well
  5222. if (FLAGCLEAR(Params.Options, soRecurse))
  5223. {
  5224. PParams |= TTerminal::spNoRecurse;
  5225. }
  5226. }
  5227. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5228. PParams, Checklist, Options);
  5229. }
  5230. catch (Exception & E)
  5231. {
  5232. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5233. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5234. {
  5235. // noop, already logged in MoreMessageDialog
  5236. }
  5237. else
  5238. {
  5239. ShowExtendedExceptionEx(Terminal, &E);
  5240. throw;
  5241. }
  5242. }
  5243. }
  5244. }
  5245. //---------------------------------------------------------------------------
  5246. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5247. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5248. const UnicodeString ErrorStr)
  5249. {
  5250. if (!Directory.IsEmpty())
  5251. {
  5252. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5253. }
  5254. else
  5255. {
  5256. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5257. }
  5258. }
  5259. //---------------------------------------------------------------------------
  5260. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5261. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5262. {
  5263. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5264. {
  5265. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5266. }
  5267. else
  5268. {
  5269. TMessageParams Params(mpNeverAskAgainCheck);
  5270. unsigned int Result = MessageDialog(
  5271. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5272. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5273. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5274. {
  5275. MaxDirectories *= 2;
  5276. if (Result == qaNeverAskAgain)
  5277. {
  5278. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5279. }
  5280. }
  5281. else
  5282. {
  5283. Abort();
  5284. }
  5285. }
  5286. }
  5287. //---------------------------------------------------------------------------
  5288. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5289. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5290. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5291. TSynchronizeOptions * Options)
  5292. {
  5293. // Keep-up-to-date mode
  5294. DebugAssert(!FAutoOperation);
  5295. void * BatchStorage;
  5296. BatchStart(BatchStorage);
  5297. FAutoOperation = true;
  5298. bool AnyOperation = false;
  5299. TDateTime StartTime = Now();
  5300. TSynchronizeChecklist * AChecklist = NULL;
  5301. try
  5302. {
  5303. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5304. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5305. {
  5306. FSynchronizeProgressForm->Start();
  5307. }
  5308. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5309. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5310. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5311. Options);
  5312. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5313. SAFE_DESTROY(FSynchronizeProgressForm);
  5314. AnyOperation = (AChecklist->CheckedCount > 0);
  5315. if (AnyOperation)
  5316. {
  5317. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5318. CreateProgressForm(&SynchronizeProgress);
  5319. try
  5320. {
  5321. Terminal->SynchronizeApply(
  5322. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5323. SynchronizeProcessedItem, NULL, NULL, NULL);
  5324. }
  5325. __finally
  5326. {
  5327. DestroyProgressForm();
  5328. }
  5329. }
  5330. }
  5331. __finally
  5332. {
  5333. if (Checklist == NULL)
  5334. {
  5335. delete AChecklist;
  5336. }
  5337. else
  5338. {
  5339. *Checklist = AChecklist;
  5340. }
  5341. FAutoOperation = false;
  5342. SAFE_DESTROY(FSynchronizeProgressForm);
  5343. BatchEnd(BatchStorage);
  5344. ReloadLocalDirectory();
  5345. if (AnyOperation)
  5346. {
  5347. OperationComplete(StartTime);
  5348. }
  5349. }
  5350. }
  5351. //---------------------------------------------------------------------------
  5352. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5353. {
  5354. // can be called from command line
  5355. return Visible &&
  5356. ((DirView(osRemote)->SelCount > 0) ||
  5357. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5358. }
  5359. //---------------------------------------------------------------------------
  5360. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5361. {
  5362. LogSynchronizeEvent(Terminal, Message);
  5363. }
  5364. //---------------------------------------------------------------------------
  5365. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5366. int Params, TSynchronizeOptions & Options)
  5367. {
  5368. DebugAssert(!IsLocalBrowserMode());
  5369. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5370. {
  5371. Options.Filter = new TStringList();
  5372. Options.Filter->CaseSensitive = false;
  5373. Options.Filter->Duplicates = Types::dupAccept;
  5374. if (DirView(osRemote)->SelCount > 0)
  5375. {
  5376. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5377. }
  5378. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5379. {
  5380. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5381. }
  5382. Options.Filter->Sort();
  5383. }
  5384. }
  5385. //---------------------------------------------------------------------------
  5386. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5387. {
  5388. TCopyParamType Defaults;
  5389. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5390. ConfigStorage->AccessMode = smRead;
  5391. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5392. {
  5393. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5394. }
  5395. ConfigStorage.reset(NULL);
  5396. std::unique_ptr<TStringList> Options(new TStringList());
  5397. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5398. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5399. UnicodeString Result;
  5400. if (Options->Count > 0)
  5401. {
  5402. Result =
  5403. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5404. }
  5405. return Result;
  5406. }
  5407. //---------------------------------------------------------------------------
  5408. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5409. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5410. {
  5411. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5412. UnicodeString AdditionalParams =
  5413. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5414. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5415. SerializeCopyParamForCommandLine(CopyParams),
  5416. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5417. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5418. ExecuteNewInstance(SessionName, AdditionalParams);
  5419. }
  5420. //---------------------------------------------------------------------------
  5421. struct TSynchronizeParams
  5422. {
  5423. UnicodeString LocalDirectory;
  5424. UnicodeString RemoteDirectory;
  5425. TSynchronizeMode Mode;
  5426. TSynchronizeChecklist * Checklist;
  5427. int Params;
  5428. TCopyParamType * CopyParam;
  5429. TDateTime CollectElapsed;
  5430. TDateTime * StartTime;
  5431. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5432. };
  5433. //---------------------------------------------------------------------------
  5434. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5435. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5436. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5437. {
  5438. DebugAssert(!FAutoOperation);
  5439. void * BatchStorage;
  5440. BatchStart(BatchStorage);
  5441. FAutoOperation = true;
  5442. TFileOperationStatistics Statistics;
  5443. TDateTime Start = Now();
  5444. try
  5445. {
  5446. Params.OnProcessedItem = OnProcessedItem;
  5447. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5448. CreateProgressForm(&SynchronizeProgress);
  5449. Terminal->SynchronizeApply(
  5450. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5451. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5452. &Statistics);
  5453. }
  5454. __finally
  5455. {
  5456. Params.OnProcessedItem = NULL;
  5457. FAutoOperation = false;
  5458. DestroyProgressForm();
  5459. BatchEnd(BatchStorage);
  5460. ReloadLocalDirectory();
  5461. }
  5462. if (WinConfiguration->SynchronizeSummary)
  5463. {
  5464. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5465. // The statistics should be 0 anyway in this case
  5466. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5467. {
  5468. Message += L"\n";
  5469. if (Statistics.FilesUploaded > 0)
  5470. {
  5471. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5472. }
  5473. if (Statistics.FilesDownloaded > 0)
  5474. {
  5475. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5476. }
  5477. if (Statistics.FilesDeletedLocal > 0)
  5478. {
  5479. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5480. }
  5481. if (Statistics.FilesDeletedRemote > 0)
  5482. {
  5483. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5484. }
  5485. }
  5486. TDateTime Elapsed = (Now() - Start);
  5487. Message +=
  5488. L"\n" +
  5489. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5490. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5491. TMessageParams Params(mpNeverAskAgainCheck);
  5492. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5493. if (Result == qaNeverAskAgain)
  5494. {
  5495. WinConfiguration->SynchronizeSummary = false;
  5496. }
  5497. }
  5498. }
  5499. //---------------------------------------------------------------------------
  5500. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5501. {
  5502. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5503. {
  5504. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5505. }
  5506. // Not set in keep-up-to-date mode - Synchronize() method
  5507. if (Token != NULL)
  5508. {
  5509. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5510. if (Params.OnProcessedItem != NULL)
  5511. {
  5512. Params.OnProcessedItem(NULL, ChecklistItem);
  5513. }
  5514. }
  5515. }
  5516. //---------------------------------------------------------------------------
  5517. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5518. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5519. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5520. {
  5521. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5522. *Params.StartTime = Now();
  5523. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5524. }
  5525. //---------------------------------------------------------------------------
  5526. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5527. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5528. {
  5529. // terminal can be already closed (e.g. dropped connection)
  5530. if (Terminal != NULL)
  5531. {
  5532. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5533. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5534. }
  5535. }
  5536. //---------------------------------------------------------------------------
  5537. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5538. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5539. {
  5540. DebugAssert(!IsLocalBrowserMode());
  5541. TAutoBatch AutoBatch(this);
  5542. TAutoFlag AutoOperationFlag(FAutoOperation);
  5543. if (Side == osRemote)
  5544. {
  5545. std::unique_ptr<TStrings> FileList(new TStringList());
  5546. FileList->AddObject(FileName, RemoteFile);
  5547. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5548. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5549. RemoteDirView->SaveSelection();
  5550. RemoteDirView->SaveSelectedNames();
  5551. try
  5552. {
  5553. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5554. }
  5555. catch(...)
  5556. {
  5557. RemoteDirView->DiscardSavedSelection();
  5558. throw;
  5559. }
  5560. RemoteDirView->RestoreSelection();
  5561. }
  5562. else if (DebugAlwaysTrue(Side == osLocal))
  5563. {
  5564. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5565. {
  5566. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5567. }
  5568. UnicodeString Directory = ExtractFileDir(FileName);
  5569. ReloadLocalDirectory(Directory);
  5570. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5571. if (!SamePaths(Directory, NewDirectory))
  5572. {
  5573. ReloadLocalDirectory(NewDirectory);
  5574. }
  5575. }
  5576. }
  5577. //---------------------------------------------------------------------------
  5578. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5579. {
  5580. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5581. if (Side == osLocal)
  5582. {
  5583. if (Action == TSynchronizeChecklist::saDownloadNew)
  5584. {
  5585. OpenFolderInExplorer(LocalPath);
  5586. }
  5587. else
  5588. {
  5589. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5590. }
  5591. }
  5592. else if (DebugAlwaysTrue(Side == osRemote))
  5593. {
  5594. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5595. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5596. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5597. if (!LocalPath.IsEmpty())
  5598. {
  5599. SessionData->LocalDirectory = LocalPath;
  5600. }
  5601. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5602. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5603. }
  5604. }
  5605. //---------------------------------------------------------------------------
  5606. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5607. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5608. const TCopyParamType * CopyParams)
  5609. {
  5610. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5611. UnicodeString AdditionalParams =
  5612. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5613. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5614. SerializeCopyParamForCommandLine(CopyParams),
  5615. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5616. LocalDirectory, RemoteDirectory, Mode, Params));
  5617. ExecuteNewInstance(SessionName, AdditionalParams);
  5618. }
  5619. //---------------------------------------------------------------------------
  5620. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5621. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5622. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5623. {
  5624. int Result;
  5625. bool SaveSettings = false;
  5626. int Options =
  5627. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5628. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5629. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5630. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5631. bool Continue =
  5632. (UseDefaults == 0) ||
  5633. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5634. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5635. if (Continue)
  5636. {
  5637. Configuration->Usage->Inc(L"Synchronizations");
  5638. UpdateCopyParamCounters(CopyParam);
  5639. TSynchronizeOptions SynchronizeOptions;
  5640. GetSynchronizeOptions(Params, SynchronizeOptions);
  5641. if (SaveSettings)
  5642. {
  5643. GUIConfiguration->SynchronizeParams = Params;
  5644. }
  5645. else
  5646. {
  5647. SaveMode = false;
  5648. }
  5649. TDateTime StartTime = Now();
  5650. TSynchronizeChecklist * Checklist = NULL;
  5651. try
  5652. {
  5653. DebugAssert(!FAutoOperation);
  5654. FAutoOperation = true;
  5655. try
  5656. {
  5657. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5658. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5659. int Files = -1;
  5660. if (DataList->Count >= 1)
  5661. {
  5662. Files = StrToIntDef(DataList->Strings[0], Files);
  5663. }
  5664. else
  5665. {
  5666. DataList->Add(UnicodeString());
  5667. }
  5668. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5669. FSynchronizeProgressForm->Start();
  5670. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5671. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5672. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5673. &SynchronizeOptions);
  5674. if (Terminal->SessionData->CacheDirectories)
  5675. {
  5676. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5677. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5678. }
  5679. }
  5680. __finally
  5681. {
  5682. FAutoOperation = false;
  5683. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5684. SAFE_DESTROY(FSynchronizeProgressForm);
  5685. }
  5686. TSynchronizeParams SynchronizeParams;
  5687. SynchronizeParams.LocalDirectory = LocalDirectory;
  5688. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5689. SynchronizeParams.Mode = Mode;
  5690. SynchronizeParams.CopyParam = &CopyParam;
  5691. SynchronizeParams.Params = Params;
  5692. SynchronizeParams.Checklist = Checklist;
  5693. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5694. SynchronizeParams.StartTime = &StartTime;
  5695. SynchronizeParams.OnProcessedItem = NULL;
  5696. Result = Checklist->Count;
  5697. if (Checklist->Count > 0)
  5698. {
  5699. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5700. {
  5701. if (!DoSynchronizeChecklistDialog(
  5702. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5703. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5704. {
  5705. Result = -1;
  5706. }
  5707. }
  5708. else
  5709. {
  5710. FullSynchronize(SynchronizeParams, NULL, NULL);
  5711. }
  5712. }
  5713. else
  5714. {
  5715. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5716. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5717. }
  5718. }
  5719. __finally
  5720. {
  5721. delete Checklist;
  5722. }
  5723. OperationComplete(StartTime);
  5724. }
  5725. else
  5726. {
  5727. Result = -1;
  5728. }
  5729. return Result;
  5730. }
  5731. //---------------------------------------------------------------------------
  5732. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5733. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5734. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5735. {
  5736. if (Collect)
  5737. {
  5738. DebugAssert(FSynchronizeProgressForm != NULL);
  5739. if (!FSynchronizeProgressForm->Started)
  5740. {
  5741. FSynchronizeProgressForm->Start();
  5742. }
  5743. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5744. SetTaskbarListProgressValue(CompareProgress);
  5745. }
  5746. else
  5747. {
  5748. DebugAssert(FSynchronizeProgressForm == NULL);
  5749. DebugAssert(FProgressForm != NULL);
  5750. }
  5751. }
  5752. //---------------------------------------------------------------------------
  5753. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5754. {
  5755. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5756. TRemoteFile * File = NULL;
  5757. Terminal->ReadFile(FullFileName, File);
  5758. if (File != NULL)
  5759. {
  5760. std::unique_ptr<TRemoteFile> FileOwner(File);
  5761. TAutoFlag Flag(FStandaloneEditing);
  5762. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5763. Application->ShowMainForm = false;
  5764. Application->Run();
  5765. }
  5766. }
  5767. //---------------------------------------------------------------------------
  5768. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5769. {
  5770. DebugFail();
  5771. }
  5772. //---------------------------------------------------------------------------
  5773. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5774. {
  5775. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5776. SessionData->Assign(ManagedSession->SessionData);
  5777. UpdateSessionData(SessionData.get());
  5778. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5779. if (IsActiveTerminal(ManagedSession))
  5780. {
  5781. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5782. }
  5783. if (!ManagedSession->SessionData->HasSessionName())
  5784. {
  5785. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5786. // once IsWorkspace is cleared
  5787. SessionData->Name = UnicodeString();
  5788. }
  5789. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5790. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5791. SessionData->IsWorkspace = false;
  5792. return SessionData.release();
  5793. }
  5794. //---------------------------------------------------------------------------
  5795. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5796. {
  5797. TSessionData * SessionData = CloneCurrentSessionData();
  5798. try
  5799. {
  5800. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5801. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5802. }
  5803. __finally
  5804. {
  5805. delete SessionData;
  5806. }
  5807. }
  5808. //---------------------------------------------------------------------------
  5809. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5810. {
  5811. TTerminalManager * Manager = TTerminalManager::Instance();
  5812. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5813. if (DebugAlwaysTrue(ManagedSession != NULL))
  5814. {
  5815. // Update ManagedSession->StateData
  5816. UpdateSession(ManagedSession);
  5817. }
  5818. Manager->SaveWorkspace(DataList.get());
  5819. return DataList.release();
  5820. }
  5821. //---------------------------------------------------------------------------
  5822. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5823. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5824. {
  5825. WinConfiguration->LastWorkspace = Name;
  5826. if (!SavePasswords)
  5827. {
  5828. for (int Index = 0; Index < DataList->Count; Index++)
  5829. {
  5830. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5831. if (SessionData->Link.IsEmpty())
  5832. {
  5833. SessionData->ClearSessionPasswords();
  5834. }
  5835. }
  5836. }
  5837. StoredSessions->NewWorkspace(Name, DataList);
  5838. // modified only
  5839. StoredSessions->Save(false, Explicit);
  5840. }
  5841. //---------------------------------------------------------------------------
  5842. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5843. {
  5844. return
  5845. DefaultStr(WinConfiguration->LastWorkspace,
  5846. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5847. }
  5848. //---------------------------------------------------------------------------
  5849. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5850. {
  5851. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5852. bool AnyNonStoredSessionWithPassword = false;
  5853. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5854. bool AllNonStoredSessionsAnonymous = true;
  5855. TTerminalManager * Manager = TTerminalManager::Instance();
  5856. for (int Index = 0; Index < DataList->Count; Index++)
  5857. {
  5858. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5859. if (SessionData->Link.IsEmpty())
  5860. {
  5861. if (SessionData->HasAnySessionPassword())
  5862. {
  5863. AnyNonStoredSessionWithPassword = true;
  5864. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5865. {
  5866. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5867. }
  5868. }
  5869. if (!SameText(SessionData->UserName, AnonymousUserName))
  5870. {
  5871. AllNonStoredSessionsAnonymous = false;
  5872. }
  5873. }
  5874. }
  5875. bool SavePasswords;
  5876. bool * PSavePasswords;
  5877. bool NotRecommendedSavingPasswords =
  5878. !CustomWinConfiguration->UseMasterPassword &&
  5879. !AllNonStoredSessionsAnonymous;
  5880. if (Configuration->DisablePasswordStoring ||
  5881. !AnyNonStoredSessionWithPassword)
  5882. {
  5883. PSavePasswords = NULL;
  5884. SavePasswords = false;
  5885. }
  5886. else
  5887. {
  5888. PSavePasswords = &SavePasswords;
  5889. SavePasswords =
  5890. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5891. !NotRecommendedSavingPasswords;
  5892. }
  5893. UnicodeString Name = WorkspaceName();
  5894. bool CreateShortcut = false;
  5895. bool Result =
  5896. DoSaveWorkspaceDialog(
  5897. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5898. EnableAutoSave);
  5899. if (Result)
  5900. {
  5901. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5902. if (CreateShortcut)
  5903. {
  5904. TOperationVisualizer Visualizer;
  5905. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5906. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5907. }
  5908. if (EnableAutoSave)
  5909. {
  5910. WinConfiguration->AutoSaveWorkspace = true;
  5911. WinConfiguration->AutoWorkspace = Name;
  5912. if (PSavePasswords != NULL)
  5913. {
  5914. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5915. }
  5916. }
  5917. }
  5918. return Result;
  5919. }
  5920. //---------------------------------------------------------------------------
  5921. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  5922. {
  5923. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  5924. DebugAssert(Session == ManagedSession);
  5925. if (!IsLocalBrowserMode())
  5926. {
  5927. SAFE_DESTROY(Session->RemoteExplorerState);
  5928. if (WinConfiguration->PreservePanelState)
  5929. {
  5930. Session->RemoteExplorerState = RemoteDirView->SaveState();
  5931. }
  5932. }
  5933. UpdateSessionData(Session->StateData);
  5934. }
  5935. //---------------------------------------------------------------------------
  5936. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5937. {
  5938. // Keep in sync with TSessionData::CopyStateData
  5939. DebugAssert(Data != NULL);
  5940. // This is inconsistent with how color (for example) is handled.
  5941. Data->Name = ManagedSession->SessionData->Name;
  5942. if (!IsLocalBrowserMode())
  5943. {
  5944. // cannot use RemoteDirView->Path, because it is empty if connection
  5945. // was already closed
  5946. // also only peek, we may not be connected at all atm
  5947. // so make sure we do not try retrieving current directory from the server
  5948. // (particularly with FTP)
  5949. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5950. if (!ACurrentDirectory.IsEmpty())
  5951. {
  5952. Data->RemoteDirectory = ACurrentDirectory;
  5953. }
  5954. }
  5955. Data->Color = SessionColor;
  5956. }
  5957. //---------------------------------------------------------------------------
  5958. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5959. {
  5960. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5961. DebugAssert(Toolbar != NULL);
  5962. for (int i = 0; i < Toolbar->Items->Count; i++)
  5963. {
  5964. TTBXCustomDropDownItem * DropDownItem;
  5965. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5966. if (DropDownItem != NULL)
  5967. {
  5968. ToolbarItemResize(DropDownItem,
  5969. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5970. Toolbar->NonClientWidth);
  5971. break;
  5972. }
  5973. }
  5974. }
  5975. //---------------------------------------------------------------------------
  5976. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5977. {
  5978. Item->EditWidth = Width;
  5979. }
  5980. //---------------------------------------------------------------------------
  5981. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5982. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5983. {
  5984. for (int i = 0; i < Toolbar->Items->Count; i++)
  5985. {
  5986. TTBXCustomDropDownItem * DropDownItem;
  5987. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5988. if (DropDownItem != NULL)
  5989. {
  5990. ASize.x -= DropDownItem->EditWidth;
  5991. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5992. }
  5993. }
  5994. }
  5995. //---------------------------------------------------------------------------
  5996. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5997. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5998. {
  5999. if (WinConfiguration->DDWarnLackOfTempSpace)
  6000. {
  6001. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6002. if (!ADrive.IsEmpty())
  6003. {
  6004. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6005. DebugAssert(RequiredSpace >= 0);
  6006. __int64 RequiredWithReserve;
  6007. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6008. if (FreeSpace < RequiredWithReserve)
  6009. {
  6010. unsigned int Result;
  6011. TMessageParams Params(mpNeverAskAgainCheck);
  6012. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6013. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6014. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6015. if (Result == qaNeverAskAgain)
  6016. {
  6017. WinConfiguration->DDWarnLackOfTempSpace = false;
  6018. }
  6019. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6020. }
  6021. }
  6022. }
  6023. }
  6024. //---------------------------------------------------------------------------
  6025. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6026. {
  6027. DoOpenDirectoryDialog(odAddBookmark, Side);
  6028. }
  6029. //---------------------------------------------------------------------------
  6030. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6031. {
  6032. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6033. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6034. try
  6035. {
  6036. TCustomDirView * DView = DirView(Side);
  6037. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6038. {
  6039. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6040. }
  6041. }
  6042. catch (...)
  6043. {
  6044. delete VisitedDirectories;
  6045. throw;
  6046. }
  6047. return VisitedDirectories;
  6048. }
  6049. //---------------------------------------------------------------------------
  6050. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6051. TOpenDirectoryMode Mode, TOperationSide Side)
  6052. {
  6053. bool Continue = true;
  6054. if (Mode == odAddBookmark)
  6055. {
  6056. TMessageParams Params(mpNeverAskAgainCheck);
  6057. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6058. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6059. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6060. unsigned int Answer =
  6061. MessageDialog(Message,
  6062. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6063. if (Answer == qaNeverAskAgain)
  6064. {
  6065. Continue = true;
  6066. WinConfiguration->UseSharedBookmarks = true;
  6067. }
  6068. else if (Answer == qaOK)
  6069. {
  6070. Continue = true;
  6071. WinConfiguration->UseSharedBookmarks = false;
  6072. }
  6073. else
  6074. {
  6075. Continue = false;
  6076. }
  6077. }
  6078. if (Continue)
  6079. {
  6080. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6081. try
  6082. {
  6083. UnicodeString Name = DirView(Side)->PathName;
  6084. bool Repeat;
  6085. do
  6086. {
  6087. Repeat = false;
  6088. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6089. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6090. // do not allow switching to location profiles,
  6091. // if we are not connected
  6092. HasDirView[osLocal] && (Terminal != NULL)))
  6093. {
  6094. TWindowLock Lock(this);
  6095. if (!TryOpenDirectory(Side, Name))
  6096. {
  6097. Repeat = true;
  6098. Mode = odBrowse;
  6099. }
  6100. }
  6101. }
  6102. while (Repeat);
  6103. }
  6104. __finally
  6105. {
  6106. delete VisitedDirectories;
  6107. }
  6108. }
  6109. }
  6110. //---------------------------------------------------------------------------
  6111. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6112. {
  6113. DebugAssert(!Terminal->CommandSessionOpened);
  6114. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6115. }
  6116. //---------------------------------------------------------------------------
  6117. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6118. {
  6119. bool Result = Terminal->IsCapable[Capability] ||
  6120. Terminal->CommandSessionOpened;
  6121. if (!Result)
  6122. {
  6123. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6124. if (!GUIConfiguration->ConfirmCommandSession)
  6125. {
  6126. Result = true;
  6127. }
  6128. else
  6129. {
  6130. TMessageParams Params(mpNeverAskAgainCheck);
  6131. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6132. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6133. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6134. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6135. if (Answer == qaNeverAskAgain)
  6136. {
  6137. GUIConfiguration->ConfirmCommandSession = false;
  6138. Result = true;
  6139. }
  6140. else if (Answer == qaOK)
  6141. {
  6142. Result = true;
  6143. }
  6144. }
  6145. if (Result)
  6146. {
  6147. Result = CommandSessionFallback();
  6148. }
  6149. }
  6150. return Result;
  6151. }
  6152. //---------------------------------------------------------------------------
  6153. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6154. {
  6155. if (EnsureCommandSessionFallback(fcAnyCommand))
  6156. {
  6157. DoConsoleDialog(Terminal, Command);
  6158. }
  6159. }
  6160. //---------------------------------------------------------------------------
  6161. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6162. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6163. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6164. {
  6165. if (!IsSideLocalBrowser(osRemote) &&
  6166. IsFileControl(DropSourceControl, osRemote) &&
  6167. (FDDExtMapFile != NULL))
  6168. {
  6169. Accept = true;
  6170. }
  6171. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6172. DebugAssert(FDDTargetControl != NULL);
  6173. }
  6174. //---------------------------------------------------------------------------
  6175. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6176. _di_IDataObject DataObj, int KeyState,
  6177. const TPoint & Point, int & Effect, bool & Accept)
  6178. {
  6179. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6180. }
  6181. //---------------------------------------------------------------------------
  6182. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6183. _di_IDataObject DataObj, int KeyState,
  6184. const TPoint & Point, int & Effect, bool & Accept)
  6185. {
  6186. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6187. }
  6188. //---------------------------------------------------------------------------
  6189. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6190. TObject * Sender)
  6191. {
  6192. DebugUsedParam(Sender);
  6193. DebugAssert(FDDTargetControl == Sender);
  6194. FDDTargetControl = NULL;
  6195. }
  6196. //---------------------------------------------------------------------------
  6197. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6198. {
  6199. FileControlDDDragLeave(SessionsPageControl);
  6200. }
  6201. //---------------------------------------------------------------------------
  6202. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6203. {
  6204. FileControlDDDragLeave(QueueView3);
  6205. }
  6206. //---------------------------------------------------------------------------
  6207. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6208. {
  6209. DebugAssert(!IsSideLocalBrowser(Side));
  6210. DebugUsedParam(Side);
  6211. bool Edit = false;
  6212. TRemoteFile * File = NULL;
  6213. UnicodeString FileName;
  6214. UnicodeString PointTo;
  6215. bool SymbolicLink = true;
  6216. if (RemoteDirView->ItemFocused)
  6217. {
  6218. DebugAssert(RemoteDirView->ItemFocused->Data);
  6219. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6220. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6221. if (Edit)
  6222. {
  6223. FileName = File->FileName;
  6224. PointTo = File->LinkTo;
  6225. }
  6226. else
  6227. {
  6228. PointTo = File->FileName;
  6229. }
  6230. }
  6231. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6232. Terminal->IsCapable[fcHardLink]))
  6233. {
  6234. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6235. if (Edit)
  6236. {
  6237. DebugAssert(File->FileName == FileName);
  6238. int Params = dfNoRecursive;
  6239. Terminal->ExceptionOnFail = true;
  6240. try
  6241. {
  6242. Terminal->DeleteFile(L"", File, &Params);
  6243. }
  6244. __finally
  6245. {
  6246. Terminal->ExceptionOnFail = false;
  6247. }
  6248. }
  6249. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6250. }
  6251. }
  6252. //---------------------------------------------------------------------------
  6253. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6254. {
  6255. return
  6256. (IsSideLocalBrowser(Side) ||
  6257. (HasActiveTerminal() &&
  6258. Terminal->ResolvingSymlinks &&
  6259. Terminal->IsCapable[fcSymbolicLink]));
  6260. }
  6261. //---------------------------------------------------------------------------
  6262. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6263. {
  6264. return
  6265. !IsSideLocalBrowser(FCurrentSide) &&
  6266. (RemoteDirView->ItemFocused != NULL) &&
  6267. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6268. Terminal->SessionData->ResolveSymlinks;
  6269. }
  6270. //---------------------------------------------------------------------------
  6271. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6272. {
  6273. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6274. FForceExecution = true;
  6275. try
  6276. {
  6277. DirView(osCurrent)->ExecuteCurrentFile();
  6278. }
  6279. __finally
  6280. {
  6281. FForceExecution = false;
  6282. }
  6283. }
  6284. //---------------------------------------------------------------------------
  6285. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6286. {
  6287. TEditorData ExternalEditor;
  6288. ExternalEditor.Editor = edExternal;
  6289. bool Remember = false;
  6290. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6291. {
  6292. if (Remember)
  6293. {
  6294. TEditorList * EditorList = new TEditorList;
  6295. try
  6296. {
  6297. *EditorList = *WinConfiguration->EditorList;
  6298. bool Found = false;
  6299. int i = 0;
  6300. while (!Found && (i < EditorList->Count))
  6301. {
  6302. const TEditorPreferences * Editor = EditorList->Editors[i];
  6303. if ((Editor->Data->Editor == edExternal) &&
  6304. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6305. {
  6306. Found = true;
  6307. }
  6308. i++;
  6309. }
  6310. if (!Found)
  6311. {
  6312. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6313. WinConfiguration->EditorList = EditorList;
  6314. }
  6315. }
  6316. __finally
  6317. {
  6318. delete EditorList;
  6319. }
  6320. }
  6321. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6322. }
  6323. }
  6324. //---------------------------------------------------------------------------
  6325. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6326. {
  6327. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6328. if (FFileFindTerminal == ATerminal)
  6329. {
  6330. FFileFindTerminal = NULL;
  6331. HideFileFindDialog();
  6332. }
  6333. if (FClipboardTerminal == ATerminal)
  6334. {
  6335. ClipboardClear(); // implies ClipboardStop
  6336. }
  6337. }
  6338. //---------------------------------------------------------------------------
  6339. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6340. {
  6341. DetachTerminal(Sender);
  6342. }
  6343. //---------------------------------------------------------------------------
  6344. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6345. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6346. {
  6347. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6348. DebugAssert(Terminal != NULL);
  6349. if (Data->Terminal == Terminal)
  6350. {
  6351. Data->Terminal = NULL;
  6352. }
  6353. }
  6354. //---------------------------------------------------------------------------
  6355. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6356. {
  6357. return NULL;
  6358. }
  6359. //---------------------------------------------------------------------------
  6360. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6361. {
  6362. UpdateControls();
  6363. SessionColor = TColor(0);
  6364. UpdateRemotePathComboBox(false);
  6365. NeedSession(false);
  6366. }
  6367. //---------------------------------------------------------------------------
  6368. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6369. {
  6370. try
  6371. {
  6372. // Cache, as the login dialog can change its value.
  6373. // See also DoShow. Also see TTerminalManager::NewSession.
  6374. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6375. try
  6376. {
  6377. if (ShowLogin)
  6378. {
  6379. bool ReloadSessions = !Startup;
  6380. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6381. }
  6382. }
  6383. __finally
  6384. {
  6385. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6386. // (so there was no chance for the user to open any session yet)
  6387. if ((ShowLogin || !Startup) &&
  6388. !WinConfiguration->KeepOpenWhenNoSession &&
  6389. // this rules out the implicit Commander's local-local browser
  6390. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6391. {
  6392. TerminateApplication();
  6393. }
  6394. }
  6395. }
  6396. catch (EAbort &)
  6397. {
  6398. // swallow
  6399. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6400. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6401. }
  6402. }
  6403. //---------------------------------------------------------------------------
  6404. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6405. {
  6406. TTerminalManager * Manager = TTerminalManager::Instance();
  6407. DebugAssert(!Manager->Updating);
  6408. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6409. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6410. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6411. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6412. try
  6413. {
  6414. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6415. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6416. {
  6417. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6418. }
  6419. for (int Index = 0; Index <= Manager->Count; Index++)
  6420. {
  6421. TThemeTabSheet * TabSheet;
  6422. if (Index >= SessionsPageControl->PageCount)
  6423. {
  6424. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6425. TabSheet->PageControl = SessionsPageControl;
  6426. }
  6427. else
  6428. {
  6429. TabSheet = SessionsPageControl->Pages[Index];
  6430. }
  6431. bool IsSessionTab = (Index < Manager->Count);
  6432. if (IsSessionTab)
  6433. {
  6434. TTerminal * Terminal = Manager->Sessions[Index];
  6435. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6436. TabSheet->CaptionTruncation = tttEllipsis;
  6437. UpdateSessionTab(TabSheet);
  6438. }
  6439. else
  6440. {
  6441. TabSheet->Tag = 0; // not really needed
  6442. TabSheet->Shadowed = false;
  6443. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6444. TabSheet->CaptionTruncation = tttNoText;
  6445. // 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)
  6446. UpdateNewTabTab();
  6447. }
  6448. }
  6449. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6450. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6451. {
  6452. SessionsPageControl->UpdateTabsCaptionTruncation();
  6453. }
  6454. }
  6455. __finally
  6456. {
  6457. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6458. }
  6459. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6460. }
  6461. //---------------------------------------------------------------------------
  6462. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6463. {
  6464. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6465. }
  6466. //---------------------------------------------------------------------------
  6467. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6468. {
  6469. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6470. UnicodeString TabCaption;
  6471. if (WinConfiguration->SelectiveToolbarText)
  6472. {
  6473. TabCaption = GetNewTabTabCaption();
  6474. }
  6475. TabSheet->BaseCaption = TabCaption;
  6476. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6477. }
  6478. //---------------------------------------------------------------------------
  6479. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6480. {
  6481. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6482. }
  6483. //---------------------------------------------------------------------------
  6484. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6485. {
  6486. DebugFail();
  6487. return UnicodeString();
  6488. }
  6489. //---------------------------------------------------------------------------
  6490. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6491. {
  6492. if (DebugAlwaysTrue(TabSheet != NULL))
  6493. {
  6494. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6495. if (DebugAlwaysTrue(ASession != NULL))
  6496. {
  6497. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6498. if (ASession->LocalBrowser)
  6499. {
  6500. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6501. }
  6502. else
  6503. {
  6504. TabSheet->ImageIndex = AddSessionColor(Color);
  6505. }
  6506. TTerminalManager * Manager = TTerminalManager::Instance();
  6507. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6508. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6509. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6510. }
  6511. }
  6512. }
  6513. //---------------------------------------------------------------------------
  6514. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6515. {
  6516. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6517. }
  6518. //---------------------------------------------------------------------------
  6519. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6520. {
  6521. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6522. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6523. bool Result = (Session != NULL);
  6524. if (Result)
  6525. {
  6526. TTerminalManager::Instance()->ActiveSession = Session;
  6527. }
  6528. else
  6529. {
  6530. try
  6531. {
  6532. NewTab();
  6533. }
  6534. __finally
  6535. {
  6536. SessionListChanged();
  6537. }
  6538. FSessionsPageControlNewTabTime = Now();
  6539. }
  6540. return Result;
  6541. }
  6542. //---------------------------------------------------------------------------
  6543. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6544. {
  6545. SessionTabSwitched();
  6546. }
  6547. //---------------------------------------------------------------------------
  6548. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6549. {
  6550. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6551. DebugAssert(TransferList != NULL);
  6552. UnicodeString Name;
  6553. if (TransferList->ItemIndex <= 0)
  6554. {
  6555. Name = L"";
  6556. }
  6557. else
  6558. {
  6559. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6560. }
  6561. if (FCopyParamAutoSelected.IsEmpty())
  6562. {
  6563. // if previous preset was not autoselected, make new preset the "default"
  6564. FCopyParamDefault = Name;
  6565. }
  6566. GUIConfiguration->CopyParamCurrent = Name;
  6567. }
  6568. //---------------------------------------------------------------------------
  6569. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6570. {
  6571. // sanity check
  6572. bool ExistingPreset =
  6573. (FTransferListHoverIndex >= 0) &&
  6574. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6575. DebugAssert(ExistingPreset);
  6576. if (ExistingPreset)
  6577. {
  6578. HDC DC = GetDC(0);
  6579. TCanvas * Canvas = new TCanvas();
  6580. try
  6581. {
  6582. Canvas->Handle = DC;
  6583. Canvas->Font = GetToolbarFont(this);
  6584. UnicodeString Name;
  6585. if (FTransferListHoverIndex == 0)
  6586. {
  6587. Name = L"";
  6588. }
  6589. else
  6590. {
  6591. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6592. }
  6593. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6594. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6595. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6596. static_cast<TObject*>(GetComponent(fcTransferList)));
  6597. UnicodeString InfoStr =
  6598. GUIConfiguration->CopyParamPreset[Name].
  6599. GetInfoStr(L"; ",
  6600. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6601. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6602. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6603. {
  6604. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6605. {
  6606. InfoStr.SetLength(InfoStr.Length() - 1);
  6607. }
  6608. InfoStr += Ellipsis;
  6609. }
  6610. // UpdateCaption does not cause invalidation of whole submenu, while
  6611. // setting Caption property does.
  6612. // also it probably does not resize the label, even if necessary
  6613. // (we do not want that anyway)
  6614. TransferLabel->UpdateCaption(InfoStr);
  6615. }
  6616. __finally
  6617. {
  6618. Canvas->Handle = NULL;
  6619. ReleaseDC(0, DC);
  6620. delete Canvas;
  6621. }
  6622. }
  6623. }
  6624. //---------------------------------------------------------------------------
  6625. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6626. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6627. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6628. {
  6629. if (FTransferListHoverIndex != AHoverIndex)
  6630. {
  6631. FTransferListHoverIndex = AHoverIndex;
  6632. UpdateTransferLabel();
  6633. }
  6634. }
  6635. //---------------------------------------------------------------------------
  6636. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6637. {
  6638. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6639. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6640. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6641. {
  6642. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6643. {
  6644. if (!NonVisualDataModule->Busy)
  6645. {
  6646. OpenDirectory(GetSide(osCurrent));
  6647. }
  6648. Message.Result = 1;
  6649. }
  6650. else
  6651. {
  6652. TForm::Dispatch(&Message);
  6653. }
  6654. }
  6655. else
  6656. {
  6657. TForm::Dispatch(&Message);
  6658. }
  6659. }
  6660. //---------------------------------------------------------------------------
  6661. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6662. {
  6663. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6664. if ((FIgnoreNextDialogChar != 0) &&
  6665. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6666. {
  6667. Message.Result = 1;
  6668. }
  6669. else
  6670. {
  6671. TForm::Dispatch(&Message);
  6672. }
  6673. FIgnoreNextDialogChar = 0;
  6674. }
  6675. //---------------------------------------------------------------------------
  6676. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6677. {
  6678. // The four low-order bits are used internally by Windows
  6679. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6680. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6681. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6682. // 61730 - restore thru double click - undocumented
  6683. // 61490 - maximize thru double click - undocumented
  6684. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6685. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6686. {
  6687. SysResizing(Cmd);
  6688. }
  6689. TForm::Dispatch(&Message);
  6690. }
  6691. //---------------------------------------------------------------------------
  6692. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6693. {
  6694. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6695. // Also there no point returning TRUE as we are not able to
  6696. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6697. // Hence the process termination might be safer :)
  6698. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6699. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6700. {
  6701. Message.Result = FALSE;
  6702. }
  6703. else
  6704. {
  6705. Message.Result = TRUE;
  6706. }
  6707. // Do not call default handling as that triggers OnCloseQuery,
  6708. // where our implementation will popup confirmation dialogs, what we do not want,
  6709. // as per Vista guidelines:
  6710. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6711. }
  6712. //---------------------------------------------------------------------------
  6713. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6714. {
  6715. if (Message.EndSession && IsApplicationMinimized())
  6716. {
  6717. // WORKAROUND
  6718. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6719. // when it receives WM_ENDSESSION,
  6720. // but that sometimes (particularly when application is minimized) cause crashes.
  6721. // Crash popups message that blocks log off.
  6722. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6723. // so we call ExitProcess() immediately, not even trying to cleanup.
  6724. // It still causes beep, so there's likely some popup, but it does not block
  6725. // log off.
  6726. ExitProcess(0);
  6727. }
  6728. TForm::Dispatch(&Message);
  6729. }
  6730. #ifdef _DEBUG
  6731. //---------------------------------------------------------------------------
  6732. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6733. {
  6734. TForm::Dispatch(&Message);
  6735. }
  6736. #endif
  6737. //---------------------------------------------------------------------------
  6738. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6739. {
  6740. }
  6741. //---------------------------------------------------------------------------
  6742. void __fastcall TCustomScpExplorerForm::DoShow()
  6743. {
  6744. // Called from TCustomScpExplorerForm::CMShowingChanged
  6745. // only now are the controls resized finally, so the size constraints
  6746. // will not conflict with possibly very small window size
  6747. RestoreFormParams();
  6748. // This indicates that the form was rescaled after parameters were loaded and
  6749. // as most probably the parameters were actually saved with the current form scaling
  6750. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6751. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6752. // and no rounding problems will occur.
  6753. // See also RestoreForm.
  6754. // (we should be safe to call RestoreParams unconditionally)
  6755. if (PixelsPerInch != Screen->PixelsPerInch)
  6756. {
  6757. RestoreParams();
  6758. }
  6759. FixControlsPlacement();
  6760. if (Position == poDefaultPosOnly)
  6761. {
  6762. CutFormToDesktop(this);
  6763. }
  6764. TForm::DoShow();
  6765. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6766. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6767. // This was previously in NeedSession, where other related code is.
  6768. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6769. // unpleasantly shows briefly.
  6770. if ((ManagedSession == NULL) &&
  6771. !WinConfiguration->ShowLoginWhenNoSession &&
  6772. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6773. // This detects if workspace was saved the last time the main widow was closed
  6774. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6775. {
  6776. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6777. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6778. }
  6779. // Before main window displays
  6780. if (ManagedSession == NULL)
  6781. {
  6782. StartingWithoutSession();
  6783. }
  6784. UpdatePixelsPerInchMainWindowCounter();
  6785. FShowing = true;
  6786. }
  6787. //---------------------------------------------------------------------------
  6788. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6789. {
  6790. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6791. }
  6792. //---------------------------------------------------------------------------
  6793. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6794. {
  6795. SessionListChanged();
  6796. InitStatusBar();
  6797. UpdateControls();
  6798. }
  6799. //---------------------------------------------------------------------------
  6800. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6801. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6802. TNotifyEvent OnBalloonClick, TObject * UserData)
  6803. {
  6804. bool Do;
  6805. UnicodeString Message;
  6806. if (E == NULL)
  6807. {
  6808. Message = Str;
  6809. Do = true;
  6810. }
  6811. else
  6812. {
  6813. Do = ExceptionMessage(E, Message);
  6814. }
  6815. if (Do && WinConfiguration->BalloonNotifications)
  6816. {
  6817. UnicodeString Title;
  6818. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6819. {
  6820. Message = TrimLeft(Message);
  6821. }
  6822. else
  6823. {
  6824. Message = UnformatMessage(Message);
  6825. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6826. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6827. Title = LoadResourceString(Captions[Type]);
  6828. if (Terminal != NULL)
  6829. {
  6830. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6831. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6832. }
  6833. }
  6834. if (Seconds == 0)
  6835. {
  6836. Seconds = WinConfiguration->NotificationsTimeout;
  6837. }
  6838. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6839. }
  6840. }
  6841. //---------------------------------------------------------------------------
  6842. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6843. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6844. UnicodeString HelpKeyword, const TMessageParams * Params,
  6845. TTerminal * Terminal)
  6846. {
  6847. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6848. (FOnFeedSynchronizeError != NULL)) &&
  6849. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6850. {
  6851. if (FOnFeedSynchronizeError != NULL)
  6852. {
  6853. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6854. }
  6855. else
  6856. {
  6857. DebugAssert(FErrorList != NULL);
  6858. TStringList * MoreMessagesCopy = NULL;
  6859. if (MoreMessages)
  6860. {
  6861. MoreMessagesCopy = new TStringList();
  6862. MoreMessagesCopy->Assign(MoreMessages);
  6863. }
  6864. FErrorList->AddObject(Message, MoreMessagesCopy);
  6865. }
  6866. PopupTrayBalloon(Terminal, Message, Type);
  6867. return ContinueAnswer(Answers);
  6868. }
  6869. else
  6870. {
  6871. bool UseBalloon = FTrayIcon->Visible;
  6872. if (UseBalloon)
  6873. {
  6874. PopupTrayBalloon(Terminal, Message, Type);
  6875. }
  6876. unsigned int Result;
  6877. try
  6878. {
  6879. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6880. }
  6881. __finally
  6882. {
  6883. // cancel only balloon we popped up, otherwise we may cancel notification
  6884. // balloon that was there before the message dialog
  6885. // (such as "dd confirmation opt in balloon)
  6886. if (UseBalloon)
  6887. {
  6888. FTrayIcon->CancelBalloon();
  6889. }
  6890. }
  6891. return Result;
  6892. }
  6893. }
  6894. //---------------------------------------------------------------------------
  6895. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6896. TTerminal * Terminal, Exception * E)
  6897. {
  6898. if (FTrayIcon->Visible)
  6899. {
  6900. PopupTrayBalloon(Terminal, L"", qtError, E);
  6901. }
  6902. // particularly prevent opening new session from jump list,
  6903. // while exception is shown
  6904. NonVisualDataModule->StartBusy();
  6905. try
  6906. {
  6907. ShowExtendedExceptionEx(Terminal, E);
  6908. }
  6909. __finally
  6910. {
  6911. NonVisualDataModule->EndBusy();
  6912. FTrayIcon->CancelBalloon();
  6913. }
  6914. }
  6915. //---------------------------------------------------------------------------
  6916. void __fastcall TCustomScpExplorerForm::SessionReady()
  6917. {
  6918. InitStatusBar();
  6919. // cannot rely on active page being page for active terminal,
  6920. // as it can happen that active page is the "new session" page
  6921. // (e.g. when reconnecting active terminal, while login dialog
  6922. // invoked from "new session" page is modal)
  6923. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  6924. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  6925. }
  6926. //---------------------------------------------------------------------------
  6927. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6928. TTerminal * Terminal, Exception * E)
  6929. {
  6930. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6931. if (!Terminal->Active)
  6932. {
  6933. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6934. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6935. {
  6936. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6937. }
  6938. }
  6939. }
  6940. //---------------------------------------------------------------------------
  6941. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6942. UnicodeString Message, TQueryType Type,
  6943. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6944. {
  6945. if ((E == NULL) ||
  6946. ExceptionMessage(E, Message))
  6947. {
  6948. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6949. if (Important)
  6950. {
  6951. Seconds *= 5;
  6952. }
  6953. UnicodeString NoteMessage(UnformatMessage(Message));
  6954. if (Terminal != NULL)
  6955. {
  6956. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6957. NoteMessage = FORMAT(L"%s: %s",
  6958. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  6959. }
  6960. if (WinConfiguration->BalloonNotifications)
  6961. {
  6962. AddNote(NoteMessage);
  6963. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6964. }
  6965. else
  6966. {
  6967. FlashOnBackground();
  6968. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6969. }
  6970. }
  6971. }
  6972. //---------------------------------------------------------------------------
  6973. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6974. {
  6975. RestoreApp();
  6976. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6977. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6978. !NonVisualDataModule->Busy)
  6979. {
  6980. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6981. TTerminalManager::Instance()->ActiveSession = Terminal;
  6982. if (!ComponentVisible[fcQueueView])
  6983. {
  6984. ToggleQueueVisibility();
  6985. GoToQueue();
  6986. }
  6987. }
  6988. }
  6989. //---------------------------------------------------------------------------
  6990. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6991. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6992. {
  6993. UnicodeString Message;
  6994. TNotifyEvent OnClick = NULL;
  6995. TObject * UserData = NULL;
  6996. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6997. switch (Event)
  6998. {
  6999. case qeEmptyButMonitored:
  7000. if ((ATerminal != Terminal) || QueueInvisible)
  7001. {
  7002. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7003. OnClick = QueueEmptyNoteClicked;
  7004. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7005. TerminalNoteData->Terminal = ATerminal;
  7006. UserData = TerminalNoteData;
  7007. }
  7008. break;
  7009. case qeEmpty:
  7010. OperationComplete(ATerminal->QueueOperationStart);
  7011. break;
  7012. case qePendingUserAction:
  7013. if ((ATerminal != Terminal) ||
  7014. (QueueInvisible && !IsQueueAutoPopup()))
  7015. {
  7016. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7017. }
  7018. break;
  7019. default:
  7020. DebugFail();
  7021. }
  7022. if (!Message.IsEmpty())
  7023. {
  7024. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7025. }
  7026. }
  7027. //---------------------------------------------------------------------------
  7028. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7029. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7030. {
  7031. if (FDDExtMapFile != NULL)
  7032. {
  7033. CloseHandle(FDDExtMapFile);
  7034. FDDExtMapFile = NULL;
  7035. }
  7036. if (WinConfiguration->DDFakeFile)
  7037. {
  7038. DDFakeFileInitDrag(FileList, Created);
  7039. }
  7040. }
  7041. //---------------------------------------------------------------------------
  7042. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7043. {
  7044. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7045. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7046. {
  7047. FFakeFileDropTarget = FileName;
  7048. }
  7049. }
  7050. //---------------------------------------------------------------------------
  7051. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7052. {
  7053. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7054. if (!ForceDirectories(ApiPath(Result)))
  7055. {
  7056. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7057. }
  7058. FileSetAttr(ApiPath(Result), faHidden);
  7059. return Result;
  7060. }
  7061. //---------------------------------------------------------------------------
  7062. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7063. bool & Created)
  7064. {
  7065. FFakeFileDropTarget = UnicodeString();
  7066. FDragFakeDirectory = CreateFakeTransferDirectory();
  7067. FileList->AddItem(NULL, FDragFakeDirectory);
  7068. Created = true;
  7069. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7070. {
  7071. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7072. }
  7073. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7074. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7075. {
  7076. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7077. TDragExtCommStruct* CommStruct;
  7078. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7079. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7080. DebugAssert(CommStruct != NULL);
  7081. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7082. CommStruct->Dragging = true;
  7083. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7084. LENOF(CommStruct->DropDest));
  7085. NULL_TERMINATE(CommStruct->DropDest);
  7086. UnmapViewOfFile(CommStruct);
  7087. }
  7088. }
  7089. //---------------------------------------------------------------------------
  7090. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7091. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7092. {
  7093. bool Result;
  7094. DebugAssert(!IsLocalBrowserMode());
  7095. if (Sender == RemoteDirView)
  7096. {
  7097. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7098. }
  7099. else
  7100. {
  7101. DebugAssert(Sender == RemoteDriveView);
  7102. TStrings * FileList = RemoteDriveView->DragFileList();
  7103. try
  7104. {
  7105. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7106. }
  7107. __finally
  7108. {
  7109. delete FileList;
  7110. }
  7111. }
  7112. if (FDDTargetControl == RemoteDriveView)
  7113. {
  7114. RemoteDriveView->UpdateDropTarget();
  7115. }
  7116. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7117. // or from tree view to dir view.
  7118. // foMove happens when file/dir is dragged from remote tree view outside of
  7119. // application via dragex
  7120. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7121. (DropSourceControl == RemoteDriveView))
  7122. {
  7123. RemoteDriveView->UpdateDropSource();
  7124. }
  7125. return Result;
  7126. }
  7127. //---------------------------------------------------------------------------
  7128. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7129. {
  7130. DebugAssert(!IsLocalBrowserMode());
  7131. // This also handles drops of remote files to queue.
  7132. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7133. SAFE_DESTROY(FDDFileList);
  7134. if (!FFakeFileDropTarget.IsEmpty())
  7135. {
  7136. RemoveDir(ApiPath(FFakeFileDropTarget));
  7137. }
  7138. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7139. {
  7140. try
  7141. {
  7142. TDragResult DDResult = (Sender == RemoteDirView) ?
  7143. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7144. // Focus is moved to the target application,
  7145. // but as we are going to present the UI, we need to steal the focus back.
  7146. // This most likely won't work though (windows does not allow application
  7147. // to steal focus most of the time)
  7148. Application->BringToFront();
  7149. // On older version of Windows we never got drMove here, see also comment below.
  7150. // On Windows 10, we get the "move".
  7151. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7152. {
  7153. UnicodeString TargetDirectory;
  7154. TFileOperation Operation;
  7155. // drInvalid may mean drMove, see comment below
  7156. switch (DDResult)
  7157. {
  7158. case drCopy:
  7159. Operation = foCopy;
  7160. break;
  7161. case drMove:
  7162. Operation = foMove;
  7163. break;
  7164. default:
  7165. DebugFail();
  7166. case drInvalid:
  7167. // prefer "copy" for safety
  7168. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7169. break;
  7170. }
  7171. TTransferOperationParam Param;
  7172. UnicodeString CounterName;
  7173. bool ForceQueue;
  7174. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7175. {
  7176. // we get drInvalid both if d&d was intercepted by ddext,
  7177. // and when users drops on no-drop location.
  7178. // we tell the difference by existence of response from ddext,
  7179. // so we ignore absence of response in this case
  7180. if (DDResult != drInvalid)
  7181. {
  7182. // here we know that the extension is installed,
  7183. // as it is checked as soon as drag&drop starts from
  7184. // RemoteFileControlDDCreateDragFileList
  7185. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7186. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7187. {
  7188. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7189. }
  7190. else
  7191. {
  7192. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7193. }
  7194. }
  7195. }
  7196. else
  7197. {
  7198. // download using fake file
  7199. Param.Temp = false;
  7200. Param.DragDrop = true;
  7201. if (ForceQueue)
  7202. {
  7203. Param.Queue = asOn;
  7204. }
  7205. if (Sender == RemoteDirView)
  7206. {
  7207. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7208. }
  7209. if (RemoteFileControlFileOperation(Sender, Operation,
  7210. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7211. {
  7212. Configuration->Usage->Inc(CounterName);
  7213. }
  7214. }
  7215. }
  7216. }
  7217. __finally
  7218. {
  7219. CloseHandle(FDDExtMapFile);
  7220. FDDExtMapFile = NULL;
  7221. delete FDragFakeMonitors;
  7222. FDragFakeMonitors = NULL;
  7223. RemoveDir(ApiPath(FDragFakeDirectory));
  7224. FDragFakeDirectory = L"";
  7225. FFakeFileDropTarget = UnicodeString();
  7226. }
  7227. }
  7228. if (FDragDropSshTerminate.get() != NULL)
  7229. {
  7230. FDragDropSshTerminate->Rethrow();
  7231. }
  7232. }
  7233. //---------------------------------------------------------------------------
  7234. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7235. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7236. {
  7237. // Remember drop effect so we know (when user drops files), if we copy or move
  7238. FLastDropEffect = dwEffect;
  7239. }
  7240. //---------------------------------------------------------------------------
  7241. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7242. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7243. {
  7244. bool Result;
  7245. if (FDDTargetControl == QueueView3)
  7246. {
  7247. Directory = DefaultDownloadTargetDirectory();
  7248. Result = true;
  7249. CounterName = L"DownloadsDragDropQueue";
  7250. ForceQueue = true;
  7251. }
  7252. else if (!FFakeFileDropTarget.IsEmpty())
  7253. {
  7254. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7255. Result = true;
  7256. ForceQueue = false;
  7257. CounterName = L"DownloadsDragDropFakeFile";
  7258. }
  7259. else
  7260. {
  7261. ForceQueue = false;
  7262. Enabled = false;
  7263. try
  7264. {
  7265. int Timer = 0;
  7266. Result = false;
  7267. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7268. {
  7269. {
  7270. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7271. TDragExtCommStruct* CommStruct;
  7272. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7273. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7274. DebugAssert(CommStruct != NULL);
  7275. Result = !CommStruct->Dragging;
  7276. if (Result)
  7277. {
  7278. Directory = ExtractFilePath(CommStruct->DropDest);
  7279. CounterName = L"DownloadsDragDropExternalExt";
  7280. }
  7281. UnmapViewOfFile(CommStruct);
  7282. }
  7283. if (!Result)
  7284. {
  7285. Sleep(50);
  7286. Timer += 50;
  7287. Application->ProcessMessages();
  7288. }
  7289. }
  7290. }
  7291. __finally
  7292. {
  7293. Enabled = true;
  7294. }
  7295. }
  7296. return Result;
  7297. }
  7298. //---------------------------------------------------------------------------
  7299. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7300. const UnicodeString TempDir, int SecDelay)
  7301. {
  7302. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7303. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7304. if (FDelayedDeletionTimer == NULL)
  7305. {
  7306. DebugAssert(HandleAllocated());
  7307. FDelayedDeletionTimer = new TTimer(this);
  7308. FDelayedDeletionTimer->Interval = 10000;
  7309. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7310. }
  7311. else
  7312. {
  7313. FDelayedDeletionTimer->Enabled = true;
  7314. }
  7315. }
  7316. //---------------------------------------------------------------------------
  7317. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7318. {
  7319. DebugAssert(FDelayedDeletionList != NULL);
  7320. TDateTime N = Now();
  7321. TDateTime Alert;
  7322. UnicodeString Directory;
  7323. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7324. {
  7325. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7326. if ((N >= Alert) || (Sender == NULL))
  7327. {
  7328. Directory = FDelayedDeletionList->Strings[Index];
  7329. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7330. {
  7331. FDelayedDeletionList->Delete(Index);
  7332. }
  7333. }
  7334. }
  7335. if (FDelayedDeletionList->Count == 0)
  7336. {
  7337. FDelayedDeletionTimer->Enabled = false;
  7338. }
  7339. }
  7340. //---------------------------------------------------------------------------
  7341. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7342. {
  7343. DebugAssert(!IsLocalBrowserMode());
  7344. if (IsFileControl(FDDTargetControl, osRemote) ||
  7345. (FDDTargetControl == SessionsPageControl))
  7346. {
  7347. TManagedTerminal * TargetTerminal = NULL;
  7348. TFileOperation Operation = foNone;
  7349. if (FDDTargetControl == SessionsPageControl)
  7350. {
  7351. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7352. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7353. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7354. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7355. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7356. {
  7357. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7358. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7359. {
  7360. Operation = foRemoteMove;
  7361. }
  7362. else
  7363. {
  7364. Operation = foRemoteCopy;
  7365. }
  7366. }
  7367. }
  7368. else
  7369. {
  7370. if (FLastDropEffect == DROPEFFECT_MOVE)
  7371. {
  7372. Operation = foRemoteMove;
  7373. }
  7374. else if (FLastDropEffect == DROPEFFECT_COPY)
  7375. {
  7376. Operation = foRemoteCopy;
  7377. }
  7378. }
  7379. if (Operation != foNone)
  7380. {
  7381. RemoteFileControlFileOperation(DropSourceControl,
  7382. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7383. }
  7384. // abort drag&drop
  7385. Abort();
  7386. }
  7387. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7388. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7389. (FDDTargetControl != QueueView3))
  7390. {
  7391. DebugAssert(!FDragTempDir.IsEmpty());
  7392. TTransferType Type;
  7393. UnicodeString TempDir = FDragTempDir;
  7394. // We clear FDragTempDir before calling
  7395. // just in case it fail (raises exception)
  7396. FDragTempDir = L"";
  7397. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7398. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7399. // empty directory parameter means temp directory -> don't display it!
  7400. UnicodeString TargetDir = L"";
  7401. int Options = 0;
  7402. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7403. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7404. {
  7405. Abort();
  7406. }
  7407. // TargetDir is set when dropped on local file control
  7408. // (this was workaround for legacy dirview event handling, now it should be
  7409. // made prettier)
  7410. if (TargetDir.IsEmpty())
  7411. {
  7412. TargetDir = TempDir;
  7413. if (ForceDirectories(ApiPath(TargetDir)))
  7414. {
  7415. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7416. FPendingTempSpaceWarn = true;
  7417. try
  7418. {
  7419. FDragDropOperation = true;
  7420. int Params = cpTemporary |
  7421. (Type == ttMove ? cpDelete : 0);
  7422. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7423. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7424. }
  7425. __finally
  7426. {
  7427. FDragDropOperation = false;
  7428. FPendingTempSpaceWarn = false;
  7429. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7430. }
  7431. }
  7432. else
  7433. {
  7434. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7435. }
  7436. }
  7437. }
  7438. }
  7439. //---------------------------------------------------------------------------
  7440. void __fastcall TCustomScpExplorerForm::DDDownload(
  7441. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7442. {
  7443. DebugAssert(!IsLocalBrowserMode());
  7444. TAutoBatch AutoBatch(this);
  7445. UpdateCopyParamCounters(*CopyParam);
  7446. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7447. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7448. {
  7449. RemoteDriveView->UpdateDropSource();
  7450. }
  7451. }
  7452. //---------------------------------------------------------------------------
  7453. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7454. {
  7455. public:
  7456. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7457. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7458. {
  7459. }
  7460. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7461. {
  7462. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7463. TDataObjectFilesEx::AllowData(FormatEtc);
  7464. }
  7465. };
  7466. //---------------------------------------------------------------------------
  7467. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7468. TObject * Sender, TDataObject *& DataObject)
  7469. {
  7470. if (FDDExtMapFile != NULL)
  7471. {
  7472. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7473. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7474. {
  7475. Abort();
  7476. }
  7477. if (FileList->Count > 0)
  7478. {
  7479. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7480. if (!FilesObject->IsValid(true, true))
  7481. {
  7482. FilesObject->_Release();
  7483. }
  7484. else
  7485. {
  7486. DataObject = FilesObject;
  7487. }
  7488. }
  7489. }
  7490. }
  7491. //---------------------------------------------------------------------------
  7492. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7493. {
  7494. DebugFail();
  7495. }
  7496. //---------------------------------------------------------------------------
  7497. void __fastcall TCustomScpExplorerForm::GoToTree()
  7498. {
  7499. ComponentVisible[fcRemoteTree] = true;
  7500. DriveView(osRemote)->SetFocus();
  7501. }
  7502. //---------------------------------------------------------------------------
  7503. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7504. TPanelExport Export)
  7505. {
  7506. TCustomDirView * DirView = this->DirView(Side);
  7507. std::unique_ptr<TStrings> ExportData(new TStringList());
  7508. switch (Export)
  7509. {
  7510. case pePath:
  7511. ExportData->Add(DirView->PathName);
  7512. break;
  7513. case peFileList:
  7514. case peFullFileList:
  7515. {
  7516. bool FullPath = (Export == peFullFileList);
  7517. DirView->CreateFileList(false, FullPath, ExportData.get());
  7518. for (int Index = 0; Index < ExportData->Count; Index++)
  7519. {
  7520. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7521. {
  7522. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7523. }
  7524. }
  7525. }
  7526. break;
  7527. default:
  7528. DebugFail();
  7529. }
  7530. return ExportData.release();
  7531. }
  7532. //---------------------------------------------------------------------------
  7533. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7534. TPanelExport Export, TPanelExportDestination Destination)
  7535. {
  7536. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7537. PanelExportStore(Side, Export, Destination, ExportData.get());
  7538. }
  7539. //---------------------------------------------------------------------------
  7540. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7541. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7542. TStrings * ExportData)
  7543. {
  7544. if (Destination == pedClipboard)
  7545. {
  7546. TInstantOperationVisualizer Visualizer;
  7547. CopyToClipboard(ExportData);
  7548. }
  7549. else
  7550. {
  7551. DebugFail();
  7552. }
  7553. }
  7554. //---------------------------------------------------------------------------
  7555. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7556. {
  7557. TCustomDirView * DirView = this->DirView(Side);
  7558. UnicodeString Mask = DirView->Mask;
  7559. if (DoFilterMaskDialog(DirView, Mask))
  7560. {
  7561. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7562. Configuration->Usage->Inc(L"Filters");
  7563. }
  7564. }
  7565. //---------------------------------------------------------------------------
  7566. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7567. {
  7568. return FQueueController->DefaultOperation();
  7569. }
  7570. //---------------------------------------------------------------------------
  7571. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7572. TQueueOperation Operation, void ** Param)
  7573. {
  7574. switch (Operation)
  7575. {
  7576. case qoPreferences:
  7577. return true;
  7578. case qoGoTo:
  7579. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7580. case qoOnceEmpty:
  7581. return IsAnythingQueued();
  7582. default:
  7583. return FQueueController->AllowOperation(Operation, Param);
  7584. }
  7585. }
  7586. //---------------------------------------------------------------------------
  7587. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7588. {
  7589. if (DebugAlwaysTrue(QueueView3->Visible))
  7590. {
  7591. QueueView3->SetFocus();
  7592. }
  7593. }
  7594. //---------------------------------------------------------------------------
  7595. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7596. TQueueOperation Operation, void * Param)
  7597. {
  7598. if (Operation == qoGoTo)
  7599. {
  7600. GoToQueue();
  7601. }
  7602. else if (Operation == qoPreferences)
  7603. {
  7604. PreferencesDialog(pmQueue);
  7605. }
  7606. else
  7607. {
  7608. FQueueController->ExecuteOperation(Operation, Param);
  7609. }
  7610. }
  7611. //---------------------------------------------------------------------------
  7612. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7613. {
  7614. return (Queue != NULL) && Queue->Enabled;
  7615. }
  7616. //---------------------------------------------------------------------------
  7617. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7618. {
  7619. DebugAssert(Queue != NULL);
  7620. if (Queue != NULL)
  7621. {
  7622. Queue->Enabled = !Queue->Enabled;
  7623. }
  7624. }
  7625. //---------------------------------------------------------------------------
  7626. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7627. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7628. {
  7629. FQueueActedItem = QueueView3->ItemFocused;
  7630. }
  7631. //---------------------------------------------------------------------------
  7632. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7633. {
  7634. return TopDock->Height + QueueSplitter->Height;
  7635. }
  7636. //---------------------------------------------------------------------------
  7637. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7638. {
  7639. return
  7640. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7641. (QueueDock->Visible ? QueueDock->Height : 0) +
  7642. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7643. }
  7644. //---------------------------------------------------------------------------
  7645. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7646. {
  7647. return ScaleByTextHeight(this, 48);
  7648. }
  7649. //---------------------------------------------------------------------------
  7650. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7651. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7652. {
  7653. // when queue is hidden by double-clicking splitter, stray attempt to
  7654. // resize the panel with strange value arrives, make sure it is ignored
  7655. if (ComponentVisible[fcQueueView])
  7656. {
  7657. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7658. if (NewSize > MaxHeightLimit)
  7659. {
  7660. NewSize = MaxHeightLimit;
  7661. }
  7662. int MinHeightLimit =
  7663. GetStaticQueuePanelComponentsHeight() +
  7664. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7665. GetMinQueueViewHeight();
  7666. if (NewSize < MinHeightLimit)
  7667. {
  7668. NewSize = MinHeightLimit;
  7669. }
  7670. }
  7671. else
  7672. {
  7673. Accept = false;
  7674. }
  7675. }
  7676. //---------------------------------------------------------------------------
  7677. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7678. {
  7679. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7680. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7681. if (QueueViewHeight < GetMinQueueViewHeight())
  7682. {
  7683. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7684. }
  7685. }
  7686. //---------------------------------------------------------------------------
  7687. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7688. TDragObject *& /*DragObject*/)
  7689. {
  7690. FQueueActedItem = QueueView3->ItemFocused;
  7691. }
  7692. //---------------------------------------------------------------------------
  7693. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7694. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7695. {
  7696. Accept = true;
  7697. if (Source == QueueView3)
  7698. {
  7699. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7700. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7701. if (Accept)
  7702. {
  7703. TQueueItemProxy * QueueItem;
  7704. TQueueItemProxy * DestQueueItem;
  7705. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7706. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7707. Accept = (QueueItem != DestQueueItem) &&
  7708. (QueueItem->Status == TQueueItem::qsPending) &&
  7709. (DestQueueItem->Status == TQueueItem::qsPending);
  7710. }
  7711. }
  7712. }
  7713. //---------------------------------------------------------------------------
  7714. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7715. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7716. {
  7717. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7718. {
  7719. TQueueItemProxy * QueueItem;
  7720. TQueueItemProxy * DestQueueItem;
  7721. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7722. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7723. QueueItem->Move(DestQueueItem);
  7724. }
  7725. }
  7726. //---------------------------------------------------------------------------
  7727. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7728. {
  7729. if ((QueueView3->ItemFocused == NULL) &&
  7730. (QueueView3->Items->Count > 0))
  7731. {
  7732. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7733. }
  7734. QueueLabelUpdateStatus();
  7735. }
  7736. //---------------------------------------------------------------------------
  7737. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7738. {
  7739. QueueLabelUpdateStatus();
  7740. }
  7741. //---------------------------------------------------------------------------
  7742. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7743. {
  7744. QueueLabel->UpdateStatus();
  7745. }
  7746. //---------------------------------------------------------------------------
  7747. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7748. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7749. {
  7750. if (Selected)
  7751. {
  7752. NonVisualDataModule->UpdateNonVisibleActions();
  7753. }
  7754. }
  7755. //---------------------------------------------------------------------------
  7756. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7757. {
  7758. TDragDropFilesEx * Result = NULL;
  7759. if (Sender == SessionsPageControl)
  7760. {
  7761. Result = FSessionsDragDropFilesEx;
  7762. }
  7763. else if (Sender == QueueView3)
  7764. {
  7765. Result = FQueueDragDropFilesEx;
  7766. }
  7767. else
  7768. {
  7769. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7770. if (DirView != NULL)
  7771. {
  7772. Result = DirView->DragDropFilesEx;
  7773. }
  7774. else
  7775. {
  7776. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7777. if (DriveView != NULL)
  7778. {
  7779. Result = DriveView->DragDropFilesEx;
  7780. }
  7781. }
  7782. }
  7783. DebugAssert(Result != NULL);
  7784. return Result;
  7785. }
  7786. //---------------------------------------------------------------------------
  7787. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7788. {
  7789. return (DView->SelCount == DView->FilesCount);
  7790. }
  7791. //---------------------------------------------------------------------------
  7792. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7793. {
  7794. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7795. }
  7796. //---------------------------------------------------------------------------
  7797. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7798. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7799. {
  7800. DebugAssert(!IsLocalBrowserMode());
  7801. TFileOperation Operation;
  7802. switch (Effect)
  7803. {
  7804. case DROPEFFECT_MOVE:
  7805. Operation = foMove;
  7806. break;
  7807. case DROPEFFECT_COPY:
  7808. // occures on WinXP (reported by user)
  7809. default:
  7810. Operation = foCopy;
  7811. break;
  7812. };
  7813. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7814. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7815. if (DragDropFilesEx->FileList->Count > 0)
  7816. {
  7817. TStrings * FileList = new TStringList();
  7818. try
  7819. {
  7820. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7821. {
  7822. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7823. }
  7824. FDragDropOperation = true;
  7825. TTransferOperationParam Param;
  7826. Param.TargetDirectory = TargetPath;
  7827. // upload, no temp dirs
  7828. Param.Temp = false;
  7829. Param.DragDrop = DragDrop;
  7830. Param.Options =
  7831. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7832. if (ForceQueue)
  7833. {
  7834. Param.Queue = asOn;
  7835. }
  7836. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7837. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7838. {
  7839. if (IsFileControl(DropSourceControl, osLocal))
  7840. {
  7841. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7842. }
  7843. else
  7844. {
  7845. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7846. }
  7847. }
  7848. }
  7849. __finally
  7850. {
  7851. FDragDropOperation = false;
  7852. delete FileList;
  7853. }
  7854. }
  7855. }
  7856. //---------------------------------------------------------------------------
  7857. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7858. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7859. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7860. {
  7861. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7862. }
  7863. //---------------------------------------------------------------------------
  7864. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7865. TObject * Sender, int grfKeyState, int & dwEffect)
  7866. {
  7867. DebugAssert(!IsLocalBrowserMode());
  7868. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7869. if ((dwEffect != DROPEFFECT_NONE) &&
  7870. IsFileControl(DropSourceControl, osRemote))
  7871. {
  7872. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7873. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7874. (RemoteDirView->DropTarget == NULL))
  7875. {
  7876. dwEffect = DROPEFFECT_NONE;
  7877. }
  7878. else
  7879. {
  7880. if (dwEffect == DROPEFFECT_COPY)
  7881. {
  7882. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  7883. // currently we support copying always (at least via temporary directory);
  7884. // remove associated checks once this all proves stable and working
  7885. bool CopyCapable = true;
  7886. // if we do not support neither of operations, there's no discussion
  7887. if (!MoveCapable && !CopyCapable)
  7888. {
  7889. dwEffect = DROPEFFECT_NONE;
  7890. }
  7891. else
  7892. {
  7893. // The default effect inside remote panel is move,
  7894. // unless we do not support it, but support copy
  7895. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7896. {
  7897. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7898. }
  7899. else
  7900. {
  7901. // with ctrl-down, we want copy unless it is not supported
  7902. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7903. }
  7904. }
  7905. }
  7906. }
  7907. }
  7908. }
  7909. //---------------------------------------------------------------------------
  7910. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7911. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7912. {
  7913. DebugAssert(!IsLocalBrowserMode());
  7914. if (FDDTotalSize >= 0)
  7915. {
  7916. if (File->IsDirectory)
  7917. {
  7918. FDDTotalSize = -1;
  7919. }
  7920. else
  7921. {
  7922. FDDTotalSize += File->Size;
  7923. }
  7924. }
  7925. DebugAssert(!FDragTempDir.IsEmpty());
  7926. // TODO: this is quite ineffective
  7927. // TODO: what if invalid character replacement is disabled?
  7928. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7929. UnicodeString TransferFileName;
  7930. if (Sender == RemoteDriveView)
  7931. {
  7932. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7933. }
  7934. else
  7935. {
  7936. TransferFileName = File->FileName;
  7937. }
  7938. FDDFileList->AddObject(TransferFileName, File);
  7939. }
  7940. //---------------------------------------------------------------------------
  7941. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7942. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7943. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7944. {
  7945. // sometimes we do not get DDEnd so the list is not released
  7946. SAFE_DESTROY(FDDFileList);
  7947. FDDFileList = new TStringList();
  7948. FDragTempDir = WinConfiguration->TemporaryDir();
  7949. FDDTotalSize = 0;
  7950. FDragDropSshTerminate.reset(NULL);
  7951. }
  7952. //---------------------------------------------------------------------------
  7953. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7954. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7955. HRESULT & Result)
  7956. {
  7957. if (Result == DRAGDROP_S_DROP)
  7958. {
  7959. try
  7960. {
  7961. GlobalDragImageList->HideDragImage();
  7962. try
  7963. {
  7964. RemoteFileControlDDTargetDrop();
  7965. }
  7966. catch(ESshTerminate & E)
  7967. {
  7968. FDragDropSshTerminate.reset(E.Clone());
  7969. }
  7970. }
  7971. catch (Exception &E)
  7972. {
  7973. // If downloading fails we need to cancel drag&drop, otherwise
  7974. // Explorer shows error
  7975. // But by the way exception probably never reach this point as
  7976. // it's catched on way.
  7977. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7978. Result = DRAGDROP_S_CANCEL;
  7979. ShowExtendedException(Terminal, &E);
  7980. }
  7981. }
  7982. }
  7983. //---------------------------------------------------------------------------
  7984. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7985. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7986. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7987. {
  7988. TFileMasks::TParams MaskParams;
  7989. MaskParams.Size = Size;
  7990. MaskParams.Modification = Modification;
  7991. // this does not re-parse the mask if it is the same as the last time
  7992. FDirViewMatchMask = Masks;
  7993. bool ImplicitMatch;
  7994. Matches =
  7995. // RecurseInclude parameter has no effect as the Path is empty
  7996. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7997. (AllowImplicitMatches || !ImplicitMatch);
  7998. }
  7999. //---------------------------------------------------------------------------
  8000. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8001. TObject * Sender, TListItem * Item, WORD & Indexes)
  8002. {
  8003. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8004. UnicodeString Ext;
  8005. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8006. {
  8007. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8008. }
  8009. else
  8010. {
  8011. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8012. }
  8013. if (SameText(Ext, Configuration->PartialExt))
  8014. {
  8015. Indexes |= oiPartial;
  8016. }
  8017. }
  8018. //---------------------------------------------------------------------------
  8019. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8020. {
  8021. return
  8022. DirViewEnabled(osCurrent) &&
  8023. DirView(osCurrent)->CanPasteFromClipBoard();
  8024. }
  8025. //---------------------------------------------------------------------------
  8026. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8027. {
  8028. bool Result = false;
  8029. if (CanPasteToDirViewFromClipBoard())
  8030. {
  8031. Result = true;
  8032. }
  8033. else
  8034. {
  8035. UnicodeString ClipboardText;
  8036. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8037. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8038. {
  8039. if (StoredSessions->IsUrl(ClipboardText))
  8040. {
  8041. Result = true;
  8042. }
  8043. else
  8044. {
  8045. Result = DirViewEnabled(osCurrent);
  8046. }
  8047. }
  8048. }
  8049. return Result;
  8050. }
  8051. //---------------------------------------------------------------------------
  8052. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8053. {
  8054. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8055. if (DoesClipboardContainOurFiles())
  8056. {
  8057. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8058. {
  8059. DebugAssert(!IsLocalBrowserMode());
  8060. TTerminalManager * Manager = TTerminalManager::Instance();
  8061. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8062. Manager->ActiveSession = FClipboardTerminal;
  8063. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8064. }
  8065. }
  8066. else if (CanPasteToDirViewFromClipBoard())
  8067. {
  8068. DirView(osCurrent)->PasteFromClipBoard();
  8069. }
  8070. else
  8071. {
  8072. UnicodeString ClipboardText;
  8073. if (NonEmptyTextFromClipboard(ClipboardText))
  8074. {
  8075. if (StoredSessions->IsUrl(ClipboardText))
  8076. {
  8077. NewSession(ClipboardText);
  8078. }
  8079. else
  8080. {
  8081. DirView(osCurrent)->Path = ClipboardText;
  8082. }
  8083. }
  8084. }
  8085. }
  8086. //---------------------------------------------------------------------------
  8087. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8088. {
  8089. // TBD
  8090. }
  8091. //---------------------------------------------------------------------------
  8092. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8093. {
  8094. TCustomDirView * ADirView = DirView(Side);
  8095. ADirView->SelectAll(Mode);
  8096. ADirView->SetFocus();
  8097. }
  8098. //---------------------------------------------------------------------------
  8099. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8100. {
  8101. TCustomDirView * ADirView = DirView(Side);
  8102. TFileFilter Filter;
  8103. DefaultFileFilter(Filter);
  8104. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8105. {
  8106. Configuration->Usage->Inc(L"MaskSelections");
  8107. ADirView->SelectFiles(Filter, Select);
  8108. ADirView->SetFocus();
  8109. }
  8110. }
  8111. //---------------------------------------------------------------------------
  8112. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8113. {
  8114. TCustomDirView * ADirView = DirView(Side);
  8115. ADirView->RestoreSelectedNames();
  8116. ADirView->SetFocus();
  8117. }
  8118. //---------------------------------------------------------------------------
  8119. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8120. {
  8121. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8122. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8123. {
  8124. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8125. UnicodeString Ext;
  8126. if (!IsSideLocalBrowser(osCurrent))
  8127. {
  8128. Ext = UnixExtractFileExt(FileName);
  8129. }
  8130. else
  8131. {
  8132. Ext = ExtractFileExt(FileName);
  8133. }
  8134. if (Ext.IsEmpty())
  8135. {
  8136. Ext = L".";
  8137. }
  8138. TFileFilter Filter;
  8139. Filter.Masks = FORMAT(L"*%s", (Ext));
  8140. Filter.Directories = false;
  8141. CurrentDirView->SelectFiles(Filter, Select);
  8142. }
  8143. }
  8144. //---------------------------------------------------------------------------
  8145. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8146. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8147. {
  8148. UnicodeString Result;
  8149. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8150. {
  8151. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8152. }
  8153. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8154. {
  8155. // noop
  8156. }
  8157. else
  8158. {
  8159. Result =
  8160. FMTLOAD(FILE_INFO_FORMAT,
  8161. (FormatBytes(FileInfo.SelectedSize),
  8162. FormatBytes(FileInfo.FilesSize),
  8163. FormatNumber(FileInfo.SelectedCount),
  8164. FormatNumber(FileInfo.FilesCount)));
  8165. }
  8166. return Result;
  8167. }
  8168. //---------------------------------------------------------------------------
  8169. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8170. TTBXStatusPanel * Panel, TOperationSide Side)
  8171. {
  8172. if (Panel->Index == 1)
  8173. {
  8174. ToggleShowHiddenFiles();
  8175. }
  8176. else if (Panel->Index == 2)
  8177. {
  8178. Filter(Side);
  8179. }
  8180. }
  8181. //---------------------------------------------------------------------------
  8182. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8183. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8184. {
  8185. DebugAssert(!StatusBar->SimplePanel);
  8186. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8187. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8188. }
  8189. //---------------------------------------------------------------------------
  8190. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8191. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8192. {
  8193. DebugAssert(StatusBar->Panels->Count >= 3);
  8194. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8195. if (FileInfo.HiddenCount > 0)
  8196. {
  8197. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8198. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8199. }
  8200. else
  8201. {
  8202. HiddenFilesPanel->ViewPriority = 0;
  8203. // not really necessary, just to cleanup no-longer-valid data
  8204. HiddenFilesPanel->Caption = L"";
  8205. }
  8206. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8207. if (FileInfo.FilteredCount > 0)
  8208. {
  8209. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8210. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8211. }
  8212. else
  8213. {
  8214. FilteredFilesPanel->ViewPriority = 0;
  8215. // not really necessary, just to cleanup no-longer-valid data
  8216. FilteredFilesPanel->Caption = L"";
  8217. }
  8218. }
  8219. //---------------------------------------------------------------------------
  8220. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8221. TObject * /*Sender*/)
  8222. {
  8223. if (DirView(osOther)->Enabled)
  8224. {
  8225. DirView(osOther)->SetFocus();
  8226. }
  8227. }
  8228. //---------------------------------------------------------------------------
  8229. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8230. {
  8231. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8232. switch (Config.Show)
  8233. {
  8234. case qvShow:
  8235. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8236. {
  8237. Config.Show = qvHide;
  8238. }
  8239. else
  8240. {
  8241. Config.Show = Config.LastHideShow;
  8242. }
  8243. break;
  8244. case qvHideWhenEmpty:
  8245. if (ComponentVisible[fcQueueView])
  8246. {
  8247. Config.Show = qvHide;
  8248. }
  8249. else
  8250. {
  8251. Config.LastHideShow = Config.Show;
  8252. Config.Show = qvShow;
  8253. }
  8254. break;
  8255. case qvHide:
  8256. Config.LastHideShow = Config.Show;
  8257. Config.Show = qvShow;
  8258. break;
  8259. }
  8260. WinConfiguration->QueueView = Config;
  8261. }
  8262. //---------------------------------------------------------------------------
  8263. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8264. {
  8265. UnicodeString Result;
  8266. if (Terminal != NULL)
  8267. {
  8268. switch (WinConfiguration->PathInCaption)
  8269. {
  8270. case picShort:
  8271. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8272. break;
  8273. case picFull:
  8274. Result = Terminal->CurrentDirectory;
  8275. break;
  8276. }
  8277. }
  8278. return Result;
  8279. }
  8280. //---------------------------------------------------------------------------
  8281. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8282. {
  8283. TColor Result = GetWindowColor(FSessionColor);
  8284. return Result;
  8285. }
  8286. //---------------------------------------------------------------------------
  8287. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8288. {
  8289. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8290. return Result;
  8291. }
  8292. //---------------------------------------------------------------------------
  8293. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8294. {
  8295. if (!FSessionChanging)
  8296. {
  8297. TTerminalManager::Instance()->UpdateAppTitle();
  8298. }
  8299. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8300. // background to gray on Windows 7 (works on Windows 10).
  8301. // See also EnableControl
  8302. if (Showing)
  8303. {
  8304. if (ManagedSession != NULL)
  8305. {
  8306. // Update path when it changes
  8307. if ((SessionsPageControl->ActivePage != NULL) &&
  8308. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8309. {
  8310. UpdateSessionTab(SessionsPageControl->ActivePage);
  8311. }
  8312. }
  8313. NonVisualDataModule->ReconnectSessionAction->Update();
  8314. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8315. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8316. CenterReconnectToolbar();
  8317. bool HasTerminal = HasActiveTerminal();
  8318. RemoteDirView->Enabled = HasTerminal;
  8319. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8320. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8321. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8322. RemoteDriveView->Color = RemoteDirView->Color;
  8323. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8324. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8325. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8326. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8327. QueueFileList->Enabled = QueueView3->Enabled;
  8328. QueueFileList->Color = QueueView3->Color;
  8329. QueueFileList->Font->Color = QueueView3->Font->Color;
  8330. QueueLabelUpdateStatus();
  8331. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8332. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8333. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8334. {
  8335. UpdateDarkMode();
  8336. }
  8337. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8338. }
  8339. }
  8340. //---------------------------------------------------------------------------
  8341. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8342. {
  8343. UpdateControls();
  8344. }
  8345. //---------------------------------------------------------------------------
  8346. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8347. TObject * Sender)
  8348. {
  8349. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8350. DebugAssert(DirView != NULL);
  8351. DoDirViewLoaded(DirView);
  8352. TransferPresetAutoSelect();
  8353. }
  8354. //---------------------------------------------------------------------------
  8355. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8356. {
  8357. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8358. // first run after installation
  8359. if (double(Updates.LastCheck) == 0)
  8360. {
  8361. // make sure next time there will be an update (if enabled)
  8362. Updates.LastCheck = TDateTime(1);
  8363. WinConfiguration->Updates = Updates;
  8364. }
  8365. else if ((double(Updates.Period) > 0) &&
  8366. (Now() - Updates.LastCheck >= Updates.Period))
  8367. {
  8368. TThreadMethod OnUpdatesChecked = NULL;
  8369. if (!IsUWP())
  8370. {
  8371. OnUpdatesChecked = UpdatesChecked;
  8372. }
  8373. StartUpdateThread(OnUpdatesChecked);
  8374. }
  8375. }
  8376. //---------------------------------------------------------------------------
  8377. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8378. {
  8379. UnicodeString Message;
  8380. bool New;
  8381. TQueryType Type;
  8382. GetUpdatesMessage(Message, New, Type, false);
  8383. if (!Message.IsEmpty())
  8384. {
  8385. if (New)
  8386. {
  8387. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8388. }
  8389. if (!New && (Type != qtWarning))
  8390. {
  8391. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8392. }
  8393. else
  8394. {
  8395. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8396. }
  8397. if (New)
  8398. {
  8399. Configuration->Usage->Inc(L"UpdateNotifications");
  8400. }
  8401. }
  8402. }
  8403. //---------------------------------------------------------------------------
  8404. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8405. {
  8406. RestoreApp();
  8407. bool CanDisplay = !NonVisualDataModule->Busy;
  8408. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8409. {
  8410. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8411. }
  8412. if (CanDisplay)
  8413. {
  8414. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8415. CheckForUpdates(true);
  8416. }
  8417. else
  8418. {
  8419. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8420. }
  8421. }
  8422. //---------------------------------------------------------------------------
  8423. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8424. TCopyParamRuleData & Data)
  8425. {
  8426. DebugAssert(Terminal != NULL);
  8427. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8428. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8429. Data.RemoteDirectory = RemoteDirView->PathName;
  8430. }
  8431. //---------------------------------------------------------------------------
  8432. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8433. {
  8434. // Terminal can be null when we are changing local directory implicitly,
  8435. // because it has been deleted, while no session is connected
  8436. // (Login dialog is open)
  8437. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8438. {
  8439. DebugAssert(!IsLocalBrowserMode());
  8440. TCopyParamRuleData Data;
  8441. GetTransferPresetAutoSelectData(Data);
  8442. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8443. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8444. if (CopyParamIndex < 0)
  8445. {
  8446. // there is no preset that matches autoselection
  8447. // set preset that we consider "default"
  8448. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8449. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8450. }
  8451. else
  8452. {
  8453. // there is preset matching autoselection
  8454. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8455. if (CopyParamName == FCopyParamAutoSelected)
  8456. {
  8457. // autoselected the same preset as the last time
  8458. // make no change (i.e. preserve custom user preset, if any)
  8459. }
  8460. else
  8461. {
  8462. // autoselected the different preset then the last time (or there
  8463. // was default preset set)
  8464. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8465. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8466. }
  8467. }
  8468. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8469. {
  8470. if (CopyParamIndex >= 0)
  8471. {
  8472. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8473. }
  8474. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8475. try
  8476. {
  8477. int Fmt =
  8478. (CopyParamIndex < 0) ?
  8479. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8480. COPY_PARAM_AUTOSELECTED;
  8481. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8482. Data->Message = MainInstructions(Message);
  8483. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8484. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8485. CopyParamAttrs);
  8486. if (CopyParamIndex >= 0)
  8487. {
  8488. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8489. Info = FORMAT(L"%s\n \n%s", (Info,
  8490. FMTLOAD(COPY_PARAM_RULE,
  8491. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8492. }
  8493. Data->Message += L"\n\n" + Info;
  8494. if (WinConfiguration->CopyParamAutoSelectNotice)
  8495. {
  8496. TransferPresetNoteMessage(Data, true);
  8497. }
  8498. else
  8499. {
  8500. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8501. Data = NULL; // ownership passed
  8502. }
  8503. }
  8504. __finally
  8505. {
  8506. delete Data;
  8507. }
  8508. }
  8509. }
  8510. }
  8511. //---------------------------------------------------------------------------
  8512. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8513. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8514. {
  8515. DebugAssert(NoteData != NULL);
  8516. DebugAssert(!IsLocalBrowserMode());
  8517. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8518. TQueryButtonAlias Aliases[1];
  8519. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8520. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8521. Params.Aliases = Aliases;
  8522. Params.AliasesCount = LENOF(Aliases);
  8523. unsigned int Result =
  8524. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8525. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8526. switch (Result)
  8527. {
  8528. case qaNeverAskAgain:
  8529. DebugAssert(AllowNeverAskAgain);
  8530. WinConfiguration->CopyParamAutoSelectNotice = false;
  8531. break;
  8532. case qaIgnore:
  8533. PreferencesDialog(pmPresets);
  8534. break;
  8535. }
  8536. }
  8537. //---------------------------------------------------------------------------
  8538. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8539. {
  8540. // as of now useless, as this is used for notes only, never for balloons, ...
  8541. RestoreApp();
  8542. // .. and we should never be busy here
  8543. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8544. {
  8545. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8546. }
  8547. }
  8548. //---------------------------------------------------------------------------
  8549. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8550. TPreferencesMode APreferencesMode)
  8551. {
  8552. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8553. TCopyParamRuleData Data;
  8554. if (Terminal != NULL)
  8555. {
  8556. PreferencesData.reset(new TPreferencesDialogData());
  8557. GetTransferPresetAutoSelectData(Data);
  8558. PreferencesData->CopyParamRuleData = &Data;
  8559. }
  8560. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8561. }
  8562. //---------------------------------------------------------------------------
  8563. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8564. const TCustomCommandType & Command)
  8565. {
  8566. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8567. {
  8568. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8569. }
  8570. }
  8571. //---------------------------------------------------------------------------
  8572. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8573. {
  8574. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8575. TCustomCommandType Command;
  8576. // make sure we use local custom command when remote are not supported
  8577. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8578. {
  8579. Command = FLastCustomCommand;
  8580. }
  8581. else
  8582. {
  8583. Command.Params = Command.Params | ccLocal;
  8584. }
  8585. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8586. FEditingFocusedAdHocCommand = OnFocused;
  8587. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8588. int Options =
  8589. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8590. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8591. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8592. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8593. {
  8594. FLastCustomCommand = Command;
  8595. UpdateCustomCommandsToolbar();
  8596. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8597. }
  8598. }
  8599. //---------------------------------------------------------------------------
  8600. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8601. {
  8602. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8603. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8604. DebugAssert(State > 0);
  8605. if (State <= 0)
  8606. {
  8607. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8608. }
  8609. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8610. }
  8611. //---------------------------------------------------------------------------
  8612. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8613. TCustomCommandType & Command, int & State)
  8614. {
  8615. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8616. if (Result)
  8617. {
  8618. Command = FLastCustomCommand;
  8619. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8620. }
  8621. return Result;
  8622. }
  8623. //---------------------------------------------------------------------------
  8624. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8625. {
  8626. ResetIncrementalSearch();
  8627. }
  8628. //---------------------------------------------------------------------------
  8629. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8630. {
  8631. DebugAssert(ComponentVisible[Component]);
  8632. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8633. }
  8634. //---------------------------------------------------------------------------
  8635. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8636. {
  8637. // when queue panel is resized here directly, the status bar is stretched
  8638. // over whole space the panel occupied
  8639. PostComponentHide(fcQueueView);
  8640. }
  8641. //---------------------------------------------------------------------------
  8642. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8643. {
  8644. ComponentVisible[fcQueueFileList] = false;
  8645. }
  8646. //---------------------------------------------------------------------------
  8647. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8648. {
  8649. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8650. RefreshColorMode();
  8651. ResetSysDarkTheme();
  8652. ConfigurationChanged();
  8653. ConfigureInterface();
  8654. // Should be called for all controls
  8655. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8656. }
  8657. //---------------------------------------------------------------------------
  8658. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8659. {
  8660. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8661. if (!FInvalid &&
  8662. (Message.LParam != 0) &&
  8663. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8664. {
  8665. ThemeChanged();
  8666. }
  8667. TForm::Dispatch(&Message);
  8668. }
  8669. //---------------------------------------------------------------------------
  8670. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8671. {
  8672. TMessage * M = static_cast<TMessage*>(Message);
  8673. switch (M->Msg)
  8674. {
  8675. case CM_DIALOGCHAR:
  8676. CMDialogChar(*M);
  8677. break;
  8678. case WM_APPCOMMAND:
  8679. WMAppCommand(*M);
  8680. break;
  8681. case WM_SYSCOMMAND:
  8682. WMSysCommand(*M);
  8683. break;
  8684. case WM_QUERYENDSESSION:
  8685. WMQueryEndSession(*M);
  8686. break;
  8687. case WM_ENDSESSION:
  8688. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8689. break;
  8690. #ifdef _DEBUG
  8691. case WM_WINDOWPOSCHANGED:
  8692. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8693. break;
  8694. case CM_CUSTOMSTYLECHANGED:
  8695. TForm::Dispatch(Message);
  8696. break;
  8697. #endif
  8698. case WM_COMPONENT_HIDE:
  8699. {
  8700. Byte Component = static_cast<Byte>(M->WParam);
  8701. // sanity check
  8702. if (ComponentVisible[Component])
  8703. {
  8704. // special treatment
  8705. if (Component == fcQueueView)
  8706. {
  8707. ToggleQueueVisibility();
  8708. DebugAssert(!ComponentVisible[fcQueueView]);
  8709. }
  8710. else
  8711. {
  8712. ComponentVisible[Component] = false;
  8713. }
  8714. }
  8715. }
  8716. break;
  8717. case WM_COPYDATA:
  8718. WMCopyData(*M);
  8719. break;
  8720. case WM_MANAGES_CAPTION:
  8721. // caption managed in TTerminalManager::UpdateAppTitle()
  8722. M->Result = 1;
  8723. break;
  8724. case WM_WANTS_MOUSEWHEEL:
  8725. M->Result = 1;
  8726. break;
  8727. case CM_SHOWINGCHANGED:
  8728. CMShowingChanged(*M);
  8729. break;
  8730. case WM_CLOSE:
  8731. WMClose(*M);
  8732. break;
  8733. case WM_DPICHANGED:
  8734. WMDpiChanged(*M);
  8735. break;
  8736. case CM_DPICHANGED:
  8737. CMDpiChanged(*M);
  8738. break;
  8739. case WM_SETTINGCHANGE:
  8740. WMSettingChange(*M);
  8741. break;
  8742. case CM_DIALOGKEY:
  8743. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8744. break;
  8745. default:
  8746. TForm::Dispatch(Message);
  8747. break;
  8748. }
  8749. }
  8750. //---------------------------------------------------------------------------
  8751. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8752. {
  8753. // noop
  8754. }
  8755. //---------------------------------------------------------------------------
  8756. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8757. {
  8758. TForm::Dispatch(&Message);
  8759. FSessionColors->Clear();
  8760. AddFixedSessionImages();
  8761. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8762. UpdateImages();
  8763. }
  8764. //---------------------------------------------------------------------------
  8765. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8766. {
  8767. TForm::Dispatch(&Message);
  8768. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8769. UpdatePixelsPerInchMainWindowCounter();
  8770. }
  8771. //---------------------------------------------------------------------------
  8772. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8773. {
  8774. // Cannot close window while we are busy.
  8775. // We cannot test this in FormCloseQuery as that is called also from
  8776. // Close(), which is called by CloseApplicationAction. So we can be busy
  8777. // there already even, when it is legitimate to close the application.
  8778. // Possibly a better place to handle this would be WMSysCommand.
  8779. if (NonVisualDataModule->Busy)
  8780. {
  8781. Message.Result = 1;
  8782. }
  8783. else
  8784. {
  8785. TForm::Dispatch(&Message);
  8786. }
  8787. }
  8788. //---------------------------------------------------------------------------
  8789. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8790. {
  8791. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8792. bool NoSession = (ManagedSession == NULL);
  8793. // This calls TCustomScpExplorerForm::DoShow
  8794. TForm::Dispatch(&Message);
  8795. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8796. if (Showing && !FStarted)
  8797. {
  8798. FStarted = true;
  8799. InterfaceStarted();
  8800. }
  8801. if (Showing && NoSession)
  8802. {
  8803. // When we are starting minimized (i.e. from an installer),
  8804. // postpone showing Login dialog until we get restored.
  8805. // Otherwise the Login dialog (and Authentication window) show restored
  8806. // over invisible (minimized) main window.
  8807. if (WindowState == wsMinimized)
  8808. {
  8809. FNeedSession = true;
  8810. }
  8811. else
  8812. {
  8813. // This happens before application ever goes idle, so the toolbars would
  8814. // stay enabled (initial state) until the Login dialog is dismissed.
  8815. UpdateActions();
  8816. NonVisualDataModule->StartBusy();
  8817. try
  8818. {
  8819. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8820. // otherwise the dialog does not receive focus.
  8821. // With Commander interface the ProcessMessages is called already
  8822. // by TDriveView, but with Explorer interface, we need to call it explicily
  8823. Application->ProcessMessages();
  8824. // do not reload sessions, they have been loaded just now (optimization)
  8825. NeedSession(true);
  8826. }
  8827. __finally
  8828. {
  8829. NonVisualDataModule->EndBusy();
  8830. }
  8831. }
  8832. }
  8833. }
  8834. //---------------------------------------------------------------------------
  8835. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8836. {
  8837. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8838. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8839. }
  8840. //---------------------------------------------------------------------------
  8841. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8842. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8843. int & MaxHeight)
  8844. {
  8845. // workaround for bug in TWinControl.CalcConstraints
  8846. // Check for empty rect (restore from iconic state) is done there only after
  8847. // call to AdjustClientRect, which enlarges the rect (for forms).
  8848. TRect R = GetClientRect();
  8849. // when restoring from iconic state, place no restrictions
  8850. if (IsRectEmpty(R))
  8851. {
  8852. MinWidth = 0;
  8853. MinHeight = 0;
  8854. MaxWidth = 32000;
  8855. MaxHeight = 32000;
  8856. }
  8857. }
  8858. //---------------------------------------------------------------------------
  8859. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8860. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8861. {
  8862. // terminal can be already closed (e.g. dropped connection)
  8863. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8864. {
  8865. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8866. try
  8867. {
  8868. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8869. }
  8870. catch(...)
  8871. {
  8872. if (!Terminal->Active)
  8873. {
  8874. Close = true;
  8875. }
  8876. throw;
  8877. }
  8878. }
  8879. }
  8880. //---------------------------------------------------------------------------
  8881. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8882. {
  8883. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8884. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8885. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8886. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8887. {
  8888. OnGetSpaceAvailable = GetSpaceAvailable;
  8889. }
  8890. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8891. OnGetSpaceAvailable);
  8892. }
  8893. //---------------------------------------------------------------------------
  8894. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8895. {
  8896. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8897. Terminal->FillSessionDataForCode(Data.get());
  8898. return Data.release();
  8899. }
  8900. //---------------------------------------------------------------------------
  8901. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8902. {
  8903. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8904. DoGenerateUrlDialog(Data.get(), Paths);
  8905. }
  8906. //---------------------------------------------------------------------------
  8907. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8908. {
  8909. GenerateUrl(NULL);
  8910. }
  8911. //---------------------------------------------------------------------------
  8912. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8913. {
  8914. std::unique_ptr<TStrings> Paths(new TStringList());
  8915. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8916. GenerateUrl(Paths.get());
  8917. }
  8918. //---------------------------------------------------------------------------
  8919. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8920. {
  8921. FSessionColor = value;
  8922. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8923. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8924. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8925. DebugAssert(ColorItem != NULL);
  8926. ColorItem->Color = C;
  8927. NonVisualDataModule->ColorMenuItem->Color = C;
  8928. UpdateControls();
  8929. }
  8930. //---------------------------------------------------------------------------
  8931. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8932. {
  8933. if (value != FSessionColor)
  8934. {
  8935. UpdateSessionColor(value);
  8936. // Is null when called from LastTerminalClosed
  8937. if (Terminal != NULL)
  8938. {
  8939. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8940. }
  8941. }
  8942. }
  8943. //---------------------------------------------------------------------------
  8944. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8945. {
  8946. bool Result = FNoteTimer->Enabled;
  8947. if (Result)
  8948. {
  8949. // cannot cancel note too early
  8950. bool NotEarly =
  8951. (Now() - FNoteShown >
  8952. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8953. if (Force || NotEarly)
  8954. {
  8955. FNoteTimer->Enabled = false;
  8956. FNote = L"";
  8957. SAFE_DESTROY(FNoteData);
  8958. FOnNoteClick = NULL;
  8959. FNoteHints = FNotes->Text;
  8960. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8961. UpdateStatusBar();
  8962. }
  8963. }
  8964. return Result;
  8965. }
  8966. //---------------------------------------------------------------------------
  8967. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8968. {
  8969. DebugAssert(FNoteTimer->Enabled);
  8970. CancelNote(true);
  8971. }
  8972. //---------------------------------------------------------------------------
  8973. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8974. {
  8975. FNoteHints = FNotes->Text.TrimRight();
  8976. }
  8977. //---------------------------------------------------------------------------
  8978. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8979. {
  8980. int P = Note.Pos(L"\n");
  8981. if (P > 0)
  8982. {
  8983. Note.SetLength(P - 1);
  8984. }
  8985. FNotes->Add(FORMAT(L"[%s] %s",
  8986. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8987. while (FNotes->Count > 10)
  8988. {
  8989. FNotes->Delete(0);
  8990. }
  8991. if (UpdateNow)
  8992. {
  8993. UpdateNoteHints();
  8994. UpdateStatusBar();
  8995. }
  8996. }
  8997. //---------------------------------------------------------------------------
  8998. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8999. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9000. {
  9001. int P = Note.Pos(L"\n");
  9002. if (P > 0)
  9003. {
  9004. Note.SetLength(P - 1);
  9005. }
  9006. UpdateNoteHints();
  9007. FNote = Note;
  9008. SAFE_DESTROY(FNoteData);
  9009. FNoteData = NoteData;
  9010. FOnNoteClick = OnNoteClick;
  9011. AddNote(Note, false);
  9012. UpdateStatusBar();
  9013. FNoteShown = Now();
  9014. FNoteTimer->Enabled = false;
  9015. if (Seconds == 0)
  9016. {
  9017. Seconds = WinConfiguration->NotificationsTimeout;
  9018. }
  9019. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9020. FNoteTimer->Enabled = true;
  9021. }
  9022. //---------------------------------------------------------------------------
  9023. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9024. {
  9025. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9026. }
  9027. //---------------------------------------------------------------------------
  9028. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9029. {
  9030. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9031. {
  9032. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9033. }
  9034. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9035. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9036. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9037. 0, NULL, NULL);
  9038. }
  9039. //---------------------------------------------------------------------------
  9040. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9041. {
  9042. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9043. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9044. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9045. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9046. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9047. }
  9048. //---------------------------------------------------------------------------
  9049. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9050. {
  9051. WinConfiguration->FormatSizeBytes = Style;
  9052. }
  9053. //---------------------------------------------------------------------------
  9054. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9055. {
  9056. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9057. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9058. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9059. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9060. }
  9061. //---------------------------------------------------------------------------
  9062. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9063. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9064. {
  9065. if (Panel->Index == 0)
  9066. {
  9067. if (FOnNoteClick != NULL)
  9068. {
  9069. // prevent the user data from being freed by possible call
  9070. // to CancelNote or PostNote during call to OnNoteClick
  9071. std::unique_ptr<TObject> NoteData(FNoteData);
  9072. TNotifyEvent OnNoteClick = FOnNoteClick;
  9073. FNoteData = NULL;
  9074. // need to cancel the note as we are going to delete its user data
  9075. CancelNote(true);
  9076. OnNoteClick(NoteData.get());
  9077. }
  9078. }
  9079. if (HasActiveTerminal() &&
  9080. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9081. {
  9082. FileSystemInfo();
  9083. }
  9084. }
  9085. //---------------------------------------------------------------------------
  9086. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9087. {
  9088. // workaround:
  9089. // 1) for unknown reason, disabling window, while minimized,
  9090. // prevents it from restoring, even if it was enabled again meanwhile
  9091. // 2) when disabling the main window, while another has focus
  9092. // minimize is no longer possible
  9093. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9094. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9095. // Shouldn't we use IsApplicationMinimized() here?
  9096. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9097. {
  9098. Enabled = false;
  9099. }
  9100. FLockLevel++;
  9101. }
  9102. //---------------------------------------------------------------------------
  9103. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9104. {
  9105. DebugAssert(FLockLevel > 0);
  9106. FLockLevel--;
  9107. if (FLockLevel == 0)
  9108. {
  9109. DebugAssert(FLockSuspendLevel == 0);
  9110. Enabled = true;
  9111. }
  9112. }
  9113. //---------------------------------------------------------------------------
  9114. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9115. {
  9116. // We need to make sure that window is enabled when the last modal window closes
  9117. // otherwise focus is not restored correctly.
  9118. // So we re-enable the window when modal window opens and
  9119. // disable it back after is closes
  9120. if (FLockLevel > 0)
  9121. {
  9122. // while we have nesting counter, we know that we never be called
  9123. // recursivelly as Application->OnModalBegin is called only
  9124. // for the top-level modal window
  9125. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9126. {
  9127. // won't be disabled when conditions in LockWindow() were not satisfied
  9128. FDisabledOnLockSuspend = !Enabled;
  9129. // When minimized to tray (or actually when set to SW_HIDE),
  9130. // setting Enabled makes the window focusable even when there's
  9131. // modal window over it
  9132. if (!FTrayIcon->Visible)
  9133. {
  9134. Enabled = true;
  9135. }
  9136. }
  9137. FLockSuspendLevel++;
  9138. }
  9139. }
  9140. //---------------------------------------------------------------------------
  9141. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9142. {
  9143. if (FLockSuspendLevel > 0)
  9144. {
  9145. DebugAssert(FLockLevel > 0);
  9146. FLockSuspendLevel--;
  9147. // see comment in SuspendWindowLock
  9148. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9149. {
  9150. // Note that window can be enabled here, when we were minized to tray when
  9151. // was SuspendWindowLock() called.
  9152. // We should possibly do the same check as in LockWindow(),
  9153. // if it is ever possible that the consitions change between
  9154. // SuspendWindowLock() and ResumeWindowLock()
  9155. Enabled = !FDisabledOnLockSuspend;
  9156. }
  9157. }
  9158. }
  9159. //---------------------------------------------------------------------------
  9160. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9161. {
  9162. TTBXComboBoxItem * RemotePathComboBox =
  9163. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9164. if (!TextOnly)
  9165. {
  9166. TStrings * Items = RemotePathComboBox->Strings;
  9167. Items->BeginUpdate();
  9168. try
  9169. {
  9170. Items->Clear();
  9171. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9172. {
  9173. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9174. while (!IsUnixRootPath(APath))
  9175. {
  9176. int P = APath.LastDelimiter(L'/');
  9177. DebugAssert(P >= 0);
  9178. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9179. APath.SetLength(P - 1);
  9180. }
  9181. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9182. }
  9183. }
  9184. __finally
  9185. {
  9186. Items->EndUpdate();
  9187. }
  9188. }
  9189. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9190. // Setting ItemIndex to -1 does not reset its text
  9191. if (RemotePathComboBox->Strings->Count == 0)
  9192. {
  9193. RemotePathComboBox->Text = L"";
  9194. }
  9195. }
  9196. //---------------------------------------------------------------------------
  9197. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9198. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9199. {
  9200. if (AIndex < 0)
  9201. {
  9202. AIndex = Sender->ItemIndex;
  9203. }
  9204. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9205. }
  9206. //---------------------------------------------------------------------------
  9207. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9208. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9209. int & ImageIndex)
  9210. {
  9211. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9212. }
  9213. //---------------------------------------------------------------------------
  9214. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9215. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9216. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9217. {
  9218. if (!IsLocalBrowserMode())
  9219. {
  9220. ARect.Left += (10 * AIndex);
  9221. }
  9222. }
  9223. //---------------------------------------------------------------------------
  9224. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9225. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9226. {
  9227. AWidth += (10 * AIndex);
  9228. }
  9229. //---------------------------------------------------------------------------
  9230. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9231. {
  9232. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9233. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9234. int Index = RemotePathComboBox->ItemIndex;
  9235. while (Index < RemotePathComboBox->Strings->Count - 1)
  9236. {
  9237. APath = UnixExtractFileDir(APath);
  9238. Index++;
  9239. }
  9240. // VanDyke style paths
  9241. if (APath.IsEmpty())
  9242. {
  9243. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9244. APath = ROOTDIRECTORY;
  9245. }
  9246. if (RemoteDirView->Path != APath)
  9247. {
  9248. RemoteDirView->Path = APath;
  9249. }
  9250. }
  9251. //---------------------------------------------------------------------------
  9252. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9253. {
  9254. DoRemotePathComboBoxItemClick(Sender);
  9255. }
  9256. //---------------------------------------------------------------------------
  9257. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9258. {
  9259. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9260. DebugUsedParam(Sender);
  9261. UpdateRemotePathComboBox(true);
  9262. }
  9263. //---------------------------------------------------------------------------
  9264. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9265. {
  9266. DoRemotePathComboBoxCancel(Sender);
  9267. }
  9268. //---------------------------------------------------------------------------
  9269. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9270. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9271. {
  9272. ResetIncrementalSearch();
  9273. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9274. DebugAssert(DirView != NULL);
  9275. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9276. {
  9277. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9278. // OnEditing is called also from TCustomListView::CanEdit
  9279. if (Edit != NULL)
  9280. {
  9281. EditSelectBaseName(Edit);
  9282. }
  9283. }
  9284. }
  9285. //---------------------------------------------------------------------------
  9286. //---------------------------------------------------------------------------
  9287. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9288. {
  9289. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9290. Result->AutoDetectDnD = false;
  9291. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9292. Result->RenderDataOn = rdoEnterAndDropSync;
  9293. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9294. return Result;
  9295. }
  9296. //---------------------------------------------------------------------------
  9297. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9298. {
  9299. if (IsWin10Build(2004))
  9300. {
  9301. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9302. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9303. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9304. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9305. }
  9306. }
  9307. //---------------------------------------------------------------------------
  9308. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9309. {
  9310. TForm::CreateWnd();
  9311. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9312. UpdateDarkMode();
  9313. if (FSessionsDragDropFilesEx == NULL)
  9314. {
  9315. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9316. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9317. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9318. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9319. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9320. }
  9321. if (FQueueDragDropFilesEx == NULL)
  9322. {
  9323. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9324. // No need to set OnDragOver as we do not have any restrictions
  9325. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9326. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9327. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9328. }
  9329. }
  9330. //---------------------------------------------------------------------------
  9331. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9332. {
  9333. TForm::DestroyWnd();
  9334. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9335. FQueueDragDropFilesEx->DragDropControl = NULL;
  9336. }
  9337. //---------------------------------------------------------------------------
  9338. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9339. {
  9340. SideEnter(FCurrentSide);
  9341. FEverShown = true;
  9342. }
  9343. //---------------------------------------------------------------------------
  9344. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9345. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9346. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9347. {
  9348. if (!NonVisualDataModule->Busy)
  9349. {
  9350. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9351. Configuration->Usage->Inc(L"FileFinds");
  9352. LockWindow(true);
  9353. NonVisualDataModule->StartBusy();
  9354. WinConfiguration->LockedInterface = true;
  9355. try
  9356. {
  9357. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9358. }
  9359. __finally
  9360. {
  9361. WinConfiguration->LockedInterface = false;
  9362. NonVisualDataModule->EndBusy();
  9363. UnlockWindow();
  9364. }
  9365. }
  9366. }
  9367. //---------------------------------------------------------------------------
  9368. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9369. {
  9370. if (!NonVisualDataModule->Busy)
  9371. {
  9372. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9373. SetFocus();
  9374. RemoteDirView->Path = UnixExtractFilePath(Path);
  9375. UnicodeString FileName = UnixExtractFileName(Path);
  9376. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9377. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9378. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9379. if (Item == NULL)
  9380. {
  9381. ReloadDirectory(osRemote);
  9382. Item = RemoteDirView->FindFileItem(FileName);
  9383. }
  9384. if (Item != NULL)
  9385. {
  9386. RemoteDirView->ItemFocused = Item;
  9387. RemoteDirView->ItemFocused->MakeVisible(false);
  9388. RemoteDirView->SetFocus();
  9389. }
  9390. }
  9391. }
  9392. //---------------------------------------------------------------------------
  9393. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9394. {
  9395. bool Result = !NonVisualDataModule->Busy;
  9396. if (Result)
  9397. {
  9398. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9399. }
  9400. return Result;
  9401. }
  9402. //---------------------------------------------------------------------------
  9403. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9404. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9405. {
  9406. if (CanOperateOnFoundFiles(ATerminal))
  9407. {
  9408. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9409. FOnFileOperationFinished = OnFileOperationFinished;
  9410. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9411. }
  9412. }
  9413. //---------------------------------------------------------------------------
  9414. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9415. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9416. {
  9417. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9418. }
  9419. //---------------------------------------------------------------------------
  9420. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9421. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9422. {
  9423. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9424. }
  9425. //---------------------------------------------------------------------------
  9426. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9427. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9428. {
  9429. if (CanOperateOnFoundFiles(ATerminal))
  9430. {
  9431. for (int Index = 0; Index < FileList->Count; Index++)
  9432. {
  9433. UnicodeString FileName = FileList->Strings[Index];
  9434. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9435. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9436. OnFileOperationFinished(osRemote, FileName, true);
  9437. }
  9438. }
  9439. }
  9440. //---------------------------------------------------------------------------
  9441. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9442. {
  9443. FFileFindTerminal = Terminal;
  9444. ShowFileFindDialog(
  9445. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9446. DoDownloadFoundFiles, DoEditFoundFiles);
  9447. }
  9448. //---------------------------------------------------------------------------
  9449. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9450. {
  9451. FTaskbarList = TaskbarList;
  9452. }
  9453. //---------------------------------------------------------------------------
  9454. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9455. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9456. {
  9457. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9458. if (Index >= 0)
  9459. {
  9460. if (Button == mbLeft)
  9461. {
  9462. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9463. // If switching tab (switching session) takes long enough for user
  9464. // to actually release the button, "mouse down" is still raised,
  9465. // but we do not get "mouse up" event, so dragging is not cancelled,
  9466. // prevent that by not beginning dragging in the first place.
  9467. if (IsKeyPressed(VK_LBUTTON))
  9468. {
  9469. // when user clicks the "+", we get mouse down only after the session
  9470. // is closed, when new session tab is already on X:Y, so dragging
  9471. // starts, prevent that
  9472. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9473. {
  9474. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9475. if (Terminal != NULL)
  9476. {
  9477. SessionsPageControl->BeginDrag(false);
  9478. }
  9479. }
  9480. }
  9481. }
  9482. else if (Button == mbMiddle)
  9483. {
  9484. // ignore middle-click for "New session tab" and the last local-local tab
  9485. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9486. if ((Session != NULL) && CanCloseSession(Session))
  9487. {
  9488. CloseSessionTab(Index);
  9489. }
  9490. }
  9491. }
  9492. }
  9493. //---------------------------------------------------------------------------
  9494. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9495. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9496. {
  9497. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9498. // do not allow dropping on the "+" tab
  9499. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9500. if ((TargetTerminal != NULL) &&
  9501. (SessionsPageControl->ActivePage->PageIndex != Index))
  9502. {
  9503. Configuration->Usage->Inc(L"SessionTabMoves");
  9504. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9505. // but we want to actually prevent that to avoid flicker
  9506. SessionsPageControl->ActivePage->PageIndex = Index;
  9507. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9508. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9509. }
  9510. }
  9511. //---------------------------------------------------------------------------
  9512. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9513. TObject * Sender, TObject * Source, int X, int Y,
  9514. TDragState /*State*/, bool & Accept)
  9515. {
  9516. Accept = (Sender == Source);
  9517. if (Accept)
  9518. {
  9519. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9520. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9521. // do not allow dragging to the "+" tab
  9522. Accept = (Terminal != NULL);
  9523. }
  9524. }
  9525. //---------------------------------------------------------------------------
  9526. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9527. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9528. {
  9529. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9530. if (Index < 0)
  9531. {
  9532. Effect = DROPEFFECT_NONE;
  9533. }
  9534. else
  9535. {
  9536. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9537. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9538. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9539. {
  9540. Effect = DROPEFFECT_NONE;
  9541. }
  9542. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9543. {
  9544. Effect = DROPEFFECT_COPY;
  9545. }
  9546. }
  9547. }
  9548. //---------------------------------------------------------------------------
  9549. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9550. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9551. {
  9552. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9553. // do not allow dropping on local-local, disconnected and the "+" tab
  9554. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9555. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9556. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9557. {
  9558. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9559. if (!IsFileControl(DropSourceControl, osRemote))
  9560. {
  9561. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9562. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9563. // Why don't we use Terminal->CurrentDirectory directly?
  9564. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9565. // do not force queue, drag drop
  9566. false, true);
  9567. }
  9568. }
  9569. }
  9570. //---------------------------------------------------------------------------
  9571. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9572. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9573. {
  9574. // Downloads are handled in RemoteFileControlDDEnd
  9575. if (!IsFileControl(DropSourceControl, osRemote))
  9576. {
  9577. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9578. Terminal->CurrentDirectory,
  9579. // force queue, drag drop
  9580. true, true);
  9581. }
  9582. }
  9583. //---------------------------------------------------------------------------
  9584. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9585. {
  9586. FShowing = false;
  9587. // Do not save empty workspace
  9588. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9589. {
  9590. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9591. UnicodeString Name = WorkspaceName();
  9592. DoSaveWorkspace(
  9593. Name, DataList.get(),
  9594. !Configuration->DisablePasswordStoring &&
  9595. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9596. WinConfiguration->LastStoredSession = Name;
  9597. }
  9598. }
  9599. //---------------------------------------------------------------------------
  9600. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9601. {
  9602. TManagedTerminal * ManagedTerminal =
  9603. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9604. if (ManagedTerminal != NULL)
  9605. {
  9606. ManagedTerminal->DirectoryLoaded = Now();
  9607. }
  9608. }
  9609. //---------------------------------------------------------------------------
  9610. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9611. TListItem * /*Item*/, bool /*Selected*/)
  9612. {
  9613. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9614. switch (DirView->LastSelectMethod)
  9615. {
  9616. case smKeyboard:
  9617. Configuration->Usage->Inc(L"KeyboardSelections");
  9618. break;
  9619. case smMouse:
  9620. Configuration->Usage->Inc(L"MouseSelections");
  9621. break;
  9622. }
  9623. }
  9624. //---------------------------------------------------------------------------
  9625. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9626. {
  9627. if (Color != 0)
  9628. {
  9629. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9630. }
  9631. else
  9632. {
  9633. return FSessionTabImageIndex;
  9634. }
  9635. }
  9636. //---------------------------------------------------------------------------
  9637. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9638. {
  9639. TPngImageCollectionItem * Item =
  9640. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9641. FSessionColors->AddPng(Item->PngImage);
  9642. return FSessionColors->Count - 1;
  9643. }
  9644. //---------------------------------------------------------------------------
  9645. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9646. {
  9647. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9648. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9649. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9650. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9651. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9652. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9653. }
  9654. //---------------------------------------------------------------------------
  9655. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9656. {
  9657. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9658. }
  9659. //---------------------------------------------------------------------------
  9660. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9661. {
  9662. switch (Side)
  9663. {
  9664. case osLocal:
  9665. return FNewLocalTabTabImageIndex;
  9666. case osRemote:
  9667. case osCurrent:
  9668. return FNewRemoteTabTabImageIndex;
  9669. default:
  9670. DebugFail();
  9671. return -1;
  9672. }
  9673. }
  9674. //---------------------------------------------------------------------------
  9675. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9676. {
  9677. for (int Index = 0; Index < Control->ControlCount; Index++)
  9678. {
  9679. TControl * AControl = Control->Controls[Index];
  9680. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9681. if (WinControl != NULL)
  9682. {
  9683. CollectItemsWithTextDisplayMode(WinControl);
  9684. }
  9685. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9686. if (Toolbar != NULL)
  9687. {
  9688. // we care for top-level items only
  9689. TTBCustomItem * Items = Toolbar->Items;
  9690. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9691. {
  9692. TTBCustomItem * Item = Items->Items[ItemIndex];
  9693. if (((Item->DisplayMode == nbdmImageAndText) ||
  9694. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9695. EligibleForImageDisplayMode(Item))
  9696. {
  9697. FItemsWithTextDisplayMode.insert(Item);
  9698. }
  9699. }
  9700. }
  9701. }
  9702. }
  9703. //---------------------------------------------------------------------------
  9704. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9705. {
  9706. return true;
  9707. }
  9708. //---------------------------------------------------------------------------
  9709. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9710. {
  9711. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9712. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9713. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9714. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9715. bool Result = true;
  9716. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9717. {
  9718. TTBCustomItem * Item = *i;
  9719. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9720. if (Label != NULL)
  9721. {
  9722. // optimization
  9723. if (Label->Visible == SelectiveToolbarText)
  9724. {
  9725. Result = false;
  9726. }
  9727. else
  9728. {
  9729. Label->Visible = SelectiveToolbarText;
  9730. }
  9731. }
  9732. else
  9733. {
  9734. // optimization
  9735. if (Item->DisplayMode == DisplayMode)
  9736. {
  9737. Result = false;
  9738. }
  9739. else
  9740. {
  9741. Item->DisplayMode = DisplayMode;
  9742. }
  9743. }
  9744. ++i;
  9745. }
  9746. UpdateNewTabTab();
  9747. return Result;
  9748. }
  9749. //---------------------------------------------------------------------------
  9750. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9751. {
  9752. DebugFail();
  9753. }
  9754. //---------------------------------------------------------------------------
  9755. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9756. {
  9757. // Among other, a lock level is non zero, while directory is being loaded,
  9758. // even when it happens as a result of dirview navigation,
  9759. // i.e. when TNonVisualDataModule is NOT FBusy.
  9760. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9761. // Among other this prevents a panel auto update to occur while
  9762. // directory is changing.
  9763. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9764. }
  9765. //---------------------------------------------------------------------------
  9766. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9767. {
  9768. // While the window is disabled, we still seem to process menu shortcuts at least,
  9769. // so stop it at least here.
  9770. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9771. bool Result;
  9772. if (Allowed == aaUpdate)
  9773. {
  9774. Result = true;
  9775. }
  9776. else
  9777. {
  9778. Result = !NonVisualDataModule->Busy;
  9779. if (Result && (Allowed == aaShortCut))
  9780. {
  9781. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9782. Action->Update();
  9783. Result = Action->Enabled;
  9784. }
  9785. }
  9786. return Result;
  9787. }
  9788. //---------------------------------------------------------------------------
  9789. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9790. {
  9791. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9792. }
  9793. //---------------------------------------------------------------------------
  9794. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9795. {
  9796. // This is somewhat redundant to LockWindow() call from
  9797. // TTerminalManager::TerminalReadDirectoryProgress.
  9798. // But disabling window is known not to block keyboard shorcuts
  9799. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9800. if (Busy > 0)
  9801. {
  9802. if (NonVisualDataModule->Busy)
  9803. {
  9804. State = false;
  9805. }
  9806. else
  9807. {
  9808. NonVisualDataModule->StartBusy();
  9809. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9810. LockWindow();
  9811. }
  9812. }
  9813. else if (Busy < 0)
  9814. {
  9815. UnlockWindow();
  9816. NonVisualDataModule->EndBusy();
  9817. }
  9818. else
  9819. {
  9820. State = NonVisualDataModule->Busy;
  9821. }
  9822. }
  9823. //---------------------------------------------------------------------------
  9824. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9825. {
  9826. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9827. if (Index >= 0)
  9828. {
  9829. TPopupMenu * PopupMenu = NULL;
  9830. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9831. if (Session != NULL)
  9832. {
  9833. SessionsPageControl->ActivePageIndex = Index;
  9834. if (DebugAlwaysTrue(SessionTabSwitched()))
  9835. {
  9836. // copied from TControl.WMContextMenu
  9837. SendCancelMode(SessionsPageControl);
  9838. // explicit popup instead of using PopupMenu property
  9839. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9840. // while connecting yet not-connected session and hence
  9841. // allowing an access to commands over not-completelly connected session
  9842. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9843. }
  9844. }
  9845. else
  9846. {
  9847. if (SupportsLocalBrowser())
  9848. {
  9849. PopupMenu = NonVisualDataModule->NewTabPopup;
  9850. }
  9851. }
  9852. if (PopupMenu != NULL)
  9853. {
  9854. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9855. PopupMenu->PopupComponent = SessionsPageControl;
  9856. PopupMenu->Popup(Point.x, Point.y);
  9857. }
  9858. }
  9859. Handled = true;
  9860. }
  9861. //---------------------------------------------------------------------------
  9862. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9863. {
  9864. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9865. }
  9866. //---------------------------------------------------------------------------
  9867. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9868. {
  9869. Configuration->Usage->Inc(L"PasswordChanges");
  9870. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9871. ChangePasswordData->DisableAuthentationsExceptPassword();
  9872. ChangePasswordData->ChangePassword = true;
  9873. std::unique_ptr<TTerminal> ChangePasswordSession(
  9874. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9875. ChangePasswordSession->AutoReadDirectory = false;
  9876. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9877. {
  9878. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9879. }
  9880. else
  9881. {
  9882. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9883. }
  9884. }
  9885. //---------------------------------------------------------------------------
  9886. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9887. {
  9888. // No nice way to assert SSH2
  9889. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9890. }
  9891. //---------------------------------------------------------------------------
  9892. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9893. {
  9894. TOperationVisualizer OperationVisualizer;
  9895. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9896. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9897. }
  9898. //---------------------------------------------------------------------------
  9899. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9900. {
  9901. TForm::ChangeScale(M, D);
  9902. int APixelsPerInch = GetControlPixelsPerInch(this);
  9903. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9904. try
  9905. {
  9906. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9907. }
  9908. __finally
  9909. {
  9910. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9911. }
  9912. }
  9913. //---------------------------------------------------------------------------
  9914. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9915. {
  9916. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9917. }
  9918. //---------------------------------------------------------------------
  9919. class TPublicControl : public TControl
  9920. {
  9921. friend class TCustomScpExplorerForm;
  9922. };
  9923. //---------------------------------------------------------------------------
  9924. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9925. {
  9926. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9927. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9928. }
  9929. //---------------------------------------------------------------------------
  9930. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9931. {
  9932. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9933. OpenBookmark(Side, Bookmark);
  9934. }
  9935. //---------------------------------------------------------------------------
  9936. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9937. {
  9938. DoBookmarkClick(osLocal, Sender);
  9939. }
  9940. //---------------------------------------------------------------------------
  9941. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9942. {
  9943. DoBookmarkClick(osRemote, Sender);
  9944. }
  9945. //---------------------------------------------------------------------------
  9946. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9947. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9948. {
  9949. if (BookmarkList != NULL)
  9950. {
  9951. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9952. if (IsLocalBrowserMode())
  9953. {
  9954. Side = osLocal;
  9955. }
  9956. if (!WinConfiguration->UseLocationProfiles)
  9957. {
  9958. std::unique_ptr<TStringList> Directories(new TStringList());
  9959. Directories->CaseSensitive = (Side == osRemote);
  9960. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9961. {
  9962. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9963. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9964. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9965. {
  9966. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9967. Item->Caption = EscapeHotkey(Directory);
  9968. Item->ShortCut = Bookmark->ShortCut;
  9969. Item->OnClick = OnBookmarkClick;
  9970. Item->Tag = reinterpret_cast<int>(Bookmark);
  9971. Directories->Add(Directory);
  9972. Menu->Add(Item.release());
  9973. }
  9974. }
  9975. }
  9976. else
  9977. {
  9978. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9979. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9980. {
  9981. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9982. if (!Bookmark->Node.IsEmpty())
  9983. {
  9984. Folders->Add(Bookmark->Node);
  9985. }
  9986. }
  9987. for (int Index = 0; Index < Folders->Count; Index++)
  9988. {
  9989. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9990. Item->Caption = Folders->Strings[Index];
  9991. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  9992. Folders->Objects[Index] = Item.get();
  9993. Menu->Add(Item.release());
  9994. }
  9995. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9996. {
  9997. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9998. TTBCustomItem * Parent;
  9999. if (!Bookmark->Node.IsEmpty())
  10000. {
  10001. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10002. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10003. }
  10004. else
  10005. {
  10006. Parent = Menu;
  10007. }
  10008. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10009. Item->Caption = Bookmark->Name;
  10010. Item->ShortCut = Bookmark->ShortCut;
  10011. Item->OnClick = OnBookmarkClick;
  10012. Item->Tag = reinterpret_cast<int>(Bookmark);
  10013. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10014. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10015. {
  10016. // No hint for location profiles that are actually mere "bookmarks"
  10017. }
  10018. else
  10019. {
  10020. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10021. UnicodeString LongHint;
  10022. if (!Bookmark->Local.IsEmpty())
  10023. {
  10024. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10025. }
  10026. if (!Bookmark->Remote.IsEmpty())
  10027. {
  10028. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10029. }
  10030. AddToList(Hint, LongHint, L"|");
  10031. Item->Hint = Hint;
  10032. }
  10033. Parent->Add(Item.release());
  10034. }
  10035. }
  10036. AddMenuSeparator(Menu);
  10037. }
  10038. }
  10039. //---------------------------------------------------------------------------
  10040. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10041. {
  10042. Menu->Clear();
  10043. std::unique_ptr<TTBCustomItem> Item;
  10044. Item.reset(new TTBXItem(Owner));
  10045. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10046. Item->Options = Item->Options << tboDefault;
  10047. Menu->Add(Item.release());
  10048. AddMenuSeparator(Menu);
  10049. if (Terminal != NULL)
  10050. {
  10051. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10052. }
  10053. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10054. Item.reset(new TTBXItem(Owner));
  10055. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10056. Menu->Add(Item.release());
  10057. }
  10058. //---------------------------------------------------------------------------
  10059. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10060. {
  10061. bool Result = true;
  10062. bool Remote = !IsSideLocalBrowser(Side);
  10063. try
  10064. {
  10065. if (Remote)
  10066. {
  10067. Terminal->ExceptionOnFail = true;
  10068. }
  10069. try
  10070. {
  10071. DirView(Side)->Path = Path;
  10072. }
  10073. __finally
  10074. {
  10075. if (Remote)
  10076. {
  10077. Terminal->ExceptionOnFail = false;
  10078. }
  10079. }
  10080. }
  10081. catch (Exception & E)
  10082. {
  10083. if (!Remote || Terminal->Active)
  10084. {
  10085. ShowExtendedExceptionEx(Terminal, &E);
  10086. Result = false;
  10087. }
  10088. else
  10089. {
  10090. throw;
  10091. }
  10092. }
  10093. return Result;
  10094. }
  10095. //---------------------------------------------------------------------------
  10096. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10097. {
  10098. // Make sure there some feedback even when loading tiny local folders
  10099. TInstantOperationVisualizer Visualizer;
  10100. DirView(Side)->ReloadDirectory();
  10101. }
  10102. //---------------------------------------------------------------------------
  10103. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10104. {
  10105. if (Index == SessionsPageControl->TabIndex)
  10106. {
  10107. CloseSession();
  10108. }
  10109. else
  10110. {
  10111. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10112. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10113. TTerminalManager * Manager = TTerminalManager::Instance();
  10114. if (!Terminal->Active)
  10115. {
  10116. Manager->FreeTerminal(Terminal);
  10117. }
  10118. else
  10119. {
  10120. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10121. if (CanCloseQueue(Queue))
  10122. {
  10123. Manager->FreeTerminal(Terminal);
  10124. }
  10125. }
  10126. }
  10127. }
  10128. //---------------------------------------------------------------------------
  10129. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10130. {
  10131. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10132. if (Session == NULL)
  10133. {
  10134. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10135. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10136. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10137. //P = SessionsPageControl->ClientToScreen(P);
  10138. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10139. }
  10140. else
  10141. {
  10142. CloseSessionTab(Index);
  10143. }
  10144. }
  10145. //---------------------------------------------------------------------------
  10146. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10147. {
  10148. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10149. {
  10150. TInstantOperationVisualizer Visualizer;
  10151. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10152. // before we create a new data object
  10153. ClipboardClear();
  10154. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10155. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10156. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10157. // Intermediate variable to WORKAROUND a crash in compiler
  10158. TObjectList * ClipboardFakeMonitors =
  10159. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10160. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10161. if (FClipboardDragDropFilesEx.get() == NULL)
  10162. {
  10163. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10164. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10165. }
  10166. FClipboardDragDropFilesEx->FileList->Clear();
  10167. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10168. FClipboardDragDropFilesEx->CopyToClipboard();
  10169. FClipboardTerminal = Terminal;
  10170. // Need full paths, as cwd can be different once files are pasted
  10171. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10172. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10173. {
  10174. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10175. }
  10176. }
  10177. }
  10178. //---------------------------------------------------------------------------
  10179. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10180. {
  10181. if (OpenClipboard(0))
  10182. {
  10183. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10184. CloseClipboard();
  10185. }
  10186. }
  10187. //---------------------------------------------------------------------------
  10188. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10189. {
  10190. return !FClipboardFakeDirectory.IsEmpty();
  10191. }
  10192. //---------------------------------------------------------------------------
  10193. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10194. {
  10195. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10196. FClipboardFakeDirectory = UnicodeString();
  10197. FClipboardTerminal = NULL;
  10198. // Also called in Idle()
  10199. FClipboardFakeMonitors.reset(NULL);
  10200. }
  10201. //---------------------------------------------------------------------------
  10202. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10203. {
  10204. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10205. {
  10206. ClipboardStop();
  10207. }
  10208. }
  10209. //---------------------------------------------------------------------------
  10210. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10211. {
  10212. TTerminalManager * Manager = TTerminalManager::Instance();
  10213. Manager->ActiveSession = FClipboardTerminal;
  10214. TTransferOperationParam Params;
  10215. Params.TargetDirectory = TargetDirectory;
  10216. Params.DragDrop = DragDrop;
  10217. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10218. }
  10219. //---------------------------------------------------------------------------
  10220. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10221. {
  10222. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10223. // (as it pumps a Windows message queue while being shutted down)
  10224. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10225. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10226. {
  10227. // Can fail as it can be e.g. locked by AV, so we retry that later
  10228. bool Removed = RemoveDir(ApiPath(FileName));
  10229. try
  10230. {
  10231. if (!NonVisualDataModule->Busy)
  10232. {
  10233. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10234. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10235. }
  10236. }
  10237. __finally
  10238. {
  10239. if (!Removed)
  10240. {
  10241. RemoveDir(ApiPath(FileName));
  10242. }
  10243. }
  10244. }
  10245. }
  10246. //---------------------------------------------------------------------------
  10247. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10248. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10249. {
  10250. TFileMasks::TParams MaskParams;
  10251. MaskParams.Size = Size;
  10252. MaskParams.Modification = Modification;
  10253. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10254. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10255. {
  10256. bool ImplicitMatch;
  10257. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10258. !ImplicitMatch)
  10259. {
  10260. Color = Iter->Color;
  10261. break;
  10262. }
  10263. }
  10264. }
  10265. //---------------------------------------------------------------------------
  10266. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10267. {
  10268. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10269. if (!DirView->IsEditing())
  10270. {
  10271. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10272. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10273. // while searching
  10274. if (Key == VK_BACK)
  10275. {
  10276. if (!FIncrementalSearch.IsEmpty())
  10277. {
  10278. if (FIncrementalSearch.Length() == 1)
  10279. {
  10280. ResetIncrementalSearch();
  10281. }
  10282. else
  10283. {
  10284. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10285. IncrementalSearch(NewText, false, false);
  10286. }
  10287. Key = 0;
  10288. }
  10289. }
  10290. }
  10291. }
  10292. //---------------------------------------------------------------------------
  10293. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10294. {
  10295. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10296. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10297. {
  10298. // Filter control sequences.
  10299. // When not searching yet, prefer use of the space for toggling file selection
  10300. // (so we cannot incrementally search for a string starting with a space).
  10301. if ((Key > VK_SPACE) ||
  10302. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10303. {
  10304. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10305. Key = 0;
  10306. }
  10307. }
  10308. }
  10309. //---------------------------------------------------------------------------
  10310. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10311. {
  10312. if (!FIncrementalSearch.IsEmpty())
  10313. {
  10314. FIncrementalSearch = L"";
  10315. DirView(osCurrent)->UpdateStatusBar();
  10316. }
  10317. }
  10318. //---------------------------------------------------------------------------
  10319. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10320. {
  10321. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10322. if (Item == NULL)
  10323. {
  10324. MessageBeep(MB_ICONHAND);
  10325. }
  10326. else
  10327. {
  10328. TCustomDirView * ADirView = DirView(osCurrent);
  10329. {
  10330. TAutoNestingCounter Guard(FIncrementalSearching);
  10331. ADirView->FocusItem(Item);
  10332. if (ADirView->NortonLike == nlOff)
  10333. {
  10334. if ((ADirView->Selected != Item) ||
  10335. (ADirView->SelCount > 1))
  10336. {
  10337. ADirView->ClearSelection();
  10338. ADirView->Selected = Item;
  10339. }
  10340. }
  10341. }
  10342. FIncrementalSearch = Text;
  10343. Item->MakeVisible(false);
  10344. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10345. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10346. ADirView->UpdateStatusBar();
  10347. }
  10348. }
  10349. //---------------------------------------------------------------------------
  10350. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10351. {
  10352. int Index = Item->Index;
  10353. if (!Reverse)
  10354. {
  10355. Index++;
  10356. if (Index >= Item->Owner->Count)
  10357. {
  10358. Index = 0;
  10359. }
  10360. }
  10361. else
  10362. {
  10363. Index--;
  10364. if (Index < 0)
  10365. {
  10366. Index = Item->Owner->Count - 1;
  10367. }
  10368. }
  10369. return Item->Owner->Item[Index];
  10370. }
  10371. //---------------------------------------------------------------------------
  10372. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10373. {
  10374. TCustomDirView * ADirView = DirView(osCurrent);
  10375. if (ADirView->Items->Count == 0)
  10376. {
  10377. return NULL;
  10378. }
  10379. else
  10380. {
  10381. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10382. TListItem * Item = CurrentItem;
  10383. if (SkipCurrent)
  10384. {
  10385. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10386. }
  10387. while (true)
  10388. {
  10389. bool Matches = false;
  10390. switch (WinConfiguration->PanelSearch)
  10391. {
  10392. case isNameStartOnly:
  10393. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10394. break;
  10395. case isName:
  10396. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10397. break;
  10398. case isAll:
  10399. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10400. int Index = 0;
  10401. while ((Index < ColCount) && !Matches)
  10402. {
  10403. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10404. if (Visible)
  10405. {
  10406. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10407. }
  10408. Index++;
  10409. }
  10410. break;
  10411. }
  10412. if (Matches)
  10413. {
  10414. return Item;
  10415. }
  10416. Item = GetNextFile(Item, Reverse);
  10417. if (Item == CurrentItem)
  10418. {
  10419. return NULL;
  10420. }
  10421. }
  10422. }
  10423. }
  10424. //---------------------------------------------------------------------------
  10425. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10426. {
  10427. ResetIncrementalSearch();
  10428. }
  10429. //---------------------------------------------------------------------------
  10430. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10431. {
  10432. if (Message.CharCode == VK_TAB)
  10433. {
  10434. if (!FIncrementalSearch.IsEmpty())
  10435. {
  10436. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10437. bool Reverse = Shift.Contains(ssShift);
  10438. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10439. Message.Result = 1;
  10440. return;
  10441. }
  10442. }
  10443. else if (Message.CharCode == VK_ESCAPE)
  10444. {
  10445. if (!FIncrementalSearch.IsEmpty())
  10446. {
  10447. ResetIncrementalSearch();
  10448. Message.Result = 1;
  10449. return;
  10450. }
  10451. }
  10452. TForm::Dispatch(&Message);
  10453. }
  10454. //---------------------------------------------------------------------------
  10455. void __fastcall TCustomScpExplorerForm::Deactivate()
  10456. {
  10457. TForm::Deactivate();
  10458. // E.g. when switching to an internal editor window
  10459. ResetIncrementalSearch();
  10460. }
  10461. //---------------------------------------------------------------------------
  10462. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10463. {
  10464. // When switching to another application
  10465. ResetIncrementalSearch();
  10466. }
  10467. //---------------------------------------------------------------------------
  10468. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10469. {
  10470. ResetIncrementalSearch();
  10471. }
  10472. //---------------------------------------------------------------------------
  10473. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10474. {
  10475. if (FIncrementalSearching <= 0)
  10476. {
  10477. ResetIncrementalSearch();
  10478. }
  10479. }
  10480. //---------------------------------------------------------------------------
  10481. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10482. {
  10483. CountClicksForWindowPrint(this);
  10484. }
  10485. //---------------------------------------------------------------------------
  10486. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10487. {
  10488. CenterReconnectToolbar();
  10489. }
  10490. //---------------------------------------------------------------------------
  10491. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10492. {
  10493. if (RemoteDirView->ItemFocused != NULL)
  10494. {
  10495. RemoteDirView->ItemFocused->Selected = true;
  10496. }
  10497. }
  10498. //---------------------------------------------------------------------------
  10499. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10500. {
  10501. if (!FQueueStatusUpdating)
  10502. {
  10503. bool Refresh;
  10504. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10505. {
  10506. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10507. }
  10508. else
  10509. {
  10510. if (FQueueFileList->GetCount() > 0)
  10511. {
  10512. FQueueFileList->Clear();
  10513. Refresh = true;
  10514. }
  10515. }
  10516. int Count = FQueueFileList->GetCount();
  10517. if (QueueFileList->Items->Count != Count)
  10518. {
  10519. DebugAssert(Refresh);
  10520. QueueFileList->Items->Count = Count;
  10521. }
  10522. if (Refresh)
  10523. {
  10524. QueueFileList->Invalidate();
  10525. }
  10526. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10527. }
  10528. }
  10529. //---------------------------------------------------------------------------
  10530. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10531. {
  10532. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10533. }
  10534. //---------------------------------------------------------------------------
  10535. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10536. {
  10537. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10538. // See TSynchronizeChecklistDialog::UpdateTimer
  10539. if (QueueFileList->Items->Count > 0)
  10540. {
  10541. QueueFileList->Items->Item[0]->MakeVisible(false);
  10542. }
  10543. UpdateQueueFileList();
  10544. }
  10545. //---------------------------------------------------------------------------
  10546. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10547. {
  10548. Active = QueueView3->Focused() || QueueFileList->Focused();
  10549. }
  10550. //---------------------------------------------------------------------------
  10551. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10552. {
  10553. QueueLabelUpdateStatus();
  10554. }
  10555. //---------------------------------------------------------------------------
  10556. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10557. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10558. {
  10559. int State = FQueueFileList->GetState(Item->Index);
  10560. if (State == qfsProcessed)
  10561. {
  10562. Sender->Canvas->Font->Color = clGrayText;
  10563. }
  10564. }
  10565. //---------------------------------------------------------------------------
  10566. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10567. {
  10568. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10569. QueueFileList->Column[0]->Width =
  10570. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10571. }
  10572. //---------------------------------------------------------------------------
  10573. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10574. {
  10575. QueueFileListColumnAutoSize();
  10576. }
  10577. //---------------------------------------------------------------------------
  10578. void __fastcall TCustomScpExplorerForm::CloseApp()
  10579. {
  10580. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10581. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10582. // So we clear the flag here.
  10583. DebugAssert(NonVisualDataModule->Busy);
  10584. NonVisualDataModule->EndBusy();
  10585. try
  10586. {
  10587. Close();
  10588. }
  10589. __finally
  10590. {
  10591. NonVisualDataModule->StartBusy();
  10592. }
  10593. }
  10594. //---------------------------------------------------------------------------
  10595. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10596. {
  10597. return (Terminal != NULL) && Terminal->Active;
  10598. }
  10599. //---------------------------------------------------------------------------
  10600. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10601. {
  10602. return (ManagedSession != NULL);
  10603. }
  10604. //---------------------------------------------------------------------------
  10605. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10606. {
  10607. return IsActiveTerminal(Terminal);
  10608. }
  10609. //---------------------------------------------------------------------------
  10610. void TCustomScpExplorerForm::LocalLocalCopy(
  10611. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10612. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10613. {
  10614. DebugFail();
  10615. }
  10616. //---------------------------------------------------------------------------
  10617. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10618. {
  10619. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10620. }
  10621. //---------------------------------------------------------------------------
  10622. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10623. {
  10624. // Changing tab list triggers the OnResize.
  10625. if (!FUpdatingSessionTabs)
  10626. {
  10627. // UpdateTabsCaptionTruncation should be enough
  10628. SessionListChanged(true);
  10629. }
  10630. }
  10631. //---------------------------------------------------------------------------
  10632. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10633. {
  10634. if (ASession == ManagedSession)
  10635. {
  10636. UpdateSession(ASession);
  10637. }
  10638. TSessionData * Data = ASession->StateData;
  10639. UnicodeString Result;
  10640. if (Side == osLocal)
  10641. {
  10642. Result = Data->LocalDirectory;
  10643. }
  10644. else if (DebugAlwaysTrue(Side == osOther))
  10645. {
  10646. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10647. }
  10648. return Result;
  10649. }
  10650. //---------------------------------------------------------------------------
  10651. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10652. {
  10653. UnicodeString Result;
  10654. if (!ASession->Active)
  10655. {
  10656. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10657. }
  10658. else
  10659. {
  10660. Result = GetSessionPath(ASession, osRemote);
  10661. }
  10662. return Result;
  10663. }
  10664. //---------------------------------------------------------------------------
  10665. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10666. {
  10667. return LoadStr(NEW_REMOTE_TAB_HINT);
  10668. }
  10669. //---------------------------------------------------------------------------
  10670. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10671. {
  10672. if (Index >= 0)
  10673. {
  10674. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10675. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10676. if (TabSession == NULL)
  10677. {
  10678. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10679. if (NewTabTabCaption != TabSheet->Caption)
  10680. {
  10681. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10682. }
  10683. }
  10684. else
  10685. {
  10686. TTerminalManager * Manager = TTerminalManager::Instance();
  10687. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10688. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10689. }
  10690. }
  10691. }
  10692. //---------------------------------------------------------------------------
  10693. typedef std::vector<TListItem *> TListItemsVector;
  10694. struct TCalculateSizeOperation
  10695. {
  10696. TCalculateSizeStats Stats;
  10697. TListItemsVector ListItems;
  10698. size_t Index;
  10699. };
  10700. //---------------------------------------------------------------------------
  10701. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10702. {
  10703. TCalculateSizeOperation CalculateSizeOperation;
  10704. TOperationSide Side = GetSide(osCurrent);
  10705. TCustomDirView * ADirView = DirView(Side);
  10706. bool FullPath = IsSideLocalBrowser(Side);
  10707. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10708. std::unique_ptr<TStrings> FileList(new TStringList());
  10709. for (int Index = 0; Index < AllFileList->Count; Index++)
  10710. {
  10711. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10712. if (ADirView->ItemIsDirectory(Item))
  10713. {
  10714. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10715. CalculateSizeOperation.ListItems.push_back(Item);
  10716. }
  10717. }
  10718. CalculateSizeOperation.Index = 0;
  10719. __int64 Size = 0;
  10720. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10721. FCalculateSizeOperation = &CalculateSizeOperation;
  10722. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10723. FOnFileOperationFinished = DirectorySizeCalculated;
  10724. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10725. FPrimaryOperation = foCalculateSize;
  10726. TCalculatedSizes CalculatedSizes;
  10727. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10728. if (Side == osLocal)
  10729. {
  10730. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10731. }
  10732. else
  10733. {
  10734. TCalculateSizeParams Params;
  10735. Params.Stats = &CalculateSizeOperation.Stats;
  10736. Params.UseCache = true;
  10737. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10738. }
  10739. }
  10740. //---------------------------------------------------------------------------
  10741. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10742. {
  10743. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10744. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10745. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10746. // optimization
  10747. bool GotExpectedItem =
  10748. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10749. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10750. int Index = -1;
  10751. if (DebugAlwaysTrue(GotExpectedItem))
  10752. {
  10753. Index = FCalculateSizeOperation->Index;
  10754. FCalculateSizeOperation->Index++;
  10755. }
  10756. else
  10757. {
  10758. TListItemsVector::iterator I =
  10759. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  10760. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  10761. {
  10762. Index = I - FCalculateSizeOperation->ListItems.begin();
  10763. }
  10764. }
  10765. if (DebugAlwaysTrue(Index >= 0))
  10766. {
  10767. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  10768. ADirView->SetItemCalculatedSize(Item, Size);
  10769. }
  10770. }