CustomScpExplorer.cpp 390 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. AddStartupSequence(L"F");
  146. FInvalid = false;
  147. FCurrentSide = osRemote;
  148. FEverShown = false;
  149. FDocks = new TList();
  150. InitControls();
  151. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  152. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  153. NonVisualDataModule->ScpExplorer = this;
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. FAutoOperation = false;
  158. FOnFileOperationFinished = NULL;
  159. FPrimaryOperation = foNone;
  160. FForceExecution = false;
  161. FIgnoreNextDialogChar = 0;
  162. FErrorList = NULL;
  163. FSynchronizeProgressForm = NULL;
  164. FProgressForm = NULL;
  165. FRefreshLocalDirectory = false;
  166. FRefreshRemoteDirectory = false;
  167. FDDExtMapFile = NULL;
  168. // CreateMutexW keeps failing with ERROR_NOACCESS
  169. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  170. DebugAssert(FDDExtMutex != NULL);
  171. FDDTargetControl = NULL;
  172. FDelayedDeletionTimer = NULL;
  173. FDelayedDeletionList = new TStringList();
  174. FDDFileList = NULL;
  175. FPendingTempSpaceWarn = false;
  176. FCapturedLog = NULL;
  177. FDragDropOperation = false;
  178. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  179. FAllowTransferPresetAutoSelect = true;
  180. FSessionChanging = false;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FDeletedFiles = NULL;
  194. FMoveToQueue = false;
  195. StandaloneOperation = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FOnSynchronizeAbort = NULL;
  198. FSynchronizeTerminal = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FUserActionTimer = new TTimer(this);
  222. FUserActionTimer->Enabled = false;
  223. FUserActionTimer->Interval = 10;
  224. FUserActionTimer->OnTimer = UserActionTimer;
  225. FNotes = new TStringList();
  226. FNoteTimer = new TTimer(this);
  227. FNoteTimer->Enabled = false;
  228. FNoteTimer->OnTimer = NoteTimer;
  229. FOnNoteClick = NULL;
  230. FOle32Library = LoadLibrary(L"Ole32.dll");
  231. FDragMoveCursor = FOle32Library != NULL ?
  232. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  233. UseSystemSettings(this);
  234. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  235. static_cast<TObject*>(GetComponent(fcTransferList)));
  236. DebugAssert(TransferList != NULL);
  237. FTransferListHoverIndex = -1;
  238. TransferList->OnChange = TransferListChange;
  239. TransferList->OnDrawItem = TransferListDrawItem;
  240. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  241. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  242. UseDesktopFont(SessionsPageControl);
  243. UpdateSessionsPageControlHeight();
  244. UseDesktopFont(RemoteDirView);
  245. UseDesktopFont(RemoteDriveView);
  246. UseDesktopFont(QueueView3);
  247. UseDesktopFont(QueueFileList);
  248. UseDesktopFont(QueueLabel);
  249. UseDesktopFont(RemoteStatusBar);
  250. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  251. QueueSplitter->ShowHint = true;
  252. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  253. QueueFileListSplitter->ShowHint = true;
  254. RemotePanelSplitter->ShowHint = true;
  255. UpdateImages();
  256. FCustomCommandMenu = CreateTBXPopupMenu(this);
  257. FCustomCommandLocalFileList = NULL;
  258. FCustomCommandRemoteFileList = NULL;
  259. FSessionColors = new TPngImageList(this);
  260. FSessionColors->ColorDepth = cd32Bit;
  261. AddFixedSessionImages();
  262. SessionsPageControl->Images = FSessionColors;
  263. UpdateQueueLabel();
  264. CheckStoreTransition();
  265. CreateHiddenWindow();
  266. StartUpdates();
  267. }
  268. //---------------------------------------------------------------------------
  269. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  270. {
  271. FInvalid = true;
  272. // this has to be one of the very first things to do
  273. StopUpdateThread();
  274. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  275. TTerminalManager::Instance()->ScpExplorer = NULL;
  276. delete FCustomCommandLocalFileList;
  277. delete FCustomCommandRemoteFileList;
  278. delete FCustomCommandMenu;
  279. delete FTrayIcon;
  280. FTrayIcon = NULL;
  281. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  282. delete FEditorManager;
  283. ForceCloseLocalEditors();
  284. delete FLocalEditors;
  285. HideFileFindDialog();
  286. if (FDelayedDeletionTimer)
  287. {
  288. DoDelayedDeletion(NULL);
  289. SAFE_DESTROY(FDelayedDeletionTimer);
  290. }
  291. SAFE_DESTROY(FDelayedDeletionList);
  292. // sometimes we do not get DDEnd so the list is not released
  293. SAFE_DESTROY(FDDFileList);
  294. DebugAssert(FSynchronizeController == NULL);
  295. CloseHandle(FDDExtMutex);
  296. FDDExtMutex = NULL;
  297. FreeLibrary(FOle32Library);
  298. FOle32Library = NULL;
  299. FDragMoveCursor = NULL;
  300. DebugAssert(!FErrorList);
  301. if (FEverShown)
  302. {
  303. // when window is never shown (like when running command-line operation),
  304. // particularly window site is not restored correctly (BoundsRect value set
  305. // in RestoreForm gets lost during handle allocation), so we do not want
  306. // it to be stored
  307. StoreParams();
  308. }
  309. ManagedSession = NULL;
  310. Queue = NULL;
  311. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  312. NonVisualDataModule->ScpExplorer = NULL;
  313. delete FQueueController;
  314. FQueueController = NULL;
  315. delete FQueueStatusSection;
  316. FQueueStatusSection = NULL;
  317. delete FQueueStatus;
  318. FQueueStatus = NULL;
  319. delete FUserActionTimer;
  320. FUserActionTimer = NULL;
  321. delete FNoteTimer;
  322. delete FNotes;
  323. delete FNoteData;
  324. SAFE_DESTROY(FDocks);
  325. SessionsPageControl->Images = NULL;
  326. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  327. SAFE_DESTROY(FSessionColors);
  328. SAFE_DESTROY(FSessionsDragDropFilesEx);
  329. SAFE_DESTROY(FHistoryMenu[0][0]);
  330. SAFE_DESTROY(FHistoryMenu[0][1]);
  331. SAFE_DESTROY(FHistoryMenu[1][0]);
  332. SAFE_DESTROY(FHistoryMenu[1][1]);
  333. if (FHiddenWindow != NULL)
  334. {
  335. DestroyWindow(FHiddenWindow);
  336. FHiddenWindow = NULL;
  337. }
  338. }
  339. //---------------------------------------------------------------------------
  340. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  341. {
  342. std::unique_ptr<TSessionData> Data;
  343. if (!Session.IsEmpty())
  344. {
  345. bool DefaultsOnly;
  346. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  347. }
  348. TTerminalManager * Manager = TTerminalManager::Instance();
  349. for (int Index = 0; Index < Manager->Count; Index++)
  350. {
  351. TTerminal * Terminal = Manager->Sessions[Index];
  352. if (Session.IsEmpty() ||
  353. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  354. {
  355. if (Path.IsEmpty())
  356. {
  357. Terminal->ClearCaches();
  358. }
  359. else
  360. {
  361. Terminal->DirectoryModified(Path, true);
  362. }
  363. }
  364. }
  365. // We should flag a pending refresh for the background terminals or busy foreground terminals
  366. if (HasActiveTerminal() &&
  367. CanCommandLineFromAnotherInstance() &&
  368. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  369. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  370. {
  371. Terminal->ReloadDirectory();
  372. }
  373. }
  374. //---------------------------------------------------------------------------
  375. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  376. {
  377. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  378. size_t MessageSize = sizeof(TCopyDataMessage);
  379. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  380. if (Result)
  381. {
  382. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  383. Result = (Message.Version == TCopyDataMessage::Version1);
  384. if (Result)
  385. {
  386. switch (Message.Command)
  387. {
  388. case TCopyDataMessage::CommandCanCommandLine:
  389. Result = CanCommandLineFromAnotherInstance();
  390. break;
  391. case TCopyDataMessage::CommandCommandLine:
  392. {
  393. UnicodeString CommandLine(Message.CommandLine);
  394. Result = CommandLineFromAnotherInstance(CommandLine);
  395. }
  396. break;
  397. case TCopyDataMessage::RefreshPanel:
  398. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  399. break;
  400. case TCopyDataMessage::MainWindowCheck:
  401. Result = true;
  402. break;
  403. default:
  404. Result = false;
  405. break;
  406. }
  407. }
  408. }
  409. Message.Result = Result ? 1 : 0;
  410. }
  411. //---------------------------------------------------------------------------
  412. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  413. {
  414. WNDCLASS WindowClass = {0};
  415. WindowClass.lpfnWndProc = DefWindowProc;
  416. WindowClass.hInstance = HInstance;
  417. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  418. FHiddenWindow = NULL;
  419. if (RegisterClass(&WindowClass))
  420. {
  421. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  422. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  423. }
  424. }
  425. //---------------------------------------------------------------------------
  426. bool __fastcall TCustomScpExplorerForm::CanConsole()
  427. {
  428. return
  429. HasActiveTerminal() &&
  430. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  434. {
  435. bool Result = Visible && !NonVisualDataModule->Busy;
  436. return Result;
  437. }
  438. //---------------------------------------------------------------------------
  439. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  440. const UnicodeString & CommandLine)
  441. {
  442. TProgramParams Params(CommandLine);
  443. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  444. if (Result)
  445. {
  446. NonVisualDataModule->StartBusy();
  447. try
  448. {
  449. // this action is initiated from another process,
  450. // so it's likely that our window is not visible,
  451. // and user won't see what is going on
  452. Application->BringToFront();
  453. // reload sessions as we may be asked to open a session
  454. // just stored by another instance
  455. StoredSessions->Reload();
  456. UnicodeString SessionName = Params.Param[1];
  457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  458. try
  459. {
  460. UnicodeString DownloadFile; // unused
  461. int Flags = GetCommandLineParseUrlFlags(&Params);
  462. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  463. }
  464. catch (EAbort &)
  465. {
  466. // Happens when opening session in PuTTY and in other situations,
  467. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  468. // to process the command-line on its own (or to pass it to yet another instance)
  469. }
  470. if (DataList->Count > 0)
  471. {
  472. TTerminalManager * Manager = TTerminalManager::Instance();
  473. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  474. }
  475. }
  476. __finally
  477. {
  478. NonVisualDataModule->EndBusy();
  479. }
  480. }
  481. return Result;
  482. }
  483. //---------------------------------------------------------------------------
  484. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  485. {
  486. TManagedTerminal * Result;
  487. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  488. {
  489. Result = FManagedSession;
  490. }
  491. else
  492. {
  493. Result = NULL;
  494. }
  495. return Result;
  496. }
  497. //---------------------------------------------------------------------------
  498. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  499. {
  500. if (FManagedSession != value)
  501. {
  502. SessionChanging();
  503. DoSetManagedSession(value, false);
  504. }
  505. }
  506. //---------------------------------------------------------------------------
  507. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  508. {
  509. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  510. FManagedSession = value;
  511. {
  512. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  513. FAllowTransferPresetAutoSelect = false;
  514. TAutoFlag SessionChangingFlag(FSessionChanging);
  515. SessionChanged(Replace);
  516. }
  517. if (Terminal != NULL)
  518. {
  519. TransferPresetAutoSelect();
  520. }
  521. // Update app and tab titles, prevented above by FSessionChanging.
  522. UpdateControls();
  523. }
  524. //---------------------------------------------------------------------------
  525. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  526. {
  527. DoSetManagedSession(value, true);
  528. }
  529. //---------------------------------------------------------------------------
  530. void __fastcall TCustomScpExplorerForm::SessionChanging()
  531. {
  532. if (ManagedSession != NULL)
  533. {
  534. UpdateSession(ManagedSession);
  535. }
  536. }
  537. //---------------------------------------------------------------------------
  538. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  539. {
  540. if (ManagedSession != NULL)
  541. {
  542. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  543. }
  544. if (!TTerminalManager::Instance()->Updating)
  545. {
  546. SessionListChanged();
  547. }
  548. if (Terminal != NULL)
  549. {
  550. TObject * State = NULL;
  551. if (WinConfiguration->PreservePanelState)
  552. {
  553. State = Terminal->RemoteExplorerState;
  554. DirView(osRemote)->AnnounceState(State);
  555. }
  556. try
  557. {
  558. if (Replaced)
  559. {
  560. RemoteDirView->ReplaceTerminal(Terminal);
  561. }
  562. else
  563. {
  564. RemoteDirView->Terminal = Terminal;
  565. }
  566. if (Terminal->Active)
  567. {
  568. Terminal->RefreshDirectory();
  569. }
  570. if (State != NULL)
  571. {
  572. DirView(osRemote)->RestoreState(State);
  573. }
  574. }
  575. __finally
  576. {
  577. DirView(osRemote)->AnnounceState(NULL);
  578. }
  579. }
  580. else
  581. {
  582. RemoteDirView->Terminal = NULL;
  583. }
  584. InitStatusBar();
  585. UpdateTransferList();
  586. // Update panels Enable state before refreshing the labels
  587. UpdateControls();
  588. }
  589. //---------------------------------------------------------------------------
  590. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  591. {
  592. if (Queue != value)
  593. {
  594. if (FQueue != NULL)
  595. {
  596. FQueue->OnListUpdate = NULL;
  597. FQueue->OnQueueItemUpdate = NULL;
  598. }
  599. FQueue = value;
  600. if (FQueue != NULL)
  601. {
  602. DebugAssert(FQueue->OnListUpdate == NULL);
  603. FQueue->OnListUpdate = QueueListUpdate;
  604. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  605. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  606. }
  607. QueueChanged();
  608. }
  609. }
  610. //---------------------------------------------------------------------------
  611. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  612. TListItem * Item)
  613. {
  614. if (FQueueActedItem == Item)
  615. {
  616. FQueueActedItem = NULL;
  617. if ((QueueView3->PopupMenu != NULL) &&
  618. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  619. {
  620. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  621. ReleaseCapture();
  622. }
  623. }
  624. if (Item->Data == FPendingQueueActionItem)
  625. {
  626. FPendingQueueActionItem = NULL;
  627. }
  628. }
  629. //---------------------------------------------------------------------------
  630. bool TCustomScpExplorerForm::IsAnythingQueued()
  631. {
  632. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  633. }
  634. //---------------------------------------------------------------------------
  635. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  636. {
  637. {
  638. TGuard Guard(FQueueStatusSection);
  639. FQueueStatusInvalidated = false;
  640. if (FQueue != NULL)
  641. {
  642. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  643. }
  644. }
  645. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  646. {
  647. FMaxQueueLength = FQueueStatus->Count;
  648. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  649. }
  650. {
  651. TAutoFlag Flag(FQueueStatusUpdating);
  652. FQueueController->UpdateQueueStatus(FQueueStatus);
  653. }
  654. UpdateQueueFileList();
  655. SetQueueProgress();
  656. UpdateQueueView();
  657. bool IsEmpty = !IsAnythingQueued();
  658. if (IsEmpty && (Terminal != NULL))
  659. {
  660. TOnceDoneOperation OnceDoneOperation =
  661. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  662. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  663. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  664. {
  665. FQueue->Enabled = false;
  666. }
  667. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  668. {
  669. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  670. }
  671. }
  672. }
  673. //---------------------------------------------------------------------------
  674. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  675. {
  676. UnicodeString Result;
  677. if (FQueueStatus != NULL)
  678. {
  679. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  680. {
  681. TFileOperationProgressType * ProgressData =
  682. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  683. if ((ProgressData != NULL) && ProgressData->InProgress)
  684. {
  685. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  686. }
  687. }
  688. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  689. {
  690. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  691. }
  692. }
  693. return Result;
  694. }
  695. //---------------------------------------------------------------------------
  696. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  697. {
  698. ComponentVisible[fcQueueView] =
  699. !IsLocalBrowserMode() &&
  700. ((WinConfiguration->QueueView.Show == qvShow) ||
  701. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  702. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TCustomScpExplorerForm::QueueChanged()
  706. {
  707. if (FQueueStatus != NULL)
  708. {
  709. // UpdateFileList implementation relies on the status being recreated when session changes
  710. delete FQueueStatus;
  711. FQueueStatus = NULL;
  712. }
  713. UpdateQueueStatus(true);
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  717. {
  718. if (FQueue == Queue)
  719. {
  720. FQueueStatusInvalidated = true;
  721. }
  722. }
  723. //---------------------------------------------------------------------------
  724. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  725. TQueueItem * Item)
  726. {
  727. if (FQueue == Queue)
  728. {
  729. TGuard Guard(FQueueStatusSection);
  730. // this may be running in parallel with QueueChanged
  731. if (FQueueStatus != NULL)
  732. {
  733. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  734. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  735. {
  736. FRefreshLocalDirectory = (QueueItem == NULL) ||
  737. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  738. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  739. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  740. }
  741. if (QueueItem != NULL)
  742. {
  743. QueueItem->UserData = (void*)true;
  744. FQueueItemInvalidated = true;
  745. }
  746. }
  747. }
  748. }
  749. //---------------------------------------------------------------------------
  750. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  751. {
  752. // during standalone editing, we have no way to see/control queue,
  753. // so we have to always popup prompts automatically
  754. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  758. {
  759. if (FQueueStatus != NULL)
  760. {
  761. bool QueueAutoPopup = IsQueueAutoPopup();
  762. bool NeedRefresh = FQueueController->NeedRefresh();
  763. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  764. FQueueItemInvalidated = false;
  765. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  766. bool Updated = false;
  767. TQueueItemProxy * QueueItem;
  768. bool WasUserAction;
  769. for (int Index = 0; Index < Limit; Index++)
  770. {
  771. bool Update = false;
  772. QueueItem = FQueueStatus->Items[Index];
  773. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  774. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  775. (FPendingQueueActionItem == NULL))
  776. {
  777. FPendingQueueActionItem = QueueItem;
  778. FUserActionTimer->Enabled = true;
  779. }
  780. if ((bool)QueueItem->UserData)
  781. {
  782. QueueItem->UserData = (void*)false;
  783. QueueItem->Update();
  784. Updated = true;
  785. Update = true;
  786. }
  787. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  788. {
  789. Update = true;
  790. }
  791. if (Update)
  792. {
  793. FQueueController->RefreshQueueItem(QueueItem);
  794. }
  795. }
  796. if (Updated)
  797. {
  798. NonVisualDataModule->UpdateNonVisibleActions();
  799. SetQueueProgress();
  800. UpdateQueueFileList();
  801. }
  802. }
  803. }
  804. //---------------------------------------------------------------------------
  805. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  806. {
  807. if (FTaskbarList != NULL)
  808. {
  809. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  810. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  811. }
  812. }
  813. //---------------------------------------------------------------------------
  814. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  815. {
  816. if (Progress >= 0)
  817. {
  818. if (FTaskbarList != NULL)
  819. {
  820. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  821. }
  822. }
  823. else
  824. {
  825. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  826. }
  827. }
  828. //---------------------------------------------------------------------------
  829. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  830. {
  831. int OverallProgress;
  832. // FProgressForm is null when this is called from SetQueueProgress
  833. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  834. {
  835. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  836. }
  837. else if (!ProgressData->IsIndeterminate())
  838. {
  839. OverallProgress = ProgressData->OverallProgress();
  840. }
  841. else
  842. {
  843. OverallProgress = -1;
  844. }
  845. SetTaskbarListProgressValue(OverallProgress);
  846. }
  847. //---------------------------------------------------------------------------
  848. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  849. {
  850. TTerminalManager::Instance()->QueueStatusUpdated();
  851. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  852. {
  853. // Do not animate task bar button, when items are queued, but processing is disabled
  854. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  855. {
  856. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  857. {
  858. TFileOperationProgressType * ProgressData = NULL;
  859. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  860. {
  861. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  862. }
  863. if ((ProgressData != NULL) &&
  864. ProgressData->InProgress)
  865. {
  866. SetTaskbarListProgressValue(ProgressData);
  867. }
  868. else
  869. {
  870. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  871. }
  872. }
  873. else
  874. {
  875. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  876. }
  877. }
  878. else
  879. {
  880. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  881. }
  882. }
  883. UpdateQueueLabel();
  884. }
  885. //---------------------------------------------------------------------------
  886. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  887. {
  888. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  889. if (FQueueStatus != NULL)
  890. {
  891. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  892. if (ActiveAndPendingPrimaryCount > 0)
  893. {
  894. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  895. }
  896. }
  897. QueueLabel->Caption = Caption;
  898. }
  899. //---------------------------------------------------------------------------
  900. void TCustomScpExplorerForm::CheckStoreTransition()
  901. {
  902. if (WinConfiguration->StoreTransition == stInit)
  903. {
  904. if (IsUWP())
  905. {
  906. WinConfiguration->StoreTransition = stStoreFresh;
  907. AppLog(L"Fresh UWP/store installation");
  908. }
  909. else
  910. {
  911. WinConfiguration->StoreTransition = stStandard;
  912. AppLog(L"Fresh standard installation");
  913. }
  914. }
  915. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  916. {
  917. WinConfiguration->StoreTransition = stStoreMigrated;
  918. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  919. AppLog(L"Standard installation migrated to store installation");
  920. }
  921. if (WinConfiguration->StoreTransition == stStoreMigrated)
  922. {
  923. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  924. Toolbar->Parent = MessageDock;
  925. Toolbar->FullSize = true;
  926. Toolbar->Color = Application->HintColor;
  927. TTBXLabelItem * LabelItem;
  928. LabelItem = new TTBXLabelItem(Toolbar);
  929. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  930. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  931. Toolbar->Items->Add(LabelItem);
  932. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  933. LinkItem->FontSettings->Color = LinkColor;
  934. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  935. LinkItem->OnClick = StoreTransitionLinkClick;
  936. Toolbar->Items->Add(LinkItem);
  937. AddMenuSeparator(Toolbar->Items);
  938. TTBCustomItem * CloseItem = new TTBXItem(Toolbar);
  939. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  940. CloseItem->OnClick = StoreTransitionCloseClick;
  941. Toolbar->Items->Add(CloseItem);
  942. GiveTBItemPriority(CloseItem);
  943. }
  944. }
  945. //---------------------------------------------------------------------------
  946. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  947. {
  948. ShowHelp(HELP_STORE_TRANSITION);
  949. }
  950. //---------------------------------------------------------------------------
  951. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  952. {
  953. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  954. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  955. delete Toolbar;
  956. WinConfiguration->StoreTransition = stStoreAcknowledged;
  957. UpdateControls();
  958. }
  959. //---------------------------------------------------------------------------
  960. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  961. {
  962. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  963. static_cast<TComponent*>(GetComponent(fcTransferList)));
  964. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  965. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  966. TransferList->Strings->BeginUpdate();
  967. try
  968. {
  969. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  970. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  971. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  972. if (FTransferDropDownHint.IsEmpty())
  973. {
  974. FTransferDropDownHint = TransferDropDown->Hint;
  975. }
  976. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  977. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  978. TransferDropDown->Text = StripHotkey(Name);
  979. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  980. (FTransferDropDownHint, StripHotkey(Name),
  981. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  982. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  983. // update the label, otherwise when it is updated only on the first draw
  984. // of the list, it is drawn "bold" for some reason
  985. FTransferListHoverIndex = TransferList->ItemIndex;
  986. UpdateTransferLabel();
  987. }
  988. __finally
  989. {
  990. TransferList->Strings->EndUpdate();
  991. }
  992. }
  993. //---------------------------------------------------------------------------
  994. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  995. {
  996. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  997. DebugAssert(Toolbar != NULL);
  998. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1002. {
  1003. TForm::UpdateActions();
  1004. if (ComponentVisible[fcCustomCommandsBand])
  1005. {
  1006. UpdateCustomCommandsToolbar();
  1007. }
  1008. }
  1009. //---------------------------------------------------------------------------
  1010. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1011. {
  1012. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1013. }
  1014. //---------------------------------------------------------------------------
  1015. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1016. {
  1017. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1018. {
  1019. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1020. // Without this, the panel turns gray and unused part of header turns black
  1021. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1022. }
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1026. {
  1027. Color = GetBtnFaceColor();
  1028. DebugAssert(Configuration && RemoteDirView);
  1029. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1030. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1031. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1032. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1033. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1034. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1035. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1036. UpdateRowSelect(RemoteDirView);
  1037. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1038. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1039. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1040. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1041. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1042. UpdateSessionsPageControlHeight();
  1043. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1044. UpdateToolbarDisplayMode();
  1045. UpdateTransferList();
  1046. if (Terminal != NULL)
  1047. {
  1048. TransferPresetAutoSelect();
  1049. }
  1050. UpdateQueueView();
  1051. UpdateControls();
  1052. // this can be called even before constuctor finishes.
  1053. if (FEditorManager != NULL)
  1054. {
  1055. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1056. }
  1057. // this can be called even before constuctor finishes.
  1058. if (FLocalEditors != NULL)
  1059. {
  1060. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1061. {
  1062. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1063. }
  1064. }
  1065. if (ComponentVisible[fcCustomCommandsBand])
  1066. {
  1067. UpdateCustomCommandsToolbar();
  1068. }
  1069. // show only when keeping queue items forever,
  1070. // otherwise, it's enough to have it in the context menu
  1071. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1072. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1073. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1074. {
  1075. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1076. FFileColorsCurrent = WinConfiguration->FileColors;
  1077. FileColorsChanged();
  1078. }
  1079. }
  1080. //---------------------------------------------------------------------------
  1081. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1082. {
  1083. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1084. DirView->Invalidate();
  1085. }
  1086. //---------------------------------------------------------------------------
  1087. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1088. {
  1089. DoFileColorsChanged(RemoteDirView);
  1090. }
  1091. //---------------------------------------------------------------------------
  1092. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1093. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1094. void * /*Arg*/)
  1095. {
  1096. if (Token != NULL)
  1097. {
  1098. TForm * Editor = dynamic_cast<TForm*>(Token);
  1099. DebugAssert(Editor != NULL);
  1100. ReconfigureEditorForm(Editor);
  1101. }
  1102. }
  1103. //---------------------------------------------------------------------------
  1104. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1105. {
  1106. WinConfiguration->DDTransferConfirmation = asOn;
  1107. }
  1108. //---------------------------------------------------------------------------
  1109. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1110. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1111. {
  1112. // noop
  1113. }
  1114. //---------------------------------------------------------------------------
  1115. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1116. {
  1117. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1121. {
  1122. if (DoNotShowAgain)
  1123. {
  1124. if (DragDrop)
  1125. {
  1126. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1127. {
  1128. PopupTrayBalloon(
  1129. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1130. }
  1131. WinConfiguration->DDTransferConfirmation = asOff;
  1132. }
  1133. else
  1134. {
  1135. WinConfiguration->ConfirmTransferring = false;
  1136. }
  1137. }
  1138. else
  1139. {
  1140. // User explicitly unchecked "do not show again",
  1141. // so show him the dialog the next time
  1142. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1143. {
  1144. WinConfiguration->DDTransferConfirmation = asOn;
  1145. }
  1146. }
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1150. TTransferDirection Direction, TTransferType Type, bool Temp,
  1151. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1152. bool Confirm, bool DragDrop, int Options)
  1153. {
  1154. bool Result = true;
  1155. DebugAssert(HasActiveTerminal());
  1156. // these parameters are known in advance
  1157. int Params =
  1158. FLAGMASK(Type == ttMove, cpDelete);
  1159. bool ToTemp = (Temp && (Direction == tdToLocal));
  1160. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1161. {
  1162. bool DisableNewerOnly =
  1163. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1164. ToTemp;
  1165. Options |=
  1166. FLAGMASK(ToTemp, coTemp) |
  1167. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1168. coDoNotShowAgain;
  1169. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1170. int CopyParamAttrs = (Direction == tdToRemote ?
  1171. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1172. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1173. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1174. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1175. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1176. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1177. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1178. if (Result)
  1179. {
  1180. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1181. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1182. }
  1183. }
  1184. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1185. {
  1186. Configuration->Usage->Inc(L"TransfersOnBackground");
  1187. // these parameter are known only after transfer dialog
  1188. Params |=
  1189. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1190. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1191. Result = false;
  1192. ClearTransferSourceSelection(Direction);
  1193. }
  1194. return Result;
  1195. }
  1196. //---------------------------------------------------------------------------
  1197. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1198. {
  1199. if (FOnFileOperationFinished != NULL)
  1200. {
  1201. FOnFileOperationFinished(Side, UnicodeString(), true);
  1202. }
  1203. else
  1204. {
  1205. if (HasDirView[Side])
  1206. {
  1207. DirView(Side)->SelectAll(smNone);
  1208. }
  1209. }
  1210. }
  1211. //---------------------------------------------------------------------------
  1212. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1213. {
  1214. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1215. ClearOperationSelection(Side);
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1219. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1220. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1221. int Params)
  1222. {
  1223. DebugAssert(Queue != NULL);
  1224. bool SingleFile = false;
  1225. if (FileList->Count == 1)
  1226. {
  1227. if (Direction == tdToRemote)
  1228. {
  1229. UnicodeString FileName = FileList->Strings[0];
  1230. SingleFile = FileExists(ApiPath(FileName));
  1231. }
  1232. else
  1233. {
  1234. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1235. SingleFile = !File->IsDirectory;
  1236. }
  1237. }
  1238. TQueueItem * QueueItem;
  1239. if (Direction == tdToRemote)
  1240. {
  1241. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1242. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1243. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1244. }
  1245. else
  1246. {
  1247. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1248. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1249. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1250. }
  1251. AddQueueItem(Queue, QueueItem, Terminal);
  1252. }
  1253. //---------------------------------------------------------------------------
  1254. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1255. {
  1256. if (Queue->IsEmpty)
  1257. {
  1258. Terminal->QueueOperationStart = Now();
  1259. }
  1260. Queue->AddItem(QueueItem);
  1261. }
  1262. //---------------------------------------------------------------------------
  1263. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1264. {
  1265. }
  1266. //---------------------------------------------------------------------------
  1267. void __fastcall TCustomScpExplorerForm::InitControls()
  1268. {
  1269. DebugAssert(FDocks != NULL);
  1270. for (int Index = 0; Index < ComponentCount; Index++)
  1271. {
  1272. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1273. if ((Dock != NULL) && (Dock->Tag == 0))
  1274. {
  1275. FDocks->Add(Dock);
  1276. }
  1277. }
  1278. CollectItemsWithTextDisplayMode(this);
  1279. }
  1280. //---------------------------------------------------------------------------
  1281. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1282. {
  1283. ConfigurationChanged();
  1284. QueuePanel->Height =
  1285. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1286. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1287. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1288. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1289. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1290. QueueFileList->Height =
  1291. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1292. if (QueueFileList->Visible)
  1293. {
  1294. AdjustQueueLayout();
  1295. }
  1296. UpdateCustomCommandsToolbar();
  1297. }
  1298. //---------------------------------------------------------------------------
  1299. void __fastcall TCustomScpExplorerForm::StoreParams()
  1300. {
  1301. int APixelsPerInch = GetControlPixelsPerInch(this);
  1302. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1303. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1304. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1305. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1306. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1307. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1308. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1309. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1313. {
  1314. if (!FFormRestored)
  1315. {
  1316. FFormRestored = true;
  1317. RestoreFormParams();
  1318. }
  1319. TForm::CreateParams(Params);
  1320. }
  1321. //---------------------------------------------------------------------------
  1322. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1323. {
  1324. DebugAssert(FDocks != NULL);
  1325. for (int Index = 0; Index < FDocks->Count; Index++)
  1326. {
  1327. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1328. }
  1329. }
  1330. //---------------------------------------------------------------------------
  1331. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1332. {
  1333. SetDockAllowDrag(true);
  1334. while (!ButtonsStr.IsEmpty())
  1335. {
  1336. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1337. UnicodeString Name = CutToChar(S, L'=', true);
  1338. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1339. {
  1340. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1341. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1342. {
  1343. while (!S.IsEmpty())
  1344. {
  1345. UnicodeString S2 = CutToChar(S, L',', true);
  1346. UnicodeString Name = CutToChar(S2, L':', true);
  1347. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1348. {
  1349. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1350. if (SameText(GetToolbarItemName(Item), Name))
  1351. {
  1352. // forward compatibility
  1353. UnicodeString S3 = CutToChar(S2, L':', true);
  1354. bool Visible = StrToIntDef(S3, 0);
  1355. Item->Visible = Visible;
  1356. }
  1357. }
  1358. }
  1359. }
  1360. }
  1361. }
  1362. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1363. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1364. }
  1365. //---------------------------------------------------------------------------
  1366. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1367. {
  1368. UnicodeString Result;
  1369. SetDockAllowDrag(true);
  1370. Result = ::GetToolbarsLayoutStr(this);
  1371. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1372. return Result;
  1373. }
  1374. //---------------------------------------------------------------------------
  1375. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1376. {
  1377. UnicodeString Result;
  1378. if (Item->Action != NULL)
  1379. {
  1380. Result = Item->Action->Name;
  1381. Result = RemoveSuffix(Result, L"Action", true);
  1382. }
  1383. else
  1384. {
  1385. Result = Item->Name;
  1386. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1387. Result = RemoveSuffix(Result, L"Item", true);
  1388. }
  1389. return Result;
  1390. }
  1391. //---------------------------------------------------------------------------
  1392. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1393. {
  1394. UnicodeString Result;
  1395. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1396. {
  1397. UnicodeString ToolbarStr;
  1398. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1399. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1400. {
  1401. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1402. {
  1403. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1404. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1405. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1406. // and save the changes here only.
  1407. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1408. {
  1409. UnicodeString Name = GetToolbarItemName(Item);
  1410. DebugAssert(Name.Pos(L"TBX") == 0);
  1411. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1412. }
  1413. }
  1414. }
  1415. if (!ToolbarStr.IsEmpty())
  1416. {
  1417. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1418. }
  1419. }
  1420. return Result;
  1421. }
  1422. //---------------------------------------------------------------------------
  1423. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1424. {
  1425. DebugAssert(FProgressForm == NULL);
  1426. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1427. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1428. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1429. FProgressForm->DeleteLocalToRecycleBin =
  1430. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1431. FProgressForm->DeleteRemoteToRecycleBin =
  1432. HasActiveTerminal() &&
  1433. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1434. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1435. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1436. LockWindow();
  1437. // Actually, do not know what hides the progress once the operation finishes
  1438. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1439. SetTaskbarListProgressState(TBPF_NORMAL);
  1440. }
  1441. //---------------------------------------------------------------------------
  1442. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1443. {
  1444. SAFE_DESTROY(FProgressForm);
  1445. UnlockWindow();
  1446. SetQueueProgress();
  1447. }
  1448. //---------------------------------------------------------------------------
  1449. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1450. TFileOperationProgressType & ProgressData)
  1451. {
  1452. // Download to temporary local directory
  1453. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1454. {
  1455. bool Continue = true;
  1456. FPendingTempSpaceWarn = false;
  1457. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1458. if (!Continue)
  1459. {
  1460. Abort();
  1461. }
  1462. }
  1463. // operation is being executed and we still didn't show up progress form
  1464. if (ProgressData.InProgress && !FProgressForm)
  1465. {
  1466. CreateProgressForm(NULL);
  1467. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1468. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1469. }
  1470. // operation is finished (or terminated), so we hide progress form
  1471. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1472. {
  1473. DestroyProgressForm();
  1474. if (ProgressData.Operation == foCalculateSize)
  1475. {
  1476. // When calculating size before transfer, the abort caused by
  1477. // cancel flag set due to "MoveToQueue" below,
  1478. // is not propagated back to us in ExecuteFileOperation,
  1479. // as it is silently swallowed.
  1480. // So we explicitly (re)throw it here.
  1481. if (FMoveToQueue)
  1482. {
  1483. Abort();
  1484. }
  1485. }
  1486. else
  1487. {
  1488. if ((ProgressData.Cancel == csContinue) &&
  1489. !FAutoOperation)
  1490. {
  1491. OperationComplete(ProgressData.StartTime);
  1492. }
  1493. }
  1494. }
  1495. if (FProgressForm &&
  1496. // Can happen only during synchronization, when ending one of the operations
  1497. ProgressData.InProgress)
  1498. {
  1499. FProgressForm->SetProgressData(ProgressData);
  1500. DebugAssert(ProgressData.InProgress);
  1501. SetTaskbarListProgressValue(&ProgressData);
  1502. if (FProgressForm->Cancel > csContinue)
  1503. {
  1504. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1505. if (FProgressForm->Cancel == csCancelFile)
  1506. {
  1507. FProgressForm->ClearCancel();
  1508. }
  1509. // cancel cancels even the move
  1510. FMoveToQueue = false;
  1511. }
  1512. else if (FProgressForm->MoveToQueue)
  1513. {
  1514. FMoveToQueue = true;
  1515. ProgressData.SetCancelAtLeast(csCancel);
  1516. }
  1517. if ((FTransferResumeList != NULL) &&
  1518. ProgressData.InProgress &&
  1519. ProgressData.IsTransfer() &&
  1520. !ProgressData.FullFileName.IsEmpty())
  1521. {
  1522. if ((FTransferResumeList->Count == 0) ||
  1523. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1524. {
  1525. // note that we do not recognize directories from files here
  1526. FTransferResumeList->Add(ProgressData.FullFileName);
  1527. }
  1528. }
  1529. }
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1533. const TDateTime & StartTime)
  1534. {
  1535. if (GUIConfiguration->BeepOnFinish &&
  1536. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1537. {
  1538. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1539. DWORD Sound;
  1540. if (!ExtractFileExt(BeepSound).IsEmpty())
  1541. {
  1542. Sound = SND_FILENAME;
  1543. }
  1544. else
  1545. {
  1546. Sound = SND_ALIAS;
  1547. }
  1548. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1549. }
  1550. }
  1551. //---------------------------------------------------------------------------
  1552. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1553. TFileOperationProgressType & ProgressData)
  1554. {
  1555. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1556. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1557. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1558. FileOperationProgress(ProgressData);
  1559. }
  1560. //---------------------------------------------------------------------------
  1561. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1562. {
  1563. UnicodeString Result;
  1564. if (FProgressForm != NULL)
  1565. {
  1566. Result = FProgressForm->ProgressStr();
  1567. }
  1568. return Result;
  1569. }
  1570. //---------------------------------------------------------------------------
  1571. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1572. bool DragDrop)
  1573. {
  1574. return (!DragDrop && (DropSourceControl == NULL)) ||
  1575. (DropSourceControl == DirView(osOther));
  1576. }
  1577. //---------------------------------------------------------------------------
  1578. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1579. {
  1580. TCustomDirView * ADirView = DirView(Side);
  1581. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1582. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1583. // this can happen when local drive is unplugged in the middle of the operation
  1584. if (Item != NULL)
  1585. {
  1586. if (Unselect)
  1587. {
  1588. Item->Selected = false;
  1589. }
  1590. ADirView->MakeProgressVisible(Item);
  1591. }
  1592. return Item;
  1593. }
  1594. //---------------------------------------------------------------------------
  1595. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1596. TFileOperation Operation, TOperationSide Side,
  1597. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1598. TOnceDoneOperation & OnceDoneOperation)
  1599. {
  1600. if (!FAutoOperation)
  1601. {
  1602. // no selection on "/upload", form serves only as event handler
  1603. // (it is not displayed)
  1604. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1605. ((Operation == FPrimaryOperation) ||
  1606. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1607. {
  1608. TOperationSide DViewSide;
  1609. if ((FProgressSide != osCurrent) &&
  1610. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1611. {
  1612. // assume the operation is over the focused panel
  1613. DViewSide = FProgressSide;
  1614. }
  1615. else
  1616. {
  1617. DViewSide = Side;
  1618. }
  1619. if (FOnFileOperationFinished != NULL)
  1620. {
  1621. FOnFileOperationFinished(DViewSide, FileName, Success);
  1622. }
  1623. else
  1624. {
  1625. VisualiseOperationFinished(DViewSide, FileName, Success);
  1626. }
  1627. }
  1628. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1629. {
  1630. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1631. {
  1632. if (Side == osLocal)
  1633. {
  1634. Configuration->Usage->Inc(L"UploadedFiles");
  1635. }
  1636. else
  1637. {
  1638. Configuration->Usage->Inc(L"DownloadedFiles");
  1639. }
  1640. }
  1641. }
  1642. }
  1643. if (Success && (FSynchronizeController != NULL))
  1644. {
  1645. DebugAssert(!IsSideLocalBrowser(osRemote));
  1646. if (Operation == foCopy)
  1647. {
  1648. DebugAssert(Side == osLocal);
  1649. FSynchronizeController->LogOperation(soUpload, FileName);
  1650. }
  1651. else if (Operation == foDelete)
  1652. {
  1653. DebugAssert(Side == osRemote);
  1654. FSynchronizeController->LogOperation(soDelete, FileName);
  1655. }
  1656. }
  1657. if (FProgressForm)
  1658. {
  1659. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1660. }
  1661. }
  1662. //---------------------------------------------------------------------------
  1663. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1664. TFileOperation Operation, TOperationSide Side,
  1665. bool Temp, const UnicodeString & FileName, Boolean Success,
  1666. TOnceDoneOperation & OnceDoneOperation)
  1667. {
  1668. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1669. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1670. OnceDoneOperation);
  1671. }
  1672. //---------------------------------------------------------------------------
  1673. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1674. {
  1675. return false;
  1676. }
  1677. //---------------------------------------------------------------------------
  1678. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1679. {
  1680. return false;
  1681. }
  1682. //---------------------------------------------------------------------------
  1683. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1684. {
  1685. return false;
  1686. }
  1687. //---------------------------------------------------------------------------
  1688. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1689. {
  1690. return NULL;
  1691. }
  1692. //---------------------------------------------------------------------------
  1693. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1694. {
  1695. DebugAssert(GetSide(Side) == osRemote);
  1696. DebugUsedParam(Side);
  1697. return RemoteDirView;
  1698. }
  1699. //---------------------------------------------------------------------------
  1700. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1701. {
  1702. DebugAssert(GetSide(Side) == osRemote);
  1703. DebugUsedParam(Side);
  1704. return RemoteDriveView;
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1708. {
  1709. DebugAssert(GetSide(Side) == osRemote);
  1710. DebugUsedParam(Side);
  1711. return HasActiveTerminal();
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1715. TOperationSide Side, int FilesOnly)
  1716. {
  1717. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1718. }
  1719. //---------------------------------------------------------------------------
  1720. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1721. TOperationSide Side, int FilesOnly)
  1722. {
  1723. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. struct THistoryItemData
  1727. {
  1728. short int Side;
  1729. short int Index;
  1730. };
  1731. //---------------------------------------------------------------------------
  1732. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1733. {
  1734. DirView(Side)->HistoryGo(Index);
  1735. }
  1736. //---------------------------------------------------------------------------
  1737. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1738. {
  1739. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1740. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1741. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1742. }
  1743. //---------------------------------------------------------------------------
  1744. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1745. bool Back)
  1746. {
  1747. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1748. {
  1749. TCustomDirView * DView = DirView(Side);
  1750. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1751. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1752. if (ICount > 10)
  1753. {
  1754. ICount = 10;
  1755. }
  1756. Menu->Items->Clear();
  1757. THistoryItemData Data;
  1758. Data.Side = Side;
  1759. for (short int i = 1; i <= ICount; i++)
  1760. {
  1761. TTBCustomItem * Item = new TTBXItem(Menu);
  1762. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1763. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1764. Item->Hint = DView->HistoryPath[Data.Index];
  1765. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1766. Item->Tag = *reinterpret_cast<int*>(&Data);
  1767. Item->OnClick = HistoryItemClick;
  1768. Menu->Items->Add(Item);
  1769. }
  1770. }
  1771. }
  1772. //---------------------------------------------------------------------------
  1773. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1774. TOperationSide Side, bool Back)
  1775. {
  1776. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1777. {
  1778. // workaround
  1779. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1780. // operator new allocates memory in Pascal code, but calls inline
  1781. // contructor in C++, leading in problems, the function does
  1782. // both in Pascal code
  1783. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1784. UpdateHistoryMenu(Side, Back);
  1785. }
  1786. return FHistoryMenu[Side == osLocal][Back];
  1787. }
  1788. //---------------------------------------------------------------------------
  1789. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1790. TCustomDirView *Sender)
  1791. {
  1792. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1793. UpdateHistoryMenu(Side, true);
  1794. UpdateHistoryMenu(Side, false);
  1795. }
  1796. //---------------------------------------------------------------------------
  1797. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1798. {
  1799. // remote custom commands can be executed only if the server supports shell commands
  1800. // or have secondary shell
  1801. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1802. }
  1803. //---------------------------------------------------------------------------
  1804. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1805. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1806. {
  1807. // -1 = hidden, 0 = disabled, 1 = enabled
  1808. int Result;
  1809. TFileCustomCommand RemoteCustomCommand;
  1810. TLocalCustomCommand LocalCustomCommand;
  1811. TFileCustomCommand * NonInteractiveCustomCommand =
  1812. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1813. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1814. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1815. if (FLAGCLEAR(Command.Params, ccLocal))
  1816. {
  1817. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1818. // custom command that does not operate with files can be executed anytime ...
  1819. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1820. {
  1821. // ... but do not show such command in remote file menu
  1822. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1823. {
  1824. Result = AllowedState;
  1825. }
  1826. else
  1827. {
  1828. Result = -1;
  1829. }
  1830. }
  1831. else
  1832. {
  1833. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1834. {
  1835. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1836. }
  1837. else
  1838. {
  1839. Result = -1;
  1840. }
  1841. }
  1842. }
  1843. else
  1844. {
  1845. // custom command that does not operate with files can be executed anytime
  1846. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1847. {
  1848. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1849. }
  1850. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1851. {
  1852. if ((ListType == ccltAll) || (ListType == ccltFile))
  1853. {
  1854. // special case is "diff"-style command that can be executed over any side,
  1855. // if we have both sides
  1856. Result =
  1857. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1858. // directly (instead of EnableSelectedOperation) to pass
  1859. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1860. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1861. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1862. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1863. }
  1864. else if (ListType == ccltBoth)
  1865. {
  1866. DebugAssert(!IsLocalBrowserMode());
  1867. Result = 1;
  1868. }
  1869. else
  1870. {
  1871. Result = -1;
  1872. }
  1873. }
  1874. else
  1875. {
  1876. if ((ListType == ccltAll) ||
  1877. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1878. {
  1879. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1880. }
  1881. else
  1882. {
  1883. Result = -1;
  1884. }
  1885. }
  1886. if ((Result > 0) &&
  1887. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1888. !HasActiveTerminal())
  1889. {
  1890. Result = 0;
  1891. }
  1892. }
  1893. return Result;
  1894. }
  1895. //---------------------------------------------------------------------------
  1896. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1897. TStrings * FileList, const TCustomCommandType & ACommand,
  1898. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1899. {
  1900. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1901. {
  1902. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1903. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1904. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1905. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1906. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1907. bool Capture =
  1908. FLAGSET(ACommand.Params, ccShowResults) ||
  1909. FLAGSET(ACommand.Params, ccCopyResults) ||
  1910. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1911. TCaptureOutputEvent OutputEvent = NULL;
  1912. DebugAssert(FCapturedLog == NULL);
  1913. if (Capture)
  1914. {
  1915. FCapturedLog = new TStringList();
  1916. OutputEvent = TerminalCaptureLog;
  1917. }
  1918. try
  1919. {
  1920. if (!RemoteCustomCommand.IsFileCommand(Command))
  1921. {
  1922. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1923. OutputEvent);
  1924. }
  1925. else
  1926. {
  1927. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1928. }
  1929. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1930. {
  1931. if (FLAGSET(ACommand.Params, ccCopyResults))
  1932. {
  1933. CopyToClipboard(FCapturedLog);
  1934. }
  1935. if (FLAGSET(ACommand.Params, ccShowResults))
  1936. {
  1937. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1938. }
  1939. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1940. {
  1941. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1942. }
  1943. }
  1944. }
  1945. __finally
  1946. {
  1947. SAFE_DESTROY(FCapturedLog);
  1948. }
  1949. }
  1950. }
  1951. //---------------------------------------------------------------------------
  1952. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1953. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1954. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1955. {
  1956. DebugAssert(!IsLocalBrowserMode());
  1957. TStrings * LocalFileList = NULL;
  1958. TStrings * RemoteFileList = NULL;
  1959. TStrings * RemoteFileListFull = NULL;
  1960. try
  1961. {
  1962. if (LocalFileCommand)
  1963. {
  1964. if (ALocalFileList == NULL)
  1965. {
  1966. DebugAssert(HasDirView[osLocal]);
  1967. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1968. // directly (instead of EnableSelectedOperation) to pass
  1969. // false to FocusedFileOnlyWhenFocused
  1970. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1971. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1972. }
  1973. else
  1974. {
  1975. LocalFileList = ALocalFileList;
  1976. }
  1977. if (FileListCommand)
  1978. {
  1979. if (LocalFileList->Count != 1)
  1980. {
  1981. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1982. }
  1983. }
  1984. else
  1985. {
  1986. if ((LocalFileList->Count != 1) &&
  1987. (FileList->Count != 1) &&
  1988. (LocalFileList->Count != FileList->Count))
  1989. {
  1990. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1991. }
  1992. }
  1993. }
  1994. UnicodeString RootTempDir;
  1995. UnicodeString TempDir;
  1996. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1997. if (!RemoteFiles)
  1998. {
  1999. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2000. }
  2001. try
  2002. {
  2003. TDateTimes RemoteFileTimes;
  2004. if (RemoteFiles)
  2005. {
  2006. RemoteFileList = FileList;
  2007. }
  2008. else
  2009. {
  2010. RemoteFileList = new TStringList();
  2011. TMakeLocalFileListParams MakeFileListParam;
  2012. MakeFileListParam.FileList = RemoteFileList;
  2013. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2014. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2015. MakeFileListParam.Recursive =
  2016. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2017. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2018. if (!MakeFileListParam.Recursive)
  2019. {
  2020. RemoteFileTimes.clear();
  2021. RemoteFileListFull = new TStringList();
  2022. MakeFileListParam.FileList = RemoteFileListFull;
  2023. MakeFileListParam.Recursive = true;
  2024. MakeFileListParam.IncludeDirs = false;
  2025. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2026. }
  2027. else
  2028. {
  2029. RemoteFileListFull = RemoteFileList;
  2030. }
  2031. }
  2032. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2033. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2034. if (!NonBlocking)
  2035. {
  2036. int Count;
  2037. if (FileListCommand)
  2038. {
  2039. Count = 1;
  2040. }
  2041. else if (LocalFileCommand)
  2042. {
  2043. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2044. }
  2045. else
  2046. {
  2047. Count = RemoteFileList->Count;
  2048. }
  2049. Progress.Start(foCustomCommand, osRemote, Count);
  2050. DebugAssert(FProgressForm != NULL);
  2051. FProgressForm->ReadOnly = true;
  2052. }
  2053. try
  2054. {
  2055. if (FileListCommand)
  2056. {
  2057. UnicodeString LocalFile;
  2058. // MakeFileList does not delimit filenames
  2059. UnicodeString FileList = MakeFileList(RemoteFileList);
  2060. if (LocalFileCommand)
  2061. {
  2062. DebugAssert(LocalFileList->Count == 1);
  2063. LocalFile = LocalFileList->Strings[0];
  2064. }
  2065. TLocalCustomCommand CustomCommand(Data,
  2066. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2067. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2068. if (NonBlocking)
  2069. {
  2070. DebugAssert(!POutput);
  2071. ExecuteShellChecked(ShellCommand);
  2072. }
  2073. else
  2074. {
  2075. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2076. }
  2077. }
  2078. else if (LocalFileCommand)
  2079. {
  2080. if (LocalFileList->Count == 1)
  2081. {
  2082. UnicodeString LocalFile = LocalFileList->Strings[0];
  2083. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2084. {
  2085. UnicodeString FileName = RemoteFileList->Strings[Index];
  2086. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2087. Progress.SetFile(FileNameForProgress);
  2088. TLocalCustomCommand CustomCommand(Data,
  2089. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2090. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2091. TOnceDoneOperation OnceDoneOperation;
  2092. // Do not unselect anything, as it won't work with two panels anyway
  2093. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2094. }
  2095. }
  2096. else if (RemoteFileList->Count == 1)
  2097. {
  2098. UnicodeString FileName = RemoteFileList->Strings[0];
  2099. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2100. {
  2101. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2102. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2103. Progress.SetFile(LocalFileNameOnly);
  2104. TLocalCustomCommand CustomCommand(
  2105. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2106. FileName, LocalFileName, L"");
  2107. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2108. TOnceDoneOperation OnceDoneOperation;
  2109. // Do not unselect anything, as it won't work with two panels anyway
  2110. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2111. }
  2112. }
  2113. else
  2114. {
  2115. if (LocalFileList->Count != RemoteFileList->Count)
  2116. {
  2117. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2118. }
  2119. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2120. {
  2121. UnicodeString FileName = RemoteFileList->Strings[Index];
  2122. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2123. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2124. Progress.SetFile(LocalFileNameOnly); // sic
  2125. TLocalCustomCommand CustomCommand(
  2126. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2127. FileName, LocalFileName, L"");
  2128. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2129. TOnceDoneOperation OnceDoneOperation;
  2130. // Do not unselect anything, as it won't work with two panels anyway
  2131. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2132. }
  2133. }
  2134. }
  2135. else
  2136. {
  2137. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2138. {
  2139. UnicodeString FileName = RemoteFileList->Strings[Index];
  2140. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2141. Progress.SetFile(FileNameForProgress);
  2142. TLocalCustomCommand CustomCommand(Data,
  2143. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2144. FileName, L"", L"");
  2145. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2146. TOnceDoneOperation OnceDoneOperation;
  2147. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2148. }
  2149. }
  2150. }
  2151. __finally
  2152. {
  2153. if (!NonBlocking)
  2154. {
  2155. Progress.Stop();
  2156. }
  2157. }
  2158. DebugAssert(!FAutoOperation);
  2159. if (!RemoteFiles)
  2160. {
  2161. TempDir = IncludeTrailingBackslash(TempDir);
  2162. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2163. {
  2164. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2165. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2166. // Skip directories, process only nested files.
  2167. // The check is redundant as FileAge fails for directories anyway.
  2168. !DirectoryExists(FileName))
  2169. {
  2170. UnicodeString RemoteDir =
  2171. UnixExtractFileDir(
  2172. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2173. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2174. TDateTime NewTime;
  2175. if (FileAge(FileName, NewTime) &&
  2176. (NewTime != RemoteFileTimes[Index]))
  2177. {
  2178. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2179. TemporaryFileCopyParam(CopyParam);
  2180. CopyParam.FileMask = L"";
  2181. FAutoOperation = true;
  2182. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2183. TemporaryFilesList->Add(FileName);
  2184. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2185. }
  2186. }
  2187. }
  2188. }
  2189. }
  2190. __finally
  2191. {
  2192. FAutoOperation = false;
  2193. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2194. {
  2195. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2196. }
  2197. }
  2198. }
  2199. __finally
  2200. {
  2201. if (RemoteFileList != FileList)
  2202. {
  2203. delete RemoteFileList;
  2204. }
  2205. if (LocalFileList != ALocalFileList)
  2206. {
  2207. delete LocalFileList;
  2208. }
  2209. if (RemoteFileListFull != RemoteFileList)
  2210. {
  2211. delete RemoteFileListFull;
  2212. }
  2213. }
  2214. }
  2215. //---------------------------------------------------------------------------
  2216. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2217. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2218. bool FileListCommand, UnicodeString * POutput)
  2219. {
  2220. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2221. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2222. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2223. {
  2224. UnicodeString FileName = SelectedFileList->Strings[Index];
  2225. if (DirectoryExists(FileName))
  2226. {
  2227. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2228. {
  2229. LocalFileList->Add(FileName);
  2230. }
  2231. if (FLAGSET(ACommand.Params, ccRecursive))
  2232. {
  2233. TMakeLocalFileListParams MakeFileListParam;
  2234. MakeFileListParam.FileList = LocalFileList.get();
  2235. MakeFileListParam.FileTimes = NULL;
  2236. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2237. MakeFileListParam.Recursive = true;
  2238. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2239. }
  2240. }
  2241. else
  2242. {
  2243. LocalFileList->Add(FileName);
  2244. }
  2245. }
  2246. UnicodeString RemotePath;
  2247. if (Terminal != NULL)
  2248. {
  2249. RemotePath = Terminal->CurrentDirectory;
  2250. }
  2251. if (FileListCommand)
  2252. {
  2253. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2254. TLocalCustomCommand CustomCommand(
  2255. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2256. L"", L"", FileList);
  2257. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2258. }
  2259. else
  2260. {
  2261. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2262. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2263. DebugAssert(FProgressForm != NULL);
  2264. FProgressForm->ReadOnly = true;
  2265. try
  2266. {
  2267. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2268. {
  2269. UnicodeString FileName = LocalFileList->Strings[Index];
  2270. Progress.SetFile(FileName);
  2271. TLocalCustomCommand CustomCommand(
  2272. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2273. FileName, L"", L"");
  2274. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2275. TOnceDoneOperation OnceDoneOperation;
  2276. Progress.Finish(FileName, true, OnceDoneOperation);
  2277. }
  2278. }
  2279. __finally
  2280. {
  2281. Progress.Stop();
  2282. }
  2283. }
  2284. }
  2285. //---------------------------------------------------------------------------
  2286. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2287. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2288. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2289. {
  2290. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2291. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2292. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2293. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2294. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2295. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2296. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2297. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2298. std::unique_ptr<UnicodeString> POutput;
  2299. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2300. FLAGSET(ACommand.Params, ccCopyResults))
  2301. {
  2302. POutput.reset(new UnicodeString());
  2303. }
  2304. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2305. FProgressSide = FCurrentSide;
  2306. if (!LocalCustomCommand.IsFileCommand(Command))
  2307. {
  2308. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2309. }
  2310. // remote files?
  2311. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2312. {
  2313. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2314. }
  2315. // local files
  2316. else
  2317. {
  2318. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2319. }
  2320. if (POutput.get() != NULL)
  2321. {
  2322. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2323. int P = POutput->Pos(sLineBreak);
  2324. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2325. {
  2326. POutput->SetLength(P - 1);
  2327. }
  2328. // Copy even empty output to clipboard
  2329. if (FLAGSET(ACommand.Params, ccCopyResults))
  2330. {
  2331. CopyToClipboard(*POutput);
  2332. }
  2333. // But do not show an empty message box.
  2334. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2335. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2336. !POutput->IsEmpty())
  2337. {
  2338. TClipboardHandler ClipboardHandler;
  2339. ClipboardHandler.Text = *POutput;
  2340. TMessageParams Params;
  2341. TQueryButtonAlias Aliases[1];
  2342. Aliases[0].Button = qaRetry;
  2343. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2344. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2345. Params.Aliases = Aliases;
  2346. Params.AliasesCount = LENOF(Aliases);
  2347. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2348. }
  2349. }
  2350. }
  2351. //---------------------------------------------------------------------------
  2352. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2353. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2354. {
  2355. TCustomCommandData Data;
  2356. UnicodeString Site;
  2357. UnicodeString RemotePath;
  2358. if (HasActiveTerminal())
  2359. {
  2360. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2361. Data = TCustomCommandData(SessionData.get());
  2362. Site = Terminal->SessionData->SessionKey;
  2363. RemotePath = Terminal->CurrentDirectory;
  2364. }
  2365. UnicodeString HelpKeyword = ACommand.HomePage;
  2366. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2367. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2368. {
  2369. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2370. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2371. {
  2372. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2373. }
  2374. else
  2375. {
  2376. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2377. }
  2378. if (!DoCustomCommandOptionsDialog(
  2379. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2380. {
  2381. Abort();
  2382. }
  2383. }
  2384. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2385. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2386. {
  2387. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2388. }
  2389. else
  2390. {
  2391. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2392. }
  2393. }
  2394. //---------------------------------------------------------------------------
  2395. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2396. const TCustomCommandType & Command)
  2397. {
  2398. DebugAssert(FCustomCommandLocalFileList != NULL);
  2399. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2400. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2401. TStrings * LocalFileList = new TStringList();
  2402. TStrings * RemoteFileList = new TStringList();
  2403. try
  2404. {
  2405. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2406. {
  2407. LocalFileList->Clear();
  2408. LocalFileList->AddObject(
  2409. FCustomCommandLocalFileList->Strings[Index],
  2410. FCustomCommandLocalFileList->Objects[Index]);
  2411. RemoteFileList->Clear();
  2412. RemoteFileList->AddObject(
  2413. FCustomCommandRemoteFileList->Strings[Index],
  2414. FCustomCommandRemoteFileList->Objects[Index]);
  2415. CustomCommand(RemoteFileList, Command, LocalFileList);
  2416. }
  2417. }
  2418. __finally
  2419. {
  2420. delete LocalFileList;
  2421. delete RemoteFileList;
  2422. }
  2423. }
  2424. //---------------------------------------------------------------------------
  2425. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2426. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2427. {
  2428. delete FCustomCommandLocalFileList;
  2429. delete FCustomCommandRemoteFileList;
  2430. // takeover ownership,
  2431. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2432. FCustomCommandLocalFileList = LocalFileList;
  2433. FCustomCommandRemoteFileList = RemoteFileList;
  2434. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2435. if (Button != NULL)
  2436. {
  2437. FCustomCommandMenu->Items->Clear();
  2438. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2439. MenuPopup(FCustomCommandMenu, Button);
  2440. }
  2441. else
  2442. {
  2443. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2444. }
  2445. }
  2446. //---------------------------------------------------------------------------
  2447. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2448. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2449. {
  2450. DebugAssert(FCapturedLog != NULL);
  2451. if ((OutputType == cotOutput) || (OutputType == cotError))
  2452. {
  2453. FCapturedLog->Add(AddedLine);
  2454. }
  2455. }
  2456. //---------------------------------------------------------------------------
  2457. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2458. TOperationSide Side)
  2459. {
  2460. return
  2461. (Side == osOther) &&
  2462. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2463. }
  2464. //---------------------------------------------------------------------------
  2465. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2466. TOperationSide Side, TTBXCustomItem * Item,
  2467. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2468. {
  2469. TCustomDirView * DView = DirView(Side);
  2470. bool IsDefault = false;
  2471. if (DView->ItemFocused != NULL)
  2472. {
  2473. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2474. TResolvedDoubleClickAction DefaultAction =
  2475. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2476. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2477. }
  2478. TTBItemOptions O = Item->Options;
  2479. if (IsDefault)
  2480. {
  2481. Item->Options = O << tboDefault;
  2482. }
  2483. else
  2484. {
  2485. Item->Options = O >> tboDefault;
  2486. }
  2487. }
  2488. //---------------------------------------------------------------------------
  2489. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2490. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2491. {
  2492. TCustomDirView * DView = DirView(Side);
  2493. TListItem * Item = DView->ItemFocused;
  2494. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2495. EnableFocusedOperation[Side])
  2496. {
  2497. TPoint ClientPoint;
  2498. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2499. TPoint(0, 0) : MousePos;
  2500. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2501. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2502. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2503. }
  2504. }
  2505. //---------------------------------------------------------------------------
  2506. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2507. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2508. {
  2509. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2510. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2511. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2512. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2513. Handled = true;
  2514. }
  2515. //---------------------------------------------------------------------------
  2516. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2517. {
  2518. }
  2519. //---------------------------------------------------------------------------
  2520. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2521. {
  2522. DebugAssert(FErrorList == NULL);
  2523. if (WinConfiguration->ContinueOnError)
  2524. {
  2525. FErrorList = new TStringList();
  2526. Configuration->Usage->Inc(L"ContinuationsOnError");
  2527. }
  2528. NonVisualDataModule->StartBusy();
  2529. }
  2530. //---------------------------------------------------------------------------
  2531. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2532. {
  2533. NonVisualDataModule->EndBusy();
  2534. if (FErrorList)
  2535. {
  2536. HandleErrorList(FErrorList);
  2537. }
  2538. }
  2539. //---------------------------------------------------------------------------
  2540. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2541. const TCopyParamType & CopyParam)
  2542. {
  2543. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2544. {
  2545. Configuration->Usage->Inc(L"FileMaskUses");
  2546. }
  2547. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2548. {
  2549. Configuration->Usage->Inc(L"OperationMaskUses");
  2550. }
  2551. }
  2552. //---------------------------------------------------------------------------
  2553. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2554. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2555. {
  2556. DebugAssert(!IsLocalBrowserMode());
  2557. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2558. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2559. TTransferOperationParam DefaultParam;
  2560. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2561. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2562. switch (Param.Queue)
  2563. {
  2564. case asOn:
  2565. CopyParam.Queue = true;
  2566. break;
  2567. case asOff:
  2568. CopyParam.Queue = false;
  2569. break;
  2570. case asAuto:
  2571. default:
  2572. // keep default
  2573. break;
  2574. }
  2575. bool Result =
  2576. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2577. if (Result)
  2578. {
  2579. DebugAssert(Terminal);
  2580. bool SelectionRestored = false;
  2581. TCustomDirView * DView = NULL;
  2582. if (HasDirView[Side])
  2583. {
  2584. DView = DirView(Side);
  2585. DView->SaveSelection();
  2586. DView->SaveSelectedNames();
  2587. }
  2588. UpdateCopyParamCounters(CopyParam);
  2589. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2590. DebugAssert(FTransferResumeList == NULL);
  2591. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2592. FMoveToQueue = false;
  2593. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2594. try
  2595. {
  2596. TStrings * PermanentFileList;
  2597. std::unique_ptr<TStrings> PermanentFileListOwner;
  2598. try
  2599. {
  2600. if (Side == osLocal)
  2601. {
  2602. PermanentFileList = FileList;
  2603. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2604. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2605. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2606. if (Operation == foMove)
  2607. {
  2608. ReloadLocalDirectory();
  2609. if (DView != NULL)
  2610. {
  2611. DView->RestoreSelection();
  2612. }
  2613. SelectionRestored = true;
  2614. }
  2615. }
  2616. else
  2617. {
  2618. // Clone the file list as it may refer to current directory files,
  2619. // which get destroyed, when the source directory is reloaded after foMove operation.
  2620. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2621. // But for a hotfix, we are not going to do such a big change.
  2622. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2623. PermanentFileList = PermanentFileListOwner.get();
  2624. try
  2625. {
  2626. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2627. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2628. }
  2629. __finally
  2630. {
  2631. if (Operation == foMove)
  2632. {
  2633. if (DView != NULL)
  2634. {
  2635. DView->RestoreSelection();
  2636. }
  2637. SelectionRestored = true;
  2638. }
  2639. ReloadLocalDirectory(Param.TargetDirectory);
  2640. }
  2641. }
  2642. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2643. if (FMoveToQueue)
  2644. {
  2645. Abort();
  2646. }
  2647. }
  2648. catch (EAbort &)
  2649. {
  2650. if (FMoveToQueue)
  2651. {
  2652. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2653. DebugAssert(CopyParam.TransferSkipList == NULL);
  2654. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2655. if (TransferResumeList->Count > 0)
  2656. {
  2657. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2658. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2659. }
  2660. CopyParam.TransferSkipList = TransferResumeList.release();
  2661. // not really needed, just to keep it consistent with TransferResumeList
  2662. FTransferResumeList = NULL;
  2663. FMoveToQueue = false;
  2664. Configuration->Usage->Inc("MovesToBackground");
  2665. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2666. ClearTransferSourceSelection(Direction);
  2667. }
  2668. throw;
  2669. }
  2670. }
  2671. __finally
  2672. {
  2673. if (!SelectionRestored && (DView != NULL))
  2674. {
  2675. DView->DiscardSavedSelection();
  2676. }
  2677. FTransferResumeList = NULL;
  2678. }
  2679. }
  2680. return Result;
  2681. }
  2682. //---------------------------------------------------------------------------
  2683. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2684. TOperationSide Side, TStrings * FileList, void * Param)
  2685. {
  2686. DebugAssert(FileList->Count);
  2687. // We deliberately do not toggle alternative flag (Param), but use OR,
  2688. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2689. // shortcut of CurrentDeleteAlternativeAction
  2690. bool Alternative =
  2691. bool(Param) || UseAlternativeFunction();
  2692. bool Recycle;
  2693. if (IsSideLocalBrowser(Side))
  2694. {
  2695. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2696. }
  2697. else
  2698. {
  2699. Recycle =
  2700. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2701. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2702. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2703. }
  2704. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2705. if (!Result)
  2706. {
  2707. UnicodeString Query;
  2708. if (FileList->Count == 1)
  2709. {
  2710. if (IsSideLocalBrowser(Side))
  2711. {
  2712. Query = ExtractFileName(FileList->Strings[0]);
  2713. }
  2714. else
  2715. {
  2716. Query = UnixExtractFileName(FileList->Strings[0]);
  2717. }
  2718. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2719. }
  2720. else
  2721. {
  2722. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2723. }
  2724. TMessageParams Params(mpNeverAskAgainCheck);
  2725. Params.ImageName = L"Delete file";
  2726. unsigned int Answer =
  2727. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2728. if (Answer == qaNeverAskAgain)
  2729. {
  2730. Result = true;
  2731. if (Recycle)
  2732. {
  2733. WinConfiguration->ConfirmRecycling = false;
  2734. }
  2735. else
  2736. {
  2737. WinConfiguration->ConfirmDeleting = false;
  2738. }
  2739. }
  2740. else
  2741. {
  2742. Result = (Answer == qaOK);
  2743. }
  2744. }
  2745. if (Result)
  2746. {
  2747. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2748. }
  2749. return Result;
  2750. }
  2751. //---------------------------------------------------------------------------
  2752. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2753. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2754. {
  2755. TAutoBatch AutoBatch(this);
  2756. bool Result;
  2757. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2758. {
  2759. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2760. }
  2761. else if (Operation == foRename)
  2762. {
  2763. DebugAssert(DirView(Side)->ItemFocused);
  2764. DirView(Side)->ItemFocused->EditCaption();
  2765. Result = true;
  2766. }
  2767. else if (Operation == foDelete)
  2768. {
  2769. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2770. }
  2771. else if (Operation == foSetProperties)
  2772. {
  2773. DirView(osRemote)->SaveSelectedNames();
  2774. Result = SetProperties(Side, FileList);
  2775. }
  2776. else if (Operation == foCustomCommand)
  2777. {
  2778. DebugAssert(Param);
  2779. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2780. DirView(osRemote)->SaveSelectedNames();
  2781. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2782. CustomCommand(FileList, *Command, NULL);
  2783. Result = true;
  2784. }
  2785. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2786. {
  2787. DebugAssert(!IsSideLocalBrowser(Side));
  2788. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2789. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2790. }
  2791. else if (Operation == foLock)
  2792. {
  2793. DebugAssert(!IsSideLocalBrowser(Side));
  2794. LockFiles(FileList, true);
  2795. Result = true;
  2796. }
  2797. else if (Operation == foUnlock)
  2798. {
  2799. DebugAssert(!IsSideLocalBrowser(Side));
  2800. LockFiles(FileList, false);
  2801. Result = true;
  2802. }
  2803. else
  2804. {
  2805. DebugFail();
  2806. }
  2807. return Result;
  2808. }
  2809. //---------------------------------------------------------------------------
  2810. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2811. {
  2812. if (Side == osCurrent)
  2813. {
  2814. Side = FCurrentSide;
  2815. }
  2816. return Side;
  2817. }
  2818. //---------------------------------------------------------------------------
  2819. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2820. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2821. {
  2822. Side = GetSide(Side);
  2823. bool FullPath = IsSideLocalBrowser(Side);
  2824. TCustomDirView * ADirView = DirView(Side);
  2825. std::unique_ptr<TStringList> FileList(new TStringList());
  2826. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2827. bool ReloadProperties =
  2828. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2829. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2830. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2831. if (Result && ReloadProperties)
  2832. {
  2833. std::unique_ptr<TStringList> FileList2(new TStringList());
  2834. FileList->CaseSensitive = true;
  2835. FileList->Sort();
  2836. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2837. {
  2838. TListItem * Item = ADirView->Items->Item[Index];
  2839. UnicodeString FileName = ADirView->ItemFileName(Item);
  2840. int Index2;
  2841. if (FileList->Find(FileName, Index2))
  2842. {
  2843. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2844. }
  2845. }
  2846. LoadFilesProperties(FileList2.get());
  2847. }
  2848. return Result;
  2849. }
  2850. //---------------------------------------------------------------------------
  2851. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2852. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2853. bool NoConfirmation, void * Param)
  2854. {
  2855. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2856. {
  2857. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2858. {
  2859. DebugAssert(!IsLocalBrowserMode());
  2860. if (GetSide(Side) == osLocal)
  2861. {
  2862. Configuration->Usage->Inc(L"UploadsCommand");
  2863. }
  2864. else
  2865. {
  2866. Configuration->Usage->Inc(L"DownloadsCommand");
  2867. }
  2868. }
  2869. }
  2870. }
  2871. //---------------------------------------------------------------------------
  2872. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2873. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2874. {
  2875. TTransferOperationParam Param;
  2876. if ((WinConfiguration->Interface != ifCommander) ||
  2877. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2878. {
  2879. Flags &= ~cocShortCutHint;
  2880. }
  2881. TCustomDirView * DView = DirView(Side);
  2882. Param.Options =
  2883. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2884. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2885. if (FLAGSET(Flags, cocQueue))
  2886. {
  2887. Param.Queue = asOn;
  2888. }
  2889. else if (FLAGSET(Flags, cocNonQueue))
  2890. {
  2891. Param.Queue = asOff;
  2892. }
  2893. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2894. }
  2895. //---------------------------------------------------------------------------
  2896. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2897. {
  2898. int Count = ErrorList->Count;
  2899. try
  2900. {
  2901. if (ErrorList->Count)
  2902. {
  2903. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2904. if (MessageDialog(Message, qtError,
  2905. qaOK | qaCancel, HELP_NONE) == qaOK)
  2906. {
  2907. unsigned int Answer;
  2908. int Index = 0;
  2909. do
  2910. {
  2911. DebugAssert(Index >= 0 && Index < Count);
  2912. TQueryButtonAlias Aliases[2];
  2913. Aliases[0].Button = qaYes;
  2914. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2915. Aliases[1].Button = qaNo;
  2916. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2917. TMessageParams Params;
  2918. Params.Aliases = Aliases;
  2919. Params.AliasesCount = LENOF(Aliases);
  2920. int No = Index + 1;
  2921. UnicodeString Message = ErrorList->Strings[Index];
  2922. UnicodeString AMainInstructions;
  2923. UnicodeString Details;
  2924. if (ExtractMainInstructions(Message, AMainInstructions))
  2925. {
  2926. Details = Message;
  2927. Message = AMainInstructions;
  2928. }
  2929. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2930. Answer = MoreMessageDialog(
  2931. Message,
  2932. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2933. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2934. qaOK, HELP_NONE, &Params);
  2935. if (Answer == qaNo)
  2936. {
  2937. Index++;
  2938. }
  2939. if (Answer == qaYes)
  2940. {
  2941. Index--;
  2942. }
  2943. }
  2944. while (Answer != qaOK);
  2945. }
  2946. }
  2947. }
  2948. __finally
  2949. {
  2950. TStrings * List = ErrorList;
  2951. ErrorList = NULL;
  2952. for (int i = 0; i < Count; i++)
  2953. {
  2954. delete List->Objects[i];
  2955. }
  2956. delete List;
  2957. }
  2958. }
  2959. //---------------------------------------------------------------------------
  2960. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2961. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2962. {
  2963. // needed for checking filemasks, as there's no directory object
  2964. // associated with the file object
  2965. File->FullFileName = FullFileName;
  2966. TFileMasks::TParams MaskParams;
  2967. MaskParams.Size = File->Size;
  2968. MaskParams.Modification = File->Modification;
  2969. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2973. {
  2974. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2975. Side = GetSide(Side);
  2976. TCustomDirView * DView = DirView(Side);
  2977. TListItem * FocusedItem = DView->ItemFocused;
  2978. UnicodeString Name;
  2979. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2980. {
  2981. Name = DView->ItemFileName(FocusedItem);
  2982. }
  2983. else
  2984. {
  2985. Name = LoadStr(NEW_FILE);
  2986. }
  2987. UnicodeString Names = Name;
  2988. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2989. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2990. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2991. {
  2992. while (!Names.IsEmpty())
  2993. {
  2994. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2995. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2996. UnicodeString TargetFileName;
  2997. UnicodeString LocalFileName;
  2998. UnicodeString RootTempDir;
  2999. UnicodeString TempDir;
  3000. UnicodeString RemoteDirectory;
  3001. bool ExistingFile = false;
  3002. if (!IsSideLocalBrowser(Side))
  3003. {
  3004. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3005. TRemoteFile * File = NULL;
  3006. if (Terminal->FileExists(Name, &File) &&
  3007. !File->IsDirectory)
  3008. {
  3009. try
  3010. {
  3011. ExecuteRemoteFile(Name, File, efDefaultEditor);
  3012. ExistingFile = true;
  3013. }
  3014. __finally
  3015. {
  3016. delete File;
  3017. }
  3018. }
  3019. if (!ExistingFile)
  3020. {
  3021. RemoteDirectory = UnixExtractFilePath(Name);
  3022. TemporaryDirectoryForRemoteFiles(
  3023. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3024. TargetFileName = UnixExtractFileName(Name);
  3025. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3026. LocalFileName = TempDir +
  3027. // We probably do not want to trim the VMS version here
  3028. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3029. }
  3030. }
  3031. else
  3032. {
  3033. if (ExtractFilePath(Name).IsEmpty())
  3034. {
  3035. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3036. }
  3037. else
  3038. {
  3039. LocalFileName = ExpandFileName(Name);
  3040. }
  3041. TargetFileName = ExtractFileName(Name);
  3042. }
  3043. if (!ExistingFile)
  3044. {
  3045. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3046. if (NewFile)
  3047. {
  3048. int File = FileCreate(ApiPath(LocalFileName));
  3049. if (File < 0)
  3050. {
  3051. if (!RootTempDir.IsEmpty())
  3052. {
  3053. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3054. }
  3055. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3056. }
  3057. else
  3058. {
  3059. FileClose(File);
  3060. }
  3061. }
  3062. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3063. const TEditorData * ExternalEditor = NULL;
  3064. TFileMasks::TParams MaskParams; // size not known
  3065. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3066. false, MaskParams);
  3067. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3068. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3069. }
  3070. }
  3071. }
  3072. }
  3073. //---------------------------------------------------------------------------
  3074. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3075. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3076. {
  3077. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3078. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3079. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3080. return
  3081. ((ExecuteFileBy == efInternalEditor)) ||
  3082. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3083. }
  3084. //---------------------------------------------------------------------------
  3085. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3086. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3087. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3088. UnicodeString RemoteDirectory, bool NewFile)
  3089. {
  3090. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3091. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3092. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3093. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3094. Side = GetSide(Side);
  3095. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3096. if (!IsSideLocalBrowser(Side))
  3097. {
  3098. Data->Terminal = Terminal;
  3099. Data->Queue = Queue;
  3100. Data->SessionData = CloneCurrentSessionData();
  3101. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3102. Data->RemoteDirectory = RemoteDirectory;
  3103. Data->SessionName = Terminal->SessionData->SessionName;
  3104. Data->LocalRootDirectory = LocalRootDirectory;
  3105. Data->OriginalFileName = OriginalFileName;
  3106. Data->Command = L""; // will be changed later for external editor
  3107. }
  3108. if (ExecuteFileBy == efInternalEditor)
  3109. {
  3110. if (!IsSideLocalBrowser(Side))
  3111. {
  3112. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3113. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3114. TForm * Editor;
  3115. try
  3116. {
  3117. Editor =
  3118. ShowEditorForm(
  3119. FileName, this,
  3120. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3121. SaveAllInternalEditors, AnyInternalEditorModified,
  3122. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3123. }
  3124. catch(...)
  3125. {
  3126. if (!LocalRootDirectory.IsEmpty())
  3127. {
  3128. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3129. }
  3130. throw;
  3131. }
  3132. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3133. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3134. }
  3135. else
  3136. {
  3137. DebugAssert(!StandaloneOperation);
  3138. TForm * Editor =
  3139. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3140. SaveAllInternalEditors, AnyInternalEditorModified,
  3141. L"", false, SessionColor, -1, NewFile);
  3142. FLocalEditors->Add(Editor);
  3143. }
  3144. }
  3145. else
  3146. {
  3147. HANDLE Process;
  3148. if (ExecuteFileBy == efExternalEditor)
  3149. {
  3150. UnicodeString Program, Params, Dir;
  3151. Data->Command = ExternalEditor->ExternalEditor;
  3152. ReformatFileNameCommand(Data->Command);
  3153. SplitCommand(Data->Command, Program, Params, Dir);
  3154. Params = ExpandFileNameCommand(Params, FileName);
  3155. Program = ExpandEnvironmentVariables(Program);
  3156. if (!ExecuteShell(Program, Params, Process))
  3157. {
  3158. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3159. }
  3160. }
  3161. else
  3162. {
  3163. DebugAssert(!IsSideLocalBrowser(Side));
  3164. if (!ExecuteShell(FileName, L"", Process))
  3165. {
  3166. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3167. }
  3168. }
  3169. if (IsSideLocalBrowser(Side) ||
  3170. ((ExecuteFileBy == efShell) &&
  3171. !WinConfiguration->Editor.SDIShellEditor) ||
  3172. ((ExecuteFileBy == efExternalEditor) &&
  3173. !ExternalEditor->SDIExternalEditor))
  3174. {
  3175. // no need for handle
  3176. if (Process != NULL)
  3177. {
  3178. DebugCheck(CloseHandle(Process));
  3179. }
  3180. Process = INVALID_HANDLE_VALUE;
  3181. }
  3182. else
  3183. {
  3184. if (Process == NULL)
  3185. {
  3186. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3187. }
  3188. }
  3189. if (!IsSideLocalBrowser(Side))
  3190. {
  3191. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3192. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3193. }
  3194. }
  3195. }
  3196. //---------------------------------------------------------------------------
  3197. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3198. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3199. void * /*Arg*/)
  3200. {
  3201. if (Token != NULL)
  3202. {
  3203. EditorFormFileSave(static_cast<TForm *>(Token));
  3204. }
  3205. }
  3206. //---------------------------------------------------------------------------
  3207. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3208. {
  3209. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3210. {
  3211. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3212. }
  3213. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3214. }
  3215. //---------------------------------------------------------------------------
  3216. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3217. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3218. void * Arg)
  3219. {
  3220. if ((Token != NULL) &&
  3221. IsEditorFormModified(static_cast<TForm *>(Token)))
  3222. {
  3223. *static_cast<bool *>(Arg) = true;
  3224. }
  3225. }
  3226. //---------------------------------------------------------------------------
  3227. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3228. TObject * /*Sender*/, bool & Modified)
  3229. {
  3230. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3231. {
  3232. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3233. {
  3234. Modified = true;
  3235. }
  3236. }
  3237. if (!Modified)
  3238. {
  3239. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3240. }
  3241. }
  3242. //---------------------------------------------------------------------------
  3243. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3244. {
  3245. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3246. }
  3247. //---------------------------------------------------------------------------
  3248. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3249. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3250. UnicodeString & Result, UnicodeString & RootDirectory)
  3251. {
  3252. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3253. {
  3254. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3255. Result = RootDirectory;
  3256. }
  3257. else
  3258. {
  3259. RootDirectory = L"";
  3260. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3261. Result = IncludeTrailingBackslash(Result);
  3262. }
  3263. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3264. {
  3265. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3266. }
  3267. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3268. {
  3269. UnicodeString ARemoteDirectory = RemoteDirectory;
  3270. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3271. {
  3272. ARemoteDirectory.Delete(1, 1);
  3273. }
  3274. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3275. }
  3276. if (!ForceDirectories(ApiPath(Result)))
  3277. {
  3278. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3279. }
  3280. }
  3281. //---------------------------------------------------------------------------
  3282. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3283. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3284. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3285. {
  3286. DebugAssert(!IsLocalBrowserMode());
  3287. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3288. if (ForceText)
  3289. {
  3290. CopyParam.TransferMode = tmAscii;
  3291. }
  3292. TemporaryFileCopyParam(CopyParam);
  3293. if (TempDir.IsEmpty())
  3294. {
  3295. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3296. }
  3297. DebugAssert(!FAutoOperation);
  3298. FAutoOperation = AutoOperation;
  3299. Terminal->ExceptionOnFail = true;
  3300. try
  3301. {
  3302. try
  3303. {
  3304. // turn off confirmations, as for MDI editors we may possibly download
  3305. // the same file over
  3306. Terminal->CopyToLocal(
  3307. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3308. if (GetTargetNames)
  3309. {
  3310. for (int i = 0; i < FileList->Count; i++)
  3311. {
  3312. FileList->Strings[i] =
  3313. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3314. }
  3315. }
  3316. }
  3317. catch(...)
  3318. {
  3319. if (!RootTempDir.IsEmpty())
  3320. {
  3321. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3322. }
  3323. throw;
  3324. }
  3325. }
  3326. __finally
  3327. {
  3328. FAutoOperation = false;
  3329. Terminal->ExceptionOnFail = false;
  3330. }
  3331. }
  3332. //---------------------------------------------------------------------------
  3333. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3334. {
  3335. // Do not waste time checking for default editor list the next time,
  3336. // or testing if default editor is not notepad.
  3337. bool TryNextTime = false;
  3338. UnicodeString UsageState;
  3339. if (!WinConfiguration->EditorList->IsDefaultList())
  3340. {
  3341. UsageState = "H";
  3342. }
  3343. else
  3344. {
  3345. UnicodeString Executable;
  3346. UnicodeString ExecutableDescription;
  3347. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3348. {
  3349. UnicodeString Message =
  3350. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3351. unsigned int Answer =
  3352. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3353. if (Answer != qaOK)
  3354. {
  3355. UsageState = "R";
  3356. }
  3357. else
  3358. {
  3359. UsageState = "A";
  3360. TEditorData EditorData;
  3361. EditorData.Editor = edExternal;
  3362. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3363. EditorData.ExternalEditorOptionsAutodetect();
  3364. TEditorList EditorList;
  3365. EditorList = *WinConfiguration->EditorList;
  3366. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3367. WinConfiguration->EditorList = &EditorList;
  3368. }
  3369. }
  3370. }
  3371. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3372. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3373. }
  3374. //---------------------------------------------------------------------------
  3375. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3376. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3377. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3378. {
  3379. if (ExecuteFileBy == efDefaultEditor)
  3380. {
  3381. if (!WinConfiguration->OfferedEditorAutoConfig)
  3382. {
  3383. EditorAutoConfig();
  3384. }
  3385. const TEditorPreferences * Editor =
  3386. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3387. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3388. {
  3389. ExecuteFileBy = efInternalEditor;
  3390. ExternalEditor = NULL;
  3391. }
  3392. else if (Editor->Data->Editor == edOpen)
  3393. {
  3394. ExecuteFileBy = efShell;
  3395. ExternalEditor = NULL;
  3396. }
  3397. else
  3398. {
  3399. ExecuteFileBy = efExternalEditor;
  3400. ExternalEditor = Editor->Data;
  3401. }
  3402. }
  3403. }
  3404. //---------------------------------------------------------------------------
  3405. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3406. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3407. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3408. {
  3409. UnicodeString OriginalFileName;
  3410. UnicodeString LocalRootDirectory;
  3411. UnicodeString RemoteDirectory;
  3412. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3413. IsSideLocalBrowser(Side), MaskParams);
  3414. UnicodeString Counter;
  3415. UnicodeString LocalFileName;
  3416. bool Handled = false;
  3417. if (!IsSideLocalBrowser(Side))
  3418. {
  3419. // We need to trim VMS version here, so that we use name without version
  3420. // when uploading back to create a new version of the file
  3421. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3422. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3423. TObject * Token = NULL;
  3424. UnicodeString LocalDirectory;
  3425. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3426. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3427. LocalDirectory))
  3428. {
  3429. if (Token != NULL)
  3430. {
  3431. TForm * Form = dynamic_cast<TForm *>(Token);
  3432. if (Form->WindowState == wsMinimized)
  3433. {
  3434. ShowWindow(Form->Handle, SW_RESTORE);
  3435. }
  3436. else
  3437. {
  3438. Form->SetFocus();
  3439. }
  3440. Handled = true;
  3441. }
  3442. else
  3443. {
  3444. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3445. }
  3446. }
  3447. if (!Handled)
  3448. {
  3449. TStringList * FileList1 = new TStringList();
  3450. try
  3451. {
  3452. FileList1->AddObject(FullFileName, Object);
  3453. TemporarilyDownloadFiles(FileList1,
  3454. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3455. LocalRootDirectory, LocalDirectory, true, true, false);
  3456. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3457. }
  3458. __finally
  3459. {
  3460. delete FileList1;
  3461. }
  3462. switch (ExecuteFileBy)
  3463. {
  3464. case efShell:
  3465. Counter = "RemoteFilesExecuted";
  3466. break;
  3467. case efInternalEditor:
  3468. Counter = "RemoteFilesOpenedInInternalEditor";
  3469. break;
  3470. case efExternalEditor:
  3471. Counter = "RemoteFilesOpenedInExternalEditor";
  3472. break;
  3473. default:
  3474. DebugFail();
  3475. }
  3476. }
  3477. }
  3478. else
  3479. {
  3480. LocalFileName = FullFileName;
  3481. OriginalFileName = ExtractFileName(FullFileName);
  3482. switch (ExecuteFileBy)
  3483. {
  3484. case efShell:
  3485. Counter = "LocalFilesExecuted";
  3486. break;
  3487. case efInternalEditor:
  3488. Counter = "LocalFilesOpenedInInternalEditor";
  3489. break;
  3490. case efExternalEditor:
  3491. Counter = "LocalFilesOpenedInExternalEditor";
  3492. break;
  3493. default:
  3494. DebugFail();
  3495. }
  3496. }
  3497. if (!Handled)
  3498. {
  3499. Configuration->Usage->Inc(Counter);
  3500. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3501. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3502. }
  3503. }
  3504. //---------------------------------------------------------------------------
  3505. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3506. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3507. bool AllSelected, bool OnFocused)
  3508. {
  3509. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3510. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3511. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3512. Side = GetSide(Side);
  3513. TCustomDirView * DView = DirView(Side);
  3514. TStrings * FileList =
  3515. AllSelected ?
  3516. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3517. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3518. try
  3519. {
  3520. DebugAssert(AllSelected || (FileList->Count == 1));
  3521. for (int i = 0; i < FileList->Count; i++)
  3522. {
  3523. UnicodeString ListFileName = FileList->Strings[i];
  3524. UnicodeString FileNameOnly =
  3525. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3526. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3527. if (!DView->ItemIsDirectory(Item))
  3528. {
  3529. UnicodeString FullFileName;
  3530. if (!IsSideLocalBrowser(Side))
  3531. {
  3532. FullFileName = DirView(Side)->Path + ListFileName;
  3533. }
  3534. else
  3535. {
  3536. FullFileName = ListFileName;
  3537. }
  3538. TObject * Object = FileList->Objects[i];
  3539. TFileMasks::TParams MaskParams;
  3540. MaskParams.Size = DView->ItemFileSize(Item);
  3541. TDateTimePrecision Precision;
  3542. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3543. std::unique_ptr<TRemoteFile> FileOwner;
  3544. if (!IsSideLocalBrowser(Side))
  3545. {
  3546. TRemoteFile * File = NULL;
  3547. Terminal->FileExists(FullFileName, &File);
  3548. if (File != NULL)
  3549. {
  3550. File->FullFileName = FullFileName;
  3551. Object = File;
  3552. FileOwner.reset(File);
  3553. MaskParams.Size = File->Size;
  3554. MaskParams.Modification = File->Modification;
  3555. }
  3556. }
  3557. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3558. }
  3559. }
  3560. }
  3561. __finally
  3562. {
  3563. delete FileList;
  3564. }
  3565. }
  3566. //---------------------------------------------------------------------------
  3567. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3568. {
  3569. CopyParam.FileNameCase = ncNoChange;
  3570. CopyParam.PreserveRights = false;
  3571. CopyParam.PreserveReadOnly = false;
  3572. CopyParam.ReplaceInvalidChars = true;
  3573. CopyParam.IncludeFileMask = TFileMasks();
  3574. CopyParam.NewerOnly = false;
  3575. CopyParam.FileMask = L"";
  3576. }
  3577. //---------------------------------------------------------------------------
  3578. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3579. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3580. {
  3581. TTerminalManager * Manager = TTerminalManager::Instance();
  3582. if (!IsActiveTerminal(Data->Terminal))
  3583. {
  3584. if (!NonVisualDataModule->Busy)
  3585. {
  3586. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3587. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3588. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3589. if (Data->Terminal == NULL)
  3590. {
  3591. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3592. if (SameSiteTerminal != NULL)
  3593. {
  3594. UnicodeString Message =
  3595. FMTLOAD(EDIT_SESSION_REATTACH,
  3596. (FileNameOnly, Data->SessionName, FileNameOnly));
  3597. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3598. {
  3599. Data->Terminal = SameSiteTerminal;
  3600. Data->Queue = Manager->FindQueueForTerminal(SameSiteTerminal);
  3601. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3602. // We might also overwrite session data
  3603. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3604. }
  3605. else
  3606. {
  3607. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3608. Abort();
  3609. }
  3610. }
  3611. }
  3612. // foreground session should reconnect itself
  3613. else if (Terminal != Data->Terminal)
  3614. {
  3615. UnicodeString Message =
  3616. MainInstructions(
  3617. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3618. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3619. {
  3620. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3621. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3622. }
  3623. else
  3624. {
  3625. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3626. Abort();
  3627. }
  3628. }
  3629. }
  3630. if (!IsActiveTerminal(Data->Terminal))
  3631. {
  3632. Configuration->Usage->Inc(L"EditInactiveSession");
  3633. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3634. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3635. (ExtractFileName(FileName), Data->SessionName)));
  3636. }
  3637. }
  3638. TStrings * FileList = new TStringList();
  3639. try
  3640. {
  3641. FileList->Add(FileName);
  3642. // Consider using the same settings (preset) as when the file was downloaded.
  3643. // More over this does not reflect the actual session that will do the upload.
  3644. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3645. TemporaryFileCopyParam(CopyParam);
  3646. if (Data->ForceText)
  3647. {
  3648. CopyParam.TransferMode = tmAscii;
  3649. }
  3650. // so i do not need to worry if masking algorithm works in all cases
  3651. // ("" means "copy file name", no masking is actually done)
  3652. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3653. {
  3654. CopyParam.FileMask = L"";
  3655. }
  3656. else
  3657. {
  3658. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3659. }
  3660. int Params = cpNoConfirmation | cpTemporary;
  3661. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3662. {
  3663. DebugAssert(Data->Queue != NULL);
  3664. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3665. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3666. QueueItem->CompleteEvent = UploadCompleteEvent;
  3667. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3668. }
  3669. else
  3670. {
  3671. if (NonVisualDataModule->Busy)
  3672. {
  3673. Retry = true;
  3674. }
  3675. else
  3676. {
  3677. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3678. SetEvent(UploadCompleteEvent);
  3679. }
  3680. }
  3681. }
  3682. __finally
  3683. {
  3684. delete FileList;
  3685. }
  3686. }
  3687. //---------------------------------------------------------------------------
  3688. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3689. const UnicodeString FileName, const TEditedFileData * Data)
  3690. {
  3691. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3692. DebugAssert(!NonVisualDataModule->Busy);
  3693. if (!IsActiveTerminal(Data->Terminal))
  3694. {
  3695. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3696. (ExtractFileName(FileName), Data->SessionName)));
  3697. }
  3698. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3699. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3700. NonVisualDataModule->StartBusy();
  3701. try
  3702. {
  3703. std::unique_ptr<TRemoteFile> File;
  3704. UnicodeString RemoteFileName =
  3705. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3706. Terminal->ExceptionOnFail = true;
  3707. try
  3708. {
  3709. TRemoteFile * AFile = NULL;
  3710. Terminal->ReadFile(RemoteFileName, AFile);
  3711. File.reset(AFile);
  3712. if (!File->HaveFullFileName)
  3713. {
  3714. File->FullFileName = RemoteFileName;
  3715. }
  3716. }
  3717. __finally
  3718. {
  3719. Terminal->ExceptionOnFail = false;
  3720. }
  3721. std::unique_ptr<TStrings> FileList(new TStringList());
  3722. FileList->AddObject(RemoteFileName, File.get());
  3723. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3724. UnicodeString TempDir = ExtractFilePath(FileName);
  3725. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3726. // sanity check, the target file name should be still the same
  3727. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3728. }
  3729. __finally
  3730. {
  3731. NonVisualDataModule->EndBusy();
  3732. // it actually may not exist anymore...
  3733. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3734. }
  3735. }
  3736. //---------------------------------------------------------------------------
  3737. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3738. const TEditedFileData * Data, bool & KeepOpen)
  3739. {
  3740. // Command is set for external editors only (not for "shell" open).
  3741. if (!Data->Command.IsEmpty())
  3742. {
  3743. bool AnyFound = false;
  3744. bool AnyMDI = false;
  3745. bool AnyNonMDI = false;
  3746. bool AnyDetect = false;
  3747. TEditorList * EditorList = new TEditorList();
  3748. try
  3749. {
  3750. *EditorList = *WinConfiguration->EditorList;
  3751. for (int i = 0; i < EditorList->Count; i++)
  3752. {
  3753. const TEditorPreferences * Editor = EditorList->Editors[i];
  3754. if ((Editor->Data->Editor == edExternal) &&
  3755. (Editor->Data->ExternalEditor == Data->Command))
  3756. {
  3757. AnyFound = true;
  3758. if (Editor->Data->SDIExternalEditor)
  3759. {
  3760. AnyNonMDI = true;
  3761. if (Editor->Data->DetectMDIExternalEditor)
  3762. {
  3763. AnyDetect = true;
  3764. }
  3765. }
  3766. else
  3767. {
  3768. AnyMDI = true;
  3769. }
  3770. }
  3771. }
  3772. bool EnableMDI = false;
  3773. bool DisableDetect = false;
  3774. if (AnyMDI)
  3775. {
  3776. KeepOpen = true;
  3777. if (AnyNonMDI)
  3778. {
  3779. // there is at least one instance of the editor with MDI support enabled,
  3780. // and one with disabled, enable it for all instances
  3781. EnableMDI = true;
  3782. }
  3783. }
  3784. else if (AnyFound && !AnyDetect)
  3785. {
  3786. // at least once instance found but all have MDI autodetection disabled
  3787. // => close the file (default action)
  3788. }
  3789. else
  3790. {
  3791. // no instance of the editor has MDI support enabled
  3792. TMessageParams Params;
  3793. if (AnyFound)
  3794. {
  3795. // there is at least one instance of the editor
  3796. Params.Params |= mpNeverAskAgainCheck;
  3797. }
  3798. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3799. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3800. switch (Answer)
  3801. {
  3802. case qaNeverAskAgain:
  3803. DisableDetect = true;
  3804. break;
  3805. case qaNo:
  3806. EnableMDI = true;
  3807. KeepOpen = true;
  3808. break;
  3809. }
  3810. }
  3811. if (AnyFound && (EnableMDI || DisableDetect))
  3812. {
  3813. bool Changed = false;
  3814. for (int i = 0; i < EditorList->Count; i++)
  3815. {
  3816. const TEditorPreferences * Editor = EditorList->Editors[i];
  3817. if ((Editor->Data->Editor == edExternal) &&
  3818. (Editor->Data->ExternalEditor == Data->Command) &&
  3819. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3820. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3821. {
  3822. Changed = true;
  3823. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3824. if (EnableMDI)
  3825. {
  3826. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3827. }
  3828. if (DisableDetect)
  3829. {
  3830. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3831. }
  3832. EditorList->Change(i, UpdatedEditor);
  3833. }
  3834. }
  3835. if (Changed)
  3836. {
  3837. WinConfiguration->EditorList = EditorList;
  3838. }
  3839. }
  3840. }
  3841. __finally
  3842. {
  3843. delete EditorList;
  3844. }
  3845. }
  3846. else
  3847. {
  3848. // "open" case
  3849. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3850. qaOK, HELP_APP_EARLY_CLOSED);
  3851. }
  3852. }
  3853. //---------------------------------------------------------------------------
  3854. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3855. {
  3856. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3857. }
  3858. //---------------------------------------------------------------------------
  3859. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3860. {
  3861. SideEnter(osRemote);
  3862. }
  3863. //---------------------------------------------------------------------------
  3864. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3865. {
  3866. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3867. SideEnter(osRemote);
  3868. }
  3869. //---------------------------------------------------------------------------
  3870. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3871. {
  3872. FCurrentSide = Side;
  3873. if (Visible)
  3874. {
  3875. UpdateControls();
  3876. }
  3877. }
  3878. //---------------------------------------------------------------------------
  3879. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3880. {
  3881. VisualiseOperationFinished(Side, FileName, Success);
  3882. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3883. {
  3884. FDeletedFiles->Add(FileName);
  3885. }
  3886. }
  3887. //---------------------------------------------------------------------------
  3888. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3889. TStrings * FileList, bool Alternative)
  3890. {
  3891. TCustomDirView * DView = DirView(Side);
  3892. DView->SaveSelection();
  3893. DView->SaveSelectedNames();
  3894. DebugAssert(!FAlternativeDelete);
  3895. FAlternativeDelete = Alternative;
  3896. try
  3897. {
  3898. if (!IsSideLocalBrowser(Side))
  3899. {
  3900. DebugAssert(Terminal != NULL);
  3901. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3902. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3903. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3904. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3905. int Params = FLAGMASK(Alternative, dfAlternative);
  3906. try
  3907. {
  3908. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3909. DebugAssert(FDeletedFiles == NULL);
  3910. FDeletedFiles = DeletedFiles.get();
  3911. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3912. FOnFileOperationFinished = FileDeleted;
  3913. FMoveToQueue = false;
  3914. Terminal->DeleteFiles(FileList, Params);
  3915. // Probably not needed for deleting, just for consistency with transfer code
  3916. if (FMoveToQueue)
  3917. {
  3918. Abort();
  3919. }
  3920. }
  3921. catch (EAbort &)
  3922. {
  3923. if (FMoveToQueue)
  3924. {
  3925. DeletedFiles->Sorted = true;
  3926. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3927. {
  3928. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3929. {
  3930. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3931. DebugAssert(Index == 0);
  3932. PermanentFileList->Delete(Index);
  3933. Index--;
  3934. }
  3935. }
  3936. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3937. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3938. AddQueueItem(Queue, QueueItem, Terminal);
  3939. ClearOperationSelection(osRemote);
  3940. }
  3941. }
  3942. }
  3943. else
  3944. {
  3945. if (IsLocalBrowserMode())
  3946. {
  3947. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3948. }
  3949. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3950. FProgressSide = FCurrentSide;
  3951. try
  3952. {
  3953. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3954. }
  3955. __finally
  3956. {
  3957. ReloadLocalDirectory();
  3958. }
  3959. }
  3960. FAlternativeDelete = false;
  3961. }
  3962. catch(...)
  3963. {
  3964. FAlternativeDelete = false;
  3965. DView->DiscardSavedSelection();
  3966. throw;
  3967. }
  3968. DView->RestoreSelection();
  3969. }
  3970. //---------------------------------------------------------------------------
  3971. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3972. {
  3973. DebugAssert(Terminal);
  3974. DebugAssert(!IsLocalBrowserMode());
  3975. RemoteDirView->SaveSelection();
  3976. RemoteDirView->SaveSelectedNames();
  3977. try
  3978. {
  3979. if (Lock)
  3980. {
  3981. Terminal->LockFiles(FileList);
  3982. }
  3983. else
  3984. {
  3985. Terminal->UnlockFiles(FileList);
  3986. }
  3987. }
  3988. catch(...)
  3989. {
  3990. RemoteDirView->DiscardSavedSelection();
  3991. throw;
  3992. }
  3993. RemoteDirView->RestoreSelection();
  3994. }
  3995. //---------------------------------------------------------------------------
  3996. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3997. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3998. bool NoConfirmation, bool Move)
  3999. {
  4000. DebugAssert(Terminal != NULL);
  4001. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4002. DebugAssert(!IsLocalBrowserMode());
  4003. if (Session == NULL)
  4004. {
  4005. Session = Terminal;
  4006. }
  4007. if (Session == Terminal)
  4008. {
  4009. if (RemoteDriveView->DropTarget != NULL)
  4010. {
  4011. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4012. }
  4013. else if (RemoteDirView->DropTarget != NULL)
  4014. {
  4015. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4016. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4017. }
  4018. else
  4019. {
  4020. Target = RemoteDirView->Path;
  4021. }
  4022. }
  4023. else
  4024. {
  4025. Target = GetSessionPath(Session, osRemote);
  4026. }
  4027. Target = UnixIncludeTrailingBackslash(Target);
  4028. if (FileList->Count == 1)
  4029. {
  4030. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4031. }
  4032. else
  4033. {
  4034. FileMask = AnyMask;
  4035. }
  4036. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4037. bool Result = true;
  4038. if (!NoConfirmation)
  4039. {
  4040. bool Multi = (FileList->Count > 1);
  4041. if (Move)
  4042. {
  4043. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4044. }
  4045. else
  4046. {
  4047. std::unique_ptr<TStrings> Sessions(new TStringList);
  4048. std::unique_ptr<TStrings> Directories(new TStringList);
  4049. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4050. for (int Index = 0; Index < SessionList->Count; Index++)
  4051. {
  4052. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4053. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4054. {
  4055. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4056. Directories->Add(GetSessionPath(ASession, osRemote));
  4057. }
  4058. }
  4059. TDirectRemoteCopy AllowDirectCopy;
  4060. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4061. {
  4062. DebugAssert(DirectCopy);
  4063. AllowDirectCopy = drcAllow;
  4064. }
  4065. else if (Terminal->IsCapable[fcSecondaryShell])
  4066. {
  4067. DebugAssert(DirectCopy);
  4068. AllowDirectCopy = drcConfirmCommandSession;
  4069. }
  4070. else
  4071. {
  4072. DebugAssert(!DirectCopy);
  4073. AllowDirectCopy = drcDisallow;
  4074. }
  4075. void * ASession = Session;
  4076. Result = DoRemoteCopyDialog(
  4077. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4078. Session = static_cast<TManagedTerminal *>(ASession);
  4079. }
  4080. }
  4081. return Result;
  4082. }
  4083. //---------------------------------------------------------------------------
  4084. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4085. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4086. {
  4087. DebugAssert(!IsLocalBrowserMode());
  4088. bool DirectCopy;
  4089. UnicodeString Target, FileMask;
  4090. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4091. if (Result)
  4092. {
  4093. if (!Move && !DirectCopy)
  4094. {
  4095. Configuration->Usage->Inc("RemoteCopyTemp");
  4096. UnicodeString RootTempDir;
  4097. UnicodeString TempDir;
  4098. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4099. TStrings * TemporaryFilesList = new TStringList();
  4100. try
  4101. {
  4102. TMakeLocalFileListParams MakeFileListParam;
  4103. MakeFileListParam.FileList = TemporaryFilesList;
  4104. MakeFileListParam.FileTimes = NULL;
  4105. MakeFileListParam.IncludeDirs = true;
  4106. MakeFileListParam.Recursive = false;
  4107. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4108. TTerminalManager::Instance()->ActiveSession = Session;
  4109. if (TemporaryFilesList->Count > 0)
  4110. {
  4111. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4112. CopyParam.FileMask = FileMask;
  4113. DebugAssert(!FAutoOperation);
  4114. FAutoOperation = true;
  4115. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4116. }
  4117. }
  4118. __finally
  4119. {
  4120. delete TemporaryFilesList;
  4121. FAutoOperation = false;
  4122. if (!RootTempDir.IsEmpty())
  4123. {
  4124. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4125. }
  4126. }
  4127. }
  4128. else
  4129. {
  4130. RemoteDirView->SaveSelection();
  4131. RemoteDirView->SaveSelectedNames();
  4132. try
  4133. {
  4134. if (Move)
  4135. {
  4136. Configuration->Usage->Inc("RemoteMove");
  4137. Terminal->MoveFiles(FileList, Target, FileMask);
  4138. }
  4139. else
  4140. {
  4141. Configuration->Usage->Inc("RemoteCopyDirect");
  4142. DebugAssert(DirectCopy);
  4143. DebugAssert(Session == Terminal);
  4144. if (Terminal->IsCapable[fcRemoteCopy] ||
  4145. Terminal->CommandSessionOpened ||
  4146. CommandSessionFallback())
  4147. {
  4148. Terminal->CopyFiles(FileList, Target, FileMask);
  4149. }
  4150. }
  4151. }
  4152. catch(...)
  4153. {
  4154. RemoteDirView->DiscardSavedSelection();
  4155. throw;
  4156. }
  4157. RemoteDirView->RestoreSelection();
  4158. }
  4159. }
  4160. return Result;
  4161. }
  4162. //---------------------------------------------------------------------------
  4163. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4164. const UnicodeString & Path, TRemoteProperties & Properties)
  4165. {
  4166. DebugAssert(!IsLocalBrowserMode());
  4167. Properties.Valid = Properties.Valid << vpEncrypt;
  4168. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4169. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4170. }
  4171. //---------------------------------------------------------------------------
  4172. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4173. {
  4174. Side = GetSide(Side);
  4175. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4176. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4177. UnicodeString Name = LoadStr(NEW_FOLDER);
  4178. int AllowedChanges =
  4179. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4180. bool SaveSettings = false;
  4181. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4182. {
  4183. TWindowLock Lock(this);
  4184. if (!IsSideLocalBrowser(Side))
  4185. {
  4186. if (SaveSettings)
  4187. {
  4188. GUIConfiguration->NewDirectoryProperties = Properties;
  4189. }
  4190. CreateRemoteDirectory(Name, Properties);
  4191. }
  4192. else
  4193. {
  4194. DirView(Side)->CreateDirectory(Name);
  4195. }
  4196. }
  4197. }
  4198. //---------------------------------------------------------------------------
  4199. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4200. {
  4201. TWindowLock Lock(this);
  4202. DirView(Side)->ExecuteHomeDirectory();
  4203. }
  4204. //---------------------------------------------------------------------------
  4205. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4206. {
  4207. DoOpenDirectoryDialog(odBrowse, Side);
  4208. }
  4209. //---------------------------------------------------------------------------
  4210. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4211. {
  4212. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4213. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4214. bool Result = !Path.IsEmpty();
  4215. if (Result)
  4216. {
  4217. TryOpenDirectory(Side, Path);
  4218. }
  4219. return Result;
  4220. }
  4221. //---------------------------------------------------------------------------
  4222. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4223. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4224. bool & Close)
  4225. {
  4226. // terminal can be already closed (e.g. dropped connection)
  4227. if (Terminal != NULL)
  4228. {
  4229. try
  4230. {
  4231. TCalculateSizeParams Params;
  4232. Params.Stats = &Stats;
  4233. Terminal->CalculateFilesSize(FileList, Size, Params);
  4234. }
  4235. catch(...)
  4236. {
  4237. if (!Terminal->Active)
  4238. {
  4239. Close = true;
  4240. }
  4241. throw;
  4242. }
  4243. }
  4244. }
  4245. //---------------------------------------------------------------------------
  4246. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4247. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4248. bool & Close)
  4249. {
  4250. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4251. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4252. {
  4253. Configuration->Usage->Inc(L"ChecksumCalculated");
  4254. try
  4255. {
  4256. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4257. }
  4258. catch(...)
  4259. {
  4260. if (!Terminal->Active)
  4261. {
  4262. Close = true;
  4263. }
  4264. throw;
  4265. }
  4266. }
  4267. }
  4268. //---------------------------------------------------------------------------
  4269. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4270. {
  4271. if (Terminal->LoadFilesProperties(FileList))
  4272. {
  4273. RemoteDirView->Invalidate();
  4274. }
  4275. }
  4276. //---------------------------------------------------------------------------
  4277. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4278. {
  4279. bool Result;
  4280. if (!IsSideLocalBrowser(Side))
  4281. {
  4282. TRemoteTokenList * GroupList = NULL;
  4283. TRemoteTokenList * UserList = NULL;
  4284. try
  4285. {
  4286. TRemoteProperties CurrentProperties;
  4287. LoadFilesProperties(FileList);
  4288. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4289. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4290. if (CapableGroupChanging || CapableOwnerChanging)
  4291. {
  4292. if (CapableGroupChanging)
  4293. {
  4294. GroupList = Terminal->Groups->Duplicate();
  4295. }
  4296. if (CapableOwnerChanging)
  4297. {
  4298. UserList = Terminal->Users->Duplicate();
  4299. }
  4300. TRemoteDirectory * Files = Terminal->Files;
  4301. int Count = Files->Count;
  4302. if (Count > 100)
  4303. {
  4304. Count = 100;
  4305. }
  4306. for (int Index = 0; Index < Count; Index++)
  4307. {
  4308. TRemoteFile * File = Files->Files[Index];
  4309. if (CapableGroupChanging)
  4310. {
  4311. GroupList->AddUnique(File->Group);
  4312. }
  4313. if (CapableOwnerChanging)
  4314. {
  4315. UserList->AddUnique(File->Owner);
  4316. }
  4317. }
  4318. // if we haven't collected tokens for all files in current directory,
  4319. // make sure we collect them at least for all selected files.
  4320. // (note that so far the files in FileList has to be from current direcotry)
  4321. if (Count < Files->Count)
  4322. {
  4323. for (int Index = 0; Index < FileList->Count; Index++)
  4324. {
  4325. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4326. if (CapableGroupChanging)
  4327. {
  4328. GroupList->AddUnique(File->Group);
  4329. }
  4330. if (CapableOwnerChanging)
  4331. {
  4332. UserList->AddUnique(File->Owner);
  4333. }
  4334. }
  4335. }
  4336. }
  4337. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4338. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4339. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4340. {
  4341. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4342. {
  4343. CapableAclChanging = false;
  4344. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4345. }
  4346. }
  4347. int Flags = 0;
  4348. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4349. if (CapableAclChanging) Flags |= cpAcl;
  4350. if (CapableOwnerChanging) Flags |= cpOwner;
  4351. if (CapableGroupChanging) Flags |= cpGroup;
  4352. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4353. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4354. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4355. {
  4356. CalculateChecksumEvent = CalculateChecksum;
  4357. }
  4358. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4359. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4360. TRemoteProperties NewProperties = CurrentProperties;
  4361. Result =
  4362. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4363. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4364. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4365. CalculateSize, CalculateChecksumEvent);
  4366. if (Result)
  4367. {
  4368. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4369. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4370. }
  4371. }
  4372. __finally
  4373. {
  4374. delete GroupList;
  4375. delete UserList;
  4376. }
  4377. }
  4378. else
  4379. {
  4380. DirView(Side)->DisplayPropertiesMenu();
  4381. Result = true;
  4382. }
  4383. return Result;
  4384. }
  4385. //---------------------------------------------------------------------------
  4386. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4387. {
  4388. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4389. {
  4390. FIgnoreNextDialogChar = Key;
  4391. }
  4392. Key = 0;
  4393. }
  4394. //---------------------------------------------------------------------------
  4395. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4396. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4397. {
  4398. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4399. if (Command != NULL)
  4400. {
  4401. KeyProcessed(Key, Shift);
  4402. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4403. {
  4404. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4405. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4406. false, false, const_cast<TCustomCommandType *>(Command));
  4407. }
  4408. }
  4409. }
  4410. //---------------------------------------------------------------------------
  4411. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4412. {
  4413. if (!NonVisualDataModule->Busy)
  4414. {
  4415. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4416. {
  4417. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4418. }
  4419. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4420. {
  4421. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4422. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4423. {
  4424. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4425. // But so far it's not a problem, as it does what we want.
  4426. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4427. if (((Action->ShortCut == KeyShortCut) ||
  4428. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4429. AllowedAction(Action, aaShortCut))
  4430. {
  4431. // Has to be called before the action as the dialog char is already in queue.
  4432. // So when the action consumes message queue, we already need to have the
  4433. // FIgnoreNextDialogChar set
  4434. KeyProcessed(Key, Shift);
  4435. // Reset reference to previous component (when menu/toolbar was clicked).
  4436. // Needed to detect that action was invoked by keyboard shortcut
  4437. // in TNonVisualDataModule::ExplorerActionsExecute
  4438. Action->ActionComponent = NULL;
  4439. Action->Execute();
  4440. return;
  4441. }
  4442. }
  4443. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4444. {
  4445. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4446. {
  4447. KeyProcessed(Key, Shift);
  4448. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4449. return;
  4450. }
  4451. }
  4452. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4453. {
  4454. KeyProcessed(Key, Shift);
  4455. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4456. }
  4457. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4458. if (IsCustomShortCut(CustomShortCut))
  4459. {
  4460. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4461. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4462. if (WinConfiguration->SharedBookmarks != NULL)
  4463. {
  4464. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4465. if ((Bookmark != NULL) &&
  4466. OpenBookmark(FCurrentSide, Bookmark))
  4467. {
  4468. KeyProcessed(Key, Shift);
  4469. }
  4470. }
  4471. }
  4472. }
  4473. }
  4474. TForm::KeyDown(Key, Shift);
  4475. }
  4476. //---------------------------------------------------------------------------
  4477. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4478. {
  4479. bool OpenedTerminal = false;
  4480. UnicodeString ProtocolName;
  4481. UnicodeString ProtocolNameHint;
  4482. UnicodeString SecurityProtocolName;
  4483. UnicodeString DurationHint;
  4484. // See the comment in TScpCommanderForm::UpdateControls()
  4485. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4486. if (ActiveTerminal != NULL)
  4487. {
  4488. // Active is not enough here
  4489. if (ActiveTerminal->Status == ssOpened)
  4490. {
  4491. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4492. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4493. if (FileSystemInfo.ProtocolName.IsEmpty())
  4494. {
  4495. ProtocolName = SessionInfo.ProtocolName;
  4496. }
  4497. else
  4498. {
  4499. ProtocolName = FileSystemInfo.ProtocolName;
  4500. }
  4501. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4502. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4503. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4504. OpenedTerminal = true;
  4505. }
  4506. else if (ActiveTerminal->Status > ssClosed)
  4507. {
  4508. ProtocolName = LoadStr(STATUS_CONNECTING);
  4509. }
  4510. else
  4511. {
  4512. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4513. }
  4514. }
  4515. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4516. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4517. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4518. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4519. if (ProtocolStatusPanel->Caption != ProtocolName)
  4520. {
  4521. ProtocolStatusPanel->Caption = ProtocolName;
  4522. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4523. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4524. }
  4525. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4526. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4527. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4528. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4529. SecurityStatusPanel->Enabled = SecurityEnabled;
  4530. UnicodeString SecurityProtocolHint;
  4531. if (SecurityEnabled)
  4532. {
  4533. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4534. }
  4535. else
  4536. {
  4537. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4538. }
  4539. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4540. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4541. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4542. DurationStatusPanel->Hint = DurationHint;
  4543. UpdateStatusBar();
  4544. }
  4545. //---------------------------------------------------------------------------
  4546. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4547. {
  4548. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4549. DebugAssert(SessionStatusBar != NULL);
  4550. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4551. if (!FNote.IsEmpty())
  4552. {
  4553. NoteStatusPanel->Caption = FNote;
  4554. }
  4555. else
  4556. {
  4557. UpdateStatusPanelText(NoteStatusPanel);
  4558. }
  4559. NoteStatusPanel->Hint = FNoteHints;
  4560. UnicodeString LoginTime;
  4561. // See the comment in TScpCommanderForm::UpdateControls()
  4562. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4563. // Active is not enough here
  4564. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4565. {
  4566. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4567. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4568. }
  4569. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4570. DurationStatusPanel->Caption = LoginTime;
  4571. }
  4572. //---------------------------------------------------------------------------
  4573. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4574. {
  4575. Panel->Caption = L"";
  4576. }
  4577. //---------------------------------------------------------------------------
  4578. void __fastcall TCustomScpExplorerForm::Idle()
  4579. {
  4580. if (FShowing ||
  4581. // Particularly for
  4582. // - Standalone editing
  4583. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4584. // while "keeping remote directory up to date".
  4585. StandaloneOperation)
  4586. {
  4587. FEditorManager->Check();
  4588. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4589. // that needs to know if queue view is visible (and it may be closed after queue update)
  4590. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4591. }
  4592. if (FShowing)
  4593. {
  4594. if (!NonVisualDataModule->Busy &&
  4595. // Menu is opened or drag&drop is going on
  4596. (Mouse->Capture == NULL))
  4597. {
  4598. if (FRefreshRemoteDirectory)
  4599. {
  4600. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4601. {
  4602. Terminal->RefreshDirectory();
  4603. }
  4604. FRefreshRemoteDirectory = false;
  4605. }
  4606. if (FRefreshLocalDirectory)
  4607. {
  4608. ReloadLocalDirectory();
  4609. FRefreshLocalDirectory = false;
  4610. }
  4611. if (WinConfiguration->RefreshRemotePanel)
  4612. {
  4613. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4614. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4615. {
  4616. RemoteDirView->ReloadDirectory();
  4617. }
  4618. }
  4619. }
  4620. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4621. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4622. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4623. // And the Alt key can be released even when other window or application is active, so handling the key up in explorer window won't do either.
  4624. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4625. MenuToolbar->Idle();
  4626. }
  4627. if (FShowing || StandaloneOperation)
  4628. {
  4629. if (FQueueStatusInvalidated)
  4630. {
  4631. UpdateQueueStatus(false);
  4632. }
  4633. RefreshQueueItems();
  4634. }
  4635. if (FShowing)
  4636. {
  4637. UpdateStatusBar();
  4638. }
  4639. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4640. {
  4641. FClipboardFakeMonitorsPendingReset = false;
  4642. FClipboardFakeMonitors.reset(NULL);
  4643. }
  4644. FIgnoreNextDialogChar = 0;
  4645. }
  4646. //---------------------------------------------------------------------------
  4647. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4648. {
  4649. try
  4650. {
  4651. FUserActionTimer->Enabled = false;
  4652. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4653. {
  4654. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4655. {
  4656. FPendingQueueActionItem->ProcessUserAction();
  4657. }
  4658. }
  4659. }
  4660. __finally
  4661. {
  4662. FPendingQueueActionItem = NULL;
  4663. }
  4664. }
  4665. //---------------------------------------------------------------------------
  4666. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4667. {
  4668. if (WinConfiguration->MinimizeToTray)
  4669. {
  4670. UpdateTrayIcon();
  4671. FTrayIcon->Visible = true;
  4672. if (Visible)
  4673. {
  4674. ShowWindow(Handle, SW_HIDE);
  4675. }
  4676. }
  4677. }
  4678. //---------------------------------------------------------------------------
  4679. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4680. {
  4681. // WORKAROUND
  4682. // When restoring maximized window from minimization,
  4683. // rarely some controls do not align properly.
  4684. // Two instances seen (both for Commander):
  4685. // - When restoring, window is temporarily narrower (not maximized),
  4686. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4687. // Once maximized already, top dock shinks back, but the session PageControl,
  4688. // do not align up, leaving space between TopDock and PageControl.
  4689. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4690. for (int Index = 0; Index < ControlCount; Index++)
  4691. {
  4692. RealignControl(Controls[Index]);
  4693. }
  4694. if (FTrayIcon->Visible)
  4695. {
  4696. FTrayIcon->Visible = false;
  4697. if (Visible)
  4698. {
  4699. ShowWindow(Handle, SW_SHOW);
  4700. }
  4701. }
  4702. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4703. {
  4704. FNeedSession = false;
  4705. NonVisualDataModule->StartBusy();
  4706. try
  4707. {
  4708. NeedSession(true);
  4709. }
  4710. __finally
  4711. {
  4712. NonVisualDataModule->EndBusy();
  4713. }
  4714. }
  4715. }
  4716. //---------------------------------------------------------------------------
  4717. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4718. {
  4719. FTrayIcon->Hint = GetMainForm()->Caption;
  4720. }
  4721. //---------------------------------------------------------------------------
  4722. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4723. {
  4724. UpdateTrayIcon();
  4725. }
  4726. //---------------------------------------------------------------------------
  4727. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4728. {
  4729. // workaround
  4730. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4731. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4732. // But WM_ACTIVATEAPP message can be received even when
  4733. // the main window is minimized to the tray and internal editor window is focused
  4734. // (after another application was previously active)
  4735. if (::IsIconic(Handle))
  4736. {
  4737. if (!IsAppIconic())
  4738. {
  4739. SetAppIconic(true);
  4740. }
  4741. }
  4742. ::ApplicationRestore();
  4743. Application->BringToFront();
  4744. }
  4745. //---------------------------------------------------------------------------
  4746. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4747. {
  4748. RestoreApp();
  4749. }
  4750. //---------------------------------------------------------------------------
  4751. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4752. {
  4753. if (OpenInNewWindow())
  4754. {
  4755. ExecuteNewInstance(SessionUrl);
  4756. }
  4757. else
  4758. {
  4759. TTerminalManager::Instance()->NewSession(SessionUrl);
  4760. }
  4761. }
  4762. //---------------------------------------------------------------------------
  4763. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4764. {
  4765. NewSession();
  4766. }
  4767. //---------------------------------------------------------------------------
  4768. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4769. {
  4770. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4771. StoredSessions->NewSession(SessionName, SessionData);
  4772. // modified only, explicit
  4773. StoredSessions->Save(false, true);
  4774. // encode session name because of slashes in hierarchical sessions
  4775. return EncodeUrlString(SessionName);
  4776. }
  4777. //---------------------------------------------------------------------------
  4778. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4779. {
  4780. // current working directories become defaults here, what is not right
  4781. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4782. return SaveHiddenDuplicateSession(SessionData.get());
  4783. }
  4784. //---------------------------------------------------------------------------
  4785. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4786. {
  4787. if (OpenInNewWindow())
  4788. {
  4789. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4790. }
  4791. else
  4792. {
  4793. // current working directories become defaults here, what is not right
  4794. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4795. TTerminalManager * Manager = TTerminalManager::Instance();
  4796. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4797. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4798. // We definitelly want these
  4799. ASession->Disconnected = ManagedSession->Disconnected;
  4800. // Not sure about these two
  4801. ASession->Permanent = ManagedSession->Permanent;
  4802. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4803. Manager->ActiveSession = ASession;
  4804. }
  4805. }
  4806. //---------------------------------------------------------------------------
  4807. void __fastcall TCustomScpExplorerForm::RenameSession()
  4808. {
  4809. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4810. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4811. // When submitting the default name, do not fix it
  4812. (Name != ManagedSession->SessionData->SessionName))
  4813. {
  4814. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4815. TSessionData::ValidateName(Name);
  4816. // This is inconsistent with how color (for example) is handled.
  4817. ManagedSession->SessionData->Name = Name;
  4818. UpdateControls();
  4819. // Add/Remove distinguishing paths from sessions of the same name.
  4820. SessionListChanged();
  4821. }
  4822. }
  4823. //---------------------------------------------------------------------------
  4824. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4825. {
  4826. DebugAssert(Queue != NULL);
  4827. bool Result = Queue->IsEmpty;
  4828. if (!Result)
  4829. {
  4830. SetFocus();
  4831. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4832. }
  4833. return Result;
  4834. }
  4835. //---------------------------------------------------------------------------
  4836. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4837. {
  4838. return CanCloseQueue(FQueue);
  4839. }
  4840. //---------------------------------------------------------------------------
  4841. void __fastcall TCustomScpExplorerForm::CloseSession()
  4842. {
  4843. if (CanCloseQueue())
  4844. {
  4845. TTerminalManager::Instance()->FreeActiveTerminal();
  4846. }
  4847. }
  4848. //---------------------------------------------------------------------------
  4849. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4850. {
  4851. if (CanCloseQueue())
  4852. {
  4853. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4854. }
  4855. }
  4856. //---------------------------------------------------------------------------
  4857. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4858. {
  4859. DetachTerminal(Terminal);
  4860. RemoteDirView->Terminal = Terminal;
  4861. UpdateRemotePathComboBox(false);
  4862. InitStatusBar();
  4863. UpdateControls();
  4864. }
  4865. //---------------------------------------------------------------------------
  4866. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4867. {
  4868. InitStatusBar();
  4869. }
  4870. //---------------------------------------------------------------------------
  4871. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4872. {
  4873. if (DebugAlwaysTrue(!Terminal->Active))
  4874. {
  4875. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4876. }
  4877. }
  4878. //---------------------------------------------------------------------------
  4879. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4880. {
  4881. if (OpenInNewWindow())
  4882. {
  4883. // encode session name because of slashes in hierarchical sessions
  4884. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4885. }
  4886. else
  4887. {
  4888. TTerminalManager * Manager = TTerminalManager::Instance();
  4889. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4890. Manager->ActiveSession = Terminal;
  4891. }
  4892. }
  4893. //---------------------------------------------------------------------------
  4894. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4895. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4896. {
  4897. TTerminalManager * Manager = TTerminalManager::Instance();
  4898. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4899. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  4900. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4901. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4902. {
  4903. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4904. // FirstSession can be null, if some of the
  4905. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4906. {
  4907. FirstSession->Disconnected = true;
  4908. FirstSession->DisconnectedTemporarily = true;
  4909. }
  4910. Manager->ActiveSession = FirstSession;
  4911. }
  4912. }
  4913. //---------------------------------------------------------------------------
  4914. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4915. {
  4916. if (OpenInNewWindow())
  4917. {
  4918. ExecuteNewInstance(Name);
  4919. }
  4920. else
  4921. {
  4922. DoOpenFolderOrWorkspace(Name, true, false);
  4923. }
  4924. }
  4925. //---------------------------------------------------------------------------
  4926. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4927. bool &CanClose)
  4928. {
  4929. // See the comment in CloseApp()
  4930. if (NonVisualDataModule->Busy)
  4931. {
  4932. CanClose = false;
  4933. }
  4934. else
  4935. {
  4936. TTerminalManager * Manager = TTerminalManager::Instance();
  4937. int ActiveSessions = 0;
  4938. for (int Index = 0; Index < Manager->Count; Index++)
  4939. {
  4940. if (Manager->Sessions[Index]->Active)
  4941. {
  4942. ActiveSessions++;
  4943. }
  4944. }
  4945. // Confirm, when there's at least one active remote session or
  4946. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4947. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4948. WinConfiguration->ConfirmClosingSession)
  4949. {
  4950. unsigned int Result;
  4951. TMessageParams Params(mpNeverAskAgainCheck);
  4952. UnicodeString Message;
  4953. int Answers = qaOK | qaCancel;
  4954. // The current session is the only active session
  4955. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4956. {
  4957. if (!WinConfiguration->AutoSaveWorkspace)
  4958. {
  4959. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4960. }
  4961. else
  4962. {
  4963. Message = LoadStr(CLOSE_SESSION);
  4964. }
  4965. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4966. }
  4967. // Multiple active sessions or one active session, but it's not the current one
  4968. else if (ActiveSessions > 0)
  4969. {
  4970. if (!WinConfiguration->AutoSaveWorkspace)
  4971. {
  4972. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4973. }
  4974. else
  4975. {
  4976. Message = LoadStr(CLOSE_SESSIONS);
  4977. }
  4978. }
  4979. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4980. else
  4981. {
  4982. DebugAlwaysTrue(Manager->Count > 1);
  4983. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4984. Message = LoadStr(CLOSE_WORKSPACE);
  4985. }
  4986. UnicodeString Note;
  4987. if (WinConfiguration->AutoSaveWorkspace)
  4988. {
  4989. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4990. }
  4991. else
  4992. {
  4993. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4994. Answers = qaYes | qaNo | qaCancel;
  4995. }
  4996. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4997. SetFocus();
  4998. Result = MessageDialog(Message, qtConfirmation,
  4999. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5000. if (Result == qaNeverAskAgain)
  5001. {
  5002. WinConfiguration->ConfirmClosingSession = false;
  5003. }
  5004. if (Result == qaNo)
  5005. {
  5006. CanClose = SaveWorkspace(true);
  5007. // note that the workspace will be saved redundatly again from FormClose
  5008. }
  5009. else
  5010. {
  5011. CanClose =
  5012. (Result == qaOK) ||
  5013. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5014. (Result == qaNeverAskAgain);
  5015. }
  5016. }
  5017. if (CanClose && HasActiveTerminal())
  5018. {
  5019. CanClose = CanCloseQueue();
  5020. }
  5021. }
  5022. if (CanClose)
  5023. {
  5024. CanClose =
  5025. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5026. FEditorManager->CloseExternalFilesWithoutProcess();
  5027. if (CanClose)
  5028. {
  5029. while (CanClose && (FLocalEditors->Count > 0))
  5030. {
  5031. int PrevCount = FLocalEditors->Count;
  5032. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5033. CanClose = (FLocalEditors->Count < PrevCount);
  5034. }
  5035. if (CanClose)
  5036. {
  5037. CanClose = FEditorManager->Empty(true);
  5038. if (!CanClose)
  5039. {
  5040. SetFocus();
  5041. CanClose =
  5042. (MessageDialog(
  5043. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5044. }
  5045. }
  5046. }
  5047. }
  5048. }
  5049. //---------------------------------------------------------------------------
  5050. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5051. {
  5052. TForm * Form = dynamic_cast<TForm *>(Sender);
  5053. DebugAssert(Form != NULL);
  5054. Form->Close();
  5055. }
  5056. //---------------------------------------------------------------------------
  5057. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5058. {
  5059. TForm * Form = dynamic_cast<TForm *>(Sender);
  5060. delete Form;
  5061. }
  5062. //---------------------------------------------------------------------------
  5063. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5064. {
  5065. while (FLocalEditors->Count > 0)
  5066. {
  5067. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5068. }
  5069. }
  5070. //---------------------------------------------------------------------------
  5071. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5072. TObject *Sender)
  5073. {
  5074. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5075. try
  5076. {
  5077. SetProperties(osRemote, FileList);
  5078. }
  5079. __finally
  5080. {
  5081. delete FileList;
  5082. }
  5083. }
  5084. //---------------------------------------------------------------------------
  5085. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5086. {
  5087. if (value)
  5088. {
  5089. if (Component == fcCustomCommandsBand)
  5090. {
  5091. UpdateCustomCommandsToolbar();
  5092. }
  5093. if (Component == fcQueueFileList)
  5094. {
  5095. DebugAssert(!QueueFileListSplitter->Visible);
  5096. AdjustQueueLayout();
  5097. UpdateQueueFileList();
  5098. }
  5099. }
  5100. }
  5101. //---------------------------------------------------------------------------
  5102. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5103. {
  5104. int MinHeightLimit =
  5105. GetStaticQueuePanelComponentsHeight() +
  5106. QueueFileListSplitter->Height +
  5107. QueueFileList->Height +
  5108. GetMinQueueViewHeight();
  5109. if (QueuePanel->ClientHeight < MinHeightLimit)
  5110. {
  5111. QueuePanel->ClientHeight = MinHeightLimit;
  5112. }
  5113. }
  5114. //---------------------------------------------------------------------------
  5115. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5116. {
  5117. TControl * Control = GetComponent(Component);
  5118. DebugAssert(Control);
  5119. bool Changed = (Control->Visible != value);
  5120. if (Changed)
  5121. {
  5122. ComponentShowing(Component, value);
  5123. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5124. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5125. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5126. if (value)
  5127. {
  5128. int RemainingHeight = Control->Parent->ClientHeight;
  5129. int RemainingWidth = Control->Parent->ClientWidth;
  5130. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5131. {
  5132. TControl * ChildControl = Control->Parent->Controls[i];
  5133. if (ChildControl->Visible)
  5134. {
  5135. switch (ChildControl->Align)
  5136. {
  5137. case alTop:
  5138. case alBottom:
  5139. RemainingHeight -= ChildControl->Height;
  5140. break;
  5141. case alLeft:
  5142. case alRight:
  5143. RemainingWidth -= ChildControl->Width;
  5144. break;
  5145. }
  5146. }
  5147. }
  5148. int Reserve = ScaleByTextHeight(this, 32);
  5149. // queue in explorer, trees in commander
  5150. if (Control->Height > RemainingHeight - Reserve)
  5151. {
  5152. Control->Height = RemainingHeight / 2;
  5153. }
  5154. if (Control->Width > RemainingWidth - Reserve)
  5155. {
  5156. Control->Width = RemainingWidth / 2;
  5157. }
  5158. }
  5159. Control->Visible = value;
  5160. if (WasFocused && Visible)
  5161. {
  5162. DirView(osCurrent)->SetFocus();
  5163. }
  5164. FixControlsPlacement();
  5165. }
  5166. }
  5167. //---------------------------------------------------------------------------
  5168. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5169. {
  5170. TControl * Control = GetComponent(Component);
  5171. if (Control == NULL)
  5172. {
  5173. return false;
  5174. }
  5175. else
  5176. {
  5177. return Control->Visible;
  5178. }
  5179. }
  5180. //---------------------------------------------------------------------------
  5181. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5182. {
  5183. bool Result;
  5184. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5185. {
  5186. Result = !IsUWP();
  5187. }
  5188. else
  5189. {
  5190. Result = true;
  5191. }
  5192. return Result;
  5193. }
  5194. //---------------------------------------------------------------------------
  5195. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5196. {
  5197. if (DirView->ItemFocused != NULL)
  5198. {
  5199. DirView->ItemFocused->MakeVisible(false);
  5200. }
  5201. }
  5202. //---------------------------------------------------------------------------
  5203. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5204. {
  5205. MakeFocusedItemVisible(RemoteDirView);
  5206. QueueSplitter->Visible = QueuePanel->Visible;
  5207. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5208. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5209. TControl * TopControlsOrder[] =
  5210. { TopDock, MessageDock, SessionsPageControl };
  5211. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5212. TControl * QueueControlsOrder[] =
  5213. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5214. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5215. }
  5216. //---------------------------------------------------------------------------
  5217. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5218. {
  5219. switch (Component) {
  5220. case fcStatusBar: return RemoteStatusBar;
  5221. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5222. case fcQueueView: return QueuePanel;
  5223. case fcQueueToolbar: return QueueDock;
  5224. case fcRemoteTree: return RemoteDrivePanel;
  5225. case fcSessionsTabs: return SessionsPageControl;
  5226. case fcQueueFileList: return QueueFileList;
  5227. default: return NULL;
  5228. }
  5229. }
  5230. //---------------------------------------------------------------------------
  5231. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5232. TObject *Sender, TListColumn *Column, TPoint &Point)
  5233. {
  5234. DebugAssert(NonVisualDataModule && Column && Sender);
  5235. NonVisualDataModule->ListColumn = Column;
  5236. TPopupMenu * DirViewColumnMenu;
  5237. if (Sender == DirView(osOther))
  5238. {
  5239. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5240. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5241. bool SizeColumn = (Column->Index == uvSize);
  5242. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5243. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5244. }
  5245. else
  5246. {
  5247. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5248. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5249. bool SizeColumn = (Column->Index == dvSize);
  5250. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5251. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5252. }
  5253. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5254. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5255. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5256. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5257. }
  5258. //---------------------------------------------------------------------------
  5259. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5260. TObject *Sender, TListItem *Item, bool &AllowExec)
  5261. {
  5262. DoDirViewExecFile(Sender, Item, AllowExec);
  5263. }
  5264. //---------------------------------------------------------------------------
  5265. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5266. TListItem * Item, bool & AllowExec)
  5267. {
  5268. DebugAssert(Sender && Item && Configuration);
  5269. DebugAssert(AllowExec);
  5270. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5271. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5272. bool Remote = !IsSideLocalBrowser(Side);
  5273. DebugAssert(Item == ADirView->ItemFocused);
  5274. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5275. TResolvedDoubleClickAction Action;
  5276. if (FForceExecution)
  5277. {
  5278. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5279. }
  5280. else
  5281. {
  5282. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5283. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5284. }
  5285. if (Action == rdcaCopy)
  5286. {
  5287. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5288. UnlockWindow();
  5289. try
  5290. {
  5291. if (IsLocalBrowserMode())
  5292. {
  5293. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5294. }
  5295. else
  5296. {
  5297. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5298. }
  5299. }
  5300. __finally
  5301. {
  5302. LockWindow();
  5303. }
  5304. AllowExec = false;
  5305. }
  5306. else if (Action == rdcaEdit)
  5307. {
  5308. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5309. {
  5310. ExecuteFile(Side, efDefaultEditor);
  5311. AllowExec = false;
  5312. }
  5313. }
  5314. else if (Action == rdcaOpen)
  5315. {
  5316. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5317. {
  5318. ExecuteFile(osRemote, efShell);
  5319. AllowExec = false;
  5320. }
  5321. }
  5322. }
  5323. //---------------------------------------------------------------------------
  5324. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5325. {
  5326. return ((Side == osRemote) || (Side == osCurrent));
  5327. }
  5328. //---------------------------------------------------------------------------
  5329. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5330. {
  5331. DebugFail();
  5332. }
  5333. //---------------------------------------------------------------------------
  5334. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5335. {
  5336. DebugFail();
  5337. }
  5338. //---------------------------------------------------------------------------
  5339. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5340. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5341. {
  5342. TSynchronizeParamType Params;
  5343. Params.LocalDirectory = LocalDirectory;
  5344. Params.RemoteDirectory = RemoteDirectory;
  5345. int UnusedParams =
  5346. (GUIConfiguration->SynchronizeParams &
  5347. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5348. TTerminal::spCaseSensitive));
  5349. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5350. Params.Options = GUIConfiguration->SynchronizeOptions;
  5351. bool SaveSettings = false;
  5352. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5353. &DoSynchronizeTooManyDirectories);
  5354. DebugAssert(FSynchronizeController == NULL);
  5355. FSynchronizeController = &Controller;
  5356. bool Result;
  5357. try
  5358. {
  5359. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5360. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5361. int Options =
  5362. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5363. DebugAssert(FOnFeedSynchronizeError == NULL);
  5364. DebugAssert(FOnSynchronizeAbort == NULL);
  5365. DebugAssert(FSynchronizeTerminal == NULL);
  5366. FSynchronizeTerminal = Terminal;
  5367. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5368. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5369. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5370. if (Result)
  5371. {
  5372. if (SaveSettings)
  5373. {
  5374. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5375. GUIConfiguration->SynchronizeOptions = Params.Options;
  5376. }
  5377. else
  5378. {
  5379. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5380. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5381. FLAGSET(Params.Options, soSynchronize))
  5382. {
  5383. GUIConfiguration->SynchronizeOptions =
  5384. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5385. soSynchronize;
  5386. }
  5387. }
  5388. LocalDirectory = Params.LocalDirectory;
  5389. RemoteDirectory = Params.RemoteDirectory;
  5390. }
  5391. }
  5392. __finally
  5393. {
  5394. FSynchronizeController = NULL;
  5395. DebugAssert(FOnFeedSynchronizeError == NULL);
  5396. FOnFeedSynchronizeError = NULL;
  5397. DebugAssert(FOnSynchronizeAbort == NULL);
  5398. FOnSynchronizeAbort = NULL;
  5399. FSynchronizeTerminal = NULL;
  5400. }
  5401. return Result;
  5402. }
  5403. //---------------------------------------------------------------------------
  5404. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5405. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5406. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5407. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5408. TSynchronizeOptions * Options, bool Full)
  5409. {
  5410. // Keep-up-to-date mode
  5411. if (Terminal->Status == ssOpened)
  5412. {
  5413. try
  5414. {
  5415. int PParams = Params.Params;
  5416. if (!Full)
  5417. {
  5418. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5419. if (FLAGSET(Params.Options, soRecurse))
  5420. {
  5421. PParams |= TTerminal::spSubDirs;
  5422. }
  5423. }
  5424. else
  5425. {
  5426. // if keepuptodate is non-recursive,
  5427. // full sync before has to be non-recursive as well
  5428. if (FLAGCLEAR(Params.Options, soRecurse))
  5429. {
  5430. PParams |= TTerminal::spNoRecurse;
  5431. }
  5432. }
  5433. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5434. PParams, Checklist, Options);
  5435. }
  5436. catch (Exception & E)
  5437. {
  5438. if (!Terminal->Active)
  5439. {
  5440. ShowExtendedExceptionEx(Terminal, &E);
  5441. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5442. if (!Terminal->Active)
  5443. {
  5444. throw;
  5445. }
  5446. }
  5447. else
  5448. {
  5449. if (FLAGSET(Params.Options, soContinueOnError) &&
  5450. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5451. {
  5452. // noop, already logged in MoreMessageDialog
  5453. }
  5454. else
  5455. {
  5456. // We get mostly EAbort here, so this is noop.
  5457. // Exception is an error when listing directories while looking for differences.
  5458. ShowExtendedExceptionEx(Terminal, &E);
  5459. throw;
  5460. }
  5461. }
  5462. }
  5463. }
  5464. }
  5465. //---------------------------------------------------------------------------
  5466. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5467. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5468. const UnicodeString ErrorStr)
  5469. {
  5470. if (!Directory.IsEmpty())
  5471. {
  5472. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5473. }
  5474. else
  5475. {
  5476. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5477. }
  5478. }
  5479. //---------------------------------------------------------------------------
  5480. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5481. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5482. {
  5483. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5484. {
  5485. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5486. }
  5487. else
  5488. {
  5489. TMessageParams Params(mpNeverAskAgainCheck);
  5490. unsigned int Result = MessageDialog(
  5491. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5492. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5493. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5494. {
  5495. MaxDirectories *= 2;
  5496. if (Result == qaNeverAskAgain)
  5497. {
  5498. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5499. }
  5500. }
  5501. else
  5502. {
  5503. Abort();
  5504. }
  5505. }
  5506. }
  5507. //---------------------------------------------------------------------------
  5508. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5509. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5510. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5511. TSynchronizeOptions * Options)
  5512. {
  5513. // Keep-up-to-date mode
  5514. DebugAssert(!FAutoOperation);
  5515. void * BatchStorage;
  5516. BatchStart(BatchStorage);
  5517. FAutoOperation = true;
  5518. bool AnyOperation = false;
  5519. TDateTime StartTime = Now();
  5520. TSynchronizeChecklist * AChecklist = NULL;
  5521. try
  5522. {
  5523. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5524. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5525. {
  5526. FSynchronizeProgressForm->Start();
  5527. }
  5528. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5529. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5530. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5531. Options);
  5532. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5533. SAFE_DESTROY(FSynchronizeProgressForm);
  5534. AnyOperation = (AChecklist->CheckedCount > 0);
  5535. if (AnyOperation)
  5536. {
  5537. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5538. CreateProgressForm(&SynchronizeProgress);
  5539. try
  5540. {
  5541. Terminal->SynchronizeApply(
  5542. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5543. SynchronizeProcessedItem, NULL, NULL, NULL);
  5544. }
  5545. __finally
  5546. {
  5547. DestroyProgressForm();
  5548. }
  5549. }
  5550. }
  5551. __finally
  5552. {
  5553. if (Checklist == NULL)
  5554. {
  5555. delete AChecklist;
  5556. }
  5557. else
  5558. {
  5559. *Checklist = AChecklist;
  5560. }
  5561. FAutoOperation = false;
  5562. SAFE_DESTROY(FSynchronizeProgressForm);
  5563. BatchEnd(BatchStorage);
  5564. ReloadLocalDirectory();
  5565. if (AnyOperation)
  5566. {
  5567. OperationComplete(StartTime);
  5568. }
  5569. }
  5570. }
  5571. //---------------------------------------------------------------------------
  5572. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5573. {
  5574. // can be called from command line
  5575. return Visible &&
  5576. ((DirView(osRemote)->SelCount > 0) ||
  5577. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5578. }
  5579. //---------------------------------------------------------------------------
  5580. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5581. {
  5582. LogSynchronizeEvent(Terminal, Message);
  5583. }
  5584. //---------------------------------------------------------------------------
  5585. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5586. int Params, TSynchronizeOptions & Options)
  5587. {
  5588. DebugAssert(!IsLocalBrowserMode());
  5589. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5590. {
  5591. Options.Filter = new TStringList();
  5592. Options.Filter->CaseSensitive = false;
  5593. Options.Filter->Duplicates = Types::dupAccept;
  5594. if (DirView(osRemote)->SelCount > 0)
  5595. {
  5596. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5597. }
  5598. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5599. {
  5600. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5601. }
  5602. Options.Filter->Sort();
  5603. }
  5604. }
  5605. //---------------------------------------------------------------------------
  5606. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5607. {
  5608. TCopyParamType Defaults;
  5609. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5610. ConfigStorage->AccessMode = smRead;
  5611. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5612. {
  5613. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5614. }
  5615. ConfigStorage.reset(NULL);
  5616. std::unique_ptr<TStringList> Options(new TStringList());
  5617. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5618. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5619. UnicodeString Result;
  5620. if (Options->Count > 0)
  5621. {
  5622. Result =
  5623. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5624. }
  5625. return Result;
  5626. }
  5627. //---------------------------------------------------------------------------
  5628. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5629. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5630. {
  5631. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5632. UnicodeString AdditionalParams =
  5633. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5634. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5635. SerializeCopyParamForCommandLine(CopyParams),
  5636. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5637. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5638. ExecuteNewInstance(SessionName, AdditionalParams);
  5639. }
  5640. //---------------------------------------------------------------------------
  5641. struct TSynchronizeParams
  5642. {
  5643. UnicodeString LocalDirectory;
  5644. UnicodeString RemoteDirectory;
  5645. TSynchronizeMode Mode;
  5646. TSynchronizeChecklist * Checklist;
  5647. int Params;
  5648. TCopyParamType * CopyParam;
  5649. TDateTime CollectElapsed;
  5650. TDateTime * StartTime;
  5651. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5652. };
  5653. //---------------------------------------------------------------------------
  5654. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5655. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5656. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5657. {
  5658. DebugAssert(!FAutoOperation);
  5659. void * BatchStorage;
  5660. BatchStart(BatchStorage);
  5661. FAutoOperation = true;
  5662. TFileOperationStatistics Statistics;
  5663. TDateTime Start = Now();
  5664. try
  5665. {
  5666. Params.OnProcessedItem = OnProcessedItem;
  5667. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5668. CreateProgressForm(&SynchronizeProgress);
  5669. Terminal->SynchronizeApply(
  5670. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5671. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5672. &Statistics);
  5673. }
  5674. __finally
  5675. {
  5676. Params.OnProcessedItem = NULL;
  5677. FAutoOperation = false;
  5678. DestroyProgressForm();
  5679. BatchEnd(BatchStorage);
  5680. ReloadLocalDirectory();
  5681. }
  5682. if (WinConfiguration->SynchronizeSummary)
  5683. {
  5684. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5685. // The statistics should be 0 anyway in this case
  5686. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5687. {
  5688. Message += L"\n";
  5689. if (Statistics.FilesUploaded > 0)
  5690. {
  5691. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5692. }
  5693. if (Statistics.FilesDownloaded > 0)
  5694. {
  5695. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5696. }
  5697. if (Statistics.FilesDeletedLocal > 0)
  5698. {
  5699. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5700. }
  5701. if (Statistics.FilesDeletedRemote > 0)
  5702. {
  5703. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5704. }
  5705. }
  5706. TDateTime Elapsed = (Now() - Start);
  5707. Message +=
  5708. L"\n" +
  5709. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5710. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5711. TMessageParams Params(mpNeverAskAgainCheck);
  5712. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5713. if (Result == qaNeverAskAgain)
  5714. {
  5715. WinConfiguration->SynchronizeSummary = false;
  5716. }
  5717. }
  5718. }
  5719. //---------------------------------------------------------------------------
  5720. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5721. {
  5722. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5723. {
  5724. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5725. }
  5726. // Not set in keep-up-to-date mode - Synchronize() method
  5727. if (Token != NULL)
  5728. {
  5729. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5730. if (Params.OnProcessedItem != NULL)
  5731. {
  5732. Params.OnProcessedItem(NULL, ChecklistItem);
  5733. }
  5734. }
  5735. }
  5736. //---------------------------------------------------------------------------
  5737. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5738. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5739. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5740. {
  5741. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5742. *Params.StartTime = Now();
  5743. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5744. }
  5745. //---------------------------------------------------------------------------
  5746. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5747. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5748. {
  5749. // terminal can be already closed (e.g. dropped connection)
  5750. if (Terminal != NULL)
  5751. {
  5752. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5753. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5754. }
  5755. }
  5756. //---------------------------------------------------------------------------
  5757. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5758. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5759. {
  5760. DebugAssert(!IsLocalBrowserMode());
  5761. TAutoBatch AutoBatch(this);
  5762. TAutoFlag AutoOperationFlag(FAutoOperation);
  5763. if (Side == osRemote)
  5764. {
  5765. std::unique_ptr<TStrings> FileList(new TStringList());
  5766. FileList->AddObject(FileName, RemoteFile);
  5767. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5768. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5769. RemoteDirView->SaveSelection();
  5770. RemoteDirView->SaveSelectedNames();
  5771. try
  5772. {
  5773. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5774. }
  5775. catch(...)
  5776. {
  5777. RemoteDirView->DiscardSavedSelection();
  5778. throw;
  5779. }
  5780. RemoteDirView->RestoreSelection();
  5781. }
  5782. else if (DebugAlwaysTrue(Side == osLocal))
  5783. {
  5784. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5785. {
  5786. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5787. }
  5788. UnicodeString Directory = ExtractFileDir(FileName);
  5789. ReloadLocalDirectory(Directory);
  5790. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5791. if (!SamePaths(Directory, NewDirectory))
  5792. {
  5793. ReloadLocalDirectory(NewDirectory);
  5794. }
  5795. }
  5796. }
  5797. //---------------------------------------------------------------------------
  5798. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5799. {
  5800. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5801. if (Side == osLocal)
  5802. {
  5803. if (Action == TSynchronizeChecklist::saDownloadNew)
  5804. {
  5805. OpenFolderInExplorer(LocalPath);
  5806. }
  5807. else
  5808. {
  5809. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5810. }
  5811. }
  5812. else if (DebugAlwaysTrue(Side == osRemote))
  5813. {
  5814. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5815. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5816. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5817. if (!LocalPath.IsEmpty())
  5818. {
  5819. SessionData->LocalDirectory = LocalPath;
  5820. }
  5821. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5822. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5823. }
  5824. }
  5825. //---------------------------------------------------------------------------
  5826. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5827. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5828. const TCopyParamType * CopyParams)
  5829. {
  5830. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5831. UnicodeString AdditionalParams =
  5832. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5833. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5834. SerializeCopyParamForCommandLine(CopyParams),
  5835. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5836. LocalDirectory, RemoteDirectory, Mode, Params));
  5837. ExecuteNewInstance(SessionName, AdditionalParams);
  5838. }
  5839. //---------------------------------------------------------------------------
  5840. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5841. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5842. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5843. {
  5844. int Result;
  5845. bool SaveSettings = false;
  5846. int Options =
  5847. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5848. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5849. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5850. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5851. bool Continue =
  5852. (UseDefaults == 0) ||
  5853. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5854. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5855. if (Continue)
  5856. {
  5857. Configuration->Usage->Inc(L"Synchronizations");
  5858. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5859. UpdateCopyParamCounters(CopyParam);
  5860. TSynchronizeOptions SynchronizeOptions;
  5861. GetSynchronizeOptions(Params, SynchronizeOptions);
  5862. if (SaveSettings)
  5863. {
  5864. GUIConfiguration->SynchronizeParams = Params;
  5865. }
  5866. else
  5867. {
  5868. SaveMode = false;
  5869. }
  5870. TDateTime StartTime = Now();
  5871. TSynchronizeChecklist * Checklist = NULL;
  5872. try
  5873. {
  5874. DebugAssert(!FAutoOperation);
  5875. FAutoOperation = true;
  5876. try
  5877. {
  5878. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5879. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5880. int Files = -1;
  5881. if (DataList->Count >= 1)
  5882. {
  5883. Files = StrToIntDef(DataList->Strings[0], Files);
  5884. }
  5885. else
  5886. {
  5887. DataList->Add(UnicodeString());
  5888. }
  5889. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5890. FSynchronizeProgressForm->Start();
  5891. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5892. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5893. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5894. &SynchronizeOptions);
  5895. if (Terminal->SessionData->CacheDirectories)
  5896. {
  5897. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5898. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5899. }
  5900. }
  5901. __finally
  5902. {
  5903. FAutoOperation = false;
  5904. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5905. SAFE_DESTROY(FSynchronizeProgressForm);
  5906. }
  5907. TSynchronizeParams SynchronizeParams;
  5908. SynchronizeParams.LocalDirectory = LocalDirectory;
  5909. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5910. SynchronizeParams.Mode = Mode;
  5911. SynchronizeParams.CopyParam = &CopyParam;
  5912. SynchronizeParams.Params = Params;
  5913. SynchronizeParams.Checklist = Checklist;
  5914. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5915. SynchronizeParams.StartTime = &StartTime;
  5916. SynchronizeParams.OnProcessedItem = NULL;
  5917. Result = Checklist->Count;
  5918. if (Checklist->Count > 0)
  5919. {
  5920. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5921. {
  5922. if (!DoSynchronizeChecklistDialog(
  5923. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5924. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5925. {
  5926. Result = -1;
  5927. }
  5928. }
  5929. else
  5930. {
  5931. FullSynchronize(SynchronizeParams, NULL, NULL);
  5932. }
  5933. }
  5934. else
  5935. {
  5936. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5937. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5938. }
  5939. }
  5940. __finally
  5941. {
  5942. delete Checklist;
  5943. }
  5944. OperationComplete(StartTime);
  5945. }
  5946. else
  5947. {
  5948. Result = -1;
  5949. }
  5950. return Result;
  5951. }
  5952. //---------------------------------------------------------------------------
  5953. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5954. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5955. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5956. {
  5957. if (Collect)
  5958. {
  5959. DebugAssert(FSynchronizeProgressForm != NULL);
  5960. if (!FSynchronizeProgressForm->Started)
  5961. {
  5962. FSynchronizeProgressForm->Start();
  5963. }
  5964. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5965. SetTaskbarListProgressValue(CompareProgress);
  5966. }
  5967. else
  5968. {
  5969. DebugAssert(FSynchronizeProgressForm == NULL);
  5970. DebugAssert(FProgressForm != NULL);
  5971. }
  5972. }
  5973. //---------------------------------------------------------------------------
  5974. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5975. {
  5976. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5977. TRemoteFile * File = NULL;
  5978. Terminal->ReadFile(FullFileName, File);
  5979. if (File != NULL)
  5980. {
  5981. std::unique_ptr<TRemoteFile> FileOwner(File);
  5982. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5983. Application->ShowMainForm = false;
  5984. Application->Run();
  5985. }
  5986. }
  5987. //---------------------------------------------------------------------------
  5988. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5989. {
  5990. DebugFail();
  5991. }
  5992. //---------------------------------------------------------------------------
  5993. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5994. {
  5995. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5996. SessionData->Assign(ManagedSession->SessionData);
  5997. UpdateSessionData(SessionData.get());
  5998. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5999. if (IsActiveTerminal(ManagedSession))
  6000. {
  6001. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6002. }
  6003. if (!ManagedSession->SessionData->HasSessionName())
  6004. {
  6005. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6006. // once IsWorkspace is cleared
  6007. SessionData->Name = UnicodeString();
  6008. }
  6009. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6010. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6011. SessionData->IsWorkspace = false;
  6012. return SessionData.release();
  6013. }
  6014. //---------------------------------------------------------------------------
  6015. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6016. {
  6017. TSessionData * SessionData = CloneCurrentSessionData();
  6018. try
  6019. {
  6020. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6021. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6022. }
  6023. __finally
  6024. {
  6025. delete SessionData;
  6026. }
  6027. }
  6028. //---------------------------------------------------------------------------
  6029. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6030. {
  6031. TTerminalManager * Manager = TTerminalManager::Instance();
  6032. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6033. if (DebugAlwaysTrue(ManagedSession != NULL))
  6034. {
  6035. // Update ManagedSession->StateData
  6036. UpdateSession(ManagedSession);
  6037. }
  6038. Manager->SaveWorkspace(DataList.get());
  6039. return DataList.release();
  6040. }
  6041. //---------------------------------------------------------------------------
  6042. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6043. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6044. {
  6045. WinConfiguration->LastWorkspace = Name;
  6046. if (!SavePasswords)
  6047. {
  6048. for (int Index = 0; Index < DataList->Count; Index++)
  6049. {
  6050. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6051. if (SessionData->Link.IsEmpty())
  6052. {
  6053. SessionData->ClearSessionPasswords();
  6054. }
  6055. }
  6056. }
  6057. StoredSessions->NewWorkspace(Name, DataList);
  6058. // modified only
  6059. StoredSessions->Save(false, Explicit);
  6060. }
  6061. //---------------------------------------------------------------------------
  6062. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6063. {
  6064. return
  6065. DefaultStr(WinConfiguration->LastWorkspace,
  6066. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6067. }
  6068. //---------------------------------------------------------------------------
  6069. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6070. {
  6071. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6072. bool AnyNonStoredSessionWithPassword = false;
  6073. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6074. bool AllNonStoredSessionsAnonymous = true;
  6075. TTerminalManager * Manager = TTerminalManager::Instance();
  6076. for (int Index = 0; Index < DataList->Count; Index++)
  6077. {
  6078. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6079. if (SessionData->Link.IsEmpty())
  6080. {
  6081. if (SessionData->HasAnySessionPassword())
  6082. {
  6083. AnyNonStoredSessionWithPassword = true;
  6084. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6085. {
  6086. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6087. }
  6088. }
  6089. if (!SameText(SessionData->UserName, AnonymousUserName))
  6090. {
  6091. AllNonStoredSessionsAnonymous = false;
  6092. }
  6093. }
  6094. }
  6095. bool SavePasswords;
  6096. bool * PSavePasswords;
  6097. bool NotRecommendedSavingPasswords =
  6098. !CustomWinConfiguration->UseMasterPassword &&
  6099. !AllNonStoredSessionsAnonymous;
  6100. if (Configuration->DisablePasswordStoring ||
  6101. !AnyNonStoredSessionWithPassword)
  6102. {
  6103. PSavePasswords = NULL;
  6104. SavePasswords = false;
  6105. }
  6106. else
  6107. {
  6108. PSavePasswords = &SavePasswords;
  6109. SavePasswords =
  6110. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6111. !NotRecommendedSavingPasswords;
  6112. }
  6113. UnicodeString Name = WorkspaceName();
  6114. bool CreateShortcut = false;
  6115. bool Result =
  6116. DoSaveWorkspaceDialog(
  6117. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6118. EnableAutoSave);
  6119. if (Result)
  6120. {
  6121. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6122. if (CreateShortcut)
  6123. {
  6124. TOperationVisualizer Visualizer;
  6125. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6126. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6127. }
  6128. if (EnableAutoSave)
  6129. {
  6130. WinConfiguration->AutoSaveWorkspace = true;
  6131. WinConfiguration->AutoWorkspace = Name;
  6132. if (PSavePasswords != NULL)
  6133. {
  6134. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6135. }
  6136. }
  6137. }
  6138. return Result;
  6139. }
  6140. //---------------------------------------------------------------------------
  6141. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6142. {
  6143. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6144. DebugAssert(Session == ManagedSession);
  6145. if (!IsLocalBrowserMode())
  6146. {
  6147. SAFE_DESTROY(Session->RemoteExplorerState);
  6148. if (WinConfiguration->PreservePanelState)
  6149. {
  6150. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6151. }
  6152. }
  6153. UpdateSessionData(Session->StateData);
  6154. }
  6155. //---------------------------------------------------------------------------
  6156. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6157. {
  6158. // Keep in sync with TSessionData::CopyStateData
  6159. DebugAssert(Data != NULL);
  6160. // This is inconsistent with how color (for example) is handled.
  6161. Data->Name = ManagedSession->SessionData->Name;
  6162. if (!IsLocalBrowserMode())
  6163. {
  6164. // cannot use RemoteDirView->Path, because it is empty if connection
  6165. // was already closed
  6166. // also only peek, we may not be connected at all atm
  6167. // so make sure we do not try retrieving current directory from the server
  6168. // (particularly with FTP)
  6169. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6170. if (!ACurrentDirectory.IsEmpty())
  6171. {
  6172. Data->RemoteDirectory = ACurrentDirectory;
  6173. }
  6174. }
  6175. Data->Color = SessionColor;
  6176. }
  6177. //---------------------------------------------------------------------------
  6178. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6179. {
  6180. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6181. DebugAssert(Toolbar != NULL);
  6182. for (int i = 0; i < Toolbar->Items->Count; i++)
  6183. {
  6184. TTBXCustomDropDownItem * DropDownItem;
  6185. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6186. if (DropDownItem != NULL)
  6187. {
  6188. ToolbarItemResize(DropDownItem,
  6189. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6190. Toolbar->NonClientWidth);
  6191. break;
  6192. }
  6193. }
  6194. }
  6195. //---------------------------------------------------------------------------
  6196. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6197. {
  6198. Item->EditWidth = Width;
  6199. }
  6200. //---------------------------------------------------------------------------
  6201. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6202. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6203. {
  6204. for (int i = 0; i < Toolbar->Items->Count; i++)
  6205. {
  6206. TTBXCustomDropDownItem * DropDownItem;
  6207. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6208. if (DropDownItem != NULL)
  6209. {
  6210. ASize.x -= DropDownItem->EditWidth;
  6211. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6212. }
  6213. }
  6214. }
  6215. //---------------------------------------------------------------------------
  6216. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6217. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6218. {
  6219. if (WinConfiguration->DDWarnLackOfTempSpace)
  6220. {
  6221. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6222. if (!ADrive.IsEmpty())
  6223. {
  6224. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6225. DebugAssert(RequiredSpace >= 0);
  6226. __int64 RequiredWithReserve;
  6227. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6228. if (FreeSpace < RequiredWithReserve)
  6229. {
  6230. unsigned int Result;
  6231. TMessageParams Params(mpNeverAskAgainCheck);
  6232. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6233. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6234. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6235. if (Result == qaNeverAskAgain)
  6236. {
  6237. WinConfiguration->DDWarnLackOfTempSpace = false;
  6238. }
  6239. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6240. }
  6241. }
  6242. }
  6243. }
  6244. //---------------------------------------------------------------------------
  6245. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6246. {
  6247. DoOpenDirectoryDialog(odAddBookmark, Side);
  6248. }
  6249. //---------------------------------------------------------------------------
  6250. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6251. {
  6252. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6253. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6254. try
  6255. {
  6256. TCustomDirView * DView = DirView(Side);
  6257. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6258. {
  6259. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6260. }
  6261. }
  6262. catch (...)
  6263. {
  6264. delete VisitedDirectories;
  6265. throw;
  6266. }
  6267. return VisitedDirectories;
  6268. }
  6269. //---------------------------------------------------------------------------
  6270. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6271. TOpenDirectoryMode Mode, TOperationSide Side)
  6272. {
  6273. bool Continue = true;
  6274. if (Mode == odAddBookmark)
  6275. {
  6276. TMessageParams Params(mpNeverAskAgainCheck);
  6277. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6278. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6279. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6280. unsigned int Answer =
  6281. MessageDialog(Message,
  6282. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6283. if (Answer == qaNeverAskAgain)
  6284. {
  6285. Continue = true;
  6286. WinConfiguration->UseSharedBookmarks = true;
  6287. }
  6288. else if (Answer == qaOK)
  6289. {
  6290. Continue = true;
  6291. WinConfiguration->UseSharedBookmarks = false;
  6292. }
  6293. else
  6294. {
  6295. Continue = false;
  6296. }
  6297. }
  6298. if (Continue)
  6299. {
  6300. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6301. try
  6302. {
  6303. UnicodeString Name = DirView(Side)->PathName;
  6304. bool Repeat;
  6305. do
  6306. {
  6307. Repeat = false;
  6308. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6309. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6310. // do not allow switching to location profiles,
  6311. // if we are not connected
  6312. HasDirView[osLocal] && (Terminal != NULL)))
  6313. {
  6314. TWindowLock Lock(this);
  6315. if (!TryOpenDirectory(Side, Name))
  6316. {
  6317. Repeat = true;
  6318. Mode = odBrowse;
  6319. }
  6320. }
  6321. }
  6322. while (Repeat);
  6323. }
  6324. __finally
  6325. {
  6326. delete VisitedDirectories;
  6327. }
  6328. }
  6329. }
  6330. //---------------------------------------------------------------------------
  6331. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6332. {
  6333. DebugAssert(!Terminal->CommandSessionOpened);
  6334. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6335. }
  6336. //---------------------------------------------------------------------------
  6337. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6338. {
  6339. bool Result = Terminal->IsCapable[Capability] ||
  6340. Terminal->CommandSessionOpened;
  6341. if (!Result)
  6342. {
  6343. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6344. if (!GUIConfiguration->ConfirmCommandSession)
  6345. {
  6346. Result = true;
  6347. }
  6348. else
  6349. {
  6350. TMessageParams Params(mpNeverAskAgainCheck);
  6351. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6352. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6353. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6354. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6355. if (Answer == qaNeverAskAgain)
  6356. {
  6357. GUIConfiguration->ConfirmCommandSession = false;
  6358. Result = true;
  6359. }
  6360. else if (Answer == qaOK)
  6361. {
  6362. Result = true;
  6363. }
  6364. }
  6365. if (Result)
  6366. {
  6367. Result = CommandSessionFallback();
  6368. }
  6369. }
  6370. return Result;
  6371. }
  6372. //---------------------------------------------------------------------------
  6373. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6374. {
  6375. if (EnsureCommandSessionFallback(fcAnyCommand))
  6376. {
  6377. DoConsoleDialog(Terminal, Command);
  6378. }
  6379. }
  6380. //---------------------------------------------------------------------------
  6381. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6382. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6383. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6384. {
  6385. if (!IsSideLocalBrowser(osRemote) &&
  6386. IsFileControl(DropSourceControl, osRemote) &&
  6387. (FDDExtMapFile != NULL))
  6388. {
  6389. Accept = true;
  6390. }
  6391. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6392. DebugAssert(FDDTargetControl != NULL);
  6393. }
  6394. //---------------------------------------------------------------------------
  6395. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6396. _di_IDataObject DataObj, int KeyState,
  6397. const TPoint & Point, int & Effect, bool & Accept)
  6398. {
  6399. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6400. }
  6401. //---------------------------------------------------------------------------
  6402. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6403. _di_IDataObject DataObj, int KeyState,
  6404. const TPoint & Point, int & Effect, bool & Accept)
  6405. {
  6406. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6407. }
  6408. //---------------------------------------------------------------------------
  6409. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6410. TObject * Sender)
  6411. {
  6412. DebugUsedParam(Sender);
  6413. DebugAssert(FDDTargetControl == Sender);
  6414. FDDTargetControl = NULL;
  6415. }
  6416. //---------------------------------------------------------------------------
  6417. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6418. {
  6419. FileControlDDDragLeave(SessionsPageControl);
  6420. }
  6421. //---------------------------------------------------------------------------
  6422. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6423. {
  6424. FileControlDDDragLeave(QueueView3);
  6425. }
  6426. //---------------------------------------------------------------------------
  6427. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6428. {
  6429. DebugAssert(!IsSideLocalBrowser(Side));
  6430. DebugUsedParam(Side);
  6431. bool Edit = false;
  6432. TRemoteFile * File = NULL;
  6433. UnicodeString FileName;
  6434. UnicodeString PointTo;
  6435. bool SymbolicLink = true;
  6436. if (RemoteDirView->ItemFocused)
  6437. {
  6438. DebugAssert(RemoteDirView->ItemFocused->Data);
  6439. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6440. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6441. if (Edit)
  6442. {
  6443. FileName = File->FileName;
  6444. PointTo = File->LinkTo;
  6445. }
  6446. else if (!File->IsParentDirectory)
  6447. {
  6448. PointTo = File->FileName;
  6449. }
  6450. }
  6451. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6452. Terminal->IsCapable[fcHardLink]))
  6453. {
  6454. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6455. if (Edit)
  6456. {
  6457. DebugAssert(File->FileName == FileName);
  6458. int Params = dfNoRecursive;
  6459. Terminal->ExceptionOnFail = true;
  6460. try
  6461. {
  6462. Terminal->DeleteFile(L"", File, &Params);
  6463. }
  6464. __finally
  6465. {
  6466. Terminal->ExceptionOnFail = false;
  6467. }
  6468. }
  6469. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6470. }
  6471. }
  6472. //---------------------------------------------------------------------------
  6473. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6474. {
  6475. return
  6476. (IsSideLocalBrowser(Side) ||
  6477. (HasActiveTerminal() &&
  6478. Terminal->ResolvingSymlinks &&
  6479. Terminal->IsCapable[fcSymbolicLink]));
  6480. }
  6481. //---------------------------------------------------------------------------
  6482. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6483. {
  6484. return
  6485. !IsSideLocalBrowser(FCurrentSide) &&
  6486. (RemoteDirView->ItemFocused != NULL) &&
  6487. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6488. Terminal->SessionData->ResolveSymlinks;
  6489. }
  6490. //---------------------------------------------------------------------------
  6491. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6492. {
  6493. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6494. FForceExecution = true;
  6495. try
  6496. {
  6497. DirView(osCurrent)->ExecuteCurrentFile();
  6498. }
  6499. __finally
  6500. {
  6501. FForceExecution = false;
  6502. }
  6503. }
  6504. //---------------------------------------------------------------------------
  6505. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6506. {
  6507. TEditorData ExternalEditor;
  6508. ExternalEditor.Editor = edExternal;
  6509. bool Remember = false;
  6510. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6511. {
  6512. if (Remember)
  6513. {
  6514. TEditorList * EditorList = new TEditorList;
  6515. try
  6516. {
  6517. *EditorList = *WinConfiguration->EditorList;
  6518. bool Found = false;
  6519. int i = 0;
  6520. while (!Found && (i < EditorList->Count))
  6521. {
  6522. const TEditorPreferences * Editor = EditorList->Editors[i];
  6523. if ((Editor->Data->Editor == edExternal) &&
  6524. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6525. {
  6526. Found = true;
  6527. }
  6528. i++;
  6529. }
  6530. if (!Found)
  6531. {
  6532. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6533. WinConfiguration->EditorList = EditorList;
  6534. }
  6535. }
  6536. __finally
  6537. {
  6538. delete EditorList;
  6539. }
  6540. }
  6541. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6542. }
  6543. }
  6544. //---------------------------------------------------------------------------
  6545. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6546. {
  6547. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6548. if (FFileFindTerminal == ATerminal)
  6549. {
  6550. FFileFindTerminal = NULL;
  6551. HideFileFindDialog();
  6552. }
  6553. if (FClipboardTerminal == ATerminal)
  6554. {
  6555. ClipboardClear(); // implies ClipboardStop
  6556. }
  6557. if ((FOnSynchronizeAbort != NULL) &&
  6558. (ATerminal == FManagedSession))
  6559. {
  6560. FOnSynchronizeAbort(NULL);
  6561. }
  6562. }
  6563. //---------------------------------------------------------------------------
  6564. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6565. {
  6566. DetachTerminal(Sender);
  6567. }
  6568. //---------------------------------------------------------------------------
  6569. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6570. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6571. {
  6572. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6573. DebugAssert(Terminal != NULL);
  6574. if (Data->Terminal == Terminal)
  6575. {
  6576. Data->Terminal = NULL;
  6577. }
  6578. }
  6579. //---------------------------------------------------------------------------
  6580. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6581. {
  6582. return NULL;
  6583. }
  6584. //---------------------------------------------------------------------------
  6585. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6586. {
  6587. UpdateControls();
  6588. SessionColor = TColor(0);
  6589. UpdateRemotePathComboBox(false);
  6590. NeedSession(false);
  6591. }
  6592. //---------------------------------------------------------------------------
  6593. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6594. {
  6595. try
  6596. {
  6597. // Cache, as the login dialog can change its value.
  6598. // See also DoShow. Also see TTerminalManager::NewSession.
  6599. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6600. try
  6601. {
  6602. if (ShowLogin)
  6603. {
  6604. bool ReloadSessions = !Startup;
  6605. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6606. }
  6607. }
  6608. __finally
  6609. {
  6610. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6611. // (so there was no chance for the user to open any session yet)
  6612. if ((ShowLogin || !Startup) &&
  6613. !WinConfiguration->KeepOpenWhenNoSession &&
  6614. // this rules out the implicit Commander's local-local browser
  6615. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6616. {
  6617. TerminateApplication();
  6618. }
  6619. }
  6620. }
  6621. catch (EAbort &)
  6622. {
  6623. // swallow
  6624. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6625. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6626. }
  6627. }
  6628. //---------------------------------------------------------------------------
  6629. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6630. {
  6631. TTerminalManager * Manager = TTerminalManager::Instance();
  6632. DebugAssert(!Manager->Updating);
  6633. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6634. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6635. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6636. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6637. try
  6638. {
  6639. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6640. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6641. {
  6642. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6643. }
  6644. for (int Index = 0; Index <= Manager->Count; Index++)
  6645. {
  6646. TThemeTabSheet * TabSheet;
  6647. if (Index >= SessionsPageControl->PageCount)
  6648. {
  6649. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6650. TabSheet->PageControl = SessionsPageControl;
  6651. }
  6652. else
  6653. {
  6654. TabSheet = SessionsPageControl->Pages[Index];
  6655. }
  6656. bool IsSessionTab = (Index < Manager->Count);
  6657. if (IsSessionTab)
  6658. {
  6659. TTerminal * Terminal = Manager->Sessions[Index];
  6660. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6661. TabSheet->CaptionTruncation = tttEllipsis;
  6662. UpdateSessionTab(TabSheet);
  6663. }
  6664. else
  6665. {
  6666. TabSheet->Tag = 0; // not really needed
  6667. TabSheet->Shadowed = false;
  6668. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6669. TabSheet->CaptionTruncation = tttNoText;
  6670. // 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)
  6671. UpdateNewTabTab();
  6672. }
  6673. }
  6674. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6675. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6676. {
  6677. SessionsPageControl->UpdateTabsCaptionTruncation();
  6678. }
  6679. }
  6680. __finally
  6681. {
  6682. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6683. }
  6684. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6685. }
  6686. //---------------------------------------------------------------------------
  6687. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6688. {
  6689. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6690. }
  6691. //---------------------------------------------------------------------------
  6692. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6693. {
  6694. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6695. UnicodeString TabCaption;
  6696. if (WinConfiguration->SelectiveToolbarText)
  6697. {
  6698. TabCaption = GetNewTabTabCaption();
  6699. }
  6700. TabSheet->BaseCaption = TabCaption;
  6701. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6702. }
  6703. //---------------------------------------------------------------------------
  6704. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6705. {
  6706. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6707. }
  6708. //---------------------------------------------------------------------------
  6709. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6710. {
  6711. DebugFail();
  6712. return UnicodeString();
  6713. }
  6714. //---------------------------------------------------------------------------
  6715. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6716. {
  6717. if (DebugAlwaysTrue(TabSheet != NULL))
  6718. {
  6719. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6720. if (DebugAlwaysTrue(ASession != NULL))
  6721. {
  6722. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6723. if (ASession->LocalBrowser)
  6724. {
  6725. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6726. }
  6727. else
  6728. {
  6729. TabSheet->ImageIndex = AddSessionColor(Color);
  6730. }
  6731. TTerminalManager * Manager = TTerminalManager::Instance();
  6732. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6733. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6734. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6735. }
  6736. }
  6737. }
  6738. //---------------------------------------------------------------------------
  6739. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6740. {
  6741. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6742. }
  6743. //---------------------------------------------------------------------------
  6744. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6745. {
  6746. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6747. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6748. bool Result = (Session != NULL);
  6749. if (Result)
  6750. {
  6751. TTerminalManager::Instance()->ActiveSession = Session;
  6752. }
  6753. else
  6754. {
  6755. try
  6756. {
  6757. NewTab();
  6758. }
  6759. __finally
  6760. {
  6761. SessionListChanged();
  6762. }
  6763. FSessionsPageControlNewTabTime = Now();
  6764. }
  6765. return Result;
  6766. }
  6767. //---------------------------------------------------------------------------
  6768. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6769. {
  6770. TWindowLock Lock(this);
  6771. SessionTabSwitched();
  6772. }
  6773. //---------------------------------------------------------------------------
  6774. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6775. {
  6776. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6777. DebugAssert(TransferList != NULL);
  6778. UnicodeString Name;
  6779. if (TransferList->ItemIndex <= 0)
  6780. {
  6781. Name = L"";
  6782. }
  6783. else
  6784. {
  6785. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6786. }
  6787. if (FCopyParamAutoSelected.IsEmpty())
  6788. {
  6789. // if previous preset was not autoselected, make new preset the "default"
  6790. FCopyParamDefault = Name;
  6791. }
  6792. GUIConfiguration->CopyParamCurrent = Name;
  6793. }
  6794. //---------------------------------------------------------------------------
  6795. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6796. {
  6797. // sanity check
  6798. bool ExistingPreset =
  6799. (FTransferListHoverIndex >= 0) &&
  6800. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6801. DebugAssert(ExistingPreset);
  6802. if (ExistingPreset)
  6803. {
  6804. HDC DC = GetDC(0);
  6805. TCanvas * Canvas = new TCanvas();
  6806. try
  6807. {
  6808. Canvas->Handle = DC;
  6809. Canvas->Font = GetToolbarFont(this);
  6810. UnicodeString Name;
  6811. if (FTransferListHoverIndex == 0)
  6812. {
  6813. Name = L"";
  6814. }
  6815. else
  6816. {
  6817. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6818. }
  6819. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6820. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6821. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6822. static_cast<TObject*>(GetComponent(fcTransferList)));
  6823. UnicodeString InfoStr =
  6824. GUIConfiguration->CopyParamPreset[Name].
  6825. GetInfoStr(L"; ",
  6826. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6827. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6828. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6829. {
  6830. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6831. {
  6832. InfoStr.SetLength(InfoStr.Length() - 1);
  6833. }
  6834. InfoStr += Ellipsis;
  6835. }
  6836. // UpdateCaption does not cause invalidation of whole submenu, while
  6837. // setting Caption property does.
  6838. // also it probably does not resize the label, even if necessary
  6839. // (we do not want that anyway)
  6840. TransferLabel->UpdateCaption(InfoStr);
  6841. }
  6842. __finally
  6843. {
  6844. Canvas->Handle = NULL;
  6845. ReleaseDC(0, DC);
  6846. delete Canvas;
  6847. }
  6848. }
  6849. }
  6850. //---------------------------------------------------------------------------
  6851. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6852. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6853. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6854. {
  6855. if (FTransferListHoverIndex != AHoverIndex)
  6856. {
  6857. FTransferListHoverIndex = AHoverIndex;
  6858. UpdateTransferLabel();
  6859. }
  6860. }
  6861. //---------------------------------------------------------------------------
  6862. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6863. {
  6864. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6865. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6866. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6867. {
  6868. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6869. {
  6870. if (!NonVisualDataModule->Busy)
  6871. {
  6872. OpenDirectory(GetSide(osCurrent));
  6873. }
  6874. Message.Result = 1;
  6875. }
  6876. else
  6877. {
  6878. TForm::Dispatch(&Message);
  6879. }
  6880. }
  6881. else
  6882. {
  6883. TForm::Dispatch(&Message);
  6884. }
  6885. }
  6886. //---------------------------------------------------------------------------
  6887. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6888. {
  6889. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6890. if ((FIgnoreNextDialogChar != 0) &&
  6891. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6892. {
  6893. Message.Result = 1;
  6894. }
  6895. else
  6896. {
  6897. TForm::Dispatch(&Message);
  6898. }
  6899. FIgnoreNextDialogChar = 0;
  6900. }
  6901. //---------------------------------------------------------------------------
  6902. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6903. {
  6904. // The four low-order bits are used internally by Windows
  6905. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6906. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6907. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6908. // 61730 - restore thru double click - undocumented
  6909. // 61490 - maximize thru double click - undocumented
  6910. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6911. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6912. {
  6913. SysResizing(Cmd);
  6914. }
  6915. TForm::Dispatch(&Message);
  6916. }
  6917. //---------------------------------------------------------------------------
  6918. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6919. {
  6920. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6921. // Also there no point returning TRUE as we are not able to
  6922. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6923. // Hence the process termination might be safer :)
  6924. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6925. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6926. {
  6927. Message.Result = FALSE;
  6928. }
  6929. else
  6930. {
  6931. Message.Result = TRUE;
  6932. }
  6933. // Do not call default handling as that triggers OnCloseQuery,
  6934. // where our implementation will popup confirmation dialogs, what we do not want,
  6935. // as per Vista guidelines:
  6936. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6937. }
  6938. //---------------------------------------------------------------------------
  6939. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6940. {
  6941. if (Message.EndSession && IsApplicationMinimized())
  6942. {
  6943. // WORKAROUND
  6944. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6945. // when it receives WM_ENDSESSION,
  6946. // but that sometimes (particularly when application is minimized) cause crashes.
  6947. // Crash popups message that blocks log off.
  6948. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6949. // so we call ExitProcess() immediately, not even trying to cleanup.
  6950. // It still causes beep, so there's likely some popup, but it does not block
  6951. // log off.
  6952. ExitProcess(0);
  6953. }
  6954. TForm::Dispatch(&Message);
  6955. }
  6956. #ifdef _DEBUG
  6957. //---------------------------------------------------------------------------
  6958. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6959. {
  6960. TForm::Dispatch(&Message);
  6961. }
  6962. #endif
  6963. //---------------------------------------------------------------------------
  6964. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6965. {
  6966. }
  6967. //---------------------------------------------------------------------------
  6968. void __fastcall TCustomScpExplorerForm::DoShow()
  6969. {
  6970. // Called from TCustomScpExplorerForm::CMShowingChanged
  6971. // only now are the controls resized finally, so the size constraints
  6972. // will not conflict with possibly very small window size
  6973. RestoreFormParams();
  6974. // This indicates that the form was rescaled after parameters were loaded and
  6975. // as most probably the parameters were actually saved with the current form scaling
  6976. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6977. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6978. // and no rounding problems will occur.
  6979. // See also RestoreForm.
  6980. // (we should be safe to call RestoreParams unconditionally)
  6981. if (PixelsPerInch != Screen->PixelsPerInch)
  6982. {
  6983. RestoreParams();
  6984. }
  6985. FixControlsPlacement();
  6986. if (Position == poDefaultPosOnly)
  6987. {
  6988. CutFormToDesktop(this);
  6989. }
  6990. TForm::DoShow();
  6991. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6992. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6993. // This was previously in NeedSession, where other related code is.
  6994. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6995. // unpleasantly shows briefly.
  6996. if ((ManagedSession == NULL) &&
  6997. !WinConfiguration->ShowLoginWhenNoSession &&
  6998. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6999. // This detects if workspace was saved the last time the main widow was closed
  7000. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7001. {
  7002. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7003. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7004. }
  7005. // Before main window displays
  7006. if (ManagedSession == NULL)
  7007. {
  7008. StartingWithoutSession();
  7009. }
  7010. UpdatePixelsPerInchMainWindowCounter();
  7011. FShowing = true;
  7012. }
  7013. //---------------------------------------------------------------------------
  7014. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7015. {
  7016. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7017. }
  7018. //---------------------------------------------------------------------------
  7019. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7020. {
  7021. SessionListChanged();
  7022. InitStatusBar();
  7023. UpdateControls();
  7024. }
  7025. //---------------------------------------------------------------------------
  7026. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7027. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7028. TNotifyEvent OnBalloonClick, TObject * UserData)
  7029. {
  7030. bool Do;
  7031. UnicodeString Message;
  7032. if (E == NULL)
  7033. {
  7034. Message = Str;
  7035. Do = true;
  7036. }
  7037. else
  7038. {
  7039. Do = ExceptionMessage(E, Message);
  7040. }
  7041. if (Do && WinConfiguration->BalloonNotifications)
  7042. {
  7043. UnicodeString Title;
  7044. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7045. {
  7046. Message = TrimLeft(Message);
  7047. }
  7048. else
  7049. {
  7050. Message = UnformatMessage(Message);
  7051. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7052. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7053. Title = LoadResourceString(Captions[Type]);
  7054. if (Terminal != NULL)
  7055. {
  7056. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7057. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7058. }
  7059. }
  7060. if (Seconds == 0)
  7061. {
  7062. Seconds = WinConfiguration->NotificationsTimeout;
  7063. }
  7064. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7065. }
  7066. }
  7067. //---------------------------------------------------------------------------
  7068. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7069. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7070. UnicodeString HelpKeyword, const TMessageParams * Params,
  7071. TTerminal * Terminal)
  7072. {
  7073. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7074. (FOnFeedSynchronizeError != NULL)) &&
  7075. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7076. {
  7077. if (FOnFeedSynchronizeError != NULL)
  7078. {
  7079. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7080. }
  7081. else
  7082. {
  7083. DebugAssert(FErrorList != NULL);
  7084. TStringList * MoreMessagesCopy = NULL;
  7085. if (MoreMessages)
  7086. {
  7087. MoreMessagesCopy = new TStringList();
  7088. MoreMessagesCopy->Assign(MoreMessages);
  7089. }
  7090. FErrorList->AddObject(Message, MoreMessagesCopy);
  7091. }
  7092. PopupTrayBalloon(Terminal, Message, Type);
  7093. return ContinueAnswer(Answers);
  7094. }
  7095. else
  7096. {
  7097. bool UseBalloon = FTrayIcon->Visible;
  7098. if (UseBalloon)
  7099. {
  7100. PopupTrayBalloon(Terminal, Message, Type);
  7101. }
  7102. unsigned int Result;
  7103. try
  7104. {
  7105. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7106. }
  7107. __finally
  7108. {
  7109. // cancel only balloon we popped up, otherwise we may cancel notification
  7110. // balloon that was there before the message dialog
  7111. // (such as "dd confirmation opt in balloon)
  7112. if (UseBalloon)
  7113. {
  7114. FTrayIcon->CancelBalloon();
  7115. }
  7116. }
  7117. return Result;
  7118. }
  7119. }
  7120. //---------------------------------------------------------------------------
  7121. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7122. TTerminal * Terminal, Exception * E)
  7123. {
  7124. if (FTrayIcon->Visible)
  7125. {
  7126. PopupTrayBalloon(Terminal, L"", qtError, E);
  7127. }
  7128. // particularly prevent opening new session from jump list,
  7129. // while exception is shown
  7130. NonVisualDataModule->StartBusy();
  7131. try
  7132. {
  7133. ShowExtendedExceptionEx(Terminal, E);
  7134. }
  7135. __finally
  7136. {
  7137. NonVisualDataModule->EndBusy();
  7138. FTrayIcon->CancelBalloon();
  7139. }
  7140. }
  7141. //---------------------------------------------------------------------------
  7142. void __fastcall TCustomScpExplorerForm::SessionReady()
  7143. {
  7144. InitStatusBar();
  7145. // cannot rely on active page being page for active terminal,
  7146. // as it can happen that active page is the "new session" page
  7147. // (e.g. when reconnecting active terminal, while login dialog
  7148. // invoked from "new session" page is modal)
  7149. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7150. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7151. }
  7152. //---------------------------------------------------------------------------
  7153. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7154. TTerminal * Terminal, Exception * E)
  7155. {
  7156. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7157. if (!Terminal->Active)
  7158. {
  7159. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7160. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7161. {
  7162. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7163. }
  7164. }
  7165. }
  7166. //---------------------------------------------------------------------------
  7167. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7168. UnicodeString Message, TQueryType Type,
  7169. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7170. {
  7171. if ((E == NULL) ||
  7172. ExceptionMessage(E, Message))
  7173. {
  7174. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7175. if (Important)
  7176. {
  7177. Seconds *= 5;
  7178. }
  7179. UnicodeString NoteMessage(UnformatMessage(Message));
  7180. if (Terminal != NULL)
  7181. {
  7182. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7183. NoteMessage = FORMAT(L"%s: %s",
  7184. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7185. }
  7186. if (WinConfiguration->BalloonNotifications)
  7187. {
  7188. AddNote(NoteMessage);
  7189. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7190. }
  7191. else
  7192. {
  7193. FlashOnBackground();
  7194. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7195. }
  7196. }
  7197. }
  7198. //---------------------------------------------------------------------------
  7199. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7200. {
  7201. RestoreApp();
  7202. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7203. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7204. !NonVisualDataModule->Busy)
  7205. {
  7206. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7207. TTerminalManager::Instance()->ActiveSession = Terminal;
  7208. if (!ComponentVisible[fcQueueView])
  7209. {
  7210. ToggleQueueVisibility();
  7211. GoToQueue();
  7212. }
  7213. }
  7214. }
  7215. //---------------------------------------------------------------------------
  7216. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7217. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7218. {
  7219. UnicodeString Message;
  7220. TNotifyEvent OnClick = NULL;
  7221. TObject * UserData = NULL;
  7222. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7223. switch (Event)
  7224. {
  7225. case qeEmptyButMonitored:
  7226. if ((ATerminal != Terminal) || QueueInvisible)
  7227. {
  7228. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7229. OnClick = QueueEmptyNoteClicked;
  7230. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7231. TerminalNoteData->Terminal = ATerminal;
  7232. UserData = TerminalNoteData;
  7233. }
  7234. break;
  7235. case qeEmpty:
  7236. OperationComplete(ATerminal->QueueOperationStart);
  7237. break;
  7238. case qePendingUserAction:
  7239. if ((ATerminal != Terminal) ||
  7240. (QueueInvisible && !IsQueueAutoPopup()))
  7241. {
  7242. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7243. }
  7244. break;
  7245. default:
  7246. DebugFail();
  7247. }
  7248. if (!Message.IsEmpty())
  7249. {
  7250. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7251. }
  7252. }
  7253. //---------------------------------------------------------------------------
  7254. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7255. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7256. {
  7257. if (FDDExtMapFile != NULL)
  7258. {
  7259. CloseHandle(FDDExtMapFile);
  7260. FDDExtMapFile = NULL;
  7261. }
  7262. if (WinConfiguration->DDFakeFile)
  7263. {
  7264. DDFakeFileInitDrag(FileList, Created);
  7265. }
  7266. }
  7267. //---------------------------------------------------------------------------
  7268. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7269. {
  7270. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7271. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7272. {
  7273. FFakeFileDropTarget = FileName;
  7274. }
  7275. }
  7276. //---------------------------------------------------------------------------
  7277. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7278. {
  7279. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7280. if (!ForceDirectories(ApiPath(Result)))
  7281. {
  7282. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7283. }
  7284. FileSetAttr(ApiPath(Result), faHidden);
  7285. return Result;
  7286. }
  7287. //---------------------------------------------------------------------------
  7288. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7289. bool & Created)
  7290. {
  7291. FFakeFileDropTarget = UnicodeString();
  7292. FDragFakeDirectory = CreateFakeTransferDirectory();
  7293. FileList->AddItem(NULL, FDragFakeDirectory);
  7294. Created = true;
  7295. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7296. {
  7297. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7298. }
  7299. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7300. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7301. {
  7302. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7303. TDragExtCommStruct* CommStruct;
  7304. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7305. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7306. DebugAssert(CommStruct != NULL);
  7307. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7308. CommStruct->Dragging = true;
  7309. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7310. LENOF(CommStruct->DropDest));
  7311. NULL_TERMINATE(CommStruct->DropDest);
  7312. UnmapViewOfFile(CommStruct);
  7313. }
  7314. }
  7315. //---------------------------------------------------------------------------
  7316. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7317. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7318. {
  7319. bool Result;
  7320. DebugAssert(!IsLocalBrowserMode());
  7321. if (Sender == RemoteDirView)
  7322. {
  7323. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7324. }
  7325. else
  7326. {
  7327. DebugAssert(Sender == RemoteDriveView);
  7328. TStrings * FileList = RemoteDriveView->DragFileList();
  7329. try
  7330. {
  7331. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7332. }
  7333. __finally
  7334. {
  7335. delete FileList;
  7336. }
  7337. }
  7338. if (FDDTargetControl == RemoteDriveView)
  7339. {
  7340. RemoteDriveView->UpdateDropTarget();
  7341. }
  7342. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7343. // or from tree view to dir view.
  7344. // foMove happens when file/dir is dragged from remote tree view outside of
  7345. // application via dragex
  7346. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7347. (DropSourceControl == RemoteDriveView))
  7348. {
  7349. RemoteDriveView->UpdateDropSource();
  7350. }
  7351. return Result;
  7352. }
  7353. //---------------------------------------------------------------------------
  7354. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7355. {
  7356. DebugAssert(!IsLocalBrowserMode());
  7357. // This also handles drops of remote files to queue.
  7358. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7359. SAFE_DESTROY(FDDFileList);
  7360. if (!FFakeFileDropTarget.IsEmpty())
  7361. {
  7362. RemoveDir(ApiPath(FFakeFileDropTarget));
  7363. }
  7364. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7365. {
  7366. try
  7367. {
  7368. TDragResult DDResult = (Sender == RemoteDirView) ?
  7369. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7370. // Focus is moved to the target application,
  7371. // but as we are going to present the UI, we need to steal the focus back.
  7372. // This most likely won't work though (windows does not allow application
  7373. // to steal focus most of the time)
  7374. Application->BringToFront();
  7375. // On older version of Windows we never got drMove here, see also comment below.
  7376. // On Windows 10, we get the "move".
  7377. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7378. {
  7379. UnicodeString TargetDirectory;
  7380. TFileOperation Operation;
  7381. // drInvalid may mean drMove, see comment below
  7382. switch (DDResult)
  7383. {
  7384. case drCopy:
  7385. Operation = foCopy;
  7386. break;
  7387. case drMove:
  7388. Operation = foMove;
  7389. break;
  7390. default:
  7391. DebugFail();
  7392. case drInvalid:
  7393. // prefer "copy" for safety
  7394. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7395. break;
  7396. }
  7397. TTransferOperationParam Param;
  7398. UnicodeString CounterName;
  7399. bool ForceQueue;
  7400. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7401. {
  7402. // we get drInvalid both if d&d was intercepted by ddext,
  7403. // and when users drops on no-drop location.
  7404. // we tell the difference by existence of response from ddext,
  7405. // so we ignore absence of response in this case
  7406. if (DDResult != drInvalid)
  7407. {
  7408. // here we know that the extension is installed,
  7409. // as it is checked as soon as drag&drop starts from
  7410. // RemoteFileControlDDCreateDragFileList
  7411. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7412. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7413. {
  7414. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7415. }
  7416. else
  7417. {
  7418. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7419. }
  7420. }
  7421. }
  7422. else
  7423. {
  7424. // download using fake file
  7425. Param.Temp = false;
  7426. Param.DragDrop = true;
  7427. if (ForceQueue)
  7428. {
  7429. Param.Queue = asOn;
  7430. }
  7431. if (Sender == RemoteDirView)
  7432. {
  7433. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7434. }
  7435. if (RemoteFileControlFileOperation(Sender, Operation,
  7436. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7437. {
  7438. Configuration->Usage->Inc(CounterName);
  7439. }
  7440. }
  7441. }
  7442. }
  7443. __finally
  7444. {
  7445. CloseHandle(FDDExtMapFile);
  7446. FDDExtMapFile = NULL;
  7447. delete FDragFakeMonitors;
  7448. FDragFakeMonitors = NULL;
  7449. RemoveDir(ApiPath(FDragFakeDirectory));
  7450. FDragFakeDirectory = L"";
  7451. FFakeFileDropTarget = UnicodeString();
  7452. }
  7453. }
  7454. if (FDragDropSshTerminate.get() != NULL)
  7455. {
  7456. FDragDropSshTerminate->Rethrow();
  7457. }
  7458. }
  7459. //---------------------------------------------------------------------------
  7460. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7461. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7462. {
  7463. // Remember drop effect so we know (when user drops files), if we copy or move
  7464. FLastDropEffect = dwEffect;
  7465. }
  7466. //---------------------------------------------------------------------------
  7467. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7468. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7469. {
  7470. bool Result;
  7471. if (FDDTargetControl == QueueView3)
  7472. {
  7473. Directory = DefaultDownloadTargetDirectory();
  7474. Result = true;
  7475. CounterName = L"DownloadsDragDropQueue";
  7476. ForceQueue = true;
  7477. }
  7478. else if (!FFakeFileDropTarget.IsEmpty())
  7479. {
  7480. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7481. Result = true;
  7482. ForceQueue = false;
  7483. CounterName = L"DownloadsDragDropFakeFile";
  7484. }
  7485. else
  7486. {
  7487. ForceQueue = false;
  7488. Enabled = false;
  7489. try
  7490. {
  7491. int Timer = 0;
  7492. Result = false;
  7493. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7494. {
  7495. {
  7496. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7497. TDragExtCommStruct* CommStruct;
  7498. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7499. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7500. DebugAssert(CommStruct != NULL);
  7501. Result = !CommStruct->Dragging;
  7502. if (Result)
  7503. {
  7504. Directory = ExtractFilePath(CommStruct->DropDest);
  7505. CounterName = L"DownloadsDragDropExternalExt";
  7506. }
  7507. UnmapViewOfFile(CommStruct);
  7508. }
  7509. if (!Result)
  7510. {
  7511. Sleep(50);
  7512. Timer += 50;
  7513. Application->ProcessMessages();
  7514. }
  7515. }
  7516. }
  7517. __finally
  7518. {
  7519. Enabled = true;
  7520. }
  7521. }
  7522. return Result;
  7523. }
  7524. //---------------------------------------------------------------------------
  7525. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7526. const UnicodeString TempDir, int SecDelay)
  7527. {
  7528. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7529. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7530. if (FDelayedDeletionTimer == NULL)
  7531. {
  7532. DebugAssert(HandleAllocated());
  7533. FDelayedDeletionTimer = new TTimer(this);
  7534. FDelayedDeletionTimer->Interval = 10000;
  7535. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7536. }
  7537. else
  7538. {
  7539. FDelayedDeletionTimer->Enabled = true;
  7540. }
  7541. }
  7542. //---------------------------------------------------------------------------
  7543. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7544. {
  7545. DebugAssert(FDelayedDeletionList != NULL);
  7546. TDateTime N = Now();
  7547. TDateTime Alert;
  7548. UnicodeString Directory;
  7549. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7550. {
  7551. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7552. if ((N >= Alert) || (Sender == NULL))
  7553. {
  7554. Directory = FDelayedDeletionList->Strings[Index];
  7555. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7556. {
  7557. FDelayedDeletionList->Delete(Index);
  7558. }
  7559. }
  7560. }
  7561. if (FDelayedDeletionList->Count == 0)
  7562. {
  7563. FDelayedDeletionTimer->Enabled = false;
  7564. }
  7565. }
  7566. //---------------------------------------------------------------------------
  7567. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7568. {
  7569. DebugAssert(!IsLocalBrowserMode());
  7570. if (IsFileControl(FDDTargetControl, osRemote) ||
  7571. (FDDTargetControl == SessionsPageControl))
  7572. {
  7573. TManagedTerminal * TargetTerminal = NULL;
  7574. TFileOperation Operation = foNone;
  7575. if (FDDTargetControl == SessionsPageControl)
  7576. {
  7577. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7578. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7579. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7580. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7581. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7582. {
  7583. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7584. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7585. {
  7586. Operation = foRemoteMove;
  7587. }
  7588. else
  7589. {
  7590. Operation = foRemoteCopy;
  7591. }
  7592. }
  7593. }
  7594. else
  7595. {
  7596. if (FLastDropEffect == DROPEFFECT_MOVE)
  7597. {
  7598. Operation = foRemoteMove;
  7599. }
  7600. else if (FLastDropEffect == DROPEFFECT_COPY)
  7601. {
  7602. Operation = foRemoteCopy;
  7603. }
  7604. }
  7605. if (Operation != foNone)
  7606. {
  7607. RemoteFileControlFileOperation(DropSourceControl,
  7608. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7609. }
  7610. // abort drag&drop
  7611. Abort();
  7612. }
  7613. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7614. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7615. (FDDTargetControl != QueueView3))
  7616. {
  7617. DebugAssert(!FDragTempDir.IsEmpty());
  7618. TTransferType Type;
  7619. UnicodeString TempDir = FDragTempDir;
  7620. // We clear FDragTempDir before calling
  7621. // just in case it fail (raises exception)
  7622. FDragTempDir = L"";
  7623. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7624. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7625. // empty directory parameter means temp directory -> don't display it!
  7626. UnicodeString TargetDir = L"";
  7627. int Options = 0;
  7628. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7629. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7630. {
  7631. Abort();
  7632. }
  7633. // TargetDir is set when dropped on local file control
  7634. // (this was workaround for legacy dirview event handling, now it should be
  7635. // made prettier)
  7636. if (TargetDir.IsEmpty())
  7637. {
  7638. TargetDir = TempDir;
  7639. if (ForceDirectories(ApiPath(TargetDir)))
  7640. {
  7641. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7642. FPendingTempSpaceWarn = true;
  7643. try
  7644. {
  7645. FDragDropOperation = true;
  7646. int Params = cpTemporary |
  7647. (Type == ttMove ? cpDelete : 0);
  7648. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7649. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7650. }
  7651. __finally
  7652. {
  7653. FDragDropOperation = false;
  7654. FPendingTempSpaceWarn = false;
  7655. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7656. }
  7657. }
  7658. else
  7659. {
  7660. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7661. }
  7662. }
  7663. }
  7664. }
  7665. //---------------------------------------------------------------------------
  7666. void __fastcall TCustomScpExplorerForm::DDDownload(
  7667. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7668. {
  7669. DebugAssert(!IsLocalBrowserMode());
  7670. TAutoBatch AutoBatch(this);
  7671. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7672. UpdateCopyParamCounters(*CopyParam);
  7673. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7674. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7675. {
  7676. RemoteDriveView->UpdateDropSource();
  7677. }
  7678. }
  7679. //---------------------------------------------------------------------------
  7680. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7681. {
  7682. public:
  7683. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7684. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7685. {
  7686. }
  7687. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7688. {
  7689. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7690. TDataObjectFilesEx::AllowData(FormatEtc);
  7691. }
  7692. };
  7693. //---------------------------------------------------------------------------
  7694. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7695. TObject * Sender, TDataObject *& DataObject)
  7696. {
  7697. if (FDDExtMapFile != NULL)
  7698. {
  7699. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7700. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7701. {
  7702. Abort();
  7703. }
  7704. if (FileList->Count > 0)
  7705. {
  7706. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7707. if (!FilesObject->IsValid(true, true))
  7708. {
  7709. FilesObject->_Release();
  7710. }
  7711. else
  7712. {
  7713. DataObject = FilesObject;
  7714. }
  7715. }
  7716. }
  7717. }
  7718. //---------------------------------------------------------------------------
  7719. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7720. {
  7721. DebugFail();
  7722. }
  7723. //---------------------------------------------------------------------------
  7724. void __fastcall TCustomScpExplorerForm::GoToTree()
  7725. {
  7726. ComponentVisible[fcRemoteTree] = true;
  7727. DriveView(osRemote)->SetFocus();
  7728. }
  7729. //---------------------------------------------------------------------------
  7730. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7731. TPanelExport Export)
  7732. {
  7733. TCustomDirView * DirView = this->DirView(Side);
  7734. std::unique_ptr<TStrings> ExportData(new TStringList());
  7735. switch (Export)
  7736. {
  7737. case pePath:
  7738. ExportData->Add(DirView->PathName);
  7739. break;
  7740. case peFileList:
  7741. case peFullFileList:
  7742. {
  7743. bool FullPath = (Export == peFullFileList);
  7744. DirView->CreateFileList(false, FullPath, ExportData.get());
  7745. for (int Index = 0; Index < ExportData->Count; Index++)
  7746. {
  7747. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7748. {
  7749. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7750. }
  7751. }
  7752. }
  7753. break;
  7754. default:
  7755. DebugFail();
  7756. }
  7757. return ExportData.release();
  7758. }
  7759. //---------------------------------------------------------------------------
  7760. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7761. TPanelExport Export, TPanelExportDestination Destination)
  7762. {
  7763. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7764. PanelExportStore(Side, Export, Destination, ExportData.get());
  7765. }
  7766. //---------------------------------------------------------------------------
  7767. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7768. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7769. TStrings * ExportData)
  7770. {
  7771. if (Destination == pedClipboard)
  7772. {
  7773. TInstantOperationVisualizer Visualizer;
  7774. CopyToClipboard(ExportData);
  7775. }
  7776. else
  7777. {
  7778. DebugFail();
  7779. }
  7780. }
  7781. //---------------------------------------------------------------------------
  7782. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7783. {
  7784. TCustomDirView * DirView = this->DirView(Side);
  7785. UnicodeString Mask = DirView->Mask;
  7786. if (DoFilterMaskDialog(DirView, Mask))
  7787. {
  7788. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7789. Configuration->Usage->Inc(L"Filters");
  7790. }
  7791. }
  7792. //---------------------------------------------------------------------------
  7793. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7794. {
  7795. return FQueueController->DefaultOperation();
  7796. }
  7797. //---------------------------------------------------------------------------
  7798. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7799. TQueueOperation Operation, void ** Param)
  7800. {
  7801. switch (Operation)
  7802. {
  7803. case qoPreferences:
  7804. return true;
  7805. case qoGoTo:
  7806. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7807. case qoOnceEmpty:
  7808. return IsAnythingQueued();
  7809. default:
  7810. return FQueueController->AllowOperation(Operation, Param);
  7811. }
  7812. }
  7813. //---------------------------------------------------------------------------
  7814. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7815. {
  7816. if (DebugAlwaysTrue(QueueView3->Visible))
  7817. {
  7818. QueueView3->SetFocus();
  7819. }
  7820. }
  7821. //---------------------------------------------------------------------------
  7822. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7823. TQueueOperation Operation, void * Param)
  7824. {
  7825. if (Operation == qoGoTo)
  7826. {
  7827. GoToQueue();
  7828. }
  7829. else if (Operation == qoPreferences)
  7830. {
  7831. PreferencesDialog(pmQueue);
  7832. }
  7833. else
  7834. {
  7835. FQueueController->ExecuteOperation(Operation, Param);
  7836. }
  7837. }
  7838. //---------------------------------------------------------------------------
  7839. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7840. {
  7841. return (Queue != NULL) && Queue->Enabled;
  7842. }
  7843. //---------------------------------------------------------------------------
  7844. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7845. {
  7846. DebugAssert(Queue != NULL);
  7847. if (Queue != NULL)
  7848. {
  7849. Queue->Enabled = !Queue->Enabled;
  7850. }
  7851. }
  7852. //---------------------------------------------------------------------------
  7853. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7854. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7855. {
  7856. FQueueActedItem = QueueView3->ItemFocused;
  7857. }
  7858. //---------------------------------------------------------------------------
  7859. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7860. {
  7861. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  7862. }
  7863. //---------------------------------------------------------------------------
  7864. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7865. {
  7866. return
  7867. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7868. (QueueDock->Visible ? QueueDock->Height : 0) +
  7869. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7870. }
  7871. //---------------------------------------------------------------------------
  7872. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7873. {
  7874. return ScaleByTextHeight(this, 48);
  7875. }
  7876. //---------------------------------------------------------------------------
  7877. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7878. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7879. {
  7880. // when queue is hidden by double-clicking splitter, stray attempt to
  7881. // resize the panel with strange value arrives, make sure it is ignored
  7882. if (ComponentVisible[fcQueueView])
  7883. {
  7884. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7885. if (NewSize > MaxHeightLimit)
  7886. {
  7887. NewSize = MaxHeightLimit;
  7888. }
  7889. int MinHeightLimit =
  7890. GetStaticQueuePanelComponentsHeight() +
  7891. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7892. GetMinQueueViewHeight();
  7893. if (NewSize < MinHeightLimit)
  7894. {
  7895. NewSize = MinHeightLimit;
  7896. }
  7897. }
  7898. else
  7899. {
  7900. Accept = false;
  7901. }
  7902. }
  7903. //---------------------------------------------------------------------------
  7904. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7905. {
  7906. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7907. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7908. if (QueueViewHeight < GetMinQueueViewHeight())
  7909. {
  7910. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7911. }
  7912. }
  7913. //---------------------------------------------------------------------------
  7914. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7915. TDragObject *& /*DragObject*/)
  7916. {
  7917. FQueueActedItem = QueueView3->ItemFocused;
  7918. }
  7919. //---------------------------------------------------------------------------
  7920. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7921. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7922. {
  7923. Accept = true;
  7924. if (Source == QueueView3)
  7925. {
  7926. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7927. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7928. if (Accept)
  7929. {
  7930. TQueueItemProxy * QueueItem;
  7931. TQueueItemProxy * DestQueueItem;
  7932. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7933. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7934. Accept = (QueueItem != DestQueueItem) &&
  7935. (QueueItem->Status == TQueueItem::qsPending) &&
  7936. (DestQueueItem->Status == TQueueItem::qsPending);
  7937. }
  7938. }
  7939. }
  7940. //---------------------------------------------------------------------------
  7941. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7942. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7943. {
  7944. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7945. {
  7946. TQueueItemProxy * QueueItem;
  7947. TQueueItemProxy * DestQueueItem;
  7948. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7949. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7950. QueueItem->Move(DestQueueItem);
  7951. }
  7952. }
  7953. //---------------------------------------------------------------------------
  7954. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7955. {
  7956. if ((QueueView3->ItemFocused == NULL) &&
  7957. (QueueView3->Items->Count > 0))
  7958. {
  7959. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7960. }
  7961. QueueLabelUpdateStatus();
  7962. }
  7963. //---------------------------------------------------------------------------
  7964. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7965. {
  7966. QueueLabelUpdateStatus();
  7967. }
  7968. //---------------------------------------------------------------------------
  7969. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7970. {
  7971. QueueLabel->UpdateStatus();
  7972. }
  7973. //---------------------------------------------------------------------------
  7974. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7975. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7976. {
  7977. if (Selected)
  7978. {
  7979. NonVisualDataModule->UpdateNonVisibleActions();
  7980. }
  7981. }
  7982. //---------------------------------------------------------------------------
  7983. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7984. {
  7985. TDragDropFilesEx * Result = NULL;
  7986. if (Sender == SessionsPageControl)
  7987. {
  7988. Result = FSessionsDragDropFilesEx;
  7989. }
  7990. else if (Sender == QueueView3)
  7991. {
  7992. Result = FQueueDragDropFilesEx;
  7993. }
  7994. else
  7995. {
  7996. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7997. if (DirView != NULL)
  7998. {
  7999. Result = DirView->DragDropFilesEx;
  8000. }
  8001. else
  8002. {
  8003. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8004. if (DriveView != NULL)
  8005. {
  8006. Result = DriveView->DragDropFilesEx;
  8007. }
  8008. }
  8009. }
  8010. DebugAssert(Result != NULL);
  8011. return Result;
  8012. }
  8013. //---------------------------------------------------------------------------
  8014. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8015. {
  8016. return (DView->SelCount == DView->FilesCount);
  8017. }
  8018. //---------------------------------------------------------------------------
  8019. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8020. {
  8021. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8022. }
  8023. //---------------------------------------------------------------------------
  8024. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8025. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8026. {
  8027. DebugAssert(!IsLocalBrowserMode());
  8028. TFileOperation Operation;
  8029. switch (Effect)
  8030. {
  8031. case DROPEFFECT_MOVE:
  8032. Operation = foMove;
  8033. break;
  8034. case DROPEFFECT_COPY:
  8035. // occures on WinXP (reported by user)
  8036. default:
  8037. Operation = foCopy;
  8038. break;
  8039. };
  8040. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8041. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8042. if (DragDropFilesEx->FileList->Count > 0)
  8043. {
  8044. TStrings * FileList = new TStringList();
  8045. try
  8046. {
  8047. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8048. {
  8049. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8050. }
  8051. FDragDropOperation = true;
  8052. TTransferOperationParam Param;
  8053. Param.TargetDirectory = TargetPath;
  8054. // upload, no temp dirs
  8055. Param.Temp = false;
  8056. Param.DragDrop = DragDrop;
  8057. Param.Options =
  8058. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8059. if (ForceQueue)
  8060. {
  8061. Param.Queue = asOn;
  8062. }
  8063. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8064. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8065. {
  8066. if (IsFileControl(DropSourceControl, osLocal))
  8067. {
  8068. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8069. }
  8070. else
  8071. {
  8072. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8073. }
  8074. }
  8075. }
  8076. __finally
  8077. {
  8078. FDragDropOperation = false;
  8079. delete FileList;
  8080. }
  8081. }
  8082. }
  8083. //---------------------------------------------------------------------------
  8084. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8085. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8086. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8087. {
  8088. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8089. }
  8090. //---------------------------------------------------------------------------
  8091. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8092. TObject * Sender, int grfKeyState, int & dwEffect)
  8093. {
  8094. DebugAssert(!IsLocalBrowserMode());
  8095. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8096. if ((dwEffect != DROPEFFECT_NONE) &&
  8097. IsFileControl(DropSourceControl, osRemote))
  8098. {
  8099. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8100. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8101. (RemoteDirView->DropTarget == NULL))
  8102. {
  8103. dwEffect = DROPEFFECT_NONE;
  8104. }
  8105. else
  8106. {
  8107. if (dwEffect == DROPEFFECT_COPY)
  8108. {
  8109. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8110. // currently we support copying always (at least via temporary directory);
  8111. // remove associated checks once this all proves stable and working
  8112. bool CopyCapable = true;
  8113. // if we do not support neither of operations, there's no discussion
  8114. if (!MoveCapable && !CopyCapable)
  8115. {
  8116. dwEffect = DROPEFFECT_NONE;
  8117. }
  8118. else
  8119. {
  8120. // The default effect inside remote panel is move,
  8121. // unless we do not support it, but support copy
  8122. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8123. {
  8124. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8125. }
  8126. else
  8127. {
  8128. // with ctrl-down, we want copy unless it is not supported
  8129. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8130. }
  8131. }
  8132. }
  8133. }
  8134. }
  8135. }
  8136. //---------------------------------------------------------------------------
  8137. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8138. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8139. {
  8140. DebugAssert(!IsLocalBrowserMode());
  8141. if (FDDTotalSize >= 0)
  8142. {
  8143. if (File->IsDirectory)
  8144. {
  8145. FDDTotalSize = -1;
  8146. }
  8147. else
  8148. {
  8149. FDDTotalSize += File->Size;
  8150. }
  8151. }
  8152. DebugAssert(!FDragTempDir.IsEmpty());
  8153. // TODO: this is quite ineffective
  8154. // TODO: what if invalid character replacement is disabled?
  8155. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8156. UnicodeString TransferFileName;
  8157. if (Sender == RemoteDriveView)
  8158. {
  8159. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8160. }
  8161. else
  8162. {
  8163. TransferFileName = File->FileName;
  8164. }
  8165. FDDFileList->AddObject(TransferFileName, File);
  8166. }
  8167. //---------------------------------------------------------------------------
  8168. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8169. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8170. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8171. {
  8172. // sometimes we do not get DDEnd so the list is not released
  8173. SAFE_DESTROY(FDDFileList);
  8174. FDDFileList = new TStringList();
  8175. FDragTempDir = WinConfiguration->TemporaryDir();
  8176. FDDTotalSize = 0;
  8177. FDragDropSshTerminate.reset(NULL);
  8178. }
  8179. //---------------------------------------------------------------------------
  8180. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8181. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8182. HRESULT & Result)
  8183. {
  8184. if (Result == DRAGDROP_S_DROP)
  8185. {
  8186. try
  8187. {
  8188. GlobalDragImageList->HideDragImage();
  8189. try
  8190. {
  8191. RemoteFileControlDDTargetDrop();
  8192. }
  8193. catch(ESshTerminate & E)
  8194. {
  8195. FDragDropSshTerminate.reset(E.Clone());
  8196. }
  8197. }
  8198. catch (Exception &E)
  8199. {
  8200. // If downloading fails we need to cancel drag&drop, otherwise
  8201. // Explorer shows error
  8202. // But by the way exception probably never reach this point as
  8203. // it's catched on way.
  8204. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8205. Result = DRAGDROP_S_CANCEL;
  8206. ShowExtendedException(Terminal, &E);
  8207. }
  8208. }
  8209. }
  8210. //---------------------------------------------------------------------------
  8211. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8212. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8213. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8214. {
  8215. TFileMasks::TParams MaskParams;
  8216. MaskParams.Size = Size;
  8217. MaskParams.Modification = Modification;
  8218. // this does not re-parse the mask if it is the same as the last time
  8219. FDirViewMatchMask = Masks;
  8220. bool ImplicitMatch;
  8221. Matches =
  8222. // Local and RecurseInclude parameters have no effect as there's no path.
  8223. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8224. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8225. (AllowImplicitMatches || !ImplicitMatch);
  8226. }
  8227. //---------------------------------------------------------------------------
  8228. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8229. TObject * Sender, TListItem * Item, WORD & Indexes)
  8230. {
  8231. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8232. UnicodeString Ext;
  8233. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8234. {
  8235. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8236. }
  8237. else
  8238. {
  8239. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8240. }
  8241. if (SameText(Ext, Configuration->PartialExt))
  8242. {
  8243. Indexes |= oiPartial;
  8244. }
  8245. }
  8246. //---------------------------------------------------------------------------
  8247. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8248. {
  8249. return
  8250. DirViewEnabled(osCurrent) &&
  8251. DirView(osCurrent)->CanPasteFromClipBoard();
  8252. }
  8253. //---------------------------------------------------------------------------
  8254. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8255. {
  8256. bool Result = false;
  8257. if (CanPasteToDirViewFromClipBoard())
  8258. {
  8259. Result = true;
  8260. }
  8261. else
  8262. {
  8263. UnicodeString ClipboardText;
  8264. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8265. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8266. {
  8267. if (StoredSessions->IsUrl(ClipboardText))
  8268. {
  8269. Result = true;
  8270. }
  8271. else
  8272. {
  8273. Result = DirViewEnabled(osCurrent);
  8274. }
  8275. }
  8276. }
  8277. return Result;
  8278. }
  8279. //---------------------------------------------------------------------------
  8280. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8281. {
  8282. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8283. if (DoesClipboardContainOurFiles())
  8284. {
  8285. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8286. {
  8287. DebugAssert(!IsLocalBrowserMode());
  8288. TTerminalManager * Manager = TTerminalManager::Instance();
  8289. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8290. Manager->ActiveSession = FClipboardTerminal;
  8291. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8292. }
  8293. }
  8294. else if (CanPasteToDirViewFromClipBoard())
  8295. {
  8296. DirView(osCurrent)->PasteFromClipBoard();
  8297. }
  8298. else
  8299. {
  8300. UnicodeString ClipboardText;
  8301. if (NonEmptyTextFromClipboard(ClipboardText))
  8302. {
  8303. if (StoredSessions->IsUrl(ClipboardText))
  8304. {
  8305. NewSession(ClipboardText);
  8306. }
  8307. else
  8308. {
  8309. DirView(osCurrent)->Path = ClipboardText;
  8310. }
  8311. }
  8312. }
  8313. }
  8314. //---------------------------------------------------------------------------
  8315. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8316. {
  8317. // TBD
  8318. }
  8319. //---------------------------------------------------------------------------
  8320. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8321. {
  8322. TCustomDirView * ADirView = DirView(Side);
  8323. ADirView->SelectAll(Mode);
  8324. ADirView->SetFocus();
  8325. }
  8326. //---------------------------------------------------------------------------
  8327. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8328. {
  8329. TCustomDirView * ADirView = DirView(Side);
  8330. TFileFilter Filter;
  8331. DefaultFileFilter(Filter);
  8332. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8333. {
  8334. Configuration->Usage->Inc(L"MaskSelections");
  8335. ADirView->SelectFiles(Filter, Select);
  8336. ADirView->SetFocus();
  8337. }
  8338. }
  8339. //---------------------------------------------------------------------------
  8340. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8341. {
  8342. TCustomDirView * ADirView = DirView(Side);
  8343. ADirView->RestoreSelectedNames();
  8344. ADirView->SetFocus();
  8345. }
  8346. //---------------------------------------------------------------------------
  8347. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8348. {
  8349. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8350. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8351. {
  8352. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8353. UnicodeString Ext;
  8354. if (!IsSideLocalBrowser(osCurrent))
  8355. {
  8356. Ext = UnixExtractFileExt(FileName);
  8357. }
  8358. else
  8359. {
  8360. Ext = ExtractFileExt(FileName);
  8361. }
  8362. if (Ext.IsEmpty())
  8363. {
  8364. Ext = L".";
  8365. }
  8366. TFileFilter Filter;
  8367. Filter.Masks = FORMAT(L"*%s", (Ext));
  8368. Filter.Directories = false;
  8369. CurrentDirView->SelectFiles(Filter, Select);
  8370. }
  8371. }
  8372. //---------------------------------------------------------------------------
  8373. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8374. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8375. {
  8376. UnicodeString Result;
  8377. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8378. {
  8379. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8380. }
  8381. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8382. {
  8383. // noop
  8384. }
  8385. else
  8386. {
  8387. Result =
  8388. FMTLOAD(FILE_INFO_FORMAT,
  8389. (FormatBytes(FileInfo.SelectedSize),
  8390. FormatBytes(FileInfo.FilesSize),
  8391. FormatNumber(FileInfo.SelectedCount),
  8392. FormatNumber(FileInfo.FilesCount)));
  8393. }
  8394. return Result;
  8395. }
  8396. //---------------------------------------------------------------------------
  8397. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8398. TTBXStatusPanel * Panel, TOperationSide Side)
  8399. {
  8400. if (Panel->Index == 1)
  8401. {
  8402. ToggleShowHiddenFiles();
  8403. }
  8404. else if (Panel->Index == 2)
  8405. {
  8406. Filter(Side);
  8407. }
  8408. }
  8409. //---------------------------------------------------------------------------
  8410. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8411. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8412. {
  8413. DebugAssert(!StatusBar->SimplePanel);
  8414. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8415. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8416. }
  8417. //---------------------------------------------------------------------------
  8418. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8419. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8420. {
  8421. DebugAssert(StatusBar->Panels->Count >= 3);
  8422. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8423. if (FileInfo.HiddenCount > 0)
  8424. {
  8425. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8426. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8427. }
  8428. else
  8429. {
  8430. HiddenFilesPanel->ViewPriority = 0;
  8431. // not really necessary, just to cleanup no-longer-valid data
  8432. HiddenFilesPanel->Caption = L"";
  8433. }
  8434. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8435. if (FileInfo.FilteredCount > 0)
  8436. {
  8437. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8438. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8439. }
  8440. else
  8441. {
  8442. FilteredFilesPanel->ViewPriority = 0;
  8443. // not really necessary, just to cleanup no-longer-valid data
  8444. FilteredFilesPanel->Caption = L"";
  8445. }
  8446. }
  8447. //---------------------------------------------------------------------------
  8448. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8449. TObject * /*Sender*/)
  8450. {
  8451. if (DirView(osOther)->Enabled)
  8452. {
  8453. DirView(osOther)->SetFocus();
  8454. }
  8455. }
  8456. //---------------------------------------------------------------------------
  8457. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8458. {
  8459. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8460. switch (Config.Show)
  8461. {
  8462. case qvShow:
  8463. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8464. {
  8465. Config.Show = qvHide;
  8466. }
  8467. else
  8468. {
  8469. Config.Show = Config.LastHideShow;
  8470. }
  8471. break;
  8472. case qvHideWhenEmpty:
  8473. if (ComponentVisible[fcQueueView])
  8474. {
  8475. Config.Show = qvHide;
  8476. }
  8477. else
  8478. {
  8479. Config.LastHideShow = Config.Show;
  8480. Config.Show = qvShow;
  8481. }
  8482. break;
  8483. case qvHide:
  8484. Config.LastHideShow = Config.Show;
  8485. Config.Show = qvShow;
  8486. break;
  8487. }
  8488. WinConfiguration->QueueView = Config;
  8489. }
  8490. //---------------------------------------------------------------------------
  8491. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8492. {
  8493. UnicodeString Result;
  8494. if (Terminal != NULL)
  8495. {
  8496. switch (WinConfiguration->PathInCaption)
  8497. {
  8498. case picShort:
  8499. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8500. break;
  8501. case picFull:
  8502. Result = Terminal->CurrentDirectory;
  8503. break;
  8504. }
  8505. }
  8506. return Result;
  8507. }
  8508. //---------------------------------------------------------------------------
  8509. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8510. {
  8511. TColor Result = GetWindowColor(FSessionColor);
  8512. return Result;
  8513. }
  8514. //---------------------------------------------------------------------------
  8515. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8516. {
  8517. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8518. return Result;
  8519. }
  8520. //---------------------------------------------------------------------------
  8521. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8522. {
  8523. if (!FSessionChanging)
  8524. {
  8525. TTerminalManager::Instance()->UpdateAppTitle();
  8526. }
  8527. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8528. // background to gray on Windows 7 (works on Windows 10).
  8529. // See also EnableControl
  8530. if (Showing)
  8531. {
  8532. if (ManagedSession != NULL)
  8533. {
  8534. // Update path when it changes
  8535. if ((SessionsPageControl->ActivePage != NULL) &&
  8536. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8537. {
  8538. UpdateSessionTab(SessionsPageControl->ActivePage);
  8539. }
  8540. }
  8541. NonVisualDataModule->ReconnectSessionAction->Update();
  8542. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8543. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8544. CenterReconnectToolbar();
  8545. bool HasTerminal = HasActiveTerminal();
  8546. RemoteDirView->Enabled = HasTerminal;
  8547. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8548. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8549. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8550. RemoteDriveView->Color = RemoteDirView->Color;
  8551. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8552. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8553. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8554. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8555. QueueFileList->Enabled = QueueView3->Enabled;
  8556. QueueFileList->Color = QueueView3->Color;
  8557. QueueFileList->Font->Color = QueueView3->Font->Color;
  8558. QueueLabelUpdateStatus();
  8559. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8560. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8561. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8562. {
  8563. UpdateDarkMode();
  8564. }
  8565. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8566. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8567. }
  8568. }
  8569. //---------------------------------------------------------------------------
  8570. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8571. {
  8572. UpdateControls();
  8573. }
  8574. //---------------------------------------------------------------------------
  8575. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8576. TObject * Sender)
  8577. {
  8578. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8579. DebugAssert(DirView != NULL);
  8580. DoDirViewLoaded(DirView);
  8581. TransferPresetAutoSelect();
  8582. }
  8583. //---------------------------------------------------------------------------
  8584. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8585. {
  8586. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8587. // first run after installation
  8588. if (double(Updates.LastCheck) == 0)
  8589. {
  8590. AppLog(L"First run, scheduling updates check for the next run");
  8591. // make sure next time there will be an update (if enabled)
  8592. Updates.LastCheck = TDateTime(1);
  8593. WinConfiguration->Updates = Updates;
  8594. }
  8595. else
  8596. {
  8597. TDateTime Period;
  8598. if (IsUWP() && !IsOfficialPackage())
  8599. {
  8600. Period = DefaultUpdatesPeriod;
  8601. AppLog(L"Thirdparty UWP package, using default updates check period");
  8602. }
  8603. else
  8604. {
  8605. Period = Updates.Period;
  8606. }
  8607. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8608. if (double(Period) > 0)
  8609. {
  8610. TDateTime Interval = Now() - Updates.LastCheck;
  8611. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8612. if (Interval >= Period)
  8613. {
  8614. StartUpdateThread(UpdatesChecked);
  8615. }
  8616. }
  8617. }
  8618. }
  8619. //---------------------------------------------------------------------------
  8620. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8621. {
  8622. AppLog(L"Updates check done");
  8623. UnicodeString Message;
  8624. bool New;
  8625. TQueryType Type;
  8626. GetUpdatesMessage(Message, New, Type, false);
  8627. if (!Message.IsEmpty())
  8628. {
  8629. AppLogFmt(L"Updates check message: %s", (Message));
  8630. if (New)
  8631. {
  8632. AppLog(L"New version detected");
  8633. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8634. }
  8635. if (!New && (Type != qtWarning))
  8636. {
  8637. AppLog(L"Posting non-critical updates note");
  8638. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8639. }
  8640. else
  8641. {
  8642. AppLog(L"Posting new version or critical message notification");
  8643. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8644. }
  8645. if (New)
  8646. {
  8647. Configuration->Usage->Inc(L"UpdateNotifications");
  8648. }
  8649. }
  8650. }
  8651. //---------------------------------------------------------------------------
  8652. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8653. {
  8654. RestoreApp();
  8655. bool CanDisplay = !NonVisualDataModule->Busy;
  8656. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8657. {
  8658. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8659. }
  8660. if (CanDisplay)
  8661. {
  8662. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8663. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8664. NonVisualDataModule->StartBusy();
  8665. try
  8666. {
  8667. CheckForUpdates(true);
  8668. }
  8669. __finally
  8670. {
  8671. NonVisualDataModule->EndBusy();
  8672. }
  8673. }
  8674. else
  8675. {
  8676. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8677. }
  8678. }
  8679. //---------------------------------------------------------------------------
  8680. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8681. TCopyParamRuleData & Data)
  8682. {
  8683. DebugAssert(Terminal != NULL);
  8684. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8685. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8686. Data.RemoteDirectory = RemoteDirView->PathName;
  8687. }
  8688. //---------------------------------------------------------------------------
  8689. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8690. {
  8691. // Terminal can be null when we are changing local directory implicitly,
  8692. // because it has been deleted, while no session is connected
  8693. // (Login dialog is open)
  8694. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8695. {
  8696. DebugAssert(!IsLocalBrowserMode());
  8697. TCopyParamRuleData Data;
  8698. GetTransferPresetAutoSelectData(Data);
  8699. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8700. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8701. if (CopyParamIndex < 0)
  8702. {
  8703. // there is no preset that matches autoselection
  8704. // set preset that we consider "default"
  8705. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8706. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8707. }
  8708. else
  8709. {
  8710. // there is preset matching autoselection
  8711. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8712. if (CopyParamName == FCopyParamAutoSelected)
  8713. {
  8714. // autoselected the same preset as the last time
  8715. // make no change (i.e. preserve custom user preset, if any)
  8716. }
  8717. else
  8718. {
  8719. // autoselected the different preset then the last time (or there
  8720. // was default preset set)
  8721. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8722. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8723. }
  8724. }
  8725. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8726. {
  8727. if (CopyParamIndex >= 0)
  8728. {
  8729. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8730. }
  8731. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8732. try
  8733. {
  8734. int Fmt =
  8735. (CopyParamIndex < 0) ?
  8736. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8737. COPY_PARAM_AUTOSELECTED;
  8738. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8739. Data->Message = MainInstructions(Message);
  8740. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8741. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8742. CopyParamAttrs);
  8743. if (CopyParamIndex >= 0)
  8744. {
  8745. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8746. Info = FORMAT(L"%s\n \n%s", (Info,
  8747. FMTLOAD(COPY_PARAM_RULE,
  8748. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8749. }
  8750. Data->Message += L"\n\n" + Info;
  8751. if (WinConfiguration->CopyParamAutoSelectNotice)
  8752. {
  8753. TransferPresetNoteMessage(Data, true);
  8754. }
  8755. else
  8756. {
  8757. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8758. Data = NULL; // ownership passed
  8759. }
  8760. }
  8761. __finally
  8762. {
  8763. delete Data;
  8764. }
  8765. }
  8766. }
  8767. }
  8768. //---------------------------------------------------------------------------
  8769. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8770. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8771. {
  8772. DebugAssert(NoteData != NULL);
  8773. DebugAssert(!IsLocalBrowserMode());
  8774. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8775. TQueryButtonAlias Aliases[1];
  8776. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8777. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8778. Params.Aliases = Aliases;
  8779. Params.AliasesCount = LENOF(Aliases);
  8780. unsigned int Result =
  8781. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8782. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8783. switch (Result)
  8784. {
  8785. case qaNeverAskAgain:
  8786. DebugAssert(AllowNeverAskAgain);
  8787. WinConfiguration->CopyParamAutoSelectNotice = false;
  8788. break;
  8789. case qaIgnore:
  8790. PreferencesDialog(pmPresets);
  8791. break;
  8792. }
  8793. }
  8794. //---------------------------------------------------------------------------
  8795. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8796. {
  8797. // as of now useless, as this is used for notes only, never for balloons, ...
  8798. RestoreApp();
  8799. // .. and we should never be busy here
  8800. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8801. {
  8802. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8803. }
  8804. }
  8805. //---------------------------------------------------------------------------
  8806. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8807. TPreferencesMode APreferencesMode)
  8808. {
  8809. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8810. TCopyParamRuleData Data;
  8811. if (Terminal != NULL)
  8812. {
  8813. PreferencesData.reset(new TPreferencesDialogData());
  8814. GetTransferPresetAutoSelectData(Data);
  8815. PreferencesData->CopyParamRuleData = &Data;
  8816. }
  8817. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8818. }
  8819. //---------------------------------------------------------------------------
  8820. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8821. const TCustomCommandType & Command)
  8822. {
  8823. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8824. {
  8825. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8826. }
  8827. }
  8828. //---------------------------------------------------------------------------
  8829. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8830. {
  8831. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8832. TCustomCommandType Command;
  8833. // make sure we use local custom command when remote are not supported
  8834. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8835. {
  8836. Command = FLastCustomCommand;
  8837. }
  8838. else
  8839. {
  8840. Command.Params = Command.Params | ccLocal;
  8841. }
  8842. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8843. FEditingFocusedAdHocCommand = OnFocused;
  8844. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8845. int Options =
  8846. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8847. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8848. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8849. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8850. {
  8851. FLastCustomCommand = Command;
  8852. UpdateCustomCommandsToolbar();
  8853. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8854. }
  8855. }
  8856. //---------------------------------------------------------------------------
  8857. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8858. {
  8859. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8860. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8861. DebugAssert(State > 0);
  8862. if (State <= 0)
  8863. {
  8864. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8865. }
  8866. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8867. }
  8868. //---------------------------------------------------------------------------
  8869. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8870. TCustomCommandType & Command, int & State)
  8871. {
  8872. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8873. if (Result)
  8874. {
  8875. Command = FLastCustomCommand;
  8876. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8877. }
  8878. return Result;
  8879. }
  8880. //---------------------------------------------------------------------------
  8881. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8882. {
  8883. ResetIncrementalSearch();
  8884. }
  8885. //---------------------------------------------------------------------------
  8886. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8887. {
  8888. DebugAssert(ComponentVisible[Component]);
  8889. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8890. }
  8891. //---------------------------------------------------------------------------
  8892. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8893. {
  8894. // when queue panel is resized here directly, the status bar is stretched
  8895. // over whole space the panel occupied
  8896. PostComponentHide(fcQueueView);
  8897. }
  8898. //---------------------------------------------------------------------------
  8899. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8900. {
  8901. ComponentVisible[fcQueueFileList] = false;
  8902. }
  8903. //---------------------------------------------------------------------------
  8904. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8905. {
  8906. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8907. RefreshColorMode();
  8908. ResetSysDarkTheme();
  8909. ConfigurationChanged();
  8910. ConfigureInterface();
  8911. // Should be called for all controls
  8912. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8913. }
  8914. //---------------------------------------------------------------------------
  8915. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8916. {
  8917. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8918. if (!FInvalid &&
  8919. (Message.LParam != 0) &&
  8920. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8921. {
  8922. ThemeChanged();
  8923. }
  8924. TForm::Dispatch(&Message);
  8925. }
  8926. //---------------------------------------------------------------------------
  8927. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8928. {
  8929. TMessage * M = static_cast<TMessage*>(Message);
  8930. switch (M->Msg)
  8931. {
  8932. case CM_DIALOGCHAR:
  8933. CMDialogChar(*M);
  8934. break;
  8935. case WM_APPCOMMAND:
  8936. WMAppCommand(*M);
  8937. break;
  8938. case WM_SYSCOMMAND:
  8939. WMSysCommand(*M);
  8940. break;
  8941. case WM_QUERYENDSESSION:
  8942. WMQueryEndSession(*M);
  8943. break;
  8944. case WM_ENDSESSION:
  8945. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8946. break;
  8947. #ifdef _DEBUG
  8948. case WM_WINDOWPOSCHANGED:
  8949. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8950. break;
  8951. case CM_CUSTOMSTYLECHANGED:
  8952. TForm::Dispatch(Message);
  8953. break;
  8954. #endif
  8955. case WM_COMPONENT_HIDE:
  8956. {
  8957. Byte Component = static_cast<Byte>(M->WParam);
  8958. // sanity check
  8959. if (ComponentVisible[Component])
  8960. {
  8961. // special treatment
  8962. if (Component == fcQueueView)
  8963. {
  8964. ToggleQueueVisibility();
  8965. DebugAssert(!ComponentVisible[fcQueueView]);
  8966. }
  8967. else
  8968. {
  8969. ComponentVisible[Component] = false;
  8970. }
  8971. }
  8972. }
  8973. break;
  8974. case WM_COPYDATA:
  8975. WMCopyData(*M);
  8976. break;
  8977. case WM_MANAGES_CAPTION:
  8978. // caption managed in TTerminalManager::UpdateAppTitle()
  8979. M->Result = 1;
  8980. break;
  8981. case WM_WANTS_MOUSEWHEEL:
  8982. M->Result = 1;
  8983. break;
  8984. case CM_SHOWINGCHANGED:
  8985. CMShowingChanged(*M);
  8986. break;
  8987. case WM_CLOSE:
  8988. WMClose(*M);
  8989. break;
  8990. case WM_DPICHANGED:
  8991. WMDpiChanged(*M);
  8992. break;
  8993. case CM_DPICHANGED:
  8994. CMDpiChanged(*M);
  8995. break;
  8996. case WM_SETTINGCHANGE:
  8997. WMSettingChange(*M);
  8998. break;
  8999. case CM_DIALOGKEY:
  9000. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9001. break;
  9002. default:
  9003. TForm::Dispatch(Message);
  9004. break;
  9005. }
  9006. }
  9007. //---------------------------------------------------------------------------
  9008. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9009. {
  9010. // noop
  9011. }
  9012. //---------------------------------------------------------------------------
  9013. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9014. {
  9015. TForm::Dispatch(&Message);
  9016. FSessionColors->Clear();
  9017. AddFixedSessionImages();
  9018. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9019. UpdateImages();
  9020. }
  9021. //---------------------------------------------------------------------------
  9022. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9023. {
  9024. TForm::Dispatch(&Message);
  9025. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9026. UpdatePixelsPerInchMainWindowCounter();
  9027. }
  9028. //---------------------------------------------------------------------------
  9029. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9030. {
  9031. // Cannot close window while we are busy.
  9032. // We cannot test this in FormCloseQuery as that is called also from
  9033. // Close(), which is called by CloseApplicationAction. So we can be busy
  9034. // there already even, when it is legitimate to close the application.
  9035. // Possibly a better place to handle this would be WMSysCommand.
  9036. if (NonVisualDataModule->Busy)
  9037. {
  9038. Message.Result = 1;
  9039. }
  9040. else
  9041. {
  9042. TForm::Dispatch(&Message);
  9043. }
  9044. }
  9045. //---------------------------------------------------------------------------
  9046. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9047. {
  9048. if (!FShowingChanged)
  9049. {
  9050. AddStartupSequence(L"D");
  9051. FShowingChanged = true;
  9052. }
  9053. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9054. bool NoSession = (ManagedSession == NULL);
  9055. // This calls TCustomScpExplorerForm::DoShow
  9056. TForm::Dispatch(&Message);
  9057. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9058. if (Showing && !FStarted)
  9059. {
  9060. FStarted = true;
  9061. InterfaceStarted();
  9062. }
  9063. if (Showing && NoSession)
  9064. {
  9065. // When we are starting minimized (i.e. from an installer),
  9066. // postpone showing Login dialog until we get restored.
  9067. // Otherwise the Login dialog (and Authentication window) show restored
  9068. // over invisible (minimized) main window.
  9069. if (WindowState == wsMinimized)
  9070. {
  9071. FNeedSession = true;
  9072. }
  9073. else
  9074. {
  9075. // This happens before application ever goes idle, so the toolbars would
  9076. // stay enabled (initial state) until the Login dialog is dismissed.
  9077. UpdateActions();
  9078. NonVisualDataModule->StartBusy();
  9079. try
  9080. {
  9081. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9082. // otherwise the dialog does not receive focus.
  9083. // With Commander interface the ProcessMessages is called already
  9084. // by TDriveView, but with Explorer interface, we need to call it explicily
  9085. Application->ProcessMessages();
  9086. // do not reload sessions, they have been loaded just now (optimization)
  9087. NeedSession(true);
  9088. }
  9089. __finally
  9090. {
  9091. NonVisualDataModule->EndBusy();
  9092. }
  9093. }
  9094. }
  9095. }
  9096. //---------------------------------------------------------------------------
  9097. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9098. {
  9099. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9100. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9101. }
  9102. //---------------------------------------------------------------------------
  9103. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9104. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9105. int & MaxHeight)
  9106. {
  9107. // workaround for bug in TWinControl.CalcConstraints
  9108. // Check for empty rect (restore from iconic state) is done there only after
  9109. // call to AdjustClientRect, which enlarges the rect (for forms).
  9110. TRect R = GetClientRect();
  9111. // when restoring from iconic state, place no restrictions
  9112. if (IsRectEmpty(R))
  9113. {
  9114. MinWidth = 0;
  9115. MinHeight = 0;
  9116. MaxWidth = 32000;
  9117. MaxHeight = 32000;
  9118. }
  9119. }
  9120. //---------------------------------------------------------------------------
  9121. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9122. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9123. {
  9124. // terminal can be already closed (e.g. dropped connection)
  9125. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9126. {
  9127. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9128. try
  9129. {
  9130. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9131. }
  9132. catch(...)
  9133. {
  9134. if (!Terminal->Active)
  9135. {
  9136. Close = true;
  9137. }
  9138. throw;
  9139. }
  9140. }
  9141. }
  9142. //---------------------------------------------------------------------------
  9143. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9144. {
  9145. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9146. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9147. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9148. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9149. {
  9150. OnGetSpaceAvailable = GetSpaceAvailable;
  9151. }
  9152. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9153. OnGetSpaceAvailable);
  9154. }
  9155. //---------------------------------------------------------------------------
  9156. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9157. {
  9158. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9159. Terminal->FillSessionDataForCode(Data.get());
  9160. return Data.release();
  9161. }
  9162. //---------------------------------------------------------------------------
  9163. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9164. {
  9165. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9166. DoGenerateUrlDialog(Data.get(), Paths);
  9167. }
  9168. //---------------------------------------------------------------------------
  9169. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9170. {
  9171. GenerateUrl(NULL);
  9172. }
  9173. //---------------------------------------------------------------------------
  9174. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9175. {
  9176. std::unique_ptr<TStrings> Paths(new TStringList());
  9177. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9178. GenerateUrl(Paths.get());
  9179. }
  9180. //---------------------------------------------------------------------------
  9181. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9182. {
  9183. FSessionColor = value;
  9184. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9185. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9186. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9187. DebugAssert(ColorItem != NULL);
  9188. ColorItem->Color = C;
  9189. NonVisualDataModule->ColorMenuItem->Color = C;
  9190. UpdateControls();
  9191. }
  9192. //---------------------------------------------------------------------------
  9193. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9194. {
  9195. if (value != FSessionColor)
  9196. {
  9197. UpdateSessionColor(value);
  9198. // Is null when called from LastTerminalClosed
  9199. if (Terminal != NULL)
  9200. {
  9201. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9202. }
  9203. }
  9204. }
  9205. //---------------------------------------------------------------------------
  9206. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9207. {
  9208. bool Result = FNoteTimer->Enabled;
  9209. if (Result)
  9210. {
  9211. // cannot cancel note too early
  9212. bool NotEarly =
  9213. (Now() - FNoteShown >
  9214. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9215. if (Force || NotEarly)
  9216. {
  9217. FNoteTimer->Enabled = false;
  9218. FNote = L"";
  9219. SAFE_DESTROY(FNoteData);
  9220. FOnNoteClick = NULL;
  9221. FNoteHints = FNotes->Text;
  9222. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9223. UpdateStatusBar();
  9224. }
  9225. }
  9226. return Result;
  9227. }
  9228. //---------------------------------------------------------------------------
  9229. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9230. {
  9231. DebugAssert(FNoteTimer->Enabled);
  9232. CancelNote(true);
  9233. }
  9234. //---------------------------------------------------------------------------
  9235. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9236. {
  9237. FNoteHints = FNotes->Text.TrimRight();
  9238. }
  9239. //---------------------------------------------------------------------------
  9240. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9241. {
  9242. int P = Note.Pos(L"\n");
  9243. if (P > 0)
  9244. {
  9245. Note.SetLength(P - 1);
  9246. }
  9247. FNotes->Add(FORMAT(L"[%s] %s",
  9248. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9249. while (FNotes->Count > 10)
  9250. {
  9251. FNotes->Delete(0);
  9252. }
  9253. if (UpdateNow)
  9254. {
  9255. UpdateNoteHints();
  9256. UpdateStatusBar();
  9257. }
  9258. }
  9259. //---------------------------------------------------------------------------
  9260. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9261. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9262. {
  9263. int P = Note.Pos(L"\n");
  9264. if (P > 0)
  9265. {
  9266. Note.SetLength(P - 1);
  9267. }
  9268. UpdateNoteHints();
  9269. FNote = Note;
  9270. SAFE_DESTROY(FNoteData);
  9271. FNoteData = NoteData;
  9272. FOnNoteClick = OnNoteClick;
  9273. AddNote(Note, false);
  9274. UpdateStatusBar();
  9275. FNoteShown = Now();
  9276. FNoteTimer->Enabled = false;
  9277. if (Seconds == 0)
  9278. {
  9279. Seconds = WinConfiguration->NotificationsTimeout;
  9280. }
  9281. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9282. FNoteTimer->Enabled = true;
  9283. }
  9284. //---------------------------------------------------------------------------
  9285. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9286. {
  9287. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9288. }
  9289. //---------------------------------------------------------------------------
  9290. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9291. {
  9292. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9293. {
  9294. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9295. }
  9296. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9297. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9298. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9299. 0, NULL, NULL);
  9300. }
  9301. //---------------------------------------------------------------------------
  9302. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9303. {
  9304. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9305. UnicodeString Note =
  9306. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9307. PostNote(Note, 0, NULL, NULL);
  9308. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9309. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9310. }
  9311. //---------------------------------------------------------------------------
  9312. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9313. {
  9314. WinConfiguration->FormatSizeBytes = Style;
  9315. }
  9316. //---------------------------------------------------------------------------
  9317. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9318. {
  9319. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9320. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9321. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9322. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9323. }
  9324. //---------------------------------------------------------------------------
  9325. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9326. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9327. {
  9328. if (Panel->Index == 0)
  9329. {
  9330. if (FOnNoteClick != NULL)
  9331. {
  9332. // prevent the user data from being freed by possible call
  9333. // to CancelNote or PostNote during call to OnNoteClick
  9334. std::unique_ptr<TObject> NoteData(FNoteData);
  9335. TNotifyEvent OnNoteClick = FOnNoteClick;
  9336. FNoteData = NULL;
  9337. // need to cancel the note as we are going to delete its user data
  9338. CancelNote(true);
  9339. OnNoteClick(NoteData.get());
  9340. }
  9341. }
  9342. if (HasActiveTerminal() &&
  9343. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9344. {
  9345. FileSystemInfo();
  9346. }
  9347. }
  9348. //---------------------------------------------------------------------------
  9349. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9350. {
  9351. // workaround:
  9352. // 1) for unknown reason, disabling window, while minimized,
  9353. // prevents it from restoring, even if it was enabled again meanwhile
  9354. // 2) when disabling the main window, while another has focus
  9355. // minimize is no longer possible
  9356. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9357. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9358. // Shouldn't we use IsApplicationMinimized() here?
  9359. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9360. {
  9361. Enabled = false;
  9362. }
  9363. FLockLevel++;
  9364. }
  9365. //---------------------------------------------------------------------------
  9366. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9367. {
  9368. DebugAssert(FLockLevel > 0);
  9369. FLockLevel--;
  9370. if (FLockLevel == 0)
  9371. {
  9372. DebugAssert(FLockSuspendLevel == 0);
  9373. Enabled = true;
  9374. }
  9375. }
  9376. //---------------------------------------------------------------------------
  9377. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9378. {
  9379. // We need to make sure that window is enabled when the last modal window closes
  9380. // otherwise focus is not restored correctly.
  9381. // So we re-enable the window when modal window opens and
  9382. // disable it back after is closes
  9383. if (FLockLevel > 0)
  9384. {
  9385. // while we have nesting counter, we know that we never be called
  9386. // recursivelly as Application->OnModalBegin is called only
  9387. // for the top-level modal window
  9388. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9389. {
  9390. // won't be disabled when conditions in LockWindow() were not satisfied
  9391. FDisabledOnLockSuspend = !Enabled;
  9392. // When minimized to tray (or actually when set to SW_HIDE),
  9393. // setting Enabled makes the window focusable even when there's
  9394. // modal window over it
  9395. if (!FTrayIcon->Visible)
  9396. {
  9397. Enabled = true;
  9398. }
  9399. }
  9400. FLockSuspendLevel++;
  9401. }
  9402. }
  9403. //---------------------------------------------------------------------------
  9404. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9405. {
  9406. if (FLockSuspendLevel > 0)
  9407. {
  9408. DebugAssert(FLockLevel > 0);
  9409. FLockSuspendLevel--;
  9410. // see comment in SuspendWindowLock
  9411. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9412. {
  9413. // Note that window can be enabled here, when we were minized to tray when
  9414. // was SuspendWindowLock() called.
  9415. // We should possibly do the same check as in LockWindow(),
  9416. // if it is ever possible that the consitions change between
  9417. // SuspendWindowLock() and ResumeWindowLock()
  9418. Enabled = !FDisabledOnLockSuspend;
  9419. }
  9420. }
  9421. }
  9422. //---------------------------------------------------------------------------
  9423. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9424. {
  9425. TTBXComboBoxItem * RemotePathComboBox =
  9426. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9427. if (!TextOnly)
  9428. {
  9429. TStrings * Items = RemotePathComboBox->Strings;
  9430. Items->BeginUpdate();
  9431. try
  9432. {
  9433. Items->Clear();
  9434. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9435. {
  9436. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9437. while (!IsUnixRootPath(APath))
  9438. {
  9439. int P = APath.LastDelimiter(L'/');
  9440. DebugAssert(P >= 0);
  9441. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9442. APath.SetLength(P - 1);
  9443. }
  9444. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9445. }
  9446. }
  9447. __finally
  9448. {
  9449. Items->EndUpdate();
  9450. }
  9451. }
  9452. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9453. // Setting ItemIndex to -1 does not reset its text
  9454. if (RemotePathComboBox->Strings->Count == 0)
  9455. {
  9456. RemotePathComboBox->Text = L"";
  9457. }
  9458. }
  9459. //---------------------------------------------------------------------------
  9460. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9461. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9462. {
  9463. if (AIndex < 0)
  9464. {
  9465. AIndex = Sender->ItemIndex;
  9466. }
  9467. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9468. }
  9469. //---------------------------------------------------------------------------
  9470. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9471. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9472. int & ImageIndex)
  9473. {
  9474. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9475. }
  9476. //---------------------------------------------------------------------------
  9477. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9478. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9479. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9480. {
  9481. if (!IsLocalBrowserMode())
  9482. {
  9483. ARect.Left += (10 * AIndex);
  9484. }
  9485. }
  9486. //---------------------------------------------------------------------------
  9487. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9488. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9489. {
  9490. AWidth += (10 * AIndex);
  9491. }
  9492. //---------------------------------------------------------------------------
  9493. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9494. {
  9495. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9496. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9497. int Index = RemotePathComboBox->ItemIndex;
  9498. while (Index < RemotePathComboBox->Strings->Count - 1)
  9499. {
  9500. APath = UnixExtractFileDir(APath);
  9501. Index++;
  9502. }
  9503. // VanDyke style paths
  9504. if (APath.IsEmpty())
  9505. {
  9506. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9507. APath = ROOTDIRECTORY;
  9508. }
  9509. if (RemoteDirView->Path != APath)
  9510. {
  9511. RemoteDirView->Path = APath;
  9512. }
  9513. }
  9514. //---------------------------------------------------------------------------
  9515. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9516. {
  9517. DoRemotePathComboBoxItemClick(Sender);
  9518. }
  9519. //---------------------------------------------------------------------------
  9520. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9521. {
  9522. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9523. DebugUsedParam(Sender);
  9524. UpdateRemotePathComboBox(true);
  9525. }
  9526. //---------------------------------------------------------------------------
  9527. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9528. {
  9529. DoRemotePathComboBoxCancel(Sender);
  9530. }
  9531. //---------------------------------------------------------------------------
  9532. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9533. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9534. {
  9535. ResetIncrementalSearch();
  9536. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9537. DebugAssert(DirView != NULL);
  9538. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9539. {
  9540. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9541. // OnEditing is called also from TCustomListView::CanEdit
  9542. if (Edit != NULL)
  9543. {
  9544. EditSelectBaseName(Edit);
  9545. }
  9546. }
  9547. }
  9548. //---------------------------------------------------------------------------
  9549. //---------------------------------------------------------------------------
  9550. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9551. {
  9552. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9553. Result->AutoDetectDnD = false;
  9554. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9555. Result->RenderDataOn = rdoEnterAndDropSync;
  9556. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9557. return Result;
  9558. }
  9559. //---------------------------------------------------------------------------
  9560. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9561. {
  9562. if (IsWin10Build(2004))
  9563. {
  9564. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9565. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9566. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9567. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9568. }
  9569. }
  9570. //---------------------------------------------------------------------------
  9571. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9572. {
  9573. TForm::CreateWnd();
  9574. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9575. UpdateDarkMode();
  9576. if (FSessionsDragDropFilesEx == NULL)
  9577. {
  9578. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9579. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9580. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9581. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9582. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9583. }
  9584. if (FQueueDragDropFilesEx == NULL)
  9585. {
  9586. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9587. // No need to set OnDragOver as we do not have any restrictions
  9588. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9589. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9590. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9591. }
  9592. }
  9593. //---------------------------------------------------------------------------
  9594. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9595. {
  9596. TForm::DestroyWnd();
  9597. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9598. FQueueDragDropFilesEx->DragDropControl = NULL;
  9599. }
  9600. //---------------------------------------------------------------------------
  9601. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9602. {
  9603. SideEnter(FCurrentSide);
  9604. FEverShown = true;
  9605. }
  9606. //---------------------------------------------------------------------------
  9607. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9608. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9609. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9610. {
  9611. if (!NonVisualDataModule->Busy)
  9612. {
  9613. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9614. Configuration->Usage->Inc(L"FileFinds");
  9615. LockWindow(true);
  9616. NonVisualDataModule->StartBusy();
  9617. WinConfiguration->LockedInterface = true;
  9618. try
  9619. {
  9620. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9621. }
  9622. __finally
  9623. {
  9624. WinConfiguration->LockedInterface = false;
  9625. NonVisualDataModule->EndBusy();
  9626. UnlockWindow();
  9627. }
  9628. }
  9629. }
  9630. //---------------------------------------------------------------------------
  9631. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9632. {
  9633. if (!NonVisualDataModule->Busy)
  9634. {
  9635. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9636. SetFocus();
  9637. RemoteDirView->Path = UnixExtractFilePath(Path);
  9638. UnicodeString FileName = UnixExtractFileName(Path);
  9639. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9640. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9641. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9642. if (Item == NULL)
  9643. {
  9644. ReloadDirectory(osRemote);
  9645. Item = RemoteDirView->FindFileItem(FileName);
  9646. }
  9647. if (Item != NULL)
  9648. {
  9649. RemoteDirView->ItemFocused = Item;
  9650. RemoteDirView->ItemFocused->MakeVisible(false);
  9651. RemoteDirView->SetFocus();
  9652. }
  9653. }
  9654. }
  9655. //---------------------------------------------------------------------------
  9656. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9657. {
  9658. bool Result = !NonVisualDataModule->Busy;
  9659. if (Result)
  9660. {
  9661. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9662. }
  9663. return Result;
  9664. }
  9665. //---------------------------------------------------------------------------
  9666. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9667. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9668. {
  9669. if (CanOperateOnFoundFiles(ATerminal))
  9670. {
  9671. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9672. FOnFileOperationFinished = OnFileOperationFinished;
  9673. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9674. }
  9675. }
  9676. //---------------------------------------------------------------------------
  9677. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9678. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9679. {
  9680. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9681. }
  9682. //---------------------------------------------------------------------------
  9683. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9684. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9685. {
  9686. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9687. }
  9688. //---------------------------------------------------------------------------
  9689. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9690. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9691. {
  9692. if (CanOperateOnFoundFiles(ATerminal))
  9693. {
  9694. for (int Index = 0; Index < FileList->Count; Index++)
  9695. {
  9696. UnicodeString FileName = FileList->Strings[Index];
  9697. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9698. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9699. OnFileOperationFinished(osRemote, FileName, true);
  9700. }
  9701. }
  9702. }
  9703. //---------------------------------------------------------------------------
  9704. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9705. {
  9706. FFileFindTerminal = Terminal;
  9707. ShowFileFindDialog(
  9708. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9709. DoDownloadFoundFiles, DoEditFoundFiles);
  9710. }
  9711. //---------------------------------------------------------------------------
  9712. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9713. {
  9714. FTaskbarList = TaskbarList;
  9715. }
  9716. //---------------------------------------------------------------------------
  9717. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9718. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9719. {
  9720. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9721. if (Index >= 0)
  9722. {
  9723. if (Button == mbLeft)
  9724. {
  9725. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9726. // If switching tab (switching session) takes long enough for user
  9727. // to actually release the button, "mouse down" is still raised,
  9728. // but we do not get "mouse up" event, so dragging is not cancelled,
  9729. // prevent that by not beginning dragging in the first place.
  9730. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9731. if (IsKeyPressed(Key))
  9732. {
  9733. // when user clicks the "+", we get mouse down only after the session
  9734. // is closed, when new session tab is already on X:Y, so dragging
  9735. // starts, prevent that
  9736. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9737. {
  9738. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9739. if (Terminal != NULL)
  9740. {
  9741. SessionsPageControl->BeginDrag(false);
  9742. }
  9743. }
  9744. }
  9745. }
  9746. else if (Button == mbMiddle)
  9747. {
  9748. // ignore middle-click for "New session tab" and the last local-local tab
  9749. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9750. if ((Session != NULL) && CanCloseSession(Session))
  9751. {
  9752. CloseSessionTab(Index);
  9753. }
  9754. }
  9755. }
  9756. }
  9757. //---------------------------------------------------------------------------
  9758. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9759. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9760. {
  9761. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9762. // do not allow dropping on the "+" tab
  9763. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9764. if ((TargetTerminal != NULL) &&
  9765. (SessionsPageControl->ActivePage->PageIndex != Index))
  9766. {
  9767. Configuration->Usage->Inc(L"SessionTabMoves");
  9768. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9769. // but we want to actually prevent that to avoid flicker
  9770. SessionsPageControl->ActivePage->PageIndex = Index;
  9771. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9772. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9773. }
  9774. }
  9775. //---------------------------------------------------------------------------
  9776. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9777. TObject * Sender, TObject * Source, int X, int Y,
  9778. TDragState /*State*/, bool & Accept)
  9779. {
  9780. Accept = (Sender == Source);
  9781. if (Accept)
  9782. {
  9783. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9784. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9785. // do not allow dragging to the "+" tab
  9786. Accept = (Terminal != NULL);
  9787. }
  9788. }
  9789. //---------------------------------------------------------------------------
  9790. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9791. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9792. {
  9793. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9794. if (Index < 0)
  9795. {
  9796. Effect = DROPEFFECT_NONE;
  9797. }
  9798. else
  9799. {
  9800. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9801. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9802. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9803. {
  9804. Effect = DROPEFFECT_NONE;
  9805. }
  9806. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9807. {
  9808. Effect = DROPEFFECT_COPY;
  9809. }
  9810. }
  9811. }
  9812. //---------------------------------------------------------------------------
  9813. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9814. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9815. {
  9816. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9817. // do not allow dropping on local-local, disconnected and the "+" tab
  9818. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9819. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9820. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9821. {
  9822. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9823. if (!IsFileControl(DropSourceControl, osRemote))
  9824. {
  9825. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9826. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9827. // Why don't we use Terminal->CurrentDirectory directly?
  9828. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9829. // do not force queue, drag drop
  9830. false, true);
  9831. }
  9832. }
  9833. }
  9834. //---------------------------------------------------------------------------
  9835. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9836. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9837. {
  9838. // Downloads are handled in RemoteFileControlDDEnd
  9839. if (!IsFileControl(DropSourceControl, osRemote))
  9840. {
  9841. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9842. Terminal->CurrentDirectory,
  9843. // force queue, drag drop
  9844. true, true);
  9845. }
  9846. }
  9847. //---------------------------------------------------------------------------
  9848. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9849. {
  9850. FShowing = false;
  9851. // Do not save empty workspace
  9852. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9853. {
  9854. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9855. UnicodeString Name = WorkspaceName();
  9856. DoSaveWorkspace(
  9857. Name, DataList.get(),
  9858. !Configuration->DisablePasswordStoring &&
  9859. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9860. WinConfiguration->LastStoredSession = Name;
  9861. }
  9862. }
  9863. //---------------------------------------------------------------------------
  9864. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9865. {
  9866. TManagedTerminal * ManagedTerminal =
  9867. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9868. if (ManagedTerminal != NULL)
  9869. {
  9870. ManagedTerminal->DirectoryLoaded = Now();
  9871. }
  9872. }
  9873. //---------------------------------------------------------------------------
  9874. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9875. TListItem * /*Item*/, bool /*Selected*/)
  9876. {
  9877. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9878. switch (DirView->LastSelectMethod)
  9879. {
  9880. case smKeyboard:
  9881. Configuration->Usage->Inc(L"KeyboardSelections");
  9882. break;
  9883. case smMouse:
  9884. Configuration->Usage->Inc(L"MouseSelections");
  9885. break;
  9886. }
  9887. }
  9888. //---------------------------------------------------------------------------
  9889. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9890. {
  9891. if (Color != 0)
  9892. {
  9893. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9894. }
  9895. else
  9896. {
  9897. return FSessionTabImageIndex;
  9898. }
  9899. }
  9900. //---------------------------------------------------------------------------
  9901. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9902. {
  9903. TPngImageCollectionItem * Item =
  9904. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9905. FSessionColors->AddPng(Item->PngImage);
  9906. return FSessionColors->Count - 1;
  9907. }
  9908. //---------------------------------------------------------------------------
  9909. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9910. {
  9911. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9912. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9913. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9914. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9915. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9916. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9917. }
  9918. //---------------------------------------------------------------------------
  9919. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9920. {
  9921. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9922. }
  9923. //---------------------------------------------------------------------------
  9924. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9925. {
  9926. switch (Side)
  9927. {
  9928. case osLocal:
  9929. return FNewLocalTabTabImageIndex;
  9930. case osRemote:
  9931. case osCurrent:
  9932. return FNewRemoteTabTabImageIndex;
  9933. default:
  9934. DebugFail();
  9935. return -1;
  9936. }
  9937. }
  9938. //---------------------------------------------------------------------------
  9939. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9940. {
  9941. for (int Index = 0; Index < Control->ControlCount; Index++)
  9942. {
  9943. TControl * AControl = Control->Controls[Index];
  9944. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9945. if (WinControl != NULL)
  9946. {
  9947. CollectItemsWithTextDisplayMode(WinControl);
  9948. }
  9949. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9950. if (Toolbar != NULL)
  9951. {
  9952. // we care for top-level items only
  9953. TTBCustomItem * Items = Toolbar->Items;
  9954. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9955. {
  9956. TTBCustomItem * Item = Items->Items[ItemIndex];
  9957. if (((Item->DisplayMode == nbdmImageAndText) ||
  9958. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9959. EligibleForImageDisplayMode(Item))
  9960. {
  9961. FItemsWithTextDisplayMode.insert(Item);
  9962. }
  9963. }
  9964. }
  9965. }
  9966. }
  9967. //---------------------------------------------------------------------------
  9968. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9969. {
  9970. return true;
  9971. }
  9972. //---------------------------------------------------------------------------
  9973. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9974. {
  9975. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9976. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9977. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9978. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9979. bool Result = true;
  9980. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9981. {
  9982. TTBCustomItem * Item = *i;
  9983. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9984. if (Label != NULL)
  9985. {
  9986. // optimization
  9987. if (Label->Visible == SelectiveToolbarText)
  9988. {
  9989. Result = false;
  9990. }
  9991. else
  9992. {
  9993. Label->Visible = SelectiveToolbarText;
  9994. }
  9995. }
  9996. else
  9997. {
  9998. // optimization
  9999. if (Item->DisplayMode == DisplayMode)
  10000. {
  10001. Result = false;
  10002. }
  10003. else
  10004. {
  10005. Item->DisplayMode = DisplayMode;
  10006. }
  10007. }
  10008. ++i;
  10009. }
  10010. UpdateNewTabTab();
  10011. return Result;
  10012. }
  10013. //---------------------------------------------------------------------------
  10014. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10015. {
  10016. DebugFail();
  10017. }
  10018. //---------------------------------------------------------------------------
  10019. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10020. {
  10021. // Among other, a lock level is non zero, while directory is being loaded,
  10022. // even when it happens as a result of dirview navigation,
  10023. // i.e. when TNonVisualDataModule is NOT FBusy.
  10024. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10025. // Among other this prevents a panel auto update to occur while
  10026. // directory is changing.
  10027. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10028. }
  10029. //---------------------------------------------------------------------------
  10030. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10031. {
  10032. // While the window is disabled, we still seem to process menu shortcuts at least,
  10033. // so stop it at least here.
  10034. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10035. bool Result;
  10036. if (Allowed == aaUpdate)
  10037. {
  10038. Result = true;
  10039. }
  10040. else
  10041. {
  10042. Result = !NonVisualDataModule->Busy;
  10043. if (Result && (Allowed == aaShortCut))
  10044. {
  10045. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10046. Action->Update();
  10047. Result = Action->Enabled;
  10048. }
  10049. }
  10050. return Result;
  10051. }
  10052. //---------------------------------------------------------------------------
  10053. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10054. {
  10055. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10056. }
  10057. //---------------------------------------------------------------------------
  10058. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10059. {
  10060. // This is somewhat redundant to LockWindow() call from
  10061. // TTerminalManager::TerminalReadDirectoryProgress.
  10062. // But disabling window is known not to block keyboard shorcuts
  10063. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10064. if (Busy > 0)
  10065. {
  10066. if (NonVisualDataModule->Busy)
  10067. {
  10068. State = false;
  10069. }
  10070. else
  10071. {
  10072. NonVisualDataModule->StartBusy();
  10073. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10074. LockWindow();
  10075. }
  10076. }
  10077. else if (Busy < 0)
  10078. {
  10079. UnlockWindow();
  10080. NonVisualDataModule->EndBusy();
  10081. }
  10082. else
  10083. {
  10084. State = NonVisualDataModule->Busy;
  10085. }
  10086. }
  10087. //---------------------------------------------------------------------------
  10088. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10089. {
  10090. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10091. if (Index >= 0)
  10092. {
  10093. TPopupMenu * PopupMenu = NULL;
  10094. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10095. if (Session != NULL)
  10096. {
  10097. SessionsPageControl->ActivePageIndex = Index;
  10098. if (DebugAlwaysTrue(SessionTabSwitched()))
  10099. {
  10100. // copied from TControl.WMContextMenu
  10101. SendCancelMode(SessionsPageControl);
  10102. // explicit popup instead of using PopupMenu property
  10103. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10104. // while connecting yet not-connected session and hence
  10105. // allowing an access to commands over not-completelly connected session
  10106. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10107. }
  10108. }
  10109. else
  10110. {
  10111. if (SupportsLocalBrowser())
  10112. {
  10113. PopupMenu = NonVisualDataModule->NewTabPopup;
  10114. }
  10115. }
  10116. if (PopupMenu != NULL)
  10117. {
  10118. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10119. PopupMenu->PopupComponent = SessionsPageControl;
  10120. PopupMenu->Popup(Point.x, Point.y);
  10121. }
  10122. }
  10123. Handled = true;
  10124. }
  10125. //---------------------------------------------------------------------------
  10126. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10127. {
  10128. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10129. }
  10130. //---------------------------------------------------------------------------
  10131. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10132. {
  10133. Configuration->Usage->Inc(L"PasswordChanges");
  10134. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10135. ChangePasswordData->DisableAuthentationsExceptPassword();
  10136. ChangePasswordData->ChangePassword = true;
  10137. std::unique_ptr<TTerminal> ChangePasswordSession(
  10138. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10139. ChangePasswordSession->AutoReadDirectory = false;
  10140. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10141. {
  10142. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10143. }
  10144. else
  10145. {
  10146. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10147. }
  10148. }
  10149. //---------------------------------------------------------------------------
  10150. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10151. {
  10152. // No nice way to assert SSH2
  10153. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10154. }
  10155. //---------------------------------------------------------------------------
  10156. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10157. {
  10158. TOperationVisualizer OperationVisualizer;
  10159. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10160. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10161. }
  10162. //---------------------------------------------------------------------------
  10163. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10164. {
  10165. TForm::ChangeScale(M, D);
  10166. int APixelsPerInch = GetControlPixelsPerInch(this);
  10167. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10168. try
  10169. {
  10170. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10171. }
  10172. __finally
  10173. {
  10174. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10175. }
  10176. }
  10177. //---------------------------------------------------------------------------
  10178. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10179. {
  10180. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10181. }
  10182. //---------------------------------------------------------------------
  10183. class TPublicControl : public TControl
  10184. {
  10185. friend class TCustomScpExplorerForm;
  10186. };
  10187. //---------------------------------------------------------------------------
  10188. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10189. {
  10190. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10191. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10192. }
  10193. //---------------------------------------------------------------------------
  10194. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10195. {
  10196. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10197. OpenBookmark(Side, Bookmark);
  10198. }
  10199. //---------------------------------------------------------------------------
  10200. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10201. {
  10202. DoBookmarkClick(osLocal, Sender);
  10203. }
  10204. //---------------------------------------------------------------------------
  10205. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10206. {
  10207. DoBookmarkClick(osRemote, Sender);
  10208. }
  10209. //---------------------------------------------------------------------------
  10210. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10211. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10212. {
  10213. if (BookmarkList != NULL)
  10214. {
  10215. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10216. if (IsLocalBrowserMode())
  10217. {
  10218. Side = osLocal;
  10219. }
  10220. if (!WinConfiguration->UseLocationProfiles)
  10221. {
  10222. std::unique_ptr<TStringList> Directories(new TStringList());
  10223. Directories->CaseSensitive = (Side == osRemote);
  10224. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10225. {
  10226. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10227. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10228. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10229. {
  10230. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10231. Item->Caption = EscapeHotkey(Directory);
  10232. Item->ShortCut = Bookmark->ShortCut;
  10233. Item->OnClick = OnBookmarkClick;
  10234. Item->Tag = reinterpret_cast<int>(Bookmark);
  10235. Directories->Add(Directory);
  10236. Menu->Add(Item.release());
  10237. }
  10238. }
  10239. }
  10240. else
  10241. {
  10242. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10243. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10244. {
  10245. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10246. if (!Bookmark->Node.IsEmpty())
  10247. {
  10248. Folders->Add(Bookmark->Node);
  10249. }
  10250. }
  10251. for (int Index = 0; Index < Folders->Count; Index++)
  10252. {
  10253. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10254. Item->Caption = Folders->Strings[Index];
  10255. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10256. Folders->Objects[Index] = Item.get();
  10257. Menu->Add(Item.release());
  10258. }
  10259. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10260. {
  10261. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10262. TTBCustomItem * Parent;
  10263. if (!Bookmark->Node.IsEmpty())
  10264. {
  10265. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10266. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10267. }
  10268. else
  10269. {
  10270. Parent = Menu;
  10271. }
  10272. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10273. Item->Caption = Bookmark->Name;
  10274. Item->ShortCut = Bookmark->ShortCut;
  10275. Item->OnClick = OnBookmarkClick;
  10276. Item->Tag = reinterpret_cast<int>(Bookmark);
  10277. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10278. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10279. {
  10280. // No hint for location profiles that are actually mere "bookmarks"
  10281. }
  10282. else
  10283. {
  10284. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10285. UnicodeString LongHint;
  10286. if (!Bookmark->Local.IsEmpty())
  10287. {
  10288. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10289. }
  10290. if (!Bookmark->Remote.IsEmpty())
  10291. {
  10292. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10293. }
  10294. AddToList(Hint, LongHint, L"|");
  10295. Item->Hint = Hint;
  10296. }
  10297. Parent->Add(Item.release());
  10298. }
  10299. }
  10300. AddMenuSeparator(Menu);
  10301. }
  10302. }
  10303. //---------------------------------------------------------------------------
  10304. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10305. {
  10306. Menu->Clear();
  10307. std::unique_ptr<TTBCustomItem> Item;
  10308. Item.reset(new TTBXItem(Owner));
  10309. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10310. Item->Options = Item->Options << tboDefault;
  10311. Menu->Add(Item.release());
  10312. AddMenuSeparator(Menu);
  10313. if (Terminal != NULL)
  10314. {
  10315. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10316. }
  10317. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10318. Item.reset(new TTBXItem(Owner));
  10319. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10320. Menu->Add(Item.release());
  10321. }
  10322. //---------------------------------------------------------------------------
  10323. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10324. {
  10325. bool Result = true;
  10326. bool Remote = !IsSideLocalBrowser(Side);
  10327. try
  10328. {
  10329. UnicodeString APath = Path;
  10330. if (Remote)
  10331. {
  10332. Terminal->ExceptionOnFail = true;
  10333. }
  10334. else
  10335. {
  10336. APath = ExpandEnvironmentVariables(Path);
  10337. }
  10338. try
  10339. {
  10340. DirView(Side)->Path = APath;
  10341. }
  10342. __finally
  10343. {
  10344. if (Remote)
  10345. {
  10346. Terminal->ExceptionOnFail = false;
  10347. }
  10348. }
  10349. }
  10350. catch (Exception & E)
  10351. {
  10352. if (!Remote || Terminal->Active)
  10353. {
  10354. ShowExtendedExceptionEx(Terminal, &E);
  10355. Result = false;
  10356. }
  10357. else
  10358. {
  10359. throw;
  10360. }
  10361. }
  10362. return Result;
  10363. }
  10364. //---------------------------------------------------------------------------
  10365. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10366. {
  10367. // Make sure there some feedback even when loading tiny local folders
  10368. TInstantOperationVisualizer Visualizer;
  10369. DirView(Side)->ReloadDirectory();
  10370. }
  10371. //---------------------------------------------------------------------------
  10372. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10373. {
  10374. if (Index == SessionsPageControl->TabIndex)
  10375. {
  10376. CloseSession();
  10377. }
  10378. else
  10379. {
  10380. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10381. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10382. TTerminalManager * Manager = TTerminalManager::Instance();
  10383. if (!Terminal->Active)
  10384. {
  10385. Manager->FreeTerminal(Terminal);
  10386. }
  10387. else
  10388. {
  10389. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10390. if (CanCloseQueue(Queue))
  10391. {
  10392. Manager->FreeTerminal(Terminal);
  10393. }
  10394. }
  10395. }
  10396. }
  10397. //---------------------------------------------------------------------------
  10398. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10399. {
  10400. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10401. if (Session == NULL)
  10402. {
  10403. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10404. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10405. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10406. //P = SessionsPageControl->ClientToScreen(P);
  10407. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10408. }
  10409. else
  10410. {
  10411. CloseSessionTab(Index);
  10412. }
  10413. }
  10414. //---------------------------------------------------------------------------
  10415. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10416. {
  10417. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10418. {
  10419. TInstantOperationVisualizer Visualizer;
  10420. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10421. // before we create a new data object
  10422. ClipboardClear();
  10423. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10424. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10425. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10426. // Intermediate variable to WORKAROUND a crash in compiler
  10427. TObjectList * ClipboardFakeMonitors =
  10428. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10429. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10430. if (FClipboardDragDropFilesEx.get() == NULL)
  10431. {
  10432. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10433. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10434. }
  10435. FClipboardDragDropFilesEx->FileList->Clear();
  10436. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10437. FClipboardDragDropFilesEx->CopyToClipboard();
  10438. FClipboardTerminal = Terminal;
  10439. // Need full paths, as cwd can be different once files are pasted
  10440. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10441. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10442. {
  10443. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10444. }
  10445. }
  10446. }
  10447. //---------------------------------------------------------------------------
  10448. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10449. {
  10450. if (OpenClipboard(0))
  10451. {
  10452. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10453. CloseClipboard();
  10454. }
  10455. }
  10456. //---------------------------------------------------------------------------
  10457. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10458. {
  10459. return !FClipboardFakeDirectory.IsEmpty();
  10460. }
  10461. //---------------------------------------------------------------------------
  10462. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10463. {
  10464. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10465. FClipboardFakeDirectory = UnicodeString();
  10466. FClipboardTerminal = NULL;
  10467. if (FDownloadingFromClipboard)
  10468. {
  10469. // We are called by the monitor, so attempt to release it would deadlock
  10470. FClipboardFakeMonitorsPendingReset = true;
  10471. }
  10472. else
  10473. {
  10474. FClipboardFakeMonitors.reset(NULL);
  10475. }
  10476. }
  10477. //---------------------------------------------------------------------------
  10478. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10479. {
  10480. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10481. {
  10482. ClipboardStop();
  10483. }
  10484. }
  10485. //---------------------------------------------------------------------------
  10486. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10487. {
  10488. TTerminalManager * Manager = TTerminalManager::Instance();
  10489. Manager->ActiveSession = FClipboardTerminal;
  10490. TTransferOperationParam Params;
  10491. Params.TargetDirectory = TargetDirectory;
  10492. Params.DragDrop = DragDrop;
  10493. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10494. }
  10495. //---------------------------------------------------------------------------
  10496. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10497. {
  10498. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10499. // (as it pumps a Windows message queue while being shutted down)
  10500. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10501. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10502. {
  10503. // Can fail as it can be e.g. locked by AV, so we retry that later
  10504. bool Removed = RemoveDir(ApiPath(FileName));
  10505. try
  10506. {
  10507. if (!NonVisualDataModule->Busy)
  10508. {
  10509. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10510. FClipboardFakeMonitorsPendingReset = false;
  10511. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10512. TAutoFlag Flag(FDownloadingFromClipboard);
  10513. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10514. }
  10515. }
  10516. __finally
  10517. {
  10518. if (!Removed)
  10519. {
  10520. RemoveDir(ApiPath(FileName));
  10521. }
  10522. }
  10523. }
  10524. }
  10525. //---------------------------------------------------------------------------
  10526. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10527. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10528. {
  10529. TFileMasks::TParams MaskParams;
  10530. MaskParams.Size = Size;
  10531. MaskParams.Modification = Modification;
  10532. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10533. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10534. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10535. if (!Local && Directory)
  10536. {
  10537. FileName = UnixExcludeTrailingBackslash(FileName);
  10538. }
  10539. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10540. {
  10541. bool ImplicitMatch;
  10542. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10543. !ImplicitMatch)
  10544. {
  10545. Color = Iter->Color;
  10546. break;
  10547. }
  10548. }
  10549. }
  10550. //---------------------------------------------------------------------------
  10551. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10552. {
  10553. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10554. if (!DirView->IsEditing())
  10555. {
  10556. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10557. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10558. // while searching
  10559. if (Key == VK_BACK)
  10560. {
  10561. if (!FIncrementalSearch.IsEmpty())
  10562. {
  10563. if (FIncrementalSearch.Length() == 1)
  10564. {
  10565. ResetIncrementalSearch();
  10566. }
  10567. else
  10568. {
  10569. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10570. IncrementalSearch(NewText, false, false);
  10571. }
  10572. Key = 0;
  10573. }
  10574. }
  10575. }
  10576. }
  10577. //---------------------------------------------------------------------------
  10578. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10579. {
  10580. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10581. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10582. {
  10583. // Filter control sequences.
  10584. // When not searching yet, prefer use of the space for toggling file selection
  10585. // (so we cannot incrementally search for a string starting with a space).
  10586. if ((Key > VK_SPACE) ||
  10587. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10588. {
  10589. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10590. Key = 0;
  10591. }
  10592. }
  10593. }
  10594. //---------------------------------------------------------------------------
  10595. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10596. {
  10597. if (!FIncrementalSearch.IsEmpty())
  10598. {
  10599. FIncrementalSearch = L"";
  10600. DirView(osCurrent)->UpdateStatusBar();
  10601. }
  10602. }
  10603. //---------------------------------------------------------------------------
  10604. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10605. {
  10606. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10607. if (Item == NULL)
  10608. {
  10609. MessageBeep(MB_ICONHAND);
  10610. }
  10611. else
  10612. {
  10613. TCustomDirView * ADirView = DirView(osCurrent);
  10614. {
  10615. TAutoNestingCounter Guard(FIncrementalSearching);
  10616. ADirView->FocusItem(Item);
  10617. if (ADirView->NortonLike == nlOff)
  10618. {
  10619. if ((ADirView->Selected != Item) ||
  10620. (ADirView->SelCount > 1))
  10621. {
  10622. ADirView->ClearSelection();
  10623. ADirView->Selected = Item;
  10624. }
  10625. }
  10626. }
  10627. FIncrementalSearch = Text;
  10628. Item->MakeVisible(false);
  10629. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10630. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10631. ADirView->UpdateStatusBar();
  10632. }
  10633. }
  10634. //---------------------------------------------------------------------------
  10635. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10636. {
  10637. int Index = Item->Index;
  10638. if (!Reverse)
  10639. {
  10640. Index++;
  10641. if (Index >= Item->Owner->Count)
  10642. {
  10643. Index = 0;
  10644. }
  10645. }
  10646. else
  10647. {
  10648. Index--;
  10649. if (Index < 0)
  10650. {
  10651. Index = Item->Owner->Count - 1;
  10652. }
  10653. }
  10654. return Item->Owner->Item[Index];
  10655. }
  10656. //---------------------------------------------------------------------------
  10657. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10658. {
  10659. TCustomDirView * ADirView = DirView(osCurrent);
  10660. if (ADirView->Items->Count == 0)
  10661. {
  10662. return NULL;
  10663. }
  10664. else
  10665. {
  10666. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10667. TListItem * Item = CurrentItem;
  10668. if (SkipCurrent)
  10669. {
  10670. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10671. }
  10672. while (true)
  10673. {
  10674. bool Matches = false;
  10675. switch (WinConfiguration->PanelSearch)
  10676. {
  10677. case isNameStartOnly:
  10678. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10679. break;
  10680. case isName:
  10681. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10682. break;
  10683. case isAll:
  10684. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10685. int Index = 0;
  10686. while ((Index < ColCount) && !Matches)
  10687. {
  10688. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10689. if (Visible)
  10690. {
  10691. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10692. }
  10693. Index++;
  10694. }
  10695. break;
  10696. }
  10697. if (Matches)
  10698. {
  10699. return Item;
  10700. }
  10701. Item = GetNextFile(Item, Reverse);
  10702. if (Item == CurrentItem)
  10703. {
  10704. return NULL;
  10705. }
  10706. }
  10707. }
  10708. }
  10709. //---------------------------------------------------------------------------
  10710. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10711. {
  10712. ResetIncrementalSearch();
  10713. }
  10714. //---------------------------------------------------------------------------
  10715. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10716. {
  10717. if (Message.CharCode == VK_TAB)
  10718. {
  10719. if (!FIncrementalSearch.IsEmpty())
  10720. {
  10721. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10722. bool Reverse = Shift.Contains(ssShift);
  10723. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10724. Message.Result = 1;
  10725. return;
  10726. }
  10727. }
  10728. else if (Message.CharCode == VK_ESCAPE)
  10729. {
  10730. if (!FIncrementalSearch.IsEmpty())
  10731. {
  10732. ResetIncrementalSearch();
  10733. Message.Result = 1;
  10734. return;
  10735. }
  10736. }
  10737. TForm::Dispatch(&Message);
  10738. }
  10739. //---------------------------------------------------------------------------
  10740. void __fastcall TCustomScpExplorerForm::Deactivate()
  10741. {
  10742. TForm::Deactivate();
  10743. // E.g. when switching to an internal editor window
  10744. ResetIncrementalSearch();
  10745. }
  10746. //---------------------------------------------------------------------------
  10747. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10748. {
  10749. // When switching to another application
  10750. ResetIncrementalSearch();
  10751. }
  10752. //---------------------------------------------------------------------------
  10753. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10754. {
  10755. ResetIncrementalSearch();
  10756. }
  10757. //---------------------------------------------------------------------------
  10758. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10759. {
  10760. if (FIncrementalSearching <= 0)
  10761. {
  10762. ResetIncrementalSearch();
  10763. }
  10764. }
  10765. //---------------------------------------------------------------------------
  10766. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10767. {
  10768. CountClicksForWindowPrint(this);
  10769. }
  10770. //---------------------------------------------------------------------------
  10771. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10772. {
  10773. CenterReconnectToolbar();
  10774. }
  10775. //---------------------------------------------------------------------------
  10776. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10777. {
  10778. if (RemoteDirView->ItemFocused != NULL)
  10779. {
  10780. RemoteDirView->ItemFocused->Selected = true;
  10781. }
  10782. }
  10783. //---------------------------------------------------------------------------
  10784. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10785. {
  10786. if (!FQueueStatusUpdating)
  10787. {
  10788. bool Refresh;
  10789. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10790. {
  10791. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10792. }
  10793. else
  10794. {
  10795. if (FQueueFileList->GetCount() > 0)
  10796. {
  10797. FQueueFileList->Clear();
  10798. Refresh = true;
  10799. }
  10800. }
  10801. int Count = FQueueFileList->GetCount();
  10802. if (QueueFileList->Items->Count != Count)
  10803. {
  10804. DebugAssert(Refresh);
  10805. QueueFileList->Items->Count = Count;
  10806. }
  10807. if (Refresh)
  10808. {
  10809. QueueFileList->Invalidate();
  10810. }
  10811. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10812. }
  10813. }
  10814. //---------------------------------------------------------------------------
  10815. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10816. {
  10817. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10818. }
  10819. //---------------------------------------------------------------------------
  10820. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10821. {
  10822. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10823. // See TSynchronizeChecklistDialog::UpdateTimer
  10824. if (QueueFileList->Items->Count > 0)
  10825. {
  10826. QueueFileList->Items->Item[0]->MakeVisible(false);
  10827. }
  10828. UpdateQueueFileList();
  10829. }
  10830. //---------------------------------------------------------------------------
  10831. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10832. {
  10833. Active = QueueView3->Focused() || QueueFileList->Focused();
  10834. }
  10835. //---------------------------------------------------------------------------
  10836. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10837. {
  10838. QueueLabelUpdateStatus();
  10839. }
  10840. //---------------------------------------------------------------------------
  10841. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10842. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10843. {
  10844. int State = FQueueFileList->GetState(Item->Index);
  10845. if (State == qfsProcessed)
  10846. {
  10847. Sender->Canvas->Font->Color = clGrayText;
  10848. }
  10849. }
  10850. //---------------------------------------------------------------------------
  10851. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10852. {
  10853. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10854. QueueFileList->Column[0]->Width =
  10855. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10856. }
  10857. //---------------------------------------------------------------------------
  10858. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10859. {
  10860. QueueFileListColumnAutoSize();
  10861. }
  10862. //---------------------------------------------------------------------------
  10863. void __fastcall TCustomScpExplorerForm::CloseApp()
  10864. {
  10865. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10866. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10867. // So we clear the flag here.
  10868. DebugAssert(NonVisualDataModule->Busy);
  10869. NonVisualDataModule->EndBusy();
  10870. try
  10871. {
  10872. Close();
  10873. }
  10874. __finally
  10875. {
  10876. NonVisualDataModule->StartBusy();
  10877. }
  10878. }
  10879. //---------------------------------------------------------------------------
  10880. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10881. {
  10882. return (Terminal != NULL) && Terminal->Active;
  10883. }
  10884. //---------------------------------------------------------------------------
  10885. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10886. {
  10887. return (ManagedSession != NULL);
  10888. }
  10889. //---------------------------------------------------------------------------
  10890. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10891. {
  10892. return IsActiveTerminal(Terminal);
  10893. }
  10894. //---------------------------------------------------------------------------
  10895. void TCustomScpExplorerForm::LocalLocalCopy(
  10896. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10897. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10898. {
  10899. DebugFail();
  10900. }
  10901. //---------------------------------------------------------------------------
  10902. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10903. {
  10904. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10905. }
  10906. //---------------------------------------------------------------------------
  10907. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10908. {
  10909. // Changing tab list triggers the OnResize.
  10910. if (!FUpdatingSessionTabs)
  10911. {
  10912. // UpdateTabsCaptionTruncation should be enough
  10913. SessionListChanged(true);
  10914. }
  10915. }
  10916. //---------------------------------------------------------------------------
  10917. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10918. {
  10919. if (ASession == ManagedSession)
  10920. {
  10921. UpdateSession(ASession);
  10922. }
  10923. TSessionData * Data = ASession->StateData;
  10924. UnicodeString Result;
  10925. if (Side == osLocal)
  10926. {
  10927. Result = Data->LocalDirectory;
  10928. }
  10929. else if (DebugAlwaysTrue(Side == osOther))
  10930. {
  10931. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10932. }
  10933. return Result;
  10934. }
  10935. //---------------------------------------------------------------------------
  10936. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10937. {
  10938. UnicodeString Result;
  10939. if (!ASession->Active)
  10940. {
  10941. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10942. }
  10943. else
  10944. {
  10945. Result = GetSessionPath(ASession, osRemote);
  10946. }
  10947. return Result;
  10948. }
  10949. //---------------------------------------------------------------------------
  10950. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10951. {
  10952. return LoadStr(NEW_REMOTE_TAB_HINT);
  10953. }
  10954. //---------------------------------------------------------------------------
  10955. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10956. {
  10957. if (Index >= 0)
  10958. {
  10959. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10960. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10961. if (TabSession == NULL)
  10962. {
  10963. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10964. if (NewTabTabCaption != TabSheet->Caption)
  10965. {
  10966. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10967. }
  10968. }
  10969. else
  10970. {
  10971. TTerminalManager * Manager = TTerminalManager::Instance();
  10972. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10973. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10974. }
  10975. }
  10976. }
  10977. //---------------------------------------------------------------------------
  10978. typedef std::vector<TListItem *> TListItemsVector;
  10979. struct TCalculateSizeOperation
  10980. {
  10981. TCalculateSizeStats Stats;
  10982. TListItemsVector ListItems;
  10983. size_t Index;
  10984. };
  10985. //---------------------------------------------------------------------------
  10986. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  10987. {
  10988. TCalculateSizeOperation CalculateSizeOperation;
  10989. Side = GetSide(Side);
  10990. TCustomDirView * ADirView = DirView(Side);
  10991. bool LocalSide = IsSideLocalBrowser(Side);
  10992. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  10993. std::unique_ptr<TStrings> FileList(new TStringList());
  10994. for (int Index = 0; Index < AllFileList->Count; Index++)
  10995. {
  10996. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10997. if (ADirView->ItemIsDirectory(Item))
  10998. {
  10999. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11000. CalculateSizeOperation.ListItems.push_back(Item);
  11001. }
  11002. }
  11003. CalculateSizeOperation.Index = 0;
  11004. __int64 Size = 0;
  11005. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  11006. FCalculateSizeOperation = &CalculateSizeOperation;
  11007. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11008. FOnFileOperationFinished = DirectorySizeCalculated;
  11009. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  11010. FPrimaryOperation = foCalculateSize;
  11011. TCalculatedSizes CalculatedSizes;
  11012. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11013. if (LocalSide)
  11014. {
  11015. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  11016. FProgressSide = Side;
  11017. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11018. }
  11019. else
  11020. {
  11021. TCalculateSizeParams Params;
  11022. Params.Stats = &CalculateSizeOperation.Stats;
  11023. Params.UseCache = true;
  11024. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11025. }
  11026. }
  11027. //---------------------------------------------------------------------------
  11028. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11029. {
  11030. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11031. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11032. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11033. // optimization
  11034. bool GotExpectedItem =
  11035. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11036. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11037. int Index = -1;
  11038. if (DebugAlwaysTrue(GotExpectedItem))
  11039. {
  11040. Index = FCalculateSizeOperation->Index;
  11041. FCalculateSizeOperation->Index++;
  11042. }
  11043. else
  11044. {
  11045. TListItemsVector::iterator I =
  11046. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11047. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11048. {
  11049. Index = I - FCalculateSizeOperation->ListItems.begin();
  11050. }
  11051. }
  11052. if (DebugAlwaysTrue(Index >= 0) && Success)
  11053. {
  11054. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11055. ADirView->SetItemCalculatedSize(Item, Size);
  11056. }
  11057. }
  11058. //---------------------------------------------------------------------------
  11059. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11060. {
  11061. Accept = false;
  11062. }
  11063. //---------------------------------------------------------------------------