CustomScpExplorer.cpp 415 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392
  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 "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. #include <System.StrUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. #define WM_PASTE_FILES (WM_WINSCP_USER + 14)
  55. static const int SessionPanelCount = 3;
  56. //---------------------------------------------------------------------------
  57. class TMutexGuard
  58. {
  59. public:
  60. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  61. int Timeout = 5000)
  62. {
  63. FMutex = NULL;
  64. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  65. if (WaitResult == WAIT_TIMEOUT)
  66. {
  67. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  68. }
  69. else
  70. {
  71. FMutex = AMutex;
  72. }
  73. }
  74. ~TMutexGuard()
  75. {
  76. if (FMutex != NULL)
  77. {
  78. ReleaseMutex(FMutex);
  79. }
  80. }
  81. private:
  82. HANDLE FMutex;
  83. };
  84. //---------------------------------------------------------------------------
  85. class TWindowLock
  86. {
  87. public:
  88. TWindowLock(TCustomScpExplorerForm * Form) :
  89. FForm(Form)
  90. {
  91. FForm->LockWindow();
  92. }
  93. ~TWindowLock()
  94. {
  95. FForm->UnlockWindow();
  96. }
  97. private:
  98. TCustomScpExplorerForm * FForm;
  99. };
  100. //---------------------------------------------------------------------------
  101. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  102. FForm(Form)
  103. {
  104. FForm->BatchStart(FBatchStorage);
  105. }
  106. //---------------------------------------------------------------------------
  107. TAutoBatch::~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. //---------------------------------------------------------------------------
  112. struct TTransferOperationParam
  113. {
  114. TTransferOperationParam();
  115. UnicodeString TargetDirectory;
  116. bool Temp;
  117. bool DragDrop;
  118. int Options;
  119. TAutoSwitch Queue;
  120. };
  121. //---------------------------------------------------------------------------
  122. TTransferOperationParam::TTransferOperationParam()
  123. {
  124. Temp = false;
  125. DragDrop = false;
  126. Options = 0;
  127. Queue = asAuto;
  128. }
  129. //---------------------------------------------------------------------------
  130. class TTransferPresetNoteData : public TObject
  131. {
  132. public:
  133. UnicodeString Message;
  134. };
  135. //---------------------------------------------------------------------------
  136. class TTerminalNoteData : public TObject
  137. {
  138. public:
  139. TManagedTerminal * Terminal;
  140. };
  141. //---------------------------------------------------------------------------
  142. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  143. FFormRestored(false),
  144. TForm(Owner)
  145. {
  146. AddStartupSequence(L"F");
  147. FInvalid = false;
  148. FCurrentSide = osRemote;
  149. FEverShown = false;
  150. FDocks = new TList();
  151. InitControls();
  152. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  153. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  154. NonVisualDataModule->ScpExplorer = this;
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. FAutoOperation = false;
  159. FOnFileOperationFinished = NULL;
  160. FPrimaryOperation = foNone;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDExtMapFile = NULL;
  169. // CreateMutexW keeps failing with ERROR_NOACCESS
  170. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  171. DebugAssert(FDDExtMutex != NULL);
  172. FDDTargetControl = NULL;
  173. FDelayedDeletionTimer = NULL;
  174. FDelayedDeletionList = new TStringList();
  175. FDDFileList = NULL;
  176. FPendingTempSpaceWarn = false;
  177. FCapturedLog = NULL;
  178. FDragDropOperation = false;
  179. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  180. FAllowTransferPresetAutoSelect = true;
  181. FSessionChanging = false;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FDeletedFiles = NULL;
  195. FMoveToQueue = false;
  196. StandaloneOperation = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FOnSynchronizeAbort = NULL;
  199. FSynchronizeTerminal = NULL;
  200. FNeedSession = false;
  201. FDoNotIdleCurrentTerminal = 0;
  202. FIncrementalSearching = 0;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FHiContrastTheme = NULL;
  209. InitializeRemoteThumbnailMask();
  210. GlyphsModule->PixelsPerInch = CurrentPPI;
  211. FEditorManager = new TEditorManager();
  212. FEditorManager->OnFileChange = ExecutedFileChanged;
  213. FEditorManager->OnFileReload = ExecutedFileReload;
  214. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  215. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  216. FLocalEditors = new TList();
  217. FQueueStatus = NULL;
  218. FQueueStatusSection = new TCriticalSection();
  219. FQueueStatusInvalidated = false;
  220. FQueueItemInvalidated = false;
  221. FQueueStatusUpdating = false;
  222. FQueueActedItem = NULL;
  223. FQueueController = new TQueueController(QueueView3);
  224. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  225. FUserActionTimer = new TTimer(this);
  226. FUserActionTimer->Enabled = false;
  227. FUserActionTimer->Interval = 10;
  228. FUserActionTimer->OnTimer = UserActionTimer;
  229. FNotes = new TStringList();
  230. FNoteTimer = new TTimer(this);
  231. FNoteTimer->Enabled = false;
  232. FNoteTimer->OnTimer = NoteTimer;
  233. FOnNoteClick = NULL;
  234. FOle32Library = LoadLibrary(L"Ole32.dll");
  235. FDragMoveCursor = FOle32Library != NULL ?
  236. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  237. UseSystemSettings(this);
  238. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  239. static_cast<TObject*>(GetComponent(fcTransferList)));
  240. DebugAssert(TransferList != NULL);
  241. FTransferListHoverIndex = -1;
  242. TransferList->OnChange = TransferListChange;
  243. TransferList->OnDrawItem = TransferListDrawItem;
  244. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  245. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  246. UseDesktopFont(SessionsPageControl);
  247. UseDesktopFont(RemoteDirView);
  248. UseDesktopFont(RemoteDriveView);
  249. UseDesktopFont(QueueView3);
  250. UseDesktopFont(QueueFileList);
  251. UseDesktopFont(QueueLabel);
  252. UseDesktopFont(RemoteStatusBar);
  253. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  254. QueueSplitter->ShowHint = true;
  255. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  256. QueueFileListSplitter->ShowHint = true;
  257. RemotePanelSplitter->ShowHint = true;
  258. UpdateImages();
  259. FCustomCommandMenu = CreateTBXPopupMenu(this);
  260. FCustomCommandLocalFileList = NULL;
  261. FCustomCommandRemoteFileList = NULL;
  262. FSessionColors = new TPngImageList(this);
  263. FSessionColors->ColorDepth = cd32Bit;
  264. AddFixedSessionImages();
  265. SessionsPageControl->Images = FSessionColors;
  266. // Needed for Explorer (Commander calls it implicitly on its own)
  267. UpdateSessionsPageControlHeight();
  268. UpdateQueueLabel();
  269. CheckStoreTransition();
  270. CreateHiddenWindow();
  271. StartUpdates();
  272. // Start loading drive data, so that they are ready once we need them for StartCreationDirectoryMonitorsOnEachDrive
  273. // (necessary for "explorer" only, as "commander" loads it for its drive menu)
  274. DriveInfo->NeedData();
  275. }
  276. //---------------------------------------------------------------------------
  277. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  278. {
  279. AppLog(L"Destroying main window");
  280. FInvalid = true;
  281. ReleaseHiContrastTheme();
  282. if (FClipboardTerminal != NULL)
  283. {
  284. ClipboardClear(); // implies ClipboardStop
  285. }
  286. // this has to be one of the very first things to do
  287. StopUpdateThread();
  288. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  289. TTerminalManager::Instance()->ScpExplorer = NULL;
  290. delete FCustomCommandLocalFileList;
  291. delete FCustomCommandRemoteFileList;
  292. delete FCustomCommandMenu;
  293. delete FTrayIcon;
  294. FTrayIcon = NULL;
  295. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  296. delete FEditorManager;
  297. ForceCloseLocalEditors();
  298. delete FLocalEditors;
  299. HideFileFindDialog();
  300. if (FDelayedDeletionTimer)
  301. {
  302. DoDelayedDeletion(NULL);
  303. SAFE_DESTROY(FDelayedDeletionTimer);
  304. }
  305. SAFE_DESTROY(FDelayedDeletionList);
  306. // sometimes we do not get DDEnd so the list is not released
  307. SAFE_DESTROY(FDDFileList);
  308. DebugAssert(FSynchronizeController == NULL);
  309. CloseHandle(FDDExtMutex);
  310. FDDExtMutex = NULL;
  311. FreeLibrary(FOle32Library);
  312. FOle32Library = NULL;
  313. FDragMoveCursor = NULL;
  314. DebugAssert(!FErrorList);
  315. if (FEverShown)
  316. {
  317. // when window is never shown (like when running command-line operation),
  318. // particularly window site is not restored correctly (BoundsRect value set
  319. // in RestoreForm gets lost during handle allocation), so we do not want
  320. // it to be stored
  321. AppLog(L"Storing main window configuration");
  322. StoreParams();
  323. }
  324. ManagedSession = NULL;
  325. Queue = NULL;
  326. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  327. NonVisualDataModule->ScpExplorer = NULL;
  328. delete FQueueScrollOnDragOver;
  329. FQueueScrollOnDragOver = NULL;
  330. delete FQueueController;
  331. FQueueController = NULL;
  332. delete FQueueStatusSection;
  333. FQueueStatusSection = NULL;
  334. delete FQueueStatus;
  335. FQueueStatus = NULL;
  336. delete FUserActionTimer;
  337. FUserActionTimer = NULL;
  338. delete FNoteTimer;
  339. delete FNotes;
  340. delete FNoteData;
  341. SAFE_DESTROY(FDocks);
  342. SessionsPageControl->Images = NULL;
  343. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  344. SAFE_DESTROY(FSessionColors);
  345. SAFE_DESTROY(FSessionsDragDropFilesEx);
  346. SAFE_DESTROY(FHistoryMenu[0][0]);
  347. SAFE_DESTROY(FHistoryMenu[0][1]);
  348. SAFE_DESTROY(FHistoryMenu[1][0]);
  349. SAFE_DESTROY(FHistoryMenu[1][1]);
  350. if (FHiddenWindow != NULL)
  351. {
  352. DestroyWindow(FHiddenWindow);
  353. FHiddenWindow = NULL;
  354. }
  355. AppLog(L"Main window destroyed");
  356. }
  357. //---------------------------------------------------------------------------
  358. void TCustomScpExplorerForm::ReleaseHiContrastTheme()
  359. {
  360. if (FHiContrastTheme != NULL)
  361. {
  362. ReleaseTBXTheme(FHiContrastTheme);
  363. FHiContrastTheme = NULL;
  364. }
  365. }
  366. //---------------------------------------------------------------------------
  367. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  368. {
  369. std::unique_ptr<TSessionData> Data;
  370. if (!Session.IsEmpty())
  371. {
  372. bool DefaultsOnly;
  373. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  374. }
  375. TTerminalManager * Manager = TTerminalManager::Instance();
  376. for (int Index = 0; Index < Manager->Count; Index++)
  377. {
  378. TTerminal * Terminal = Manager->Sessions[Index];
  379. if (Session.IsEmpty() ||
  380. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  381. {
  382. if (Path.IsEmpty())
  383. {
  384. Terminal->ClearCaches();
  385. }
  386. else
  387. {
  388. Terminal->DirectoryModified(Path, true);
  389. }
  390. }
  391. }
  392. // We should flag a pending refresh for the background terminals or busy foreground terminals
  393. if (HasActiveTerminal() &&
  394. CanCommandLineFromAnotherInstance() &&
  395. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  396. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  397. {
  398. Terminal->ReloadDirectory();
  399. }
  400. }
  401. //---------------------------------------------------------------------------
  402. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  403. {
  404. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  405. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  406. size_t MessageSize = sizeof(TCopyDataMessage);
  407. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  408. if (Result)
  409. {
  410. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  411. Result = (Message.Version == TCopyDataMessage::Version1);
  412. if (Result)
  413. {
  414. switch (Message.Command)
  415. {
  416. case TCopyDataMessage::CommandCanCommandLine:
  417. Result = CanCommandLineFromAnotherInstance();
  418. if (Result)
  419. {
  420. AppLog(L"Can process command-line from other instance");
  421. }
  422. else
  423. {
  424. AppLog(L"Cannot process command-line from other instance");
  425. }
  426. break;
  427. case TCopyDataMessage::CommandCommandLine:
  428. {
  429. UnicodeString CommandLine(Message.CommandLine);
  430. Result = CommandLineFromAnotherInstance(CommandLine);
  431. }
  432. break;
  433. case TCopyDataMessage::RefreshPanel:
  434. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  435. break;
  436. case TCopyDataMessage::MainWindowCheck:
  437. Result = true;
  438. break;
  439. default:
  440. Result = false;
  441. break;
  442. }
  443. }
  444. }
  445. Message.Result = Result ? 1 : 0;
  446. }
  447. //---------------------------------------------------------------------------
  448. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  449. {
  450. WNDCLASS WindowClass = {0};
  451. WindowClass.lpfnWndProc = DefWindowProc;
  452. WindowClass.hInstance = HInstance;
  453. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  454. FHiddenWindow = NULL;
  455. if (RegisterClass(&WindowClass))
  456. {
  457. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  458. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  459. }
  460. }
  461. //---------------------------------------------------------------------------
  462. bool __fastcall TCustomScpExplorerForm::CanConsole()
  463. {
  464. return
  465. HasActiveTerminal() &&
  466. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  467. }
  468. //---------------------------------------------------------------------------
  469. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  470. {
  471. bool Result = Visible && !NonVisualDataModule->Busy;
  472. return Result;
  473. }
  474. //---------------------------------------------------------------------------
  475. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  476. const UnicodeString & CommandLine)
  477. {
  478. TProgramParams Params(CommandLine);
  479. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  480. if (Result)
  481. {
  482. NonVisualDataModule->StartBusy();
  483. try
  484. {
  485. // this action is initiated from another process,
  486. // so it's likely that our window is not visible,
  487. // and user won't see what is going on
  488. Application->BringToFront();
  489. // reload sessions as we may be asked to open a session
  490. // just stored by another instance
  491. StoredSessions->Reload();
  492. UnicodeString SessionName = Params.Param[1];
  493. std::unique_ptr<TObjectList> DataList(new TObjectList());
  494. try
  495. {
  496. UnicodeString DownloadFile; // unused
  497. int Flags = GetCommandLineParseUrlFlags(&Params);
  498. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  499. }
  500. catch (EAbort &)
  501. {
  502. // Happens when opening session in PuTTY and in other situations,
  503. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  504. // to process the command-line on its own (or to pass it to yet another instance)
  505. }
  506. if (DataList->Count > 0)
  507. {
  508. TTerminalManager * Manager = TTerminalManager::Instance();
  509. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  510. }
  511. }
  512. __finally
  513. {
  514. NonVisualDataModule->EndBusy();
  515. }
  516. }
  517. return Result;
  518. }
  519. //---------------------------------------------------------------------------
  520. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  521. {
  522. TManagedTerminal * Result;
  523. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  524. {
  525. Result = FManagedSession;
  526. }
  527. else
  528. {
  529. Result = NULL;
  530. }
  531. return Result;
  532. }
  533. //---------------------------------------------------------------------------
  534. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  535. {
  536. if (FManagedSession != value)
  537. {
  538. TWindowLock Lock(this);
  539. SessionChanging();
  540. DoSetManagedSession(value, false);
  541. }
  542. }
  543. //---------------------------------------------------------------------------
  544. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  545. {
  546. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  547. FManagedSession = value;
  548. {
  549. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect, false);
  550. TAutoFlag SessionChangingFlag(FSessionChanging);
  551. SessionChanged(Replace);
  552. }
  553. if (Terminal != NULL)
  554. {
  555. TransferPresetAutoSelect();
  556. }
  557. // Update app and tab titles, prevented above by FSessionChanging.
  558. UpdateControls();
  559. }
  560. //---------------------------------------------------------------------------
  561. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  562. {
  563. FEditorManager->ProcessFiles(FileTerminalReplaced, value);
  564. DoSetManagedSession(value, true);
  565. }
  566. //---------------------------------------------------------------------------
  567. void __fastcall TCustomScpExplorerForm::SessionChanging()
  568. {
  569. if (ManagedSession != NULL)
  570. {
  571. UpdateSession(ManagedSession);
  572. }
  573. }
  574. //---------------------------------------------------------------------------
  575. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  576. {
  577. if (ManagedSession != NULL)
  578. {
  579. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  580. }
  581. if (!TTerminalManager::Instance()->Updating)
  582. {
  583. SessionListChanged();
  584. }
  585. if (Terminal != NULL)
  586. {
  587. TObject * State = NULL;
  588. if (WinConfiguration->PreservePanelState)
  589. {
  590. State = Terminal->RemoteExplorerState;
  591. DirView(osRemote)->AnnounceState(State);
  592. }
  593. try
  594. {
  595. if (Replaced)
  596. {
  597. RemoteDirView->ReplaceTerminal(Terminal);
  598. }
  599. else
  600. {
  601. RemoteDirView->Terminal = Terminal;
  602. }
  603. if (Terminal->Active)
  604. {
  605. Terminal->RefreshDirectory();
  606. }
  607. if (State != NULL)
  608. {
  609. DirView(osRemote)->RestoreState(State);
  610. }
  611. }
  612. __finally
  613. {
  614. DirView(osRemote)->AnnounceState(NULL);
  615. }
  616. }
  617. else
  618. {
  619. RemoteDirView->Terminal = NULL;
  620. }
  621. InitStatusBar();
  622. UpdateTransferList();
  623. // Update panels Enable state before refreshing the labels
  624. UpdateControls();
  625. }
  626. //---------------------------------------------------------------------------
  627. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  628. {
  629. if (Queue != value)
  630. {
  631. if (FQueue != NULL)
  632. {
  633. FQueue->OnListUpdate = NULL;
  634. FQueue->OnQueueItemUpdate = NULL;
  635. }
  636. FQueue = value;
  637. if (FQueue != NULL)
  638. {
  639. DebugAssert(FQueue->OnListUpdate == NULL);
  640. FQueue->OnListUpdate = QueueListUpdate;
  641. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  642. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  643. }
  644. QueueChanged();
  645. }
  646. }
  647. //---------------------------------------------------------------------------
  648. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  649. TListItem * Item)
  650. {
  651. if (FQueueActedItem == Item)
  652. {
  653. FQueueActedItem = NULL;
  654. if ((QueueView3->PopupMenu != NULL) &&
  655. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  656. {
  657. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  658. ReleaseCapture();
  659. }
  660. }
  661. if (Item->Data == FPendingQueueActionItem)
  662. {
  663. FPendingQueueActionItem = NULL;
  664. }
  665. }
  666. //---------------------------------------------------------------------------
  667. bool TCustomScpExplorerForm::IsAnythingQueued()
  668. {
  669. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  670. }
  671. //---------------------------------------------------------------------------
  672. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  673. {
  674. {
  675. TGuard Guard(FQueueStatusSection);
  676. FQueueStatusInvalidated = false;
  677. if (FQueue != NULL)
  678. {
  679. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  680. }
  681. }
  682. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  683. {
  684. FMaxQueueLength = FQueueStatus->Count;
  685. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  686. }
  687. {
  688. TAutoFlag Flag(FQueueStatusUpdating);
  689. FQueueController->UpdateQueueStatus(FQueueStatus);
  690. }
  691. UpdateQueueFileList();
  692. SetQueueProgress();
  693. UpdateQueueView();
  694. bool IsEmpty = !IsAnythingQueued();
  695. if (IsEmpty && (Terminal != NULL))
  696. {
  697. TOnceDoneOperation OnceDoneOperation =
  698. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  699. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  700. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  701. {
  702. FQueue->Enabled = false;
  703. }
  704. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  705. {
  706. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  707. }
  708. }
  709. }
  710. //---------------------------------------------------------------------------
  711. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  712. {
  713. UnicodeString Result;
  714. if (FQueueStatus != NULL)
  715. {
  716. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  717. {
  718. TFileOperationProgressType * ProgressData =
  719. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  720. if ((ProgressData != NULL) && ProgressData->InProgress)
  721. {
  722. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  723. }
  724. }
  725. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  726. {
  727. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  728. }
  729. }
  730. return Result;
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  734. {
  735. ComponentVisible[fcQueueView] =
  736. !IsLocalBrowserMode() &&
  737. ((WinConfiguration->QueueView.Show == qvShow) ||
  738. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  739. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  740. }
  741. //---------------------------------------------------------------------------
  742. void __fastcall TCustomScpExplorerForm::QueueChanged()
  743. {
  744. if (FQueueStatus != NULL)
  745. {
  746. // UpdateFileList implementation relies on the status being recreated when session changes
  747. delete FQueueStatus;
  748. FQueueStatus = NULL;
  749. }
  750. UpdateQueueStatus(true);
  751. }
  752. //---------------------------------------------------------------------------
  753. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  754. {
  755. if (FQueue == Queue)
  756. {
  757. FQueueStatusInvalidated = true;
  758. }
  759. }
  760. //---------------------------------------------------------------------------
  761. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  762. TQueueItem * Item)
  763. {
  764. if (FQueue == Queue)
  765. {
  766. TGuard Guard(FQueueStatusSection);
  767. // this may be running in parallel with QueueChanged
  768. if (FQueueStatus != NULL)
  769. {
  770. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  771. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  772. {
  773. FRefreshLocalDirectory = (QueueItem == NULL) ||
  774. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  775. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  776. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  777. }
  778. if (QueueItem != NULL)
  779. {
  780. QueueItem->UserData = (void*)true;
  781. FQueueItemInvalidated = true;
  782. }
  783. }
  784. }
  785. }
  786. //---------------------------------------------------------------------------
  787. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  788. {
  789. // during standalone editing, we have no way to see/control queue,
  790. // so we have to always popup prompts automatically
  791. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  792. }
  793. //---------------------------------------------------------------------------
  794. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  795. {
  796. if (FQueueStatus != NULL)
  797. {
  798. bool QueueAutoPopup = IsQueueAutoPopup();
  799. bool NeedRefresh = FQueueController->NeedRefresh();
  800. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  801. FQueueItemInvalidated = false;
  802. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  803. bool Updated = false;
  804. TQueueItemProxy * QueueItem;
  805. bool WasUserAction;
  806. for (int Index = 0; Index < Limit; Index++)
  807. {
  808. bool Update = false;
  809. QueueItem = FQueueStatus->Items[Index];
  810. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  811. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  812. (FPendingQueueActionItem == NULL))
  813. {
  814. FPendingQueueActionItem = QueueItem;
  815. FUserActionTimer->Enabled = true;
  816. }
  817. if ((bool)QueueItem->UserData)
  818. {
  819. QueueItem->UserData = (void*)false;
  820. QueueItem->Update();
  821. Updated = true;
  822. Update = true;
  823. }
  824. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  825. {
  826. Update = true;
  827. }
  828. if (Update)
  829. {
  830. FQueueController->RefreshQueueItem(QueueItem);
  831. }
  832. }
  833. if (Updated)
  834. {
  835. NonVisualDataModule->UpdateNonVisibleActions();
  836. SetQueueProgress();
  837. UpdateQueueFileList();
  838. }
  839. }
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  843. {
  844. if (FTaskbarList != NULL)
  845. {
  846. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  847. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  848. }
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  852. {
  853. if (Progress >= 0)
  854. {
  855. if (FTaskbarList != NULL)
  856. {
  857. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  858. }
  859. }
  860. else
  861. {
  862. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  863. }
  864. }
  865. //---------------------------------------------------------------------------
  866. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  867. {
  868. int OverallProgress;
  869. // FProgressForm is null when this is called from SetQueueProgress
  870. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  871. {
  872. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  873. }
  874. else if (!ProgressData->IsIndeterminate())
  875. {
  876. OverallProgress = ProgressData->OverallProgress();
  877. }
  878. else
  879. {
  880. OverallProgress = -1;
  881. }
  882. SetTaskbarListProgressValue(OverallProgress);
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  886. {
  887. TTerminalManager::Instance()->QueueStatusUpdated();
  888. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  889. {
  890. // Do not animate task bar button, when items are queued, but processing is disabled
  891. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  892. {
  893. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  894. {
  895. TFileOperationProgressType * ProgressData = NULL;
  896. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  897. {
  898. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  899. }
  900. if ((ProgressData != NULL) &&
  901. ProgressData->InProgress)
  902. {
  903. SetTaskbarListProgressValue(ProgressData);
  904. }
  905. else
  906. {
  907. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  908. }
  909. }
  910. else
  911. {
  912. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  913. }
  914. }
  915. else
  916. {
  917. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  918. }
  919. }
  920. UpdateQueueLabel();
  921. }
  922. //---------------------------------------------------------------------------
  923. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  924. {
  925. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  926. if (FQueueStatus != NULL)
  927. {
  928. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  929. if (ActiveAndPendingPrimaryCount > 0)
  930. {
  931. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  932. }
  933. }
  934. QueueLabel->Caption = Caption;
  935. }
  936. //---------------------------------------------------------------------------
  937. void TCustomScpExplorerForm::CheckStoreTransition()
  938. {
  939. if (WinConfiguration->StoreTransition == stInit)
  940. {
  941. if (IsUWP())
  942. {
  943. WinConfiguration->StoreTransition = stStoreFresh;
  944. AppLog(L"Fresh UWP/store installation");
  945. }
  946. else
  947. {
  948. WinConfiguration->StoreTransition = stStandard;
  949. AppLog(L"Fresh standard installation");
  950. }
  951. }
  952. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  953. {
  954. WinConfiguration->StoreTransition = stStoreMigrated;
  955. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  956. AppLog(L"Standard installation migrated to store installation");
  957. }
  958. if (WinConfiguration->StoreTransition == stStoreMigrated)
  959. {
  960. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  961. Toolbar->Parent = MessageDock;
  962. Toolbar->FullSize = true;
  963. Toolbar->Color = Application->HintColor;
  964. TTBXLabelItem * LabelItem;
  965. LabelItem = new TTBXLabelItem(Toolbar);
  966. // Convert to explicit RGB "black" color, to make sure it is contrasting even in Dark theme
  967. // (TTBXOfficeXPTheme.GetStandardColor would convert clWindow to "white" in dark theme)
  968. LabelItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  969. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  970. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  971. Toolbar->Items->Add(LabelItem);
  972. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  973. LinkItem->FontSettings->Color = LinkColor;
  974. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  975. LinkItem->OnClick = StoreTransitionLinkClick;
  976. Toolbar->Items->Add(LinkItem);
  977. AddMenuSeparator(Toolbar->Items);
  978. TTBXCustomItem * CloseItem = new TTBXItem(Toolbar);
  979. CloseItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  980. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  981. CloseItem->OnClick = StoreTransitionCloseClick;
  982. Toolbar->Items->Add(CloseItem);
  983. GiveTBItemPriority(CloseItem);
  984. }
  985. }
  986. //---------------------------------------------------------------------------
  987. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  988. {
  989. ShowHelp(HELP_STORE_TRANSITION);
  990. }
  991. //---------------------------------------------------------------------------
  992. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  993. {
  994. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  995. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  996. delete Toolbar;
  997. WinConfiguration->StoreTransition = stStoreAcknowledged;
  998. UpdateControls();
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  1002. {
  1003. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  1004. static_cast<TComponent*>(GetComponent(fcTransferList)));
  1005. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  1006. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  1007. TransferList->Strings->BeginUpdate();
  1008. try
  1009. {
  1010. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  1011. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  1012. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  1013. if (FTransferDropDownHint.IsEmpty())
  1014. {
  1015. FTransferDropDownHint = TransferDropDown->Hint;
  1016. }
  1017. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  1018. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  1019. TransferDropDown->Text = StripHotkey(Name);
  1020. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  1021. (FTransferDropDownHint, StripHotkey(Name),
  1022. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  1023. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  1024. // update the label, otherwise when it is updated only on the first draw
  1025. // of the list, it is drawn "bold" for some reason
  1026. FTransferListHoverIndex = TransferList->ItemIndex;
  1027. UpdateTransferLabel();
  1028. }
  1029. __finally
  1030. {
  1031. TransferList->Strings->EndUpdate();
  1032. }
  1033. }
  1034. //---------------------------------------------------------------------------
  1035. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1036. {
  1037. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1038. DebugAssert(Toolbar != NULL);
  1039. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1043. {
  1044. TForm::UpdateActions();
  1045. if (ComponentVisible[fcCustomCommandsBand])
  1046. {
  1047. UpdateCustomCommandsToolbar();
  1048. }
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1052. {
  1053. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1054. }
  1055. //---------------------------------------------------------------------------
  1056. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1057. {
  1058. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1059. {
  1060. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1061. // Without this, the panel turns gray and unused part of header turns black
  1062. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1063. }
  1064. }
  1065. //---------------------------------------------------------------------------
  1066. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1067. {
  1068. Color = GetBtnFaceColor();
  1069. DebugAssert(Configuration && RemoteDirView);
  1070. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1071. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1072. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1073. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1074. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1075. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1076. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1077. UpdateRowSelect(RemoteDirView);
  1078. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1079. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1080. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1081. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1082. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1083. UpdateSessionsPageControlHeight();
  1084. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1085. UpdateToolbarDisplayMode();
  1086. UpdateTransferList();
  1087. if (Terminal != NULL)
  1088. {
  1089. TransferPresetAutoSelect();
  1090. }
  1091. UpdateQueueView();
  1092. UpdateControls();
  1093. // this can be called even before constuctor finishes.
  1094. if (FEditorManager != NULL)
  1095. {
  1096. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1097. }
  1098. // this can be called even before constuctor finishes.
  1099. if (FLocalEditors != NULL)
  1100. {
  1101. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1102. {
  1103. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1104. }
  1105. }
  1106. if (ComponentVisible[fcCustomCommandsBand])
  1107. {
  1108. UpdateCustomCommandsToolbar();
  1109. }
  1110. // show only when keeping queue items forever,
  1111. // otherwise, it's enough to have it in the context menu
  1112. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1113. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1114. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1115. {
  1116. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1117. FFileColorsCurrent = WinConfiguration->FileColors;
  1118. FileColorsChanged();
  1119. }
  1120. if ((GetNewTabTab()->CaptionTruncation != GetNewTabTabCaptionTruncation()) &&
  1121. (FSessionColors != NULL))
  1122. {
  1123. SessionListChanged();
  1124. }
  1125. if (GlyphsModule->LargerToolbar != WinConfiguration->LargerToolbar)
  1126. {
  1127. GlyphsModule->LargerToolbar = WinConfiguration->LargerToolbar;
  1128. if (FSessionColors != NULL)
  1129. {
  1130. RegenerateSessionColorsImageList();
  1131. UpdateSessionsPageControlHeight();
  1132. }
  1133. }
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1137. {
  1138. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1139. DirView->Invalidate();
  1140. }
  1141. //---------------------------------------------------------------------------
  1142. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1143. {
  1144. DoFileColorsChanged(RemoteDirView);
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1148. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1149. void * /*Arg*/)
  1150. {
  1151. if (Token != NULL)
  1152. {
  1153. TForm * Editor = dynamic_cast<TForm*>(Token);
  1154. DebugAssert(Editor != NULL);
  1155. ReconfigureEditorForm(Editor);
  1156. }
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1160. {
  1161. WinConfiguration->DDTransferConfirmation = asOn;
  1162. }
  1163. //---------------------------------------------------------------------------
  1164. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1165. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1166. {
  1167. // noop
  1168. }
  1169. //---------------------------------------------------------------------------
  1170. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1171. {
  1172. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1173. }
  1174. //---------------------------------------------------------------------------
  1175. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1176. {
  1177. if (DoNotShowAgain)
  1178. {
  1179. if (DragDrop)
  1180. {
  1181. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1182. {
  1183. PopupTrayBalloon(
  1184. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1185. }
  1186. WinConfiguration->DDTransferConfirmation = asOff;
  1187. }
  1188. else
  1189. {
  1190. WinConfiguration->ConfirmTransferring = false;
  1191. }
  1192. }
  1193. else
  1194. {
  1195. // User explicitly unchecked "do not show again",
  1196. // so show him the dialog the next time
  1197. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1198. {
  1199. WinConfiguration->DDTransferConfirmation = asOn;
  1200. }
  1201. }
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1205. TTransferDirection Direction, TTransferType Type, bool Temp,
  1206. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1207. bool Confirm, bool DragDrop, int Options)
  1208. {
  1209. bool Result = true;
  1210. DebugAssert(HasActiveTerminal());
  1211. // these parameters are known in advance
  1212. int Params =
  1213. FLAGMASK(Type == ttMove, cpDelete);
  1214. bool ToTemp = (Temp && (Direction == tdToLocal));
  1215. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1216. {
  1217. bool DisableNewerOnly =
  1218. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1219. ToTemp;
  1220. Options |=
  1221. FLAGMASK(ToTemp, coTemp) |
  1222. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1223. coDoNotShowAgain;
  1224. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1225. int CopyParamAttrs = (Direction == tdToRemote ?
  1226. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1227. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1228. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1229. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1230. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1231. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1232. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1233. if (Result)
  1234. {
  1235. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1236. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1237. }
  1238. }
  1239. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1240. {
  1241. Configuration->Usage->Inc(L"TransfersOnBackground");
  1242. // these parameter are known only after transfer dialog
  1243. Params |=
  1244. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1245. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1246. Result = false;
  1247. ClearTransferSourceSelection(Direction);
  1248. }
  1249. return Result;
  1250. }
  1251. //---------------------------------------------------------------------------
  1252. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1253. {
  1254. if (FOnFileOperationFinished != NULL)
  1255. {
  1256. FOnFileOperationFinished(Side, UnicodeString(), true, true);
  1257. }
  1258. else
  1259. {
  1260. if (HasDirView[Side])
  1261. {
  1262. DirView(Side)->SelectAll(smNone);
  1263. }
  1264. }
  1265. }
  1266. //---------------------------------------------------------------------------
  1267. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1268. {
  1269. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1270. ClearOperationSelection(Side);
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1274. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1275. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1276. int Params)
  1277. {
  1278. DebugAssert(Queue != NULL);
  1279. TQueueItem * QueueItem;
  1280. if (Direction == tdToRemote)
  1281. {
  1282. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1283. QueueItem =
  1284. new TUploadQueueItem(
  1285. Terminal, FileList, TargetDirectory, &CopyParam, Params, CopyParam.QueueParallel);
  1286. }
  1287. else
  1288. {
  1289. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1290. QueueItem =
  1291. new TDownloadQueueItem(
  1292. Terminal, FileList, TargetDirectory, &CopyParam, Params, CopyParam.QueueParallel);
  1293. }
  1294. AddQueueItem(Queue, QueueItem, Terminal);
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1298. {
  1299. if (Queue->IsEmpty)
  1300. {
  1301. Terminal->QueueOperationStart = Now();
  1302. }
  1303. Queue->AddItem(QueueItem);
  1304. }
  1305. //---------------------------------------------------------------------------
  1306. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1307. {
  1308. }
  1309. //---------------------------------------------------------------------------
  1310. void __fastcall TCustomScpExplorerForm::InitControls()
  1311. {
  1312. DebugAssert(FDocks != NULL);
  1313. for (int Index = 0; Index < ComponentCount; Index++)
  1314. {
  1315. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1316. if ((Dock != NULL) && (Dock->Tag == 0))
  1317. {
  1318. FDocks->Add(Dock);
  1319. }
  1320. }
  1321. CollectItemsWithTextDisplayMode(this);
  1322. }
  1323. //---------------------------------------------------------------------------
  1324. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1325. {
  1326. ConfigurationChanged();
  1327. QueuePanel->Height =
  1328. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1329. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1330. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1331. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1332. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1333. QueueFileList->Height =
  1334. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1335. if (QueueFileList->Visible)
  1336. {
  1337. AdjustQueueLayout();
  1338. }
  1339. UpdateCustomCommandsToolbar();
  1340. }
  1341. //---------------------------------------------------------------------------
  1342. void __fastcall TCustomScpExplorerForm::StoreParams()
  1343. {
  1344. int APixelsPerInch = GetControlPixelsPerInch(this);
  1345. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1346. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1347. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1348. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1349. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1350. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1351. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1352. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1353. }
  1354. //---------------------------------------------------------------------------
  1355. void __fastcall TCustomScpExplorerForm::Loaded()
  1356. {
  1357. // Loaded is called in more deterministic moment than previously used CreateParams
  1358. if (!FFormRestored)
  1359. {
  1360. FFormRestored = true;
  1361. RestoreFormParams();
  1362. }
  1363. TForm::Loaded();
  1364. }
  1365. //---------------------------------------------------------------------------
  1366. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1367. {
  1368. DebugAssert(FDocks != NULL);
  1369. for (int Index = 0; Index < FDocks->Count; Index++)
  1370. {
  1371. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1372. }
  1373. }
  1374. //---------------------------------------------------------------------------
  1375. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1376. {
  1377. SetDockAllowDrag(true);
  1378. while (!ButtonsStr.IsEmpty())
  1379. {
  1380. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1381. UnicodeString Name = CutToChar(S, L'=', true);
  1382. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1383. {
  1384. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1385. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1386. {
  1387. while (!S.IsEmpty())
  1388. {
  1389. UnicodeString S2 = CutToChar(S, L',', true);
  1390. UnicodeString Name = CutToChar(S2, L':', true);
  1391. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1392. {
  1393. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1394. if (SameText(GetToolbarItemName(Item), Name))
  1395. {
  1396. // forward compatibility
  1397. UnicodeString S3 = CutToChar(S2, L':', true);
  1398. bool Visible = StrToIntDef(S3, 0);
  1399. Item->Visible = Visible;
  1400. }
  1401. }
  1402. }
  1403. }
  1404. }
  1405. }
  1406. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1407. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1408. }
  1409. //---------------------------------------------------------------------------
  1410. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1411. {
  1412. UnicodeString Result;
  1413. SetDockAllowDrag(true);
  1414. Result = ::GetToolbarsLayoutStr(this);
  1415. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1416. return Result;
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1420. {
  1421. UnicodeString Result;
  1422. if (Item->Action != NULL)
  1423. {
  1424. Result = Item->Action->Name;
  1425. Result = RemoveSuffix(Result, L"Action", true);
  1426. }
  1427. else
  1428. {
  1429. Result = Item->Name;
  1430. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1431. Result = RemoveSuffix(Result, L"Item", true);
  1432. }
  1433. return Result;
  1434. }
  1435. //---------------------------------------------------------------------------
  1436. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1437. {
  1438. UnicodeString Result;
  1439. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1440. {
  1441. UnicodeString ToolbarStr;
  1442. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1443. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1444. {
  1445. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1446. {
  1447. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1448. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1449. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1450. // and save the changes here only.
  1451. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1452. {
  1453. UnicodeString Name = GetToolbarItemName(Item);
  1454. DebugAssert(Name.Pos(L"TBX") == 0);
  1455. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1456. }
  1457. }
  1458. }
  1459. if (!ToolbarStr.IsEmpty())
  1460. {
  1461. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1462. }
  1463. }
  1464. return Result;
  1465. }
  1466. //---------------------------------------------------------------------------
  1467. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1468. {
  1469. DebugAssert(FProgressForm == NULL);
  1470. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1471. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1472. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1473. FProgressForm->DeleteLocalToRecycleBin =
  1474. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1475. FProgressForm->DeleteRemoteToRecycleBin =
  1476. HasActiveTerminal() &&
  1477. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1478. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1479. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1480. LockWindow();
  1481. // Actually, do not know what hides the progress once the operation finishes
  1482. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1483. SetTaskbarListProgressState(TBPF_NORMAL);
  1484. }
  1485. //---------------------------------------------------------------------------
  1486. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1487. {
  1488. UnlockWindow();
  1489. SAFE_DESTROY(FProgressForm);
  1490. SetQueueProgress();
  1491. }
  1492. //---------------------------------------------------------------------------
  1493. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1494. TFileOperationProgressType & ProgressData)
  1495. {
  1496. // Download to temporary local directory
  1497. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1498. {
  1499. bool Continue = true;
  1500. FPendingTempSpaceWarn = false;
  1501. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1502. if (!Continue)
  1503. {
  1504. Abort();
  1505. }
  1506. }
  1507. // operation is being executed and we still didn't show up progress form
  1508. if (ProgressData.InProgress && !FProgressForm)
  1509. {
  1510. CreateProgressForm(NULL);
  1511. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1512. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1513. }
  1514. // operation is finished (or terminated), so we hide progress form
  1515. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1516. {
  1517. DestroyProgressForm();
  1518. if (ProgressData.Operation == foCalculateSize)
  1519. {
  1520. // When calculating size before transfer, the abort caused by
  1521. // cancel flag set due to "MoveToQueue" below,
  1522. // is not propagated back to us in ExecuteFileOperation,
  1523. // as it is silently swallowed.
  1524. // So we explicitly (re)throw it here.
  1525. if (FMoveToQueue)
  1526. {
  1527. Abort();
  1528. }
  1529. }
  1530. else
  1531. {
  1532. if ((ProgressData.Cancel == csContinue) &&
  1533. !FAutoOperation)
  1534. {
  1535. OperationComplete(ProgressData.StartTime);
  1536. }
  1537. }
  1538. }
  1539. if (FProgressForm &&
  1540. // Can happen only during synchronization, when ending one of the operations
  1541. ProgressData.InProgress)
  1542. {
  1543. FProgressForm->SetProgressData(ProgressData);
  1544. DebugAssert(ProgressData.InProgress);
  1545. SetTaskbarListProgressValue(&ProgressData);
  1546. if (FProgressForm->Cancel > csContinue)
  1547. {
  1548. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1549. if (FProgressForm->Cancel == csCancelFile)
  1550. {
  1551. FProgressForm->ClearCancel();
  1552. }
  1553. // cancel cancels even the move
  1554. FMoveToQueue = false;
  1555. }
  1556. else if (FProgressForm->MoveToQueue)
  1557. {
  1558. FMoveToQueue = true;
  1559. ProgressData.SetCancelAtLeast(csCancel);
  1560. }
  1561. if ((FTransferResumeList != NULL) &&
  1562. ProgressData.InProgress &&
  1563. ProgressData.IsTransfer() &&
  1564. !ProgressData.FullFileName.IsEmpty())
  1565. {
  1566. if ((FTransferResumeList->Count == 0) ||
  1567. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1568. {
  1569. // note that we do not recognize directories from files here
  1570. FTransferResumeList->Add(ProgressData.FullFileName);
  1571. }
  1572. }
  1573. }
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1577. const TDateTime & StartTime)
  1578. {
  1579. if (GUIConfiguration->BeepOnFinish &&
  1580. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1581. {
  1582. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1583. DWORD Sound;
  1584. if (!ExtractFileExt(BeepSound).IsEmpty())
  1585. {
  1586. Sound = SND_FILENAME;
  1587. }
  1588. else
  1589. {
  1590. Sound = SND_ALIAS;
  1591. }
  1592. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1593. }
  1594. }
  1595. //---------------------------------------------------------------------------
  1596. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1597. TFileOperationProgressType & ProgressData)
  1598. {
  1599. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1600. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1601. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1602. FileOperationProgress(ProgressData);
  1603. }
  1604. //---------------------------------------------------------------------------
  1605. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1606. {
  1607. UnicodeString Result;
  1608. if (FProgressForm != NULL)
  1609. {
  1610. Result = FProgressForm->ProgressStr();
  1611. }
  1612. return Result;
  1613. }
  1614. //---------------------------------------------------------------------------
  1615. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1616. bool DragDrop)
  1617. {
  1618. return (!DragDrop && (DropSourceControl == NULL)) ||
  1619. (DropSourceControl == DirView(osOther));
  1620. }
  1621. //---------------------------------------------------------------------------
  1622. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1623. {
  1624. TCustomDirView * ADirView = DirView(Side);
  1625. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1626. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1627. // this can happen when local drive is unplugged in the middle of the operation
  1628. if (Item != NULL)
  1629. {
  1630. if (Unselect)
  1631. {
  1632. Item->Selected = false;
  1633. }
  1634. ADirView->MakeProgressVisible(Item);
  1635. }
  1636. return Item;
  1637. }
  1638. //---------------------------------------------------------------------------
  1639. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1640. TFileOperation Operation, TOperationSide Side,
  1641. bool /*Temp*/, const UnicodeString & FileName, bool Success, bool NotCancelled,
  1642. TOnceDoneOperation & OnceDoneOperation)
  1643. {
  1644. if (!FAutoOperation)
  1645. {
  1646. // no selection on "/upload", form serves only as event handler
  1647. // (it is not displayed)
  1648. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1649. ((Operation == FPrimaryOperation) ||
  1650. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1651. {
  1652. TOperationSide DViewSide;
  1653. if ((FProgressSide != osCurrent) &&
  1654. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1655. {
  1656. // assume the operation is over the focused panel
  1657. DViewSide = FProgressSide;
  1658. }
  1659. else
  1660. {
  1661. DViewSide = Side;
  1662. }
  1663. if (FOnFileOperationFinished != NULL)
  1664. {
  1665. FOnFileOperationFinished(DViewSide, FileName, Success, NotCancelled);
  1666. }
  1667. else
  1668. {
  1669. VisualiseOperationFinished(DViewSide, FileName, Success && NotCancelled);
  1670. }
  1671. }
  1672. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1673. {
  1674. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1675. {
  1676. if (Side == osLocal)
  1677. {
  1678. Configuration->Usage->Inc(L"UploadedFiles");
  1679. }
  1680. else
  1681. {
  1682. Configuration->Usage->Inc(L"DownloadedFiles");
  1683. }
  1684. }
  1685. }
  1686. }
  1687. if (Success && (FSynchronizeController != NULL))
  1688. {
  1689. DebugAssert(!IsSideLocalBrowser(osRemote));
  1690. if (Operation == foCopy)
  1691. {
  1692. DebugAssert(Side == osLocal);
  1693. FSynchronizeController->LogOperation(soUpload, FileName);
  1694. }
  1695. else if (Operation == foDelete)
  1696. {
  1697. DebugAssert(Side == osRemote);
  1698. FSynchronizeController->LogOperation(soDelete, FileName);
  1699. }
  1700. }
  1701. if (FProgressForm)
  1702. {
  1703. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1704. }
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1708. TFileOperation Operation, TOperationSide Side,
  1709. bool Temp, const UnicodeString & FileName, bool Success, bool NotCancelled,
  1710. TOnceDoneOperation & OnceDoneOperation)
  1711. {
  1712. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1713. DoOperationFinished(Operation, Side, Temp, FileName, Success, NotCancelled, OnceDoneOperation);
  1714. }
  1715. //---------------------------------------------------------------------------
  1716. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1717. {
  1718. return false;
  1719. }
  1720. //---------------------------------------------------------------------------
  1721. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1722. {
  1723. return false;
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1727. {
  1728. return false;
  1729. }
  1730. //---------------------------------------------------------------------------
  1731. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1732. {
  1733. return NULL;
  1734. }
  1735. //---------------------------------------------------------------------------
  1736. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1737. {
  1738. DebugAssert(GetSide(Side) == osRemote);
  1739. DebugUsedParam(Side);
  1740. return RemoteDirView;
  1741. }
  1742. //---------------------------------------------------------------------------
  1743. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1744. {
  1745. DebugAssert(GetSide(Side) == osRemote);
  1746. DebugUsedParam(Side);
  1747. return RemoteDriveView;
  1748. }
  1749. //---------------------------------------------------------------------------
  1750. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1751. {
  1752. DebugAssert(GetSide(Side) == osRemote);
  1753. DebugUsedParam(Side);
  1754. return HasActiveTerminal();
  1755. }
  1756. //---------------------------------------------------------------------------
  1757. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1758. TOperationSide Side, int FilesOnly)
  1759. {
  1760. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1761. }
  1762. //---------------------------------------------------------------------------
  1763. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1764. TOperationSide Side, int FilesOnly)
  1765. {
  1766. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1767. }
  1768. //---------------------------------------------------------------------------
  1769. struct THistoryItemData
  1770. {
  1771. short int Side;
  1772. short int Index;
  1773. };
  1774. //---------------------------------------------------------------------------
  1775. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1776. {
  1777. DirView(Side)->HistoryGo(Index);
  1778. }
  1779. //---------------------------------------------------------------------------
  1780. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1781. {
  1782. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1783. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1784. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1785. }
  1786. //---------------------------------------------------------------------------
  1787. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1788. bool Back)
  1789. {
  1790. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1791. {
  1792. TCustomDirView * DView = DirView(Side);
  1793. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1794. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1795. if (ICount > 10)
  1796. {
  1797. ICount = 10;
  1798. }
  1799. Menu->Items->Clear();
  1800. THistoryItemData Data;
  1801. Data.Side = Side;
  1802. for (short int i = 1; i <= ICount; i++)
  1803. {
  1804. TTBCustomItem * Item = new TTBXItem(Menu);
  1805. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1806. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1807. Item->Hint = DView->HistoryPath[Data.Index];
  1808. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1809. Item->Tag = *reinterpret_cast<int*>(&Data);
  1810. Item->OnClick = HistoryItemClick;
  1811. Menu->Items->Add(Item);
  1812. }
  1813. }
  1814. }
  1815. //---------------------------------------------------------------------------
  1816. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1817. TOperationSide Side, bool Back)
  1818. {
  1819. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1820. {
  1821. // workaround
  1822. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1823. // operator new allocates memory in Pascal code, but calls inline
  1824. // constructor in C++, leading in problems, the function does
  1825. // both in Pascal code
  1826. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1827. UpdateHistoryMenu(Side, Back);
  1828. }
  1829. return FHistoryMenu[Side == osLocal][Back];
  1830. }
  1831. //---------------------------------------------------------------------------
  1832. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1833. TCustomDirView *Sender)
  1834. {
  1835. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1836. UpdateHistoryMenu(Side, true);
  1837. UpdateHistoryMenu(Side, false);
  1838. }
  1839. //---------------------------------------------------------------------------
  1840. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1841. {
  1842. // remote custom commands can be executed only if the server supports shell commands
  1843. // or have secondary shell
  1844. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1845. }
  1846. //---------------------------------------------------------------------------
  1847. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1848. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1849. {
  1850. // -1 = hidden, 0 = disabled, 1 = enabled
  1851. int Result;
  1852. TFileCustomCommand RemoteCustomCommand;
  1853. TLocalCustomCommand LocalCustomCommand;
  1854. TFileCustomCommand * NonInteractiveCustomCommand =
  1855. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1856. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1857. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1858. if (FLAGCLEAR(Command.Params, ccLocal))
  1859. {
  1860. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1861. // custom command that does not operate with files can be executed anytime ...
  1862. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1863. {
  1864. // ... but do not show such command in remote file menu
  1865. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1866. {
  1867. Result = AllowedState;
  1868. }
  1869. else
  1870. {
  1871. Result = -1;
  1872. }
  1873. }
  1874. else
  1875. {
  1876. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1877. {
  1878. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1879. }
  1880. else
  1881. {
  1882. Result = -1;
  1883. }
  1884. }
  1885. }
  1886. else
  1887. {
  1888. // custom command that does not operate with files can be executed anytime
  1889. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1890. {
  1891. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1892. }
  1893. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1894. {
  1895. if ((ListType == ccltAll) || (ListType == ccltFile))
  1896. {
  1897. // special case is "diff"-style command that can be executed over any side,
  1898. // if we have both sides
  1899. Result =
  1900. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1901. // directly (instead of EnableSelectedOperation) to pass
  1902. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1903. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1904. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1905. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1906. }
  1907. else if (ListType == ccltBoth)
  1908. {
  1909. DebugAssert(!IsLocalBrowserMode());
  1910. Result = 1;
  1911. }
  1912. else
  1913. {
  1914. Result = -1;
  1915. }
  1916. }
  1917. else
  1918. {
  1919. if ((ListType == ccltAll) ||
  1920. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1921. {
  1922. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1923. }
  1924. else
  1925. {
  1926. Result = -1;
  1927. }
  1928. }
  1929. if ((Result > 0) &&
  1930. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1931. !HasActiveTerminal())
  1932. {
  1933. Result = 0;
  1934. }
  1935. }
  1936. return Result;
  1937. }
  1938. //---------------------------------------------------------------------------
  1939. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1940. TStrings * FileList, const TCustomCommandType & ACommand,
  1941. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1942. {
  1943. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1944. {
  1945. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1946. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1947. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1948. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1949. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1950. bool Capture =
  1951. FLAGSET(ACommand.Params, ccShowResults) ||
  1952. FLAGSET(ACommand.Params, ccCopyResults) ||
  1953. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1954. TCaptureOutputEvent OutputEvent = NULL;
  1955. DebugAssert(FCapturedLog == NULL);
  1956. if (Capture)
  1957. {
  1958. FCapturedLog = new TStringList();
  1959. OutputEvent = TerminalCaptureLog;
  1960. }
  1961. try
  1962. {
  1963. if (!RemoteCustomCommand.IsFileCommand(Command))
  1964. {
  1965. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1966. OutputEvent);
  1967. }
  1968. else
  1969. {
  1970. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1971. }
  1972. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1973. {
  1974. if (FLAGSET(ACommand.Params, ccCopyResults))
  1975. {
  1976. CopyToClipboard(FCapturedLog);
  1977. }
  1978. if (FLAGSET(ACommand.Params, ccShowResults))
  1979. {
  1980. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1981. }
  1982. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1983. {
  1984. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1985. }
  1986. }
  1987. }
  1988. __finally
  1989. {
  1990. SAFE_DESTROY(FCapturedLog);
  1991. }
  1992. }
  1993. }
  1994. //---------------------------------------------------------------------------
  1995. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1996. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1997. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1998. {
  1999. DebugAssert(!IsLocalBrowserMode());
  2000. TStrings * LocalFileList = NULL;
  2001. TStrings * RemoteFileList = NULL;
  2002. TStrings * RemoteFileListFull = NULL;
  2003. try
  2004. {
  2005. if (LocalFileCommand)
  2006. {
  2007. if (ALocalFileList == NULL)
  2008. {
  2009. DebugAssert(HasDirView[osLocal]);
  2010. // Cannot have focus on both panels, so we have to call AnyFileSelected
  2011. // directly (instead of EnableSelectedOperation) to pass
  2012. // false to FocusedFileOnlyWhenFocused
  2013. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  2014. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  2015. }
  2016. else
  2017. {
  2018. LocalFileList = ALocalFileList;
  2019. }
  2020. if (FileListCommand)
  2021. {
  2022. if (LocalFileList->Count != 1)
  2023. {
  2024. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  2025. }
  2026. }
  2027. else
  2028. {
  2029. if ((LocalFileList->Count != 1) &&
  2030. (FileList->Count != 1) &&
  2031. (LocalFileList->Count != FileList->Count))
  2032. {
  2033. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2034. }
  2035. }
  2036. }
  2037. UnicodeString RootTempDir;
  2038. UnicodeString TempDir;
  2039. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2040. if (!RemoteFiles)
  2041. {
  2042. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2043. }
  2044. try
  2045. {
  2046. TDateTimes RemoteFileTimes;
  2047. if (RemoteFiles)
  2048. {
  2049. RemoteFileList = FileList;
  2050. }
  2051. else
  2052. {
  2053. RemoteFileList = new TStringList();
  2054. TMakeLocalFileListParams MakeFileListParam;
  2055. MakeFileListParam.FileList = RemoteFileList;
  2056. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2057. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2058. MakeFileListParam.Recursive =
  2059. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2060. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2061. if (!MakeFileListParam.Recursive)
  2062. {
  2063. RemoteFileTimes.clear();
  2064. RemoteFileListFull = new TStringList();
  2065. MakeFileListParam.FileList = RemoteFileListFull;
  2066. MakeFileListParam.Recursive = true;
  2067. MakeFileListParam.IncludeDirs = false;
  2068. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2069. }
  2070. else
  2071. {
  2072. RemoteFileListFull = RemoteFileList;
  2073. }
  2074. }
  2075. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2076. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2077. if (!NonBlocking)
  2078. {
  2079. int Count;
  2080. if (FileListCommand)
  2081. {
  2082. Count = 1;
  2083. }
  2084. else if (LocalFileCommand)
  2085. {
  2086. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2087. }
  2088. else
  2089. {
  2090. Count = RemoteFileList->Count;
  2091. }
  2092. Progress.Start(foCustomCommand, osRemote, Count);
  2093. DebugAssert(FProgressForm != NULL);
  2094. FProgressForm->ReadOnly = true;
  2095. }
  2096. try
  2097. {
  2098. if (FileListCommand)
  2099. {
  2100. UnicodeString LocalFile;
  2101. // MakeFileList does not delimit filenames
  2102. UnicodeString FileList = MakeFileList(RemoteFileList);
  2103. if (LocalFileCommand)
  2104. {
  2105. DebugAssert(LocalFileList->Count == 1);
  2106. LocalFile = LocalFileList->Strings[0];
  2107. }
  2108. TLocalCustomCommand CustomCommand(Data,
  2109. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2110. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2111. if (NonBlocking)
  2112. {
  2113. DebugAssert(!POutput);
  2114. ExecuteShellChecked(ShellCommand);
  2115. }
  2116. else
  2117. {
  2118. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2119. }
  2120. }
  2121. else if (LocalFileCommand)
  2122. {
  2123. if (LocalFileList->Count == 1)
  2124. {
  2125. UnicodeString LocalFile = LocalFileList->Strings[0];
  2126. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2127. {
  2128. UnicodeString FileName = RemoteFileList->Strings[Index];
  2129. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2130. Progress.SetFile(FileNameForProgress);
  2131. TLocalCustomCommand CustomCommand(Data,
  2132. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2133. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2134. TOnceDoneOperation OnceDoneOperation;
  2135. // Do not unselect anything, as it won't work with two panels anyway
  2136. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2137. }
  2138. }
  2139. else if (RemoteFileList->Count == 1)
  2140. {
  2141. UnicodeString FileName = RemoteFileList->Strings[0];
  2142. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2143. {
  2144. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2145. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2146. Progress.SetFile(LocalFileNameOnly);
  2147. TLocalCustomCommand CustomCommand(
  2148. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2149. FileName, LocalFileName, L"");
  2150. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2151. TOnceDoneOperation OnceDoneOperation;
  2152. // Do not unselect anything, as it won't work with two panels anyway
  2153. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2154. }
  2155. }
  2156. else
  2157. {
  2158. if (LocalFileList->Count != RemoteFileList->Count)
  2159. {
  2160. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2161. }
  2162. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2163. {
  2164. UnicodeString FileName = RemoteFileList->Strings[Index];
  2165. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2166. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2167. Progress.SetFile(LocalFileNameOnly); // sic
  2168. TLocalCustomCommand CustomCommand(
  2169. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2170. FileName, LocalFileName, L"");
  2171. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2172. TOnceDoneOperation OnceDoneOperation;
  2173. // Do not unselect anything, as it won't work with two panels anyway
  2174. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2175. }
  2176. }
  2177. }
  2178. else
  2179. {
  2180. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2181. {
  2182. UnicodeString FileName = RemoteFileList->Strings[Index];
  2183. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2184. Progress.SetFile(FileNameForProgress);
  2185. TLocalCustomCommand CustomCommand(Data,
  2186. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2187. FileName, L"", L"");
  2188. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2189. TOnceDoneOperation OnceDoneOperation;
  2190. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2191. }
  2192. }
  2193. }
  2194. __finally
  2195. {
  2196. if (!NonBlocking)
  2197. {
  2198. Progress.Stop();
  2199. }
  2200. }
  2201. DebugAssert(!FAutoOperation);
  2202. if (!RemoteFiles)
  2203. {
  2204. TempDir = IncludeTrailingBackslash(TempDir);
  2205. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2206. {
  2207. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2208. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2209. // Skip directories, process only nested files.
  2210. // The check is redundant as FileAge fails for directories anyway.
  2211. !DirectoryExists(FileName))
  2212. {
  2213. UnicodeString RemoteDir =
  2214. UnixExtractFileDir(
  2215. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2216. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2217. TSearchRecSmart SearchRec;
  2218. if (FileSearchRec(FileName, SearchRec) &&
  2219. (SearchRec.GetLastWriteTime() != RemoteFileTimes[Index]))
  2220. {
  2221. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2222. TemporaryFileCopyParam(CopyParam);
  2223. CopyParam.FileMask = L"";
  2224. FAutoOperation = true;
  2225. FMoveToQueue = false;
  2226. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2227. TemporaryFilesList->Add(FileName);
  2228. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2229. }
  2230. }
  2231. }
  2232. }
  2233. }
  2234. __finally
  2235. {
  2236. FAutoOperation = false;
  2237. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2238. {
  2239. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  2240. }
  2241. }
  2242. }
  2243. __finally
  2244. {
  2245. if (RemoteFileList != FileList)
  2246. {
  2247. delete RemoteFileList;
  2248. }
  2249. if (LocalFileList != ALocalFileList)
  2250. {
  2251. delete LocalFileList;
  2252. }
  2253. if (RemoteFileListFull != RemoteFileList)
  2254. {
  2255. delete RemoteFileListFull;
  2256. }
  2257. }
  2258. }
  2259. //---------------------------------------------------------------------------
  2260. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2261. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2262. bool FileListCommand, UnicodeString * POutput)
  2263. {
  2264. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2265. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2266. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2267. {
  2268. UnicodeString FileName = SelectedFileList->Strings[Index];
  2269. if (DirectoryExists(FileName))
  2270. {
  2271. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2272. {
  2273. LocalFileList->Add(FileName);
  2274. }
  2275. if (FLAGSET(ACommand.Params, ccRecursive))
  2276. {
  2277. TMakeLocalFileListParams MakeFileListParam;
  2278. MakeFileListParam.FileList = LocalFileList.get();
  2279. MakeFileListParam.FileTimes = NULL;
  2280. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2281. MakeFileListParam.Recursive = true;
  2282. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2283. }
  2284. }
  2285. else
  2286. {
  2287. LocalFileList->Add(FileName);
  2288. }
  2289. }
  2290. UnicodeString RemotePath;
  2291. if (Terminal != NULL)
  2292. {
  2293. RemotePath = Terminal->CurrentDirectory;
  2294. }
  2295. if (FileListCommand)
  2296. {
  2297. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2298. TLocalCustomCommand CustomCommand(
  2299. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2300. L"", L"", FileList);
  2301. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2302. }
  2303. else
  2304. {
  2305. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2306. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2307. DebugAssert(FProgressForm != NULL);
  2308. FProgressForm->ReadOnly = true;
  2309. try
  2310. {
  2311. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2312. {
  2313. UnicodeString FileName = LocalFileList->Strings[Index];
  2314. Progress.SetFile(FileName);
  2315. TLocalCustomCommand CustomCommand(
  2316. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2317. FileName, L"", L"");
  2318. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2319. TOnceDoneOperation OnceDoneOperation;
  2320. Progress.Finish(FileName, true, OnceDoneOperation);
  2321. }
  2322. }
  2323. __finally
  2324. {
  2325. Progress.Stop();
  2326. }
  2327. }
  2328. }
  2329. //---------------------------------------------------------------------------
  2330. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2331. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2332. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2333. {
  2334. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2335. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2336. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2337. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2338. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2339. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2340. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2341. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2342. std::unique_ptr<UnicodeString> POutput;
  2343. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2344. FLAGSET(ACommand.Params, ccCopyResults))
  2345. {
  2346. POutput.reset(new UnicodeString());
  2347. }
  2348. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  2349. if (!LocalCustomCommand.IsFileCommand(Command))
  2350. {
  2351. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2352. }
  2353. // remote files?
  2354. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2355. {
  2356. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2357. }
  2358. // local files
  2359. else
  2360. {
  2361. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2362. }
  2363. if (POutput.get() != NULL)
  2364. {
  2365. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2366. int P = POutput->Pos(sLineBreak);
  2367. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2368. {
  2369. POutput->SetLength(P - 1);
  2370. }
  2371. // Copy even empty output to clipboard
  2372. if (FLAGSET(ACommand.Params, ccCopyResults))
  2373. {
  2374. CopyToClipboard(*POutput);
  2375. }
  2376. // But do not show an empty message box.
  2377. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2378. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2379. !POutput->IsEmpty())
  2380. {
  2381. TClipboardHandler ClipboardHandler;
  2382. ClipboardHandler.Text = *POutput;
  2383. TMessageParams Params;
  2384. TQueryButtonAlias Aliases[1];
  2385. Aliases[0].Button = qaRetry;
  2386. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2387. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2388. Params.Aliases = Aliases;
  2389. Params.AliasesCount = LENOF(Aliases);
  2390. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2391. }
  2392. }
  2393. }
  2394. //---------------------------------------------------------------------------
  2395. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2396. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2397. {
  2398. TCustomCommandData Data;
  2399. UnicodeString Site;
  2400. UnicodeString RemotePath;
  2401. if (HasActiveTerminal())
  2402. {
  2403. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2404. Data = TCustomCommandData(SessionData.get());
  2405. Site = Terminal->SessionData->SessionKey;
  2406. RemotePath = Terminal->CurrentDirectory;
  2407. }
  2408. UnicodeString HelpKeyword = ACommand.HomePage;
  2409. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2410. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2411. {
  2412. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2413. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2414. {
  2415. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2416. }
  2417. else
  2418. {
  2419. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2420. }
  2421. if (!DoCustomCommandOptionsDialog(
  2422. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2423. {
  2424. Abort();
  2425. }
  2426. }
  2427. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2428. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2429. {
  2430. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2431. }
  2432. else
  2433. {
  2434. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2435. }
  2436. }
  2437. //---------------------------------------------------------------------------
  2438. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2439. const TCustomCommandType & Command)
  2440. {
  2441. DebugAssert(FCustomCommandLocalFileList != NULL);
  2442. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2443. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2444. TStrings * LocalFileList = new TStringList();
  2445. TStrings * RemoteFileList = new TStringList();
  2446. try
  2447. {
  2448. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2449. {
  2450. LocalFileList->Clear();
  2451. LocalFileList->AddObject(
  2452. FCustomCommandLocalFileList->Strings[Index],
  2453. FCustomCommandLocalFileList->Objects[Index]);
  2454. RemoteFileList->Clear();
  2455. RemoteFileList->AddObject(
  2456. FCustomCommandRemoteFileList->Strings[Index],
  2457. FCustomCommandRemoteFileList->Objects[Index]);
  2458. CustomCommand(RemoteFileList, Command, LocalFileList);
  2459. }
  2460. }
  2461. __finally
  2462. {
  2463. delete LocalFileList;
  2464. delete RemoteFileList;
  2465. }
  2466. }
  2467. //---------------------------------------------------------------------------
  2468. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2469. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2470. {
  2471. delete FCustomCommandLocalFileList;
  2472. delete FCustomCommandRemoteFileList;
  2473. // takeover ownership,
  2474. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2475. FCustomCommandLocalFileList = LocalFileList;
  2476. FCustomCommandRemoteFileList = RemoteFileList;
  2477. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2478. if (Button != NULL)
  2479. {
  2480. FCustomCommandMenu->Items->Clear();
  2481. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2482. MenuPopup(FCustomCommandMenu, Button);
  2483. }
  2484. else
  2485. {
  2486. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2487. }
  2488. }
  2489. //---------------------------------------------------------------------------
  2490. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2491. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2492. {
  2493. DebugAssert(FCapturedLog != NULL);
  2494. if ((OutputType == cotOutput) || (OutputType == cotError))
  2495. {
  2496. FCapturedLog->Add(AddedLine);
  2497. }
  2498. }
  2499. //---------------------------------------------------------------------------
  2500. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2501. TOperationSide Side)
  2502. {
  2503. return
  2504. (Side == osOther) &&
  2505. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2506. }
  2507. //---------------------------------------------------------------------------
  2508. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2509. TOperationSide Side, TTBXCustomItem * Item,
  2510. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2511. {
  2512. TCustomDirView * DView = DirView(Side);
  2513. bool IsDefault = false;
  2514. if (DView->ItemFocused != NULL)
  2515. {
  2516. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2517. TResolvedDoubleClickAction DefaultAction =
  2518. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2519. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2520. }
  2521. TTBItemOptions O = Item->Options;
  2522. if (IsDefault)
  2523. {
  2524. Item->Options = O << tboDefault;
  2525. }
  2526. else
  2527. {
  2528. Item->Options = O >> tboDefault;
  2529. }
  2530. }
  2531. //---------------------------------------------------------------------------
  2532. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2533. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2534. {
  2535. TCustomDirView * DView = DirView(Side);
  2536. TListItem * Item = DView->ItemFocused;
  2537. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2538. EnableFocusedOperation[Side])
  2539. {
  2540. TPoint ClientPoint;
  2541. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2542. TPoint(0, 0) : MousePos;
  2543. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2544. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2545. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2546. }
  2547. }
  2548. //---------------------------------------------------------------------------
  2549. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2550. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2551. {
  2552. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2553. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2554. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2555. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2556. Handled = true;
  2557. }
  2558. //---------------------------------------------------------------------------
  2559. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2560. {
  2561. }
  2562. //---------------------------------------------------------------------------
  2563. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2564. {
  2565. DebugAssert(FErrorList == NULL);
  2566. if (WinConfiguration->ContinueOnError)
  2567. {
  2568. FErrorList = new TStringList();
  2569. Configuration->Usage->Inc(L"ContinuationsOnError");
  2570. }
  2571. NonVisualDataModule->StartBusy();
  2572. }
  2573. //---------------------------------------------------------------------------
  2574. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2575. {
  2576. NonVisualDataModule->EndBusy();
  2577. if (FErrorList)
  2578. {
  2579. HandleErrorList(FErrorList);
  2580. }
  2581. }
  2582. //---------------------------------------------------------------------------
  2583. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2584. const TCopyParamType & CopyParam)
  2585. {
  2586. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2587. {
  2588. Configuration->Usage->Inc(L"FileMaskUses");
  2589. }
  2590. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2591. {
  2592. Configuration->Usage->Inc(L"OperationMaskUses");
  2593. }
  2594. }
  2595. //---------------------------------------------------------------------------
  2596. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2597. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2598. {
  2599. DebugAssert(!IsLocalBrowserMode());
  2600. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2601. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2602. TTransferOperationParam DefaultParam;
  2603. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2604. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2605. switch (Param.Queue)
  2606. {
  2607. case asOn:
  2608. CopyParam.Queue = true;
  2609. break;
  2610. case asOff:
  2611. CopyParam.Queue = false;
  2612. break;
  2613. case asAuto:
  2614. default:
  2615. // keep default
  2616. break;
  2617. }
  2618. bool Result =
  2619. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2620. if (Result)
  2621. {
  2622. DebugAssert(Terminal);
  2623. bool SelectionRestored = false;
  2624. TCustomDirView * DView = NULL;
  2625. if (HasDirView[Side])
  2626. {
  2627. DView = DirView(Side);
  2628. DView->SaveSelection();
  2629. DView->SaveSelectedNames();
  2630. }
  2631. UpdateCopyParamCounters(CopyParam);
  2632. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2633. DebugAssert(FTransferResumeList == NULL);
  2634. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2635. FMoveToQueue = false;
  2636. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2637. try
  2638. {
  2639. TStrings * PermanentFileList;
  2640. std::unique_ptr<TStrings> PermanentFileListOwner;
  2641. try
  2642. {
  2643. if (Side == osLocal)
  2644. {
  2645. PermanentFileList = FileList;
  2646. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2647. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2648. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2649. if (Operation == foMove)
  2650. {
  2651. ReloadLocalDirectory();
  2652. if (DView != NULL)
  2653. {
  2654. DView->RestoreSelection();
  2655. }
  2656. SelectionRestored = true;
  2657. }
  2658. }
  2659. else
  2660. {
  2661. // Clone the file list as it may refer to current directory files,
  2662. // which get destroyed, when the source directory is reloaded after foMove operation.
  2663. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2664. // But for a hotfix, we are not going to do such a big change.
  2665. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2666. PermanentFileList = PermanentFileListOwner.get();
  2667. try
  2668. {
  2669. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2670. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2671. }
  2672. __finally
  2673. {
  2674. if (Operation == foMove)
  2675. {
  2676. if (DView != NULL)
  2677. {
  2678. DView->RestoreSelection();
  2679. }
  2680. SelectionRestored = true;
  2681. }
  2682. ReloadLocalDirectory(Param.TargetDirectory);
  2683. }
  2684. }
  2685. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2686. if (FMoveToQueue)
  2687. {
  2688. Abort();
  2689. }
  2690. }
  2691. catch (EAbort &)
  2692. {
  2693. if (FMoveToQueue)
  2694. {
  2695. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2696. DebugAssert(CopyParam.TransferSkipList == NULL);
  2697. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2698. if (TransferResumeList->Count > 0)
  2699. {
  2700. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2701. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2702. }
  2703. CopyParam.TransferSkipList = TransferResumeList.release();
  2704. // not really needed, just to keep it consistent with TransferResumeList
  2705. FTransferResumeList = NULL;
  2706. FMoveToQueue = false;
  2707. Configuration->Usage->Inc("MovesToBackground");
  2708. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2709. ClearTransferSourceSelection(Direction);
  2710. }
  2711. throw;
  2712. }
  2713. }
  2714. __finally
  2715. {
  2716. if (!SelectionRestored && (DView != NULL))
  2717. {
  2718. DView->DiscardSavedSelection();
  2719. }
  2720. FTransferResumeList = NULL;
  2721. }
  2722. }
  2723. return Result;
  2724. }
  2725. //---------------------------------------------------------------------------
  2726. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2727. TOperationSide Side, TStrings * FileList, void * Param)
  2728. {
  2729. DebugAssert(FileList->Count);
  2730. // We deliberately do not toggle alternative flag (Param), but use OR,
  2731. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2732. // shortcut of CurrentDeleteAlternativeAction
  2733. bool Alternative =
  2734. bool(Param) || UseAlternativeFunction();
  2735. bool Recycle;
  2736. if (IsSideLocalBrowser(Side))
  2737. {
  2738. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2739. }
  2740. else
  2741. {
  2742. Recycle =
  2743. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2744. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2745. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2746. }
  2747. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2748. if (!Result)
  2749. {
  2750. UnicodeString Query;
  2751. if (FileList->Count == 1)
  2752. {
  2753. if (IsSideLocalBrowser(Side))
  2754. {
  2755. Query = ExtractFileName(FileList->Strings[0]);
  2756. }
  2757. else
  2758. {
  2759. Query = UnixExtractFileName(FileList->Strings[0]);
  2760. }
  2761. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2762. }
  2763. else
  2764. {
  2765. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2766. }
  2767. TMessageParams Params(mpNeverAskAgainCheck);
  2768. Params.ImageName = L"Delete file";
  2769. unsigned int Answer =
  2770. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2771. if (Answer == qaNeverAskAgain)
  2772. {
  2773. Result = true;
  2774. if (Recycle)
  2775. {
  2776. WinConfiguration->ConfirmRecycling = false;
  2777. }
  2778. else
  2779. {
  2780. WinConfiguration->ConfirmDeleting = false;
  2781. }
  2782. }
  2783. else
  2784. {
  2785. Result = (Answer == qaOK);
  2786. }
  2787. }
  2788. if (Result)
  2789. {
  2790. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2791. }
  2792. return Result;
  2793. }
  2794. //---------------------------------------------------------------------------
  2795. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2796. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2797. {
  2798. TAutoBatch AutoBatch(this);
  2799. bool Result;
  2800. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2801. {
  2802. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2803. }
  2804. else if (Operation == foRename)
  2805. {
  2806. DebugAssert(DirView(Side)->ItemFocused);
  2807. DirView(Side)->ItemFocused->EditCaption();
  2808. Result = true;
  2809. }
  2810. else if (Operation == foDelete)
  2811. {
  2812. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2813. }
  2814. else if (Operation == foSetProperties)
  2815. {
  2816. DirView(osRemote)->SaveSelectedNames();
  2817. Result = SetProperties(Side, FileList);
  2818. }
  2819. else if (Operation == foCustomCommand)
  2820. {
  2821. DebugAssert(Param);
  2822. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2823. DirView(osRemote)->SaveSelectedNames();
  2824. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2825. CustomCommand(FileList, *Command, NULL);
  2826. Result = true;
  2827. }
  2828. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2829. {
  2830. DebugAssert(!IsSideLocalBrowser(Side));
  2831. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2832. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2833. }
  2834. else if (Operation == foLock)
  2835. {
  2836. DebugAssert(!IsSideLocalBrowser(Side));
  2837. LockFiles(FileList, true);
  2838. Result = true;
  2839. }
  2840. else if (Operation == foUnlock)
  2841. {
  2842. DebugAssert(!IsSideLocalBrowser(Side));
  2843. LockFiles(FileList, false);
  2844. Result = true;
  2845. }
  2846. else
  2847. {
  2848. DebugFail();
  2849. }
  2850. return Result;
  2851. }
  2852. //---------------------------------------------------------------------------
  2853. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2854. {
  2855. if (Side == osCurrent)
  2856. {
  2857. Side = FCurrentSide;
  2858. }
  2859. return Side;
  2860. }
  2861. //---------------------------------------------------------------------------
  2862. TOperationSide TCustomScpExplorerForm::GetOtherSide(TOperationSide Side)
  2863. {
  2864. TOperationSide Result;
  2865. switch (GetSide(Side))
  2866. {
  2867. case osLocal:
  2868. Result = osOther;
  2869. break;
  2870. case osOther:
  2871. Result = osLocal;
  2872. break;
  2873. default:
  2874. DebugFail();
  2875. Abort();
  2876. }
  2877. return Result;
  2878. }
  2879. //---------------------------------------------------------------------------
  2880. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2881. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2882. {
  2883. Side = GetSide(Side);
  2884. bool FullPath = IsSideLocalBrowser(Side);
  2885. TCustomDirView * ADirView = DirView(Side);
  2886. std::unique_ptr<TStringList> FileList(new TStringList());
  2887. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2888. bool ReloadProperties =
  2889. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2890. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2891. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2892. if (Result && ReloadProperties)
  2893. {
  2894. std::unique_ptr<TStringList> FileList2(new TStringList());
  2895. FileList->CaseSensitive = true;
  2896. FileList->Sort();
  2897. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2898. {
  2899. TListItem * Item = ADirView->Items->Item[Index];
  2900. UnicodeString FileName = ADirView->ItemFileName(Item);
  2901. int Index2;
  2902. if (FileList->Find(FileName, Index2))
  2903. {
  2904. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2905. }
  2906. }
  2907. LoadFilesProperties(FileList2.get());
  2908. }
  2909. return Result;
  2910. }
  2911. //---------------------------------------------------------------------------
  2912. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2913. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2914. bool NoConfirmation, void * Param)
  2915. {
  2916. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2917. {
  2918. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2919. {
  2920. DebugAssert(!IsLocalBrowserMode());
  2921. if (GetSide(Side) == osLocal)
  2922. {
  2923. Configuration->Usage->Inc(L"UploadsCommand");
  2924. }
  2925. else
  2926. {
  2927. Configuration->Usage->Inc(L"DownloadsCommand");
  2928. }
  2929. }
  2930. }
  2931. }
  2932. //---------------------------------------------------------------------------
  2933. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2934. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2935. {
  2936. TTransferOperationParam Param;
  2937. if ((WinConfiguration->Interface != ifCommander) ||
  2938. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2939. {
  2940. Flags &= ~cocShortCutHint;
  2941. }
  2942. TCustomDirView * DView = DirView(Side);
  2943. Param.Options =
  2944. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2945. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2946. if (FLAGSET(Flags, cocQueue))
  2947. {
  2948. Param.Queue = asOn;
  2949. }
  2950. else if (FLAGSET(Flags, cocNonQueue))
  2951. {
  2952. Param.Queue = asOff;
  2953. }
  2954. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2955. }
  2956. //---------------------------------------------------------------------------
  2957. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2958. {
  2959. int Count = ErrorList->Count;
  2960. try
  2961. {
  2962. if (ErrorList->Count)
  2963. {
  2964. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2965. if (MessageDialog(Message, qtError,
  2966. qaOK | qaCancel, HELP_NONE) == qaOK)
  2967. {
  2968. unsigned int Answer;
  2969. int Index = 0;
  2970. do
  2971. {
  2972. DebugAssert(Index >= 0 && Index < Count);
  2973. TQueryButtonAlias Aliases[2];
  2974. Aliases[0].Button = qaYes;
  2975. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2976. Aliases[1].Button = qaNo;
  2977. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2978. TMessageParams Params;
  2979. Params.Aliases = Aliases;
  2980. Params.AliasesCount = LENOF(Aliases);
  2981. int No = Index + 1;
  2982. UnicodeString Message = ErrorList->Strings[Index];
  2983. UnicodeString AMainInstructions;
  2984. UnicodeString Details;
  2985. if (ExtractMainInstructions(Message, AMainInstructions))
  2986. {
  2987. Details = Message;
  2988. Message = AMainInstructions;
  2989. }
  2990. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2991. Answer = MoreMessageDialog(
  2992. Message,
  2993. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2994. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2995. qaOK, HELP_NONE, &Params);
  2996. if (Answer == qaNo)
  2997. {
  2998. Index++;
  2999. }
  3000. if (Answer == qaYes)
  3001. {
  3002. Index--;
  3003. }
  3004. }
  3005. while (Answer != qaOK);
  3006. }
  3007. }
  3008. }
  3009. __finally
  3010. {
  3011. TStrings * List = ErrorList;
  3012. ErrorList = NULL;
  3013. for (int i = 0; i < Count; i++)
  3014. {
  3015. delete List->Objects[i];
  3016. }
  3017. delete List;
  3018. }
  3019. }
  3020. //---------------------------------------------------------------------------
  3021. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  3022. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  3023. {
  3024. // needed for checking filemasks, as there's no directory object
  3025. // associated with the file object
  3026. File->FullFileName = FullFileName;
  3027. TFileMasks::TParams MaskParams;
  3028. MaskParams.Size = File->Size;
  3029. MaskParams.Modification = File->Modification;
  3030. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  3031. }
  3032. //---------------------------------------------------------------------------
  3033. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  3034. {
  3035. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3036. Side = GetSide(Side);
  3037. TCustomDirView * DView = DirView(Side);
  3038. TListItem * FocusedItem = DView->ItemFocused;
  3039. UnicodeString Name;
  3040. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  3041. {
  3042. Name = DView->ItemFileName(FocusedItem);
  3043. }
  3044. else
  3045. {
  3046. Name = LoadStr(NEW_FILE);
  3047. }
  3048. UnicodeString Names = Name;
  3049. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3050. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3051. HELP_EDIT_NEW, History.get(), true, NULL, true, 444))
  3052. {
  3053. while (!Names.IsEmpty())
  3054. {
  3055. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3056. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3057. UnicodeString TargetFileName;
  3058. UnicodeString LocalFileName;
  3059. UnicodeString RootTempDir;
  3060. UnicodeString TempDir;
  3061. UnicodeString RemoteDirectory;
  3062. bool ExistingFile = false;
  3063. if (!IsSideLocalBrowser(Side))
  3064. {
  3065. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3066. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3067. if ((File.get() != NULL) &&
  3068. !File->IsDirectory)
  3069. {
  3070. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3071. ExistingFile = true;
  3072. }
  3073. File.reset(NULL);
  3074. if (!ExistingFile)
  3075. {
  3076. RemoteDirectory = UnixExtractFilePath(Name);
  3077. TemporaryDirectoryForRemoteFiles(
  3078. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3079. TargetFileName = UnixExtractFileName(Name);
  3080. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3081. LocalFileName = TempDir +
  3082. // We probably do not want to trim the VMS version here
  3083. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3084. }
  3085. }
  3086. else
  3087. {
  3088. if (ExtractFilePath(Name).IsEmpty())
  3089. {
  3090. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3091. }
  3092. else
  3093. {
  3094. LocalFileName = ExpandFileName(Name);
  3095. }
  3096. TargetFileName = ExtractFileName(Name);
  3097. }
  3098. if (!ExistingFile)
  3099. {
  3100. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3101. if (NewFile)
  3102. {
  3103. int File = FileCreate(ApiPath(LocalFileName));
  3104. if (File < 0)
  3105. {
  3106. if (!RootTempDir.IsEmpty())
  3107. {
  3108. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3109. }
  3110. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3111. }
  3112. else
  3113. {
  3114. FileClose(File);
  3115. }
  3116. }
  3117. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3118. const TEditorData * ExternalEditor = NULL;
  3119. TFileMasks::TParams MaskParams; // size not known
  3120. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3121. false, MaskParams);
  3122. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3123. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3124. }
  3125. }
  3126. }
  3127. }
  3128. //---------------------------------------------------------------------------
  3129. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3130. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3131. {
  3132. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3133. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3134. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3135. return
  3136. ((ExecuteFileBy == efInternalEditor)) ||
  3137. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3138. }
  3139. //---------------------------------------------------------------------------
  3140. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3141. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3142. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3143. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3144. {
  3145. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3146. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3147. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3148. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3149. Side = GetSide(Side);
  3150. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3151. if (!IsSideLocalBrowser(Side))
  3152. {
  3153. Data->Terminal = Terminal;
  3154. Data->SessionData = CloneCurrentSessionData();
  3155. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3156. Data->RemoteDirectory = RemoteDirectory;
  3157. Data->SessionName = Terminal->SessionData->SessionName;
  3158. Data->LocalRootDirectory = LocalRootDirectory;
  3159. Data->OriginalFileName = OriginalFileName;
  3160. Data->Command = L""; // will be changed later for external editor
  3161. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3162. if (WinConfiguration->EditorCheckNotModified)
  3163. {
  3164. Data->SourceTimestamp = SourceTimestamp;
  3165. }
  3166. }
  3167. if (ExecuteFileBy == efInternalEditor)
  3168. {
  3169. if (!IsSideLocalBrowser(Side))
  3170. {
  3171. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3172. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3173. TForm * Editor;
  3174. try
  3175. {
  3176. Editor =
  3177. ShowEditorForm(
  3178. FileName, this,
  3179. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3180. SaveAllInternalEditors, AnyInternalEditorModified,
  3181. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3182. }
  3183. catch(...)
  3184. {
  3185. if (!LocalRootDirectory.IsEmpty())
  3186. {
  3187. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory));
  3188. }
  3189. throw;
  3190. }
  3191. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3192. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3193. }
  3194. else
  3195. {
  3196. DebugAssert(!StandaloneOperation);
  3197. TForm * Editor =
  3198. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3199. SaveAllInternalEditors, AnyInternalEditorModified,
  3200. L"", false, SessionColor, -1, NewFile);
  3201. FLocalEditors->Add(Editor);
  3202. }
  3203. }
  3204. else
  3205. {
  3206. HANDLE Process;
  3207. if (ExecuteFileBy == efExternalEditor)
  3208. {
  3209. UnicodeString Program, Params, Dir;
  3210. Data->Command = ExternalEditor->ExternalEditor;
  3211. ReformatFileNameCommand(Data->Command);
  3212. SplitCommand(Data->Command, Program, Params, Dir);
  3213. Params = ExpandFileNameCommand(Params, FileName);
  3214. Program = ExpandEnvironmentVariables(Program);
  3215. if (!ExecuteShell(Program, Params, Process))
  3216. {
  3217. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3218. }
  3219. }
  3220. else
  3221. {
  3222. DebugAssert(!IsSideLocalBrowser(Side));
  3223. if (!ExecuteShell(FileName, L"", Process))
  3224. {
  3225. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3226. }
  3227. }
  3228. if (IsSideLocalBrowser(Side) ||
  3229. ((ExecuteFileBy == efShell) &&
  3230. !WinConfiguration->Editor.SDIShellEditor) ||
  3231. ((ExecuteFileBy == efExternalEditor) &&
  3232. !ExternalEditor->SDIExternalEditor))
  3233. {
  3234. // no need for handle
  3235. if (Process != NULL)
  3236. {
  3237. DebugCheck(CloseHandle(Process));
  3238. }
  3239. Process = INVALID_HANDLE_VALUE;
  3240. }
  3241. else
  3242. {
  3243. if (Process == NULL)
  3244. {
  3245. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3246. }
  3247. }
  3248. if (!IsSideLocalBrowser(Side))
  3249. {
  3250. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3251. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3252. }
  3253. }
  3254. }
  3255. //---------------------------------------------------------------------------
  3256. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3257. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3258. void * /*Arg*/)
  3259. {
  3260. if (Token != NULL)
  3261. {
  3262. EditorFormFileSave(static_cast<TForm *>(Token));
  3263. }
  3264. }
  3265. //---------------------------------------------------------------------------
  3266. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3267. {
  3268. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3269. {
  3270. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3271. }
  3272. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3273. }
  3274. //---------------------------------------------------------------------------
  3275. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3276. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3277. void * Arg)
  3278. {
  3279. if ((Token != NULL) &&
  3280. IsEditorFormModified(static_cast<TForm *>(Token)))
  3281. {
  3282. *static_cast<bool *>(Arg) = true;
  3283. }
  3284. }
  3285. //---------------------------------------------------------------------------
  3286. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3287. TObject * /*Sender*/, bool & Modified)
  3288. {
  3289. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3290. {
  3291. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3292. {
  3293. Modified = true;
  3294. }
  3295. }
  3296. if (!Modified)
  3297. {
  3298. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3299. }
  3300. }
  3301. //---------------------------------------------------------------------------
  3302. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3303. {
  3304. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3305. }
  3306. //---------------------------------------------------------------------------
  3307. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3308. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3309. UnicodeString & Result, UnicodeString & RootDirectory)
  3310. {
  3311. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3312. {
  3313. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3314. Result = RootDirectory;
  3315. }
  3316. else
  3317. {
  3318. RootDirectory = L"";
  3319. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3320. Result = IncludeTrailingBackslash(Result);
  3321. }
  3322. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3323. {
  3324. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3325. }
  3326. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3327. {
  3328. UnicodeString ARemoteDirectory = RemoteDirectory;
  3329. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3330. {
  3331. ARemoteDirectory.Delete(1, 1);
  3332. }
  3333. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3334. }
  3335. if (!ForceDirectories(ApiPath(Result)))
  3336. {
  3337. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3338. }
  3339. }
  3340. //---------------------------------------------------------------------------
  3341. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3342. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3343. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3344. {
  3345. DebugAssert(!IsLocalBrowserMode());
  3346. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3347. if (ForceText)
  3348. {
  3349. CopyParam.TransferMode = tmAscii;
  3350. }
  3351. TemporaryFileCopyParam(CopyParam);
  3352. if (TempDir.IsEmpty())
  3353. {
  3354. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3355. }
  3356. DebugAssert(!FAutoOperation);
  3357. FAutoOperation = AutoOperation;
  3358. FMoveToQueue = false;
  3359. Terminal->ExceptionOnFail = true;
  3360. try
  3361. {
  3362. try
  3363. {
  3364. // turn off confirmations, as for MDI editors we may possibly download
  3365. // the same file over
  3366. Terminal->CopyToLocal(
  3367. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3368. if (GetTargetNames)
  3369. {
  3370. for (int i = 0; i < FileList->Count; i++)
  3371. {
  3372. FileList->Strings[i] =
  3373. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3374. }
  3375. }
  3376. }
  3377. catch(...)
  3378. {
  3379. if (!RootTempDir.IsEmpty())
  3380. {
  3381. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3382. }
  3383. throw;
  3384. }
  3385. }
  3386. __finally
  3387. {
  3388. FAutoOperation = false;
  3389. Terminal->ExceptionOnFail = false;
  3390. }
  3391. }
  3392. //---------------------------------------------------------------------------
  3393. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3394. {
  3395. // Do not waste time checking for default editor list the next time,
  3396. // or testing if default editor is not notepad.
  3397. bool TryNextTime = false;
  3398. UnicodeString UsageState;
  3399. if (!WinConfiguration->EditorList->IsDefaultList())
  3400. {
  3401. UsageState = "H";
  3402. }
  3403. else
  3404. {
  3405. UnicodeString Executable;
  3406. UnicodeString ExecutableDescription;
  3407. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3408. {
  3409. UnicodeString Message =
  3410. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3411. unsigned int Answer =
  3412. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3413. if (Answer != qaOK)
  3414. {
  3415. UsageState = "R";
  3416. }
  3417. else
  3418. {
  3419. UsageState = "A";
  3420. TEditorData EditorData;
  3421. EditorData.Editor = edExternal;
  3422. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3423. EditorData.ExternalEditorOptionsAutodetect();
  3424. TEditorList EditorList;
  3425. EditorList = *WinConfiguration->EditorList;
  3426. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3427. WinConfiguration->EditorList = &EditorList;
  3428. }
  3429. }
  3430. }
  3431. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3432. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3433. }
  3434. //---------------------------------------------------------------------------
  3435. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3436. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3437. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3438. {
  3439. if (ExecuteFileBy == efDefaultEditor)
  3440. {
  3441. if (!WinConfiguration->OfferedEditorAutoConfig)
  3442. {
  3443. EditorAutoConfig();
  3444. }
  3445. const TEditorPreferences * Editor =
  3446. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3447. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3448. {
  3449. ExecuteFileBy = efInternalEditor;
  3450. ExternalEditor = NULL;
  3451. }
  3452. else if (Editor->Data->Editor == edOpen)
  3453. {
  3454. ExecuteFileBy = efShell;
  3455. ExternalEditor = NULL;
  3456. }
  3457. else
  3458. {
  3459. ExecuteFileBy = efExternalEditor;
  3460. ExternalEditor = Editor->Data;
  3461. }
  3462. }
  3463. }
  3464. //---------------------------------------------------------------------------
  3465. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3466. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3467. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3468. {
  3469. UnicodeString OriginalFileName;
  3470. UnicodeString LocalRootDirectory;
  3471. UnicodeString RemoteDirectory;
  3472. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3473. IsSideLocalBrowser(Side), MaskParams);
  3474. UnicodeString Counter;
  3475. UnicodeString LocalFileName;
  3476. bool Handled = false;
  3477. if (!IsSideLocalBrowser(Side))
  3478. {
  3479. // We need to trim VMS version here, so that we use name without version
  3480. // when uploading back to create a new version of the file
  3481. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3482. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3483. TObject * Token = NULL;
  3484. UnicodeString LocalDirectory;
  3485. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3486. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3487. LocalDirectory))
  3488. {
  3489. if (Token != NULL)
  3490. {
  3491. TForm * Form = dynamic_cast<TForm *>(Token);
  3492. if (Form->WindowState == wsMinimized)
  3493. {
  3494. ShowWindow(Form->Handle, SW_RESTORE);
  3495. }
  3496. else
  3497. {
  3498. Form->SetFocus();
  3499. }
  3500. Handled = true;
  3501. }
  3502. else
  3503. {
  3504. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3505. }
  3506. }
  3507. if (!Handled)
  3508. {
  3509. TStringList * FileList1 = new TStringList();
  3510. try
  3511. {
  3512. FileList1->AddObject(FullFileName, Object);
  3513. TemporarilyDownloadFiles(FileList1,
  3514. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3515. LocalRootDirectory, LocalDirectory, true, true, false);
  3516. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3517. }
  3518. __finally
  3519. {
  3520. delete FileList1;
  3521. }
  3522. switch (ExecuteFileBy)
  3523. {
  3524. case efShell:
  3525. Counter = "RemoteFilesExecuted";
  3526. break;
  3527. case efInternalEditor:
  3528. Counter = "RemoteFilesOpenedInInternalEditor";
  3529. break;
  3530. case efExternalEditor:
  3531. Counter = "RemoteFilesOpenedInExternalEditor";
  3532. break;
  3533. default:
  3534. DebugFail();
  3535. }
  3536. }
  3537. }
  3538. else
  3539. {
  3540. LocalFileName = FullFileName;
  3541. OriginalFileName = ExtractFileName(FullFileName);
  3542. switch (ExecuteFileBy)
  3543. {
  3544. case efShell:
  3545. Counter = "LocalFilesExecuted";
  3546. break;
  3547. case efInternalEditor:
  3548. Counter = "LocalFilesOpenedInInternalEditor";
  3549. break;
  3550. case efExternalEditor:
  3551. Counter = "LocalFilesOpenedInExternalEditor";
  3552. break;
  3553. default:
  3554. DebugFail();
  3555. }
  3556. }
  3557. if (!Handled)
  3558. {
  3559. Configuration->Usage->Inc(Counter);
  3560. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3561. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3562. }
  3563. }
  3564. //---------------------------------------------------------------------------
  3565. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3566. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3567. bool AllSelected, bool OnFocused)
  3568. {
  3569. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3570. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3571. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3572. Side = GetSide(Side);
  3573. TCustomDirView * DView = DirView(Side);
  3574. TStrings * FileList =
  3575. AllSelected ?
  3576. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3577. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3578. try
  3579. {
  3580. DebugAssert(AllSelected || (FileList->Count == 1));
  3581. for (int i = 0; i < FileList->Count; i++)
  3582. {
  3583. UnicodeString ListFileName = FileList->Strings[i];
  3584. UnicodeString FileNameOnly =
  3585. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3586. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3587. if (!DView->ItemIsDirectory(Item))
  3588. {
  3589. UnicodeString FullFileName;
  3590. if (!IsSideLocalBrowser(Side))
  3591. {
  3592. FullFileName = DirView(Side)->Path + ListFileName;
  3593. }
  3594. else
  3595. {
  3596. FullFileName = ListFileName;
  3597. }
  3598. TObject * Object = FileList->Objects[i];
  3599. TFileMasks::TParams MaskParams;
  3600. MaskParams.Size = DView->ItemFileSize(Item);
  3601. TDateTimePrecision Precision;
  3602. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3603. std::unique_ptr<TRemoteFile> File;
  3604. if (!IsSideLocalBrowser(Side))
  3605. {
  3606. File.reset(Terminal->TryReadFile(FullFileName));
  3607. if (File.get() != NULL)
  3608. {
  3609. File->FullFileName = FullFileName;
  3610. Object = File.get();
  3611. MaskParams.Size = File->Size;
  3612. MaskParams.Modification = File->Modification;
  3613. }
  3614. }
  3615. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3616. }
  3617. }
  3618. }
  3619. __finally
  3620. {
  3621. delete FileList;
  3622. }
  3623. }
  3624. //---------------------------------------------------------------------------
  3625. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3626. {
  3627. CopyParam.FileNameCase = ncNoChange;
  3628. CopyParam.PreserveRights = false;
  3629. CopyParam.PreserveReadOnly = false;
  3630. CopyParam.ReplaceInvalidChars = true;
  3631. CopyParam.IncludeFileMask = TFileMasks();
  3632. CopyParam.NewerOnly = false;
  3633. CopyParam.FileMask = L"";
  3634. }
  3635. //---------------------------------------------------------------------------
  3636. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3637. {
  3638. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3639. }
  3640. //---------------------------------------------------------------------------
  3641. class TEditorUploadQueueItem : public TUploadQueueItem
  3642. {
  3643. public:
  3644. __fastcall TEditorUploadQueueItem(
  3645. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  3646. const TCopyParamType * CopyParam, int Params) :
  3647. TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, false)
  3648. {
  3649. }
  3650. protected:
  3651. virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  3652. {
  3653. TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
  3654. TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
  3655. }
  3656. };
  3657. //---------------------------------------------------------------------------
  3658. void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
  3659. {
  3660. if (WinConfiguration->EditorCheckNotModified) // optimization
  3661. {
  3662. UnicodeString RemoteFilePath;
  3663. {
  3664. TGuard Guard(FEditorManager->Section);
  3665. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3666. if (DebugAlwaysTrue(Data != NULL) &&
  3667. EditorCheckNotModified(Data))
  3668. {
  3669. RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3670. }
  3671. }
  3672. if (!RemoteFilePath.IsEmpty())
  3673. {
  3674. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
  3675. if (File.get() != NULL)
  3676. {
  3677. TGuard Guard(FEditorManager->Section);
  3678. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3679. if (DebugAlwaysTrue(Data != NULL))
  3680. {
  3681. Data->SourceTimestamp = File->Modification;
  3682. AppLogFmt(L"Uploaded edited remote file timestamp: %s", (StandardTimestamp(Data->SourceTimestamp)));
  3683. }
  3684. }
  3685. }
  3686. }
  3687. }
  3688. //---------------------------------------------------------------------------
  3689. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3690. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3691. {
  3692. TTerminalManager * Manager = TTerminalManager::Instance();
  3693. if ((Manager->ActiveTerminal == Data->Terminal) &&
  3694. Manager->ScheduleTerminalReconnnect(Data->Terminal))
  3695. {
  3696. AppLog(L"Scheduled session reconnect, will retry the upload later");
  3697. Retry = true;
  3698. }
  3699. else if (!IsActiveTerminal(Data->Terminal))
  3700. {
  3701. if (!NonVisualDataModule->Busy)
  3702. {
  3703. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3704. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3705. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3706. if (Data->Terminal == NULL)
  3707. {
  3708. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3709. if (SameSiteTerminal != NULL)
  3710. {
  3711. UnicodeString Message =
  3712. FMTLOAD(EDIT_SESSION_REATTACH,
  3713. (FileNameOnly, Data->SessionName, FileNameOnly));
  3714. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3715. {
  3716. Data->Terminal = SameSiteTerminal;
  3717. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3718. // We might also overwrite session data
  3719. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3720. }
  3721. else
  3722. {
  3723. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3724. Abort();
  3725. }
  3726. }
  3727. }
  3728. // Foreground session should reconnect itself (except for those disconnected explicitly by the user)
  3729. else if (DebugAlwaysTrue(Terminal->Disconnected || (Terminal != Data->Terminal)))
  3730. {
  3731. UnicodeString Message =
  3732. MainInstructions(
  3733. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3734. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3735. {
  3736. if (Terminal->Disconnected)
  3737. {
  3738. Manager->ActiveSession = Terminal;
  3739. ReconnectSession();
  3740. // Was it reconnected? (if not, Data->Terminal should be nulled now)
  3741. if (!IsActiveTerminal(Data->Terminal) || (Terminal != Data->Terminal))
  3742. {
  3743. Abort();
  3744. }
  3745. }
  3746. else
  3747. {
  3748. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3749. }
  3750. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3751. }
  3752. else
  3753. {
  3754. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3755. Abort();
  3756. }
  3757. }
  3758. }
  3759. if (!IsActiveTerminal(Data->Terminal))
  3760. {
  3761. Configuration->Usage->Inc(L"EditInactiveSession");
  3762. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3763. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3764. (ExtractFileName(FileName), Data->SessionName)));
  3765. }
  3766. }
  3767. if (!Retry && EditorCheckNotModified(Data))
  3768. {
  3769. if (NonVisualDataModule->Busy)
  3770. {
  3771. Retry = true;
  3772. }
  3773. else
  3774. {
  3775. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3776. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3777. if (File.get() != NULL)
  3778. {
  3779. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3780. if (File->Modification != Data->SourceTimestamp)
  3781. {
  3782. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3783. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3784. {
  3785. Abort();
  3786. }
  3787. }
  3788. }
  3789. }
  3790. }
  3791. if (!Retry)
  3792. {
  3793. TStrings * FileList = new TStringList();
  3794. try
  3795. {
  3796. FileList->Add(FileName);
  3797. // Consider using the same settings (preset) as when the file was downloaded.
  3798. // More over this does not reflect the actual session that will do the upload.
  3799. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3800. TemporaryFileCopyParam(CopyParam);
  3801. if (Data->ForceText)
  3802. {
  3803. CopyParam.TransferMode = tmAscii;
  3804. }
  3805. // so i do not need to worry if masking algorithm works in all cases
  3806. // ("" means "copy file name", no masking is actually done)
  3807. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3808. {
  3809. CopyParam.FileMask = L"";
  3810. }
  3811. else
  3812. {
  3813. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3814. }
  3815. int Params = cpNoConfirmation | cpTemporary;
  3816. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3817. {
  3818. TTerminalQueue * AQueue = Manager->FindQueueForTerminal(Data->Terminal);
  3819. TQueueItem * QueueItem =
  3820. new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
  3821. QueueItem->CompleteEvent = UploadCompleteEvent;
  3822. AddQueueItem(AQueue, QueueItem, Data->Terminal);
  3823. }
  3824. else
  3825. {
  3826. if (NonVisualDataModule->Busy)
  3827. {
  3828. Retry = true;
  3829. }
  3830. else
  3831. {
  3832. FMoveToQueue = false;
  3833. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3834. SetEvent(UploadCompleteEvent);
  3835. }
  3836. }
  3837. }
  3838. __finally
  3839. {
  3840. delete FileList;
  3841. }
  3842. }
  3843. }
  3844. //---------------------------------------------------------------------------
  3845. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3846. const UnicodeString & FileName, TEditedFileData * Data)
  3847. {
  3848. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3849. DebugAssert(!NonVisualDataModule->Busy);
  3850. if (!IsActiveTerminal(Data->Terminal))
  3851. {
  3852. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3853. (ExtractFileName(FileName), Data->SessionName)));
  3854. }
  3855. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3856. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3857. NonVisualDataModule->StartBusy();
  3858. try
  3859. {
  3860. std::unique_ptr<TRemoteFile> File;
  3861. UnicodeString RemoteFileName =
  3862. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3863. Terminal->ExceptionOnFail = true;
  3864. try
  3865. {
  3866. File.reset(Terminal->ReadFile(RemoteFileName));
  3867. if (!File->HaveFullFileName)
  3868. {
  3869. File->FullFileName = RemoteFileName;
  3870. }
  3871. }
  3872. __finally
  3873. {
  3874. Terminal->ExceptionOnFail = false;
  3875. }
  3876. std::unique_ptr<TStrings> FileList(new TStringList());
  3877. FileList->AddObject(RemoteFileName, File.get());
  3878. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3879. UnicodeString TempDir = ExtractFilePath(FileName);
  3880. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3881. if (EditorCheckNotModified(Data))
  3882. {
  3883. Data->SourceTimestamp = File->Modification;
  3884. }
  3885. // sanity check, the target file name should be still the same
  3886. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3887. }
  3888. __finally
  3889. {
  3890. NonVisualDataModule->EndBusy();
  3891. // it actually may not exist anymore...
  3892. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3893. }
  3894. }
  3895. //---------------------------------------------------------------------------
  3896. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3897. const TEditedFileData * Data, bool & KeepOpen)
  3898. {
  3899. // Command is set for external editors only (not for "shell" open).
  3900. if (!Data->Command.IsEmpty())
  3901. {
  3902. bool AnyFound = false;
  3903. bool AnyMDI = false;
  3904. bool AnyNonMDI = false;
  3905. bool AnyDetect = false;
  3906. TEditorList * EditorList = new TEditorList();
  3907. try
  3908. {
  3909. *EditorList = *WinConfiguration->EditorList;
  3910. for (int i = 0; i < EditorList->Count; i++)
  3911. {
  3912. const TEditorPreferences * Editor = EditorList->Editors[i];
  3913. if ((Editor->Data->Editor == edExternal) &&
  3914. (Editor->Data->ExternalEditor == Data->Command))
  3915. {
  3916. AnyFound = true;
  3917. if (Editor->Data->SDIExternalEditor)
  3918. {
  3919. AnyNonMDI = true;
  3920. if (Editor->Data->DetectMDIExternalEditor)
  3921. {
  3922. AnyDetect = true;
  3923. }
  3924. }
  3925. else
  3926. {
  3927. AnyMDI = true;
  3928. }
  3929. }
  3930. }
  3931. bool EnableMDI = false;
  3932. bool DisableDetect = false;
  3933. if (AnyMDI)
  3934. {
  3935. KeepOpen = true;
  3936. if (AnyNonMDI)
  3937. {
  3938. // there is at least one instance of the editor with MDI support enabled,
  3939. // and one with disabled, enable it for all instances
  3940. EnableMDI = true;
  3941. }
  3942. }
  3943. else if (AnyFound && !AnyDetect)
  3944. {
  3945. // at least once instance found but all have MDI autodetection disabled
  3946. // => close the file (default action)
  3947. }
  3948. else
  3949. {
  3950. // no instance of the editor has MDI support enabled
  3951. TMessageParams Params;
  3952. if (AnyFound)
  3953. {
  3954. // there is at least one instance of the editor
  3955. Params.Params |= mpNeverAskAgainCheck;
  3956. }
  3957. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3958. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3959. switch (Answer)
  3960. {
  3961. case qaNeverAskAgain:
  3962. DisableDetect = true;
  3963. break;
  3964. case qaNo:
  3965. EnableMDI = true;
  3966. KeepOpen = true;
  3967. break;
  3968. }
  3969. }
  3970. if (AnyFound && (EnableMDI || DisableDetect))
  3971. {
  3972. bool Changed = false;
  3973. for (int i = 0; i < EditorList->Count; i++)
  3974. {
  3975. const TEditorPreferences * Editor = EditorList->Editors[i];
  3976. if ((Editor->Data->Editor == edExternal) &&
  3977. (Editor->Data->ExternalEditor == Data->Command) &&
  3978. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3979. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3980. {
  3981. Changed = true;
  3982. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3983. if (EnableMDI)
  3984. {
  3985. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3986. }
  3987. if (DisableDetect)
  3988. {
  3989. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3990. }
  3991. EditorList->Change(i, UpdatedEditor);
  3992. }
  3993. }
  3994. if (Changed)
  3995. {
  3996. WinConfiguration->EditorList = EditorList;
  3997. }
  3998. }
  3999. }
  4000. __finally
  4001. {
  4002. delete EditorList;
  4003. }
  4004. }
  4005. else
  4006. {
  4007. // "open" case
  4008. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  4009. qaOK, HELP_APP_EARLY_CLOSED);
  4010. }
  4011. }
  4012. //---------------------------------------------------------------------------
  4013. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  4014. {
  4015. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  4016. }
  4017. //---------------------------------------------------------------------------
  4018. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  4019. {
  4020. SideEnter(osRemote);
  4021. }
  4022. //---------------------------------------------------------------------------
  4023. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  4024. {
  4025. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  4026. SideEnter(osRemote);
  4027. }
  4028. //---------------------------------------------------------------------------
  4029. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  4030. {
  4031. FCurrentSide = Side;
  4032. if (Visible)
  4033. {
  4034. UpdateControls();
  4035. }
  4036. }
  4037. //---------------------------------------------------------------------------
  4038. void __fastcall TCustomScpExplorerForm::FileDeleted(
  4039. TOperationSide Side, const UnicodeString & FileName, bool Success, bool NotCancelled)
  4040. {
  4041. VisualiseOperationFinished(Side, FileName, Success && NotCancelled);
  4042. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  4043. {
  4044. FDeletedFiles->Add(FileName);
  4045. }
  4046. }
  4047. //---------------------------------------------------------------------------
  4048. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  4049. TStrings * FileList, bool Alternative)
  4050. {
  4051. TCustomDirView * DView = DirView(Side);
  4052. DView->SaveSelection();
  4053. DView->SaveSelectedNames();
  4054. DebugAssert(!FAlternativeDelete);
  4055. FAlternativeDelete = Alternative;
  4056. try
  4057. {
  4058. int Params = FLAGMASK(Alternative, dfAlternative);
  4059. bool Remote = !IsSideLocalBrowser(Side);
  4060. std::unique_ptr<TStringList> DeletedFiles;
  4061. std::unique_ptr<TStrings> PermanentFileList;
  4062. bool AllowMoveToQueue = (Terminal != NULL);
  4063. if (AllowMoveToQueue)
  4064. {
  4065. DeletedFiles.reset(new TStringList());
  4066. DeletedFiles->CaseSensitive = Remote;
  4067. }
  4068. try
  4069. {
  4070. DebugAssert(FDeletedFiles == NULL);
  4071. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles, DeletedFiles.get());
  4072. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  4073. if (AllowMoveToQueue)
  4074. {
  4075. FOnFileOperationFinished = FileDeleted;
  4076. }
  4077. FMoveToQueue = false;
  4078. if (Remote)
  4079. {
  4080. DebugAssert(AllowMoveToQueue);
  4081. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  4082. PermanentFileList.reset(TRemoteFileList::CloneStrings(FileList));
  4083. DebugAssert(Terminal != NULL);
  4084. Terminal->DeleteFiles(FileList, Params);
  4085. }
  4086. else
  4087. {
  4088. if (AllowMoveToQueue) // optimization)
  4089. {
  4090. PermanentFileList.reset(CloneStrings(FileList));
  4091. }
  4092. if (IsLocalBrowserMode())
  4093. {
  4094. Configuration->Usage->Inc(L"LocalLocalDeletes");
  4095. }
  4096. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  4097. try
  4098. {
  4099. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, Params);
  4100. }
  4101. __finally
  4102. {
  4103. ReloadLocalDirectory();
  4104. }
  4105. }
  4106. // Probably not needed for deleting, just for consistency with transfer code
  4107. if (FMoveToQueue)
  4108. {
  4109. Abort();
  4110. }
  4111. }
  4112. catch (EAbort &)
  4113. {
  4114. if (FMoveToQueue && DebugAlwaysTrue(AllowMoveToQueue) && DebugAlwaysTrue(Terminal != NULL))
  4115. {
  4116. FMoveToQueue = false;
  4117. DeletedFiles->Sorted = true;
  4118. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  4119. {
  4120. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  4121. {
  4122. // We should always be deleting the first item => what can be used to optimize this code, if needed
  4123. DebugAssert(Index == 0);
  4124. PermanentFileList->Delete(Index);
  4125. Index--;
  4126. }
  4127. }
  4128. if (Remote)
  4129. {
  4130. Configuration->Usage->Inc("MovesToBackgroundDelete");
  4131. TQueueItem * QueueItem = new TRemoteDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  4132. AddQueueItem(Queue, QueueItem, Terminal);
  4133. }
  4134. else
  4135. {
  4136. Configuration->Usage->Inc("MovesToBackgroundDeleteLocal");
  4137. TQueueItem * QueueItem = new TLocalDeleteQueueItem(PermanentFileList.get(), Params);
  4138. AddQueueItem(Queue, QueueItem, Terminal);
  4139. }
  4140. ClearOperationSelection(Side);
  4141. }
  4142. }
  4143. FAlternativeDelete = false;
  4144. }
  4145. catch(...)
  4146. {
  4147. FAlternativeDelete = false;
  4148. DView->DiscardSavedSelection();
  4149. throw;
  4150. }
  4151. DView->RestoreSelection();
  4152. }
  4153. //---------------------------------------------------------------------------
  4154. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4155. {
  4156. DebugAssert(Terminal);
  4157. DebugAssert(!IsLocalBrowserMode());
  4158. RemoteDirView->SaveSelection();
  4159. RemoteDirView->SaveSelectedNames();
  4160. try
  4161. {
  4162. if (Lock)
  4163. {
  4164. Terminal->LockFiles(FileList);
  4165. }
  4166. else
  4167. {
  4168. Terminal->UnlockFiles(FileList);
  4169. }
  4170. }
  4171. catch(...)
  4172. {
  4173. RemoteDirView->DiscardSavedSelection();
  4174. throw;
  4175. }
  4176. RemoteDirView->RestoreSelection();
  4177. }
  4178. //---------------------------------------------------------------------------
  4179. bool TCustomScpExplorerForm::DoDirectoryExists(void * Session, const UnicodeString & Directory)
  4180. {
  4181. TTerminal * ATerminal = (Session == NULL) ? Terminal : reinterpret_cast<TTerminal *>(Session);
  4182. bool Result =
  4183. IsActiveTerminal(ATerminal) &&
  4184. ATerminal->DirectoryExists(Directory);
  4185. return Result;
  4186. }
  4187. //---------------------------------------------------------------------------
  4188. bool TCustomScpExplorerForm::NeedSecondarySessionForRemoteCopy(TStrings * FileList)
  4189. {
  4190. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4191. return
  4192. !Terminal->IsCapable[fcRemoteCopy] ||
  4193. (CopyDirsOnSecondarySession && TRemoteFileList::AnyDirectory(FileList));
  4194. }
  4195. //---------------------------------------------------------------------------
  4196. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4197. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4198. bool NoConfirmation, bool Move)
  4199. {
  4200. DebugAssert(Terminal != NULL);
  4201. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4202. DebugAssert(!IsLocalBrowserMode());
  4203. if (Session == NULL)
  4204. {
  4205. Session = Terminal;
  4206. }
  4207. bool TargetConfirmed = false;
  4208. if (Session == Terminal)
  4209. {
  4210. if (RemoteDriveView->DropTarget != NULL)
  4211. {
  4212. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4213. TargetConfirmed = true;
  4214. }
  4215. else if (RemoteDirView->DropTarget != NULL)
  4216. {
  4217. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4218. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4219. TargetConfirmed = true;
  4220. }
  4221. else
  4222. {
  4223. Target = RemoteDirView->Path;
  4224. }
  4225. }
  4226. else
  4227. {
  4228. Target = GetSessionPath(Session, osRemote);
  4229. }
  4230. Target = UnixIncludeTrailingBackslash(Target);
  4231. if (FileList->Count == 1)
  4232. {
  4233. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4234. }
  4235. else
  4236. {
  4237. FileMask = AnyMask;
  4238. }
  4239. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4240. bool Result = true;
  4241. if (!NoConfirmation)
  4242. {
  4243. bool Multi = (FileList->Count > 1);
  4244. if (Move)
  4245. {
  4246. Result = DoRemoteMoveDialog(Multi, Target, FileMask, DoDirectoryExists);
  4247. }
  4248. else
  4249. {
  4250. std::unique_ptr<TStrings> Sessions(new TStringList);
  4251. std::unique_ptr<TStrings> Directories(new TStringList);
  4252. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4253. for (int Index = 0; Index < SessionList->Count; Index++)
  4254. {
  4255. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4256. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4257. {
  4258. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4259. Directories->Add(GetSessionPath(ASession, osRemote));
  4260. }
  4261. }
  4262. bool AnyDirectory = false;
  4263. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4264. if (CopyDirsOnSecondarySession && !Terminal->CommandSessionOpened) // optimization
  4265. {
  4266. for (int Index = 0; !AnyDirectory && (Index < FileList->Count); Index++)
  4267. {
  4268. AnyDirectory = DebugNotNull(dynamic_cast<TRemoteFile *>(FileList->Objects[Index]))->IsDirectory;
  4269. }
  4270. }
  4271. TDirectRemoteCopy AllowDirectCopy;
  4272. if (Terminal->CommandSessionOpened || !NeedSecondarySessionForRemoteCopy(FileList))
  4273. {
  4274. DebugAssert(DirectCopy);
  4275. AllowDirectCopy = drcAllow;
  4276. }
  4277. else if (Terminal->IsCapable[fcSecondaryShell])
  4278. {
  4279. DebugAssert(DirectCopy);
  4280. AllowDirectCopy = Terminal->IsCapable[fcRemoteCopy] ? drcConfirmCommandSessionDirs : drcConfirmCommandSession;
  4281. }
  4282. else
  4283. {
  4284. DebugAssert(!DirectCopy);
  4285. AllowDirectCopy = drcDisallow;
  4286. }
  4287. void * ASession = Session;
  4288. Result = DoRemoteCopyDialog(
  4289. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal,
  4290. DoDirectoryExists, TargetConfirmed);
  4291. Session = static_cast<TManagedTerminal *>(ASession);
  4292. }
  4293. }
  4294. return Result;
  4295. }
  4296. //---------------------------------------------------------------------------
  4297. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4298. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4299. {
  4300. DebugAssert(!IsLocalBrowserMode());
  4301. bool DirectCopy;
  4302. UnicodeString Target, FileMask;
  4303. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4304. if (Result)
  4305. {
  4306. if (!Move && !DirectCopy)
  4307. {
  4308. Configuration->Usage->Inc("RemoteCopyTemp");
  4309. UnicodeString RootTempDir;
  4310. UnicodeString TempDir;
  4311. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4312. TStrings * TemporaryFilesList = new TStringList();
  4313. try
  4314. {
  4315. TMakeLocalFileListParams MakeFileListParam;
  4316. MakeFileListParam.FileList = TemporaryFilesList;
  4317. MakeFileListParam.FileTimes = NULL;
  4318. MakeFileListParam.IncludeDirs = true;
  4319. MakeFileListParam.Recursive = false;
  4320. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4321. TTerminalManager::Instance()->ActiveSession = Session;
  4322. if (TemporaryFilesList->Count > 0)
  4323. {
  4324. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4325. CopyParam.FileMask = FileMask;
  4326. DebugAssert(!FAutoOperation);
  4327. FAutoOperation = true;
  4328. FMoveToQueue = false;
  4329. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4330. }
  4331. }
  4332. __finally
  4333. {
  4334. delete TemporaryFilesList;
  4335. FAutoOperation = false;
  4336. if (!RootTempDir.IsEmpty())
  4337. {
  4338. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  4339. }
  4340. }
  4341. }
  4342. else
  4343. {
  4344. RemoteDirView->SaveSelection();
  4345. RemoteDirView->SaveSelectedNames();
  4346. try
  4347. {
  4348. if (Move)
  4349. {
  4350. Configuration->Usage->Inc("RemoteMove");
  4351. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4352. }
  4353. else
  4354. {
  4355. Configuration->Usage->Inc("RemoteCopyDirect");
  4356. DebugAssert(DirectCopy);
  4357. DebugAssert(Session == Terminal);
  4358. if (!NeedSecondarySessionForRemoteCopy(FileList) ||
  4359. Terminal->CommandSessionOpened ||
  4360. CommandSessionFallback())
  4361. {
  4362. Terminal->CopyFiles(FileList, Target, FileMask, false);
  4363. }
  4364. }
  4365. }
  4366. catch(...)
  4367. {
  4368. RemoteDirView->DiscardSavedSelection();
  4369. throw;
  4370. }
  4371. RemoteDirView->RestoreSelection();
  4372. }
  4373. }
  4374. return Result;
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4378. const UnicodeString & Path, TRemoteProperties & Properties)
  4379. {
  4380. DebugAssert(!IsLocalBrowserMode());
  4381. Properties.Valid = Properties.Valid << vpEncrypt;
  4382. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4383. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4384. }
  4385. //---------------------------------------------------------------------------
  4386. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4387. {
  4388. Side = GetSide(Side);
  4389. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4390. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4391. UnicodeString Name = LoadStr(NEW_FOLDER);
  4392. int AllowedChanges =
  4393. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4394. bool SaveSettings = false;
  4395. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4396. {
  4397. TWindowLock Lock(this);
  4398. if (!IsSideLocalBrowser(Side))
  4399. {
  4400. if (SaveSettings)
  4401. {
  4402. GUIConfiguration->NewDirectoryProperties = Properties;
  4403. }
  4404. CreateRemoteDirectory(Name, Properties);
  4405. }
  4406. else
  4407. {
  4408. DirView(Side)->CreateDirectory(Name);
  4409. }
  4410. }
  4411. }
  4412. //---------------------------------------------------------------------------
  4413. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4414. {
  4415. TWindowLock Lock(this);
  4416. DirView(Side)->ExecuteHomeDirectory();
  4417. }
  4418. //---------------------------------------------------------------------------
  4419. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4420. {
  4421. DoOpenDirectoryDialog(odBrowse, Side);
  4422. }
  4423. //---------------------------------------------------------------------------
  4424. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4425. {
  4426. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4427. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4428. bool Result = !Path.IsEmpty();
  4429. if (Result)
  4430. {
  4431. TryOpenDirectory(Side, Path);
  4432. }
  4433. return Result;
  4434. }
  4435. //---------------------------------------------------------------------------
  4436. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4437. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4438. bool & Close)
  4439. {
  4440. // terminal can be already closed (e.g. dropped connection)
  4441. if (Terminal != NULL)
  4442. {
  4443. try
  4444. {
  4445. TCalculateSizeParams Params;
  4446. Params.Stats = &Stats;
  4447. Terminal->CalculateFilesSize(FileList, Size, Params);
  4448. }
  4449. catch(...)
  4450. {
  4451. if (!Terminal->Active)
  4452. {
  4453. Close = true;
  4454. }
  4455. throw;
  4456. }
  4457. }
  4458. }
  4459. //---------------------------------------------------------------------------
  4460. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4461. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4462. bool & Close)
  4463. {
  4464. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4465. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4466. {
  4467. Configuration->Usage->Inc(L"ChecksumCalculated");
  4468. try
  4469. {
  4470. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4471. }
  4472. catch(...)
  4473. {
  4474. if (!Terminal->Active)
  4475. {
  4476. Close = true;
  4477. }
  4478. throw;
  4479. }
  4480. }
  4481. }
  4482. //---------------------------------------------------------------------------
  4483. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4484. {
  4485. if (Terminal->LoadFilesProperties(FileList))
  4486. {
  4487. RemoteDirView->Invalidate();
  4488. }
  4489. }
  4490. //---------------------------------------------------------------------------
  4491. bool TCustomScpExplorerForm::CanCalculateChecksum()
  4492. {
  4493. return
  4494. Terminal->IsCapable[fcCalculatingChecksum] ||
  4495. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles());
  4496. }
  4497. //---------------------------------------------------------------------------
  4498. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4499. {
  4500. bool Result;
  4501. if (!IsSideLocalBrowser(Side))
  4502. {
  4503. TRemoteTokenList * GroupList = NULL;
  4504. TRemoteTokenList * UserList = NULL;
  4505. try
  4506. {
  4507. TRemoteProperties CurrentProperties;
  4508. LoadFilesProperties(FileList);
  4509. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4510. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4511. if (CapableGroupChanging || CapableOwnerChanging)
  4512. {
  4513. if (CapableGroupChanging)
  4514. {
  4515. GroupList = Terminal->Groups->Duplicate();
  4516. }
  4517. if (CapableOwnerChanging)
  4518. {
  4519. UserList = Terminal->Users->Duplicate();
  4520. }
  4521. TRemoteDirectory * Files = Terminal->Files;
  4522. int Count = Files->Count;
  4523. if (Count > 100)
  4524. {
  4525. Count = 100;
  4526. }
  4527. for (int Index = 0; Index < Count; Index++)
  4528. {
  4529. TRemoteFile * File = Files->Files[Index];
  4530. if (CapableGroupChanging)
  4531. {
  4532. GroupList->AddUnique(File->Group);
  4533. }
  4534. if (CapableOwnerChanging)
  4535. {
  4536. UserList->AddUnique(File->Owner);
  4537. }
  4538. }
  4539. // if we haven't collected tokens for all files in current directory,
  4540. // make sure we collect them at least for all selected files.
  4541. // (note that so far the files in FileList has to be from current direcotry)
  4542. if (Count < Files->Count)
  4543. {
  4544. for (int Index = 0; Index < FileList->Count; Index++)
  4545. {
  4546. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4547. if (CapableGroupChanging)
  4548. {
  4549. GroupList->AddUnique(File->Group);
  4550. }
  4551. if (CapableOwnerChanging)
  4552. {
  4553. UserList->AddUnique(File->Owner);
  4554. }
  4555. }
  4556. }
  4557. }
  4558. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4559. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4560. if (CapableAclChanging && TRemoteFileList::AnyDirectory(FileList))
  4561. {
  4562. CapableAclChanging = false;
  4563. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4564. }
  4565. int Flags = 0;
  4566. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4567. if (CapableAclChanging) Flags |= cpAcl;
  4568. if (CapableOwnerChanging) Flags |= cpOwner;
  4569. if (CapableGroupChanging) Flags |= cpGroup;
  4570. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4571. if (CanCalculateChecksum())
  4572. {
  4573. CalculateChecksumEvent = CalculateChecksum;
  4574. }
  4575. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4576. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4577. int Options =
  4578. FLAGMASK(Terminal->IsCapable[fcGroupOwnerChangingByID], poUserGroupByID) |
  4579. FLAGMASK(Terminal->IsCapable[fcTags], poTags);
  4580. TRemoteProperties NewProperties = CurrentProperties;
  4581. Result =
  4582. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4583. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags, Options,
  4584. CalculateSize, CalculateChecksumEvent);
  4585. if (Result)
  4586. {
  4587. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4588. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4589. }
  4590. }
  4591. __finally
  4592. {
  4593. delete GroupList;
  4594. delete UserList;
  4595. }
  4596. }
  4597. else
  4598. {
  4599. DirView(Side)->DisplayPropertiesMenu();
  4600. Result = true;
  4601. }
  4602. return Result;
  4603. }
  4604. //---------------------------------------------------------------------------
  4605. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4606. {
  4607. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4608. {
  4609. FIgnoreNextDialogChar = Key;
  4610. }
  4611. Key = 0;
  4612. }
  4613. //---------------------------------------------------------------------------
  4614. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4615. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4616. {
  4617. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4618. if (Command != NULL)
  4619. {
  4620. KeyProcessed(Key, Shift);
  4621. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4622. {
  4623. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4624. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4625. false, false, const_cast<TCustomCommandType *>(Command));
  4626. }
  4627. }
  4628. }
  4629. //---------------------------------------------------------------------------
  4630. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4631. {
  4632. if (!NonVisualDataModule->Busy)
  4633. {
  4634. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4635. {
  4636. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4637. }
  4638. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4639. {
  4640. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4641. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4642. {
  4643. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4644. // But so far it's not a problem, as it does what we want.
  4645. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4646. if (((Action->ShortCut == KeyShortCut) ||
  4647. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4648. AllowedAction(Action, aaShortCut))
  4649. {
  4650. // Has to be called before the action as the dialog char is already in queue.
  4651. // So when the action consumes message queue, we already need to have the
  4652. // FIgnoreNextDialogChar set
  4653. KeyProcessed(Key, Shift);
  4654. // Reset reference to previous component (when menu/toolbar was clicked).
  4655. // Needed to detect that action was invoked by keyboard shortcut
  4656. // in TNonVisualDataModule::ExplorerActionsExecute
  4657. Action->ActionComponent = NULL;
  4658. Action->Execute();
  4659. return;
  4660. }
  4661. }
  4662. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4663. {
  4664. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4665. {
  4666. KeyProcessed(Key, Shift);
  4667. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4668. return;
  4669. }
  4670. }
  4671. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4672. {
  4673. KeyProcessed(Key, Shift);
  4674. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4675. }
  4676. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4677. if (IsCustomShortCut(CustomShortCut))
  4678. {
  4679. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4680. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4681. if (WinConfiguration->SharedBookmarks != NULL)
  4682. {
  4683. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4684. if ((Bookmark != NULL) &&
  4685. OpenBookmark(FCurrentSide, Bookmark))
  4686. {
  4687. KeyProcessed(Key, Shift);
  4688. }
  4689. }
  4690. }
  4691. }
  4692. }
  4693. TForm::KeyDown(Key, Shift);
  4694. }
  4695. //---------------------------------------------------------------------------
  4696. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4697. {
  4698. bool OpenedTerminal = false;
  4699. UnicodeString ProtocolName;
  4700. UnicodeString ProtocolNameHint;
  4701. UnicodeString SecurityProtocolName;
  4702. UnicodeString DurationHint;
  4703. // See the comment in TScpCommanderForm::UpdateControls()
  4704. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4705. if (ActiveTerminal != NULL)
  4706. {
  4707. // Active is not enough here
  4708. if (ActiveTerminal->Status == ssOpened)
  4709. {
  4710. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4711. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4712. if (FileSystemInfo.ProtocolName.IsEmpty())
  4713. {
  4714. ProtocolName = SessionInfo.ProtocolName;
  4715. }
  4716. else
  4717. {
  4718. ProtocolName = FileSystemInfo.ProtocolName;
  4719. }
  4720. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4721. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4722. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4723. OpenedTerminal = true;
  4724. }
  4725. else if (ActiveTerminal->Status > ssClosed)
  4726. {
  4727. ProtocolName = LoadStr(STATUS_CONNECTING);
  4728. }
  4729. else
  4730. {
  4731. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4732. }
  4733. }
  4734. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4735. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4736. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4737. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4738. if (ProtocolStatusPanel->Caption != ProtocolName)
  4739. {
  4740. ProtocolStatusPanel->Caption = ProtocolName;
  4741. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4742. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4743. }
  4744. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4745. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4746. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4747. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4748. SecurityStatusPanel->Enabled = SecurityEnabled;
  4749. UnicodeString SecurityProtocolHint;
  4750. if (SecurityEnabled)
  4751. {
  4752. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4753. }
  4754. else
  4755. {
  4756. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4757. }
  4758. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4759. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4760. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4761. DurationStatusPanel->Hint = DurationHint;
  4762. UpdateStatusBar();
  4763. }
  4764. //---------------------------------------------------------------------------
  4765. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4766. {
  4767. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4768. DebugAssert(SessionStatusBar != NULL);
  4769. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4770. if (!FNote.IsEmpty())
  4771. {
  4772. NoteStatusPanel->Caption = FNote;
  4773. }
  4774. else
  4775. {
  4776. UpdateStatusPanelText(NoteStatusPanel);
  4777. }
  4778. NoteStatusPanel->Hint = FNoteHints;
  4779. UnicodeString LoginTime;
  4780. // See the comment in TScpCommanderForm::UpdateControls()
  4781. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4782. // Active is not enough here
  4783. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4784. {
  4785. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4786. LoginTime = FormatDateTimeSpan(Now() - SessionInfo.LoginTime);
  4787. }
  4788. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4789. DurationStatusPanel->Caption = LoginTime;
  4790. }
  4791. //---------------------------------------------------------------------------
  4792. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4793. {
  4794. Panel->Caption = L"";
  4795. }
  4796. //---------------------------------------------------------------------------
  4797. void __fastcall TCustomScpExplorerForm::Idle()
  4798. {
  4799. if (FShowing ||
  4800. // Particularly for
  4801. // - Standalone editing
  4802. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4803. // while "keeping remote directory up to date".
  4804. StandaloneOperation)
  4805. {
  4806. FEditorManager->Check();
  4807. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4808. // that needs to know if queue view is visible (and it may be closed after queue update)
  4809. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4810. }
  4811. if (FShowing)
  4812. {
  4813. if (!NonVisualDataModule->Busy &&
  4814. // Menu is opened or drag&drop is going on
  4815. (Mouse->Capture == NULL))
  4816. {
  4817. if (FRefreshRemoteDirectory)
  4818. {
  4819. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4820. {
  4821. Terminal->RefreshDirectory();
  4822. }
  4823. FRefreshRemoteDirectory = false;
  4824. }
  4825. if (FRefreshLocalDirectory)
  4826. {
  4827. ReloadLocalDirectory();
  4828. FRefreshLocalDirectory = false;
  4829. }
  4830. if (WinConfiguration->RefreshRemotePanel)
  4831. {
  4832. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4833. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4834. {
  4835. RemoteDirView->ReloadDirectory();
  4836. }
  4837. }
  4838. }
  4839. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4840. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4841. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4842. // 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.
  4843. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4844. MenuToolbar->Idle();
  4845. }
  4846. if (FShowing || StandaloneOperation)
  4847. {
  4848. if (FQueueStatusInvalidated)
  4849. {
  4850. UpdateQueueStatus(false);
  4851. }
  4852. RefreshQueueItems();
  4853. }
  4854. if (FShowing)
  4855. {
  4856. UpdateStatusBar();
  4857. }
  4858. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4859. {
  4860. FClipboardFakeMonitorsPendingReset = false;
  4861. FClipboardFakeMonitors.reset(NULL);
  4862. }
  4863. FIgnoreNextDialogChar = 0;
  4864. }
  4865. //---------------------------------------------------------------------------
  4866. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4867. {
  4868. try
  4869. {
  4870. FUserActionTimer->Enabled = false;
  4871. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4872. {
  4873. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4874. {
  4875. FPendingQueueActionItem->ProcessUserAction();
  4876. }
  4877. }
  4878. }
  4879. __finally
  4880. {
  4881. FPendingQueueActionItem = NULL;
  4882. }
  4883. }
  4884. //---------------------------------------------------------------------------
  4885. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4886. {
  4887. if (WinConfiguration->MinimizeToTray)
  4888. {
  4889. UpdateTrayIcon();
  4890. FTrayIcon->Visible = true;
  4891. if (Visible)
  4892. {
  4893. ShowWindow(Handle, SW_HIDE);
  4894. }
  4895. }
  4896. }
  4897. //---------------------------------------------------------------------------
  4898. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4899. {
  4900. // WORKAROUND
  4901. // When restoring maximized window from minimization,
  4902. // rarely some controls do not align properly.
  4903. // Two instances seen (both for Commander):
  4904. // - When restoring, window is temporarily narrower (not maximized),
  4905. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4906. // Once maximized already, top dock shinks back, but the session PageControl,
  4907. // do not align up, leaving space between TopDock and PageControl.
  4908. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4909. for (int Index = 0; Index < ControlCount; Index++)
  4910. {
  4911. RealignControl(Controls[Index]);
  4912. }
  4913. if (FTrayIcon->Visible)
  4914. {
  4915. FTrayIcon->Visible = false;
  4916. if (Visible)
  4917. {
  4918. ShowWindow(Handle, SW_SHOW);
  4919. }
  4920. }
  4921. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4922. {
  4923. FNeedSession = false;
  4924. NonVisualDataModule->StartBusy();
  4925. try
  4926. {
  4927. NeedSession(true);
  4928. }
  4929. __finally
  4930. {
  4931. NonVisualDataModule->EndBusy();
  4932. }
  4933. }
  4934. }
  4935. //---------------------------------------------------------------------------
  4936. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4937. {
  4938. FTrayIcon->Hint = GetMainForm()->Caption;
  4939. }
  4940. //---------------------------------------------------------------------------
  4941. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4942. {
  4943. UpdateTrayIcon();
  4944. }
  4945. //---------------------------------------------------------------------------
  4946. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4947. {
  4948. // workaround
  4949. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4950. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4951. // But WM_ACTIVATEAPP message can be received even when
  4952. // the main window is minimized to the tray and internal editor window is focused
  4953. // (after another application was previously active)
  4954. if (::IsIconic(Handle))
  4955. {
  4956. if (!IsAppIconic())
  4957. {
  4958. SetAppIconic(true);
  4959. }
  4960. }
  4961. ::ApplicationRestore();
  4962. Application->BringToFront();
  4963. }
  4964. //---------------------------------------------------------------------------
  4965. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4966. {
  4967. RestoreApp();
  4968. }
  4969. //---------------------------------------------------------------------------
  4970. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4971. {
  4972. if (OpenInNewWindow())
  4973. {
  4974. ExecuteNewInstance(SessionUrl);
  4975. }
  4976. else
  4977. {
  4978. TTerminalManager::Instance()->NewSession(SessionUrl);
  4979. }
  4980. }
  4981. //---------------------------------------------------------------------------
  4982. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4983. {
  4984. NewSession();
  4985. }
  4986. //---------------------------------------------------------------------------
  4987. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4988. {
  4989. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4990. StoredSessions->NewSession(SessionName, SessionData);
  4991. // modified only, explicit
  4992. StoredSessions->Save(false, true);
  4993. // encode session name because of slashes in hierarchical sessions
  4994. return EncodeUrlString(SessionName);
  4995. }
  4996. //---------------------------------------------------------------------------
  4997. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4998. {
  4999. // current working directories become defaults here, what is not right
  5000. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5001. return SaveHiddenDuplicateSession(SessionData.get());
  5002. }
  5003. //---------------------------------------------------------------------------
  5004. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  5005. {
  5006. if (OpenInNewWindow())
  5007. {
  5008. ExecuteNewInstance(CreateHiddenDuplicateSession());
  5009. }
  5010. else
  5011. {
  5012. // current working directories become defaults here, what is not right
  5013. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5014. TTerminalManager * Manager = TTerminalManager::Instance();
  5015. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  5016. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  5017. // We definitely want these
  5018. ASession->Disconnected = ManagedSession->Disconnected;
  5019. // Not sure about these two
  5020. ASession->Permanent = ManagedSession->Permanent;
  5021. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  5022. Manager->ActiveSession = ASession;
  5023. }
  5024. }
  5025. //---------------------------------------------------------------------------
  5026. void __fastcall TCustomScpExplorerForm::RenameTab()
  5027. {
  5028. UnicodeString Name = ManagedSession->SessionData->SessionName;
  5029. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  5030. // When submitting the default name, do not fix it
  5031. (Name != ManagedSession->SessionData->SessionName))
  5032. {
  5033. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  5034. TSessionData::ValidateName(Name);
  5035. // This is inconsistent with how color (for example) is handled.
  5036. ManagedSession->SessionData->Name = Name;
  5037. UpdateControls();
  5038. // Add/Remove distinguishing paths from sessions of the same name.
  5039. SessionListChanged();
  5040. }
  5041. }
  5042. //---------------------------------------------------------------------------
  5043. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  5044. {
  5045. DebugAssert(Queue != NULL);
  5046. bool Result = Queue->IsEmpty;
  5047. if (!Result)
  5048. {
  5049. SetFocus();
  5050. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  5051. }
  5052. return Result;
  5053. }
  5054. //---------------------------------------------------------------------------
  5055. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  5056. {
  5057. return CanCloseQueue(FQueue);
  5058. }
  5059. //---------------------------------------------------------------------------
  5060. void __fastcall TCustomScpExplorerForm::CloseTab()
  5061. {
  5062. if (CanCloseQueue())
  5063. {
  5064. TTerminalManager::Instance()->FreeActiveTerminal();
  5065. }
  5066. }
  5067. //---------------------------------------------------------------------------
  5068. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  5069. {
  5070. if (CanCloseQueue())
  5071. {
  5072. TTerminalManager::Instance()->DisconnectActiveTerminal();
  5073. }
  5074. }
  5075. //---------------------------------------------------------------------------
  5076. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  5077. {
  5078. DetachTerminal(Terminal);
  5079. RemoteDirView->Terminal = Terminal;
  5080. UpdateRemotePathComboBox(false);
  5081. InitStatusBar();
  5082. UpdateControls();
  5083. }
  5084. //---------------------------------------------------------------------------
  5085. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  5086. {
  5087. InitStatusBar();
  5088. }
  5089. //---------------------------------------------------------------------------
  5090. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  5091. {
  5092. if (DebugAlwaysTrue(!Terminal->Active))
  5093. {
  5094. TTerminalManager::Instance()->ReconnectActiveTerminal();
  5095. }
  5096. }
  5097. //---------------------------------------------------------------------------
  5098. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  5099. {
  5100. if (OpenInNewWindow())
  5101. {
  5102. // encode session name because of slashes in hierarchical sessions
  5103. ExecuteNewInstance(EncodeUrlString(Data->Name));
  5104. }
  5105. else
  5106. {
  5107. TTerminalManager * Manager = TTerminalManager::Instance();
  5108. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  5109. Manager->ActiveSession = Terminal;
  5110. }
  5111. }
  5112. //---------------------------------------------------------------------------
  5113. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  5114. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  5115. {
  5116. TTerminalManager * Manager = TTerminalManager::Instance();
  5117. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5118. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  5119. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  5120. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  5121. {
  5122. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  5123. // FirstSession can be null, if none of the workspace sites exist anymore
  5124. // or if all workspace sessions are not suported by the interface
  5125. // (when trying to open all local-local workspace in "explorer" interface)
  5126. if (FirstSession != NULL)
  5127. {
  5128. if (!ConnectFirstTerminal)
  5129. {
  5130. FirstSession->Disconnected = true;
  5131. FirstSession->DisconnectedTemporarily = true;
  5132. }
  5133. Manager->ActiveSession = FirstSession;
  5134. }
  5135. }
  5136. }
  5137. //---------------------------------------------------------------------------
  5138. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  5139. {
  5140. if (OpenInNewWindow())
  5141. {
  5142. ExecuteNewInstance(Name);
  5143. }
  5144. else
  5145. {
  5146. DoOpenFolderOrWorkspace(Name, true, false);
  5147. }
  5148. }
  5149. //---------------------------------------------------------------------------
  5150. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  5151. bool &CanClose)
  5152. {
  5153. AppLog(L"Windows close query");
  5154. // See the comment in CloseApp()
  5155. if (NonVisualDataModule->Busy)
  5156. {
  5157. CanClose = false;
  5158. }
  5159. else
  5160. {
  5161. TTerminalManager * Manager = TTerminalManager::Instance();
  5162. int ActiveSessions = 0;
  5163. for (int Index = 0; Index < Manager->Count; Index++)
  5164. {
  5165. if (Manager->Sessions[Index]->Active)
  5166. {
  5167. ActiveSessions++;
  5168. }
  5169. }
  5170. // Confirm, when there's at least one active remote session or
  5171. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  5172. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  5173. WinConfiguration->ConfirmClosingSession)
  5174. {
  5175. unsigned int Result;
  5176. TMessageParams Params(mpNeverAskAgainCheck);
  5177. UnicodeString Message;
  5178. int Answers = qaOK | qaCancel;
  5179. // The current session is the only active session
  5180. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5181. {
  5182. if (!WinConfiguration->AutoSaveWorkspace)
  5183. {
  5184. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5185. }
  5186. else
  5187. {
  5188. Message = LoadStr(CLOSE_SESSION);
  5189. }
  5190. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5191. }
  5192. // Multiple active sessions or one active session, but it's not the current one
  5193. else if (ActiveSessions > 0)
  5194. {
  5195. if (!WinConfiguration->AutoSaveWorkspace)
  5196. {
  5197. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5198. }
  5199. else
  5200. {
  5201. Message = LoadStr(CLOSE_SESSIONS);
  5202. }
  5203. }
  5204. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5205. else
  5206. {
  5207. DebugAlwaysTrue(Manager->Count > 1);
  5208. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5209. Message = LoadStr(CLOSE_WORKSPACE);
  5210. }
  5211. UnicodeString Note;
  5212. if (WinConfiguration->AutoSaveWorkspace)
  5213. {
  5214. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5215. }
  5216. else
  5217. {
  5218. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5219. Answers = qaYes | qaNo | qaCancel;
  5220. }
  5221. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5222. SetFocus();
  5223. Result = MessageDialog(Message, qtConfirmation,
  5224. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5225. if (Result == qaNeverAskAgain)
  5226. {
  5227. WinConfiguration->ConfirmClosingSession = false;
  5228. }
  5229. if (Result == qaNo)
  5230. {
  5231. CanClose = SaveWorkspace(true);
  5232. // note that the workspace will be saved redundatly again from FormClose
  5233. }
  5234. else
  5235. {
  5236. CanClose =
  5237. (Result == qaOK) ||
  5238. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5239. (Result == qaNeverAskAgain);
  5240. }
  5241. }
  5242. if (CanClose && HasActiveTerminal())
  5243. {
  5244. CanClose = CanCloseQueue();
  5245. }
  5246. }
  5247. if (CanClose)
  5248. {
  5249. CanClose =
  5250. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5251. FEditorManager->CloseExternalFilesWithoutProcess();
  5252. if (CanClose)
  5253. {
  5254. while (CanClose && (FLocalEditors->Count > 0))
  5255. {
  5256. int PrevCount = FLocalEditors->Count;
  5257. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5258. CanClose = (FLocalEditors->Count < PrevCount);
  5259. }
  5260. if (CanClose)
  5261. {
  5262. CanClose = FEditorManager->Empty(true);
  5263. if (!CanClose)
  5264. {
  5265. SetFocus();
  5266. CanClose =
  5267. (MessageDialog(
  5268. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5269. }
  5270. }
  5271. }
  5272. }
  5273. }
  5274. //---------------------------------------------------------------------------
  5275. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5276. {
  5277. TForm * Form = dynamic_cast<TForm *>(Sender);
  5278. DebugAssert(Form != NULL);
  5279. Form->Close();
  5280. }
  5281. //---------------------------------------------------------------------------
  5282. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5283. {
  5284. TForm * Form = dynamic_cast<TForm *>(Sender);
  5285. delete Form;
  5286. }
  5287. //---------------------------------------------------------------------------
  5288. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5289. {
  5290. while (FLocalEditors->Count > 0)
  5291. {
  5292. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5293. }
  5294. }
  5295. //---------------------------------------------------------------------------
  5296. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5297. TObject *Sender)
  5298. {
  5299. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5300. try
  5301. {
  5302. SetProperties(osRemote, FileList);
  5303. }
  5304. __finally
  5305. {
  5306. delete FileList;
  5307. }
  5308. }
  5309. //---------------------------------------------------------------------------
  5310. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5311. {
  5312. if (value)
  5313. {
  5314. if (Component == fcCustomCommandsBand)
  5315. {
  5316. UpdateCustomCommandsToolbar();
  5317. }
  5318. if (Component == fcQueueFileList)
  5319. {
  5320. DebugAssert(!QueueFileListSplitter->Visible);
  5321. AdjustQueueLayout();
  5322. UpdateQueueFileList();
  5323. }
  5324. }
  5325. }
  5326. //---------------------------------------------------------------------------
  5327. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5328. {
  5329. int MinHeightLimit =
  5330. GetStaticQueuePanelComponentsHeight() +
  5331. QueueFileListSplitter->Height +
  5332. QueueFileList->Height +
  5333. GetMinQueueViewHeight();
  5334. if (QueuePanel->ClientHeight < MinHeightLimit)
  5335. {
  5336. QueuePanel->ClientHeight = MinHeightLimit;
  5337. }
  5338. }
  5339. //---------------------------------------------------------------------------
  5340. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5341. {
  5342. TControl * Control = GetComponent(Component);
  5343. DebugAssert(Control);
  5344. bool Changed = (Control->Visible != value);
  5345. if (Changed)
  5346. {
  5347. ComponentShowing(Component, value);
  5348. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5349. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5350. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5351. if (value)
  5352. {
  5353. int RemainingHeight = Control->Parent->ClientHeight;
  5354. int RemainingWidth = Control->Parent->ClientWidth;
  5355. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5356. {
  5357. TControl * ChildControl = Control->Parent->Controls[i];
  5358. if (ChildControl->Visible)
  5359. {
  5360. switch (ChildControl->Align)
  5361. {
  5362. case alTop:
  5363. case alBottom:
  5364. RemainingHeight -= ChildControl->Height;
  5365. break;
  5366. case alLeft:
  5367. case alRight:
  5368. RemainingWidth -= ChildControl->Width;
  5369. break;
  5370. }
  5371. }
  5372. }
  5373. int Reserve = ScaleByTextHeight(this, 32);
  5374. // queue in explorer, trees in commander
  5375. if (Control->Height > RemainingHeight - Reserve)
  5376. {
  5377. Control->Height = RemainingHeight / 2;
  5378. }
  5379. if (Control->Width > RemainingWidth - Reserve)
  5380. {
  5381. Control->Width = RemainingWidth / 2;
  5382. }
  5383. }
  5384. Control->Visible = value;
  5385. if (WasFocused && Visible)
  5386. {
  5387. DirView(osCurrent)->SetFocus();
  5388. }
  5389. FixControlsPlacement();
  5390. }
  5391. }
  5392. //---------------------------------------------------------------------------
  5393. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5394. {
  5395. TControl * Control = GetComponent(Component);
  5396. if (Control == NULL)
  5397. {
  5398. return false;
  5399. }
  5400. else
  5401. {
  5402. return Control->Visible;
  5403. }
  5404. }
  5405. //---------------------------------------------------------------------------
  5406. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5407. {
  5408. bool Result;
  5409. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5410. {
  5411. Result = !IsUWP();
  5412. }
  5413. else
  5414. {
  5415. Result = true;
  5416. }
  5417. return Result;
  5418. }
  5419. //---------------------------------------------------------------------------
  5420. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5421. {
  5422. if (DirView->ItemFocused != NULL)
  5423. {
  5424. DirView->ItemFocused->MakeVisible(false);
  5425. }
  5426. }
  5427. //---------------------------------------------------------------------------
  5428. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5429. {
  5430. MakeFocusedItemVisible(RemoteDirView);
  5431. QueueSplitter->Visible = QueuePanel->Visible;
  5432. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5433. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5434. TControl * TopControlsOrder[] =
  5435. { TopDock, MessageDock, SessionsPageControl };
  5436. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5437. TControl * QueueControlsOrder[] =
  5438. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5439. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5440. }
  5441. //---------------------------------------------------------------------------
  5442. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5443. {
  5444. switch (Component) {
  5445. case fcStatusBar: return RemoteStatusBar;
  5446. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5447. case fcQueueView: return QueuePanel;
  5448. case fcQueueToolbar: return QueueDock;
  5449. case fcRemoteTree: return RemoteDrivePanel;
  5450. case fcSessionsTabs: return SessionsPageControl;
  5451. case fcQueueFileList: return QueueFileList;
  5452. default: return NULL;
  5453. }
  5454. }
  5455. //---------------------------------------------------------------------------
  5456. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5457. TObject *Sender, TListColumn *Column, TPoint &Point)
  5458. {
  5459. DebugAssert(NonVisualDataModule && Column && Sender);
  5460. NonVisualDataModule->ListColumn = Column;
  5461. TPopupMenu * DirViewColumnMenu;
  5462. if (Sender == DirView(osOther))
  5463. {
  5464. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5465. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5466. bool SizeColumn = (Column->Index == uvSize);
  5467. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5468. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5469. }
  5470. else
  5471. {
  5472. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5473. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5474. bool SizeColumn = (Column->Index == dvSize);
  5475. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5476. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5477. }
  5478. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5479. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5480. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5481. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5482. }
  5483. //---------------------------------------------------------------------------
  5484. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5485. TObject *Sender, TListItem *Item, bool &AllowExec)
  5486. {
  5487. DoDirViewExecFile(Sender, Item, AllowExec);
  5488. }
  5489. //---------------------------------------------------------------------------
  5490. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5491. TListItem * Item, bool & AllowExec)
  5492. {
  5493. DebugAssert(Sender && Item && Configuration);
  5494. DebugAssert(AllowExec);
  5495. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5496. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5497. bool Remote = !IsSideLocalBrowser(Side);
  5498. DebugAssert(Item == ADirView->ItemFocused);
  5499. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5500. TResolvedDoubleClickAction Action;
  5501. if (FForceExecution)
  5502. {
  5503. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5504. }
  5505. else
  5506. {
  5507. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5508. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5509. }
  5510. if (Action == rdcaCopy)
  5511. {
  5512. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5513. UnlockWindow();
  5514. try
  5515. {
  5516. if (IsLocalBrowserMode())
  5517. {
  5518. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5519. }
  5520. else
  5521. {
  5522. if (HasActiveTerminal())
  5523. {
  5524. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5525. }
  5526. }
  5527. }
  5528. __finally
  5529. {
  5530. LockWindow();
  5531. }
  5532. AllowExec = false;
  5533. }
  5534. else if (Action == rdcaEdit)
  5535. {
  5536. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5537. {
  5538. ExecuteFile(Side, efDefaultEditor);
  5539. AllowExec = false;
  5540. }
  5541. }
  5542. else if (Action == rdcaOpen)
  5543. {
  5544. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5545. {
  5546. ExecuteFile(osRemote, efShell);
  5547. AllowExec = false;
  5548. }
  5549. }
  5550. }
  5551. //---------------------------------------------------------------------------
  5552. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5553. {
  5554. return ((Side == osRemote) || (Side == osCurrent));
  5555. }
  5556. //---------------------------------------------------------------------------
  5557. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5558. {
  5559. DebugFail();
  5560. }
  5561. //---------------------------------------------------------------------------
  5562. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5563. {
  5564. DebugFail();
  5565. }
  5566. //---------------------------------------------------------------------------
  5567. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5568. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5569. {
  5570. TSynchronizeParamType Params;
  5571. Params.LocalDirectory = LocalDirectory;
  5572. Params.RemoteDirectory = RemoteDirectory;
  5573. int UnusedParams =
  5574. (GUIConfiguration->SynchronizeParams &
  5575. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5576. TTerminal::spCaseSensitive | TTerminal::spMirror | TTerminal::spByChecksum));
  5577. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5578. Params.Options = GUIConfiguration->SynchronizeOptions;
  5579. bool SaveSettings = false;
  5580. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5581. &DoSynchronizeTooManyDirectories);
  5582. DebugAssert(FSynchronizeController == NULL);
  5583. FSynchronizeController = &Controller;
  5584. bool Result;
  5585. try
  5586. {
  5587. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5588. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5589. int Options =
  5590. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5591. DebugAssert(FOnFeedSynchronizeError == NULL);
  5592. DebugAssert(FOnSynchronizeAbort == NULL);
  5593. DebugAssert(FSynchronizeTerminal == NULL);
  5594. FSynchronizeTerminal = Terminal;
  5595. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5596. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5597. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5598. if (Result)
  5599. {
  5600. if (SaveSettings)
  5601. {
  5602. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5603. GUIConfiguration->SynchronizeOptions = Params.Options;
  5604. }
  5605. else
  5606. {
  5607. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5608. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5609. FLAGSET(Params.Options, soSynchronize))
  5610. {
  5611. GUIConfiguration->SynchronizeOptions =
  5612. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5613. soSynchronize;
  5614. }
  5615. }
  5616. LocalDirectory = Params.LocalDirectory;
  5617. RemoteDirectory = Params.RemoteDirectory;
  5618. }
  5619. }
  5620. __finally
  5621. {
  5622. FSynchronizeController = NULL;
  5623. DebugAssert(FOnFeedSynchronizeError == NULL);
  5624. FOnFeedSynchronizeError = NULL;
  5625. DebugAssert(FOnSynchronizeAbort == NULL);
  5626. FOnSynchronizeAbort = NULL;
  5627. FSynchronizeTerminal = NULL;
  5628. }
  5629. return Result;
  5630. }
  5631. //---------------------------------------------------------------------------
  5632. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5633. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5634. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5635. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5636. TSynchronizeOptions * Options, bool Full)
  5637. {
  5638. // Keep-up-to-date mode
  5639. if (Terminal->Status == ssOpened)
  5640. {
  5641. try
  5642. {
  5643. int PParams = Params.Params;
  5644. if (!Full)
  5645. {
  5646. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5647. if (FLAGSET(Params.Options, soRecurse))
  5648. {
  5649. PParams |= TTerminal::spSubDirs;
  5650. }
  5651. }
  5652. else
  5653. {
  5654. // if keepuptodate is non-recursive,
  5655. // full sync before has to be non-recursive as well
  5656. if (FLAGCLEAR(Params.Options, soRecurse))
  5657. {
  5658. PParams |= TTerminal::spNoRecurse;
  5659. }
  5660. }
  5661. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5662. PParams, Checklist, Options);
  5663. }
  5664. catch (Exception & E)
  5665. {
  5666. if (!Terminal->Active)
  5667. {
  5668. ShowExtendedExceptionEx(Terminal, &E);
  5669. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5670. if (!Terminal->Active)
  5671. {
  5672. throw;
  5673. }
  5674. }
  5675. else
  5676. {
  5677. if (FLAGSET(Params.Options, soContinueOnError) &&
  5678. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5679. {
  5680. // noop, already logged in MoreMessageDialog
  5681. }
  5682. else
  5683. {
  5684. // We get mostly EAbort here, so this is noop.
  5685. // Exception is an error when listing directories while looking for differences.
  5686. ShowExtendedExceptionEx(Terminal, &E);
  5687. throw;
  5688. }
  5689. }
  5690. }
  5691. }
  5692. }
  5693. //---------------------------------------------------------------------------
  5694. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5695. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5696. const UnicodeString ErrorStr)
  5697. {
  5698. if (!Directory.IsEmpty())
  5699. {
  5700. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5701. }
  5702. else
  5703. {
  5704. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5705. }
  5706. }
  5707. //---------------------------------------------------------------------------
  5708. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5709. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5710. {
  5711. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5712. {
  5713. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5714. }
  5715. else
  5716. {
  5717. TMessageParams Params(mpNeverAskAgainCheck);
  5718. unsigned int Result = MessageDialog(
  5719. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5720. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5721. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5722. {
  5723. MaxDirectories *= 2;
  5724. if (Result == qaNeverAskAgain)
  5725. {
  5726. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5727. }
  5728. }
  5729. else
  5730. {
  5731. Abort();
  5732. }
  5733. }
  5734. }
  5735. //---------------------------------------------------------------------------
  5736. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5737. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5738. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5739. TSynchronizeOptions * Options)
  5740. {
  5741. // Keep-up-to-date mode
  5742. DebugAssert(!FAutoOperation);
  5743. void * BatchStorage;
  5744. BatchStart(BatchStorage);
  5745. FAutoOperation = true;
  5746. bool AnyOperation = false;
  5747. TDateTime StartTime = Now();
  5748. TSynchronizeChecklist * AChecklist = NULL;
  5749. try
  5750. {
  5751. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5752. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5753. {
  5754. FSynchronizeProgressForm->Start();
  5755. }
  5756. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5757. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5758. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5759. Options);
  5760. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5761. SAFE_DESTROY(FSynchronizeProgressForm);
  5762. AnyOperation = (AChecklist->CheckedCount > 0);
  5763. if (AnyOperation)
  5764. {
  5765. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5766. CreateProgressForm(&SynchronizeProgress);
  5767. try
  5768. {
  5769. Terminal->SynchronizeApply(
  5770. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5771. SynchronizeProcessedItem, NULL, NULL, NULL);
  5772. }
  5773. __finally
  5774. {
  5775. DestroyProgressForm();
  5776. }
  5777. }
  5778. }
  5779. __finally
  5780. {
  5781. if (Checklist == NULL)
  5782. {
  5783. delete AChecklist;
  5784. }
  5785. else
  5786. {
  5787. *Checklist = AChecklist;
  5788. }
  5789. FAutoOperation = false;
  5790. SAFE_DESTROY(FSynchronizeProgressForm);
  5791. BatchEnd(BatchStorage);
  5792. ReloadLocalDirectory();
  5793. if (AnyOperation)
  5794. {
  5795. OperationComplete(StartTime);
  5796. }
  5797. }
  5798. }
  5799. //---------------------------------------------------------------------------
  5800. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5801. {
  5802. // can be called from command line
  5803. return Visible &&
  5804. ((DirView(osRemote)->SelCount > 0) ||
  5805. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5806. }
  5807. //---------------------------------------------------------------------------
  5808. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5809. {
  5810. LogSynchronizeEvent(Terminal, Message);
  5811. }
  5812. //---------------------------------------------------------------------------
  5813. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5814. int Params, TSynchronizeOptions & Options)
  5815. {
  5816. DebugAssert(!IsLocalBrowserMode());
  5817. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5818. {
  5819. Options.Filter = new TStringList();
  5820. Options.Filter->CaseSensitive = false;
  5821. Options.Filter->Duplicates = Types::dupAccept;
  5822. if (DirView(osRemote)->SelCount > 0)
  5823. {
  5824. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5825. }
  5826. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5827. {
  5828. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5829. }
  5830. Options.Filter->Sort();
  5831. }
  5832. }
  5833. //---------------------------------------------------------------------------
  5834. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5835. {
  5836. TCopyParamType Defaults;
  5837. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5838. ConfigStorage->AccessMode = smRead;
  5839. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5840. {
  5841. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5842. }
  5843. ConfigStorage.reset(NULL);
  5844. std::unique_ptr<TStringList> Options(new TStringList());
  5845. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5846. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5847. UnicodeString Result;
  5848. if (Options->Count > 0)
  5849. {
  5850. Result =
  5851. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5852. }
  5853. return Result;
  5854. }
  5855. //---------------------------------------------------------------------------
  5856. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5857. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5858. {
  5859. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5860. UnicodeString AdditionalParams =
  5861. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5862. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5863. SerializeCopyParamForCommandLine(CopyParams),
  5864. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5865. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5866. ExecuteNewInstance(SessionName, AdditionalParams);
  5867. }
  5868. //---------------------------------------------------------------------------
  5869. struct TSynchronizeParams
  5870. {
  5871. UnicodeString LocalDirectory;
  5872. UnicodeString RemoteDirectory;
  5873. TSynchronizeMode Mode;
  5874. TSynchronizeChecklist * Checklist;
  5875. int Params;
  5876. TCopyParamType * CopyParam;
  5877. TDateTime CollectElapsed;
  5878. TDateTime * StartTime;
  5879. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5880. };
  5881. //---------------------------------------------------------------------------
  5882. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5883. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5884. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5885. {
  5886. DebugAssert(!FAutoOperation);
  5887. void * BatchStorage;
  5888. BatchStart(BatchStorage);
  5889. FAutoOperation = true;
  5890. TFileOperationStatistics Statistics;
  5891. TDateTime Start = Now();
  5892. try
  5893. {
  5894. Params.OnProcessedItem = OnProcessedItem;
  5895. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5896. CreateProgressForm(&SynchronizeProgress);
  5897. Terminal->SynchronizeApply(
  5898. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5899. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5900. &Statistics);
  5901. }
  5902. __finally
  5903. {
  5904. Params.OnProcessedItem = NULL;
  5905. FAutoOperation = false;
  5906. DestroyProgressForm();
  5907. BatchEnd(BatchStorage);
  5908. ReloadLocalDirectory();
  5909. }
  5910. if (WinConfiguration->SynchronizeSummary)
  5911. {
  5912. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5913. // The statistics should be 0 anyway in this case
  5914. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5915. {
  5916. Message += L"\n";
  5917. if (Statistics.FilesUploaded > 0)
  5918. {
  5919. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5920. }
  5921. if (Statistics.FilesDownloaded > 0)
  5922. {
  5923. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5924. }
  5925. if (Statistics.FilesDeletedLocal > 0)
  5926. {
  5927. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5928. }
  5929. if (Statistics.FilesDeletedRemote > 0)
  5930. {
  5931. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5932. }
  5933. }
  5934. TDateTime Elapsed = (Now() - Start);
  5935. Message +=
  5936. L"\n" +
  5937. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Params.CollectElapsed))) + L"\n" +
  5938. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Elapsed)));
  5939. TMessageParams Params(mpNeverAskAgainCheck);
  5940. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5941. if (Result == qaNeverAskAgain)
  5942. {
  5943. WinConfiguration->SynchronizeSummary = false;
  5944. }
  5945. }
  5946. }
  5947. //---------------------------------------------------------------------------
  5948. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5949. {
  5950. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5951. {
  5952. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5953. }
  5954. // Not set in keep-up-to-date mode - Synchronize() method
  5955. if (Token != NULL)
  5956. {
  5957. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5958. if (Params.OnProcessedItem != NULL)
  5959. {
  5960. Params.OnProcessedItem(NULL, ChecklistItem);
  5961. }
  5962. }
  5963. }
  5964. //---------------------------------------------------------------------------
  5965. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5966. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5967. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5968. {
  5969. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5970. *Params.StartTime = Now();
  5971. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5972. }
  5973. //---------------------------------------------------------------------------
  5974. void TCustomScpExplorerForm::DoQueueSynchronize(void * Token)
  5975. {
  5976. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5977. int CopyParams = TTerminal::GetSynchronizeCopyParams(Params.Params);
  5978. TCopyParamType SyncCopyParam = TTerminal::GetSynchronizeCopyParam(Params.CopyParam, Params.Params);
  5979. bool Parallel = GUIConfiguration->CurrentCopyParam.QueueParallel;
  5980. int Index = 0;
  5981. const TSynchronizeChecklist::TItem * ChecklistItem;
  5982. while (Params.Checklist->GetNextChecked(Index, ChecklistItem))
  5983. {
  5984. TQueueItem * QueueItem = Terminal->SynchronizeToQueue(ChecklistItem, &SyncCopyParam, CopyParams, Parallel);
  5985. AddQueueItem(Queue, QueueItem, Terminal);
  5986. }
  5987. }
  5988. //---------------------------------------------------------------------------
  5989. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5990. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5991. {
  5992. // terminal can be already closed (e.g. dropped connection)
  5993. if (Terminal != NULL)
  5994. {
  5995. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5996. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5997. }
  5998. }
  5999. //---------------------------------------------------------------------------
  6000. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  6001. TOperationSide Side, TStrings * FileList, const UnicodeString & NewFileName, bool TargetIsDirectory, void * Token)
  6002. {
  6003. DebugAssert(!IsLocalBrowserMode());
  6004. TAutoBatch AutoBatch(this);
  6005. TAutoFlag AutoOperationFlag(FAutoOperation);
  6006. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6007. if (Side == osRemote)
  6008. {
  6009. UnicodeString Target;
  6010. UnicodeString FileMask;
  6011. if (TargetIsDirectory)
  6012. {
  6013. Target = NewFileName;
  6014. Terminal->CreateTargetDirectory(Target, faDirectory, Params.CopyParam);
  6015. }
  6016. else
  6017. {
  6018. Target = UnixExtractFileDir(NewFileName);
  6019. FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  6020. }
  6021. RemoteDirView->SaveSelection();
  6022. RemoteDirView->SaveSelectedNames();
  6023. try
  6024. {
  6025. Terminal->MoveFiles(FileList, Target, FileMask, false);
  6026. }
  6027. catch(...)
  6028. {
  6029. RemoteDirView->DiscardSavedSelection();
  6030. throw;
  6031. }
  6032. RemoteDirView->RestoreSelection();
  6033. }
  6034. else if (DebugAlwaysTrue(Side == osLocal))
  6035. {
  6036. std::unique_ptr<TStrings> Directories(CreateSortedStringList());
  6037. if (TargetIsDirectory)
  6038. {
  6039. if (!ForceDirectories(ApiPath(NewFileName)))
  6040. {
  6041. throw EOSExtException(FMTLOAD(CREATE_DIR_ERROR, (NewFileName)));
  6042. }
  6043. Directories->Add(NewFileName);
  6044. }
  6045. else
  6046. {
  6047. Directories->Add(ExtractFileDir(NewFileName));
  6048. }
  6049. for (int Index = 0; Index < FileList->Count; Index++)
  6050. {
  6051. UnicodeString FileName = FileList->Strings[Index];
  6052. UnicodeString Directory = ExtractFileDir(FileName);
  6053. Directories->Add(Directory);
  6054. UnicodeString NewFilePath = NewFileName;
  6055. if (TargetIsDirectory)
  6056. {
  6057. NewFilePath = TPath::Combine(NewFilePath, ExtractFileName(FileName));
  6058. }
  6059. if (!MoveFile(ApiPath(FileName).c_str(), ApiPath(NewFilePath).c_str()))
  6060. {
  6061. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFilePath)));
  6062. }
  6063. }
  6064. for (int Index = 0; Index < Directories->Count; Index++)
  6065. {
  6066. ReloadLocalDirectory(Directories->Strings[Index]);
  6067. }
  6068. }
  6069. }
  6070. //---------------------------------------------------------------------------
  6071. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  6072. {
  6073. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  6074. if (Side == osLocal)
  6075. {
  6076. if (Action == TSynchronizeChecklist::saDownloadNew)
  6077. {
  6078. OpenFolderInExplorer(LocalPath);
  6079. }
  6080. else
  6081. {
  6082. OpenFileInExplorer(CombinePaths(LocalPath, Item->GetFileName()));
  6083. }
  6084. }
  6085. else if (DebugAlwaysTrue(Side == osRemote))
  6086. {
  6087. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  6088. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  6089. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  6090. if (!LocalPath.IsEmpty())
  6091. {
  6092. SessionData->LocalDirectory = LocalPath;
  6093. }
  6094. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  6095. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  6096. }
  6097. }
  6098. //---------------------------------------------------------------------------
  6099. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  6100. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6101. const TCopyParamType * CopyParams)
  6102. {
  6103. UnicodeString SessionName = CreateHiddenDuplicateSession();
  6104. UnicodeString AdditionalParams =
  6105. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  6106. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  6107. SerializeCopyParamForCommandLine(CopyParams),
  6108. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  6109. LocalDirectory, RemoteDirectory, Mode, Params));
  6110. ExecuteNewInstance(SessionName, AdditionalParams);
  6111. }
  6112. //---------------------------------------------------------------------------
  6113. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  6114. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  6115. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  6116. {
  6117. int Result;
  6118. bool SaveSettings = false;
  6119. int Options =
  6120. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  6121. FLAGMASK(!CanCalculateChecksum(), fsoDisableByChecksum) |
  6122. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  6123. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  6124. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  6125. bool Continue =
  6126. ((UseDefaults == 0) ||
  6127. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  6128. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults)) &&
  6129. (FLAGCLEAR(Params, TTerminal::spByChecksum) || EnsureCommandSessionFallback(fcCalculatingChecksum));
  6130. if (Continue)
  6131. {
  6132. Configuration->Usage->Inc(L"Synchronizations");
  6133. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  6134. UpdateCopyParamCounters(CopyParam);
  6135. TSynchronizeOptions SynchronizeOptions;
  6136. GetSynchronizeOptions(Params, SynchronizeOptions);
  6137. if (SaveSettings)
  6138. {
  6139. GUIConfiguration->SynchronizeParams = Params;
  6140. }
  6141. else
  6142. {
  6143. SaveMode = false;
  6144. }
  6145. TDateTime StartTime = Now();
  6146. TSynchronizeChecklist * Checklist = NULL;
  6147. try
  6148. {
  6149. DebugAssert(!FAutoOperation);
  6150. FAutoOperation = true;
  6151. try
  6152. {
  6153. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  6154. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  6155. int Files = -1;
  6156. if (DataList->Count >= 1)
  6157. {
  6158. Files = StrToIntDef(DataList->Strings[0], Files);
  6159. }
  6160. else
  6161. {
  6162. DataList->Add(UnicodeString());
  6163. }
  6164. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  6165. FSynchronizeProgressForm->Start();
  6166. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  6167. static_cast<TTerminal::TSynchronizeMode>(Mode),
  6168. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  6169. &SynchronizeOptions);
  6170. if (Terminal->SessionData->CacheDirectories)
  6171. {
  6172. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  6173. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  6174. }
  6175. }
  6176. __finally
  6177. {
  6178. FAutoOperation = false;
  6179. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  6180. SAFE_DESTROY(FSynchronizeProgressForm);
  6181. }
  6182. TSynchronizeParams SynchronizeParams;
  6183. SynchronizeParams.LocalDirectory = LocalDirectory;
  6184. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  6185. SynchronizeParams.Mode = Mode;
  6186. SynchronizeParams.CopyParam = &CopyParam;
  6187. SynchronizeParams.Params = Params;
  6188. SynchronizeParams.Checklist = Checklist;
  6189. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  6190. SynchronizeParams.StartTime = &StartTime;
  6191. SynchronizeParams.OnProcessedItem = NULL;
  6192. Result = Checklist->Count;
  6193. if (Checklist->Count > 0)
  6194. {
  6195. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  6196. {
  6197. TQueueSynchronizeEvent OnQueueSynchronize = NULL;
  6198. if (Visible && FLAGCLEAR(Params, TTerminal::spTimestamp))
  6199. {
  6200. OnQueueSynchronize = DoQueueSynchronize;
  6201. }
  6202. if (!DoSynchronizeChecklistDialog(
  6203. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  6204. OnQueueSynchronize, DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse,
  6205. &SynchronizeParams))
  6206. {
  6207. Result = -1;
  6208. }
  6209. }
  6210. else
  6211. {
  6212. FullSynchronize(SynchronizeParams, NULL, NULL);
  6213. }
  6214. }
  6215. else
  6216. {
  6217. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  6218. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  6219. }
  6220. }
  6221. __finally
  6222. {
  6223. delete Checklist;
  6224. }
  6225. OperationComplete(StartTime);
  6226. }
  6227. else
  6228. {
  6229. Result = -1;
  6230. }
  6231. return Result;
  6232. }
  6233. //---------------------------------------------------------------------------
  6234. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6235. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6236. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6237. {
  6238. if (Collect)
  6239. {
  6240. DebugAssert(FSynchronizeProgressForm != NULL);
  6241. if (!FSynchronizeProgressForm->Started)
  6242. {
  6243. FSynchronizeProgressForm->Start();
  6244. }
  6245. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6246. SetTaskbarListProgressValue(CompareProgress);
  6247. }
  6248. else
  6249. {
  6250. DebugAssert(FSynchronizeProgressForm == NULL);
  6251. DebugAssert(FProgressForm != NULL);
  6252. }
  6253. }
  6254. //---------------------------------------------------------------------------
  6255. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6256. {
  6257. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6258. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6259. if (File.get() != NULL)
  6260. {
  6261. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6262. Application->ShowMainForm = false;
  6263. Application->Run();
  6264. }
  6265. }
  6266. //---------------------------------------------------------------------------
  6267. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6268. {
  6269. DebugFail();
  6270. }
  6271. //---------------------------------------------------------------------------
  6272. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6273. {
  6274. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6275. SessionData->Assign(ManagedSession->SessionData);
  6276. UpdateSessionData(SessionData.get());
  6277. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6278. if (IsActiveTerminal(ManagedSession))
  6279. {
  6280. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6281. }
  6282. if (!ManagedSession->SessionData->HasSessionName())
  6283. {
  6284. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6285. // once IsWorkspace is cleared
  6286. SessionData->Name = UnicodeString();
  6287. }
  6288. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6289. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6290. SessionData->IsWorkspace = false;
  6291. return SessionData.release();
  6292. }
  6293. //---------------------------------------------------------------------------
  6294. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6295. {
  6296. TSessionData * SessionData = CloneCurrentSessionData();
  6297. try
  6298. {
  6299. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6300. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6301. }
  6302. __finally
  6303. {
  6304. delete SessionData;
  6305. }
  6306. }
  6307. //---------------------------------------------------------------------------
  6308. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6309. {
  6310. TTerminalManager * Manager = TTerminalManager::Instance();
  6311. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6312. if (DebugAlwaysTrue(ManagedSession != NULL))
  6313. {
  6314. // Update ManagedSession->StateData
  6315. UpdateSession(ManagedSession);
  6316. }
  6317. Manager->SaveWorkspace(DataList.get());
  6318. return DataList.release();
  6319. }
  6320. //---------------------------------------------------------------------------
  6321. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6322. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6323. {
  6324. WinConfiguration->LastWorkspace = Name;
  6325. if (!SavePasswords)
  6326. {
  6327. for (int Index = 0; Index < DataList->Count; Index++)
  6328. {
  6329. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6330. if (SessionData->Link.IsEmpty())
  6331. {
  6332. SessionData->ClearSessionPasswords();
  6333. }
  6334. }
  6335. }
  6336. StoredSessions->NewWorkspace(Name, DataList);
  6337. // modified only
  6338. StoredSessions->Save(false, Explicit);
  6339. }
  6340. //---------------------------------------------------------------------------
  6341. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6342. {
  6343. return
  6344. DefaultStr(WinConfiguration->LastWorkspace,
  6345. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6346. }
  6347. //---------------------------------------------------------------------------
  6348. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6349. {
  6350. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6351. bool AnyNonStoredSessionWithPassword = false;
  6352. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6353. bool AllNonStoredSessionsAnonymous = true;
  6354. TTerminalManager * Manager = TTerminalManager::Instance();
  6355. for (int Index = 0; Index < DataList->Count; Index++)
  6356. {
  6357. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6358. if (SessionData->Link.IsEmpty())
  6359. {
  6360. if (SessionData->HasAnySessionPassword())
  6361. {
  6362. AnyNonStoredSessionWithPassword = true;
  6363. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6364. {
  6365. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6366. }
  6367. }
  6368. if (!SameText(SessionData->UserName, AnonymousUserName))
  6369. {
  6370. AllNonStoredSessionsAnonymous = false;
  6371. }
  6372. }
  6373. }
  6374. bool SavePasswords;
  6375. bool * PSavePasswords;
  6376. bool NotRecommendedSavingPasswords =
  6377. !CustomWinConfiguration->UseMasterPassword &&
  6378. !AllNonStoredSessionsAnonymous;
  6379. if (Configuration->DisablePasswordStoring ||
  6380. !AnyNonStoredSessionWithPassword)
  6381. {
  6382. PSavePasswords = NULL;
  6383. SavePasswords = false;
  6384. }
  6385. else
  6386. {
  6387. PSavePasswords = &SavePasswords;
  6388. SavePasswords =
  6389. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6390. !NotRecommendedSavingPasswords;
  6391. }
  6392. UnicodeString Name = WorkspaceName();
  6393. bool CreateShortcut = false;
  6394. bool Result =
  6395. DoSaveWorkspaceDialog(
  6396. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6397. EnableAutoSave);
  6398. if (Result)
  6399. {
  6400. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6401. if (CreateShortcut)
  6402. {
  6403. TOperationVisualizer Visualizer;
  6404. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6405. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6406. }
  6407. if (EnableAutoSave)
  6408. {
  6409. WinConfiguration->AutoSaveWorkspace = true;
  6410. WinConfiguration->AutoWorkspace = Name;
  6411. if (PSavePasswords != NULL)
  6412. {
  6413. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6414. }
  6415. }
  6416. }
  6417. return Result;
  6418. }
  6419. //---------------------------------------------------------------------------
  6420. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6421. {
  6422. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6423. DebugAssert(Session == ManagedSession);
  6424. if (!IsLocalBrowserMode())
  6425. {
  6426. SAFE_DESTROY(Session->RemoteExplorerState);
  6427. if (WinConfiguration->PreservePanelState)
  6428. {
  6429. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6430. }
  6431. }
  6432. UpdateSessionData(Session->StateData);
  6433. }
  6434. //---------------------------------------------------------------------------
  6435. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6436. {
  6437. // Keep in sync with TSessionData::CopyStateData
  6438. DebugAssert(Data != NULL);
  6439. // This is inconsistent with how color (for example) is handled.
  6440. Data->Name = ManagedSession->SessionData->Name;
  6441. if (!IsLocalBrowserMode())
  6442. {
  6443. // cannot use RemoteDirView->Path, because it is empty if connection
  6444. // was already closed
  6445. // also only peek, we may not be connected at all atm
  6446. // so make sure we do not try retrieving current directory from the server
  6447. // (particularly with FTP)
  6448. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6449. if (!ACurrentDirectory.IsEmpty())
  6450. {
  6451. Data->RemoteDirectory = ACurrentDirectory;
  6452. }
  6453. }
  6454. Data->Color = SessionColor;
  6455. }
  6456. //---------------------------------------------------------------------------
  6457. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6458. {
  6459. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6460. DebugAssert(Toolbar != NULL);
  6461. for (int i = 0; i < Toolbar->Items->Count; i++)
  6462. {
  6463. TTBXCustomDropDownItem * DropDownItem;
  6464. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6465. if (DropDownItem != NULL)
  6466. {
  6467. ToolbarItemResize(DropDownItem,
  6468. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6469. Toolbar->NonClientWidth);
  6470. break;
  6471. }
  6472. }
  6473. }
  6474. //---------------------------------------------------------------------------
  6475. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6476. {
  6477. Item->EditWidth = Width;
  6478. }
  6479. //---------------------------------------------------------------------------
  6480. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6481. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6482. {
  6483. for (int i = 0; i < Toolbar->Items->Count; i++)
  6484. {
  6485. TTBXCustomDropDownItem * DropDownItem;
  6486. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6487. if (DropDownItem != NULL)
  6488. {
  6489. ASize.x -= DropDownItem->EditWidth;
  6490. TControl * Control = this;
  6491. ASize.x += ScaleByTextHeight(Control, 50) /* minimal combo width */;
  6492. }
  6493. }
  6494. }
  6495. //---------------------------------------------------------------------------
  6496. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6497. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6498. {
  6499. if (WinConfiguration->DDWarnLackOfTempSpace)
  6500. {
  6501. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6502. if (!ADrive.IsEmpty())
  6503. {
  6504. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6505. DebugAssert(RequiredSpace >= 0);
  6506. __int64 RequiredWithReserve;
  6507. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6508. if (FreeSpace < RequiredWithReserve)
  6509. {
  6510. unsigned int Result;
  6511. TMessageParams Params(mpNeverAskAgainCheck);
  6512. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6513. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6514. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6515. if (Result == qaNeverAskAgain)
  6516. {
  6517. WinConfiguration->DDWarnLackOfTempSpace = false;
  6518. }
  6519. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6520. }
  6521. }
  6522. }
  6523. }
  6524. //---------------------------------------------------------------------------
  6525. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6526. {
  6527. DoOpenDirectoryDialog(odAddBookmark, Side);
  6528. }
  6529. //---------------------------------------------------------------------------
  6530. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6531. {
  6532. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6533. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6534. try
  6535. {
  6536. TCustomDirView * DView = DirView(Side);
  6537. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6538. {
  6539. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6540. }
  6541. }
  6542. catch (...)
  6543. {
  6544. delete VisitedDirectories;
  6545. throw;
  6546. }
  6547. return VisitedDirectories;
  6548. }
  6549. //---------------------------------------------------------------------------
  6550. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6551. TOpenDirectoryMode Mode, TOperationSide Side)
  6552. {
  6553. bool Continue = true;
  6554. if (Mode == odAddBookmark)
  6555. {
  6556. TMessageParams Params(mpNeverAskAgainCheck);
  6557. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6558. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6559. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6560. unsigned int Answer =
  6561. MessageDialog(Message,
  6562. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6563. if (Answer == qaNeverAskAgain)
  6564. {
  6565. Continue = true;
  6566. WinConfiguration->UseSharedBookmarks = true;
  6567. }
  6568. else if (Answer == qaOK)
  6569. {
  6570. Continue = true;
  6571. WinConfiguration->UseSharedBookmarks = false;
  6572. }
  6573. else
  6574. {
  6575. Continue = false;
  6576. }
  6577. }
  6578. if (Continue)
  6579. {
  6580. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6581. try
  6582. {
  6583. UnicodeString Name = DirView(Side)->PathName;
  6584. bool Repeat;
  6585. do
  6586. {
  6587. Repeat = false;
  6588. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6589. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6590. // do not allow switching to location profiles,
  6591. // if we are not connected
  6592. HasDirView[osLocal] && IsActiveTerminal(Terminal)))
  6593. {
  6594. TWindowLock Lock(this);
  6595. if (!TryOpenDirectory(Side, Name))
  6596. {
  6597. Repeat = true;
  6598. Mode = odBrowse;
  6599. }
  6600. }
  6601. }
  6602. while (Repeat);
  6603. }
  6604. __finally
  6605. {
  6606. delete VisitedDirectories;
  6607. }
  6608. }
  6609. }
  6610. //---------------------------------------------------------------------------
  6611. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6612. {
  6613. DebugAssert(!Terminal->CommandSessionOpened);
  6614. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6615. }
  6616. //---------------------------------------------------------------------------
  6617. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6618. {
  6619. bool Result = Terminal->IsCapable[Capability] ||
  6620. Terminal->CommandSessionOpened;
  6621. if (!Result)
  6622. {
  6623. // We might get here for example when "checksum" synchronization is requested from command-line
  6624. // on server that supports neither checksum nor shell
  6625. if (!Terminal->IsCapable[fcSecondaryShell])
  6626. {
  6627. NotSupported();
  6628. }
  6629. else if (!GUIConfiguration->ConfirmCommandSession)
  6630. {
  6631. Result = true;
  6632. }
  6633. else
  6634. {
  6635. TMessageParams Params(mpNeverAskAgainCheck);
  6636. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6637. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6638. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6639. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6640. if (Answer == qaNeverAskAgain)
  6641. {
  6642. GUIConfiguration->ConfirmCommandSession = false;
  6643. Result = true;
  6644. }
  6645. else if (Answer == qaOK)
  6646. {
  6647. Result = true;
  6648. }
  6649. }
  6650. if (Result)
  6651. {
  6652. Result = CommandSessionFallback();
  6653. }
  6654. }
  6655. return Result;
  6656. }
  6657. //---------------------------------------------------------------------------
  6658. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6659. {
  6660. if (EnsureCommandSessionFallback(fcAnyCommand))
  6661. {
  6662. DoConsoleDialog(Terminal, Command);
  6663. }
  6664. }
  6665. //---------------------------------------------------------------------------
  6666. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6667. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6668. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6669. {
  6670. if (!IsSideLocalBrowser(osRemote) &&
  6671. IsFileControl(DropSourceControl, osRemote) &&
  6672. (FDDExtMapFile != NULL))
  6673. {
  6674. Accept = true;
  6675. }
  6676. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6677. DebugAssert(FDDTargetControl != NULL);
  6678. }
  6679. //---------------------------------------------------------------------------
  6680. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6681. _di_IDataObject DataObj, int KeyState,
  6682. const TPoint & Point, int & Effect, bool & Accept)
  6683. {
  6684. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6685. }
  6686. //---------------------------------------------------------------------------
  6687. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6688. _di_IDataObject DataObj, int KeyState,
  6689. const TPoint & Point, int & Effect, bool & Accept)
  6690. {
  6691. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6692. }
  6693. //---------------------------------------------------------------------------
  6694. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6695. TObject * Sender)
  6696. {
  6697. DebugUsedParam(Sender);
  6698. DebugAssert(FDDTargetControl == Sender);
  6699. FDDTargetControl = NULL;
  6700. }
  6701. //---------------------------------------------------------------------------
  6702. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave(int)
  6703. {
  6704. FileControlDDDragLeave(SessionsPageControl);
  6705. }
  6706. //---------------------------------------------------------------------------
  6707. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave(int)
  6708. {
  6709. FileControlDDDragLeave(QueueView3);
  6710. }
  6711. //---------------------------------------------------------------------------
  6712. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6713. {
  6714. DebugAssert(!IsSideLocalBrowser(Side));
  6715. DebugUsedParam(Side);
  6716. bool Edit = false;
  6717. TRemoteFile * File = NULL;
  6718. UnicodeString FileName;
  6719. UnicodeString PointTo;
  6720. bool SymbolicLink = true;
  6721. if (RemoteDirView->ItemFocused)
  6722. {
  6723. DebugAssert(RemoteDirView->ItemFocused->Data);
  6724. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6725. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6726. if (Edit)
  6727. {
  6728. FileName = File->FileName;
  6729. PointTo = File->LinkTo;
  6730. }
  6731. else if (!File->IsParentDirectory)
  6732. {
  6733. PointTo = File->FileName;
  6734. }
  6735. }
  6736. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6737. Terminal->IsCapable[fcHardLink]))
  6738. {
  6739. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6740. if (Edit)
  6741. {
  6742. DebugAssert(File->FileName == FileName);
  6743. int Params = dfNoRecursive;
  6744. Terminal->ExceptionOnFail = true;
  6745. try
  6746. {
  6747. Terminal->DeleteFile(L"", File, &Params);
  6748. }
  6749. __finally
  6750. {
  6751. Terminal->ExceptionOnFail = false;
  6752. }
  6753. }
  6754. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6755. }
  6756. }
  6757. //---------------------------------------------------------------------------
  6758. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6759. {
  6760. return
  6761. (IsSideLocalBrowser(Side) ||
  6762. (HasActiveTerminal() &&
  6763. Terminal->ResolvingSymlinks &&
  6764. Terminal->IsCapable[fcSymbolicLink]));
  6765. }
  6766. //---------------------------------------------------------------------------
  6767. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6768. {
  6769. return
  6770. !IsSideLocalBrowser(FCurrentSide) &&
  6771. (RemoteDirView->ItemFocused != NULL) &&
  6772. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6773. Terminal->SessionData->ResolveSymlinks;
  6774. }
  6775. //---------------------------------------------------------------------------
  6776. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6777. {
  6778. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6779. FForceExecution = true;
  6780. try
  6781. {
  6782. DirView(osCurrent)->ExecuteCurrentFile();
  6783. }
  6784. __finally
  6785. {
  6786. FForceExecution = false;
  6787. }
  6788. }
  6789. //---------------------------------------------------------------------------
  6790. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6791. {
  6792. TEditorData ExternalEditor;
  6793. ExternalEditor.Editor = edExternal;
  6794. bool Remember = false;
  6795. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6796. {
  6797. if (Remember)
  6798. {
  6799. TEditorList * EditorList = new TEditorList;
  6800. try
  6801. {
  6802. *EditorList = *WinConfiguration->EditorList;
  6803. bool Found = false;
  6804. int i = 0;
  6805. while (!Found && (i < EditorList->Count))
  6806. {
  6807. const TEditorPreferences * Editor = EditorList->Editors[i];
  6808. if ((Editor->Data->Editor == edExternal) &&
  6809. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6810. {
  6811. Found = true;
  6812. }
  6813. i++;
  6814. }
  6815. if (!Found)
  6816. {
  6817. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6818. WinConfiguration->EditorList = EditorList;
  6819. }
  6820. }
  6821. __finally
  6822. {
  6823. delete EditorList;
  6824. }
  6825. }
  6826. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6827. }
  6828. }
  6829. //---------------------------------------------------------------------------
  6830. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6831. {
  6832. if (FFileFindTerminal == ATerminal)
  6833. {
  6834. FFileFindTerminal = NULL;
  6835. HideFileFindDialog();
  6836. }
  6837. if (FClipboardTerminal == ATerminal)
  6838. {
  6839. ClipboardClear(); // implies ClipboardStop
  6840. }
  6841. if ((FOnSynchronizeAbort != NULL) &&
  6842. (ATerminal == FManagedSession))
  6843. {
  6844. FOnSynchronizeAbort(NULL);
  6845. }
  6846. }
  6847. //---------------------------------------------------------------------------
  6848. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6849. {
  6850. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6851. DetachTerminal(Sender);
  6852. }
  6853. //---------------------------------------------------------------------------
  6854. void __fastcall TCustomScpExplorerForm::FileTerminalReplaced(
  6855. const UnicodeString FileName, TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6856. {
  6857. TManagedTerminal * ATerminal = static_cast<TManagedTerminal *>(Arg);
  6858. if (Data->Terminal == Terminal)
  6859. {
  6860. Data->Terminal = ATerminal;
  6861. }
  6862. }
  6863. //---------------------------------------------------------------------------
  6864. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6865. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6866. {
  6867. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6868. DebugAssert(Terminal != NULL);
  6869. if (Data->Terminal == Terminal)
  6870. {
  6871. Data->Terminal = NULL;
  6872. }
  6873. }
  6874. //---------------------------------------------------------------------------
  6875. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6876. {
  6877. return NULL;
  6878. }
  6879. //---------------------------------------------------------------------------
  6880. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6881. {
  6882. UpdateControls();
  6883. SessionColor = TColor(0);
  6884. UpdateRemotePathComboBox(false);
  6885. NeedSession(false);
  6886. }
  6887. //---------------------------------------------------------------------------
  6888. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6889. {
  6890. try
  6891. {
  6892. // Cache, as the login dialog can change its value.
  6893. // See also DoShow. Also see TTerminalManager::NewSession.
  6894. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6895. try
  6896. {
  6897. if (ShowLogin)
  6898. {
  6899. bool ReloadSessions = !Startup;
  6900. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6901. }
  6902. }
  6903. __finally
  6904. {
  6905. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6906. // (so there was no chance for the user to open any session yet)
  6907. if ((ShowLogin || !Startup) &&
  6908. !WinConfiguration->KeepOpenWhenNoSession &&
  6909. // this rules out the implicit Commander's local-local browser
  6910. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6911. {
  6912. TerminateApplication();
  6913. }
  6914. }
  6915. }
  6916. catch (EAbort &)
  6917. {
  6918. // swallow
  6919. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6920. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6921. }
  6922. }
  6923. //---------------------------------------------------------------------------
  6924. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6925. {
  6926. TTerminalManager * Manager = TTerminalManager::Instance();
  6927. DebugAssert(!Manager->Updating);
  6928. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6929. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6930. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6931. SessionsPageControl->LockDrawing();
  6932. try
  6933. {
  6934. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6935. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6936. {
  6937. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6938. }
  6939. for (int Index = 0; Index <= Manager->Count; Index++)
  6940. {
  6941. TThemeTabSheet * TabSheet;
  6942. if (Index >= SessionsPageControl->PageCount)
  6943. {
  6944. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6945. TabSheet->PageControl = SessionsPageControl;
  6946. }
  6947. else
  6948. {
  6949. TabSheet = SessionsPageControl->Pages[Index];
  6950. }
  6951. bool IsSessionTab = (Index < Manager->Count);
  6952. if (IsSessionTab)
  6953. {
  6954. TTerminal * Terminal = Manager->Sessions[Index];
  6955. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6956. TabSheet->CaptionTruncation = WinConfiguration->SessionTabCaptionTruncation ? tttEllipsis : tttNone;
  6957. UpdateSessionTab(TabSheet);
  6958. }
  6959. else
  6960. {
  6961. TabSheet->Tag = 0; // not really needed
  6962. TabSheet->Shadowed = false;
  6963. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6964. TabSheet->CaptionTruncation = GetNewTabTabCaptionTruncation();
  6965. // 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)
  6966. UpdateNewTabTab();
  6967. }
  6968. }
  6969. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6970. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6971. {
  6972. SessionsPageControl->UpdateTabsCaptionTruncation();
  6973. }
  6974. }
  6975. __finally
  6976. {
  6977. SessionsPageControl->UnlockDrawing();
  6978. }
  6979. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6980. }
  6981. //---------------------------------------------------------------------------
  6982. TThemeTabCaptionTruncation TCustomScpExplorerForm::GetNewTabTabCaptionTruncation()
  6983. {
  6984. return WinConfiguration->SessionTabCaptionTruncation ? tttNoText : tttNone;
  6985. }
  6986. //---------------------------------------------------------------------------
  6987. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6988. {
  6989. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6990. }
  6991. //---------------------------------------------------------------------------
  6992. TThemeTabSheet * TCustomScpExplorerForm::GetNewTabTab()
  6993. {
  6994. return SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6995. }
  6996. //---------------------------------------------------------------------------
  6997. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6998. {
  6999. TThemeTabSheet * TabSheet = GetNewTabTab();
  7000. UnicodeString TabCaption;
  7001. if (WinConfiguration->SelectiveToolbarText)
  7002. {
  7003. TabCaption = GetNewTabTabCaption();
  7004. }
  7005. TabSheet->BaseCaption = TabCaption;
  7006. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  7007. }
  7008. //---------------------------------------------------------------------------
  7009. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  7010. {
  7011. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  7012. }
  7013. //---------------------------------------------------------------------------
  7014. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  7015. {
  7016. DebugFail();
  7017. return UnicodeString();
  7018. }
  7019. //---------------------------------------------------------------------------
  7020. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  7021. {
  7022. if (DebugAlwaysTrue(TabSheet != NULL))
  7023. {
  7024. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  7025. if (DebugAlwaysTrue(ASession != NULL))
  7026. {
  7027. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  7028. if (ASession->LocalBrowser)
  7029. {
  7030. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  7031. }
  7032. else
  7033. {
  7034. TabSheet->ImageIndex = AddSessionColor(Color);
  7035. }
  7036. TTerminalManager * Manager = TTerminalManager::Instance();
  7037. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  7038. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  7039. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  7040. }
  7041. }
  7042. }
  7043. //---------------------------------------------------------------------------
  7044. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  7045. {
  7046. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  7047. }
  7048. //---------------------------------------------------------------------------
  7049. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  7050. {
  7051. DebugAssert(SessionsPageControl->ActivePage != NULL);
  7052. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  7053. bool Result = (Session != NULL);
  7054. if (Result)
  7055. {
  7056. TTerminalManager::Instance()->ActiveSession = Session;
  7057. }
  7058. else
  7059. {
  7060. try
  7061. {
  7062. NewTab();
  7063. }
  7064. __finally
  7065. {
  7066. SessionListChanged();
  7067. }
  7068. FSessionsPageControlNewTabTime = Now();
  7069. }
  7070. return Result;
  7071. }
  7072. //---------------------------------------------------------------------------
  7073. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  7074. {
  7075. TWindowLock Lock(this);
  7076. SessionTabSwitched();
  7077. }
  7078. //---------------------------------------------------------------------------
  7079. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  7080. {
  7081. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  7082. DebugAssert(TransferList != NULL);
  7083. UnicodeString Name;
  7084. if (TransferList->ItemIndex <= 0)
  7085. {
  7086. Name = L"";
  7087. }
  7088. else
  7089. {
  7090. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  7091. }
  7092. if (FCopyParamAutoSelected.IsEmpty())
  7093. {
  7094. // if previous preset was not autoselected, make new preset the "default"
  7095. FCopyParamDefault = Name;
  7096. }
  7097. GUIConfiguration->CopyParamCurrent = Name;
  7098. }
  7099. //---------------------------------------------------------------------------
  7100. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  7101. {
  7102. // sanity check
  7103. bool ExistingPreset =
  7104. (FTransferListHoverIndex >= 0) &&
  7105. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  7106. DebugAssert(ExistingPreset);
  7107. if (ExistingPreset)
  7108. {
  7109. HDC DC = GetDC(0);
  7110. TCanvas * Canvas = new TCanvas();
  7111. try
  7112. {
  7113. Canvas->Handle = DC;
  7114. Canvas->Font = GetToolbarFont(this);
  7115. UnicodeString Name;
  7116. if (FTransferListHoverIndex == 0)
  7117. {
  7118. Name = L"";
  7119. }
  7120. else
  7121. {
  7122. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  7123. }
  7124. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  7125. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  7126. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  7127. static_cast<TObject*>(GetComponent(fcTransferList)));
  7128. UnicodeString InfoStr =
  7129. GUIConfiguration->CopyParamPreset[Name].
  7130. GetInfoStr(L"; ",
  7131. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  7132. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  7133. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  7134. {
  7135. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  7136. {
  7137. InfoStr.SetLength(InfoStr.Length() - 1);
  7138. }
  7139. InfoStr += Ellipsis;
  7140. }
  7141. // UpdateCaption does not cause invalidation of whole submenu, while
  7142. // setting Caption property does.
  7143. // also it probably does not resize the label, even if necessary
  7144. // (we do not want that anyway)
  7145. TransferLabel->UpdateCaption(InfoStr);
  7146. }
  7147. __finally
  7148. {
  7149. Canvas->Handle = NULL;
  7150. ReleaseDC(0, DC);
  7151. delete Canvas;
  7152. }
  7153. }
  7154. }
  7155. //---------------------------------------------------------------------------
  7156. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  7157. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  7158. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  7159. {
  7160. if (FTransferListHoverIndex != AHoverIndex)
  7161. {
  7162. FTransferListHoverIndex = AHoverIndex;
  7163. UpdateTransferLabel();
  7164. }
  7165. }
  7166. //---------------------------------------------------------------------------
  7167. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  7168. {
  7169. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  7170. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  7171. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  7172. {
  7173. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  7174. {
  7175. if (!NonVisualDataModule->Busy)
  7176. {
  7177. OpenDirectory(GetSide(osCurrent));
  7178. }
  7179. Message.Result = 1;
  7180. }
  7181. else
  7182. {
  7183. TForm::Dispatch(&Message);
  7184. }
  7185. }
  7186. else
  7187. {
  7188. TForm::Dispatch(&Message);
  7189. }
  7190. }
  7191. //---------------------------------------------------------------------------
  7192. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  7193. {
  7194. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  7195. if ((FIgnoreNextDialogChar != 0) &&
  7196. (towupper(Message.CharCode) == towupper(FIgnoreNextDialogChar)))
  7197. {
  7198. Message.Result = 1;
  7199. }
  7200. else
  7201. {
  7202. TForm::Dispatch(&Message);
  7203. }
  7204. FIgnoreNextDialogChar = 0;
  7205. }
  7206. //---------------------------------------------------------------------------
  7207. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  7208. {
  7209. // The four low-order bits are used internally by Windows
  7210. unsigned int Cmd = (Message.WParam & 0xFFF0);
  7211. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  7212. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  7213. // 61730 - restore thru double click - undocumented
  7214. // 61490 - maximize thru double click - undocumented
  7215. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  7216. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  7217. {
  7218. SysResizing(Cmd);
  7219. }
  7220. TForm::Dispatch(&Message);
  7221. }
  7222. //---------------------------------------------------------------------------
  7223. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  7224. {
  7225. AppLog(L"Windows session end query");
  7226. // We were actually never able to make ENDSESSION_CRITICAL happen.
  7227. // Also there no point returning TRUE as we are not able to
  7228. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  7229. // Hence the process termination might be safer :)
  7230. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  7231. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  7232. {
  7233. Message.Result = FALSE;
  7234. }
  7235. else
  7236. {
  7237. Message.Result = TRUE;
  7238. }
  7239. // Do not call default handling as that triggers OnCloseQuery,
  7240. // where our implementation will popup confirmation dialogs, what we do not want,
  7241. // as per Vista guidelines:
  7242. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  7243. }
  7244. //---------------------------------------------------------------------------
  7245. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  7246. {
  7247. AppLog(L"Windows session end");
  7248. if (Message.EndSession && IsApplicationMinimized())
  7249. {
  7250. // WORKAROUND
  7251. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  7252. // when it receives WM_ENDSESSION,
  7253. // but that sometimes (particularly when application is minimized) cause crashes.
  7254. // Crash popups message that blocks log off.
  7255. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  7256. // so we call ExitProcess() immediately, not even trying to cleanup.
  7257. // It still causes beep, so there's likely some popup, but it does not block
  7258. // log off.
  7259. ExitProcess(0);
  7260. }
  7261. TForm::Dispatch(&Message);
  7262. }
  7263. #ifdef _DEBUG
  7264. //---------------------------------------------------------------------------
  7265. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7266. {
  7267. TForm::Dispatch(&Message);
  7268. }
  7269. #endif
  7270. //---------------------------------------------------------------------------
  7271. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7272. {
  7273. }
  7274. //---------------------------------------------------------------------------
  7275. void __fastcall TCustomScpExplorerForm::DoShow()
  7276. {
  7277. // Called from TCustomScpExplorerForm::CMShowingChanged
  7278. // only now are the controls resized finally, so the size constraints
  7279. // will not conflict with possibly very small window size
  7280. RestoreFormParams();
  7281. // This indicates that the form was rescaled after parameters were loaded and
  7282. // as most probably the parameters were actually saved with the current form scaling
  7283. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7284. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7285. // and no rounding problems will occur.
  7286. // See also RestoreForm.
  7287. // (we should be safe to call RestoreParams unconditionally)
  7288. if (PixelsPerInch != Screen->PixelsPerInch)
  7289. {
  7290. RestoreParams();
  7291. }
  7292. FixControlsPlacement();
  7293. if (Position == poDefaultPosOnly)
  7294. {
  7295. CutFormToDesktop(this);
  7296. }
  7297. TForm::DoShow();
  7298. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7299. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7300. // This was previously in NeedSession, where other related code is.
  7301. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7302. // unpleasantly shows briefly.
  7303. if ((ManagedSession == NULL) &&
  7304. !WinConfiguration->ShowLoginWhenNoSession &&
  7305. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7306. // This detects if workspace was saved the last time the main widow was closed
  7307. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7308. {
  7309. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7310. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7311. }
  7312. // Before main window displays
  7313. if (ManagedSession == NULL)
  7314. {
  7315. StartingWithoutSession();
  7316. }
  7317. UpdatePixelsPerInchMainWindowCounter();
  7318. FShowing = true;
  7319. }
  7320. //---------------------------------------------------------------------------
  7321. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7322. {
  7323. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7324. }
  7325. //---------------------------------------------------------------------------
  7326. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7327. {
  7328. SessionListChanged();
  7329. InitStatusBar();
  7330. UpdateControls();
  7331. }
  7332. //---------------------------------------------------------------------------
  7333. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7334. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7335. TNotifyEvent OnBalloonClick, TObject * UserData)
  7336. {
  7337. bool Do;
  7338. UnicodeString Message;
  7339. if (E == NULL)
  7340. {
  7341. Message = Str;
  7342. Do = true;
  7343. }
  7344. else
  7345. {
  7346. Do = ExceptionMessage(E, Message);
  7347. }
  7348. if (Do && WinConfiguration->BalloonNotifications)
  7349. {
  7350. UnicodeString Title;
  7351. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7352. {
  7353. Message = TrimLeft(Message);
  7354. }
  7355. else
  7356. {
  7357. Message = UnformatMessage(Message);
  7358. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7359. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7360. Title = LoadResourceString(Captions[Type]);
  7361. if (Terminal != NULL)
  7362. {
  7363. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal->PrimaryTerminal));
  7364. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7365. }
  7366. }
  7367. if (Seconds == 0)
  7368. {
  7369. Seconds = WinConfiguration->NotificationsTimeout;
  7370. }
  7371. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7372. }
  7373. }
  7374. //---------------------------------------------------------------------------
  7375. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7376. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7377. UnicodeString HelpKeyword, const TMessageParams * Params,
  7378. TTerminal * Terminal)
  7379. {
  7380. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7381. (FOnFeedSynchronizeError != NULL)) &&
  7382. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7383. {
  7384. if (FOnFeedSynchronizeError != NULL)
  7385. {
  7386. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7387. }
  7388. else
  7389. {
  7390. DebugAssert(FErrorList != NULL);
  7391. TStringList * MoreMessagesCopy = NULL;
  7392. if (MoreMessages)
  7393. {
  7394. MoreMessagesCopy = new TStringList();
  7395. MoreMessagesCopy->Assign(MoreMessages);
  7396. }
  7397. FErrorList->AddObject(Message, MoreMessagesCopy);
  7398. }
  7399. PopupTrayBalloon(Terminal, Message, Type);
  7400. return ContinueAnswer(Answers);
  7401. }
  7402. else
  7403. {
  7404. bool UseBalloon = FTrayIcon->Visible;
  7405. if (UseBalloon)
  7406. {
  7407. PopupTrayBalloon(Terminal, Message, Type);
  7408. }
  7409. unsigned int Result;
  7410. try
  7411. {
  7412. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7413. }
  7414. __finally
  7415. {
  7416. // cancel only balloon we popped up, otherwise we may cancel notification
  7417. // balloon that was there before the message dialog
  7418. // (such as "dd confirmation opt in balloon)
  7419. if (UseBalloon)
  7420. {
  7421. FTrayIcon->CancelBalloon();
  7422. }
  7423. }
  7424. return Result;
  7425. }
  7426. }
  7427. //---------------------------------------------------------------------------
  7428. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7429. TTerminal * Terminal, Exception * E)
  7430. {
  7431. if (FTrayIcon->Visible)
  7432. {
  7433. PopupTrayBalloon(Terminal, L"", qtError, E);
  7434. }
  7435. // particularly prevent opening new session from jump list,
  7436. // while exception is shown
  7437. NonVisualDataModule->StartBusy();
  7438. try
  7439. {
  7440. ShowExtendedExceptionEx(Terminal, E);
  7441. }
  7442. __finally
  7443. {
  7444. NonVisualDataModule->EndBusy();
  7445. FTrayIcon->CancelBalloon();
  7446. }
  7447. }
  7448. //---------------------------------------------------------------------------
  7449. void __fastcall TCustomScpExplorerForm::SessionReady()
  7450. {
  7451. InitStatusBar();
  7452. // cannot rely on active page being page for active terminal,
  7453. // as it can happen that active page is the "new session" page
  7454. // (e.g. when reconnecting active terminal, while login dialog
  7455. // invoked from "new session" page is modal)
  7456. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7457. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7458. }
  7459. //---------------------------------------------------------------------------
  7460. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7461. TTerminal * Terminal, Exception * E)
  7462. {
  7463. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7464. if (!Terminal->Active)
  7465. {
  7466. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7467. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7468. {
  7469. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7470. }
  7471. }
  7472. }
  7473. //---------------------------------------------------------------------------
  7474. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7475. UnicodeString Message, TQueryType Type,
  7476. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7477. {
  7478. if ((E == NULL) ||
  7479. ExceptionMessage(E, Message))
  7480. {
  7481. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7482. if (Important)
  7483. {
  7484. Seconds *= 5;
  7485. }
  7486. UnicodeString NoteMessage(UnformatMessage(Message));
  7487. if (Terminal != NULL)
  7488. {
  7489. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7490. NoteMessage = FORMAT(L"%s: %s",
  7491. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7492. }
  7493. if (WinConfiguration->BalloonNotifications)
  7494. {
  7495. AddNote(NoteMessage);
  7496. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7497. }
  7498. else
  7499. {
  7500. FlashOnBackground();
  7501. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7502. }
  7503. }
  7504. }
  7505. //---------------------------------------------------------------------------
  7506. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7507. {
  7508. RestoreApp();
  7509. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7510. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7511. !NonVisualDataModule->Busy)
  7512. {
  7513. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7514. TTerminalManager::Instance()->ActiveSession = Terminal;
  7515. if (!ComponentVisible[fcQueueView])
  7516. {
  7517. ToggleQueueVisibility();
  7518. GoToQueue();
  7519. }
  7520. }
  7521. }
  7522. //---------------------------------------------------------------------------
  7523. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7524. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7525. {
  7526. UnicodeString Message;
  7527. TNotifyEvent OnClick = NULL;
  7528. TObject * UserData = NULL;
  7529. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7530. switch (Event)
  7531. {
  7532. case qeEmptyButMonitored:
  7533. if ((ATerminal != Terminal) || QueueInvisible)
  7534. {
  7535. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7536. OnClick = QueueEmptyNoteClicked;
  7537. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7538. TerminalNoteData->Terminal = ATerminal;
  7539. UserData = TerminalNoteData;
  7540. }
  7541. break;
  7542. case qeEmpty:
  7543. OperationComplete(ATerminal->QueueOperationStart);
  7544. break;
  7545. case qePendingUserAction:
  7546. if ((ATerminal != Terminal) ||
  7547. (QueueInvisible && !IsQueueAutoPopup()))
  7548. {
  7549. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7550. }
  7551. break;
  7552. default:
  7553. DebugFail();
  7554. }
  7555. if (!Message.IsEmpty())
  7556. {
  7557. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7558. }
  7559. }
  7560. //---------------------------------------------------------------------------
  7561. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7562. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7563. {
  7564. if (FDDExtMapFile != NULL)
  7565. {
  7566. CloseHandle(FDDExtMapFile);
  7567. FDDExtMapFile = NULL;
  7568. }
  7569. if (WinConfiguration->DDFakeFile)
  7570. {
  7571. DDFakeFileInitDrag(FileList, Created);
  7572. }
  7573. }
  7574. //---------------------------------------------------------------------------
  7575. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7576. {
  7577. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7578. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7579. {
  7580. FFakeFileDropTarget = FileName;
  7581. }
  7582. }
  7583. //---------------------------------------------------------------------------
  7584. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7585. {
  7586. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7587. if (!ForceDirectories(ApiPath(Result)))
  7588. {
  7589. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7590. }
  7591. FileSetAttr(ApiPath(Result), faHidden);
  7592. return Result;
  7593. }
  7594. //---------------------------------------------------------------------------
  7595. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7596. bool & Created)
  7597. {
  7598. FFakeFileDropTarget = UnicodeString();
  7599. FDragFakeDirectory = CreateFakeTransferDirectory();
  7600. FileList->AddItem(NULL, FDragFakeDirectory);
  7601. Created = true;
  7602. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7603. {
  7604. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7605. }
  7606. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7607. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7608. {
  7609. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7610. TDragExtCommStruct* CommStruct;
  7611. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7612. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7613. DebugAssert(CommStruct != NULL);
  7614. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7615. CommStruct->Dragging = true;
  7616. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7617. LENOF(CommStruct->DropDest));
  7618. NULL_TERMINATE(CommStruct->DropDest);
  7619. UnmapViewOfFile(CommStruct);
  7620. }
  7621. }
  7622. //---------------------------------------------------------------------------
  7623. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7624. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7625. {
  7626. bool Result;
  7627. DebugAssert(!IsLocalBrowserMode());
  7628. if (Sender == RemoteDirView)
  7629. {
  7630. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7631. }
  7632. else
  7633. {
  7634. DebugAssert(Sender == RemoteDriveView);
  7635. TStrings * FileList = RemoteDriveView->DragFileList();
  7636. try
  7637. {
  7638. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7639. }
  7640. __finally
  7641. {
  7642. delete FileList;
  7643. }
  7644. }
  7645. if (FDDTargetControl == RemoteDriveView)
  7646. {
  7647. RemoteDriveView->UpdateDropTarget();
  7648. }
  7649. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7650. // or from tree view to dir view.
  7651. // foMove happens when file/dir is dragged from remote tree view outside of
  7652. // application via dragex
  7653. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7654. (DropSourceControl == RemoteDriveView))
  7655. {
  7656. RemoteDriveView->UpdateDropSource();
  7657. }
  7658. return Result;
  7659. }
  7660. //---------------------------------------------------------------------------
  7661. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7662. {
  7663. DebugAssert(!IsLocalBrowserMode());
  7664. // This also handles drops of remote files to queue.
  7665. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7666. SAFE_DESTROY(FDDFileList);
  7667. if (!FFakeFileDropTarget.IsEmpty())
  7668. {
  7669. RemoveDir(ApiPath(FFakeFileDropTarget));
  7670. }
  7671. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7672. {
  7673. try
  7674. {
  7675. TDragResult DDResult = (Sender == RemoteDirView) ?
  7676. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7677. // Focus is moved to the target application,
  7678. // but as we are going to present the UI, we need to steal the focus back.
  7679. // This most likely won't work though (windows does not allow application
  7680. // to steal focus most of the time)
  7681. Application->BringToFront();
  7682. // On older version of Windows we never got drMove here, see also comment below.
  7683. // On Windows 10, we get the "move".
  7684. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7685. {
  7686. UnicodeString TargetDirectory;
  7687. TFileOperation Operation;
  7688. // drInvalid may mean drMove, see comment below
  7689. switch (DDResult)
  7690. {
  7691. case drCopy:
  7692. Operation = foCopy;
  7693. break;
  7694. case drMove:
  7695. Operation = foMove;
  7696. break;
  7697. default:
  7698. DebugFail();
  7699. case drInvalid:
  7700. // prefer "copy" for safety
  7701. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7702. break;
  7703. }
  7704. TTransferOperationParam Param;
  7705. UnicodeString CounterName;
  7706. bool ForceQueue;
  7707. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7708. {
  7709. // we get drInvalid both if d&d was intercepted by ddext,
  7710. // and when users drops on no-drop location.
  7711. // we tell the difference by existence of response from ddext,
  7712. // so we ignore absence of response in this case
  7713. if (DDResult != drInvalid)
  7714. {
  7715. // here we know that the extension is installed,
  7716. // as it is checked as soon as drag&drop starts from
  7717. // RemoteFileControlDDCreateDragFileList
  7718. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7719. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7720. {
  7721. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7722. }
  7723. else
  7724. {
  7725. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7726. }
  7727. }
  7728. }
  7729. else
  7730. {
  7731. // download using fake file
  7732. Param.Temp = false;
  7733. Param.DragDrop = true;
  7734. if (ForceQueue)
  7735. {
  7736. Param.Queue = asOn;
  7737. }
  7738. if (Sender == RemoteDirView)
  7739. {
  7740. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7741. }
  7742. if (RemoteFileControlFileOperation(Sender, Operation,
  7743. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7744. {
  7745. Configuration->Usage->Inc(CounterName);
  7746. }
  7747. }
  7748. }
  7749. }
  7750. __finally
  7751. {
  7752. CloseHandle(FDDExtMapFile);
  7753. FDDExtMapFile = NULL;
  7754. delete FDragFakeMonitors;
  7755. FDragFakeMonitors = NULL;
  7756. RemoveDir(ApiPath(FDragFakeDirectory));
  7757. FDragFakeDirectory = L"";
  7758. FFakeFileDropTarget = UnicodeString();
  7759. }
  7760. }
  7761. if (FDragDropSshTerminate.get() != NULL)
  7762. {
  7763. FDragDropSshTerminate->Rethrow();
  7764. }
  7765. }
  7766. //---------------------------------------------------------------------------
  7767. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7768. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7769. {
  7770. // Remember drop effect so we know (when user drops files), if we copy or move
  7771. FLastDropEffect = dwEffect;
  7772. }
  7773. //---------------------------------------------------------------------------
  7774. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7775. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7776. {
  7777. bool Result;
  7778. if (FDDTargetControl == QueueView3)
  7779. {
  7780. Directory = DefaultDownloadTargetDirectory();
  7781. Result = true;
  7782. CounterName = L"DownloadsDragDropQueue";
  7783. ForceQueue = true;
  7784. }
  7785. else if (!FFakeFileDropTarget.IsEmpty())
  7786. {
  7787. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7788. Result = true;
  7789. ForceQueue = false;
  7790. CounterName = L"DownloadsDragDropFakeFile";
  7791. }
  7792. else
  7793. {
  7794. ForceQueue = false;
  7795. Enabled = false;
  7796. try
  7797. {
  7798. int Timer = 0;
  7799. Result = false;
  7800. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7801. {
  7802. {
  7803. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7804. TDragExtCommStruct* CommStruct;
  7805. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7806. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7807. DebugAssert(CommStruct != NULL);
  7808. Result = !CommStruct->Dragging;
  7809. if (Result)
  7810. {
  7811. Directory = ExtractFilePath(CommStruct->DropDest);
  7812. CounterName = L"DownloadsDragDropExternalExt";
  7813. }
  7814. UnmapViewOfFile(CommStruct);
  7815. }
  7816. if (!Result)
  7817. {
  7818. Sleep(50);
  7819. Timer += 50;
  7820. Application->ProcessMessages();
  7821. }
  7822. }
  7823. }
  7824. __finally
  7825. {
  7826. Enabled = true;
  7827. }
  7828. }
  7829. return Result;
  7830. }
  7831. //---------------------------------------------------------------------------
  7832. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7833. const UnicodeString TempDir, int SecDelay)
  7834. {
  7835. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7836. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7837. if (FDelayedDeletionTimer == NULL)
  7838. {
  7839. DebugAssert(HandleAllocated());
  7840. FDelayedDeletionTimer = new TTimer(this);
  7841. FDelayedDeletionTimer->Interval = 10000;
  7842. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7843. }
  7844. else
  7845. {
  7846. FDelayedDeletionTimer->Enabled = true;
  7847. }
  7848. }
  7849. //---------------------------------------------------------------------------
  7850. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7851. {
  7852. DebugAssert(FDelayedDeletionList != NULL);
  7853. TDateTime N = Now();
  7854. TDateTime Alert;
  7855. UnicodeString Directory;
  7856. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7857. {
  7858. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7859. if ((N >= Alert) || (Sender == NULL))
  7860. {
  7861. Directory = FDelayedDeletionList->Strings[Index];
  7862. if (RecursiveDeleteFile(ExcludeTrailingBackslash(Directory)))
  7863. {
  7864. FDelayedDeletionList->Delete(Index);
  7865. }
  7866. }
  7867. }
  7868. if (FDelayedDeletionList->Count == 0)
  7869. {
  7870. FDelayedDeletionTimer->Enabled = false;
  7871. }
  7872. }
  7873. //---------------------------------------------------------------------------
  7874. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7875. {
  7876. DebugAssert(!IsLocalBrowserMode());
  7877. if (IsFileControl(FDDTargetControl, osRemote) ||
  7878. (FDDTargetControl == SessionsPageControl))
  7879. {
  7880. TManagedTerminal * TargetTerminal = NULL;
  7881. TFileOperation Operation = foNone;
  7882. if (FDDTargetControl == SessionsPageControl)
  7883. {
  7884. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7885. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7886. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7887. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7888. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7889. {
  7890. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7891. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7892. {
  7893. Operation = foRemoteMove;
  7894. }
  7895. else
  7896. {
  7897. Operation = foRemoteCopy;
  7898. }
  7899. }
  7900. }
  7901. else
  7902. {
  7903. if (FLastDropEffect == DROPEFFECT_MOVE)
  7904. {
  7905. Operation = foRemoteMove;
  7906. }
  7907. else if (FLastDropEffect == DROPEFFECT_COPY)
  7908. {
  7909. Operation = foRemoteCopy;
  7910. }
  7911. }
  7912. if (Operation != foNone)
  7913. {
  7914. RemoteFileControlFileOperation(DropSourceControl,
  7915. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7916. }
  7917. // abort drag&drop
  7918. Abort();
  7919. }
  7920. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7921. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7922. (FDDTargetControl != QueueView3))
  7923. {
  7924. DebugAssert(!FDragTempDir.IsEmpty());
  7925. TTransferType Type;
  7926. UnicodeString TempDir = FDragTempDir;
  7927. // We clear FDragTempDir before calling
  7928. // just in case it fail (raises exception)
  7929. FDragTempDir = L"";
  7930. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7931. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7932. // empty directory parameter means temp directory -> don't display it!
  7933. UnicodeString TargetDir = L"";
  7934. int Options = 0;
  7935. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7936. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7937. {
  7938. Abort();
  7939. }
  7940. // TargetDir is set when dropped on local file control
  7941. // (this was workaround for legacy dirview event handling, now it should be
  7942. // made prettier)
  7943. if (TargetDir.IsEmpty())
  7944. {
  7945. TargetDir = TempDir;
  7946. if (ForceDirectories(ApiPath(TargetDir)))
  7947. {
  7948. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7949. FPendingTempSpaceWarn = true;
  7950. try
  7951. {
  7952. FDragDropOperation = true;
  7953. int Params = cpTemporary |
  7954. (Type == ttMove ? cpDelete : 0);
  7955. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7956. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7957. }
  7958. __finally
  7959. {
  7960. FDragDropOperation = false;
  7961. FPendingTempSpaceWarn = false;
  7962. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7963. }
  7964. }
  7965. else
  7966. {
  7967. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7968. }
  7969. }
  7970. }
  7971. }
  7972. //---------------------------------------------------------------------------
  7973. void __fastcall TCustomScpExplorerForm::DDDownload(
  7974. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7975. {
  7976. DebugAssert(!IsLocalBrowserMode());
  7977. TAutoBatch AutoBatch(this);
  7978. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7979. UpdateCopyParamCounters(*CopyParam);
  7980. FMoveToQueue = false;
  7981. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7982. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7983. {
  7984. RemoteDriveView->UpdateDropSource();
  7985. }
  7986. }
  7987. //---------------------------------------------------------------------------
  7988. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7989. {
  7990. public:
  7991. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7992. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7993. {
  7994. }
  7995. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7996. {
  7997. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7998. TDataObjectFilesEx::AllowData(FormatEtc);
  7999. }
  8000. };
  8001. //---------------------------------------------------------------------------
  8002. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  8003. TObject * Sender, TDataObject *& DataObject)
  8004. {
  8005. if (FDDExtMapFile != NULL)
  8006. {
  8007. TFileList * FileList = DragDropFiles(Sender)->FileList;
  8008. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  8009. {
  8010. Abort();
  8011. }
  8012. if (FileList->Count > 0)
  8013. {
  8014. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  8015. if (!FilesObject->IsValid(true, true))
  8016. {
  8017. FilesObject->_Release();
  8018. }
  8019. else
  8020. {
  8021. DataObject = FilesObject;
  8022. }
  8023. }
  8024. }
  8025. }
  8026. //---------------------------------------------------------------------------
  8027. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  8028. {
  8029. DebugFail();
  8030. }
  8031. //---------------------------------------------------------------------------
  8032. void __fastcall TCustomScpExplorerForm::GoToTree()
  8033. {
  8034. ComponentVisible[fcRemoteTree] = true;
  8035. DriveView(osRemote)->SetFocus();
  8036. }
  8037. //---------------------------------------------------------------------------
  8038. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  8039. TPanelExport Export)
  8040. {
  8041. TCustomDirView * DirView = this->DirView(Side);
  8042. std::unique_ptr<TStrings> ExportData(new TStringList());
  8043. switch (Export)
  8044. {
  8045. case pePath:
  8046. ExportData->Add(DirView->PathName);
  8047. break;
  8048. case peFileList:
  8049. case peFullFileList:
  8050. {
  8051. bool FullPath = (Export == peFullFileList);
  8052. DirView->CreateFileList(false, FullPath, ExportData.get());
  8053. for (int Index = 0; Index < ExportData->Count; Index++)
  8054. {
  8055. if (ExportData->Strings[Index].Pos(L" ") > 0)
  8056. {
  8057. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  8058. }
  8059. }
  8060. }
  8061. break;
  8062. default:
  8063. DebugFail();
  8064. }
  8065. return ExportData.release();
  8066. }
  8067. //---------------------------------------------------------------------------
  8068. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  8069. TPanelExport Export, TPanelExportDestination Destination)
  8070. {
  8071. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  8072. PanelExportStore(Side, Export, Destination, ExportData.get());
  8073. }
  8074. //---------------------------------------------------------------------------
  8075. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  8076. TPanelExport /*Export*/, TPanelExportDestination Destination,
  8077. TStrings * ExportData)
  8078. {
  8079. if (Destination == pedClipboard)
  8080. {
  8081. TInstantOperationVisualizer Visualizer;
  8082. CopyToClipboard(ExportData);
  8083. }
  8084. else
  8085. {
  8086. DebugFail();
  8087. }
  8088. }
  8089. //---------------------------------------------------------------------------
  8090. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  8091. {
  8092. TCustomDirView * DirView = this->DirView(Side);
  8093. UnicodeString Mask = DirView->Mask;
  8094. if (DoFilterMaskDialog(DirView, Mask))
  8095. {
  8096. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  8097. Configuration->Usage->Inc(L"Filters");
  8098. }
  8099. }
  8100. //---------------------------------------------------------------------------
  8101. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  8102. {
  8103. return FQueueController->DefaultOperation();
  8104. }
  8105. //---------------------------------------------------------------------------
  8106. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  8107. TQueueOperation Operation, void ** Param)
  8108. {
  8109. switch (Operation)
  8110. {
  8111. case qoPreferences:
  8112. return true;
  8113. case qoGoTo:
  8114. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  8115. case qoOnceEmpty:
  8116. return IsAnythingQueued();
  8117. default:
  8118. return FQueueController->AllowOperation(Operation, Param);
  8119. }
  8120. }
  8121. //---------------------------------------------------------------------------
  8122. void __fastcall TCustomScpExplorerForm::GoToQueue()
  8123. {
  8124. if (DebugAlwaysTrue(QueueView3->Visible))
  8125. {
  8126. QueueView3->SetFocus();
  8127. }
  8128. }
  8129. //---------------------------------------------------------------------------
  8130. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  8131. TQueueOperation Operation, void * Param)
  8132. {
  8133. if (Operation == qoGoTo)
  8134. {
  8135. GoToQueue();
  8136. }
  8137. else if (Operation == qoPreferences)
  8138. {
  8139. PreferencesDialog(pmQueue);
  8140. }
  8141. else
  8142. {
  8143. FQueueController->ExecuteOperation(Operation, Param);
  8144. }
  8145. }
  8146. //---------------------------------------------------------------------------
  8147. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  8148. {
  8149. return (Queue != NULL) && Queue->Enabled;
  8150. }
  8151. //---------------------------------------------------------------------------
  8152. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  8153. {
  8154. DebugAssert(Queue != NULL);
  8155. if (Queue != NULL)
  8156. {
  8157. Queue->Enabled = !Queue->Enabled;
  8158. }
  8159. }
  8160. //---------------------------------------------------------------------------
  8161. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  8162. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  8163. {
  8164. FQueueActedItem = QueueView3->ItemFocused;
  8165. }
  8166. //---------------------------------------------------------------------------
  8167. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  8168. {
  8169. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  8170. }
  8171. //---------------------------------------------------------------------------
  8172. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  8173. {
  8174. return
  8175. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  8176. (QueueDock->Visible ? QueueDock->Height : 0) +
  8177. (QueueLabel->Visible ? QueueLabel->Height : 0);
  8178. }
  8179. //---------------------------------------------------------------------------
  8180. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  8181. {
  8182. return ScaleByTextHeight(this, 48);
  8183. }
  8184. //---------------------------------------------------------------------------
  8185. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  8186. TObject * /*Sender*/, int & NewSize, bool & Accept)
  8187. {
  8188. // when queue is hidden by double-clicking splitter, stray attempt to
  8189. // resize the panel with strange value arrives, make sure it is ignored
  8190. if (ComponentVisible[fcQueueView])
  8191. {
  8192. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  8193. if (NewSize > MaxHeightLimit)
  8194. {
  8195. NewSize = MaxHeightLimit;
  8196. }
  8197. int MinHeightLimit =
  8198. GetStaticQueuePanelComponentsHeight() +
  8199. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  8200. GetMinQueueViewHeight();
  8201. if (NewSize < MinHeightLimit)
  8202. {
  8203. NewSize = MinHeightLimit;
  8204. }
  8205. }
  8206. else
  8207. {
  8208. Accept = false;
  8209. }
  8210. }
  8211. //---------------------------------------------------------------------------
  8212. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  8213. {
  8214. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  8215. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  8216. if (QueueViewHeight < GetMinQueueViewHeight())
  8217. {
  8218. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  8219. }
  8220. }
  8221. //---------------------------------------------------------------------------
  8222. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  8223. TDragObject *& /*DragObject*/)
  8224. {
  8225. FQueueActedItem = QueueView3->ItemFocused;
  8226. FQueueScrollOnDragOver->StartDrag();
  8227. }
  8228. //---------------------------------------------------------------------------
  8229. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  8230. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  8231. {
  8232. FQueueScrollOnDragOver->EndDrag();
  8233. }
  8234. //---------------------------------------------------------------------------
  8235. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  8236. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  8237. {
  8238. Accept = true;
  8239. if (Source == QueueView3)
  8240. {
  8241. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  8242. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  8243. if (Accept)
  8244. {
  8245. TQueueItemProxy * QueueItem;
  8246. TQueueItemProxy * DestQueueItem;
  8247. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8248. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  8249. Accept = (QueueItem != DestQueueItem) &&
  8250. (QueueItem->Status == TQueueItem::qsPending) &&
  8251. (DestQueueItem->Status == TQueueItem::qsPending);
  8252. }
  8253. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  8254. }
  8255. }
  8256. //---------------------------------------------------------------------------
  8257. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  8258. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8259. {
  8260. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8261. {
  8262. TQueueItemProxy * QueueItem;
  8263. TQueueItemProxy * DestQueueItem;
  8264. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8265. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8266. QueueItem->Move(DestQueueItem);
  8267. }
  8268. }
  8269. //---------------------------------------------------------------------------
  8270. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8271. {
  8272. if ((QueueView3->ItemFocused == NULL) &&
  8273. (QueueView3->Items->Count > 0))
  8274. {
  8275. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8276. }
  8277. QueueLabelUpdateStatus();
  8278. }
  8279. //---------------------------------------------------------------------------
  8280. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8281. {
  8282. QueueLabelUpdateStatus();
  8283. }
  8284. //---------------------------------------------------------------------------
  8285. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8286. {
  8287. QueueLabel->UpdateStatus();
  8288. }
  8289. //---------------------------------------------------------------------------
  8290. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8291. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8292. {
  8293. if (Selected)
  8294. {
  8295. NonVisualDataModule->UpdateNonVisibleActions();
  8296. }
  8297. }
  8298. //---------------------------------------------------------------------------
  8299. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8300. {
  8301. TDragDropFilesEx * Result = NULL;
  8302. if (Sender == SessionsPageControl)
  8303. {
  8304. Result = FSessionsDragDropFilesEx;
  8305. }
  8306. else if (Sender == QueueView3)
  8307. {
  8308. Result = FQueueDragDropFilesEx;
  8309. }
  8310. else
  8311. {
  8312. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8313. if (DirView != NULL)
  8314. {
  8315. Result = DirView->DragDropFilesEx;
  8316. }
  8317. else
  8318. {
  8319. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8320. if (DriveView != NULL)
  8321. {
  8322. Result = DriveView->DragDropFilesEx;
  8323. }
  8324. }
  8325. }
  8326. DebugAssert(Result != NULL);
  8327. return Result;
  8328. }
  8329. //---------------------------------------------------------------------------
  8330. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8331. {
  8332. return (DView->SelCount == DView->FilesCount);
  8333. }
  8334. //---------------------------------------------------------------------------
  8335. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8336. {
  8337. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8338. }
  8339. //---------------------------------------------------------------------------
  8340. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8341. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8342. {
  8343. DebugAssert(!IsLocalBrowserMode());
  8344. TFileOperation Operation;
  8345. switch (Effect)
  8346. {
  8347. case DROPEFFECT_MOVE:
  8348. Operation = foMove;
  8349. break;
  8350. case DROPEFFECT_COPY:
  8351. // occures on WinXP (reported by user)
  8352. default:
  8353. Operation = foCopy;
  8354. break;
  8355. };
  8356. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8357. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8358. if (DragDropFilesEx->FileList->Count > 0)
  8359. {
  8360. TStrings * FileList = new TStringList();
  8361. try
  8362. {
  8363. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8364. {
  8365. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8366. }
  8367. FDragDropOperation = true;
  8368. TTransferOperationParam Param;
  8369. Param.TargetDirectory = TargetPath;
  8370. // upload, no temp dirs
  8371. Param.Temp = false;
  8372. Param.DragDrop = DragDrop;
  8373. Param.Options =
  8374. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8375. if (ForceQueue)
  8376. {
  8377. Param.Queue = asOn;
  8378. }
  8379. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8380. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8381. {
  8382. if (IsFileControl(DropSourceControl, osLocal))
  8383. {
  8384. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8385. }
  8386. else
  8387. {
  8388. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8389. }
  8390. }
  8391. }
  8392. __finally
  8393. {
  8394. FDragDropOperation = false;
  8395. delete FileList;
  8396. }
  8397. }
  8398. }
  8399. //---------------------------------------------------------------------------
  8400. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8401. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8402. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8403. {
  8404. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8405. }
  8406. //---------------------------------------------------------------------------
  8407. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8408. TObject * Sender, int grfKeyState, int & dwEffect)
  8409. {
  8410. DebugAssert(!IsLocalBrowserMode());
  8411. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8412. if ((dwEffect != DROPEFFECT_NONE) &&
  8413. IsFileControl(DropSourceControl, osRemote))
  8414. {
  8415. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8416. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8417. (RemoteDirView->DropTarget == NULL))
  8418. {
  8419. dwEffect = DROPEFFECT_NONE;
  8420. }
  8421. else
  8422. {
  8423. if (dwEffect == DROPEFFECT_COPY)
  8424. {
  8425. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8426. // currently we support copying always (at least via temporary directory);
  8427. // remove associated checks once this all proves stable and working
  8428. bool CopyCapable = true;
  8429. // if we do not support neither of operations, there's no discussion
  8430. if (!MoveCapable && !CopyCapable)
  8431. {
  8432. dwEffect = DROPEFFECT_NONE;
  8433. }
  8434. else
  8435. {
  8436. // The default effect inside remote panel is move,
  8437. // unless we do not support it, but support copy
  8438. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8439. {
  8440. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8441. }
  8442. else
  8443. {
  8444. // with ctrl-down, we want copy unless it is not supported
  8445. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8446. }
  8447. }
  8448. }
  8449. }
  8450. }
  8451. }
  8452. //---------------------------------------------------------------------------
  8453. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8454. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8455. {
  8456. DebugAssert(!IsLocalBrowserMode());
  8457. if (FDDTotalSize >= 0)
  8458. {
  8459. if (File->IsDirectory)
  8460. {
  8461. FDDTotalSize = -1;
  8462. }
  8463. else
  8464. {
  8465. FDDTotalSize += File->Size;
  8466. }
  8467. }
  8468. DebugAssert(!FDragTempDir.IsEmpty());
  8469. // TODO: this is quite ineffective
  8470. // TODO: what if invalid character replacement is disabled?
  8471. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8472. UnicodeString TransferFileName;
  8473. if (Sender == RemoteDriveView)
  8474. {
  8475. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8476. }
  8477. else
  8478. {
  8479. TransferFileName = File->FileName;
  8480. }
  8481. FDDFileList->AddObject(TransferFileName, File);
  8482. }
  8483. //---------------------------------------------------------------------------
  8484. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8485. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8486. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8487. {
  8488. // sometimes we do not get DDEnd so the list is not released
  8489. SAFE_DESTROY(FDDFileList);
  8490. FDDFileList = new TStringList();
  8491. FDragTempDir = WinConfiguration->TemporaryDir();
  8492. FDDTotalSize = 0;
  8493. FDragDropSshTerminate.reset(NULL);
  8494. }
  8495. //---------------------------------------------------------------------------
  8496. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8497. TObject * /*Sender*/, LongBool /*FEscapePressed*/, int /*grfKeyState*/,
  8498. HRESULT & Result)
  8499. {
  8500. if (Result == DRAGDROP_S_DROP)
  8501. {
  8502. try
  8503. {
  8504. GlobalDragImageList->HideDragImage();
  8505. try
  8506. {
  8507. RemoteFileControlDDTargetDrop();
  8508. }
  8509. catch(ESshTerminate & E)
  8510. {
  8511. FDragDropSshTerminate.reset(E.Clone());
  8512. }
  8513. }
  8514. catch (Exception &E)
  8515. {
  8516. // If downloading fails we need to cancel drag&drop, otherwise
  8517. // Explorer shows error
  8518. // But by the way exception probably never reach this point as
  8519. // it's catched on way.
  8520. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8521. Result = DRAGDROP_S_CANCEL;
  8522. ShowExtendedException(Terminal, &E);
  8523. }
  8524. }
  8525. }
  8526. //---------------------------------------------------------------------------
  8527. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8528. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8529. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8530. {
  8531. TFileMasks::TParams MaskParams;
  8532. MaskParams.Size = Size;
  8533. MaskParams.Modification = Modification;
  8534. // this does not re-parse the mask if it is the same as the last time
  8535. FDirViewMatchMask = Masks;
  8536. bool ImplicitMatch;
  8537. Matches =
  8538. // Local and RecurseInclude parameters have no effect as there's no path.
  8539. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8540. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8541. (AllowImplicitMatches || !ImplicitMatch);
  8542. }
  8543. //---------------------------------------------------------------------------
  8544. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8545. TObject * Sender, TListItem * Item, WORD & Indexes)
  8546. {
  8547. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8548. if (GetPartialFileExtLen(DirView->ItemFileName(Item)) > 0)
  8549. {
  8550. Indexes |= oiPartial;
  8551. }
  8552. }
  8553. //---------------------------------------------------------------------------
  8554. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8555. {
  8556. return
  8557. DirViewEnabled(osCurrent) &&
  8558. DirView(osCurrent)->CanPasteFromClipBoard();
  8559. }
  8560. //---------------------------------------------------------------------------
  8561. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8562. {
  8563. bool Result = false;
  8564. if (CanPasteToDirViewFromClipBoard())
  8565. {
  8566. Result = true;
  8567. }
  8568. else
  8569. {
  8570. UnicodeString ClipboardText;
  8571. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8572. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8573. {
  8574. if (StoredSessions->IsUrl(ClipboardText))
  8575. {
  8576. Result = true;
  8577. }
  8578. else
  8579. {
  8580. Result = DirViewEnabled(osCurrent);
  8581. }
  8582. }
  8583. }
  8584. return Result;
  8585. }
  8586. //---------------------------------------------------------------------------
  8587. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8588. {
  8589. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8590. if (DoesClipboardContainOurFiles())
  8591. {
  8592. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8593. {
  8594. DebugAssert(!IsLocalBrowserMode());
  8595. TTerminalManager * Manager = TTerminalManager::Instance();
  8596. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8597. Manager->ActiveSession = FClipboardTerminal;
  8598. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8599. }
  8600. }
  8601. else if (CanPasteToDirViewFromClipBoard())
  8602. {
  8603. DirView(osCurrent)->PasteFromClipBoard();
  8604. }
  8605. else
  8606. {
  8607. UnicodeString ClipboardText;
  8608. if (NonEmptyTextFromClipboard(ClipboardText))
  8609. {
  8610. if (StoredSessions->IsUrl(ClipboardText))
  8611. {
  8612. NewSession(ClipboardText);
  8613. }
  8614. else
  8615. {
  8616. DirView(osCurrent)->Path = ClipboardText;
  8617. }
  8618. }
  8619. }
  8620. }
  8621. //---------------------------------------------------------------------------
  8622. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8623. {
  8624. // TBD
  8625. }
  8626. //---------------------------------------------------------------------------
  8627. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8628. {
  8629. TCustomDirView * ADirView = DirView(Side);
  8630. ADirView->SelectAll(Mode);
  8631. ADirView->SetFocus();
  8632. }
  8633. //---------------------------------------------------------------------------
  8634. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8635. {
  8636. TCustomDirView * ADirView = DirView(Side);
  8637. TFileFilter Filter;
  8638. DefaultFileFilter(Filter);
  8639. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8640. {
  8641. Configuration->Usage->Inc(L"MaskSelections");
  8642. ADirView->SelectFiles(Filter, Select);
  8643. ADirView->SetFocus();
  8644. }
  8645. }
  8646. //---------------------------------------------------------------------------
  8647. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8648. {
  8649. TCustomDirView * ADirView = DirView(Side);
  8650. ADirView->RestoreSelectedNames();
  8651. ADirView->SetFocus();
  8652. }
  8653. //---------------------------------------------------------------------------
  8654. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8655. {
  8656. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8657. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8658. {
  8659. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8660. UnicodeString Ext;
  8661. if (!IsSideLocalBrowser(osCurrent))
  8662. {
  8663. Ext = UnixExtractFileExt(FileName);
  8664. }
  8665. else
  8666. {
  8667. Ext = ExtractFileExt(FileName);
  8668. }
  8669. if (Ext.IsEmpty())
  8670. {
  8671. Ext = L".";
  8672. }
  8673. TFileFilter Filter;
  8674. Filter.Masks = FORMAT(L"*%s", (Ext));
  8675. Filter.Directories = false;
  8676. CurrentDirView->SelectFiles(Filter, Select);
  8677. }
  8678. }
  8679. //---------------------------------------------------------------------------
  8680. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8681. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8682. {
  8683. UnicodeString Result;
  8684. if (FIncrementalSearchState.Searching && (Side == GetSide(osCurrent)))
  8685. {
  8686. Result = FormatIncrementalSearchStatus(FIncrementalSearchState);
  8687. }
  8688. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8689. {
  8690. // noop
  8691. }
  8692. else
  8693. {
  8694. Result =
  8695. FMTLOAD(FILE_INFO_FORMAT,
  8696. (FormatBytes(FileInfo.SelectedSize),
  8697. FormatBytes(FileInfo.FilesSize),
  8698. FormatNumber(FileInfo.SelectedCount),
  8699. FormatNumber(FileInfo.FilesCount)));
  8700. }
  8701. return Result;
  8702. }
  8703. //---------------------------------------------------------------------------
  8704. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8705. TTBXStatusPanel * Panel, TOperationSide Side)
  8706. {
  8707. if (Panel->Index == 1)
  8708. {
  8709. ToggleShowHiddenFiles();
  8710. }
  8711. else if (Panel->Index == 2)
  8712. {
  8713. Filter(Side);
  8714. }
  8715. }
  8716. //---------------------------------------------------------------------------
  8717. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8718. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8719. {
  8720. DebugAssert(!StatusBar->SimplePanel);
  8721. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8722. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8723. }
  8724. //---------------------------------------------------------------------------
  8725. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8726. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8727. {
  8728. DebugAssert(StatusBar->Panels->Count >= 3);
  8729. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8730. if (FileInfo.HiddenCount > 0)
  8731. {
  8732. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8733. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8734. }
  8735. else
  8736. {
  8737. HiddenFilesPanel->ViewPriority = 0;
  8738. // not really necessary, just to cleanup no-longer-valid data
  8739. HiddenFilesPanel->Caption = L"";
  8740. }
  8741. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8742. if (FileInfo.FilteredCount > 0)
  8743. {
  8744. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8745. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8746. }
  8747. else
  8748. {
  8749. FilteredFilesPanel->ViewPriority = 0;
  8750. // not really necessary, just to cleanup no-longer-valid data
  8751. FilteredFilesPanel->Caption = L"";
  8752. }
  8753. }
  8754. //---------------------------------------------------------------------------
  8755. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8756. TObject * /*Sender*/)
  8757. {
  8758. if (DirView(osOther)->Enabled)
  8759. {
  8760. DirView(osOther)->SetFocus();
  8761. }
  8762. }
  8763. //---------------------------------------------------------------------------
  8764. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8765. {
  8766. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8767. switch (Config.Show)
  8768. {
  8769. case qvShow:
  8770. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8771. {
  8772. Config.Show = qvHide;
  8773. }
  8774. else
  8775. {
  8776. Config.Show = Config.LastHideShow;
  8777. }
  8778. break;
  8779. case qvHideWhenEmpty:
  8780. if (ComponentVisible[fcQueueView])
  8781. {
  8782. Config.Show = qvHide;
  8783. }
  8784. else
  8785. {
  8786. Config.LastHideShow = Config.Show;
  8787. Config.Show = qvShow;
  8788. }
  8789. break;
  8790. case qvHide:
  8791. Config.LastHideShow = Config.Show;
  8792. Config.Show = qvShow;
  8793. break;
  8794. }
  8795. WinConfiguration->QueueView = Config;
  8796. }
  8797. //---------------------------------------------------------------------------
  8798. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8799. {
  8800. UnicodeString Result;
  8801. if (Terminal != NULL)
  8802. {
  8803. switch (WinConfiguration->PathInCaption)
  8804. {
  8805. case picShort:
  8806. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8807. break;
  8808. case picFull:
  8809. Result = Terminal->CurrentDirectory;
  8810. break;
  8811. }
  8812. }
  8813. return Result;
  8814. }
  8815. //---------------------------------------------------------------------------
  8816. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8817. {
  8818. TColor Result = GetWindowColor(FSessionColor);
  8819. return Result;
  8820. }
  8821. //---------------------------------------------------------------------------
  8822. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8823. {
  8824. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8825. return Result;
  8826. }
  8827. //---------------------------------------------------------------------------
  8828. void TCustomScpExplorerForm::UpdatePanelControls(TCustomDirView * ADirView, TCustomDriveView * ADriveView)
  8829. {
  8830. bool UseDarkTheme = WinConfiguration->UseDarkTheme();
  8831. ADirView->DarkMode = UseDarkTheme;
  8832. ADriveView->DarkMode = UseDarkTheme;
  8833. }
  8834. //---------------------------------------------------------------------------
  8835. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8836. {
  8837. if (!FSessionChanging)
  8838. {
  8839. TTerminalManager::Instance()->UpdateAppTitle();
  8840. }
  8841. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8842. // background to gray on Windows 7 (works on Windows 10).
  8843. // See also EnableControl
  8844. if (Showing)
  8845. {
  8846. if (ManagedSession != NULL)
  8847. {
  8848. // Update path when it changes
  8849. if ((SessionsPageControl->ActivePage != NULL) &&
  8850. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8851. {
  8852. UpdateSessionTab(SessionsPageControl->ActivePage);
  8853. }
  8854. }
  8855. NonVisualDataModule->ReconnectSessionAction->Update();
  8856. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8857. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8858. CenterReconnectToolbar();
  8859. bool HasTerminal = HasActiveTerminal();
  8860. RemoteDirView->Enabled = HasTerminal;
  8861. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8862. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8863. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8864. RemoteDriveView->Color = RemoteDirView->Color;
  8865. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8866. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8867. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8868. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8869. QueueFileList->Enabled = QueueView3->Enabled;
  8870. QueueFileList->Color = QueueView3->Color;
  8871. QueueFileList->Font->Color = QueueView3->Font->Color;
  8872. QueueLabelUpdateStatus();
  8873. UpdatePanelControls(RemoteDirView, RemoteDriveView);
  8874. bool UseDarkTheme = WinConfiguration->UseDarkTheme();
  8875. if (FImmersiveDarkMode != UseDarkTheme)
  8876. {
  8877. UpdateDarkMode();
  8878. }
  8879. SessionsPageControl->TabTheme = UseDarkTheme ? CurrentTheme : NULL;
  8880. UnicodeString CurrentHiContrastThemeName = (FHiContrastTheme != NULL) ? FHiContrastTheme->Name : EmptyStr;
  8881. bool HiContrast = WinConfiguration->HiContrast;
  8882. UnicodeString NewHiContrastThemeName = HiContrast ? GetThemeName(!UseDarkTheme) : EmptyStr;
  8883. if (CurrentHiContrastThemeName != NewHiContrastThemeName)
  8884. {
  8885. ReleaseHiContrastTheme();
  8886. if (!NewHiContrastThemeName.IsEmpty())
  8887. {
  8888. FHiContrastTheme = GetTBXTheme(NewHiContrastThemeName);
  8889. }
  8890. }
  8891. SessionsPageControl->ActiveTabTheme = HiContrast ? DebugNotNull(FHiContrastTheme) : NULL;
  8892. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8893. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8894. }
  8895. }
  8896. //---------------------------------------------------------------------------
  8897. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8898. {
  8899. UpdateControls();
  8900. }
  8901. //---------------------------------------------------------------------------
  8902. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8903. TObject * Sender)
  8904. {
  8905. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8906. DebugAssert(DirView != NULL);
  8907. DoDirViewLoaded(DirView);
  8908. TransferPresetAutoSelect();
  8909. TUnixDirView * UnixDirView = dynamic_cast<TUnixDirView *>(DirView);
  8910. if (UnixDirView != NULL)
  8911. {
  8912. TManagedTerminal * ATerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(UnixDirView->Terminal));
  8913. TTerminalManager::Instance()->TerminalLoadedDirectory(ATerminal);
  8914. }
  8915. }
  8916. //---------------------------------------------------------------------------
  8917. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8918. {
  8919. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8920. // first run after installation
  8921. if (double(Updates.LastCheck) == 0)
  8922. {
  8923. AppLog(L"First run, scheduling updates check for the next run");
  8924. // make sure next time there will be an update (if enabled)
  8925. Updates.LastCheck = TDateTime(1);
  8926. WinConfiguration->Updates = Updates;
  8927. }
  8928. else
  8929. {
  8930. TDateTime Period;
  8931. if (IsUWP() && !IsOfficialPackage())
  8932. {
  8933. Period = DefaultUpdatesPeriod;
  8934. AppLog(L"Thirdparty UWP package, using default updates check period");
  8935. }
  8936. else
  8937. {
  8938. Period = Updates.Period;
  8939. }
  8940. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8941. if (double(Period) > 0)
  8942. {
  8943. TDateTime Interval = Now() - Updates.LastCheck;
  8944. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8945. if (Interval >= Period)
  8946. {
  8947. StartUpdateThread(UpdatesChecked);
  8948. }
  8949. }
  8950. }
  8951. }
  8952. //---------------------------------------------------------------------------
  8953. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8954. {
  8955. AppLog(L"Updates check done");
  8956. UnicodeString Message;
  8957. bool New;
  8958. TQueryType Type;
  8959. GetUpdatesMessage(Message, New, Type, false);
  8960. if (!Message.IsEmpty())
  8961. {
  8962. AppLogFmt(L"Updates check message: %s", (Message));
  8963. if (New)
  8964. {
  8965. AppLog(L"New version detected");
  8966. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8967. }
  8968. if (!New && (Type != qtWarning))
  8969. {
  8970. AppLog(L"Posting non-critical updates note");
  8971. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8972. }
  8973. else
  8974. {
  8975. AppLog(L"Posting new version or critical message notification");
  8976. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8977. }
  8978. if (New)
  8979. {
  8980. Configuration->Usage->Inc(L"UpdateNotifications");
  8981. }
  8982. }
  8983. }
  8984. //---------------------------------------------------------------------------
  8985. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8986. {
  8987. RestoreApp();
  8988. bool CanDisplay = !NonVisualDataModule->Busy;
  8989. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8990. {
  8991. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8992. }
  8993. if (CanDisplay)
  8994. {
  8995. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8996. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8997. NonVisualDataModule->StartBusy();
  8998. try
  8999. {
  9000. CheckForUpdates(true);
  9001. }
  9002. __finally
  9003. {
  9004. NonVisualDataModule->EndBusy();
  9005. }
  9006. }
  9007. else
  9008. {
  9009. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  9010. }
  9011. }
  9012. //---------------------------------------------------------------------------
  9013. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  9014. TCopyParamRuleData & Data)
  9015. {
  9016. DebugAssert(Terminal != NULL);
  9017. Data.HostName = Terminal->SessionData->HostNameExpanded;
  9018. Data.UserName = Terminal->SessionData->UserNameExpanded;
  9019. Data.RemoteDirectory = RemoteDirView->PathName;
  9020. }
  9021. //---------------------------------------------------------------------------
  9022. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  9023. {
  9024. // Terminal can be null when we are changing local directory implicitly,
  9025. // because it has been deleted, while no session is connected
  9026. // (Login dialog is open)
  9027. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  9028. {
  9029. DebugAssert(!IsLocalBrowserMode());
  9030. TCopyParamRuleData Data;
  9031. GetTransferPresetAutoSelectData(Data);
  9032. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  9033. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  9034. if (CopyParamIndex < 0)
  9035. {
  9036. // there is no preset that matches autoselection
  9037. // set preset that we consider "default"
  9038. FCopyParamAutoSelected = L""; // forget last autoselected preset
  9039. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  9040. }
  9041. else
  9042. {
  9043. // there is preset matching autoselection
  9044. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  9045. if (CopyParamName == FCopyParamAutoSelected)
  9046. {
  9047. // autoselected the same preset as the last time
  9048. // make no change (i.e. preserve custom user preset, if any)
  9049. }
  9050. else
  9051. {
  9052. // autoselected the different preset then the last time (or there
  9053. // was default preset set)
  9054. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  9055. GUIConfiguration->CopyParamCurrent = CopyParamName;
  9056. }
  9057. }
  9058. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  9059. {
  9060. if (CopyParamIndex >= 0)
  9061. {
  9062. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  9063. }
  9064. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  9065. try
  9066. {
  9067. int Fmt =
  9068. (CopyParamIndex < 0) ?
  9069. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  9070. COPY_PARAM_AUTOSELECTED;
  9071. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  9072. Data->Message = MainInstructions(Message);
  9073. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  9074. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  9075. CopyParamAttrs);
  9076. if (CopyParamIndex >= 0)
  9077. {
  9078. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  9079. Info = FORMAT(L"%s\n \n%s", (Info,
  9080. FMTLOAD(COPY_PARAM_RULE,
  9081. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  9082. }
  9083. Data->Message += L"\n\n" + Info;
  9084. if (WinConfiguration->CopyParamAutoSelectNotice)
  9085. {
  9086. TransferPresetNoteMessage(Data, true);
  9087. }
  9088. else
  9089. {
  9090. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  9091. Data = NULL; // ownership passed
  9092. }
  9093. }
  9094. __finally
  9095. {
  9096. delete Data;
  9097. }
  9098. }
  9099. }
  9100. }
  9101. //---------------------------------------------------------------------------
  9102. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  9103. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  9104. {
  9105. DebugAssert(NoteData != NULL);
  9106. DebugAssert(!IsLocalBrowserMode());
  9107. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  9108. TQueryButtonAlias Aliases[1];
  9109. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  9110. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  9111. Params.Aliases = Aliases;
  9112. Params.AliasesCount = LENOF(Aliases);
  9113. unsigned int Result =
  9114. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  9115. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  9116. switch (Result)
  9117. {
  9118. case qaNeverAskAgain:
  9119. DebugAssert(AllowNeverAskAgain);
  9120. WinConfiguration->CopyParamAutoSelectNotice = false;
  9121. break;
  9122. case qaIgnore:
  9123. PreferencesDialog(pmPresets);
  9124. break;
  9125. }
  9126. }
  9127. //---------------------------------------------------------------------------
  9128. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  9129. {
  9130. // as of now useless, as this is used for notes only, never for balloons, ...
  9131. RestoreApp();
  9132. // .. and we should never be busy here
  9133. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  9134. {
  9135. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  9136. }
  9137. }
  9138. //---------------------------------------------------------------------------
  9139. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  9140. TPreferencesMode APreferencesMode)
  9141. {
  9142. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  9143. TCopyParamRuleData Data;
  9144. if (Terminal != NULL)
  9145. {
  9146. PreferencesData.reset(new TPreferencesDialogData());
  9147. GetTransferPresetAutoSelectData(Data);
  9148. PreferencesData->CopyParamRuleData = &Data;
  9149. }
  9150. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  9151. }
  9152. //---------------------------------------------------------------------------
  9153. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  9154. const TCustomCommandType & Command)
  9155. {
  9156. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  9157. {
  9158. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  9159. }
  9160. }
  9161. //---------------------------------------------------------------------------
  9162. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  9163. {
  9164. bool RemoteAllowed = CustomCommandRemoteAllowed();
  9165. TCustomCommandType Command;
  9166. // make sure we use local custom command when remote are not supported
  9167. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  9168. {
  9169. Command = FLastCustomCommand;
  9170. }
  9171. else
  9172. {
  9173. Command.Params = Command.Params | ccLocal;
  9174. }
  9175. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  9176. FEditingFocusedAdHocCommand = OnFocused;
  9177. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  9178. int Options =
  9179. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  9180. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  9181. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  9182. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  9183. {
  9184. FLastCustomCommand = Command;
  9185. UpdateCustomCommandsToolbar();
  9186. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  9187. }
  9188. }
  9189. //---------------------------------------------------------------------------
  9190. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  9191. {
  9192. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  9193. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9194. DebugAssert(State > 0);
  9195. if (State <= 0)
  9196. {
  9197. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  9198. }
  9199. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  9200. }
  9201. //---------------------------------------------------------------------------
  9202. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  9203. TCustomCommandType & Command, int & State)
  9204. {
  9205. bool Result = !FLastCustomCommand.Command.IsEmpty();
  9206. if (Result)
  9207. {
  9208. Command = FLastCustomCommand;
  9209. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9210. }
  9211. return Result;
  9212. }
  9213. //---------------------------------------------------------------------------
  9214. void __fastcall TCustomScpExplorerForm::BeforeAction()
  9215. {
  9216. ResetIncrementalSearch();
  9217. }
  9218. //---------------------------------------------------------------------------
  9219. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  9220. {
  9221. DebugAssert(ComponentVisible[Component]);
  9222. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  9223. }
  9224. //---------------------------------------------------------------------------
  9225. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  9226. {
  9227. // when queue panel is resized here directly, the status bar is stretched
  9228. // over whole space the panel occupied
  9229. PostComponentHide(fcQueueView);
  9230. }
  9231. //---------------------------------------------------------------------------
  9232. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  9233. {
  9234. ComponentVisible[fcQueueFileList] = false;
  9235. }
  9236. //---------------------------------------------------------------------------
  9237. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  9238. {
  9239. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  9240. RefreshColorMode();
  9241. ResetSysDarkTheme();
  9242. ConfigurationChanged();
  9243. ConfigureInterface();
  9244. // To update references to recreated/destroyed themes in SessionsPageControl
  9245. UpdateControls();
  9246. // Should be called for all controls
  9247. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  9248. }
  9249. //---------------------------------------------------------------------------
  9250. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  9251. {
  9252. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  9253. if (!FInvalid &&
  9254. (Message.LParam != 0) &&
  9255. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  9256. {
  9257. ThemeChanged();
  9258. }
  9259. TForm::Dispatch(&Message);
  9260. }
  9261. //---------------------------------------------------------------------------
  9262. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  9263. {
  9264. TMessage * M = static_cast<TMessage*>(Message);
  9265. switch (M->Msg)
  9266. {
  9267. case CM_DIALOGCHAR:
  9268. CMDialogChar(*M);
  9269. break;
  9270. case WM_APPCOMMAND:
  9271. WMAppCommand(*M);
  9272. break;
  9273. case WM_SYSCOMMAND:
  9274. WMSysCommand(*M);
  9275. break;
  9276. case WM_QUERYENDSESSION:
  9277. WMQueryEndSession(*M);
  9278. break;
  9279. case WM_ENDSESSION:
  9280. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9281. break;
  9282. #ifdef _DEBUG
  9283. case WM_WINDOWPOSCHANGED:
  9284. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9285. break;
  9286. case CM_CUSTOMSTYLECHANGED:
  9287. TForm::Dispatch(Message);
  9288. break;
  9289. #endif
  9290. case WM_COMPONENT_HIDE:
  9291. {
  9292. Byte Component = static_cast<Byte>(M->WParam);
  9293. // sanity check
  9294. if (ComponentVisible[Component])
  9295. {
  9296. // special treatment
  9297. if (Component == fcQueueView)
  9298. {
  9299. ToggleQueueVisibility();
  9300. DebugAssert(!ComponentVisible[fcQueueView]);
  9301. }
  9302. else
  9303. {
  9304. ComponentVisible[Component] = false;
  9305. }
  9306. }
  9307. }
  9308. break;
  9309. case WM_COPYDATA:
  9310. WMCopyData(*M);
  9311. break;
  9312. case WM_MANAGES_CAPTION:
  9313. // caption managed in TTerminalManager::UpdateAppTitle()
  9314. M->Result = 1;
  9315. break;
  9316. case WM_WANTS_MOUSEWHEEL:
  9317. M->Result = 1;
  9318. break;
  9319. case CM_SHOWINGCHANGED:
  9320. CMShowingChanged(*M);
  9321. break;
  9322. case WM_CLOSE:
  9323. WMClose(*M);
  9324. break;
  9325. case WM_DPICHANGED:
  9326. WMDpiChanged(*M);
  9327. break;
  9328. case WM_SETTINGCHANGE:
  9329. WMSettingChange(*M);
  9330. break;
  9331. case CM_DIALOGKEY:
  9332. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9333. break;
  9334. case WM_PASTE_FILES:
  9335. PasteFiles();
  9336. break;
  9337. case WM_IS_HIDDEN:
  9338. // To treat master password prompt as a main window while showing it when workspace is loading while
  9339. // the exporer is showing up (Visible is true already)
  9340. M->Result = !FShowing;
  9341. break;
  9342. case WM_SHOWWINDOW:
  9343. if (M->WParam)
  9344. {
  9345. AddStartupSequence(L"J");
  9346. }
  9347. TForm::Dispatch(Message);
  9348. break;
  9349. case WM_QUEUE_CALLBACK:
  9350. WMQueueCallback(*M);
  9351. break;
  9352. default:
  9353. TForm::Dispatch(Message);
  9354. break;
  9355. }
  9356. }
  9357. //---------------------------------------------------------------------------
  9358. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9359. {
  9360. // noop
  9361. }
  9362. //---------------------------------------------------------------------------
  9363. void TCustomScpExplorerForm::RegenerateSessionColorsImageList()
  9364. {
  9365. FSessionColors->Clear();
  9366. AddFixedSessionImages();
  9367. ::RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9368. UpdateImages();
  9369. }
  9370. //---------------------------------------------------------------------------
  9371. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9372. {
  9373. TForm::Dispatch(&Message);
  9374. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9375. UpdatePixelsPerInchMainWindowCounter();
  9376. }
  9377. //---------------------------------------------------------------------------
  9378. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9379. {
  9380. AppLog(L"Main window WM_CLOSE");
  9381. // Cannot close window while we are busy.
  9382. // We cannot test this in FormCloseQuery as that is called also from
  9383. // Close(), which is called by CloseApplicationAction. So we can be busy
  9384. // there already even, when it is legitimate to close the application.
  9385. // Possibly a better place to handle this would be WMSysCommand.
  9386. if (NonVisualDataModule->Busy)
  9387. {
  9388. Message.Result = 1;
  9389. }
  9390. else
  9391. {
  9392. TForm::Dispatch(&Message);
  9393. }
  9394. }
  9395. //---------------------------------------------------------------------------
  9396. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9397. {
  9398. if (!FShowingChanged)
  9399. {
  9400. AddStartupSequence(L"D");
  9401. FShowingChanged = true;
  9402. }
  9403. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9404. bool NoSession = (ManagedSession == NULL);
  9405. // This calls TCustomScpExplorerForm::DoShow
  9406. TForm::Dispatch(&Message);
  9407. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9408. if (Showing && !FStarted)
  9409. {
  9410. FStarted = true;
  9411. InterfaceStarted();
  9412. }
  9413. if (Showing && NoSession)
  9414. {
  9415. // When we are starting minimized (i.e. from an installer),
  9416. // postpone showing Login dialog until we get restored.
  9417. // Otherwise the Login dialog (and Authentication window) show restored
  9418. // over invisible (minimized) main window.
  9419. if (WindowState == wsMinimized)
  9420. {
  9421. FNeedSession = true;
  9422. }
  9423. else
  9424. {
  9425. // This happens before application ever goes idle, so the toolbars would
  9426. // stay enabled (initial state) until the Login dialog is dismissed.
  9427. UpdateActions();
  9428. NonVisualDataModule->StartBusy();
  9429. try
  9430. {
  9431. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9432. // otherwise the dialog does not receive focus.
  9433. // With Commander interface the ProcessMessages is called already
  9434. // by TDriveView, but with Explorer interface, we need to call it explicily
  9435. Application->ProcessMessages();
  9436. // do not reload sessions, they have been loaded just now (optimization)
  9437. NeedSession(true);
  9438. }
  9439. __finally
  9440. {
  9441. NonVisualDataModule->EndBusy();
  9442. }
  9443. }
  9444. }
  9445. }
  9446. //---------------------------------------------------------------------------
  9447. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9448. {
  9449. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9450. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9451. }
  9452. //---------------------------------------------------------------------------
  9453. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9454. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9455. int & MaxHeight)
  9456. {
  9457. // workaround for bug in TWinControl.CalcConstraints
  9458. // Check for empty rect (restore from iconic state) is done there only after
  9459. // call to AdjustClientRect, which enlarges the rect (for forms).
  9460. TRect R = GetClientRect();
  9461. // when restoring from iconic state, place no restrictions
  9462. if (IsRectEmpty(R))
  9463. {
  9464. MinWidth = 0;
  9465. MinHeight = 0;
  9466. MaxWidth = 32000;
  9467. MaxHeight = 32000;
  9468. }
  9469. }
  9470. //---------------------------------------------------------------------------
  9471. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9472. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9473. {
  9474. // terminal can be already closed (e.g. dropped connection)
  9475. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9476. {
  9477. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9478. try
  9479. {
  9480. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9481. }
  9482. catch(...)
  9483. {
  9484. if (!Terminal->Active)
  9485. {
  9486. Close = true;
  9487. }
  9488. throw;
  9489. }
  9490. }
  9491. }
  9492. //---------------------------------------------------------------------------
  9493. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9494. {
  9495. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9496. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9497. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9498. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9499. {
  9500. OnGetSpaceAvailable = GetSpaceAvailable;
  9501. }
  9502. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9503. OnGetSpaceAvailable);
  9504. }
  9505. //---------------------------------------------------------------------------
  9506. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9507. {
  9508. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9509. Terminal->FillSessionDataForCode(Data.get());
  9510. return Data.release();
  9511. }
  9512. //---------------------------------------------------------------------------
  9513. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9514. {
  9515. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9516. DoGenerateUrlDialog(Data.get(), Paths);
  9517. }
  9518. //---------------------------------------------------------------------------
  9519. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9520. {
  9521. GenerateUrl(NULL);
  9522. }
  9523. //---------------------------------------------------------------------------
  9524. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9525. {
  9526. std::unique_ptr<TStrings> Paths(new TStringList());
  9527. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9528. GenerateUrl(Paths.get());
  9529. }
  9530. //---------------------------------------------------------------------------
  9531. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9532. {
  9533. FSessionColor = value;
  9534. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9535. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9536. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9537. DebugAssert(ColorItem != NULL);
  9538. ColorItem->Color = C;
  9539. NonVisualDataModule->ColorMenuItem->Color = C;
  9540. UpdateControls();
  9541. }
  9542. //---------------------------------------------------------------------------
  9543. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9544. {
  9545. if (value != FSessionColor)
  9546. {
  9547. UpdateSessionColor(value);
  9548. // Is null when called from LastTerminalClosed
  9549. if (Terminal != NULL)
  9550. {
  9551. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9552. }
  9553. }
  9554. }
  9555. //---------------------------------------------------------------------------
  9556. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9557. {
  9558. bool Result = FNoteTimer->Enabled;
  9559. if (Result)
  9560. {
  9561. // cannot cancel note too early
  9562. bool NotEarly =
  9563. (Now() - FNoteShown >
  9564. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9565. if (Force || NotEarly)
  9566. {
  9567. FNoteTimer->Enabled = false;
  9568. FNote = L"";
  9569. SAFE_DESTROY(FNoteData);
  9570. FOnNoteClick = NULL;
  9571. FNoteHints = FNotes->Text;
  9572. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9573. UpdateStatusBar();
  9574. }
  9575. }
  9576. return Result;
  9577. }
  9578. //---------------------------------------------------------------------------
  9579. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9580. {
  9581. DebugAssert(FNoteTimer->Enabled);
  9582. CancelNote(true);
  9583. }
  9584. //---------------------------------------------------------------------------
  9585. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9586. {
  9587. FNoteHints = FNotes->Text.TrimRight();
  9588. }
  9589. //---------------------------------------------------------------------------
  9590. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9591. {
  9592. int P = Note.Pos(L"\n");
  9593. if (P > 0)
  9594. {
  9595. Note.SetLength(P - 1);
  9596. }
  9597. FNotes->Add(FORMAT(L"[%s] %s",
  9598. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9599. while (FNotes->Count > 10)
  9600. {
  9601. FNotes->Delete(0);
  9602. }
  9603. if (UpdateNow)
  9604. {
  9605. UpdateNoteHints();
  9606. UpdateStatusBar();
  9607. }
  9608. }
  9609. //---------------------------------------------------------------------------
  9610. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9611. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9612. {
  9613. int P = Note.Pos(L"\n");
  9614. if (P > 0)
  9615. {
  9616. Note.SetLength(P - 1);
  9617. }
  9618. UpdateNoteHints();
  9619. FNote = Note;
  9620. SAFE_DESTROY(FNoteData);
  9621. FNoteData = NoteData;
  9622. FOnNoteClick = OnNoteClick;
  9623. AddNote(Note, false);
  9624. UpdateStatusBar();
  9625. FNoteShown = Now();
  9626. FNoteTimer->Enabled = false;
  9627. if (Seconds == 0)
  9628. {
  9629. Seconds = WinConfiguration->NotificationsTimeout;
  9630. }
  9631. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9632. FNoteTimer->Enabled = true;
  9633. }
  9634. //---------------------------------------------------------------------------
  9635. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9636. {
  9637. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9638. }
  9639. //---------------------------------------------------------------------------
  9640. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9641. {
  9642. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9643. {
  9644. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9645. }
  9646. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9647. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9648. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9649. 0, NULL, NULL);
  9650. }
  9651. //---------------------------------------------------------------------------
  9652. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9653. {
  9654. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9655. UnicodeString Note =
  9656. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9657. PostNote(Note, 0, NULL, NULL);
  9658. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9659. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9660. }
  9661. //---------------------------------------------------------------------------
  9662. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9663. {
  9664. WinConfiguration->FormatSizeBytes = Style;
  9665. }
  9666. //---------------------------------------------------------------------------
  9667. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9668. {
  9669. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9670. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9671. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9672. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9673. }
  9674. //---------------------------------------------------------------------------
  9675. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9676. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9677. {
  9678. if (Panel->Index == 0)
  9679. {
  9680. if (FOnNoteClick != NULL)
  9681. {
  9682. // prevent the user data from being freed by possible call
  9683. // to CancelNote or PostNote during call to OnNoteClick
  9684. std::unique_ptr<TObject> NoteData(FNoteData);
  9685. TNotifyEvent OnNoteClick = FOnNoteClick;
  9686. FNoteData = NULL;
  9687. // need to cancel the note as we are going to delete its user data
  9688. CancelNote(true);
  9689. OnNoteClick(NoteData.get());
  9690. }
  9691. }
  9692. if (HasActiveTerminal() &&
  9693. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9694. {
  9695. FileSystemInfo();
  9696. }
  9697. }
  9698. //---------------------------------------------------------------------------
  9699. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9700. {
  9701. // workaround:
  9702. // 1) for unknown reason, disabling window, while minimized,
  9703. // prevents it from restoring, even if it was enabled again meanwhile
  9704. // 2) when disabling the main window, while another has focus
  9705. // minimize is no longer possible
  9706. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9707. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9708. // Shouldn't we use IsApplicationMinimized() here?
  9709. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9710. {
  9711. Enabled = false;
  9712. }
  9713. FLockLevel++;
  9714. }
  9715. //---------------------------------------------------------------------------
  9716. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9717. {
  9718. DebugAssert(FLockLevel > 0);
  9719. FLockLevel--;
  9720. if (FLockLevel == 0)
  9721. {
  9722. DebugAssert(FLockSuspendLevel == 0);
  9723. Enabled = true;
  9724. // VCL_COPY (TCustomForm.SetWindowFocus)
  9725. if (Active && (ActiveControl != NULL))
  9726. {
  9727. ::SetFocus(ActiveControl->Handle);
  9728. if (GetFocus() == ActiveControl->Handle)
  9729. {
  9730. ActiveControl->Perform(CM_UIACTIVATE, 0, 0);
  9731. }
  9732. }
  9733. }
  9734. }
  9735. //---------------------------------------------------------------------------
  9736. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9737. {
  9738. // We need to make sure that window is enabled when the last modal window closes
  9739. // otherwise focus is not restored correctly.
  9740. // So we re-enable the window when modal window opens and
  9741. // disable it back after is closes
  9742. if (FLockLevel > 0)
  9743. {
  9744. // while we have nesting counter, we know that we never be called
  9745. // recursivelly as Application->OnModalBegin is called only
  9746. // for the top-level modal window
  9747. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9748. {
  9749. // won't be disabled when conditions in LockWindow() were not satisfied
  9750. FDisabledOnLockSuspend = !Enabled;
  9751. // When minimized to tray (or actually when set to SW_HIDE),
  9752. // setting Enabled makes the window focusable even when there's
  9753. // modal window over it
  9754. if (!FTrayIcon->Visible)
  9755. {
  9756. Enabled = true;
  9757. }
  9758. }
  9759. FLockSuspendLevel++;
  9760. }
  9761. }
  9762. //---------------------------------------------------------------------------
  9763. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9764. {
  9765. if (FLockSuspendLevel > 0)
  9766. {
  9767. DebugAssert(FLockLevel > 0);
  9768. FLockSuspendLevel--;
  9769. // see comment in SuspendWindowLock
  9770. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9771. {
  9772. // Note that window can be enabled here, when we were minized to tray when
  9773. // was SuspendWindowLock() called.
  9774. // We should possibly do the same check as in LockWindow(),
  9775. // if it is ever possible that the consitions change between
  9776. // SuspendWindowLock() and ResumeWindowLock()
  9777. Enabled = !FDisabledOnLockSuspend;
  9778. }
  9779. }
  9780. }
  9781. //---------------------------------------------------------------------------
  9782. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9783. {
  9784. TTBXComboBoxItem * RemotePathComboBox =
  9785. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9786. if (!TextOnly)
  9787. {
  9788. TStrings * Items = RemotePathComboBox->Strings;
  9789. Items->BeginUpdate();
  9790. try
  9791. {
  9792. Items->Clear();
  9793. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9794. {
  9795. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9796. while (!IsUnixRootPath(APath))
  9797. {
  9798. int P = APath.LastDelimiter(L'/');
  9799. DebugAssert(P >= 0);
  9800. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9801. APath.SetLength(P - 1);
  9802. }
  9803. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9804. }
  9805. }
  9806. __finally
  9807. {
  9808. Items->EndUpdate();
  9809. }
  9810. }
  9811. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9812. // Setting ItemIndex to -1 does not reset its text
  9813. if (RemotePathComboBox->Strings->Count == 0)
  9814. {
  9815. RemotePathComboBox->Text = L"";
  9816. }
  9817. }
  9818. //---------------------------------------------------------------------------
  9819. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9820. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9821. {
  9822. if (AIndex < 0)
  9823. {
  9824. AIndex = Sender->ItemIndex;
  9825. }
  9826. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9827. }
  9828. //---------------------------------------------------------------------------
  9829. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9830. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9831. int & ImageIndex)
  9832. {
  9833. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9834. }
  9835. //---------------------------------------------------------------------------
  9836. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9837. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9838. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9839. {
  9840. if (!IsLocalBrowserMode())
  9841. {
  9842. ARect.Left += (10 * AIndex);
  9843. }
  9844. }
  9845. //---------------------------------------------------------------------------
  9846. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9847. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9848. {
  9849. AWidth += (10 * AIndex);
  9850. }
  9851. //---------------------------------------------------------------------------
  9852. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9853. {
  9854. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9855. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9856. int Index = RemotePathComboBox->ItemIndex;
  9857. while (Index < RemotePathComboBox->Strings->Count - 1)
  9858. {
  9859. APath = UnixExtractFileDir(APath);
  9860. Index++;
  9861. }
  9862. // VanDyke style paths
  9863. if (APath.IsEmpty())
  9864. {
  9865. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9866. APath = ROOTDIRECTORY;
  9867. }
  9868. if (RemoteDirView->Path != APath)
  9869. {
  9870. RemoteDirView->Path = APath;
  9871. }
  9872. }
  9873. //---------------------------------------------------------------------------
  9874. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9875. {
  9876. DoRemotePathComboBoxItemClick(Sender);
  9877. }
  9878. //---------------------------------------------------------------------------
  9879. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9880. {
  9881. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9882. DebugUsedParam(Sender);
  9883. UpdateRemotePathComboBox(true);
  9884. }
  9885. //---------------------------------------------------------------------------
  9886. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9887. {
  9888. DoRemotePathComboBoxCancel(Sender);
  9889. }
  9890. //---------------------------------------------------------------------------
  9891. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9892. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9893. {
  9894. ResetIncrementalSearch();
  9895. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9896. DebugAssert(DirView != NULL);
  9897. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9898. {
  9899. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9900. // OnEditing is called also from TCustomListView::CanEdit
  9901. if (Edit != NULL)
  9902. {
  9903. EditSelectBaseName(Edit);
  9904. }
  9905. }
  9906. }
  9907. //---------------------------------------------------------------------------
  9908. //---------------------------------------------------------------------------
  9909. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9910. {
  9911. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9912. Result->AutoDetectDnD = false;
  9913. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9914. Result->RenderDataOn = rdoEnterAndDropSync;
  9915. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9916. return Result;
  9917. }
  9918. //---------------------------------------------------------------------------
  9919. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9920. {
  9921. if (IsWin10Build(19041))
  9922. {
  9923. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9924. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9925. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9926. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9927. }
  9928. }
  9929. //---------------------------------------------------------------------------
  9930. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9931. {
  9932. TForm::CreateWnd();
  9933. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9934. UpdateDarkMode();
  9935. if (FSessionsDragDropFilesEx == NULL)
  9936. {
  9937. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9938. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9939. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9940. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9941. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9942. }
  9943. if (FQueueDragDropFilesEx == NULL)
  9944. {
  9945. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9946. // No need to set OnDragOver as we do not have any restrictions
  9947. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9948. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9949. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9950. }
  9951. }
  9952. //---------------------------------------------------------------------------
  9953. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9954. {
  9955. TForm::DestroyWnd();
  9956. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9957. FQueueDragDropFilesEx->DragDropControl = NULL;
  9958. }
  9959. //---------------------------------------------------------------------------
  9960. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9961. {
  9962. SideEnter(FCurrentSide);
  9963. FEverShown = true;
  9964. }
  9965. //---------------------------------------------------------------------------
  9966. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9967. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9968. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9969. {
  9970. if (!NonVisualDataModule->Busy)
  9971. {
  9972. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9973. Configuration->Usage->Inc(L"FileFinds");
  9974. LockWindow(true);
  9975. NonVisualDataModule->StartBusy();
  9976. WinConfiguration->LockedInterface = true;
  9977. try
  9978. {
  9979. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9980. }
  9981. __finally
  9982. {
  9983. WinConfiguration->LockedInterface = false;
  9984. NonVisualDataModule->EndBusy();
  9985. UnlockWindow();
  9986. }
  9987. }
  9988. }
  9989. //---------------------------------------------------------------------------
  9990. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9991. {
  9992. if (!NonVisualDataModule->Busy)
  9993. {
  9994. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9995. SetFocus();
  9996. RemoteDirView->Path = UnixExtractFilePath(Path);
  9997. UnicodeString FileName = UnixExtractFileName(Path);
  9998. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9999. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  10000. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  10001. if (Item == NULL)
  10002. {
  10003. ReloadDirectory(osRemote);
  10004. Item = RemoteDirView->FindFileItem(FileName);
  10005. }
  10006. if (Item != NULL)
  10007. {
  10008. RemoteDirView->ItemFocused = Item;
  10009. RemoteDirView->ItemFocused->MakeVisible(false);
  10010. RemoteDirView->SetFocus();
  10011. }
  10012. }
  10013. }
  10014. //---------------------------------------------------------------------------
  10015. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  10016. {
  10017. bool Result = !NonVisualDataModule->Busy;
  10018. if (Result)
  10019. {
  10020. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  10021. }
  10022. return Result;
  10023. }
  10024. //---------------------------------------------------------------------------
  10025. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  10026. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10027. {
  10028. if (CanOperateOnFoundFiles(ATerminal))
  10029. {
  10030. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished, OnFileOperationFinished);
  10031. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  10032. }
  10033. }
  10034. //---------------------------------------------------------------------------
  10035. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  10036. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10037. {
  10038. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  10039. }
  10040. //---------------------------------------------------------------------------
  10041. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  10042. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10043. {
  10044. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  10045. }
  10046. //---------------------------------------------------------------------------
  10047. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  10048. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10049. {
  10050. if (CanOperateOnFoundFiles(ATerminal))
  10051. {
  10052. for (int Index = 0; Index < FileList->Count; Index++)
  10053. {
  10054. UnicodeString FileName = FileList->Strings[Index];
  10055. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  10056. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  10057. OnFileOperationFinished(osRemote, FileName, true, true);
  10058. }
  10059. }
  10060. }
  10061. //---------------------------------------------------------------------------
  10062. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  10063. {
  10064. FFileFindTerminal = Terminal;
  10065. ShowFileFindDialog(
  10066. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  10067. DoDownloadFoundFiles, DoEditFoundFiles);
  10068. }
  10069. //---------------------------------------------------------------------------
  10070. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  10071. {
  10072. FTaskbarList = TaskbarList;
  10073. }
  10074. //---------------------------------------------------------------------------
  10075. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  10076. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  10077. {
  10078. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10079. if (Index >= 0)
  10080. {
  10081. if (Button == mbLeft)
  10082. {
  10083. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  10084. // If switching tab (switching session) takes long enough for user
  10085. // to actually release the button, "mouse down" is still raised,
  10086. // but we do not get "mouse up" event, so dragging is not cancelled,
  10087. // prevent that by not beginning dragging in the first place.
  10088. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  10089. if (IsKeyPressed(Key))
  10090. {
  10091. // when user clicks the "+", we get mouse down only after the session
  10092. // is closed, when new session tab is already on X:Y, so dragging
  10093. // starts, prevent that
  10094. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  10095. {
  10096. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10097. if (Terminal != NULL)
  10098. {
  10099. SessionsPageControl->BeginDrag(false);
  10100. }
  10101. }
  10102. }
  10103. }
  10104. else if (Button == mbMiddle)
  10105. {
  10106. // ignore middle-click for "New session tab" and the last local-local tab
  10107. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10108. if ((Session != NULL) && CanCloseSession(Session))
  10109. {
  10110. CloseSessionTab(Index);
  10111. }
  10112. }
  10113. }
  10114. }
  10115. //---------------------------------------------------------------------------
  10116. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  10117. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  10118. {
  10119. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10120. // do not allow dropping on the "+" tab
  10121. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10122. if ((TargetTerminal != NULL) &&
  10123. (SessionsPageControl->ActivePage->PageIndex != Index))
  10124. {
  10125. Configuration->Usage->Inc(L"SessionTabMoves");
  10126. // this is almost redundant as we would recreate tabs in SessionListChanged,
  10127. // but we want to actually prevent that to avoid flicker
  10128. SessionsPageControl->ActivePage->PageIndex = Index;
  10129. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  10130. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  10131. }
  10132. }
  10133. //---------------------------------------------------------------------------
  10134. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  10135. TObject * Sender, TObject * Source, int X, int Y,
  10136. TDragState /*State*/, bool & Accept)
  10137. {
  10138. Accept = (Sender == Source);
  10139. if (Accept)
  10140. {
  10141. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10142. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10143. // do not allow dragging to the "+" tab
  10144. Accept = (Terminal != NULL);
  10145. }
  10146. }
  10147. //---------------------------------------------------------------------------
  10148. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  10149. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  10150. {
  10151. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10152. if (Index < 0)
  10153. {
  10154. Effect = DROPEFFECT_NONE;
  10155. }
  10156. else
  10157. {
  10158. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10159. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  10160. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  10161. {
  10162. Effect = DROPEFFECT_NONE;
  10163. }
  10164. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  10165. {
  10166. Effect = DROPEFFECT_COPY;
  10167. }
  10168. }
  10169. }
  10170. //---------------------------------------------------------------------------
  10171. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  10172. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  10173. {
  10174. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10175. // do not allow dropping on local-local, disconnected and the "+" tab
  10176. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  10177. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10178. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  10179. {
  10180. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  10181. if (!IsFileControl(DropSourceControl, osRemote))
  10182. {
  10183. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  10184. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  10185. // Why don't we use Terminal->CurrentDirectory directly?
  10186. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  10187. // do not force queue, drag drop
  10188. false, true);
  10189. }
  10190. }
  10191. }
  10192. //---------------------------------------------------------------------------
  10193. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  10194. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  10195. {
  10196. // Downloads are handled in RemoteFileControlDDEnd
  10197. if (!IsFileControl(DropSourceControl, osRemote))
  10198. {
  10199. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  10200. Terminal->CurrentDirectory,
  10201. // force queue, drag drop
  10202. true, true);
  10203. }
  10204. }
  10205. //---------------------------------------------------------------------------
  10206. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  10207. {
  10208. FShowing = false;
  10209. // Do not save empty workspace
  10210. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  10211. {
  10212. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  10213. UnicodeString Name = WorkspaceName();
  10214. DoSaveWorkspace(
  10215. Name, DataList.get(),
  10216. !Configuration->DisablePasswordStoring &&
  10217. WinConfiguration->AutoSaveWorkspacePasswords, false);
  10218. WinConfiguration->LastStoredSession = Name;
  10219. }
  10220. AppLog(L"Closing main window");
  10221. }
  10222. //---------------------------------------------------------------------------
  10223. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  10224. {
  10225. TManagedTerminal * ManagedTerminal =
  10226. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  10227. if (ManagedTerminal != NULL)
  10228. {
  10229. ManagedTerminal->DirectoryLoaded = Now();
  10230. }
  10231. }
  10232. //---------------------------------------------------------------------------
  10233. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  10234. TListItem * /*Item*/, bool /*Selected*/)
  10235. {
  10236. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  10237. switch (DirView->LastSelectMethod)
  10238. {
  10239. case smKeyboard:
  10240. Configuration->Usage->Inc(L"KeyboardSelections");
  10241. break;
  10242. case smMouse:
  10243. Configuration->Usage->Inc(L"MouseSelections");
  10244. break;
  10245. }
  10246. }
  10247. //---------------------------------------------------------------------------
  10248. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  10249. {
  10250. if (Color != 0)
  10251. {
  10252. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  10253. }
  10254. else
  10255. {
  10256. return FSessionTabImageIndex;
  10257. }
  10258. }
  10259. //---------------------------------------------------------------------------
  10260. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  10261. {
  10262. TPngImageCollectionItem * Item =
  10263. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  10264. FSessionColors->AddPng(Item->PngImage);
  10265. return FSessionColors->Count - 1;
  10266. }
  10267. //---------------------------------------------------------------------------
  10268. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  10269. {
  10270. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  10271. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  10272. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  10273. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  10274. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  10275. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  10276. }
  10277. //---------------------------------------------------------------------------
  10278. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  10279. {
  10280. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  10281. }
  10282. //---------------------------------------------------------------------------
  10283. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  10284. {
  10285. switch (Side)
  10286. {
  10287. case osLocal:
  10288. return FNewLocalTabTabImageIndex;
  10289. case osRemote:
  10290. case osCurrent:
  10291. return FNewRemoteTabTabImageIndex;
  10292. default:
  10293. DebugFail();
  10294. return -1;
  10295. }
  10296. }
  10297. //---------------------------------------------------------------------------
  10298. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  10299. {
  10300. for (int Index = 0; Index < Control->ControlCount; Index++)
  10301. {
  10302. TControl * AControl = Control->Controls[Index];
  10303. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10304. if (WinControl != NULL)
  10305. {
  10306. CollectItemsWithTextDisplayMode(WinControl);
  10307. }
  10308. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10309. if (Toolbar != NULL)
  10310. {
  10311. // we care for top-level items only
  10312. TTBCustomItem * Items = Toolbar->Items;
  10313. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10314. {
  10315. TTBCustomItem * Item = Items->Items[ItemIndex];
  10316. if (((Item->DisplayMode == nbdmImageAndText) ||
  10317. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10318. EligibleForImageDisplayMode(Item))
  10319. {
  10320. FItemsWithTextDisplayMode.insert(Item);
  10321. }
  10322. }
  10323. }
  10324. }
  10325. }
  10326. //---------------------------------------------------------------------------
  10327. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10328. {
  10329. return true;
  10330. }
  10331. //---------------------------------------------------------------------------
  10332. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10333. {
  10334. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10335. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10336. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10337. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10338. bool Result = true;
  10339. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10340. {
  10341. TTBCustomItem * Item = *i;
  10342. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10343. if (Label != NULL)
  10344. {
  10345. // optimization
  10346. if (Label->Visible == SelectiveToolbarText)
  10347. {
  10348. Result = false;
  10349. }
  10350. else
  10351. {
  10352. Label->Visible = SelectiveToolbarText;
  10353. }
  10354. }
  10355. else
  10356. {
  10357. // optimization
  10358. if (Item->DisplayMode == DisplayMode)
  10359. {
  10360. Result = false;
  10361. }
  10362. else
  10363. {
  10364. Item->DisplayMode = DisplayMode;
  10365. }
  10366. }
  10367. ++i;
  10368. }
  10369. UpdateNewTabTab();
  10370. return Result;
  10371. }
  10372. //---------------------------------------------------------------------------
  10373. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10374. {
  10375. DebugFail();
  10376. }
  10377. //---------------------------------------------------------------------------
  10378. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10379. {
  10380. // Among other, a lock level is non zero, while directory is being loaded,
  10381. // even when it happens as a result of dirview navigation,
  10382. // i.e. when TNonVisualDataModule is NOT FBusy.
  10383. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10384. // Among other this prevents a panel auto update to occur while
  10385. // directory is changing.
  10386. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10387. }
  10388. //---------------------------------------------------------------------------
  10389. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10390. {
  10391. // While the window is disabled, we still seem to process menu shortcuts at least,
  10392. // so stop it at least here.
  10393. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10394. bool Result;
  10395. if (Allowed == aaUpdate)
  10396. {
  10397. Result = true;
  10398. }
  10399. else
  10400. {
  10401. Result = !NonVisualDataModule->Busy;
  10402. if (Result && (Allowed == aaShortCut))
  10403. {
  10404. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10405. Action->Update();
  10406. Result = Action->Enabled;
  10407. }
  10408. }
  10409. return Result;
  10410. }
  10411. //---------------------------------------------------------------------------
  10412. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10413. {
  10414. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10415. }
  10416. //---------------------------------------------------------------------------
  10417. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10418. {
  10419. // This is somewhat redundant to LockWindow() call from
  10420. // TTerminalManager::TerminalReadDirectoryProgress.
  10421. // But disabling window is known not to block keyboard shorcuts
  10422. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10423. if (Busy > 0)
  10424. {
  10425. if (NonVisualDataModule->Busy)
  10426. {
  10427. State = false;
  10428. }
  10429. else
  10430. {
  10431. NonVisualDataModule->StartBusy();
  10432. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10433. LockWindow();
  10434. }
  10435. }
  10436. else if (Busy < 0)
  10437. {
  10438. UnlockWindow();
  10439. NonVisualDataModule->EndBusy();
  10440. }
  10441. else
  10442. {
  10443. State = NonVisualDataModule->Busy;
  10444. }
  10445. }
  10446. //---------------------------------------------------------------------------
  10447. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10448. {
  10449. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10450. if (Index >= 0)
  10451. {
  10452. TPopupMenu * PopupMenu = NULL;
  10453. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10454. if (Session != NULL)
  10455. {
  10456. SessionsPageControl->ActivePageIndex = Index;
  10457. if (DebugAlwaysTrue(SessionTabSwitched()))
  10458. {
  10459. // copied from TControl.WMContextMenu
  10460. SendCancelMode(SessionsPageControl);
  10461. // explicit popup instead of using PopupMenu property
  10462. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10463. // while connecting yet not-connected session and hence
  10464. // allowing an access to commands over not-completely connected session
  10465. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10466. }
  10467. }
  10468. else
  10469. {
  10470. if (SupportsLocalBrowser())
  10471. {
  10472. PopupMenu = NonVisualDataModule->NewTabPopup;
  10473. }
  10474. }
  10475. if (PopupMenu != NULL)
  10476. {
  10477. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10478. PopupMenu->PopupComponent = SessionsPageControl;
  10479. PopupMenu->Popup(Point.x, Point.y);
  10480. }
  10481. }
  10482. Handled = true;
  10483. }
  10484. //---------------------------------------------------------------------------
  10485. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10486. {
  10487. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10488. }
  10489. //---------------------------------------------------------------------------
  10490. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10491. {
  10492. Configuration->Usage->Inc(L"PasswordChanges");
  10493. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10494. ChangePasswordData->DisableAuthentationsExceptPassword();
  10495. ChangePasswordData->ChangePassword = true;
  10496. std::unique_ptr<TTerminal> ChangePasswordSession(
  10497. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10498. ChangePasswordSession->AutoReadDirectory = false;
  10499. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10500. {
  10501. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10502. }
  10503. else
  10504. {
  10505. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10506. }
  10507. }
  10508. //---------------------------------------------------------------------------
  10509. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10510. {
  10511. // No nice way to assert SSH2
  10512. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10513. }
  10514. //---------------------------------------------------------------------------
  10515. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10516. {
  10517. TOperationVisualizer OperationVisualizer;
  10518. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10519. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10520. }
  10521. //---------------------------------------------------------------------------
  10522. void __fastcall TCustomScpExplorerForm::FormAfterMonitorDpiChanged(TObject *, int OldDPI, int NewDPI)
  10523. {
  10524. DebugUsedParam(OldDPI);
  10525. DebugAssert(NewDPI == GetControlPixelsPerInch(this));
  10526. GlyphsModule->PixelsPerInch = NewDPI;
  10527. // Can be called too early when primary monitor has non-system dpi
  10528. if (FSessionColors != NULL)
  10529. {
  10530. RegenerateSessionColorsImageList();
  10531. }
  10532. UpdateTabsSize();
  10533. UpdateSessionsPageControlHeight();
  10534. QueueFileListColumnAutoSize();
  10535. }
  10536. //---------------------------------------------------------------------------
  10537. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10538. {
  10539. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10540. }
  10541. //---------------------------------------------------------------------
  10542. class TPublicControl : public TControl
  10543. {
  10544. friend class TCustomScpExplorerForm;
  10545. };
  10546. //---------------------------------------------------------------------------
  10547. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10548. {
  10549. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10550. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10551. }
  10552. //---------------------------------------------------------------------------
  10553. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10554. {
  10555. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10556. OpenBookmark(Side, Bookmark);
  10557. }
  10558. //---------------------------------------------------------------------------
  10559. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10560. {
  10561. DoBookmarkClick(osLocal, Sender);
  10562. }
  10563. //---------------------------------------------------------------------------
  10564. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10565. {
  10566. DoBookmarkClick(osRemote, Sender);
  10567. }
  10568. //---------------------------------------------------------------------------
  10569. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10570. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10571. {
  10572. if (BookmarkList != NULL)
  10573. {
  10574. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10575. if (IsLocalBrowserMode())
  10576. {
  10577. Side = osLocal;
  10578. }
  10579. if (!WinConfiguration->UseLocationProfiles)
  10580. {
  10581. std::unique_ptr<TStringList> Directories(new TStringList());
  10582. Directories->CaseSensitive = (Side == osRemote);
  10583. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10584. {
  10585. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10586. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10587. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10588. {
  10589. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10590. Item->Caption = EscapeHotkey(Directory);
  10591. Item->ShortCut = Bookmark->ShortCut;
  10592. Item->OnClick = OnBookmarkClick;
  10593. Item->Tag = reinterpret_cast<int>(Bookmark);
  10594. Directories->Add(Directory);
  10595. Menu->Add(Item.release());
  10596. }
  10597. }
  10598. }
  10599. else
  10600. {
  10601. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10602. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10603. {
  10604. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10605. if (!Bookmark->Node.IsEmpty())
  10606. {
  10607. Folders->Add(Bookmark->Node);
  10608. }
  10609. }
  10610. for (int Index = 0; Index < Folders->Count; Index++)
  10611. {
  10612. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10613. Item->Caption = Folders->Strings[Index];
  10614. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10615. Folders->Objects[Index] = Item.get();
  10616. Menu->Add(Item.release());
  10617. }
  10618. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10619. {
  10620. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10621. TTBCustomItem * Parent;
  10622. if (!Bookmark->Node.IsEmpty())
  10623. {
  10624. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10625. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10626. }
  10627. else
  10628. {
  10629. Parent = Menu;
  10630. }
  10631. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10632. Item->Caption = Bookmark->Name;
  10633. Item->ShortCut = Bookmark->ShortCut;
  10634. Item->OnClick = OnBookmarkClick;
  10635. Item->Tag = reinterpret_cast<int>(Bookmark);
  10636. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10637. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10638. {
  10639. // No hint for location profiles that are actually mere "bookmarks"
  10640. }
  10641. else
  10642. {
  10643. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10644. UnicodeString LongHint;
  10645. if (!Bookmark->Local.IsEmpty())
  10646. {
  10647. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10648. }
  10649. if (!Bookmark->Remote.IsEmpty())
  10650. {
  10651. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10652. }
  10653. AddToList(Hint, LongHint, L"|");
  10654. Item->Hint = Hint;
  10655. }
  10656. Parent->Add(Item.release());
  10657. }
  10658. }
  10659. AddMenuSeparator(Menu);
  10660. }
  10661. }
  10662. //---------------------------------------------------------------------------
  10663. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10664. {
  10665. Menu->Clear();
  10666. std::unique_ptr<TTBCustomItem> Item;
  10667. Item.reset(new TTBXItem(Owner));
  10668. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10669. Item->Options = Item->Options << tboDefault;
  10670. Menu->Add(Item.release());
  10671. AddMenuSeparator(Menu);
  10672. if (Terminal != NULL)
  10673. {
  10674. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10675. }
  10676. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10677. Item.reset(new TTBXItem(Owner));
  10678. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10679. Menu->Add(Item.release());
  10680. }
  10681. //---------------------------------------------------------------------------
  10682. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10683. {
  10684. bool Result = true;
  10685. bool Remote = !IsSideLocalBrowser(Side);
  10686. try
  10687. {
  10688. UnicodeString APath = Path;
  10689. if (Remote)
  10690. {
  10691. Terminal->ExceptionOnFail = true;
  10692. }
  10693. else
  10694. {
  10695. APath = ExpandEnvironmentVariables(Path);
  10696. }
  10697. try
  10698. {
  10699. DirView(Side)->Path = APath;
  10700. }
  10701. __finally
  10702. {
  10703. if (Remote)
  10704. {
  10705. Terminal->ExceptionOnFail = false;
  10706. }
  10707. }
  10708. }
  10709. catch (Exception & E)
  10710. {
  10711. if (!Remote || Terminal->Active)
  10712. {
  10713. ShowExtendedExceptionEx(Terminal, &E);
  10714. Result = false;
  10715. }
  10716. else
  10717. {
  10718. throw;
  10719. }
  10720. }
  10721. return Result;
  10722. }
  10723. //---------------------------------------------------------------------------
  10724. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10725. {
  10726. // Make sure there some feedback even when loading tiny local folders
  10727. TInstantOperationVisualizer Visualizer;
  10728. DirView(Side)->ReloadDirectory();
  10729. }
  10730. //---------------------------------------------------------------------------
  10731. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10732. {
  10733. if (Index == SessionsPageControl->TabIndex)
  10734. {
  10735. CloseTab();
  10736. }
  10737. else
  10738. {
  10739. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10740. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10741. TTerminalManager * Manager = TTerminalManager::Instance();
  10742. if (!Terminal->Active)
  10743. {
  10744. Manager->FreeTerminal(Terminal);
  10745. }
  10746. else
  10747. {
  10748. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10749. if (CanCloseQueue(Queue))
  10750. {
  10751. Manager->FreeTerminal(Terminal);
  10752. }
  10753. }
  10754. }
  10755. }
  10756. //---------------------------------------------------------------------------
  10757. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10758. {
  10759. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10760. if (Session == NULL)
  10761. {
  10762. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10763. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10764. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10765. //P = SessionsPageControl->ClientToScreen(P);
  10766. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10767. }
  10768. else
  10769. {
  10770. CloseSessionTab(Index);
  10771. }
  10772. }
  10773. //---------------------------------------------------------------------------
  10774. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10775. {
  10776. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10777. {
  10778. TInstantOperationVisualizer Visualizer;
  10779. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10780. // before we create a new data object
  10781. ClipboardClear();
  10782. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10783. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10784. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10785. // Intermediate variable to WORKAROUND a crash in compiler
  10786. TObjectList * ClipboardFakeMonitors =
  10787. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10788. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10789. if (FClipboardDragDropFilesEx.get() == NULL)
  10790. {
  10791. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10792. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10793. }
  10794. FClipboardDragDropFilesEx->FileList->Clear();
  10795. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10796. FClipboardDragDropFilesEx->CopyToClipboard();
  10797. FClipboardTerminal = Terminal;
  10798. // Need full paths, as cwd can be different once files are pasted
  10799. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10800. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10801. {
  10802. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10803. }
  10804. AppLogFmt(L"Created fake clipboard directory \"%s\" and started drive monitors", (FClipboardFakeDirectory));
  10805. }
  10806. }
  10807. //---------------------------------------------------------------------------
  10808. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10809. {
  10810. if (OpenClipboard(0))
  10811. {
  10812. AppLog(L"Clearing clipboard");
  10813. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10814. CloseClipboard();
  10815. }
  10816. }
  10817. //---------------------------------------------------------------------------
  10818. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10819. {
  10820. return !FClipboardFakeDirectory.IsEmpty();
  10821. }
  10822. //---------------------------------------------------------------------------
  10823. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10824. {
  10825. bool Deleted = RemoveDir(ApiPath(FClipboardFakeDirectory));
  10826. AppLogFmt(L"Clearing clipboard, deleted clipboard temporary folder \"%s\" - %d", (FClipboardFakeDirectory, int(Deleted)));
  10827. FClipboardFakeDirectory = UnicodeString();
  10828. FClipboardTerminal = NULL;
  10829. if (FDownloadingFromClipboard)
  10830. {
  10831. // We are called by the monitor, so attempt to release it would deadlock
  10832. FClipboardFakeMonitorsPendingReset = true;
  10833. }
  10834. else
  10835. {
  10836. FClipboardFakeMonitors.reset(NULL);
  10837. }
  10838. }
  10839. //---------------------------------------------------------------------------
  10840. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10841. {
  10842. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10843. {
  10844. ClipboardStop();
  10845. }
  10846. }
  10847. //---------------------------------------------------------------------------
  10848. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10849. {
  10850. TTerminalManager * Manager = TTerminalManager::Instance();
  10851. Manager->ActiveSession = FClipboardTerminal;
  10852. TTransferOperationParam Params;
  10853. Params.TargetDirectory = TargetDirectory;
  10854. Params.DragDrop = DragDrop;
  10855. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10856. }
  10857. //---------------------------------------------------------------------------
  10858. void TCustomScpExplorerForm::PasteFiles()
  10859. {
  10860. // Guard against possible race conditions
  10861. if (!FClipboardPasteTarget.IsEmpty())
  10862. {
  10863. // ClipboardFakeCreated might be called while we are here, so make sure we finish operating on the same path we started with
  10864. UnicodeString Target = FClipboardPasteTarget;
  10865. FClipboardPasteTarget = EmptyStr;
  10866. // Can fail as it can be e.g. locked by AV, so we retry that later
  10867. bool Removed = RemoveDir(ApiPath(Target));
  10868. AppLogFmt(L"First attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10869. try
  10870. {
  10871. if (NonVisualDataModule->Busy)
  10872. {
  10873. AppLog(L"Is busy, cannot paste");
  10874. }
  10875. else
  10876. {
  10877. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10878. FClipboardFakeMonitorsPendingReset = false;
  10879. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10880. TAutoFlag Flag(FDownloadingFromClipboard);
  10881. AppLog(L"Downloading to paste target...");
  10882. ClipboardDownload(ExtractFilePath(Target), NoConfirmation, true);
  10883. AppLog(L"Downloaded to paste target...");
  10884. }
  10885. }
  10886. __finally
  10887. {
  10888. if (!Removed)
  10889. {
  10890. Removed = RemoveDir(ApiPath(Target));
  10891. AppLogFmt(L"Second attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10892. }
  10893. }
  10894. }
  10895. }
  10896. //---------------------------------------------------------------------------
  10897. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10898. {
  10899. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10900. // (as it pumps a Windows message queue while being shutted down)
  10901. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10902. // Is can happen that creation of our temporary directory is detected (even though we are creating it before starting the monitors).
  10903. // It tent do happen with later copy-pastes, probably because everything is cached already and monitors start quickly.
  10904. !IsPathToSameFile(FClipboardFakeDirectory, FileName) &&
  10905. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10906. {
  10907. AppLogFmt(L"Fake clipboard directory pasted to \"%s\"", (FileName));
  10908. FClipboardPasteTarget = FileName;
  10909. // Particularly when pasting to external HDD, the fake directory is strangely recreated if removed here in the monitor callback.
  10910. // Postponing removing (or actually whole pasting operation) seems to help.
  10911. PostMessage(Handle, WM_PASTE_FILES, 0, 0);
  10912. }
  10913. }
  10914. //---------------------------------------------------------------------------
  10915. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10916. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10917. {
  10918. TFileMasks::TParams MaskParams;
  10919. MaskParams.Size = Size;
  10920. MaskParams.Modification = Modification;
  10921. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10922. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10923. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10924. if (!Local && Directory)
  10925. {
  10926. FileName = UnixExcludeTrailingBackslash(FileName);
  10927. }
  10928. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10929. {
  10930. bool ImplicitMatch;
  10931. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10932. !ImplicitMatch)
  10933. {
  10934. Color = Iter->Color;
  10935. break;
  10936. }
  10937. }
  10938. }
  10939. //---------------------------------------------------------------------------
  10940. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10941. {
  10942. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10943. if (!DirView->IsEditing())
  10944. {
  10945. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10946. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10947. // while searching
  10948. if (Key == VK_BACK)
  10949. {
  10950. if (FIncrementalSearchState.Searching)
  10951. {
  10952. if (FIncrementalSearchState.Text.Length() <= 1)
  10953. {
  10954. ResetIncrementalSearch();
  10955. }
  10956. else
  10957. {
  10958. UnicodeString NewText = LeftStr(FIncrementalSearchState.Text, FIncrementalSearchState.Text.Length() - 1);
  10959. IncrementalSearch(NewText, false, false);
  10960. }
  10961. Key = 0;
  10962. }
  10963. }
  10964. }
  10965. }
  10966. //---------------------------------------------------------------------------
  10967. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10968. {
  10969. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10970. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10971. {
  10972. // Filter control sequences.
  10973. // When not searching yet, prefer use of the space for toggling file selection
  10974. // (so we cannot incrementally search for a string starting with a space).
  10975. if ((Key > VK_SPACE) ||
  10976. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && FIncrementalSearchState.Searching))
  10977. {
  10978. IncrementalSearch(FIncrementalSearchState.Text + Key, false, false);
  10979. Key = 0;
  10980. }
  10981. }
  10982. }
  10983. //---------------------------------------------------------------------------
  10984. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10985. {
  10986. if (FIncrementalSearchState.Searching)
  10987. {
  10988. FIncrementalSearchState.Reset();
  10989. DirView(osCurrent)->UpdateStatusBar();
  10990. }
  10991. }
  10992. //---------------------------------------------------------------------------
  10993. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10994. {
  10995. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10996. if (Item == NULL)
  10997. {
  10998. MessageBeep(MB_ICONHAND);
  10999. }
  11000. else
  11001. {
  11002. TCustomDirView * ADirView = DirView(osCurrent);
  11003. {
  11004. TAutoNestingCounter Guard(FIncrementalSearching);
  11005. ADirView->FocusItem(Item);
  11006. if (ADirView->NortonLike == nlOff)
  11007. {
  11008. if ((ADirView->Selected != Item) ||
  11009. (ADirView->SelCount > 1))
  11010. {
  11011. ADirView->ClearSelection();
  11012. ADirView->Selected = Item;
  11013. }
  11014. }
  11015. }
  11016. FIncrementalSearchState.Searching = true;
  11017. FIncrementalSearchState.Text = Text;
  11018. Item->MakeVisible(false);
  11019. TListItem * NextItem = SearchFile(Text, true, Reverse);
  11020. FIncrementalSearchState.HaveNext = (NextItem != NULL) && (NextItem != Item);
  11021. ADirView->UpdateStatusBar();
  11022. }
  11023. }
  11024. //---------------------------------------------------------------------------
  11025. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  11026. {
  11027. int Index = Item->Index;
  11028. if (!Reverse)
  11029. {
  11030. Index++;
  11031. if (Index >= Item->Owner->Count)
  11032. {
  11033. Index = 0;
  11034. }
  11035. }
  11036. else
  11037. {
  11038. Index--;
  11039. if (Index < 0)
  11040. {
  11041. Index = Item->Owner->Count - 1;
  11042. }
  11043. }
  11044. return Item->Owner->Item[Index];
  11045. }
  11046. //---------------------------------------------------------------------------
  11047. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  11048. {
  11049. TCustomDirView * ADirView = DirView(osCurrent);
  11050. if (ADirView->Items->Count == 0)
  11051. {
  11052. return NULL;
  11053. }
  11054. else
  11055. {
  11056. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  11057. TListItem * Item = CurrentItem;
  11058. if (SkipCurrent)
  11059. {
  11060. Item = DebugNotNull(GetNextFile(Item, Reverse));
  11061. }
  11062. while (true)
  11063. {
  11064. bool Matches = false;
  11065. switch (WinConfiguration->PanelSearch)
  11066. {
  11067. case isNameStartOnly:
  11068. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  11069. break;
  11070. case isName:
  11071. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  11072. break;
  11073. case isAll:
  11074. int ColCount = (ADirView->DirViewStyle == dvsReport) ? ADirView->ColProperties->Count : 1;
  11075. int Index = 0;
  11076. while ((Index < ColCount) && !Matches)
  11077. {
  11078. bool Visible = (ADirView->DirViewStyle == dvsReport) ? ADirView->ColProperties->Visible[Index] : true;
  11079. if (Visible)
  11080. {
  11081. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  11082. }
  11083. Index++;
  11084. }
  11085. break;
  11086. }
  11087. if (Matches)
  11088. {
  11089. return Item;
  11090. }
  11091. Item = GetNextFile(Item, Reverse);
  11092. if (Item == CurrentItem)
  11093. {
  11094. return NULL;
  11095. }
  11096. }
  11097. }
  11098. }
  11099. //---------------------------------------------------------------------------
  11100. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  11101. {
  11102. ResetIncrementalSearch();
  11103. }
  11104. //---------------------------------------------------------------------------
  11105. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  11106. {
  11107. if (Message.CharCode == VK_TAB)
  11108. {
  11109. if (!FIncrementalSearchState.Text.IsEmpty())
  11110. {
  11111. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  11112. bool Reverse = Shift.Contains(ssShift);
  11113. IncrementalSearch(FIncrementalSearchState.Text, true, Reverse);
  11114. Message.Result = 1;
  11115. return;
  11116. }
  11117. }
  11118. else if (Message.CharCode == VK_ESCAPE)
  11119. {
  11120. if (FIncrementalSearchState.Searching)
  11121. {
  11122. ResetIncrementalSearch();
  11123. Message.Result = 1;
  11124. return;
  11125. }
  11126. }
  11127. TForm::Dispatch(&Message);
  11128. }
  11129. //---------------------------------------------------------------------------
  11130. void __fastcall TCustomScpExplorerForm::Deactivate()
  11131. {
  11132. TForm::Deactivate();
  11133. // E.g. when switching to an internal editor window
  11134. ResetIncrementalSearch();
  11135. }
  11136. //---------------------------------------------------------------------------
  11137. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  11138. {
  11139. // When switching to another application
  11140. ResetIncrementalSearch();
  11141. }
  11142. //---------------------------------------------------------------------------
  11143. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  11144. {
  11145. ResetIncrementalSearch();
  11146. }
  11147. //---------------------------------------------------------------------------
  11148. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  11149. {
  11150. if (FIncrementalSearching <= 0)
  11151. {
  11152. ResetIncrementalSearch();
  11153. }
  11154. }
  11155. //---------------------------------------------------------------------------
  11156. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  11157. {
  11158. CountClicksForWindowPrint(this);
  11159. }
  11160. //---------------------------------------------------------------------------
  11161. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  11162. {
  11163. CenterReconnectToolbar();
  11164. }
  11165. //---------------------------------------------------------------------------
  11166. void TCustomScpExplorerForm::DoBrowseFile(TCustomDirView * DirView, const UnicodeString & FileName)
  11167. {
  11168. if (!FileName.IsEmpty())
  11169. {
  11170. DirView->FocusByName(FileName);
  11171. if (DirView->ItemFocused != NULL)
  11172. {
  11173. DirView->ItemFocused->Selected = true;
  11174. }
  11175. }
  11176. }
  11177. //---------------------------------------------------------------------------
  11178. void __fastcall TCustomScpExplorerForm::BrowseFile(const UnicodeString & FileName)
  11179. {
  11180. DoBrowseFile(RemoteDirView, FileName);
  11181. }
  11182. //---------------------------------------------------------------------------
  11183. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  11184. {
  11185. if (!FQueueStatusUpdating)
  11186. {
  11187. bool Refresh;
  11188. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  11189. {
  11190. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  11191. }
  11192. else
  11193. {
  11194. if (FQueueFileList->GetCount() > 0)
  11195. {
  11196. FQueueFileList->Clear();
  11197. Refresh = true;
  11198. }
  11199. }
  11200. int Count = FQueueFileList->GetCount();
  11201. if (QueueFileList->Items->Count != Count)
  11202. {
  11203. DebugAssert(Refresh);
  11204. QueueFileList->Items->Count = Count;
  11205. }
  11206. if (Refresh)
  11207. {
  11208. QueueFileList->Invalidate();
  11209. }
  11210. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  11211. }
  11212. }
  11213. //---------------------------------------------------------------------------
  11214. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  11215. {
  11216. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  11217. }
  11218. //---------------------------------------------------------------------------
  11219. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  11220. {
  11221. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  11222. // See TSynchronizeChecklistDialog::UpdateTimer
  11223. if (QueueFileList->Items->Count > 0)
  11224. {
  11225. QueueFileList->Items->Item[0]->MakeVisible(false);
  11226. }
  11227. UpdateQueueFileList();
  11228. }
  11229. //---------------------------------------------------------------------------
  11230. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  11231. {
  11232. Active = QueueView3->Focused() || QueueFileList->Focused();
  11233. }
  11234. //---------------------------------------------------------------------------
  11235. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  11236. {
  11237. QueueLabelUpdateStatus();
  11238. }
  11239. //---------------------------------------------------------------------------
  11240. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  11241. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  11242. {
  11243. int State = FQueueFileList->GetState(Item->Index);
  11244. if (State == qfsProcessed)
  11245. {
  11246. Sender->Canvas->Font->Color = clGrayText;
  11247. }
  11248. }
  11249. //---------------------------------------------------------------------------
  11250. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  11251. {
  11252. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  11253. QueueFileList->Column[0]->Width =
  11254. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  11255. }
  11256. //---------------------------------------------------------------------------
  11257. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  11258. {
  11259. QueueFileListColumnAutoSize();
  11260. }
  11261. //---------------------------------------------------------------------------
  11262. void __fastcall TCustomScpExplorerForm::CloseApp()
  11263. {
  11264. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  11265. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  11266. // So we clear the flag here.
  11267. DebugAssert(NonVisualDataModule->Busy);
  11268. NonVisualDataModule->EndBusy();
  11269. try
  11270. {
  11271. Close();
  11272. }
  11273. __finally
  11274. {
  11275. NonVisualDataModule->StartBusy();
  11276. }
  11277. }
  11278. //---------------------------------------------------------------------------
  11279. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  11280. {
  11281. return (Terminal != NULL) && Terminal->Active;
  11282. }
  11283. //---------------------------------------------------------------------------
  11284. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  11285. {
  11286. return (ManagedSession != NULL);
  11287. }
  11288. //---------------------------------------------------------------------------
  11289. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  11290. {
  11291. return IsActiveTerminal(Terminal);
  11292. }
  11293. //---------------------------------------------------------------------------
  11294. void TCustomScpExplorerForm::LocalLocalCopy(
  11295. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  11296. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  11297. {
  11298. DebugFail();
  11299. }
  11300. //---------------------------------------------------------------------------
  11301. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  11302. {
  11303. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  11304. }
  11305. //---------------------------------------------------------------------------
  11306. void TCustomScpExplorerForm::UpdateTabsSize()
  11307. {
  11308. // 1) Changing tab list triggers the OnResize.
  11309. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  11310. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  11311. {
  11312. // UpdateTabsCaptionTruncation should be enough
  11313. SessionListChanged(true);
  11314. }
  11315. }
  11316. //---------------------------------------------------------------------------
  11317. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  11318. {
  11319. UpdateTabsSize();
  11320. }
  11321. //---------------------------------------------------------------------------
  11322. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  11323. {
  11324. if (ASession == ManagedSession)
  11325. {
  11326. UpdateSession(ASession);
  11327. }
  11328. TSessionData * Data = ASession->StateData;
  11329. UnicodeString Result;
  11330. if (Side == osLocal)
  11331. {
  11332. Result = Data->LocalDirectory;
  11333. }
  11334. else if (DebugAlwaysTrue(Side == osOther))
  11335. {
  11336. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  11337. }
  11338. return Result;
  11339. }
  11340. //---------------------------------------------------------------------------
  11341. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  11342. {
  11343. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  11344. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  11345. if (Title == Result)
  11346. {
  11347. Result = EmptyStr;
  11348. }
  11349. else
  11350. {
  11351. Result += L"\n";
  11352. }
  11353. return Result;
  11354. }
  11355. //---------------------------------------------------------------------------
  11356. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11357. {
  11358. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11359. if (!ASession->Active)
  11360. {
  11361. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11362. }
  11363. else
  11364. {
  11365. Result += GetSessionPath(ASession, osRemote);
  11366. }
  11367. return Result;
  11368. }
  11369. //---------------------------------------------------------------------------
  11370. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11371. {
  11372. return LoadStr(NEW_REMOTE_TAB_HINT);
  11373. }
  11374. //---------------------------------------------------------------------------
  11375. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11376. {
  11377. if (Index >= 0)
  11378. {
  11379. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11380. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11381. if (TabSession == NULL)
  11382. {
  11383. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11384. if (NewTabTabCaption != TabSheet->Caption)
  11385. {
  11386. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11387. }
  11388. }
  11389. else
  11390. {
  11391. TTerminalManager * Manager = TTerminalManager::Instance();
  11392. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11393. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11394. }
  11395. }
  11396. }
  11397. //---------------------------------------------------------------------------
  11398. typedef std::vector<TListItem *> TListItemsVector;
  11399. struct TCalculateSizeOperation
  11400. {
  11401. TCalculateSizeStats Stats;
  11402. TListItemsVector ListItems;
  11403. size_t Index;
  11404. };
  11405. //---------------------------------------------------------------------------
  11406. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11407. {
  11408. TCalculateSizeOperation CalculateSizeOperation;
  11409. Side = GetSide(Side);
  11410. TCustomDirView * ADirView = DirView(Side);
  11411. bool LocalSide = IsSideLocalBrowser(Side);
  11412. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11413. std::unique_ptr<TStrings> FileList(new TStringList());
  11414. for (int Index = 0; Index < AllFileList->Count; Index++)
  11415. {
  11416. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11417. if (ADirView->ItemIsDirectory(Item))
  11418. {
  11419. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11420. CalculateSizeOperation.ListItems.push_back(Item);
  11421. }
  11422. }
  11423. CalculateSizeOperation.Index = 0;
  11424. __int64 Size = 0;
  11425. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation, &CalculateSizeOperation);
  11426. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11427. FOnFileOperationFinished = DirectorySizeCalculated;
  11428. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation, foCalculateSize);
  11429. TCalculatedSizes CalculatedSizes;
  11430. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11431. if (LocalSide)
  11432. {
  11433. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, Side);
  11434. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11435. }
  11436. else
  11437. {
  11438. TCalculateSizeParams Params;
  11439. Params.Stats = &CalculateSizeOperation.Stats;
  11440. Params.UseCache = true;
  11441. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11442. }
  11443. }
  11444. //---------------------------------------------------------------------------
  11445. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(
  11446. TOperationSide Side, const UnicodeString & FileName, bool Success, bool NotCancelled)
  11447. {
  11448. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11449. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11450. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11451. // optimization
  11452. bool GotExpectedItem =
  11453. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11454. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11455. int Index = -1;
  11456. if (DebugAlwaysTrue(GotExpectedItem))
  11457. {
  11458. Index = FCalculateSizeOperation->Index;
  11459. FCalculateSizeOperation->Index++;
  11460. }
  11461. else
  11462. {
  11463. TListItemsVector::iterator I =
  11464. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11465. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11466. {
  11467. Index = I - FCalculateSizeOperation->ListItems.begin();
  11468. }
  11469. }
  11470. if (DebugAlwaysTrue(Index >= 0) && Success && NotCancelled)
  11471. {
  11472. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11473. ADirView->SetItemCalculatedSize(Item, Size);
  11474. }
  11475. }
  11476. //---------------------------------------------------------------------------
  11477. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11478. {
  11479. Accept = false;
  11480. }
  11481. //---------------------------------------------------------------------------
  11482. void TCustomScpExplorerForm::AutoSizeColumns(TOperationSide Side)
  11483. {
  11484. // Send Ctrl+Gray+ to the list view
  11485. TKeyboardState KeyState;
  11486. GetKeyboardState(KeyState);
  11487. TKeyboardState NewKeyState;
  11488. memset(NewKeyState, 0, sizeof(NewKeyState));
  11489. NewKeyState[VK_CONTROL] = 0x81;
  11490. SetKeyboardState(NewKeyState);
  11491. SendMessage(DirView(Side)->Handle, WM_KEYDOWN, VK_ADD, 0);
  11492. SetKeyboardState(KeyState);
  11493. }
  11494. //---------------------------------------------------------------------------
  11495. void TCustomScpExplorerForm::QueueResetLayoutColumns()
  11496. {
  11497. LoadListViewStr(QueueView3, QueueViewLayoutDefault);
  11498. }
  11499. //---------------------------------------------------------------------------
  11500. void * TCustomScpExplorerForm::SaveFocus()
  11501. {
  11502. return NULL;
  11503. }
  11504. //---------------------------------------------------------------------------
  11505. void TCustomScpExplorerForm::RestoreFocus(void * Focus)
  11506. {
  11507. DebugUsedParam(Focus);
  11508. DebugAssert(Focus == NULL);
  11509. }
  11510. //---------------------------------------------------------------------------
  11511. void TCustomScpExplorerForm::IncrementalSearchStart()
  11512. {
  11513. TCustomDirView * ADirView = DirView(osCurrent);
  11514. ADirView->SetFocus();
  11515. if (!FIncrementalSearchState.Searching)
  11516. {
  11517. FIncrementalSearchState.Searching = true;
  11518. FIncrementalSearchState.HaveNext = false;
  11519. DebugAssert(FIncrementalSearchState.Text.IsEmpty());
  11520. ADirView->UpdateStatusBar();
  11521. }
  11522. }
  11523. //---------------------------------------------------------------------------
  11524. void __fastcall TCustomScpExplorerForm::RemoteDirViewThumbnailNeeded(
  11525. TUnixDirView *, TListItem * Item, TRemoteFile * File, const TSize & Size, TBitmap *& Bitmap)
  11526. {
  11527. // though when downloading to temporary folder, encryption should not be a problem
  11528. if (Terminal->IsCapable[fcBackgroundTransfers] &&
  11529. !File->IsDirectory)
  11530. {
  11531. UnicodeString FileName = File->FullFileName;
  11532. TFileMasks::TParams MaskParams;
  11533. MaskParams.Size = File->Size;
  11534. MaskParams.Modification = File->Modification;
  11535. if ((File->Size <= static_cast<__int64>(WinConfiguration->RemoteThumbnailSizeLimit) * 1024) &&
  11536. FRemoteThumbnailMask.Matches(FileName, false, File->IsDirectory, &MaskParams))
  11537. {
  11538. Bitmap = TTerminalManager::Instance()->ThumbnailNeeded(Terminal, Item->Index, File, Size);
  11539. }
  11540. }
  11541. }
  11542. //---------------------------------------------------------------------------
  11543. TThumbnailDownloadQueueItem * TCustomScpExplorerForm::AddThumbnailDownloadQueueItem(TManagedTerminal * ATerminal)
  11544. {
  11545. DebugAssert(ATerminal == Terminal);
  11546. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  11547. TemporaryFileCopyParam(CopyParam);
  11548. CopyParam.TransferMode = tmBinary;
  11549. UnicodeString SourceDir = ATerminal->CurrentDirectory;
  11550. UnicodeString TempDir, RootTempDir;
  11551. TemporaryDirectoryForRemoteFiles(SourceDir, CopyParam, true, TempDir, RootTempDir);
  11552. TTerminalQueue * AQueue = TTerminalManager::Instance()->FindQueueForTerminal(ATerminal);
  11553. DebugAssert((Queue == NULL) || (AQueue == Queue)); // we might get here before queue is set
  11554. TThumbnailDownloadQueueItem * QueueItem =
  11555. new TThumbnailDownloadQueueItem(this, Terminal, SourceDir, TempDir, &CopyParam);
  11556. AddQueueItem(AQueue, QueueItem, ATerminal);
  11557. return QueueItem;
  11558. }
  11559. //---------------------------------------------------------------------------
  11560. enum { qccRemoteItemVisible, qccRemoteItemRedraw };
  11561. typedef std::pair<int, UnicodeString> TRemoteItemVisibleData;
  11562. //---------------------------------------------------------------------------
  11563. void TCustomScpExplorerForm::PostThumbnailVisibleQueueQuery(int Index, const UnicodeString & FileName)
  11564. {
  11565. // The queue might be destroyed before the WM_QUEUE_CALLBACK is processed
  11566. TRemoteItemVisibleData * Data = new TRemoteItemVisibleData();
  11567. Data->first = Index;
  11568. Data->second = FileName;
  11569. PostMessage(Handle, WM_QUEUE_CALLBACK, qccRemoteItemVisible, reinterpret_cast<WPARAM>(Data));
  11570. }
  11571. //---------------------------------------------------------------------------
  11572. void TCustomScpExplorerForm::PostThumbnailDrawRequest(int Index)
  11573. {
  11574. PostMessage(Handle, WM_QUEUE_CALLBACK, qccRemoteItemRedraw, Index);
  11575. }
  11576. //---------------------------------------------------------------------------
  11577. void TCustomScpExplorerForm::WMQueueCallback(TMessage & Message)
  11578. {
  11579. int Command = Message.WParam;
  11580. if (Command == qccRemoteItemVisible)
  11581. {
  11582. TRemoteItemVisibleData * Data = reinterpret_cast<TRemoteItemVisibleData *>(Message.LParam);
  11583. int Index = Data->first;
  11584. UnicodeString FileName = Data->second;
  11585. delete Data;
  11586. TGuard Guard(Terminal->ThumbnailsSection.get());
  11587. bool Visible = False;
  11588. if (Terminal->ThumbnailsEnabled &&
  11589. // though we should better also disable thumbnails once we switch to different view style
  11590. (RemoteDirView->DirViewStyle == dvsThumbnail))
  11591. {
  11592. // The message might be processed, while we are already loading a different directory
  11593. if ((Index >= 0) &&
  11594. (Index < RemoteDirView->Items->Count))
  11595. {
  11596. TListItem * Item = RemoteDirView->Items->Item[Index];
  11597. Visible =
  11598. (RemoteDirView->ItemFullFileName(Item) == FileName) &&
  11599. ListView_IsItemVisible(RemoteDirView->Handle, Index);
  11600. }
  11601. }
  11602. Terminal->ThumbnailVisible(Index, FileName, Visible);
  11603. }
  11604. else if (Command == qccRemoteItemRedraw)
  11605. {
  11606. int Index = Message.LParam;
  11607. if ((Index >= 0) &&
  11608. (Index < RemoteDirView->Items->Count))
  11609. {
  11610. RemoteDirView->InvalidateItem(RemoteDirView->Items->Item[Index]);
  11611. }
  11612. }
  11613. }
  11614. //---------------------------------------------------------------------------
  11615. void TCustomScpExplorerForm::InitializeRemoteThumbnailMask()
  11616. {
  11617. UnicodeString DefaultRemoteThumbnailMask = L"*.jpg;*.jpeg;*.gif;*.png;*.svg;*.bmp;*.raw;*.ico;*.heic;*.webp";
  11618. UnicodeString RemoteThumbnailMask = WinConfiguration->RemoteThumbnailMask;
  11619. if (RemoteThumbnailMask.IsEmpty() ||
  11620. StartsStr(L"+", RemoteThumbnailMask) || StartsStr(L"-", RemoteThumbnailMask) || StartsStr(L"*", RemoteThumbnailMask))
  11621. {
  11622. std::unique_ptr<TStrings> RemoteThumbnailMasks(new TStringList());
  11623. while (!DefaultRemoteThumbnailMask.IsEmpty())
  11624. {
  11625. RemoteThumbnailMasks->Add(CutToChar(DefaultRemoteThumbnailMask, L';', true));
  11626. }
  11627. RemoteThumbnailMasks.reset(ProcessFeatures(RemoteThumbnailMasks.get(), RemoteThumbnailMask));
  11628. RemoteThumbnailMask = EmptyStr;
  11629. for (int Index = 0; Index < RemoteThumbnailMasks->Count; Index++)
  11630. {
  11631. AddToList(RemoteThumbnailMask, RemoteThumbnailMasks->Strings[Index], L";");
  11632. }
  11633. }
  11634. FRemoteThumbnailMask.Masks = RemoteThumbnailMask;
  11635. }
  11636. //---------------------------------------------------------------------------
  11637. void __fastcall TCustomScpExplorerForm::RemoteDirViewStartLoading(TObject *)
  11638. {
  11639. Terminal->StartLoadingDirectory();
  11640. }
  11641. //---------------------------------------------------------------------------
  11642. void __fastcall TCustomScpExplorerForm::RemoteDirViewStartReading(TObject *)
  11643. {
  11644. Terminal->DisableThumbnails();
  11645. }
  11646. //---------------------------------------------------------------------------
  11647. void TCustomScpExplorerForm::ChangeDirViewStyle(TOperationSide Side, TDirViewStyle DirViewStyle)
  11648. {
  11649. TCustomDirView * ADirView = DirView(Side);
  11650. if (ADirView->DirViewStyle != DirViewStyle)
  11651. {
  11652. if (DirViewStyle == dvsThumbnail)
  11653. {
  11654. if (dynamic_cast<TUnixDirView *>(ADirView) != NULL)
  11655. {
  11656. Configuration->Usage->Inc(L"ThumbnailsRemote");
  11657. }
  11658. else
  11659. {
  11660. Configuration->Usage->Inc(L"ThumbnailsLocal");
  11661. }
  11662. }
  11663. ADirView->DirViewStyle = DirViewStyle;
  11664. UpdateControls();
  11665. }
  11666. }