CustomScpExplorer.cpp 384 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. AddStartupSequence(L"F");
  146. FInvalid = false;
  147. FCurrentSide = osRemote;
  148. FEverShown = false;
  149. FDocks = new TList();
  150. InitControls();
  151. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  152. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  153. NonVisualDataModule->ScpExplorer = this;
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. FAutoOperation = false;
  158. FOnFileOperationFinished = NULL;
  159. FPrimaryOperation = foNone;
  160. FForceExecution = false;
  161. FIgnoreNextDialogChar = 0;
  162. FErrorList = NULL;
  163. FSynchronizeProgressForm = NULL;
  164. FProgressForm = NULL;
  165. FRefreshLocalDirectory = false;
  166. FRefreshRemoteDirectory = false;
  167. FDDExtMapFile = NULL;
  168. // CreateMutexW keeps failing with ERROR_NOACCESS
  169. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  170. DebugAssert(FDDExtMutex != NULL);
  171. FDDTargetControl = NULL;
  172. FDelayedDeletionTimer = NULL;
  173. FDelayedDeletionList = new TStringList();
  174. FDDFileList = NULL;
  175. FPendingTempSpaceWarn = false;
  176. FCapturedLog = NULL;
  177. FDragDropOperation = false;
  178. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  179. FAllowTransferPresetAutoSelect = true;
  180. FSessionChanging = false;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FDeletedFiles = NULL;
  194. FMoveToQueue = false;
  195. StandaloneOperation = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FOnSynchronizeAbort = NULL;
  198. FSynchronizeTerminal = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FUserActionTimer = new TTimer(this);
  222. FUserActionTimer->Enabled = false;
  223. FUserActionTimer->Interval = 10;
  224. FUserActionTimer->OnTimer = UserActionTimer;
  225. FNotes = new TStringList();
  226. FNoteTimer = new TTimer(this);
  227. FNoteTimer->Enabled = false;
  228. FNoteTimer->OnTimer = NoteTimer;
  229. FOnNoteClick = NULL;
  230. FOle32Library = LoadLibrary(L"Ole32.dll");
  231. FDragMoveCursor = FOle32Library != NULL ?
  232. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  233. UseSystemSettings(this);
  234. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  235. static_cast<TObject*>(GetComponent(fcTransferList)));
  236. DebugAssert(TransferList != NULL);
  237. FTransferListHoverIndex = -1;
  238. TransferList->OnChange = TransferListChange;
  239. TransferList->OnDrawItem = TransferListDrawItem;
  240. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  241. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  242. UseDesktopFont(SessionsPageControl);
  243. UpdateSessionsPageControlHeight();
  244. UseDesktopFont(RemoteDirView);
  245. UseDesktopFont(RemoteDriveView);
  246. UseDesktopFont(QueueView3);
  247. UseDesktopFont(QueueFileList);
  248. UseDesktopFont(QueueLabel);
  249. UseDesktopFont(RemoteStatusBar);
  250. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  251. QueueSplitter->ShowHint = true;
  252. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  253. QueueFileListSplitter->ShowHint = true;
  254. RemotePanelSplitter->ShowHint = true;
  255. UpdateImages();
  256. FCustomCommandMenu = CreateTBXPopupMenu(this);
  257. FCustomCommandLocalFileList = NULL;
  258. FCustomCommandRemoteFileList = NULL;
  259. FSessionColors = new TPngImageList(this);
  260. FSessionColors->ColorDepth = cd32Bit;
  261. AddFixedSessionImages();
  262. SessionsPageControl->Images = FSessionColors;
  263. UpdateQueueLabel();
  264. CreateHiddenWindow();
  265. StartUpdates();
  266. }
  267. //---------------------------------------------------------------------------
  268. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  269. {
  270. FInvalid = true;
  271. // this has to be one of the very first things to do
  272. StopUpdateThread();
  273. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  274. TTerminalManager::Instance()->ScpExplorer = NULL;
  275. delete FCustomCommandLocalFileList;
  276. delete FCustomCommandRemoteFileList;
  277. delete FCustomCommandMenu;
  278. delete FTrayIcon;
  279. FTrayIcon = NULL;
  280. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  281. delete FEditorManager;
  282. ForceCloseLocalEditors();
  283. delete FLocalEditors;
  284. HideFileFindDialog();
  285. if (FDelayedDeletionTimer)
  286. {
  287. DoDelayedDeletion(NULL);
  288. SAFE_DESTROY(FDelayedDeletionTimer);
  289. }
  290. SAFE_DESTROY(FDelayedDeletionList);
  291. // sometimes we do not get DDEnd so the list is not released
  292. SAFE_DESTROY(FDDFileList);
  293. DebugAssert(FSynchronizeController == NULL);
  294. CloseHandle(FDDExtMutex);
  295. FDDExtMutex = NULL;
  296. FreeLibrary(FOle32Library);
  297. FOle32Library = NULL;
  298. FDragMoveCursor = NULL;
  299. DebugAssert(!FErrorList);
  300. if (FEverShown)
  301. {
  302. // when window is never shown (like when running command-line operation),
  303. // particularly window site is not restored correctly (BoundsRect value set
  304. // in RestoreForm gets lost during handle allocation), so we do not want
  305. // it to be stored
  306. StoreParams();
  307. }
  308. ManagedSession = NULL;
  309. Queue = NULL;
  310. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  311. NonVisualDataModule->ScpExplorer = NULL;
  312. delete FQueueController;
  313. FQueueController = NULL;
  314. delete FQueueStatusSection;
  315. FQueueStatusSection = NULL;
  316. delete FQueueStatus;
  317. FQueueStatus = NULL;
  318. delete FUserActionTimer;
  319. FUserActionTimer = NULL;
  320. delete FNoteTimer;
  321. delete FNotes;
  322. delete FNoteData;
  323. SAFE_DESTROY(FDocks);
  324. SessionsPageControl->Images = NULL;
  325. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  326. SAFE_DESTROY(FSessionColors);
  327. SAFE_DESTROY(FSessionsDragDropFilesEx);
  328. SAFE_DESTROY(FHistoryMenu[0][0]);
  329. SAFE_DESTROY(FHistoryMenu[0][1]);
  330. SAFE_DESTROY(FHistoryMenu[1][0]);
  331. SAFE_DESTROY(FHistoryMenu[1][1]);
  332. if (FHiddenWindow != NULL)
  333. {
  334. DestroyWindow(FHiddenWindow);
  335. FHiddenWindow = NULL;
  336. }
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  340. {
  341. std::unique_ptr<TSessionData> Data;
  342. if (!Session.IsEmpty())
  343. {
  344. bool DefaultsOnly;
  345. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  346. }
  347. TTerminalManager * Manager = TTerminalManager::Instance();
  348. for (int Index = 0; Index < Manager->Count; Index++)
  349. {
  350. TTerminal * Terminal = Manager->Sessions[Index];
  351. if (Session.IsEmpty() ||
  352. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  353. {
  354. if (Path.IsEmpty())
  355. {
  356. Terminal->ClearCaches();
  357. }
  358. else
  359. {
  360. Terminal->DirectoryModified(Path, true);
  361. }
  362. }
  363. }
  364. // We should flag a pending refresh for the background terminals or busy foreground terminals
  365. if (HasActiveTerminal() &&
  366. CanCommandLineFromAnotherInstance() &&
  367. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  368. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  369. {
  370. Terminal->ReloadDirectory();
  371. }
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  375. {
  376. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  377. size_t MessageSize = sizeof(TCopyDataMessage);
  378. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  379. if (Result)
  380. {
  381. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  382. Result = (Message.Version == TCopyDataMessage::Version1);
  383. if (Result)
  384. {
  385. switch (Message.Command)
  386. {
  387. case TCopyDataMessage::CommandCanCommandLine:
  388. Result = CanCommandLineFromAnotherInstance();
  389. break;
  390. case TCopyDataMessage::CommandCommandLine:
  391. {
  392. UnicodeString CommandLine(Message.CommandLine);
  393. Result = CommandLineFromAnotherInstance(CommandLine);
  394. }
  395. break;
  396. case TCopyDataMessage::RefreshPanel:
  397. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  398. break;
  399. case TCopyDataMessage::MainWindowCheck:
  400. Result = true;
  401. break;
  402. default:
  403. Result = false;
  404. break;
  405. }
  406. }
  407. }
  408. Message.Result = Result ? 1 : 0;
  409. }
  410. //---------------------------------------------------------------------------
  411. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  412. {
  413. WNDCLASS WindowClass = {0};
  414. WindowClass.lpfnWndProc = DefWindowProc;
  415. WindowClass.hInstance = HInstance;
  416. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  417. FHiddenWindow = NULL;
  418. if (RegisterClass(&WindowClass))
  419. {
  420. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  421. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  422. }
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanConsole()
  426. {
  427. return
  428. HasActiveTerminal() &&
  429. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  430. }
  431. //---------------------------------------------------------------------------
  432. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  433. {
  434. bool Result = Visible && !NonVisualDataModule->Busy;
  435. return Result;
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  439. const UnicodeString & CommandLine)
  440. {
  441. TProgramParams Params(CommandLine);
  442. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  443. if (Result)
  444. {
  445. NonVisualDataModule->StartBusy();
  446. try
  447. {
  448. // this action is initiated from another process,
  449. // so it's likely that our window is not visible,
  450. // and user won't see what is going on
  451. Application->BringToFront();
  452. // reload sessions as we may be asked to open a session
  453. // just stored by another instance
  454. StoredSessions->Reload();
  455. UnicodeString SessionName = Params.Param[1];
  456. std::unique_ptr<TObjectList> DataList(new TObjectList());
  457. try
  458. {
  459. UnicodeString DownloadFile; // unused
  460. int Flags = GetCommandLineParseUrlFlags(&Params);
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  484. {
  485. TManagedTerminal * Result;
  486. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  487. {
  488. Result = FManagedSession;
  489. }
  490. else
  491. {
  492. Result = NULL;
  493. }
  494. return Result;
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  498. {
  499. if (FManagedSession != value)
  500. {
  501. SessionChanging();
  502. DoSetManagedSession(value, false);
  503. }
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  507. {
  508. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  509. FManagedSession = value;
  510. {
  511. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  512. FAllowTransferPresetAutoSelect = false;
  513. TAutoFlag SessionChangingFlag(FSessionChanging);
  514. SessionChanged(Replace);
  515. }
  516. if (Terminal != NULL)
  517. {
  518. TransferPresetAutoSelect();
  519. }
  520. // Update app and tab titles, prevented above by FSessionChanging.
  521. UpdateControls();
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  525. {
  526. DoSetManagedSession(value, true);
  527. }
  528. //---------------------------------------------------------------------------
  529. void __fastcall TCustomScpExplorerForm::SessionChanging()
  530. {
  531. if (ManagedSession != NULL)
  532. {
  533. UpdateSession(ManagedSession);
  534. }
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  538. {
  539. if (Terminal != NULL)
  540. {
  541. UpdateSessionColor((TColor)Terminal->StateData->Color);
  542. }
  543. if (!TTerminalManager::Instance()->Updating)
  544. {
  545. SessionListChanged();
  546. }
  547. if (Terminal != NULL)
  548. {
  549. TObject * State = NULL;
  550. if (WinConfiguration->PreservePanelState)
  551. {
  552. State = Terminal->RemoteExplorerState;
  553. DirView(osRemote)->AnnounceState(State);
  554. }
  555. try
  556. {
  557. if (Replaced)
  558. {
  559. RemoteDirView->ReplaceTerminal(Terminal);
  560. }
  561. else
  562. {
  563. RemoteDirView->Terminal = Terminal;
  564. }
  565. if (Terminal->Active)
  566. {
  567. Terminal->RefreshDirectory();
  568. }
  569. if (State != NULL)
  570. {
  571. DirView(osRemote)->RestoreState(State);
  572. }
  573. }
  574. __finally
  575. {
  576. DirView(osRemote)->AnnounceState(NULL);
  577. }
  578. }
  579. else
  580. {
  581. RemoteDirView->Terminal = NULL;
  582. }
  583. InitStatusBar();
  584. UpdateTransferList();
  585. // Update panels Enable state before refreshing the labels
  586. UpdateControls();
  587. }
  588. //---------------------------------------------------------------------------
  589. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  590. {
  591. if (Queue != value)
  592. {
  593. if (FQueue != NULL)
  594. {
  595. FQueue->OnListUpdate = NULL;
  596. FQueue->OnQueueItemUpdate = NULL;
  597. }
  598. FQueue = value;
  599. if (FQueue != NULL)
  600. {
  601. DebugAssert(FQueue->OnListUpdate == NULL);
  602. FQueue->OnListUpdate = QueueListUpdate;
  603. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  604. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  605. }
  606. QueueChanged();
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  611. TListItem * Item)
  612. {
  613. if (FQueueActedItem == Item)
  614. {
  615. FQueueActedItem = NULL;
  616. if ((QueueView3->PopupMenu != NULL) &&
  617. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  618. {
  619. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  620. ReleaseCapture();
  621. }
  622. }
  623. if (Item->Data == FPendingQueueActionItem)
  624. {
  625. FPendingQueueActionItem = NULL;
  626. }
  627. }
  628. //---------------------------------------------------------------------------
  629. bool TCustomScpExplorerForm::IsAnythingQueued()
  630. {
  631. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  632. }
  633. //---------------------------------------------------------------------------
  634. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  635. {
  636. {
  637. TGuard Guard(FQueueStatusSection);
  638. FQueueStatusInvalidated = false;
  639. if (FQueue != NULL)
  640. {
  641. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  642. }
  643. }
  644. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  645. {
  646. FMaxQueueLength = FQueueStatus->Count;
  647. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  648. }
  649. {
  650. TAutoFlag Flag(FQueueStatusUpdating);
  651. FQueueController->UpdateQueueStatus(FQueueStatus);
  652. }
  653. UpdateQueueFileList();
  654. SetQueueProgress();
  655. UpdateQueueView();
  656. bool IsEmpty = !IsAnythingQueued();
  657. if (IsEmpty && (Terminal != NULL))
  658. {
  659. TOnceDoneOperation OnceDoneOperation =
  660. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  661. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  662. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  663. {
  664. FQueue->Enabled = false;
  665. }
  666. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  667. {
  668. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  669. }
  670. }
  671. }
  672. //---------------------------------------------------------------------------
  673. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  674. {
  675. UnicodeString Result;
  676. if (FQueueStatus != NULL)
  677. {
  678. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  679. {
  680. TFileOperationProgressType * ProgressData =
  681. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  682. if ((ProgressData != NULL) && ProgressData->InProgress)
  683. {
  684. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  685. }
  686. }
  687. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  688. {
  689. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  690. }
  691. }
  692. return Result;
  693. }
  694. //---------------------------------------------------------------------------
  695. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  696. {
  697. ComponentVisible[fcQueueView] =
  698. !IsLocalBrowserMode() &&
  699. ((WinConfiguration->QueueView.Show == qvShow) ||
  700. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  701. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  702. }
  703. //---------------------------------------------------------------------------
  704. void __fastcall TCustomScpExplorerForm::QueueChanged()
  705. {
  706. if (FQueueStatus != NULL)
  707. {
  708. // UpdateFileList implementation relies on the status being recreated when session changes
  709. delete FQueueStatus;
  710. FQueueStatus = NULL;
  711. }
  712. UpdateQueueStatus(true);
  713. }
  714. //---------------------------------------------------------------------------
  715. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  716. {
  717. if (FQueue == Queue)
  718. {
  719. FQueueStatusInvalidated = true;
  720. }
  721. }
  722. //---------------------------------------------------------------------------
  723. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  724. TQueueItem * Item)
  725. {
  726. if (FQueue == Queue)
  727. {
  728. TGuard Guard(FQueueStatusSection);
  729. // this may be running in parallel with QueueChanged
  730. if (FQueueStatus != NULL)
  731. {
  732. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  733. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  734. {
  735. FRefreshLocalDirectory = (QueueItem == NULL) ||
  736. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  737. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  738. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  739. }
  740. if (QueueItem != NULL)
  741. {
  742. QueueItem->UserData = (void*)true;
  743. FQueueItemInvalidated = true;
  744. }
  745. }
  746. }
  747. }
  748. //---------------------------------------------------------------------------
  749. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  750. {
  751. // during standalone editing, we have no way to see/control queue,
  752. // so we have to always popup prompts automatically
  753. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  754. }
  755. //---------------------------------------------------------------------------
  756. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  757. {
  758. if (FQueueStatus != NULL)
  759. {
  760. bool QueueAutoPopup = IsQueueAutoPopup();
  761. bool NeedRefresh = FQueueController->NeedRefresh();
  762. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  763. FQueueItemInvalidated = false;
  764. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  765. bool Updated = false;
  766. TQueueItemProxy * QueueItem;
  767. bool WasUserAction;
  768. for (int Index = 0; Index < Limit; Index++)
  769. {
  770. bool Update = false;
  771. QueueItem = FQueueStatus->Items[Index];
  772. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  773. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  774. (FPendingQueueActionItem == NULL))
  775. {
  776. FPendingQueueActionItem = QueueItem;
  777. FUserActionTimer->Enabled = true;
  778. }
  779. if ((bool)QueueItem->UserData)
  780. {
  781. QueueItem->UserData = (void*)false;
  782. QueueItem->Update();
  783. Updated = true;
  784. Update = true;
  785. }
  786. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  787. {
  788. Update = true;
  789. }
  790. if (Update)
  791. {
  792. FQueueController->RefreshQueueItem(QueueItem);
  793. }
  794. }
  795. if (Updated)
  796. {
  797. NonVisualDataModule->UpdateNonVisibleActions();
  798. SetQueueProgress();
  799. UpdateQueueFileList();
  800. }
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  805. {
  806. if (FTaskbarList != NULL)
  807. {
  808. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  809. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  810. }
  811. }
  812. //---------------------------------------------------------------------------
  813. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  814. {
  815. if (Progress >= 0)
  816. {
  817. if (FTaskbarList != NULL)
  818. {
  819. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  820. }
  821. }
  822. else
  823. {
  824. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  825. }
  826. }
  827. //---------------------------------------------------------------------------
  828. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  829. {
  830. int OverallProgress;
  831. // FProgressForm is null when this is called from SetQueueProgress
  832. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  833. {
  834. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  835. }
  836. else if (!ProgressData->IsIndeterminate())
  837. {
  838. OverallProgress = ProgressData->OverallProgress();
  839. }
  840. else
  841. {
  842. OverallProgress = -1;
  843. }
  844. SetTaskbarListProgressValue(OverallProgress);
  845. }
  846. //---------------------------------------------------------------------------
  847. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  848. {
  849. TTerminalManager::Instance()->QueueStatusUpdated();
  850. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  851. {
  852. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  853. {
  854. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  855. {
  856. TFileOperationProgressType * ProgressData = NULL;
  857. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  858. {
  859. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  860. }
  861. if ((ProgressData != NULL) &&
  862. ProgressData->InProgress)
  863. {
  864. SetTaskbarListProgressValue(ProgressData);
  865. }
  866. else
  867. {
  868. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  869. }
  870. }
  871. else
  872. {
  873. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  874. }
  875. }
  876. else
  877. {
  878. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  879. }
  880. }
  881. UpdateQueueLabel();
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  885. {
  886. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  887. if (FQueueStatus != NULL)
  888. {
  889. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  890. if (ActiveAndPendingPrimaryCount > 0)
  891. {
  892. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  893. }
  894. }
  895. QueueLabel->Caption = Caption;
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  899. {
  900. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  901. static_cast<TComponent*>(GetComponent(fcTransferList)));
  902. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  903. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  904. TransferList->Strings->BeginUpdate();
  905. try
  906. {
  907. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  908. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  909. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  910. if (FTransferDropDownHint.IsEmpty())
  911. {
  912. FTransferDropDownHint = TransferDropDown->Hint;
  913. }
  914. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  915. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  916. TransferDropDown->Text = StripHotkey(Name);
  917. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  918. (FTransferDropDownHint, StripHotkey(Name),
  919. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  920. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  921. // update the label, otherwise when it is updated only on the first draw
  922. // of the list, it is drawn "bold" for some reason
  923. FTransferListHoverIndex = TransferList->ItemIndex;
  924. UpdateTransferLabel();
  925. }
  926. __finally
  927. {
  928. TransferList->Strings->EndUpdate();
  929. }
  930. }
  931. //---------------------------------------------------------------------------
  932. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  933. {
  934. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  935. DebugAssert(Toolbar != NULL);
  936. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  937. }
  938. //---------------------------------------------------------------------------
  939. void __fastcall TCustomScpExplorerForm::UpdateActions()
  940. {
  941. TForm::UpdateActions();
  942. if (ComponentVisible[fcCustomCommandsBand])
  943. {
  944. UpdateCustomCommandsToolbar();
  945. }
  946. }
  947. //---------------------------------------------------------------------------
  948. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  949. {
  950. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  951. }
  952. //---------------------------------------------------------------------------
  953. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  954. {
  955. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  956. {
  957. DirView->RowSelect = WinConfiguration->FullRowSelect;
  958. // Without this, the panel turns gray and unused part of header turns black
  959. DirView->Perform(CM_COLORCHANGED, 0, 0);
  960. }
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  964. {
  965. Color = GetBtnFaceColor();
  966. DebugAssert(Configuration && RemoteDirView);
  967. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  968. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  969. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  970. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  971. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  972. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  973. UpdateRowSelect(RemoteDirView);
  974. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  975. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  976. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  977. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  978. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  979. UpdateSessionsPageControlHeight();
  980. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  981. UpdateToolbarDisplayMode();
  982. UpdateTransferList();
  983. if (Terminal != NULL)
  984. {
  985. TransferPresetAutoSelect();
  986. }
  987. UpdateQueueView();
  988. UpdateControls();
  989. // this can be called even before constuctor finishes.
  990. if (FEditorManager != NULL)
  991. {
  992. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  993. }
  994. // this can be called even before constuctor finishes.
  995. if (FLocalEditors != NULL)
  996. {
  997. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  998. {
  999. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1000. }
  1001. }
  1002. if (ComponentVisible[fcCustomCommandsBand])
  1003. {
  1004. UpdateCustomCommandsToolbar();
  1005. }
  1006. // show only when keeping queue items forever,
  1007. // otherwise, it's enough to have it in the context menu
  1008. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1009. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1010. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1011. {
  1012. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1013. FFileColorsCurrent = WinConfiguration->FileColors;
  1014. FileColorsChanged();
  1015. }
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1019. {
  1020. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1021. DirView->Invalidate();
  1022. }
  1023. //---------------------------------------------------------------------------
  1024. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1025. {
  1026. DoFileColorsChanged(RemoteDirView);
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1030. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1031. void * /*Arg*/)
  1032. {
  1033. if (Token != NULL)
  1034. {
  1035. TForm * Editor = dynamic_cast<TForm*>(Token);
  1036. DebugAssert(Editor != NULL);
  1037. ReconfigureEditorForm(Editor);
  1038. }
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1042. {
  1043. WinConfiguration->DDTransferConfirmation = asOn;
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1047. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1048. {
  1049. // noop
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1053. {
  1054. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1058. {
  1059. if (DoNotShowAgain)
  1060. {
  1061. if (DragDrop)
  1062. {
  1063. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1064. {
  1065. PopupTrayBalloon(
  1066. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1067. }
  1068. WinConfiguration->DDTransferConfirmation = asOff;
  1069. }
  1070. else
  1071. {
  1072. WinConfiguration->ConfirmTransferring = false;
  1073. }
  1074. }
  1075. else
  1076. {
  1077. // User explicitly unchecked "do not show again",
  1078. // so show him the dialog the next time
  1079. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1080. {
  1081. WinConfiguration->DDTransferConfirmation = asOn;
  1082. }
  1083. }
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1087. TTransferDirection Direction, TTransferType Type, bool Temp,
  1088. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1089. bool Confirm, bool DragDrop, int Options)
  1090. {
  1091. bool Result = true;
  1092. DebugAssert(HasActiveTerminal());
  1093. // these parameters are known in advance
  1094. int Params =
  1095. FLAGMASK(Type == ttMove, cpDelete);
  1096. bool ToTemp = (Temp && (Direction == tdToLocal));
  1097. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1098. {
  1099. bool DisableNewerOnly =
  1100. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1101. ToTemp;
  1102. Options |=
  1103. FLAGMASK(ToTemp, coTemp) |
  1104. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1105. coDoNotShowAgain;
  1106. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1107. int CopyParamAttrs = (Direction == tdToRemote ?
  1108. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1109. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1110. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1111. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1112. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1113. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1114. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1115. if (Result)
  1116. {
  1117. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1118. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1119. }
  1120. }
  1121. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1122. {
  1123. Configuration->Usage->Inc(L"TransfersOnBackground");
  1124. // these parameter are known only after transfer dialog
  1125. Params |=
  1126. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1127. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1128. Result = false;
  1129. ClearTransferSourceSelection(Direction);
  1130. }
  1131. return Result;
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1135. {
  1136. if (FOnFileOperationFinished != NULL)
  1137. {
  1138. FOnFileOperationFinished(Side, UnicodeString(), true);
  1139. }
  1140. else
  1141. {
  1142. if (HasDirView[Side])
  1143. {
  1144. DirView(Side)->SelectAll(smNone);
  1145. }
  1146. }
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1150. {
  1151. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1152. ClearOperationSelection(Side);
  1153. }
  1154. //---------------------------------------------------------------------------
  1155. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1156. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1157. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1158. int Params)
  1159. {
  1160. DebugAssert(Queue != NULL);
  1161. bool SingleFile = false;
  1162. if (FileList->Count == 1)
  1163. {
  1164. if (Direction == tdToRemote)
  1165. {
  1166. UnicodeString FileName = FileList->Strings[0];
  1167. SingleFile = FileExists(ApiPath(FileName));
  1168. }
  1169. else
  1170. {
  1171. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1172. SingleFile = !File->IsDirectory;
  1173. }
  1174. }
  1175. TQueueItem * QueueItem;
  1176. if (Direction == tdToRemote)
  1177. {
  1178. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1179. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1180. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1181. }
  1182. else
  1183. {
  1184. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1185. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1186. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1187. }
  1188. AddQueueItem(Queue, QueueItem, Terminal);
  1189. }
  1190. //---------------------------------------------------------------------------
  1191. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1192. {
  1193. if (Queue->IsEmpty)
  1194. {
  1195. Terminal->QueueOperationStart = Now();
  1196. }
  1197. Queue->AddItem(QueueItem);
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1201. {
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. void __fastcall TCustomScpExplorerForm::InitControls()
  1205. {
  1206. DebugAssert(FDocks != NULL);
  1207. for (int Index = 0; Index < ComponentCount; Index++)
  1208. {
  1209. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1210. if ((Dock != NULL) && (Dock->Tag == 0))
  1211. {
  1212. FDocks->Add(Dock);
  1213. }
  1214. }
  1215. CollectItemsWithTextDisplayMode(this);
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1219. {
  1220. ConfigurationChanged();
  1221. QueuePanel->Height =
  1222. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1223. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1224. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1225. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1226. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1227. QueueFileList->Height =
  1228. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1229. if (QueueFileList->Visible)
  1230. {
  1231. AdjustQueueLayout();
  1232. }
  1233. UpdateCustomCommandsToolbar();
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. void __fastcall TCustomScpExplorerForm::StoreParams()
  1237. {
  1238. int APixelsPerInch = GetControlPixelsPerInch(this);
  1239. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1240. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1241. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1242. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1243. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1244. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1245. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1246. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1250. {
  1251. if (!FFormRestored)
  1252. {
  1253. FFormRestored = true;
  1254. RestoreFormParams();
  1255. }
  1256. TForm::CreateParams(Params);
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1260. {
  1261. DebugAssert(FDocks != NULL);
  1262. for (int Index = 0; Index < FDocks->Count; Index++)
  1263. {
  1264. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1265. }
  1266. }
  1267. //---------------------------------------------------------------------------
  1268. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1269. {
  1270. SetDockAllowDrag(true);
  1271. while (!ButtonsStr.IsEmpty())
  1272. {
  1273. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1274. UnicodeString Name = CutToChar(S, L'=', true);
  1275. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1276. {
  1277. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1278. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1279. {
  1280. while (!S.IsEmpty())
  1281. {
  1282. UnicodeString S2 = CutToChar(S, L',', true);
  1283. UnicodeString Name = CutToChar(S2, L':', true);
  1284. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1285. {
  1286. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1287. if (SameText(GetToolbarItemName(Item), Name))
  1288. {
  1289. // forward compatibility
  1290. UnicodeString S3 = CutToChar(S2, L':', true);
  1291. bool Visible = StrToIntDef(S3, 0);
  1292. Item->Visible = Visible;
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. }
  1299. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1300. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1301. }
  1302. //---------------------------------------------------------------------------
  1303. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1304. {
  1305. UnicodeString Result;
  1306. SetDockAllowDrag(true);
  1307. Result = ::GetToolbarsLayoutStr(this);
  1308. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1309. return Result;
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1313. {
  1314. UnicodeString Result;
  1315. if (Item->Action != NULL)
  1316. {
  1317. Result = Item->Action->Name;
  1318. Result = RemoveSuffix(Result, L"Action", true);
  1319. }
  1320. else
  1321. {
  1322. Result = Item->Name;
  1323. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1324. Result = RemoveSuffix(Result, L"Item", true);
  1325. }
  1326. return Result;
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1330. {
  1331. UnicodeString Result;
  1332. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1333. {
  1334. UnicodeString ToolbarStr;
  1335. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1336. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1337. {
  1338. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1339. {
  1340. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1341. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1342. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1343. // and save the changes here only.
  1344. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1345. {
  1346. UnicodeString Name = GetToolbarItemName(Item);
  1347. DebugAssert(Name.Pos(L"TBX") == 0);
  1348. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1349. }
  1350. }
  1351. }
  1352. if (!ToolbarStr.IsEmpty())
  1353. {
  1354. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1355. }
  1356. }
  1357. return Result;
  1358. }
  1359. //---------------------------------------------------------------------------
  1360. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1361. {
  1362. DebugAssert(FProgressForm == NULL);
  1363. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1364. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1365. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1366. FProgressForm->DeleteLocalToRecycleBin =
  1367. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1368. FProgressForm->DeleteRemoteToRecycleBin =
  1369. HasActiveTerminal() &&
  1370. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1371. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1372. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1373. LockWindow();
  1374. // Actually, do not know what hides the progress once the operation finishes
  1375. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1376. SetTaskbarListProgressState(TBPF_NORMAL);
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1380. {
  1381. UnlockWindow();
  1382. SAFE_DESTROY(FProgressForm);
  1383. SetQueueProgress();
  1384. }
  1385. //---------------------------------------------------------------------------
  1386. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1387. TFileOperationProgressType & ProgressData)
  1388. {
  1389. // Download to temporary local directory
  1390. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1391. {
  1392. bool Continue = true;
  1393. FPendingTempSpaceWarn = false;
  1394. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1395. if (!Continue)
  1396. {
  1397. Abort();
  1398. }
  1399. }
  1400. // operation is being executed and we still didn't show up progress form
  1401. if (ProgressData.InProgress && !FProgressForm)
  1402. {
  1403. CreateProgressForm(NULL);
  1404. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1405. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1406. }
  1407. // operation is finished (or terminated), so we hide progress form
  1408. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1409. {
  1410. DestroyProgressForm();
  1411. if (ProgressData.Operation == foCalculateSize)
  1412. {
  1413. // When calculating size before transfer, the abort caused by
  1414. // cancel flag set due to "MoveToQueue" below,
  1415. // is not propagated back to us in ExecuteFileOperation,
  1416. // as it is silently swallowed.
  1417. // So we explicitly (re)throw it here.
  1418. if (FMoveToQueue)
  1419. {
  1420. Abort();
  1421. }
  1422. }
  1423. else
  1424. {
  1425. if ((ProgressData.Cancel == csContinue) &&
  1426. !FAutoOperation)
  1427. {
  1428. OperationComplete(ProgressData.StartTime);
  1429. }
  1430. }
  1431. }
  1432. if (FProgressForm &&
  1433. // Can happen only during synchronization, when ending one of the operations
  1434. ProgressData.InProgress)
  1435. {
  1436. FProgressForm->SetProgressData(ProgressData);
  1437. DebugAssert(ProgressData.InProgress);
  1438. SetTaskbarListProgressValue(&ProgressData);
  1439. if (FProgressForm->Cancel > csContinue)
  1440. {
  1441. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1442. if (FProgressForm->Cancel == csCancelFile)
  1443. {
  1444. FProgressForm->ClearCancel();
  1445. }
  1446. // cancel cancels even the move
  1447. FMoveToQueue = false;
  1448. }
  1449. else if (FProgressForm->MoveToQueue)
  1450. {
  1451. FMoveToQueue = true;
  1452. ProgressData.SetCancelAtLeast(csCancel);
  1453. }
  1454. if ((FTransferResumeList != NULL) &&
  1455. ProgressData.InProgress &&
  1456. ProgressData.IsTransfer() &&
  1457. !ProgressData.FullFileName.IsEmpty())
  1458. {
  1459. if ((FTransferResumeList->Count == 0) ||
  1460. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1461. {
  1462. // note that we do not recognize directories from files here
  1463. FTransferResumeList->Add(ProgressData.FullFileName);
  1464. }
  1465. }
  1466. }
  1467. }
  1468. //---------------------------------------------------------------------------
  1469. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1470. const TDateTime & StartTime)
  1471. {
  1472. if (GUIConfiguration->BeepOnFinish &&
  1473. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1474. {
  1475. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1476. DWORD Sound;
  1477. if (!ExtractFileExt(BeepSound).IsEmpty())
  1478. {
  1479. Sound = SND_FILENAME;
  1480. }
  1481. else
  1482. {
  1483. Sound = SND_ALIAS;
  1484. }
  1485. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1486. }
  1487. }
  1488. //---------------------------------------------------------------------------
  1489. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1490. TFileOperationProgressType & ProgressData)
  1491. {
  1492. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1493. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1494. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1495. FileOperationProgress(ProgressData);
  1496. }
  1497. //---------------------------------------------------------------------------
  1498. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1499. {
  1500. UnicodeString Result;
  1501. if (FProgressForm != NULL)
  1502. {
  1503. Result = FProgressForm->ProgressStr();
  1504. }
  1505. return Result;
  1506. }
  1507. //---------------------------------------------------------------------------
  1508. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1509. bool DragDrop)
  1510. {
  1511. return (!DragDrop && (DropSourceControl == NULL)) ||
  1512. (DropSourceControl == DirView(osOther));
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1516. {
  1517. TCustomDirView * ADirView = DirView(Side);
  1518. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1519. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1520. // this can happen when local drive is unplugged in the middle of the operation
  1521. if (Item != NULL)
  1522. {
  1523. if (Unselect)
  1524. {
  1525. Item->Selected = false;
  1526. }
  1527. ADirView->MakeProgressVisible(Item);
  1528. }
  1529. return Item;
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1533. TFileOperation Operation, TOperationSide Side,
  1534. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1535. TOnceDoneOperation & OnceDoneOperation)
  1536. {
  1537. if (!FAutoOperation)
  1538. {
  1539. // no selection on "/upload", form serves only as event handler
  1540. // (it is not displayed)
  1541. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1542. ((Operation == FPrimaryOperation) ||
  1543. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1544. {
  1545. TOperationSide DViewSide;
  1546. if ((FProgressSide != osCurrent) &&
  1547. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1548. {
  1549. // assume the operation is over the focused panel
  1550. DViewSide = FProgressSide;
  1551. }
  1552. else
  1553. {
  1554. DViewSide = Side;
  1555. }
  1556. if (FOnFileOperationFinished != NULL)
  1557. {
  1558. FOnFileOperationFinished(DViewSide, FileName, Success);
  1559. }
  1560. else
  1561. {
  1562. VisualiseOperationFinished(DViewSide, FileName, Success);
  1563. }
  1564. }
  1565. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1566. {
  1567. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1568. {
  1569. if (Side == osLocal)
  1570. {
  1571. Configuration->Usage->Inc(L"UploadedFiles");
  1572. }
  1573. else
  1574. {
  1575. Configuration->Usage->Inc(L"DownloadedFiles");
  1576. }
  1577. }
  1578. }
  1579. }
  1580. if (Success && (FSynchronizeController != NULL))
  1581. {
  1582. DebugAssert(!IsSideLocalBrowser(osRemote));
  1583. if (Operation == foCopy)
  1584. {
  1585. DebugAssert(Side == osLocal);
  1586. FSynchronizeController->LogOperation(soUpload, FileName);
  1587. }
  1588. else if (Operation == foDelete)
  1589. {
  1590. DebugAssert(Side == osRemote);
  1591. FSynchronizeController->LogOperation(soDelete, FileName);
  1592. }
  1593. }
  1594. if (FProgressForm)
  1595. {
  1596. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1597. }
  1598. }
  1599. //---------------------------------------------------------------------------
  1600. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1601. TFileOperation Operation, TOperationSide Side,
  1602. bool Temp, const UnicodeString & FileName, Boolean Success,
  1603. TOnceDoneOperation & OnceDoneOperation)
  1604. {
  1605. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1606. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1607. OnceDoneOperation);
  1608. }
  1609. //---------------------------------------------------------------------------
  1610. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1611. {
  1612. return false;
  1613. }
  1614. //---------------------------------------------------------------------------
  1615. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1616. {
  1617. return false;
  1618. }
  1619. //---------------------------------------------------------------------------
  1620. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1621. {
  1622. return false;
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1626. {
  1627. return NULL;
  1628. }
  1629. //---------------------------------------------------------------------------
  1630. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1631. {
  1632. DebugAssert(GetSide(Side) == osRemote);
  1633. DebugUsedParam(Side);
  1634. return RemoteDirView;
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1638. {
  1639. DebugAssert(GetSide(Side) == osRemote);
  1640. DebugUsedParam(Side);
  1641. return RemoteDriveView;
  1642. }
  1643. //---------------------------------------------------------------------------
  1644. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1645. {
  1646. DebugAssert(GetSide(Side) == osRemote);
  1647. DebugUsedParam(Side);
  1648. return HasActiveTerminal();
  1649. }
  1650. //---------------------------------------------------------------------------
  1651. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1652. TOperationSide Side, int FilesOnly)
  1653. {
  1654. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1655. }
  1656. //---------------------------------------------------------------------------
  1657. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1658. TOperationSide Side, int FilesOnly)
  1659. {
  1660. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1661. }
  1662. //---------------------------------------------------------------------------
  1663. struct THistoryItemData
  1664. {
  1665. short int Side;
  1666. short int Index;
  1667. };
  1668. //---------------------------------------------------------------------------
  1669. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1670. {
  1671. DirView(Side)->HistoryGo(Index);
  1672. }
  1673. //---------------------------------------------------------------------------
  1674. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1675. {
  1676. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1677. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1678. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1682. bool Back)
  1683. {
  1684. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1685. {
  1686. TCustomDirView * DView = DirView(Side);
  1687. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1688. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1689. if (ICount > 10)
  1690. {
  1691. ICount = 10;
  1692. }
  1693. Menu->Items->Clear();
  1694. THistoryItemData Data;
  1695. Data.Side = Side;
  1696. for (short int i = 1; i <= ICount; i++)
  1697. {
  1698. TTBCustomItem * Item = new TTBXItem(Menu);
  1699. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1700. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1701. Item->Hint = DView->HistoryPath[Data.Index];
  1702. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1703. Item->Tag = *reinterpret_cast<int*>(&Data);
  1704. Item->OnClick = HistoryItemClick;
  1705. Menu->Items->Add(Item);
  1706. }
  1707. }
  1708. }
  1709. //---------------------------------------------------------------------------
  1710. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1711. TOperationSide Side, bool Back)
  1712. {
  1713. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1714. {
  1715. // workaround
  1716. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1717. // operator new allocates memory in Pascal code, but calls inline
  1718. // contructor in C++, leading in problems, the function does
  1719. // both in Pascal code
  1720. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1721. UpdateHistoryMenu(Side, Back);
  1722. }
  1723. return FHistoryMenu[Side == osLocal][Back];
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1727. TCustomDirView *Sender)
  1728. {
  1729. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1730. UpdateHistoryMenu(Side, true);
  1731. UpdateHistoryMenu(Side, false);
  1732. }
  1733. //---------------------------------------------------------------------------
  1734. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1735. {
  1736. // remote custom commands can be executed only if the server supports shell commands
  1737. // or have secondary shell
  1738. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1739. }
  1740. //---------------------------------------------------------------------------
  1741. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1742. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1743. {
  1744. // -1 = hidden, 0 = disabled, 1 = enabled
  1745. int Result;
  1746. TFileCustomCommand RemoteCustomCommand;
  1747. TLocalCustomCommand LocalCustomCommand;
  1748. TFileCustomCommand * NonInteractiveCustomCommand =
  1749. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1750. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1751. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1752. if (FLAGCLEAR(Command.Params, ccLocal))
  1753. {
  1754. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1755. // custom command that does not operate with files can be executed anytime ...
  1756. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1757. {
  1758. // ... but do not show such command in remote file menu
  1759. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1760. {
  1761. Result = AllowedState;
  1762. }
  1763. else
  1764. {
  1765. Result = -1;
  1766. }
  1767. }
  1768. else
  1769. {
  1770. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1771. {
  1772. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1773. }
  1774. else
  1775. {
  1776. Result = -1;
  1777. }
  1778. }
  1779. }
  1780. else
  1781. {
  1782. // custom command that does not operate with files can be executed anytime
  1783. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1784. {
  1785. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1786. }
  1787. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1788. {
  1789. if ((ListType == ccltAll) || (ListType == ccltFile))
  1790. {
  1791. // special case is "diff"-style command that can be executed over any side,
  1792. // if we have both sides
  1793. Result =
  1794. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1795. // directly (instead of EnableSelectedOperation) to pass
  1796. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1797. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1798. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1799. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1800. }
  1801. else if (ListType == ccltBoth)
  1802. {
  1803. DebugAssert(!IsLocalBrowserMode());
  1804. Result = 1;
  1805. }
  1806. else
  1807. {
  1808. Result = -1;
  1809. }
  1810. }
  1811. else
  1812. {
  1813. if ((ListType == ccltAll) ||
  1814. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1815. {
  1816. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1817. }
  1818. else
  1819. {
  1820. Result = -1;
  1821. }
  1822. }
  1823. if ((Result > 0) &&
  1824. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1825. !HasActiveTerminal())
  1826. {
  1827. Result = 0;
  1828. }
  1829. }
  1830. return Result;
  1831. }
  1832. //---------------------------------------------------------------------------
  1833. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1834. TStrings * FileList, const TCustomCommandType & ACommand,
  1835. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1836. {
  1837. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1838. {
  1839. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1840. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1841. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1842. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1843. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1844. bool Capture =
  1845. FLAGSET(ACommand.Params, ccShowResults) ||
  1846. FLAGSET(ACommand.Params, ccCopyResults) ||
  1847. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1848. TCaptureOutputEvent OutputEvent = NULL;
  1849. DebugAssert(FCapturedLog == NULL);
  1850. if (Capture)
  1851. {
  1852. FCapturedLog = new TStringList();
  1853. OutputEvent = TerminalCaptureLog;
  1854. }
  1855. try
  1856. {
  1857. if (!RemoteCustomCommand.IsFileCommand(Command))
  1858. {
  1859. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1860. OutputEvent);
  1861. }
  1862. else
  1863. {
  1864. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1865. }
  1866. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1867. {
  1868. if (FLAGSET(ACommand.Params, ccCopyResults))
  1869. {
  1870. CopyToClipboard(FCapturedLog);
  1871. }
  1872. if (FLAGSET(ACommand.Params, ccShowResults))
  1873. {
  1874. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1875. }
  1876. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1877. {
  1878. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1879. }
  1880. }
  1881. }
  1882. __finally
  1883. {
  1884. SAFE_DESTROY(FCapturedLog);
  1885. }
  1886. }
  1887. }
  1888. //---------------------------------------------------------------------------
  1889. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1890. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1891. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1892. {
  1893. DebugAssert(!IsLocalBrowserMode());
  1894. TStrings * LocalFileList = NULL;
  1895. TStrings * RemoteFileList = NULL;
  1896. TStrings * RemoteFileListFull = NULL;
  1897. try
  1898. {
  1899. if (LocalFileCommand)
  1900. {
  1901. if (ALocalFileList == NULL)
  1902. {
  1903. DebugAssert(HasDirView[osLocal]);
  1904. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1905. // directly (instead of EnableSelectedOperation) to pass
  1906. // false to FocusedFileOnlyWhenFocused
  1907. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1908. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1909. }
  1910. else
  1911. {
  1912. LocalFileList = ALocalFileList;
  1913. }
  1914. if (FileListCommand)
  1915. {
  1916. if (LocalFileList->Count != 1)
  1917. {
  1918. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1919. }
  1920. }
  1921. else
  1922. {
  1923. if ((LocalFileList->Count != 1) &&
  1924. (FileList->Count != 1) &&
  1925. (LocalFileList->Count != FileList->Count))
  1926. {
  1927. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1928. }
  1929. }
  1930. }
  1931. UnicodeString RootTempDir;
  1932. UnicodeString TempDir;
  1933. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1934. if (!RemoteFiles)
  1935. {
  1936. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true);
  1937. }
  1938. try
  1939. {
  1940. TDateTimes RemoteFileTimes;
  1941. if (RemoteFiles)
  1942. {
  1943. RemoteFileList = FileList;
  1944. }
  1945. else
  1946. {
  1947. RemoteFileList = new TStringList();
  1948. TMakeLocalFileListParams MakeFileListParam;
  1949. MakeFileListParam.FileList = RemoteFileList;
  1950. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1951. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1952. MakeFileListParam.Recursive =
  1953. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1954. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1955. if (!MakeFileListParam.Recursive)
  1956. {
  1957. RemoteFileTimes.clear();
  1958. RemoteFileListFull = new TStringList();
  1959. MakeFileListParam.FileList = RemoteFileListFull;
  1960. MakeFileListParam.Recursive = true;
  1961. MakeFileListParam.IncludeDirs = false;
  1962. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1963. }
  1964. else
  1965. {
  1966. RemoteFileListFull = RemoteFileList;
  1967. }
  1968. }
  1969. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1970. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1971. if (!NonBlocking)
  1972. {
  1973. int Count;
  1974. if (FileListCommand)
  1975. {
  1976. Count = 1;
  1977. }
  1978. else if (LocalFileCommand)
  1979. {
  1980. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1981. }
  1982. else
  1983. {
  1984. Count = RemoteFileList->Count;
  1985. }
  1986. Progress.Start(foCustomCommand, osRemote, Count);
  1987. DebugAssert(FProgressForm != NULL);
  1988. FProgressForm->ReadOnly = true;
  1989. }
  1990. try
  1991. {
  1992. if (FileListCommand)
  1993. {
  1994. UnicodeString LocalFile;
  1995. // MakeFileList does not delimit filenames
  1996. UnicodeString FileList = MakeFileList(RemoteFileList);
  1997. if (LocalFileCommand)
  1998. {
  1999. DebugAssert(LocalFileList->Count == 1);
  2000. LocalFile = LocalFileList->Strings[0];
  2001. }
  2002. TLocalCustomCommand CustomCommand(Data,
  2003. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2004. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2005. if (NonBlocking)
  2006. {
  2007. DebugAssert(!POutput);
  2008. ExecuteShellChecked(ShellCommand);
  2009. }
  2010. else
  2011. {
  2012. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2013. }
  2014. }
  2015. else if (LocalFileCommand)
  2016. {
  2017. if (LocalFileList->Count == 1)
  2018. {
  2019. UnicodeString LocalFile = LocalFileList->Strings[0];
  2020. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2021. {
  2022. UnicodeString FileName = RemoteFileList->Strings[Index];
  2023. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2024. Progress.SetFile(FileNameForProgress);
  2025. TLocalCustomCommand CustomCommand(Data,
  2026. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2027. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2028. TOnceDoneOperation OnceDoneOperation;
  2029. // Do not unselect anything, as it won't work with two panels anyway
  2030. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2031. }
  2032. }
  2033. else if (RemoteFileList->Count == 1)
  2034. {
  2035. UnicodeString FileName = RemoteFileList->Strings[0];
  2036. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2037. {
  2038. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2039. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2040. Progress.SetFile(LocalFileNameOnly);
  2041. TLocalCustomCommand CustomCommand(
  2042. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2043. FileName, LocalFileName, L"");
  2044. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2045. TOnceDoneOperation OnceDoneOperation;
  2046. // Do not unselect anything, as it won't work with two panels anyway
  2047. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2048. }
  2049. }
  2050. else
  2051. {
  2052. if (LocalFileList->Count != RemoteFileList->Count)
  2053. {
  2054. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2055. }
  2056. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2057. {
  2058. UnicodeString FileName = RemoteFileList->Strings[Index];
  2059. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2060. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2061. Progress.SetFile(LocalFileNameOnly); // sic
  2062. TLocalCustomCommand CustomCommand(
  2063. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2064. FileName, LocalFileName, L"");
  2065. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2066. TOnceDoneOperation OnceDoneOperation;
  2067. // Do not unselect anything, as it won't work with two panels anyway
  2068. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2069. }
  2070. }
  2071. }
  2072. else
  2073. {
  2074. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2075. {
  2076. UnicodeString FileName = RemoteFileList->Strings[Index];
  2077. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2078. Progress.SetFile(FileNameForProgress);
  2079. TLocalCustomCommand CustomCommand(Data,
  2080. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2081. FileName, L"", L"");
  2082. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2083. TOnceDoneOperation OnceDoneOperation;
  2084. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2085. }
  2086. }
  2087. }
  2088. __finally
  2089. {
  2090. if (!NonBlocking)
  2091. {
  2092. Progress.Stop();
  2093. }
  2094. }
  2095. DebugAssert(!FAutoOperation);
  2096. if (!RemoteFiles)
  2097. {
  2098. TempDir = IncludeTrailingBackslash(TempDir);
  2099. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2100. {
  2101. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2102. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2103. // Skip directories, process only nested files.
  2104. // The check is redundant as FileAge fails for directories anyway.
  2105. !DirectoryExists(FileName))
  2106. {
  2107. UnicodeString RemoteDir =
  2108. UnixExtractFileDir(
  2109. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2110. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2111. TDateTime NewTime;
  2112. if (FileAge(FileName, NewTime) &&
  2113. (NewTime != RemoteFileTimes[Index]))
  2114. {
  2115. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2116. TemporaryFileCopyParam(CopyParam);
  2117. CopyParam.FileMask = L"";
  2118. FAutoOperation = true;
  2119. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2120. TemporaryFilesList->Add(FileName);
  2121. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2122. }
  2123. }
  2124. }
  2125. }
  2126. }
  2127. __finally
  2128. {
  2129. FAutoOperation = false;
  2130. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2131. {
  2132. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2133. }
  2134. }
  2135. }
  2136. __finally
  2137. {
  2138. if (RemoteFileList != FileList)
  2139. {
  2140. delete RemoteFileList;
  2141. }
  2142. if (LocalFileList != ALocalFileList)
  2143. {
  2144. delete LocalFileList;
  2145. }
  2146. if (RemoteFileListFull != RemoteFileList)
  2147. {
  2148. delete RemoteFileListFull;
  2149. }
  2150. }
  2151. }
  2152. //---------------------------------------------------------------------------
  2153. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2154. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2155. bool FileListCommand, UnicodeString * POutput)
  2156. {
  2157. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2158. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2159. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2160. {
  2161. UnicodeString FileName = SelectedFileList->Strings[Index];
  2162. if (DirectoryExists(FileName))
  2163. {
  2164. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2165. {
  2166. LocalFileList->Add(FileName);
  2167. }
  2168. if (FLAGSET(ACommand.Params, ccRecursive))
  2169. {
  2170. TMakeLocalFileListParams MakeFileListParam;
  2171. MakeFileListParam.FileList = LocalFileList.get();
  2172. MakeFileListParam.FileTimes = NULL;
  2173. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2174. MakeFileListParam.Recursive = true;
  2175. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2176. }
  2177. }
  2178. else
  2179. {
  2180. LocalFileList->Add(FileName);
  2181. }
  2182. }
  2183. UnicodeString RemotePath;
  2184. if (Terminal != NULL)
  2185. {
  2186. RemotePath = Terminal->CurrentDirectory;
  2187. }
  2188. if (FileListCommand)
  2189. {
  2190. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2191. TLocalCustomCommand CustomCommand(
  2192. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2193. L"", L"", FileList);
  2194. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2195. }
  2196. else
  2197. {
  2198. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2199. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2200. DebugAssert(FProgressForm != NULL);
  2201. FProgressForm->ReadOnly = true;
  2202. try
  2203. {
  2204. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2205. {
  2206. UnicodeString FileName = LocalFileList->Strings[Index];
  2207. Progress.SetFile(FileName);
  2208. TLocalCustomCommand CustomCommand(
  2209. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2210. FileName, L"", L"");
  2211. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2212. TOnceDoneOperation OnceDoneOperation;
  2213. Progress.Finish(FileName, true, OnceDoneOperation);
  2214. }
  2215. }
  2216. __finally
  2217. {
  2218. Progress.Stop();
  2219. }
  2220. }
  2221. }
  2222. //---------------------------------------------------------------------------
  2223. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2224. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2225. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2226. {
  2227. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2228. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2229. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2230. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2231. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2232. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2233. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2234. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2235. std::unique_ptr<UnicodeString> POutput;
  2236. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2237. FLAGSET(ACommand.Params, ccCopyResults))
  2238. {
  2239. POutput.reset(new UnicodeString());
  2240. }
  2241. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2242. FProgressSide = FCurrentSide;
  2243. if (!LocalCustomCommand.IsFileCommand(Command))
  2244. {
  2245. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2246. }
  2247. // remote files?
  2248. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2249. {
  2250. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2251. }
  2252. // local files
  2253. else
  2254. {
  2255. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2256. }
  2257. if (POutput.get() != NULL)
  2258. {
  2259. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2260. int P = POutput->Pos(sLineBreak);
  2261. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2262. {
  2263. POutput->SetLength(P - 1);
  2264. }
  2265. // Copy even empty output to clipboard
  2266. if (FLAGSET(ACommand.Params, ccCopyResults))
  2267. {
  2268. CopyToClipboard(*POutput);
  2269. }
  2270. // But do not show an empty message box.
  2271. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2272. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2273. !POutput->IsEmpty())
  2274. {
  2275. TClipboardHandler ClipboardHandler;
  2276. ClipboardHandler.Text = *POutput;
  2277. TMessageParams Params;
  2278. TQueryButtonAlias Aliases[1];
  2279. Aliases[0].Button = qaRetry;
  2280. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2281. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2282. Params.Aliases = Aliases;
  2283. Params.AliasesCount = LENOF(Aliases);
  2284. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2285. }
  2286. }
  2287. }
  2288. //---------------------------------------------------------------------------
  2289. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2290. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2291. {
  2292. TCustomCommandData Data;
  2293. UnicodeString Site;
  2294. UnicodeString RemotePath;
  2295. if (HasActiveTerminal())
  2296. {
  2297. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2298. Data = TCustomCommandData(SessionData.get());
  2299. Site = Terminal->SessionData->SessionKey;
  2300. RemotePath = Terminal->CurrentDirectory;
  2301. }
  2302. UnicodeString HelpKeyword = ACommand.HomePage;
  2303. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2304. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2305. {
  2306. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2307. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2308. {
  2309. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2310. }
  2311. else
  2312. {
  2313. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2314. }
  2315. if (!DoCustomCommandOptionsDialog(
  2316. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2317. {
  2318. Abort();
  2319. }
  2320. }
  2321. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2322. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2323. {
  2324. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2325. }
  2326. else
  2327. {
  2328. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2329. }
  2330. }
  2331. //---------------------------------------------------------------------------
  2332. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2333. const TCustomCommandType & Command)
  2334. {
  2335. DebugAssert(FCustomCommandLocalFileList != NULL);
  2336. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2337. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2338. TStrings * LocalFileList = new TStringList();
  2339. TStrings * RemoteFileList = new TStringList();
  2340. try
  2341. {
  2342. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2343. {
  2344. LocalFileList->Clear();
  2345. LocalFileList->AddObject(
  2346. FCustomCommandLocalFileList->Strings[Index],
  2347. FCustomCommandLocalFileList->Objects[Index]);
  2348. RemoteFileList->Clear();
  2349. RemoteFileList->AddObject(
  2350. FCustomCommandRemoteFileList->Strings[Index],
  2351. FCustomCommandRemoteFileList->Objects[Index]);
  2352. CustomCommand(RemoteFileList, Command, LocalFileList);
  2353. }
  2354. }
  2355. __finally
  2356. {
  2357. delete LocalFileList;
  2358. delete RemoteFileList;
  2359. }
  2360. }
  2361. //---------------------------------------------------------------------------
  2362. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2363. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2364. {
  2365. delete FCustomCommandLocalFileList;
  2366. delete FCustomCommandRemoteFileList;
  2367. // takeover ownership,
  2368. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2369. FCustomCommandLocalFileList = LocalFileList;
  2370. FCustomCommandRemoteFileList = RemoteFileList;
  2371. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2372. if (Button != NULL)
  2373. {
  2374. FCustomCommandMenu->Items->Clear();
  2375. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2376. MenuPopup(FCustomCommandMenu, Button);
  2377. }
  2378. else
  2379. {
  2380. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2381. }
  2382. }
  2383. //---------------------------------------------------------------------------
  2384. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2385. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2386. {
  2387. DebugAssert(FCapturedLog != NULL);
  2388. if ((OutputType == cotOutput) || (OutputType == cotError))
  2389. {
  2390. FCapturedLog->Add(AddedLine);
  2391. }
  2392. }
  2393. //---------------------------------------------------------------------------
  2394. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2395. TOperationSide Side)
  2396. {
  2397. return
  2398. (Side == osOther) &&
  2399. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2400. }
  2401. //---------------------------------------------------------------------------
  2402. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2403. TOperationSide Side, TTBXCustomItem * Item,
  2404. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2405. {
  2406. TCustomDirView * DView = DirView(Side);
  2407. bool IsDefault = false;
  2408. if (DView->ItemFocused != NULL)
  2409. {
  2410. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2411. TResolvedDoubleClickAction DefaultAction =
  2412. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2413. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2414. }
  2415. TTBItemOptions O = Item->Options;
  2416. if (IsDefault)
  2417. {
  2418. Item->Options = O << tboDefault;
  2419. }
  2420. else
  2421. {
  2422. Item->Options = O >> tboDefault;
  2423. }
  2424. }
  2425. //---------------------------------------------------------------------------
  2426. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2427. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2428. {
  2429. TCustomDirView * DView = DirView(Side);
  2430. TListItem * Item = DView->ItemFocused;
  2431. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2432. EnableFocusedOperation[Side])
  2433. {
  2434. TPoint ClientPoint;
  2435. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2436. TPoint(0, 0) : MousePos;
  2437. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2438. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2439. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2440. }
  2441. }
  2442. //---------------------------------------------------------------------------
  2443. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2444. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2445. {
  2446. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2447. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2448. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2449. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2450. Handled = true;
  2451. }
  2452. //---------------------------------------------------------------------------
  2453. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2454. {
  2455. }
  2456. //---------------------------------------------------------------------------
  2457. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2458. {
  2459. DebugAssert(FErrorList == NULL);
  2460. if (WinConfiguration->ContinueOnError)
  2461. {
  2462. FErrorList = new TStringList();
  2463. Configuration->Usage->Inc(L"ContinuationsOnError");
  2464. }
  2465. NonVisualDataModule->StartBusy();
  2466. }
  2467. //---------------------------------------------------------------------------
  2468. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2469. {
  2470. NonVisualDataModule->EndBusy();
  2471. if (FErrorList)
  2472. {
  2473. HandleErrorList(FErrorList);
  2474. }
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2478. const TCopyParamType & CopyParam)
  2479. {
  2480. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2481. {
  2482. Configuration->Usage->Inc(L"FileMaskUses");
  2483. }
  2484. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2485. {
  2486. Configuration->Usage->Inc(L"OperationMaskUses");
  2487. }
  2488. }
  2489. //---------------------------------------------------------------------------
  2490. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2491. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2492. {
  2493. DebugAssert(!IsLocalBrowserMode());
  2494. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2495. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2496. TTransferOperationParam DefaultParam;
  2497. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2498. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2499. switch (Param.Queue)
  2500. {
  2501. case asOn:
  2502. CopyParam.Queue = true;
  2503. break;
  2504. case asOff:
  2505. CopyParam.Queue = false;
  2506. break;
  2507. case asAuto:
  2508. default:
  2509. // keep default
  2510. break;
  2511. }
  2512. bool Result =
  2513. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2514. if (Result)
  2515. {
  2516. DebugAssert(Terminal);
  2517. bool SelectionRestored = false;
  2518. TCustomDirView * DView = NULL;
  2519. if (HasDirView[Side])
  2520. {
  2521. DView = DirView(Side);
  2522. DView->SaveSelection();
  2523. DView->SaveSelectedNames();
  2524. }
  2525. UpdateCopyParamCounters(CopyParam);
  2526. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2527. DebugAssert(FTransferResumeList == NULL);
  2528. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2529. FMoveToQueue = false;
  2530. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2531. try
  2532. {
  2533. TStrings * PermanentFileList;
  2534. std::unique_ptr<TStrings> PermanentFileListOwner;
  2535. try
  2536. {
  2537. if (Side == osLocal)
  2538. {
  2539. PermanentFileList = FileList;
  2540. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2541. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2542. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2543. if (Operation == foMove)
  2544. {
  2545. ReloadLocalDirectory();
  2546. if (DView != NULL)
  2547. {
  2548. DView->RestoreSelection();
  2549. }
  2550. SelectionRestored = true;
  2551. }
  2552. }
  2553. else
  2554. {
  2555. // Clone the file list as it may refer to current directory files,
  2556. // which get destroyed, when the source directory is reloaded after foMove operation.
  2557. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2558. // But for a hotfix, we are not going to do such a big change.
  2559. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2560. PermanentFileList = PermanentFileListOwner.get();
  2561. try
  2562. {
  2563. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2564. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2565. }
  2566. __finally
  2567. {
  2568. if (Operation == foMove)
  2569. {
  2570. if (DView != NULL)
  2571. {
  2572. DView->RestoreSelection();
  2573. }
  2574. SelectionRestored = true;
  2575. }
  2576. ReloadLocalDirectory(Param.TargetDirectory);
  2577. }
  2578. }
  2579. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2580. if (FMoveToQueue)
  2581. {
  2582. Abort();
  2583. }
  2584. }
  2585. catch (EAbort &)
  2586. {
  2587. if (FMoveToQueue)
  2588. {
  2589. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2590. DebugAssert(CopyParam.TransferSkipList == NULL);
  2591. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2592. if (TransferResumeList->Count > 0)
  2593. {
  2594. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2595. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2596. }
  2597. CopyParam.TransferSkipList = TransferResumeList.release();
  2598. // not really needed, just to keep it consistent with TransferResumeList
  2599. FTransferResumeList = NULL;
  2600. FMoveToQueue = false;
  2601. Configuration->Usage->Inc("MovesToBackground");
  2602. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2603. ClearTransferSourceSelection(Direction);
  2604. }
  2605. throw;
  2606. }
  2607. }
  2608. __finally
  2609. {
  2610. if (!SelectionRestored && (DView != NULL))
  2611. {
  2612. DView->DiscardSavedSelection();
  2613. }
  2614. FTransferResumeList = NULL;
  2615. }
  2616. }
  2617. return Result;
  2618. }
  2619. //---------------------------------------------------------------------------
  2620. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2621. TOperationSide Side, TStrings * FileList, void * Param)
  2622. {
  2623. DebugAssert(FileList->Count);
  2624. // We deliberately do not toggle alternative flag (Param), but use OR,
  2625. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2626. // shortcut of CurrentDeleteAlternativeAction
  2627. bool Alternative =
  2628. bool(Param) || UseAlternativeFunction();
  2629. bool Recycle;
  2630. if (IsSideLocalBrowser(Side))
  2631. {
  2632. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2633. }
  2634. else
  2635. {
  2636. Recycle =
  2637. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2638. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2639. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2640. }
  2641. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2642. if (!Result)
  2643. {
  2644. UnicodeString Query;
  2645. if (FileList->Count == 1)
  2646. {
  2647. if (IsSideLocalBrowser(Side))
  2648. {
  2649. Query = ExtractFileName(FileList->Strings[0]);
  2650. }
  2651. else
  2652. {
  2653. Query = UnixExtractFileName(FileList->Strings[0]);
  2654. }
  2655. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2656. }
  2657. else
  2658. {
  2659. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2660. }
  2661. TMessageParams Params(mpNeverAskAgainCheck);
  2662. Params.ImageName = L"Delete file";
  2663. unsigned int Answer =
  2664. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2665. if (Answer == qaNeverAskAgain)
  2666. {
  2667. Result = true;
  2668. if (Recycle)
  2669. {
  2670. WinConfiguration->ConfirmRecycling = false;
  2671. }
  2672. else
  2673. {
  2674. WinConfiguration->ConfirmDeleting = false;
  2675. }
  2676. }
  2677. else
  2678. {
  2679. Result = (Answer == qaOK);
  2680. }
  2681. }
  2682. if (Result)
  2683. {
  2684. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2685. }
  2686. return Result;
  2687. }
  2688. //---------------------------------------------------------------------------
  2689. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2690. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2691. {
  2692. TAutoBatch AutoBatch(this);
  2693. bool Result;
  2694. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2695. {
  2696. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2697. }
  2698. else if (Operation == foRename)
  2699. {
  2700. DebugAssert(DirView(Side)->ItemFocused);
  2701. DirView(Side)->ItemFocused->EditCaption();
  2702. Result = true;
  2703. }
  2704. else if (Operation == foDelete)
  2705. {
  2706. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2707. }
  2708. else if (Operation == foSetProperties)
  2709. {
  2710. DirView(osRemote)->SaveSelectedNames();
  2711. Result = SetProperties(Side, FileList);
  2712. }
  2713. else if (Operation == foCustomCommand)
  2714. {
  2715. DebugAssert(Param);
  2716. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2717. DirView(osRemote)->SaveSelectedNames();
  2718. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2719. CustomCommand(FileList, *Command, NULL);
  2720. Result = true;
  2721. }
  2722. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2723. {
  2724. DebugAssert(!IsSideLocalBrowser(Side));
  2725. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2726. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2727. }
  2728. else if (Operation == foLock)
  2729. {
  2730. DebugAssert(!IsSideLocalBrowser(Side));
  2731. LockFiles(FileList, true);
  2732. Result = true;
  2733. }
  2734. else if (Operation == foUnlock)
  2735. {
  2736. DebugAssert(!IsSideLocalBrowser(Side));
  2737. LockFiles(FileList, false);
  2738. Result = true;
  2739. }
  2740. else
  2741. {
  2742. DebugFail();
  2743. }
  2744. return Result;
  2745. }
  2746. //---------------------------------------------------------------------------
  2747. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2748. {
  2749. if (Side == osCurrent)
  2750. {
  2751. Side = FCurrentSide;
  2752. }
  2753. return Side;
  2754. }
  2755. //---------------------------------------------------------------------------
  2756. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2757. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2758. {
  2759. Side = GetSide(Side);
  2760. bool FullPath = IsSideLocalBrowser(Side);
  2761. TCustomDirView * ADirView = DirView(Side);
  2762. std::unique_ptr<TStringList> FileList(new TStringList());
  2763. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2764. bool ReloadProperties =
  2765. (Side == osRemote) && (Operation == foSetProperties) &&
  2766. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2767. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2768. if (Result && ReloadProperties)
  2769. {
  2770. std::unique_ptr<TStringList> FileList2(new TStringList());
  2771. FileList->CaseSensitive = true;
  2772. FileList->Sort();
  2773. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2774. {
  2775. TListItem * Item = ADirView->Items->Item[Index];
  2776. UnicodeString FileName = ADirView->ItemFileName(Item);
  2777. int Index2;
  2778. if (FileList->Find(FileName, Index2))
  2779. {
  2780. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2781. }
  2782. }
  2783. LoadFilesProperties(FileList2.get());
  2784. }
  2785. return Result;
  2786. }
  2787. //---------------------------------------------------------------------------
  2788. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2789. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2790. bool NoConfirmation, void * Param)
  2791. {
  2792. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2793. {
  2794. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2795. {
  2796. DebugAssert(!IsLocalBrowserMode());
  2797. if (GetSide(Side) == osLocal)
  2798. {
  2799. Configuration->Usage->Inc(L"UploadsCommand");
  2800. }
  2801. else
  2802. {
  2803. Configuration->Usage->Inc(L"DownloadsCommand");
  2804. }
  2805. }
  2806. }
  2807. }
  2808. //---------------------------------------------------------------------------
  2809. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2810. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2811. {
  2812. TTransferOperationParam Param;
  2813. if ((WinConfiguration->Interface != ifCommander) ||
  2814. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2815. {
  2816. Flags &= ~cocShortCutHint;
  2817. }
  2818. TCustomDirView * DView = DirView(Side);
  2819. Param.Options =
  2820. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2821. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2822. if (FLAGSET(Flags, cocQueue))
  2823. {
  2824. Param.Queue = asOn;
  2825. }
  2826. else if (FLAGSET(Flags, cocNonQueue))
  2827. {
  2828. Param.Queue = asOff;
  2829. }
  2830. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2831. }
  2832. //---------------------------------------------------------------------------
  2833. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2834. {
  2835. int Count = ErrorList->Count;
  2836. try
  2837. {
  2838. if (ErrorList->Count)
  2839. {
  2840. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2841. if (MessageDialog(Message, qtError,
  2842. qaOK | qaCancel, HELP_NONE) == qaOK)
  2843. {
  2844. unsigned int Answer;
  2845. int Index = 0;
  2846. do
  2847. {
  2848. DebugAssert(Index >= 0 && Index < Count);
  2849. TQueryButtonAlias Aliases[2];
  2850. Aliases[0].Button = qaYes;
  2851. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2852. Aliases[1].Button = qaNo;
  2853. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2854. TMessageParams Params;
  2855. Params.Aliases = Aliases;
  2856. Params.AliasesCount = LENOF(Aliases);
  2857. int No = Index + 1;
  2858. UnicodeString Message = ErrorList->Strings[Index];
  2859. UnicodeString AMainInstructions;
  2860. UnicodeString Details;
  2861. if (ExtractMainInstructions(Message, AMainInstructions))
  2862. {
  2863. Details = Message;
  2864. Message = AMainInstructions;
  2865. }
  2866. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2867. Answer = MoreMessageDialog(
  2868. Message,
  2869. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2870. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2871. qaOK, HELP_NONE, &Params);
  2872. if (Answer == qaNo)
  2873. {
  2874. Index++;
  2875. }
  2876. if (Answer == qaYes)
  2877. {
  2878. Index--;
  2879. }
  2880. }
  2881. while (Answer != qaOK);
  2882. }
  2883. }
  2884. }
  2885. __finally
  2886. {
  2887. TStrings * List = ErrorList;
  2888. ErrorList = NULL;
  2889. for (int i = 0; i < Count; i++)
  2890. {
  2891. delete List->Objects[i];
  2892. }
  2893. delete List;
  2894. }
  2895. }
  2896. //---------------------------------------------------------------------------
  2897. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2898. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2899. {
  2900. // needed for checking filemasks, as there's no directory object
  2901. // associated with the file object
  2902. File->FullFileName = FullFileName;
  2903. TFileMasks::TParams MaskParams;
  2904. MaskParams.Size = File->Size;
  2905. MaskParams.Modification = File->Modification;
  2906. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2907. }
  2908. //---------------------------------------------------------------------------
  2909. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2910. {
  2911. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2912. Side = GetSide(Side);
  2913. TCustomDirView * DView = DirView(Side);
  2914. TListItem * FocusedItem = DView->ItemFocused;
  2915. UnicodeString Name;
  2916. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2917. {
  2918. Name = DView->ItemFileName(FocusedItem);
  2919. }
  2920. else
  2921. {
  2922. Name = LoadStr(NEW_FILE);
  2923. }
  2924. UnicodeString Names = Name;
  2925. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2926. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2927. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2928. {
  2929. while (!Names.IsEmpty())
  2930. {
  2931. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2932. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2933. UnicodeString TargetFileName;
  2934. UnicodeString LocalFileName;
  2935. UnicodeString RootTempDir;
  2936. UnicodeString TempDir;
  2937. UnicodeString RemoteDirectory;
  2938. bool ExistingFile = false;
  2939. if (!IsSideLocalBrowser(Side))
  2940. {
  2941. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2942. TRemoteFile * File = NULL;
  2943. if (Terminal->FileExists(Name, &File) &&
  2944. !File->IsDirectory)
  2945. {
  2946. try
  2947. {
  2948. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2949. ExistingFile = true;
  2950. }
  2951. __finally
  2952. {
  2953. delete File;
  2954. }
  2955. }
  2956. if (!ExistingFile)
  2957. {
  2958. RemoteDirectory = UnixExtractFilePath(Name);
  2959. TemporaryDirectoryForRemoteFiles(
  2960. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2961. TargetFileName = UnixExtractFileName(Name);
  2962. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2963. LocalFileName = TempDir +
  2964. // We probably do not want to trim the VMS version here
  2965. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2966. }
  2967. }
  2968. else
  2969. {
  2970. if (ExtractFilePath(Name).IsEmpty())
  2971. {
  2972. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2973. }
  2974. else
  2975. {
  2976. LocalFileName = ExpandFileName(Name);
  2977. }
  2978. TargetFileName = ExtractFileName(Name);
  2979. }
  2980. if (!ExistingFile)
  2981. {
  2982. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2983. if (NewFile)
  2984. {
  2985. int File = FileCreate(ApiPath(LocalFileName));
  2986. if (File < 0)
  2987. {
  2988. if (!RootTempDir.IsEmpty())
  2989. {
  2990. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2991. }
  2992. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2993. }
  2994. else
  2995. {
  2996. FileClose(File);
  2997. }
  2998. }
  2999. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3000. const TEditorData * ExternalEditor = NULL;
  3001. TFileMasks::TParams MaskParams; // size not known
  3002. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3003. false, MaskParams);
  3004. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3005. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3006. }
  3007. }
  3008. }
  3009. }
  3010. //---------------------------------------------------------------------------
  3011. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3012. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3013. {
  3014. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3015. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3016. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3017. return
  3018. ((ExecuteFileBy == efInternalEditor)) ||
  3019. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3020. }
  3021. //---------------------------------------------------------------------------
  3022. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3023. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3024. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3025. UnicodeString RemoteDirectory, bool NewFile)
  3026. {
  3027. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3028. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3029. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3030. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3031. Side = GetSide(Side);
  3032. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3033. if (!IsSideLocalBrowser(Side))
  3034. {
  3035. Data->Terminal = Terminal;
  3036. Data->Queue = Queue;
  3037. Data->SessionData = CloneCurrentSessionData();
  3038. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3039. Data->RemoteDirectory = RemoteDirectory;
  3040. Data->SessionName = Terminal->SessionData->SessionName;
  3041. Data->LocalRootDirectory = LocalRootDirectory;
  3042. Data->OriginalFileName = OriginalFileName;
  3043. Data->Command = L""; // will be changed later for external editor
  3044. }
  3045. if (ExecuteFileBy == efInternalEditor)
  3046. {
  3047. if (!IsSideLocalBrowser(Side))
  3048. {
  3049. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3050. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3051. TForm * Editor;
  3052. try
  3053. {
  3054. Editor =
  3055. ShowEditorForm(
  3056. FileName, this,
  3057. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3058. SaveAllInternalEditors, AnyInternalEditorModified,
  3059. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3060. }
  3061. catch(...)
  3062. {
  3063. if (!LocalRootDirectory.IsEmpty())
  3064. {
  3065. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3066. }
  3067. throw;
  3068. }
  3069. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3070. }
  3071. else
  3072. {
  3073. DebugAssert(!StandaloneOperation);
  3074. TForm * Editor =
  3075. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3076. SaveAllInternalEditors, AnyInternalEditorModified,
  3077. L"", false, SessionColor, -1, NewFile);
  3078. FLocalEditors->Add(Editor);
  3079. }
  3080. }
  3081. else
  3082. {
  3083. HANDLE Process;
  3084. if (ExecuteFileBy == efExternalEditor)
  3085. {
  3086. UnicodeString Program, Params, Dir;
  3087. Data->Command = ExternalEditor->ExternalEditor;
  3088. ReformatFileNameCommand(Data->Command);
  3089. SplitCommand(Data->Command, Program, Params, Dir);
  3090. Params = ExpandFileNameCommand(Params, FileName);
  3091. Program = ExpandEnvironmentVariables(Program);
  3092. if (!ExecuteShell(Program, Params, Process))
  3093. {
  3094. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3095. }
  3096. }
  3097. else
  3098. {
  3099. DebugAssert(!IsSideLocalBrowser(Side));
  3100. if (!ExecuteShell(FileName, L"", Process))
  3101. {
  3102. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3103. }
  3104. }
  3105. if (IsSideLocalBrowser(Side) ||
  3106. ((ExecuteFileBy == efShell) &&
  3107. !WinConfiguration->Editor.SDIShellEditor) ||
  3108. ((ExecuteFileBy == efExternalEditor) &&
  3109. !ExternalEditor->SDIExternalEditor))
  3110. {
  3111. // no need for handle
  3112. if (Process != NULL)
  3113. {
  3114. DebugCheck(CloseHandle(Process));
  3115. }
  3116. Process = INVALID_HANDLE_VALUE;
  3117. }
  3118. else
  3119. {
  3120. if (Process == NULL)
  3121. {
  3122. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3123. }
  3124. }
  3125. if (!IsSideLocalBrowser(Side))
  3126. {
  3127. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3128. }
  3129. }
  3130. }
  3131. //---------------------------------------------------------------------------
  3132. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3133. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3134. void * /*Arg*/)
  3135. {
  3136. if (Token != NULL)
  3137. {
  3138. EditorFormFileSave(static_cast<TForm *>(Token));
  3139. }
  3140. }
  3141. //---------------------------------------------------------------------------
  3142. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3143. {
  3144. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3145. {
  3146. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3147. }
  3148. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3149. }
  3150. //---------------------------------------------------------------------------
  3151. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3152. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3153. void * Arg)
  3154. {
  3155. if ((Token != NULL) &&
  3156. IsEditorFormModified(static_cast<TForm *>(Token)))
  3157. {
  3158. *static_cast<bool *>(Arg) = true;
  3159. }
  3160. }
  3161. //---------------------------------------------------------------------------
  3162. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3163. TObject * /*Sender*/, bool & Modified)
  3164. {
  3165. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3166. {
  3167. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3168. {
  3169. Modified = true;
  3170. }
  3171. }
  3172. if (!Modified)
  3173. {
  3174. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3175. }
  3176. }
  3177. //---------------------------------------------------------------------------
  3178. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3179. {
  3180. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3181. }
  3182. //---------------------------------------------------------------------------
  3183. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3184. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3185. UnicodeString & Result, UnicodeString & RootDirectory)
  3186. {
  3187. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3188. {
  3189. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3190. Result = RootDirectory;
  3191. }
  3192. else
  3193. {
  3194. RootDirectory = L"";
  3195. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3196. Result = IncludeTrailingBackslash(Result);
  3197. }
  3198. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3199. {
  3200. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3201. }
  3202. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3203. {
  3204. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3205. {
  3206. RemoteDirectory.Delete(1, 1);
  3207. }
  3208. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3209. }
  3210. if (!ForceDirectories(ApiPath(Result)))
  3211. {
  3212. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3213. }
  3214. }
  3215. //---------------------------------------------------------------------------
  3216. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3217. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3218. bool GetTargetNames, bool AutoOperation)
  3219. {
  3220. DebugAssert(!IsLocalBrowserMode());
  3221. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3222. if (ForceText)
  3223. {
  3224. CopyParam.TransferMode = tmAscii;
  3225. }
  3226. TemporaryFileCopyParam(CopyParam);
  3227. if (TempDir.IsEmpty())
  3228. {
  3229. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3230. }
  3231. DebugAssert(!FAutoOperation);
  3232. FAutoOperation = AutoOperation;
  3233. Terminal->ExceptionOnFail = true;
  3234. try
  3235. {
  3236. try
  3237. {
  3238. // turn off confirmations, as for MDI editors we may possibly download
  3239. // the same file over
  3240. Terminal->CopyToLocal(
  3241. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3242. if (GetTargetNames)
  3243. {
  3244. for (int i = 0; i < FileList->Count; i++)
  3245. {
  3246. FileList->Strings[i] =
  3247. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3248. }
  3249. }
  3250. }
  3251. catch(...)
  3252. {
  3253. if (!RootTempDir.IsEmpty())
  3254. {
  3255. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3256. }
  3257. throw;
  3258. }
  3259. }
  3260. __finally
  3261. {
  3262. FAutoOperation = false;
  3263. Terminal->ExceptionOnFail = false;
  3264. }
  3265. }
  3266. //---------------------------------------------------------------------------
  3267. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3268. {
  3269. // Do not waste time checking for default editor list the next time,
  3270. // or testing if default editor is not notepad.
  3271. bool TryNextTime = false;
  3272. UnicodeString UsageState;
  3273. if (!WinConfiguration->EditorList->IsDefaultList())
  3274. {
  3275. UsageState = "H";
  3276. }
  3277. else
  3278. {
  3279. UnicodeString Executable;
  3280. UnicodeString ExecutableDescription;
  3281. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3282. {
  3283. UnicodeString Message =
  3284. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3285. unsigned int Answer =
  3286. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3287. if (Answer != qaOK)
  3288. {
  3289. UsageState = "R";
  3290. }
  3291. else
  3292. {
  3293. UsageState = "A";
  3294. TEditorData EditorData;
  3295. EditorData.Editor = edExternal;
  3296. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3297. EditorData.ExternalEditorOptionsAutodetect();
  3298. TEditorList EditorList;
  3299. EditorList = *WinConfiguration->EditorList;
  3300. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3301. WinConfiguration->EditorList = &EditorList;
  3302. }
  3303. }
  3304. }
  3305. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3306. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3307. }
  3308. //---------------------------------------------------------------------------
  3309. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3310. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3311. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3312. {
  3313. if (ExecuteFileBy == efDefaultEditor)
  3314. {
  3315. if (!WinConfiguration->OfferedEditorAutoConfig)
  3316. {
  3317. EditorAutoConfig();
  3318. }
  3319. const TEditorPreferences * Editor =
  3320. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3321. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3322. {
  3323. ExecuteFileBy = efInternalEditor;
  3324. ExternalEditor = NULL;
  3325. }
  3326. else if (Editor->Data->Editor == edOpen)
  3327. {
  3328. ExecuteFileBy = efShell;
  3329. ExternalEditor = NULL;
  3330. }
  3331. else
  3332. {
  3333. ExecuteFileBy = efExternalEditor;
  3334. ExternalEditor = Editor->Data;
  3335. }
  3336. }
  3337. }
  3338. //---------------------------------------------------------------------------
  3339. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3340. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3341. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3342. {
  3343. UnicodeString OriginalFileName;
  3344. UnicodeString LocalRootDirectory;
  3345. UnicodeString RemoteDirectory;
  3346. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3347. IsSideLocalBrowser(Side), MaskParams);
  3348. UnicodeString Counter;
  3349. UnicodeString LocalFileName;
  3350. bool Handled = false;
  3351. if (!IsSideLocalBrowser(Side))
  3352. {
  3353. // We need to trim VMS version here, so that we use name without version
  3354. // when uploading back to create a new version of the file
  3355. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3356. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3357. TObject * Token = NULL;
  3358. UnicodeString LocalDirectory;
  3359. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3360. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3361. LocalDirectory))
  3362. {
  3363. if (Token != NULL)
  3364. {
  3365. TForm * Form = dynamic_cast<TForm *>(Token);
  3366. if (Form->WindowState == wsMinimized)
  3367. {
  3368. ShowWindow(Form->Handle, SW_RESTORE);
  3369. }
  3370. else
  3371. {
  3372. Form->SetFocus();
  3373. }
  3374. Handled = true;
  3375. }
  3376. else
  3377. {
  3378. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3379. }
  3380. }
  3381. if (!Handled)
  3382. {
  3383. TStringList * FileList1 = new TStringList();
  3384. try
  3385. {
  3386. FileList1->AddObject(FullFileName, Object);
  3387. TemporarilyDownloadFiles(FileList1,
  3388. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3389. LocalRootDirectory, LocalDirectory, true, true);
  3390. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3391. }
  3392. __finally
  3393. {
  3394. delete FileList1;
  3395. }
  3396. switch (ExecuteFileBy)
  3397. {
  3398. case efShell:
  3399. Counter = "RemoteFilesExecuted";
  3400. break;
  3401. case efInternalEditor:
  3402. Counter = "RemoteFilesOpenedInInternalEditor";
  3403. break;
  3404. case efExternalEditor:
  3405. Counter = "RemoteFilesOpenedInExternalEditor";
  3406. break;
  3407. default:
  3408. DebugFail();
  3409. }
  3410. }
  3411. }
  3412. else
  3413. {
  3414. LocalFileName = FullFileName;
  3415. OriginalFileName = ExtractFileName(FullFileName);
  3416. switch (ExecuteFileBy)
  3417. {
  3418. case efShell:
  3419. Counter = "LocalFilesExecuted";
  3420. break;
  3421. case efInternalEditor:
  3422. Counter = "LocalFilesOpenedInInternalEditor";
  3423. break;
  3424. case efExternalEditor:
  3425. Counter = "LocalFilesOpenedInExternalEditor";
  3426. break;
  3427. default:
  3428. DebugFail();
  3429. }
  3430. }
  3431. if (!Handled)
  3432. {
  3433. Configuration->Usage->Inc(Counter);
  3434. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3435. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3436. }
  3437. }
  3438. //---------------------------------------------------------------------------
  3439. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3440. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3441. bool AllSelected, bool OnFocused)
  3442. {
  3443. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3444. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3445. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3446. Side = GetSide(Side);
  3447. TCustomDirView * DView = DirView(Side);
  3448. TStrings * FileList =
  3449. AllSelected ?
  3450. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3451. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3452. try
  3453. {
  3454. DebugAssert(AllSelected || (FileList->Count == 1));
  3455. for (int i = 0; i < FileList->Count; i++)
  3456. {
  3457. UnicodeString ListFileName = FileList->Strings[i];
  3458. UnicodeString FileNameOnly =
  3459. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3460. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3461. if (!DView->ItemIsDirectory(Item))
  3462. {
  3463. UnicodeString FullFileName;
  3464. if (!IsSideLocalBrowser(Side))
  3465. {
  3466. FullFileName = DirView(Side)->Path + ListFileName;
  3467. }
  3468. else
  3469. {
  3470. FullFileName = ListFileName;
  3471. }
  3472. TFileMasks::TParams MaskParams;
  3473. MaskParams.Size = DView->ItemFileSize(Item);
  3474. TDateTimePrecision Precision;
  3475. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3476. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3477. FileList->Objects[i], MaskParams);
  3478. }
  3479. }
  3480. }
  3481. __finally
  3482. {
  3483. delete FileList;
  3484. }
  3485. }
  3486. //---------------------------------------------------------------------------
  3487. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3488. {
  3489. CopyParam.FileNameCase = ncNoChange;
  3490. CopyParam.PreserveRights = false;
  3491. CopyParam.PreserveReadOnly = false;
  3492. CopyParam.ReplaceInvalidChars = true;
  3493. CopyParam.IncludeFileMask = TFileMasks();
  3494. CopyParam.NewerOnly = false;
  3495. CopyParam.FileMask = L"";
  3496. }
  3497. //---------------------------------------------------------------------------
  3498. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3499. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3500. {
  3501. TTerminalManager * Manager = TTerminalManager::Instance();
  3502. if (!IsActiveTerminal(Data->Terminal))
  3503. {
  3504. if (!NonVisualDataModule->Busy)
  3505. {
  3506. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3507. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3508. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3509. if (Data->Terminal == NULL)
  3510. {
  3511. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3512. if (SameSiteTerminal != NULL)
  3513. {
  3514. UnicodeString Message =
  3515. FMTLOAD(EDIT_SESSION_REATTACH,
  3516. (FileNameOnly, Data->SessionName, FileNameOnly));
  3517. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3518. {
  3519. Data->Terminal = Terminal;
  3520. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3521. Data->SessionName = Terminal->SessionData->SessionName;
  3522. // We might also overwrite session data
  3523. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3524. }
  3525. else
  3526. {
  3527. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3528. Abort();
  3529. }
  3530. }
  3531. }
  3532. // foreground session should reconnect itself
  3533. else if (Terminal != Data->Terminal)
  3534. {
  3535. UnicodeString Message =
  3536. MainInstructions(
  3537. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3538. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3539. {
  3540. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3541. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3542. }
  3543. else
  3544. {
  3545. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3546. Abort();
  3547. }
  3548. }
  3549. }
  3550. if (!IsActiveTerminal(Data->Terminal))
  3551. {
  3552. Configuration->Usage->Inc(L"EditInactiveSession");
  3553. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3554. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3555. (ExtractFileName(FileName), Data->SessionName)));
  3556. }
  3557. }
  3558. TStrings * FileList = new TStringList();
  3559. try
  3560. {
  3561. FileList->Add(FileName);
  3562. // consider using the same settings (preset) as when the file was downloaded
  3563. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3564. TemporaryFileCopyParam(CopyParam);
  3565. if (Data->ForceText)
  3566. {
  3567. CopyParam.TransferMode = tmAscii;
  3568. }
  3569. // so i do not need to worry if masking algorithm works in all cases
  3570. // ("" means "copy file name", no masking is actually done)
  3571. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3572. {
  3573. CopyParam.FileMask = L"";
  3574. }
  3575. else
  3576. {
  3577. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3578. }
  3579. int Params = cpNoConfirmation | cpTemporary;
  3580. if (Terminal->IsCapable[fcBackgroundTransfers])
  3581. {
  3582. DebugAssert(Data->Queue != NULL);
  3583. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3584. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3585. QueueItem->CompleteEvent = UploadCompleteEvent;
  3586. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3587. }
  3588. else
  3589. {
  3590. if (NonVisualDataModule->Busy)
  3591. {
  3592. Retry = true;
  3593. }
  3594. else
  3595. {
  3596. Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3597. SetEvent(UploadCompleteEvent);
  3598. }
  3599. }
  3600. }
  3601. __finally
  3602. {
  3603. delete FileList;
  3604. }
  3605. }
  3606. //---------------------------------------------------------------------------
  3607. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3608. const UnicodeString FileName, const TEditedFileData * Data)
  3609. {
  3610. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3611. DebugAssert(!NonVisualDataModule->Busy);
  3612. if (!IsActiveTerminal(Data->Terminal))
  3613. {
  3614. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3615. (ExtractFileName(FileName), Data->SessionName)));
  3616. }
  3617. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3618. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3619. NonVisualDataModule->StartBusy();
  3620. try
  3621. {
  3622. std::unique_ptr<TRemoteFile> File;
  3623. UnicodeString RemoteFileName =
  3624. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3625. Terminal->ExceptionOnFail = true;
  3626. try
  3627. {
  3628. TRemoteFile * AFile = NULL;
  3629. Terminal->ReadFile(RemoteFileName, AFile);
  3630. File.reset(AFile);
  3631. if (!File->HaveFullFileName)
  3632. {
  3633. File->FullFileName = RemoteFileName;
  3634. }
  3635. }
  3636. __finally
  3637. {
  3638. Terminal->ExceptionOnFail = false;
  3639. }
  3640. std::unique_ptr<TStrings> FileList(new TStringList());
  3641. FileList->AddObject(RemoteFileName, File.get());
  3642. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3643. UnicodeString TempDir = ExtractFilePath(FileName);
  3644. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true);
  3645. // sanity check, the target file name should be still the same
  3646. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3647. }
  3648. __finally
  3649. {
  3650. NonVisualDataModule->EndBusy();
  3651. // it actually may not exist anymore...
  3652. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3653. }
  3654. }
  3655. //---------------------------------------------------------------------------
  3656. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3657. const TEditedFileData * Data, bool & KeepOpen)
  3658. {
  3659. // Command is set for external editors only (not for "shell" open).
  3660. if (!Data->Command.IsEmpty())
  3661. {
  3662. bool AnyFound = false;
  3663. bool AnyMDI = false;
  3664. bool AnyNonMDI = false;
  3665. bool AnyDetect = false;
  3666. TEditorList * EditorList = new TEditorList();
  3667. try
  3668. {
  3669. *EditorList = *WinConfiguration->EditorList;
  3670. for (int i = 0; i < EditorList->Count; i++)
  3671. {
  3672. const TEditorPreferences * Editor = EditorList->Editors[i];
  3673. if ((Editor->Data->Editor == edExternal) &&
  3674. (Editor->Data->ExternalEditor == Data->Command))
  3675. {
  3676. AnyFound = true;
  3677. if (Editor->Data->SDIExternalEditor)
  3678. {
  3679. AnyNonMDI = true;
  3680. if (Editor->Data->DetectMDIExternalEditor)
  3681. {
  3682. AnyDetect = true;
  3683. }
  3684. }
  3685. else
  3686. {
  3687. AnyMDI = true;
  3688. }
  3689. }
  3690. }
  3691. bool EnableMDI = false;
  3692. bool DisableDetect = false;
  3693. if (AnyMDI)
  3694. {
  3695. KeepOpen = true;
  3696. if (AnyNonMDI)
  3697. {
  3698. // there is at least one instance of the editor with MDI support enabled,
  3699. // and one with disabled, enable it for all instances
  3700. EnableMDI = true;
  3701. }
  3702. }
  3703. else if (AnyFound && !AnyDetect)
  3704. {
  3705. // at least once instance found but all have MDI autodetection disabled
  3706. // => close the file (default action)
  3707. }
  3708. else
  3709. {
  3710. // no instance of the editor has MDI support enabled
  3711. TMessageParams Params;
  3712. if (AnyFound)
  3713. {
  3714. // there is at least one instance of the editor
  3715. Params.Params |= mpNeverAskAgainCheck;
  3716. }
  3717. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3718. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3719. switch (Answer)
  3720. {
  3721. case qaNeverAskAgain:
  3722. DisableDetect = true;
  3723. break;
  3724. case qaNo:
  3725. EnableMDI = true;
  3726. KeepOpen = true;
  3727. break;
  3728. }
  3729. }
  3730. if (AnyFound && (EnableMDI || DisableDetect))
  3731. {
  3732. bool Changed = false;
  3733. for (int i = 0; i < EditorList->Count; i++)
  3734. {
  3735. const TEditorPreferences * Editor = EditorList->Editors[i];
  3736. if ((Editor->Data->Editor == edExternal) &&
  3737. (Editor->Data->ExternalEditor == Data->Command) &&
  3738. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3739. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3740. {
  3741. Changed = true;
  3742. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3743. if (EnableMDI)
  3744. {
  3745. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3746. }
  3747. if (DisableDetect)
  3748. {
  3749. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3750. }
  3751. EditorList->Change(i, UpdatedEditor);
  3752. }
  3753. }
  3754. if (Changed)
  3755. {
  3756. WinConfiguration->EditorList = EditorList;
  3757. }
  3758. }
  3759. }
  3760. __finally
  3761. {
  3762. delete EditorList;
  3763. }
  3764. }
  3765. else
  3766. {
  3767. // "open" case
  3768. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3769. qaOK, HELP_APP_EARLY_CLOSED);
  3770. }
  3771. }
  3772. //---------------------------------------------------------------------------
  3773. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3774. {
  3775. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3776. }
  3777. //---------------------------------------------------------------------------
  3778. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3779. {
  3780. SideEnter(osRemote);
  3781. }
  3782. //---------------------------------------------------------------------------
  3783. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3784. {
  3785. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3786. SideEnter(osRemote);
  3787. }
  3788. //---------------------------------------------------------------------------
  3789. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3790. {
  3791. FCurrentSide = Side;
  3792. if (Visible)
  3793. {
  3794. UpdateControls();
  3795. }
  3796. }
  3797. //---------------------------------------------------------------------------
  3798. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3799. {
  3800. VisualiseOperationFinished(Side, FileName, Success);
  3801. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3802. {
  3803. FDeletedFiles->Add(FileName);
  3804. }
  3805. }
  3806. //---------------------------------------------------------------------------
  3807. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3808. TStrings * FileList, bool Alternative)
  3809. {
  3810. TCustomDirView * DView = DirView(Side);
  3811. DView->SaveSelection();
  3812. DView->SaveSelectedNames();
  3813. DebugAssert(!FAlternativeDelete);
  3814. FAlternativeDelete = Alternative;
  3815. try
  3816. {
  3817. if (!IsSideLocalBrowser(Side))
  3818. {
  3819. DebugAssert(Terminal != NULL);
  3820. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3821. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3822. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3823. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3824. int Params = FLAGMASK(Alternative, dfAlternative);
  3825. try
  3826. {
  3827. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3828. DebugAssert(FDeletedFiles == NULL);
  3829. FDeletedFiles = DeletedFiles.get();
  3830. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3831. FOnFileOperationFinished = FileDeleted;
  3832. FMoveToQueue = false;
  3833. Terminal->DeleteFiles(FileList, Params);
  3834. // Probably not needed for deleting, just for consistency with transfer code
  3835. if (FMoveToQueue)
  3836. {
  3837. Abort();
  3838. }
  3839. }
  3840. catch (EAbort &)
  3841. {
  3842. if (FMoveToQueue)
  3843. {
  3844. DeletedFiles->Sorted = true;
  3845. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3846. {
  3847. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3848. {
  3849. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3850. DebugAssert(Index == 0);
  3851. PermanentFileList->Delete(Index);
  3852. Index--;
  3853. }
  3854. }
  3855. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3856. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3857. AddQueueItem(Queue, QueueItem, Terminal);
  3858. ClearOperationSelection(osRemote);
  3859. }
  3860. }
  3861. }
  3862. else
  3863. {
  3864. if (IsLocalBrowserMode())
  3865. {
  3866. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3867. }
  3868. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3869. FProgressSide = FCurrentSide;
  3870. try
  3871. {
  3872. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3873. }
  3874. __finally
  3875. {
  3876. ReloadLocalDirectory();
  3877. }
  3878. }
  3879. FAlternativeDelete = false;
  3880. }
  3881. catch(...)
  3882. {
  3883. FAlternativeDelete = false;
  3884. DView->DiscardSavedSelection();
  3885. throw;
  3886. }
  3887. DView->RestoreSelection();
  3888. }
  3889. //---------------------------------------------------------------------------
  3890. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3891. {
  3892. DebugAssert(Terminal);
  3893. DebugAssert(!IsLocalBrowserMode());
  3894. RemoteDirView->SaveSelection();
  3895. RemoteDirView->SaveSelectedNames();
  3896. try
  3897. {
  3898. if (Lock)
  3899. {
  3900. Terminal->LockFiles(FileList);
  3901. }
  3902. else
  3903. {
  3904. Terminal->UnlockFiles(FileList);
  3905. }
  3906. }
  3907. catch(...)
  3908. {
  3909. RemoteDirView->DiscardSavedSelection();
  3910. throw;
  3911. }
  3912. RemoteDirView->RestoreSelection();
  3913. }
  3914. //---------------------------------------------------------------------------
  3915. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3916. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3917. bool NoConfirmation, bool Move)
  3918. {
  3919. DebugAssert(Terminal != NULL);
  3920. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3921. DebugAssert(!IsLocalBrowserMode());
  3922. if (Session == NULL)
  3923. {
  3924. Session = Terminal;
  3925. }
  3926. if (Session == Terminal)
  3927. {
  3928. if (RemoteDriveView->DropTarget != NULL)
  3929. {
  3930. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3931. }
  3932. else if (RemoteDirView->DropTarget != NULL)
  3933. {
  3934. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3935. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3936. }
  3937. else
  3938. {
  3939. Target = RemoteDirView->Path;
  3940. }
  3941. }
  3942. else
  3943. {
  3944. Target = GetSessionPath(Session, osRemote);
  3945. }
  3946. Target = UnixIncludeTrailingBackslash(Target);
  3947. if (FileList->Count == 1)
  3948. {
  3949. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3950. }
  3951. else
  3952. {
  3953. FileMask = AnyMask;
  3954. }
  3955. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3956. bool Result = true;
  3957. if (!NoConfirmation)
  3958. {
  3959. bool Multi = (FileList->Count > 1);
  3960. if (Move)
  3961. {
  3962. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3963. }
  3964. else
  3965. {
  3966. std::unique_ptr<TStrings> Sessions(new TStringList);
  3967. std::unique_ptr<TStrings> Directories(new TStringList);
  3968. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3969. for (int Index = 0; Index < SessionList->Count; Index++)
  3970. {
  3971. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3972. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3973. {
  3974. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3975. Directories->Add(GetSessionPath(ASession, osRemote));
  3976. }
  3977. }
  3978. TDirectRemoteCopy AllowDirectCopy;
  3979. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3980. {
  3981. DebugAssert(DirectCopy);
  3982. AllowDirectCopy = drcAllow;
  3983. }
  3984. else if (Terminal->IsCapable[fcSecondaryShell])
  3985. {
  3986. DebugAssert(DirectCopy);
  3987. AllowDirectCopy = drcConfirmCommandSession;
  3988. }
  3989. else
  3990. {
  3991. DebugAssert(!DirectCopy);
  3992. AllowDirectCopy = drcDisallow;
  3993. }
  3994. void * ASession = Session;
  3995. Result = DoRemoteCopyDialog(
  3996. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3997. Session = static_cast<TManagedTerminal *>(ASession);
  3998. }
  3999. }
  4000. return Result;
  4001. }
  4002. //---------------------------------------------------------------------------
  4003. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4004. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4005. {
  4006. DebugAssert(!IsLocalBrowserMode());
  4007. bool DirectCopy;
  4008. UnicodeString Target, FileMask;
  4009. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4010. if (Result)
  4011. {
  4012. if (!Move && !DirectCopy)
  4013. {
  4014. Configuration->Usage->Inc("RemoteCopyTemp");
  4015. UnicodeString RootTempDir;
  4016. UnicodeString TempDir;
  4017. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false);
  4018. TStrings * TemporaryFilesList = new TStringList();
  4019. try
  4020. {
  4021. TMakeLocalFileListParams MakeFileListParam;
  4022. MakeFileListParam.FileList = TemporaryFilesList;
  4023. MakeFileListParam.FileTimes = NULL;
  4024. MakeFileListParam.IncludeDirs = true;
  4025. MakeFileListParam.Recursive = false;
  4026. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4027. TTerminalManager::Instance()->ActiveSession = Session;
  4028. if (TemporaryFilesList->Count > 0)
  4029. {
  4030. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4031. CopyParam.FileMask = FileMask;
  4032. DebugAssert(!FAutoOperation);
  4033. FAutoOperation = true;
  4034. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4035. }
  4036. }
  4037. __finally
  4038. {
  4039. delete TemporaryFilesList;
  4040. FAutoOperation = false;
  4041. if (!RootTempDir.IsEmpty())
  4042. {
  4043. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4044. }
  4045. }
  4046. }
  4047. else
  4048. {
  4049. RemoteDirView->SaveSelection();
  4050. RemoteDirView->SaveSelectedNames();
  4051. try
  4052. {
  4053. if (Move)
  4054. {
  4055. Configuration->Usage->Inc("RemoteMove");
  4056. Terminal->MoveFiles(FileList, Target, FileMask);
  4057. }
  4058. else
  4059. {
  4060. Configuration->Usage->Inc("RemoteCopyDirect");
  4061. DebugAssert(DirectCopy);
  4062. DebugAssert(Session == Terminal);
  4063. if (Terminal->IsCapable[fcRemoteCopy] ||
  4064. Terminal->CommandSessionOpened ||
  4065. CommandSessionFallback())
  4066. {
  4067. Terminal->CopyFiles(FileList, Target, FileMask);
  4068. }
  4069. }
  4070. }
  4071. catch(...)
  4072. {
  4073. RemoteDirView->DiscardSavedSelection();
  4074. throw;
  4075. }
  4076. RemoteDirView->RestoreSelection();
  4077. }
  4078. }
  4079. return Result;
  4080. }
  4081. //---------------------------------------------------------------------------
  4082. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4083. const UnicodeString & Path, TRemoteProperties & Properties)
  4084. {
  4085. DebugAssert(!IsLocalBrowserMode());
  4086. Properties.Valid = Properties.Valid << vpEncrypt;
  4087. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4088. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4089. }
  4090. //---------------------------------------------------------------------------
  4091. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4092. {
  4093. Side = GetSide(Side);
  4094. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4095. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4096. UnicodeString Name = LoadStr(NEW_FOLDER);
  4097. int AllowedChanges =
  4098. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4099. bool SaveSettings = false;
  4100. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4101. {
  4102. TWindowLock Lock(this);
  4103. if (!IsSideLocalBrowser(Side))
  4104. {
  4105. if (SaveSettings)
  4106. {
  4107. GUIConfiguration->NewDirectoryProperties = Properties;
  4108. }
  4109. CreateRemoteDirectory(Name, Properties);
  4110. }
  4111. else
  4112. {
  4113. DirView(Side)->CreateDirectory(Name);
  4114. }
  4115. }
  4116. }
  4117. //---------------------------------------------------------------------------
  4118. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4119. {
  4120. TWindowLock Lock(this);
  4121. DirView(Side)->ExecuteHomeDirectory();
  4122. }
  4123. //---------------------------------------------------------------------------
  4124. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4125. {
  4126. DoOpenDirectoryDialog(odBrowse, Side);
  4127. }
  4128. //---------------------------------------------------------------------------
  4129. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4130. {
  4131. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4132. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4133. bool Result = !Path.IsEmpty();
  4134. if (Result)
  4135. {
  4136. TryOpenDirectory(Side, Path);
  4137. }
  4138. return Result;
  4139. }
  4140. //---------------------------------------------------------------------------
  4141. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4142. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4143. bool & Close)
  4144. {
  4145. // terminal can be already closed (e.g. dropped connection)
  4146. if (Terminal != NULL)
  4147. {
  4148. try
  4149. {
  4150. TCalculateSizeParams Params;
  4151. Params.Stats = &Stats;
  4152. Terminal->CalculateFilesSize(FileList, Size, Params);
  4153. }
  4154. catch(...)
  4155. {
  4156. if (!Terminal->Active)
  4157. {
  4158. Close = true;
  4159. }
  4160. throw;
  4161. }
  4162. }
  4163. }
  4164. //---------------------------------------------------------------------------
  4165. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4166. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4167. bool & Close)
  4168. {
  4169. // terminal can be already closed (e.g. dropped connection)
  4170. if (Terminal != NULL)
  4171. {
  4172. Configuration->Usage->Inc(L"ChecksumCalculated");
  4173. try
  4174. {
  4175. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4176. }
  4177. catch(...)
  4178. {
  4179. if (!Terminal->Active)
  4180. {
  4181. Close = true;
  4182. }
  4183. throw;
  4184. }
  4185. }
  4186. }
  4187. //---------------------------------------------------------------------------
  4188. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4189. {
  4190. if (Terminal->LoadFilesProperties(FileList))
  4191. {
  4192. RemoteDirView->Invalidate();
  4193. }
  4194. }
  4195. //---------------------------------------------------------------------------
  4196. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4197. {
  4198. bool Result;
  4199. if (!IsSideLocalBrowser(Side))
  4200. {
  4201. TRemoteTokenList * GroupList = NULL;
  4202. TRemoteTokenList * UserList = NULL;
  4203. try
  4204. {
  4205. TRemoteProperties CurrentProperties;
  4206. LoadFilesProperties(FileList);
  4207. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4208. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4209. if (CapableGroupChanging || CapableOwnerChanging)
  4210. {
  4211. if (CapableGroupChanging)
  4212. {
  4213. GroupList = Terminal->Groups->Duplicate();
  4214. }
  4215. if (CapableOwnerChanging)
  4216. {
  4217. UserList = Terminal->Users->Duplicate();
  4218. }
  4219. TRemoteDirectory * Files = Terminal->Files;
  4220. int Count = Files->Count;
  4221. if (Count > 100)
  4222. {
  4223. Count = 100;
  4224. }
  4225. for (int Index = 0; Index < Count; Index++)
  4226. {
  4227. TRemoteFile * File = Files->Files[Index];
  4228. if (CapableGroupChanging)
  4229. {
  4230. GroupList->AddUnique(File->Group);
  4231. }
  4232. if (CapableOwnerChanging)
  4233. {
  4234. UserList->AddUnique(File->Owner);
  4235. }
  4236. }
  4237. // if we haven't collected tokens for all files in current directory,
  4238. // make sure we collect them at least for all selected files.
  4239. // (note that so far the files in FileList has to be from current direcotry)
  4240. if (Count < Files->Count)
  4241. {
  4242. for (int Index = 0; Index < FileList->Count; Index++)
  4243. {
  4244. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4245. if (CapableGroupChanging)
  4246. {
  4247. GroupList->AddUnique(File->Group);
  4248. }
  4249. if (CapableOwnerChanging)
  4250. {
  4251. UserList->AddUnique(File->Owner);
  4252. }
  4253. }
  4254. }
  4255. }
  4256. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4257. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4258. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4259. {
  4260. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4261. {
  4262. CapableAclChanging = false;
  4263. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4264. }
  4265. }
  4266. int Flags = 0;
  4267. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4268. if (CapableAclChanging) Flags |= cpAcl;
  4269. if (CapableOwnerChanging) Flags |= cpOwner;
  4270. if (CapableGroupChanging) Flags |= cpGroup;
  4271. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4272. if (Terminal->IsCapable[fcCalculatingChecksum])
  4273. {
  4274. CalculateChecksumEvent = CalculateChecksum;
  4275. }
  4276. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4277. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4278. TRemoteProperties NewProperties = CurrentProperties;
  4279. Result =
  4280. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4281. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4282. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4283. CalculateSize, CalculateChecksumEvent);
  4284. if (Result)
  4285. {
  4286. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4287. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4288. }
  4289. }
  4290. __finally
  4291. {
  4292. delete GroupList;
  4293. delete UserList;
  4294. }
  4295. }
  4296. else
  4297. {
  4298. DirView(Side)->DisplayPropertiesMenu();
  4299. Result = true;
  4300. }
  4301. return Result;
  4302. }
  4303. //---------------------------------------------------------------------------
  4304. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4305. {
  4306. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4307. {
  4308. FIgnoreNextDialogChar = Key;
  4309. }
  4310. Key = 0;
  4311. }
  4312. //---------------------------------------------------------------------------
  4313. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4314. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4315. {
  4316. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4317. if (Command != NULL)
  4318. {
  4319. KeyProcessed(Key, Shift);
  4320. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4321. {
  4322. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4323. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4324. false, false, const_cast<TCustomCommandType *>(Command));
  4325. }
  4326. }
  4327. }
  4328. //---------------------------------------------------------------------------
  4329. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4330. {
  4331. if (!NonVisualDataModule->Busy)
  4332. {
  4333. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4334. {
  4335. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4336. }
  4337. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4338. {
  4339. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4340. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4341. {
  4342. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4343. // But so far it's not a problem, as it does what we want.
  4344. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4345. if (((Action->ShortCut == KeyShortCut) ||
  4346. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4347. AllowedAction(Action, aaShortCut))
  4348. {
  4349. // Has to be called before the action as the dialog char is already in queue.
  4350. // So when the action consumes message queue, we already need to have the
  4351. // FIgnoreNextDialogChar set
  4352. KeyProcessed(Key, Shift);
  4353. // Reset reference to previous component (when menu/toolbar was clicked).
  4354. // Needed to detect that action was invoked by keyboard shortcut
  4355. // in TNonVisualDataModule::ExplorerActionsExecute
  4356. Action->ActionComponent = NULL;
  4357. Action->Execute();
  4358. return;
  4359. }
  4360. }
  4361. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4362. {
  4363. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4364. {
  4365. KeyProcessed(Key, Shift);
  4366. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4367. return;
  4368. }
  4369. }
  4370. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4371. {
  4372. KeyProcessed(Key, Shift);
  4373. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4374. }
  4375. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4376. if (IsCustomShortCut(CustomShortCut))
  4377. {
  4378. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4379. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4380. if (WinConfiguration->SharedBookmarks != NULL)
  4381. {
  4382. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4383. if ((Bookmark != NULL) &&
  4384. OpenBookmark(FCurrentSide, Bookmark))
  4385. {
  4386. KeyProcessed(Key, Shift);
  4387. }
  4388. }
  4389. }
  4390. }
  4391. }
  4392. TForm::KeyDown(Key, Shift);
  4393. }
  4394. //---------------------------------------------------------------------------
  4395. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4396. {
  4397. bool OpenedTerminal = false;
  4398. UnicodeString ProtocolName;
  4399. UnicodeString ProtocolNameHint;
  4400. UnicodeString SecurityProtocolName;
  4401. UnicodeString DurationHint;
  4402. // See the comment in TScpCommanderForm::UpdateControls()
  4403. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4404. if (ActiveTerminal != NULL)
  4405. {
  4406. // Active is not enough here
  4407. if (ActiveTerminal->Status == ssOpened)
  4408. {
  4409. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4410. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4411. if (FileSystemInfo.ProtocolName.IsEmpty())
  4412. {
  4413. ProtocolName = SessionInfo.ProtocolName;
  4414. }
  4415. else
  4416. {
  4417. ProtocolName = FileSystemInfo.ProtocolName;
  4418. }
  4419. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4420. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4421. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4422. OpenedTerminal = true;
  4423. }
  4424. else if (ActiveTerminal->Status > ssClosed)
  4425. {
  4426. ProtocolName = LoadStr(STATUS_CONNECTING);
  4427. }
  4428. else
  4429. {
  4430. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4431. }
  4432. }
  4433. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4434. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4435. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4436. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4437. if (ProtocolStatusPanel->Caption != ProtocolName)
  4438. {
  4439. ProtocolStatusPanel->Caption = ProtocolName;
  4440. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4441. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4442. }
  4443. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4444. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4445. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4446. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4447. SecurityStatusPanel->Enabled = SecurityEnabled;
  4448. UnicodeString SecurityProtocolHint;
  4449. if (SecurityEnabled)
  4450. {
  4451. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4452. }
  4453. else
  4454. {
  4455. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4456. }
  4457. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4458. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4459. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4460. DurationStatusPanel->Hint = DurationHint;
  4461. UpdateStatusBar();
  4462. }
  4463. //---------------------------------------------------------------------------
  4464. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4465. {
  4466. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4467. DebugAssert(SessionStatusBar != NULL);
  4468. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4469. if (!FNote.IsEmpty())
  4470. {
  4471. NoteStatusPanel->Caption = FNote;
  4472. }
  4473. else
  4474. {
  4475. UpdateStatusPanelText(NoteStatusPanel);
  4476. }
  4477. NoteStatusPanel->Hint = FNoteHints;
  4478. UnicodeString LoginTime;
  4479. // See the comment in TScpCommanderForm::UpdateControls()
  4480. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4481. // Active is not enough here
  4482. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4483. {
  4484. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4485. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4486. }
  4487. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4488. DurationStatusPanel->Caption = LoginTime;
  4489. }
  4490. //---------------------------------------------------------------------------
  4491. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4492. {
  4493. Panel->Caption = L"";
  4494. }
  4495. //---------------------------------------------------------------------------
  4496. void __fastcall TCustomScpExplorerForm::Idle()
  4497. {
  4498. if (FShowing ||
  4499. // Particularly for
  4500. // - Standalone editing
  4501. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4502. // while "keeping remote directory up to date".
  4503. StandaloneOperation)
  4504. {
  4505. FEditorManager->Check();
  4506. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4507. // that needs to know if queue view is visible (and it may be closed after queue update)
  4508. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4509. }
  4510. if (FShowing)
  4511. {
  4512. if (!NonVisualDataModule->Busy &&
  4513. // Menu is opened or drag&drop is going on
  4514. (Mouse->Capture == NULL))
  4515. {
  4516. if (FRefreshRemoteDirectory)
  4517. {
  4518. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4519. {
  4520. Terminal->RefreshDirectory();
  4521. }
  4522. FRefreshRemoteDirectory = false;
  4523. }
  4524. if (FRefreshLocalDirectory)
  4525. {
  4526. ReloadLocalDirectory();
  4527. FRefreshLocalDirectory = false;
  4528. }
  4529. if (WinConfiguration->RefreshRemotePanel)
  4530. {
  4531. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4532. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4533. {
  4534. RemoteDirView->ReloadDirectory();
  4535. }
  4536. }
  4537. }
  4538. }
  4539. if (FShowing || StandaloneOperation)
  4540. {
  4541. if (FQueueStatusInvalidated)
  4542. {
  4543. UpdateQueueStatus(false);
  4544. }
  4545. RefreshQueueItems();
  4546. }
  4547. if (FShowing)
  4548. {
  4549. UpdateStatusBar();
  4550. }
  4551. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4552. {
  4553. FClipboardFakeMonitorsPendingReset = false;
  4554. FClipboardFakeMonitors.reset(NULL);
  4555. }
  4556. FIgnoreNextDialogChar = 0;
  4557. }
  4558. //---------------------------------------------------------------------------
  4559. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4560. {
  4561. try
  4562. {
  4563. FUserActionTimer->Enabled = false;
  4564. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4565. {
  4566. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4567. {
  4568. FPendingQueueActionItem->ProcessUserAction();
  4569. }
  4570. }
  4571. }
  4572. __finally
  4573. {
  4574. FPendingQueueActionItem = NULL;
  4575. }
  4576. }
  4577. //---------------------------------------------------------------------------
  4578. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4579. {
  4580. if (WinConfiguration->MinimizeToTray)
  4581. {
  4582. UpdateTrayIcon();
  4583. FTrayIcon->Visible = true;
  4584. if (Visible)
  4585. {
  4586. ShowWindow(Handle, SW_HIDE);
  4587. }
  4588. }
  4589. }
  4590. //---------------------------------------------------------------------------
  4591. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4592. {
  4593. // WORKAROUND
  4594. // When restoring maximized window from minimization,
  4595. // rarely some controls do not align properly.
  4596. // Two instances seen (both for Commander):
  4597. // - When restoring, window is temporarily narrower (not maximized),
  4598. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4599. // Once maximized already, top dock shinks back, but the session PageControl,
  4600. // do not align up, leaving space between TopDock and PageControl.
  4601. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4602. for (int Index = 0; Index < ControlCount; Index++)
  4603. {
  4604. RealignControl(Controls[Index]);
  4605. }
  4606. if (FTrayIcon->Visible)
  4607. {
  4608. FTrayIcon->Visible = false;
  4609. if (Visible)
  4610. {
  4611. ShowWindow(Handle, SW_SHOW);
  4612. }
  4613. }
  4614. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4615. {
  4616. FNeedSession = false;
  4617. NonVisualDataModule->StartBusy();
  4618. try
  4619. {
  4620. NeedSession(true);
  4621. }
  4622. __finally
  4623. {
  4624. NonVisualDataModule->EndBusy();
  4625. }
  4626. }
  4627. }
  4628. //---------------------------------------------------------------------------
  4629. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4630. {
  4631. FTrayIcon->Hint = GetMainForm()->Caption;
  4632. }
  4633. //---------------------------------------------------------------------------
  4634. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4635. {
  4636. UpdateTrayIcon();
  4637. }
  4638. //---------------------------------------------------------------------------
  4639. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4640. {
  4641. // workaround
  4642. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4643. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4644. // But WM_ACTIVATEAPP message can be received even when
  4645. // the main window is minimized to the tray and internal editor window is focused
  4646. // (after another application was previously active)
  4647. if (::IsIconic(Handle))
  4648. {
  4649. if (!IsAppIconic())
  4650. {
  4651. SetAppIconic(true);
  4652. }
  4653. }
  4654. ::ApplicationRestore();
  4655. Application->BringToFront();
  4656. }
  4657. //---------------------------------------------------------------------------
  4658. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4659. {
  4660. RestoreApp();
  4661. }
  4662. //---------------------------------------------------------------------------
  4663. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4664. {
  4665. if (OpenInNewWindow())
  4666. {
  4667. ExecuteNewInstance(SessionUrl);
  4668. }
  4669. else
  4670. {
  4671. TTerminalManager::Instance()->NewSession(SessionUrl);
  4672. }
  4673. }
  4674. //---------------------------------------------------------------------------
  4675. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4676. {
  4677. NewSession();
  4678. }
  4679. //---------------------------------------------------------------------------
  4680. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4681. {
  4682. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4683. StoredSessions->NewSession(SessionName, SessionData);
  4684. // modified only, explicit
  4685. StoredSessions->Save(false, true);
  4686. // encode session name because of slashes in hierarchical sessions
  4687. return EncodeUrlString(SessionName);
  4688. }
  4689. //---------------------------------------------------------------------------
  4690. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4691. {
  4692. // current working directories become defaults here, what is not right
  4693. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4694. return SaveHiddenDuplicateSession(SessionData.get());
  4695. }
  4696. //---------------------------------------------------------------------------
  4697. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4698. {
  4699. if (OpenInNewWindow())
  4700. {
  4701. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4702. }
  4703. else
  4704. {
  4705. // current working directories become defaults here, what is not right
  4706. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4707. TTerminalManager * Manager = TTerminalManager::Instance();
  4708. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4709. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4710. // We definitelly want these
  4711. ASession->Disconnected = ManagedSession->Disconnected;
  4712. // Not sure about these two
  4713. ASession->Permanent = ManagedSession->Permanent;
  4714. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4715. Manager->ActiveSession = ASession;
  4716. }
  4717. }
  4718. //---------------------------------------------------------------------------
  4719. void __fastcall TCustomScpExplorerForm::RenameSession()
  4720. {
  4721. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4722. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4723. // When submitting the default name, do not fix it
  4724. (Name != ManagedSession->SessionData->SessionName))
  4725. {
  4726. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4727. TSessionData::ValidateName(Name);
  4728. // This is inconsistent with how color (for example) is handled.
  4729. ManagedSession->SessionData->Name = Name;
  4730. UpdateControls();
  4731. // Add/Remove distinguishing paths from sessions of the same name.
  4732. SessionListChanged();
  4733. }
  4734. }
  4735. //---------------------------------------------------------------------------
  4736. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4737. {
  4738. DebugAssert(Queue != NULL);
  4739. bool Result = Queue->IsEmpty;
  4740. if (!Result)
  4741. {
  4742. SetFocus();
  4743. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4744. }
  4745. return Result;
  4746. }
  4747. //---------------------------------------------------------------------------
  4748. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4749. {
  4750. return CanCloseQueue(FQueue);
  4751. }
  4752. //---------------------------------------------------------------------------
  4753. void __fastcall TCustomScpExplorerForm::CloseSession()
  4754. {
  4755. if (CanCloseQueue())
  4756. {
  4757. TTerminalManager::Instance()->FreeActiveTerminal();
  4758. }
  4759. }
  4760. //---------------------------------------------------------------------------
  4761. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4762. {
  4763. if (CanCloseQueue())
  4764. {
  4765. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4766. }
  4767. }
  4768. //---------------------------------------------------------------------------
  4769. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4770. {
  4771. DetachTerminal(Terminal);
  4772. RemoteDirView->Terminal = Terminal;
  4773. UpdateRemotePathComboBox(false);
  4774. InitStatusBar();
  4775. UpdateControls();
  4776. }
  4777. //---------------------------------------------------------------------------
  4778. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4779. {
  4780. InitStatusBar();
  4781. }
  4782. //---------------------------------------------------------------------------
  4783. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4784. {
  4785. if (DebugAlwaysTrue(!Terminal->Active))
  4786. {
  4787. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4788. }
  4789. }
  4790. //---------------------------------------------------------------------------
  4791. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4792. {
  4793. if (OpenInNewWindow())
  4794. {
  4795. // encode session name because of slashes in hierarchical sessions
  4796. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4797. }
  4798. else
  4799. {
  4800. TTerminalManager * Manager = TTerminalManager::Instance();
  4801. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4802. Manager->ActiveSession = Terminal;
  4803. }
  4804. }
  4805. //---------------------------------------------------------------------------
  4806. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4807. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4808. {
  4809. TTerminalManager * Manager = TTerminalManager::Instance();
  4810. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4811. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4812. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4813. {
  4814. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4815. // FirstSession can be null, if some of the
  4816. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4817. {
  4818. FirstSession->Disconnected = true;
  4819. FirstSession->DisconnectedTemporarily = true;
  4820. }
  4821. Manager->ActiveSession = FirstSession;
  4822. }
  4823. }
  4824. //---------------------------------------------------------------------------
  4825. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4826. {
  4827. if (OpenInNewWindow())
  4828. {
  4829. ExecuteNewInstance(Name);
  4830. }
  4831. else
  4832. {
  4833. DoOpenFolderOrWorkspace(Name, true, false);
  4834. }
  4835. }
  4836. //---------------------------------------------------------------------------
  4837. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4838. bool &CanClose)
  4839. {
  4840. // See the comment in CloseApp()
  4841. if (NonVisualDataModule->Busy)
  4842. {
  4843. CanClose = false;
  4844. }
  4845. else
  4846. {
  4847. TTerminalManager * Manager = TTerminalManager::Instance();
  4848. int ActiveSessions = 0;
  4849. for (int Index = 0; Index < Manager->Count; Index++)
  4850. {
  4851. if (Manager->Sessions[Index]->Active)
  4852. {
  4853. ActiveSessions++;
  4854. }
  4855. }
  4856. // Confirm, when there's at least one active remote session or
  4857. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4858. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4859. WinConfiguration->ConfirmClosingSession)
  4860. {
  4861. unsigned int Result;
  4862. TMessageParams Params(mpNeverAskAgainCheck);
  4863. UnicodeString Message;
  4864. int Answers = qaOK | qaCancel;
  4865. // The current session is the only active session
  4866. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4867. {
  4868. if (!WinConfiguration->AutoSaveWorkspace)
  4869. {
  4870. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4871. }
  4872. else
  4873. {
  4874. Message = LoadStr(CLOSE_SESSION);
  4875. }
  4876. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4877. }
  4878. // Multiple active sessions or one active session, but it's not the current one
  4879. else if (ActiveSessions > 0)
  4880. {
  4881. if (!WinConfiguration->AutoSaveWorkspace)
  4882. {
  4883. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4884. }
  4885. else
  4886. {
  4887. Message = LoadStr(CLOSE_SESSIONS);
  4888. }
  4889. }
  4890. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4891. else
  4892. {
  4893. DebugAlwaysTrue(Manager->Count > 1);
  4894. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4895. Message = LoadStr(CLOSE_WORKSPACE);
  4896. }
  4897. UnicodeString Note;
  4898. if (WinConfiguration->AutoSaveWorkspace)
  4899. {
  4900. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4901. }
  4902. else
  4903. {
  4904. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4905. Answers = qaYes | qaNo | qaCancel;
  4906. }
  4907. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4908. SetFocus();
  4909. Result = MessageDialog(Message, qtConfirmation,
  4910. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4911. if (Result == qaNeverAskAgain)
  4912. {
  4913. WinConfiguration->ConfirmClosingSession = false;
  4914. }
  4915. if (Result == qaNo)
  4916. {
  4917. CanClose = SaveWorkspace(true);
  4918. // note that the workspace will be saved redundatly again from FormClose
  4919. }
  4920. else
  4921. {
  4922. CanClose =
  4923. (Result == qaOK) ||
  4924. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4925. (Result == qaNeverAskAgain);
  4926. }
  4927. }
  4928. if (CanClose && HasActiveTerminal())
  4929. {
  4930. CanClose = CanCloseQueue();
  4931. }
  4932. }
  4933. if (CanClose)
  4934. {
  4935. CanClose =
  4936. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4937. FEditorManager->CloseExternalFilesWithoutProcess();
  4938. if (CanClose)
  4939. {
  4940. while (CanClose && (FLocalEditors->Count > 0))
  4941. {
  4942. int PrevCount = FLocalEditors->Count;
  4943. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4944. CanClose = (FLocalEditors->Count < PrevCount);
  4945. }
  4946. if (CanClose)
  4947. {
  4948. CanClose = FEditorManager->Empty(true);
  4949. if (!CanClose)
  4950. {
  4951. SetFocus();
  4952. CanClose =
  4953. (MessageDialog(
  4954. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4955. }
  4956. }
  4957. }
  4958. }
  4959. }
  4960. //---------------------------------------------------------------------------
  4961. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4962. {
  4963. TForm * Form = dynamic_cast<TForm *>(Sender);
  4964. DebugAssert(Form != NULL);
  4965. Form->Close();
  4966. }
  4967. //---------------------------------------------------------------------------
  4968. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4969. {
  4970. TForm * Form = dynamic_cast<TForm *>(Sender);
  4971. delete Form;
  4972. }
  4973. //---------------------------------------------------------------------------
  4974. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4975. {
  4976. while (FLocalEditors->Count > 0)
  4977. {
  4978. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4979. }
  4980. }
  4981. //---------------------------------------------------------------------------
  4982. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4983. TObject *Sender)
  4984. {
  4985. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4986. try
  4987. {
  4988. SetProperties(osRemote, FileList);
  4989. }
  4990. __finally
  4991. {
  4992. delete FileList;
  4993. }
  4994. }
  4995. //---------------------------------------------------------------------------
  4996. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4997. {
  4998. if (value)
  4999. {
  5000. if (Component == fcCustomCommandsBand)
  5001. {
  5002. UpdateCustomCommandsToolbar();
  5003. }
  5004. if (Component == fcQueueFileList)
  5005. {
  5006. DebugAssert(!QueueFileListSplitter->Visible);
  5007. AdjustQueueLayout();
  5008. UpdateQueueFileList();
  5009. }
  5010. }
  5011. }
  5012. //---------------------------------------------------------------------------
  5013. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5014. {
  5015. int MinHeightLimit =
  5016. GetStaticQueuePanelComponentsHeight() +
  5017. QueueFileListSplitter->Height +
  5018. QueueFileList->Height +
  5019. GetMinQueueViewHeight();
  5020. if (QueuePanel->ClientHeight < MinHeightLimit)
  5021. {
  5022. QueuePanel->ClientHeight = MinHeightLimit;
  5023. }
  5024. }
  5025. //---------------------------------------------------------------------------
  5026. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5027. {
  5028. TControl * Control = GetComponent(Component);
  5029. DebugAssert(Control);
  5030. bool Changed = (Control->Visible != value);
  5031. if (Changed)
  5032. {
  5033. ComponentShowing(Component, value);
  5034. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5035. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5036. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5037. if (value)
  5038. {
  5039. int RemainingHeight = Control->Parent->ClientHeight;
  5040. int RemainingWidth = Control->Parent->ClientWidth;
  5041. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5042. {
  5043. TControl * ChildControl = Control->Parent->Controls[i];
  5044. if (ChildControl->Visible)
  5045. {
  5046. switch (ChildControl->Align)
  5047. {
  5048. case alTop:
  5049. case alBottom:
  5050. RemainingHeight -= ChildControl->Height;
  5051. break;
  5052. case alLeft:
  5053. case alRight:
  5054. RemainingWidth -= ChildControl->Width;
  5055. break;
  5056. }
  5057. }
  5058. }
  5059. int Reserve = ScaleByTextHeight(this, 32);
  5060. // queue in explorer, trees in commander
  5061. if (Control->Height > RemainingHeight - Reserve)
  5062. {
  5063. Control->Height = RemainingHeight / 2;
  5064. }
  5065. if (Control->Width > RemainingWidth - Reserve)
  5066. {
  5067. Control->Width = RemainingWidth / 2;
  5068. }
  5069. }
  5070. Control->Visible = value;
  5071. if (WasFocused && Visible)
  5072. {
  5073. DirView(osCurrent)->SetFocus();
  5074. }
  5075. FixControlsPlacement();
  5076. }
  5077. }
  5078. //---------------------------------------------------------------------------
  5079. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5080. {
  5081. TControl * Control = GetComponent(Component);
  5082. if (Control == NULL)
  5083. {
  5084. return false;
  5085. }
  5086. else
  5087. {
  5088. return Control->Visible;
  5089. }
  5090. }
  5091. //---------------------------------------------------------------------------
  5092. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5093. {
  5094. bool Result;
  5095. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5096. {
  5097. Result = !IsUWP();
  5098. }
  5099. else
  5100. {
  5101. Result = true;
  5102. }
  5103. return Result;
  5104. }
  5105. //---------------------------------------------------------------------------
  5106. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5107. {
  5108. if (DirView->ItemFocused != NULL)
  5109. {
  5110. DirView->ItemFocused->MakeVisible(false);
  5111. }
  5112. }
  5113. //---------------------------------------------------------------------------
  5114. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5115. {
  5116. MakeFocusedItemVisible(RemoteDirView);
  5117. QueueSplitter->Visible = QueuePanel->Visible;
  5118. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5119. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5120. TControl * QueueControlsOrder[] =
  5121. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5122. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5123. }
  5124. //---------------------------------------------------------------------------
  5125. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5126. {
  5127. switch (Component) {
  5128. case fcStatusBar: return RemoteStatusBar;
  5129. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5130. case fcQueueView: return QueuePanel;
  5131. case fcQueueToolbar: return QueueDock;
  5132. case fcRemoteTree: return RemoteDrivePanel;
  5133. case fcSessionsTabs: return SessionsPageControl;
  5134. case fcQueueFileList: return QueueFileList;
  5135. default: return NULL;
  5136. }
  5137. }
  5138. //---------------------------------------------------------------------------
  5139. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5140. TObject *Sender, TListColumn *Column, TPoint &Point)
  5141. {
  5142. DebugAssert(NonVisualDataModule && Column && Sender);
  5143. NonVisualDataModule->ListColumn = Column;
  5144. TPopupMenu * DirViewColumnMenu;
  5145. if (Sender == DirView(osOther))
  5146. {
  5147. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5148. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5149. (Column->Index == uvName);
  5150. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5151. (Column->Index == uvSize);
  5152. }
  5153. else
  5154. {
  5155. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5156. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5157. (Column->Index == dvName);
  5158. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5159. (Column->Index == dvSize);
  5160. }
  5161. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5162. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5163. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5164. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5165. }
  5166. //---------------------------------------------------------------------------
  5167. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5168. TObject *Sender, TListItem *Item, bool &AllowExec)
  5169. {
  5170. DoDirViewExecFile(Sender, Item, AllowExec);
  5171. }
  5172. //---------------------------------------------------------------------------
  5173. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5174. TListItem * Item, bool & AllowExec)
  5175. {
  5176. DebugAssert(Sender && Item && Configuration);
  5177. DebugAssert(AllowExec);
  5178. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5179. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5180. bool Remote = !IsSideLocalBrowser(Side);
  5181. DebugAssert(Item == ADirView->ItemFocused);
  5182. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5183. TResolvedDoubleClickAction Action;
  5184. if (FForceExecution)
  5185. {
  5186. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5187. }
  5188. else
  5189. {
  5190. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5191. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5192. }
  5193. if (Action == rdcaCopy)
  5194. {
  5195. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5196. UnlockWindow();
  5197. try
  5198. {
  5199. if (IsLocalBrowserMode())
  5200. {
  5201. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5202. }
  5203. else
  5204. {
  5205. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5206. }
  5207. }
  5208. __finally
  5209. {
  5210. LockWindow();
  5211. }
  5212. AllowExec = false;
  5213. }
  5214. else if (Action == rdcaEdit)
  5215. {
  5216. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5217. {
  5218. ExecuteFile(Side, efDefaultEditor);
  5219. AllowExec = false;
  5220. }
  5221. }
  5222. else if (Action == rdcaOpen)
  5223. {
  5224. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5225. {
  5226. ExecuteFile(osRemote, efShell);
  5227. AllowExec = false;
  5228. }
  5229. }
  5230. }
  5231. //---------------------------------------------------------------------------
  5232. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5233. {
  5234. return ((Side == osRemote) || (Side == osCurrent));
  5235. }
  5236. //---------------------------------------------------------------------------
  5237. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5238. {
  5239. DebugFail();
  5240. }
  5241. //---------------------------------------------------------------------------
  5242. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5243. {
  5244. DebugFail();
  5245. }
  5246. //---------------------------------------------------------------------------
  5247. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5248. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5249. {
  5250. TSynchronizeParamType Params;
  5251. Params.LocalDirectory = LocalDirectory;
  5252. Params.RemoteDirectory = RemoteDirectory;
  5253. int UnusedParams =
  5254. (GUIConfiguration->SynchronizeParams &
  5255. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5256. TTerminal::spCaseSensitive));
  5257. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5258. Params.Options = GUIConfiguration->SynchronizeOptions;
  5259. bool SaveSettings = false;
  5260. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5261. &DoSynchronizeTooManyDirectories);
  5262. DebugAssert(FSynchronizeController == NULL);
  5263. FSynchronizeController = &Controller;
  5264. bool Result;
  5265. try
  5266. {
  5267. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5268. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5269. int Options =
  5270. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5271. DebugAssert(FOnFeedSynchronizeError == NULL);
  5272. DebugAssert(FOnSynchronizeAbort == NULL);
  5273. DebugAssert(FSynchronizeTerminal == NULL);
  5274. FSynchronizeTerminal = Terminal;
  5275. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5276. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5277. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5278. if (Result)
  5279. {
  5280. if (SaveSettings)
  5281. {
  5282. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5283. GUIConfiguration->SynchronizeOptions = Params.Options;
  5284. }
  5285. else
  5286. {
  5287. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5288. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5289. FLAGSET(Params.Options, soSynchronize))
  5290. {
  5291. GUIConfiguration->SynchronizeOptions =
  5292. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5293. soSynchronize;
  5294. }
  5295. }
  5296. LocalDirectory = Params.LocalDirectory;
  5297. RemoteDirectory = Params.RemoteDirectory;
  5298. }
  5299. }
  5300. __finally
  5301. {
  5302. FSynchronizeController = NULL;
  5303. DebugAssert(FOnFeedSynchronizeError == NULL);
  5304. FOnFeedSynchronizeError = NULL;
  5305. DebugAssert(FOnSynchronizeAbort == NULL);
  5306. FOnSynchronizeAbort = NULL;
  5307. FSynchronizeTerminal = NULL;
  5308. }
  5309. return Result;
  5310. }
  5311. //---------------------------------------------------------------------------
  5312. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5313. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5314. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5315. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5316. TSynchronizeOptions * Options, bool Full)
  5317. {
  5318. // Keep-up-to-date mode
  5319. if (Terminal->Status == ssOpened)
  5320. {
  5321. try
  5322. {
  5323. int PParams = Params.Params;
  5324. if (!Full)
  5325. {
  5326. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5327. if (FLAGSET(Params.Options, soRecurse))
  5328. {
  5329. PParams |= TTerminal::spSubDirs;
  5330. }
  5331. }
  5332. else
  5333. {
  5334. // if keepuptodate is non-recursive,
  5335. // full sync before has to be non-recursive as well
  5336. if (FLAGCLEAR(Params.Options, soRecurse))
  5337. {
  5338. PParams |= TTerminal::spNoRecurse;
  5339. }
  5340. }
  5341. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5342. PParams, Checklist, Options);
  5343. }
  5344. catch (Exception & E)
  5345. {
  5346. if (!Terminal->Active)
  5347. {
  5348. ShowExtendedExceptionEx(Terminal, &E);
  5349. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5350. if (!Terminal->Active)
  5351. {
  5352. throw;
  5353. }
  5354. }
  5355. else
  5356. {
  5357. if (FLAGSET(Params.Options, soContinueOnError) &&
  5358. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5359. {
  5360. // noop, already logged in MoreMessageDialog
  5361. }
  5362. else
  5363. {
  5364. // We get mostly EAbort here, so this is noop.
  5365. // Exception is an error when listing directories while looking for differences.
  5366. ShowExtendedExceptionEx(Terminal, &E);
  5367. throw;
  5368. }
  5369. }
  5370. }
  5371. }
  5372. }
  5373. //---------------------------------------------------------------------------
  5374. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5375. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5376. const UnicodeString ErrorStr)
  5377. {
  5378. if (!Directory.IsEmpty())
  5379. {
  5380. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5381. }
  5382. else
  5383. {
  5384. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5385. }
  5386. }
  5387. //---------------------------------------------------------------------------
  5388. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5389. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5390. {
  5391. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5392. {
  5393. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5394. }
  5395. else
  5396. {
  5397. TMessageParams Params(mpNeverAskAgainCheck);
  5398. unsigned int Result = MessageDialog(
  5399. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5400. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5401. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5402. {
  5403. MaxDirectories *= 2;
  5404. if (Result == qaNeverAskAgain)
  5405. {
  5406. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5407. }
  5408. }
  5409. else
  5410. {
  5411. Abort();
  5412. }
  5413. }
  5414. }
  5415. //---------------------------------------------------------------------------
  5416. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5417. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5418. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5419. TSynchronizeOptions * Options)
  5420. {
  5421. // Keep-up-to-date mode
  5422. DebugAssert(!FAutoOperation);
  5423. void * BatchStorage;
  5424. BatchStart(BatchStorage);
  5425. FAutoOperation = true;
  5426. bool AnyOperation = false;
  5427. TDateTime StartTime = Now();
  5428. TSynchronizeChecklist * AChecklist = NULL;
  5429. try
  5430. {
  5431. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5432. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5433. {
  5434. FSynchronizeProgressForm->Start();
  5435. }
  5436. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5437. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5438. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5439. Options);
  5440. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5441. SAFE_DESTROY(FSynchronizeProgressForm);
  5442. AnyOperation = (AChecklist->CheckedCount > 0);
  5443. if (AnyOperation)
  5444. {
  5445. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5446. CreateProgressForm(&SynchronizeProgress);
  5447. try
  5448. {
  5449. Terminal->SynchronizeApply(
  5450. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5451. SynchronizeProcessedItem, NULL, NULL, NULL);
  5452. }
  5453. __finally
  5454. {
  5455. DestroyProgressForm();
  5456. }
  5457. }
  5458. }
  5459. __finally
  5460. {
  5461. if (Checklist == NULL)
  5462. {
  5463. delete AChecklist;
  5464. }
  5465. else
  5466. {
  5467. *Checklist = AChecklist;
  5468. }
  5469. FAutoOperation = false;
  5470. SAFE_DESTROY(FSynchronizeProgressForm);
  5471. BatchEnd(BatchStorage);
  5472. ReloadLocalDirectory();
  5473. if (AnyOperation)
  5474. {
  5475. OperationComplete(StartTime);
  5476. }
  5477. }
  5478. }
  5479. //---------------------------------------------------------------------------
  5480. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5481. {
  5482. // can be called from command line
  5483. return Visible &&
  5484. ((DirView(osRemote)->SelCount > 0) ||
  5485. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5486. }
  5487. //---------------------------------------------------------------------------
  5488. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5489. {
  5490. LogSynchronizeEvent(Terminal, Message);
  5491. }
  5492. //---------------------------------------------------------------------------
  5493. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5494. int Params, TSynchronizeOptions & Options)
  5495. {
  5496. DebugAssert(!IsLocalBrowserMode());
  5497. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5498. {
  5499. Options.Filter = new TStringList();
  5500. Options.Filter->CaseSensitive = false;
  5501. Options.Filter->Duplicates = Types::dupAccept;
  5502. if (DirView(osRemote)->SelCount > 0)
  5503. {
  5504. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5505. }
  5506. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5507. {
  5508. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5509. }
  5510. Options.Filter->Sort();
  5511. }
  5512. }
  5513. //---------------------------------------------------------------------------
  5514. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5515. {
  5516. TCopyParamType Defaults;
  5517. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5518. ConfigStorage->AccessMode = smRead;
  5519. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5520. {
  5521. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5522. }
  5523. ConfigStorage.reset(NULL);
  5524. std::unique_ptr<TStringList> Options(new TStringList());
  5525. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5526. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5527. UnicodeString Result;
  5528. if (Options->Count > 0)
  5529. {
  5530. Result =
  5531. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5532. }
  5533. return Result;
  5534. }
  5535. //---------------------------------------------------------------------------
  5536. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5537. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5538. {
  5539. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5540. UnicodeString AdditionalParams =
  5541. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5542. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5543. SerializeCopyParamForCommandLine(CopyParams),
  5544. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5545. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5546. ExecuteNewInstance(SessionName, AdditionalParams);
  5547. }
  5548. //---------------------------------------------------------------------------
  5549. struct TSynchronizeParams
  5550. {
  5551. UnicodeString LocalDirectory;
  5552. UnicodeString RemoteDirectory;
  5553. TSynchronizeMode Mode;
  5554. TSynchronizeChecklist * Checklist;
  5555. int Params;
  5556. TCopyParamType * CopyParam;
  5557. TDateTime CollectElapsed;
  5558. TDateTime * StartTime;
  5559. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5560. };
  5561. //---------------------------------------------------------------------------
  5562. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5563. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5564. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5565. {
  5566. DebugAssert(!FAutoOperation);
  5567. void * BatchStorage;
  5568. BatchStart(BatchStorage);
  5569. FAutoOperation = true;
  5570. TFileOperationStatistics Statistics;
  5571. TDateTime Start = Now();
  5572. try
  5573. {
  5574. Params.OnProcessedItem = OnProcessedItem;
  5575. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5576. CreateProgressForm(&SynchronizeProgress);
  5577. Terminal->SynchronizeApply(
  5578. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5579. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5580. &Statistics);
  5581. }
  5582. __finally
  5583. {
  5584. Params.OnProcessedItem = NULL;
  5585. FAutoOperation = false;
  5586. DestroyProgressForm();
  5587. BatchEnd(BatchStorage);
  5588. ReloadLocalDirectory();
  5589. }
  5590. if (WinConfiguration->SynchronizeSummary)
  5591. {
  5592. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5593. // The statistics should be 0 anyway in this case
  5594. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5595. {
  5596. Message += L"\n";
  5597. if (Statistics.FilesUploaded > 0)
  5598. {
  5599. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5600. }
  5601. if (Statistics.FilesDownloaded > 0)
  5602. {
  5603. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5604. }
  5605. if (Statistics.FilesDeletedLocal > 0)
  5606. {
  5607. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5608. }
  5609. if (Statistics.FilesDeletedRemote > 0)
  5610. {
  5611. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5612. }
  5613. }
  5614. TDateTime Elapsed = (Now() - Start);
  5615. Message +=
  5616. L"\n" +
  5617. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5618. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5619. TMessageParams Params(mpNeverAskAgainCheck);
  5620. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5621. if (Result == qaNeverAskAgain)
  5622. {
  5623. WinConfiguration->SynchronizeSummary = false;
  5624. }
  5625. }
  5626. }
  5627. //---------------------------------------------------------------------------
  5628. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5629. {
  5630. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5631. {
  5632. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5633. }
  5634. // Not set in keep-up-to-date mode - Synchronize() method
  5635. if (Token != NULL)
  5636. {
  5637. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5638. if (Params.OnProcessedItem != NULL)
  5639. {
  5640. Params.OnProcessedItem(NULL, ChecklistItem);
  5641. }
  5642. }
  5643. }
  5644. //---------------------------------------------------------------------------
  5645. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5646. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5647. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5648. {
  5649. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5650. *Params.StartTime = Now();
  5651. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5652. }
  5653. //---------------------------------------------------------------------------
  5654. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5655. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5656. {
  5657. // terminal can be already closed (e.g. dropped connection)
  5658. if (Terminal != NULL)
  5659. {
  5660. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5661. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5662. }
  5663. }
  5664. //---------------------------------------------------------------------------
  5665. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5666. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5667. {
  5668. DebugAssert(!IsLocalBrowserMode());
  5669. TAutoBatch AutoBatch(this);
  5670. TAutoFlag AutoOperationFlag(FAutoOperation);
  5671. if (Side == osRemote)
  5672. {
  5673. std::unique_ptr<TStrings> FileList(new TStringList());
  5674. FileList->AddObject(FileName, RemoteFile);
  5675. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5676. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5677. RemoteDirView->SaveSelection();
  5678. RemoteDirView->SaveSelectedNames();
  5679. try
  5680. {
  5681. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5682. }
  5683. catch(...)
  5684. {
  5685. RemoteDirView->DiscardSavedSelection();
  5686. throw;
  5687. }
  5688. RemoteDirView->RestoreSelection();
  5689. }
  5690. else if (DebugAlwaysTrue(Side == osLocal))
  5691. {
  5692. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5693. {
  5694. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5695. }
  5696. UnicodeString Directory = ExtractFileDir(FileName);
  5697. ReloadLocalDirectory(Directory);
  5698. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5699. if (!SamePaths(Directory, NewDirectory))
  5700. {
  5701. ReloadLocalDirectory(NewDirectory);
  5702. }
  5703. }
  5704. }
  5705. //---------------------------------------------------------------------------
  5706. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5707. {
  5708. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5709. if (Side == osLocal)
  5710. {
  5711. if (Action == TSynchronizeChecklist::saDownloadNew)
  5712. {
  5713. OpenFolderInExplorer(LocalPath);
  5714. }
  5715. else
  5716. {
  5717. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5718. }
  5719. }
  5720. else if (DebugAlwaysTrue(Side == osRemote))
  5721. {
  5722. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5723. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5724. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5725. if (!LocalPath.IsEmpty())
  5726. {
  5727. SessionData->LocalDirectory = LocalPath;
  5728. }
  5729. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5730. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5731. }
  5732. }
  5733. //---------------------------------------------------------------------------
  5734. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5735. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5736. const TCopyParamType * CopyParams)
  5737. {
  5738. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5739. UnicodeString AdditionalParams =
  5740. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5741. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5742. SerializeCopyParamForCommandLine(CopyParams),
  5743. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5744. LocalDirectory, RemoteDirectory, Mode, Params));
  5745. ExecuteNewInstance(SessionName, AdditionalParams);
  5746. }
  5747. //---------------------------------------------------------------------------
  5748. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5749. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5750. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5751. {
  5752. int Result;
  5753. bool SaveSettings = false;
  5754. int Options =
  5755. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5756. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5757. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5758. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5759. bool Continue =
  5760. (UseDefaults == 0) ||
  5761. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5762. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5763. if (Continue)
  5764. {
  5765. Configuration->Usage->Inc(L"Synchronizations");
  5766. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5767. UpdateCopyParamCounters(CopyParam);
  5768. TSynchronizeOptions SynchronizeOptions;
  5769. GetSynchronizeOptions(Params, SynchronizeOptions);
  5770. if (SaveSettings)
  5771. {
  5772. GUIConfiguration->SynchronizeParams = Params;
  5773. }
  5774. else
  5775. {
  5776. SaveMode = false;
  5777. }
  5778. TDateTime StartTime = Now();
  5779. TSynchronizeChecklist * Checklist = NULL;
  5780. try
  5781. {
  5782. DebugAssert(!FAutoOperation);
  5783. FAutoOperation = true;
  5784. try
  5785. {
  5786. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5787. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5788. int Files = -1;
  5789. if (DataList->Count >= 1)
  5790. {
  5791. Files = StrToIntDef(DataList->Strings[0], Files);
  5792. }
  5793. else
  5794. {
  5795. DataList->Add(UnicodeString());
  5796. }
  5797. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5798. FSynchronizeProgressForm->Start();
  5799. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5800. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5801. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5802. &SynchronizeOptions);
  5803. if (Terminal->SessionData->CacheDirectories)
  5804. {
  5805. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5806. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5807. }
  5808. }
  5809. __finally
  5810. {
  5811. FAutoOperation = false;
  5812. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5813. SAFE_DESTROY(FSynchronizeProgressForm);
  5814. }
  5815. TSynchronizeParams SynchronizeParams;
  5816. SynchronizeParams.LocalDirectory = LocalDirectory;
  5817. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5818. SynchronizeParams.Mode = Mode;
  5819. SynchronizeParams.CopyParam = &CopyParam;
  5820. SynchronizeParams.Params = Params;
  5821. SynchronizeParams.Checklist = Checklist;
  5822. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5823. SynchronizeParams.StartTime = &StartTime;
  5824. SynchronizeParams.OnProcessedItem = NULL;
  5825. Result = Checklist->Count;
  5826. if (Checklist->Count > 0)
  5827. {
  5828. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5829. {
  5830. if (!DoSynchronizeChecklistDialog(
  5831. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5832. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5833. {
  5834. Result = -1;
  5835. }
  5836. }
  5837. else
  5838. {
  5839. FullSynchronize(SynchronizeParams, NULL, NULL);
  5840. }
  5841. }
  5842. else
  5843. {
  5844. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5845. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5846. }
  5847. }
  5848. __finally
  5849. {
  5850. delete Checklist;
  5851. }
  5852. OperationComplete(StartTime);
  5853. }
  5854. else
  5855. {
  5856. Result = -1;
  5857. }
  5858. return Result;
  5859. }
  5860. //---------------------------------------------------------------------------
  5861. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5862. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5863. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5864. {
  5865. if (Collect)
  5866. {
  5867. DebugAssert(FSynchronizeProgressForm != NULL);
  5868. if (!FSynchronizeProgressForm->Started)
  5869. {
  5870. FSynchronizeProgressForm->Start();
  5871. }
  5872. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5873. SetTaskbarListProgressValue(CompareProgress);
  5874. }
  5875. else
  5876. {
  5877. DebugAssert(FSynchronizeProgressForm == NULL);
  5878. DebugAssert(FProgressForm != NULL);
  5879. }
  5880. }
  5881. //---------------------------------------------------------------------------
  5882. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5883. {
  5884. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5885. TRemoteFile * File = NULL;
  5886. Terminal->ReadFile(FullFileName, File);
  5887. if (File != NULL)
  5888. {
  5889. std::unique_ptr<TRemoteFile> FileOwner(File);
  5890. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5891. Application->ShowMainForm = false;
  5892. Application->Run();
  5893. }
  5894. }
  5895. //---------------------------------------------------------------------------
  5896. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5897. {
  5898. DebugFail();
  5899. }
  5900. //---------------------------------------------------------------------------
  5901. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5902. {
  5903. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5904. SessionData->Assign(ManagedSession->SessionData);
  5905. UpdateSessionData(SessionData.get());
  5906. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5907. if (IsActiveTerminal(ManagedSession))
  5908. {
  5909. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5910. }
  5911. if (!ManagedSession->SessionData->HasSessionName())
  5912. {
  5913. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5914. // once IsWorkspace is cleared
  5915. SessionData->Name = UnicodeString();
  5916. }
  5917. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5918. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5919. SessionData->IsWorkspace = false;
  5920. return SessionData.release();
  5921. }
  5922. //---------------------------------------------------------------------------
  5923. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5924. {
  5925. TSessionData * SessionData = CloneCurrentSessionData();
  5926. try
  5927. {
  5928. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5929. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5930. }
  5931. __finally
  5932. {
  5933. delete SessionData;
  5934. }
  5935. }
  5936. //---------------------------------------------------------------------------
  5937. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5938. {
  5939. TTerminalManager * Manager = TTerminalManager::Instance();
  5940. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5941. if (DebugAlwaysTrue(ManagedSession != NULL))
  5942. {
  5943. // Update ManagedSession->StateData
  5944. UpdateSession(ManagedSession);
  5945. }
  5946. Manager->SaveWorkspace(DataList.get());
  5947. return DataList.release();
  5948. }
  5949. //---------------------------------------------------------------------------
  5950. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5951. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5952. {
  5953. WinConfiguration->LastWorkspace = Name;
  5954. if (!SavePasswords)
  5955. {
  5956. for (int Index = 0; Index < DataList->Count; Index++)
  5957. {
  5958. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5959. if (SessionData->Link.IsEmpty())
  5960. {
  5961. SessionData->ClearSessionPasswords();
  5962. }
  5963. }
  5964. }
  5965. StoredSessions->NewWorkspace(Name, DataList);
  5966. // modified only
  5967. StoredSessions->Save(false, Explicit);
  5968. }
  5969. //---------------------------------------------------------------------------
  5970. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5971. {
  5972. return
  5973. DefaultStr(WinConfiguration->LastWorkspace,
  5974. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5975. }
  5976. //---------------------------------------------------------------------------
  5977. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5978. {
  5979. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5980. bool AnyNonStoredSessionWithPassword = false;
  5981. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5982. bool AllNonStoredSessionsAnonymous = true;
  5983. TTerminalManager * Manager = TTerminalManager::Instance();
  5984. for (int Index = 0; Index < DataList->Count; Index++)
  5985. {
  5986. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5987. if (SessionData->Link.IsEmpty())
  5988. {
  5989. if (SessionData->HasAnySessionPassword())
  5990. {
  5991. AnyNonStoredSessionWithPassword = true;
  5992. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5993. {
  5994. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5995. }
  5996. }
  5997. if (!SameText(SessionData->UserName, AnonymousUserName))
  5998. {
  5999. AllNonStoredSessionsAnonymous = false;
  6000. }
  6001. }
  6002. }
  6003. bool SavePasswords;
  6004. bool * PSavePasswords;
  6005. bool NotRecommendedSavingPasswords =
  6006. !CustomWinConfiguration->UseMasterPassword &&
  6007. !AllNonStoredSessionsAnonymous;
  6008. if (Configuration->DisablePasswordStoring ||
  6009. !AnyNonStoredSessionWithPassword)
  6010. {
  6011. PSavePasswords = NULL;
  6012. SavePasswords = false;
  6013. }
  6014. else
  6015. {
  6016. PSavePasswords = &SavePasswords;
  6017. SavePasswords =
  6018. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6019. !NotRecommendedSavingPasswords;
  6020. }
  6021. UnicodeString Name = WorkspaceName();
  6022. bool CreateShortcut = false;
  6023. bool Result =
  6024. DoSaveWorkspaceDialog(
  6025. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6026. EnableAutoSave);
  6027. if (Result)
  6028. {
  6029. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6030. if (CreateShortcut)
  6031. {
  6032. TOperationVisualizer Visualizer;
  6033. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6034. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6035. }
  6036. if (EnableAutoSave)
  6037. {
  6038. WinConfiguration->AutoSaveWorkspace = true;
  6039. WinConfiguration->AutoWorkspace = Name;
  6040. if (PSavePasswords != NULL)
  6041. {
  6042. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6043. }
  6044. }
  6045. }
  6046. return Result;
  6047. }
  6048. //---------------------------------------------------------------------------
  6049. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6050. {
  6051. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6052. DebugAssert(Session == ManagedSession);
  6053. if (!IsLocalBrowserMode())
  6054. {
  6055. SAFE_DESTROY(Session->RemoteExplorerState);
  6056. if (WinConfiguration->PreservePanelState)
  6057. {
  6058. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6059. }
  6060. }
  6061. UpdateSessionData(Session->StateData);
  6062. }
  6063. //---------------------------------------------------------------------------
  6064. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6065. {
  6066. // Keep in sync with TSessionData::CopyStateData
  6067. DebugAssert(Data != NULL);
  6068. // This is inconsistent with how color (for example) is handled.
  6069. Data->Name = ManagedSession->SessionData->Name;
  6070. if (!IsLocalBrowserMode())
  6071. {
  6072. // cannot use RemoteDirView->Path, because it is empty if connection
  6073. // was already closed
  6074. // also only peek, we may not be connected at all atm
  6075. // so make sure we do not try retrieving current directory from the server
  6076. // (particularly with FTP)
  6077. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6078. if (!ACurrentDirectory.IsEmpty())
  6079. {
  6080. Data->RemoteDirectory = ACurrentDirectory;
  6081. }
  6082. }
  6083. Data->Color = SessionColor;
  6084. }
  6085. //---------------------------------------------------------------------------
  6086. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6087. {
  6088. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6089. DebugAssert(Toolbar != NULL);
  6090. for (int i = 0; i < Toolbar->Items->Count; i++)
  6091. {
  6092. TTBXCustomDropDownItem * DropDownItem;
  6093. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6094. if (DropDownItem != NULL)
  6095. {
  6096. ToolbarItemResize(DropDownItem,
  6097. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6098. Toolbar->NonClientWidth);
  6099. break;
  6100. }
  6101. }
  6102. }
  6103. //---------------------------------------------------------------------------
  6104. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6105. {
  6106. Item->EditWidth = Width;
  6107. }
  6108. //---------------------------------------------------------------------------
  6109. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6110. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6111. {
  6112. for (int i = 0; i < Toolbar->Items->Count; i++)
  6113. {
  6114. TTBXCustomDropDownItem * DropDownItem;
  6115. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6116. if (DropDownItem != NULL)
  6117. {
  6118. ASize.x -= DropDownItem->EditWidth;
  6119. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6120. }
  6121. }
  6122. }
  6123. //---------------------------------------------------------------------------
  6124. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6125. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6126. {
  6127. if (WinConfiguration->DDWarnLackOfTempSpace)
  6128. {
  6129. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6130. if (!ADrive.IsEmpty())
  6131. {
  6132. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6133. DebugAssert(RequiredSpace >= 0);
  6134. __int64 RequiredWithReserve;
  6135. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6136. if (FreeSpace < RequiredWithReserve)
  6137. {
  6138. unsigned int Result;
  6139. TMessageParams Params(mpNeverAskAgainCheck);
  6140. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6141. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6142. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6143. if (Result == qaNeverAskAgain)
  6144. {
  6145. WinConfiguration->DDWarnLackOfTempSpace = false;
  6146. }
  6147. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6148. }
  6149. }
  6150. }
  6151. }
  6152. //---------------------------------------------------------------------------
  6153. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6154. {
  6155. DoOpenDirectoryDialog(odAddBookmark, Side);
  6156. }
  6157. //---------------------------------------------------------------------------
  6158. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6159. {
  6160. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6161. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6162. try
  6163. {
  6164. TCustomDirView * DView = DirView(Side);
  6165. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6166. {
  6167. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6168. }
  6169. }
  6170. catch (...)
  6171. {
  6172. delete VisitedDirectories;
  6173. throw;
  6174. }
  6175. return VisitedDirectories;
  6176. }
  6177. //---------------------------------------------------------------------------
  6178. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6179. TOpenDirectoryMode Mode, TOperationSide Side)
  6180. {
  6181. bool Continue = true;
  6182. if (Mode == odAddBookmark)
  6183. {
  6184. TMessageParams Params(mpNeverAskAgainCheck);
  6185. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6186. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6187. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6188. unsigned int Answer =
  6189. MessageDialog(Message,
  6190. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6191. if (Answer == qaNeverAskAgain)
  6192. {
  6193. Continue = true;
  6194. WinConfiguration->UseSharedBookmarks = true;
  6195. }
  6196. else if (Answer == qaOK)
  6197. {
  6198. Continue = true;
  6199. WinConfiguration->UseSharedBookmarks = false;
  6200. }
  6201. else
  6202. {
  6203. Continue = false;
  6204. }
  6205. }
  6206. if (Continue)
  6207. {
  6208. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6209. try
  6210. {
  6211. UnicodeString Name = DirView(Side)->PathName;
  6212. bool Repeat;
  6213. do
  6214. {
  6215. Repeat = false;
  6216. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6217. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6218. // do not allow switching to location profiles,
  6219. // if we are not connected
  6220. HasDirView[osLocal] && (Terminal != NULL)))
  6221. {
  6222. TWindowLock Lock(this);
  6223. if (!TryOpenDirectory(Side, Name))
  6224. {
  6225. Repeat = true;
  6226. Mode = odBrowse;
  6227. }
  6228. }
  6229. }
  6230. while (Repeat);
  6231. }
  6232. __finally
  6233. {
  6234. delete VisitedDirectories;
  6235. }
  6236. }
  6237. }
  6238. //---------------------------------------------------------------------------
  6239. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6240. {
  6241. DebugAssert(!Terminal->CommandSessionOpened);
  6242. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6243. }
  6244. //---------------------------------------------------------------------------
  6245. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6246. {
  6247. bool Result = Terminal->IsCapable[Capability] ||
  6248. Terminal->CommandSessionOpened;
  6249. if (!Result)
  6250. {
  6251. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6252. if (!GUIConfiguration->ConfirmCommandSession)
  6253. {
  6254. Result = true;
  6255. }
  6256. else
  6257. {
  6258. TMessageParams Params(mpNeverAskAgainCheck);
  6259. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6260. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6261. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6262. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6263. if (Answer == qaNeverAskAgain)
  6264. {
  6265. GUIConfiguration->ConfirmCommandSession = false;
  6266. Result = true;
  6267. }
  6268. else if (Answer == qaOK)
  6269. {
  6270. Result = true;
  6271. }
  6272. }
  6273. if (Result)
  6274. {
  6275. Result = CommandSessionFallback();
  6276. }
  6277. }
  6278. return Result;
  6279. }
  6280. //---------------------------------------------------------------------------
  6281. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6282. {
  6283. if (EnsureCommandSessionFallback(fcAnyCommand))
  6284. {
  6285. DoConsoleDialog(Terminal, Command);
  6286. }
  6287. }
  6288. //---------------------------------------------------------------------------
  6289. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6290. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6291. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6292. {
  6293. if (!IsSideLocalBrowser(osRemote) &&
  6294. IsFileControl(DropSourceControl, osRemote) &&
  6295. (FDDExtMapFile != NULL))
  6296. {
  6297. Accept = true;
  6298. }
  6299. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6300. DebugAssert(FDDTargetControl != NULL);
  6301. }
  6302. //---------------------------------------------------------------------------
  6303. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6304. _di_IDataObject DataObj, int KeyState,
  6305. const TPoint & Point, int & Effect, bool & Accept)
  6306. {
  6307. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6308. }
  6309. //---------------------------------------------------------------------------
  6310. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6311. _di_IDataObject DataObj, int KeyState,
  6312. const TPoint & Point, int & Effect, bool & Accept)
  6313. {
  6314. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6315. }
  6316. //---------------------------------------------------------------------------
  6317. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6318. TObject * Sender)
  6319. {
  6320. DebugUsedParam(Sender);
  6321. DebugAssert(FDDTargetControl == Sender);
  6322. FDDTargetControl = NULL;
  6323. }
  6324. //---------------------------------------------------------------------------
  6325. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6326. {
  6327. FileControlDDDragLeave(SessionsPageControl);
  6328. }
  6329. //---------------------------------------------------------------------------
  6330. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6331. {
  6332. FileControlDDDragLeave(QueueView3);
  6333. }
  6334. //---------------------------------------------------------------------------
  6335. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6336. {
  6337. DebugAssert(!IsSideLocalBrowser(Side));
  6338. DebugUsedParam(Side);
  6339. bool Edit = false;
  6340. TRemoteFile * File = NULL;
  6341. UnicodeString FileName;
  6342. UnicodeString PointTo;
  6343. bool SymbolicLink = true;
  6344. if (RemoteDirView->ItemFocused)
  6345. {
  6346. DebugAssert(RemoteDirView->ItemFocused->Data);
  6347. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6348. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6349. if (Edit)
  6350. {
  6351. FileName = File->FileName;
  6352. PointTo = File->LinkTo;
  6353. }
  6354. else
  6355. {
  6356. PointTo = File->FileName;
  6357. }
  6358. }
  6359. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6360. Terminal->IsCapable[fcHardLink]))
  6361. {
  6362. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6363. if (Edit)
  6364. {
  6365. DebugAssert(File->FileName == FileName);
  6366. int Params = dfNoRecursive;
  6367. Terminal->ExceptionOnFail = true;
  6368. try
  6369. {
  6370. Terminal->DeleteFile(L"", File, &Params);
  6371. }
  6372. __finally
  6373. {
  6374. Terminal->ExceptionOnFail = false;
  6375. }
  6376. }
  6377. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6378. }
  6379. }
  6380. //---------------------------------------------------------------------------
  6381. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6382. {
  6383. return
  6384. (IsSideLocalBrowser(Side) ||
  6385. (HasActiveTerminal() &&
  6386. Terminal->ResolvingSymlinks &&
  6387. Terminal->IsCapable[fcSymbolicLink]));
  6388. }
  6389. //---------------------------------------------------------------------------
  6390. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6391. {
  6392. return
  6393. !IsSideLocalBrowser(FCurrentSide) &&
  6394. (RemoteDirView->ItemFocused != NULL) &&
  6395. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6396. Terminal->SessionData->ResolveSymlinks;
  6397. }
  6398. //---------------------------------------------------------------------------
  6399. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6400. {
  6401. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6402. FForceExecution = true;
  6403. try
  6404. {
  6405. DirView(osCurrent)->ExecuteCurrentFile();
  6406. }
  6407. __finally
  6408. {
  6409. FForceExecution = false;
  6410. }
  6411. }
  6412. //---------------------------------------------------------------------------
  6413. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6414. {
  6415. TEditorData ExternalEditor;
  6416. ExternalEditor.Editor = edExternal;
  6417. bool Remember = false;
  6418. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6419. {
  6420. if (Remember)
  6421. {
  6422. TEditorList * EditorList = new TEditorList;
  6423. try
  6424. {
  6425. *EditorList = *WinConfiguration->EditorList;
  6426. bool Found = false;
  6427. int i = 0;
  6428. while (!Found && (i < EditorList->Count))
  6429. {
  6430. const TEditorPreferences * Editor = EditorList->Editors[i];
  6431. if ((Editor->Data->Editor == edExternal) &&
  6432. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6433. {
  6434. Found = true;
  6435. }
  6436. i++;
  6437. }
  6438. if (!Found)
  6439. {
  6440. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6441. WinConfiguration->EditorList = EditorList;
  6442. }
  6443. }
  6444. __finally
  6445. {
  6446. delete EditorList;
  6447. }
  6448. }
  6449. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6450. }
  6451. }
  6452. //---------------------------------------------------------------------------
  6453. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6454. {
  6455. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6456. if (FFileFindTerminal == ATerminal)
  6457. {
  6458. FFileFindTerminal = NULL;
  6459. HideFileFindDialog();
  6460. }
  6461. if (FClipboardTerminal == ATerminal)
  6462. {
  6463. ClipboardClear(); // implies ClipboardStop
  6464. }
  6465. if ((FOnSynchronizeAbort != NULL) &&
  6466. (ATerminal == FManagedSession))
  6467. {
  6468. FOnSynchronizeAbort(NULL);
  6469. }
  6470. }
  6471. //---------------------------------------------------------------------------
  6472. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6473. {
  6474. DetachTerminal(Sender);
  6475. }
  6476. //---------------------------------------------------------------------------
  6477. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6478. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6479. {
  6480. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6481. DebugAssert(Terminal != NULL);
  6482. if (Data->Terminal == Terminal)
  6483. {
  6484. Data->Terminal = NULL;
  6485. }
  6486. }
  6487. //---------------------------------------------------------------------------
  6488. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6489. {
  6490. return NULL;
  6491. }
  6492. //---------------------------------------------------------------------------
  6493. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6494. {
  6495. UpdateControls();
  6496. SessionColor = TColor(0);
  6497. UpdateRemotePathComboBox(false);
  6498. NeedSession(false);
  6499. }
  6500. //---------------------------------------------------------------------------
  6501. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6502. {
  6503. try
  6504. {
  6505. // Cache, as the login dialog can change its value.
  6506. // See also DoShow. Also see TTerminalManager::NewSession.
  6507. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6508. try
  6509. {
  6510. if (ShowLogin)
  6511. {
  6512. bool ReloadSessions = !Startup;
  6513. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6514. }
  6515. }
  6516. __finally
  6517. {
  6518. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6519. // (so there was no chance for the user to open any session yet)
  6520. if ((ShowLogin || !Startup) &&
  6521. !WinConfiguration->KeepOpenWhenNoSession &&
  6522. // this rules out the implicit Commander's local-local browser
  6523. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6524. {
  6525. TerminateApplication();
  6526. }
  6527. }
  6528. }
  6529. catch (EAbort &)
  6530. {
  6531. // swallow
  6532. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6533. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6534. }
  6535. }
  6536. //---------------------------------------------------------------------------
  6537. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6538. {
  6539. TTerminalManager * Manager = TTerminalManager::Instance();
  6540. DebugAssert(!Manager->Updating);
  6541. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6542. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6543. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6544. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6545. try
  6546. {
  6547. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6548. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6549. {
  6550. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6551. }
  6552. for (int Index = 0; Index <= Manager->Count; Index++)
  6553. {
  6554. TThemeTabSheet * TabSheet;
  6555. if (Index >= SessionsPageControl->PageCount)
  6556. {
  6557. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6558. TabSheet->PageControl = SessionsPageControl;
  6559. }
  6560. else
  6561. {
  6562. TabSheet = SessionsPageControl->Pages[Index];
  6563. }
  6564. bool IsSessionTab = (Index < Manager->Count);
  6565. if (IsSessionTab)
  6566. {
  6567. TTerminal * Terminal = Manager->Sessions[Index];
  6568. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6569. TabSheet->CaptionTruncation = tttEllipsis;
  6570. UpdateSessionTab(TabSheet);
  6571. }
  6572. else
  6573. {
  6574. TabSheet->Tag = 0; // not really needed
  6575. TabSheet->Shadowed = false;
  6576. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6577. TabSheet->CaptionTruncation = tttNoText;
  6578. // 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)
  6579. UpdateNewTabTab();
  6580. }
  6581. }
  6582. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6583. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6584. {
  6585. SessionsPageControl->UpdateTabsCaptionTruncation();
  6586. }
  6587. }
  6588. __finally
  6589. {
  6590. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6591. }
  6592. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6593. }
  6594. //---------------------------------------------------------------------------
  6595. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6596. {
  6597. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6598. }
  6599. //---------------------------------------------------------------------------
  6600. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6601. {
  6602. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6603. UnicodeString TabCaption;
  6604. if (WinConfiguration->SelectiveToolbarText)
  6605. {
  6606. TabCaption = GetNewTabTabCaption();
  6607. }
  6608. TabSheet->BaseCaption = TabCaption;
  6609. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6610. }
  6611. //---------------------------------------------------------------------------
  6612. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6613. {
  6614. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6615. }
  6616. //---------------------------------------------------------------------------
  6617. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6618. {
  6619. DebugFail();
  6620. return UnicodeString();
  6621. }
  6622. //---------------------------------------------------------------------------
  6623. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6624. {
  6625. if (DebugAlwaysTrue(TabSheet != NULL))
  6626. {
  6627. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6628. if (DebugAlwaysTrue(ASession != NULL))
  6629. {
  6630. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6631. if (ASession->LocalBrowser)
  6632. {
  6633. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6634. }
  6635. else
  6636. {
  6637. TabSheet->ImageIndex = AddSessionColor(Color);
  6638. }
  6639. TTerminalManager * Manager = TTerminalManager::Instance();
  6640. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6641. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6642. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6643. }
  6644. }
  6645. }
  6646. //---------------------------------------------------------------------------
  6647. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6648. {
  6649. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6650. }
  6651. //---------------------------------------------------------------------------
  6652. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6653. {
  6654. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6655. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6656. bool Result = (Session != NULL);
  6657. if (Result)
  6658. {
  6659. TTerminalManager::Instance()->ActiveSession = Session;
  6660. }
  6661. else
  6662. {
  6663. try
  6664. {
  6665. NewTab();
  6666. }
  6667. __finally
  6668. {
  6669. SessionListChanged();
  6670. }
  6671. FSessionsPageControlNewTabTime = Now();
  6672. }
  6673. return Result;
  6674. }
  6675. //---------------------------------------------------------------------------
  6676. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6677. {
  6678. SessionTabSwitched();
  6679. }
  6680. //---------------------------------------------------------------------------
  6681. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6682. {
  6683. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6684. DebugAssert(TransferList != NULL);
  6685. UnicodeString Name;
  6686. if (TransferList->ItemIndex <= 0)
  6687. {
  6688. Name = L"";
  6689. }
  6690. else
  6691. {
  6692. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6693. }
  6694. if (FCopyParamAutoSelected.IsEmpty())
  6695. {
  6696. // if previous preset was not autoselected, make new preset the "default"
  6697. FCopyParamDefault = Name;
  6698. }
  6699. GUIConfiguration->CopyParamCurrent = Name;
  6700. }
  6701. //---------------------------------------------------------------------------
  6702. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6703. {
  6704. // sanity check
  6705. bool ExistingPreset =
  6706. (FTransferListHoverIndex >= 0) &&
  6707. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6708. DebugAssert(ExistingPreset);
  6709. if (ExistingPreset)
  6710. {
  6711. HDC DC = GetDC(0);
  6712. TCanvas * Canvas = new TCanvas();
  6713. try
  6714. {
  6715. Canvas->Handle = DC;
  6716. Canvas->Font = GetToolbarFont(this);
  6717. UnicodeString Name;
  6718. if (FTransferListHoverIndex == 0)
  6719. {
  6720. Name = L"";
  6721. }
  6722. else
  6723. {
  6724. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6725. }
  6726. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6727. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6728. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6729. static_cast<TObject*>(GetComponent(fcTransferList)));
  6730. UnicodeString InfoStr =
  6731. GUIConfiguration->CopyParamPreset[Name].
  6732. GetInfoStr(L"; ",
  6733. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6734. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6735. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6736. {
  6737. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6738. {
  6739. InfoStr.SetLength(InfoStr.Length() - 1);
  6740. }
  6741. InfoStr += Ellipsis;
  6742. }
  6743. // UpdateCaption does not cause invalidation of whole submenu, while
  6744. // setting Caption property does.
  6745. // also it probably does not resize the label, even if necessary
  6746. // (we do not want that anyway)
  6747. TransferLabel->UpdateCaption(InfoStr);
  6748. }
  6749. __finally
  6750. {
  6751. Canvas->Handle = NULL;
  6752. ReleaseDC(0, DC);
  6753. delete Canvas;
  6754. }
  6755. }
  6756. }
  6757. //---------------------------------------------------------------------------
  6758. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6759. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6760. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6761. {
  6762. if (FTransferListHoverIndex != AHoverIndex)
  6763. {
  6764. FTransferListHoverIndex = AHoverIndex;
  6765. UpdateTransferLabel();
  6766. }
  6767. }
  6768. //---------------------------------------------------------------------------
  6769. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6770. {
  6771. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6772. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6773. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6774. {
  6775. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6776. {
  6777. if (!NonVisualDataModule->Busy)
  6778. {
  6779. OpenDirectory(GetSide(osCurrent));
  6780. }
  6781. Message.Result = 1;
  6782. }
  6783. else
  6784. {
  6785. TForm::Dispatch(&Message);
  6786. }
  6787. }
  6788. else
  6789. {
  6790. TForm::Dispatch(&Message);
  6791. }
  6792. }
  6793. //---------------------------------------------------------------------------
  6794. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6795. {
  6796. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6797. if ((FIgnoreNextDialogChar != 0) &&
  6798. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6799. {
  6800. Message.Result = 1;
  6801. }
  6802. else
  6803. {
  6804. TForm::Dispatch(&Message);
  6805. }
  6806. FIgnoreNextDialogChar = 0;
  6807. }
  6808. //---------------------------------------------------------------------------
  6809. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6810. {
  6811. // The four low-order bits are used internally by Windows
  6812. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6813. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6814. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6815. // 61730 - restore thru double click - undocumented
  6816. // 61490 - maximize thru double click - undocumented
  6817. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6818. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6819. {
  6820. SysResizing(Cmd);
  6821. }
  6822. TForm::Dispatch(&Message);
  6823. }
  6824. //---------------------------------------------------------------------------
  6825. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6826. {
  6827. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6828. // Also there no point returning TRUE as we are not able to
  6829. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6830. // Hence the process termination might be safer :)
  6831. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6832. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6833. {
  6834. Message.Result = FALSE;
  6835. }
  6836. else
  6837. {
  6838. Message.Result = TRUE;
  6839. }
  6840. // Do not call default handling as that triggers OnCloseQuery,
  6841. // where our implementation will popup confirmation dialogs, what we do not want,
  6842. // as per Vista guidelines:
  6843. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6844. }
  6845. //---------------------------------------------------------------------------
  6846. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6847. {
  6848. if (Message.EndSession && IsApplicationMinimized())
  6849. {
  6850. // WORKAROUND
  6851. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6852. // when it receives WM_ENDSESSION,
  6853. // but that sometimes (particularly when application is minimized) cause crashes.
  6854. // Crash popups message that blocks log off.
  6855. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6856. // so we call ExitProcess() immediately, not even trying to cleanup.
  6857. // It still causes beep, so there's likely some popup, but it does not block
  6858. // log off.
  6859. ExitProcess(0);
  6860. }
  6861. TForm::Dispatch(&Message);
  6862. }
  6863. #ifdef _DEBUG
  6864. //---------------------------------------------------------------------------
  6865. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6866. {
  6867. TForm::Dispatch(&Message);
  6868. }
  6869. #endif
  6870. //---------------------------------------------------------------------------
  6871. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6872. {
  6873. }
  6874. //---------------------------------------------------------------------------
  6875. void __fastcall TCustomScpExplorerForm::DoShow()
  6876. {
  6877. // Called from TCustomScpExplorerForm::CMShowingChanged
  6878. // only now are the controls resized finally, so the size constraints
  6879. // will not conflict with possibly very small window size
  6880. RestoreFormParams();
  6881. // This indicates that the form was rescaled after parameters were loaded and
  6882. // as most probably the parameters were actually saved with the current form scaling
  6883. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6884. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6885. // and no rounding problems will occur.
  6886. // See also RestoreForm.
  6887. // (we should be safe to call RestoreParams unconditionally)
  6888. if (PixelsPerInch != Screen->PixelsPerInch)
  6889. {
  6890. RestoreParams();
  6891. }
  6892. FixControlsPlacement();
  6893. if (Position == poDefaultPosOnly)
  6894. {
  6895. CutFormToDesktop(this);
  6896. }
  6897. TForm::DoShow();
  6898. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6899. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6900. // This was previously in NeedSession, where other related code is.
  6901. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6902. // unpleasantly shows briefly.
  6903. if ((ManagedSession == NULL) &&
  6904. !WinConfiguration->ShowLoginWhenNoSession &&
  6905. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6906. // This detects if workspace was saved the last time the main widow was closed
  6907. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6908. {
  6909. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6910. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6911. }
  6912. // Before main window displays
  6913. if (ManagedSession == NULL)
  6914. {
  6915. StartingWithoutSession();
  6916. }
  6917. UpdatePixelsPerInchMainWindowCounter();
  6918. FShowing = true;
  6919. }
  6920. //---------------------------------------------------------------------------
  6921. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6922. {
  6923. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6924. }
  6925. //---------------------------------------------------------------------------
  6926. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6927. {
  6928. SessionListChanged();
  6929. InitStatusBar();
  6930. UpdateControls();
  6931. }
  6932. //---------------------------------------------------------------------------
  6933. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6934. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6935. TNotifyEvent OnBalloonClick, TObject * UserData)
  6936. {
  6937. bool Do;
  6938. UnicodeString Message;
  6939. if (E == NULL)
  6940. {
  6941. Message = Str;
  6942. Do = true;
  6943. }
  6944. else
  6945. {
  6946. Do = ExceptionMessage(E, Message);
  6947. }
  6948. if (Do && WinConfiguration->BalloonNotifications)
  6949. {
  6950. UnicodeString Title;
  6951. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6952. {
  6953. Message = TrimLeft(Message);
  6954. }
  6955. else
  6956. {
  6957. Message = UnformatMessage(Message);
  6958. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6959. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6960. Title = LoadResourceString(Captions[Type]);
  6961. if (Terminal != NULL)
  6962. {
  6963. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6964. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6965. }
  6966. }
  6967. if (Seconds == 0)
  6968. {
  6969. Seconds = WinConfiguration->NotificationsTimeout;
  6970. }
  6971. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6972. }
  6973. }
  6974. //---------------------------------------------------------------------------
  6975. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6976. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6977. UnicodeString HelpKeyword, const TMessageParams * Params,
  6978. TTerminal * Terminal)
  6979. {
  6980. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6981. (FOnFeedSynchronizeError != NULL)) &&
  6982. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6983. {
  6984. if (FOnFeedSynchronizeError != NULL)
  6985. {
  6986. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6987. }
  6988. else
  6989. {
  6990. DebugAssert(FErrorList != NULL);
  6991. TStringList * MoreMessagesCopy = NULL;
  6992. if (MoreMessages)
  6993. {
  6994. MoreMessagesCopy = new TStringList();
  6995. MoreMessagesCopy->Assign(MoreMessages);
  6996. }
  6997. FErrorList->AddObject(Message, MoreMessagesCopy);
  6998. }
  6999. PopupTrayBalloon(Terminal, Message, Type);
  7000. return ContinueAnswer(Answers);
  7001. }
  7002. else
  7003. {
  7004. bool UseBalloon = FTrayIcon->Visible;
  7005. if (UseBalloon)
  7006. {
  7007. PopupTrayBalloon(Terminal, Message, Type);
  7008. }
  7009. unsigned int Result;
  7010. try
  7011. {
  7012. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7013. }
  7014. __finally
  7015. {
  7016. // cancel only balloon we popped up, otherwise we may cancel notification
  7017. // balloon that was there before the message dialog
  7018. // (such as "dd confirmation opt in balloon)
  7019. if (UseBalloon)
  7020. {
  7021. FTrayIcon->CancelBalloon();
  7022. }
  7023. }
  7024. return Result;
  7025. }
  7026. }
  7027. //---------------------------------------------------------------------------
  7028. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7029. TTerminal * Terminal, Exception * E)
  7030. {
  7031. if (FTrayIcon->Visible)
  7032. {
  7033. PopupTrayBalloon(Terminal, L"", qtError, E);
  7034. }
  7035. // particularly prevent opening new session from jump list,
  7036. // while exception is shown
  7037. NonVisualDataModule->StartBusy();
  7038. try
  7039. {
  7040. ShowExtendedExceptionEx(Terminal, E);
  7041. }
  7042. __finally
  7043. {
  7044. NonVisualDataModule->EndBusy();
  7045. FTrayIcon->CancelBalloon();
  7046. }
  7047. }
  7048. //---------------------------------------------------------------------------
  7049. void __fastcall TCustomScpExplorerForm::SessionReady()
  7050. {
  7051. InitStatusBar();
  7052. // cannot rely on active page being page for active terminal,
  7053. // as it can happen that active page is the "new session" page
  7054. // (e.g. when reconnecting active terminal, while login dialog
  7055. // invoked from "new session" page is modal)
  7056. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7057. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7058. }
  7059. //---------------------------------------------------------------------------
  7060. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7061. TTerminal * Terminal, Exception * E)
  7062. {
  7063. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7064. if (!Terminal->Active)
  7065. {
  7066. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7067. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7068. {
  7069. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7070. }
  7071. }
  7072. }
  7073. //---------------------------------------------------------------------------
  7074. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7075. UnicodeString Message, TQueryType Type,
  7076. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7077. {
  7078. if ((E == NULL) ||
  7079. ExceptionMessage(E, Message))
  7080. {
  7081. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7082. if (Important)
  7083. {
  7084. Seconds *= 5;
  7085. }
  7086. UnicodeString NoteMessage(UnformatMessage(Message));
  7087. if (Terminal != NULL)
  7088. {
  7089. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7090. NoteMessage = FORMAT(L"%s: %s",
  7091. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7092. }
  7093. if (WinConfiguration->BalloonNotifications)
  7094. {
  7095. AddNote(NoteMessage);
  7096. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7097. }
  7098. else
  7099. {
  7100. FlashOnBackground();
  7101. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7102. }
  7103. }
  7104. }
  7105. //---------------------------------------------------------------------------
  7106. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7107. {
  7108. RestoreApp();
  7109. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7110. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7111. !NonVisualDataModule->Busy)
  7112. {
  7113. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7114. TTerminalManager::Instance()->ActiveSession = Terminal;
  7115. if (!ComponentVisible[fcQueueView])
  7116. {
  7117. ToggleQueueVisibility();
  7118. GoToQueue();
  7119. }
  7120. }
  7121. }
  7122. //---------------------------------------------------------------------------
  7123. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7124. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7125. {
  7126. UnicodeString Message;
  7127. TNotifyEvent OnClick = NULL;
  7128. TObject * UserData = NULL;
  7129. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7130. switch (Event)
  7131. {
  7132. case qeEmptyButMonitored:
  7133. if ((ATerminal != Terminal) || QueueInvisible)
  7134. {
  7135. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7136. OnClick = QueueEmptyNoteClicked;
  7137. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7138. TerminalNoteData->Terminal = ATerminal;
  7139. UserData = TerminalNoteData;
  7140. }
  7141. break;
  7142. case qeEmpty:
  7143. OperationComplete(ATerminal->QueueOperationStart);
  7144. break;
  7145. case qePendingUserAction:
  7146. if ((ATerminal != Terminal) ||
  7147. (QueueInvisible && !IsQueueAutoPopup()))
  7148. {
  7149. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7150. }
  7151. break;
  7152. default:
  7153. DebugFail();
  7154. }
  7155. if (!Message.IsEmpty())
  7156. {
  7157. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7158. }
  7159. }
  7160. //---------------------------------------------------------------------------
  7161. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7162. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7163. {
  7164. if (FDDExtMapFile != NULL)
  7165. {
  7166. CloseHandle(FDDExtMapFile);
  7167. FDDExtMapFile = NULL;
  7168. }
  7169. if (WinConfiguration->DDFakeFile)
  7170. {
  7171. DDFakeFileInitDrag(FileList, Created);
  7172. }
  7173. }
  7174. //---------------------------------------------------------------------------
  7175. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7176. {
  7177. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7178. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7179. {
  7180. FFakeFileDropTarget = FileName;
  7181. }
  7182. }
  7183. //---------------------------------------------------------------------------
  7184. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7185. {
  7186. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7187. if (!ForceDirectories(ApiPath(Result)))
  7188. {
  7189. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7190. }
  7191. FileSetAttr(ApiPath(Result), faHidden);
  7192. return Result;
  7193. }
  7194. //---------------------------------------------------------------------------
  7195. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7196. bool & Created)
  7197. {
  7198. FFakeFileDropTarget = UnicodeString();
  7199. FDragFakeDirectory = CreateFakeTransferDirectory();
  7200. FileList->AddItem(NULL, FDragFakeDirectory);
  7201. Created = true;
  7202. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7203. {
  7204. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7205. }
  7206. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7207. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7208. {
  7209. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7210. TDragExtCommStruct* CommStruct;
  7211. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7212. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7213. DebugAssert(CommStruct != NULL);
  7214. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7215. CommStruct->Dragging = true;
  7216. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7217. LENOF(CommStruct->DropDest));
  7218. NULL_TERMINATE(CommStruct->DropDest);
  7219. UnmapViewOfFile(CommStruct);
  7220. }
  7221. }
  7222. //---------------------------------------------------------------------------
  7223. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7224. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7225. {
  7226. bool Result;
  7227. DebugAssert(!IsLocalBrowserMode());
  7228. if (Sender == RemoteDirView)
  7229. {
  7230. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7231. }
  7232. else
  7233. {
  7234. DebugAssert(Sender == RemoteDriveView);
  7235. TStrings * FileList = RemoteDriveView->DragFileList();
  7236. try
  7237. {
  7238. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7239. }
  7240. __finally
  7241. {
  7242. delete FileList;
  7243. }
  7244. }
  7245. if (FDDTargetControl == RemoteDriveView)
  7246. {
  7247. RemoteDriveView->UpdateDropTarget();
  7248. }
  7249. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7250. // or from tree view to dir view.
  7251. // foMove happens when file/dir is dragged from remote tree view outside of
  7252. // application via dragex
  7253. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7254. (DropSourceControl == RemoteDriveView))
  7255. {
  7256. RemoteDriveView->UpdateDropSource();
  7257. }
  7258. return Result;
  7259. }
  7260. //---------------------------------------------------------------------------
  7261. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7262. {
  7263. DebugAssert(!IsLocalBrowserMode());
  7264. // This also handles drops of remote files to queue.
  7265. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7266. SAFE_DESTROY(FDDFileList);
  7267. if (!FFakeFileDropTarget.IsEmpty())
  7268. {
  7269. RemoveDir(ApiPath(FFakeFileDropTarget));
  7270. }
  7271. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7272. {
  7273. try
  7274. {
  7275. TDragResult DDResult = (Sender == RemoteDirView) ?
  7276. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7277. // Focus is moved to the target application,
  7278. // but as we are going to present the UI, we need to steal the focus back.
  7279. // This most likely won't work though (windows does not allow application
  7280. // to steal focus most of the time)
  7281. Application->BringToFront();
  7282. // On older version of Windows we never got drMove here, see also comment below.
  7283. // On Windows 10, we get the "move".
  7284. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7285. {
  7286. UnicodeString TargetDirectory;
  7287. TFileOperation Operation;
  7288. // drInvalid may mean drMove, see comment below
  7289. switch (DDResult)
  7290. {
  7291. case drCopy:
  7292. Operation = foCopy;
  7293. break;
  7294. case drMove:
  7295. Operation = foMove;
  7296. break;
  7297. default:
  7298. DebugFail();
  7299. case drInvalid:
  7300. // prefer "copy" for safety
  7301. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7302. break;
  7303. }
  7304. TTransferOperationParam Param;
  7305. UnicodeString CounterName;
  7306. bool ForceQueue;
  7307. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7308. {
  7309. // we get drInvalid both if d&d was intercepted by ddext,
  7310. // and when users drops on no-drop location.
  7311. // we tell the difference by existence of response from ddext,
  7312. // so we ignore absence of response in this case
  7313. if (DDResult != drInvalid)
  7314. {
  7315. // here we know that the extension is installed,
  7316. // as it is checked as soon as drag&drop starts from
  7317. // RemoteFileControlDDCreateDragFileList
  7318. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7319. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7320. {
  7321. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7322. }
  7323. else
  7324. {
  7325. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7326. }
  7327. }
  7328. }
  7329. else
  7330. {
  7331. // download using fake file
  7332. Param.Temp = false;
  7333. Param.DragDrop = true;
  7334. if (ForceQueue)
  7335. {
  7336. Param.Queue = asOn;
  7337. }
  7338. if (Sender == RemoteDirView)
  7339. {
  7340. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7341. }
  7342. if (RemoteFileControlFileOperation(Sender, Operation,
  7343. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7344. {
  7345. Configuration->Usage->Inc(CounterName);
  7346. }
  7347. }
  7348. }
  7349. }
  7350. __finally
  7351. {
  7352. CloseHandle(FDDExtMapFile);
  7353. FDDExtMapFile = NULL;
  7354. delete FDragFakeMonitors;
  7355. FDragFakeMonitors = NULL;
  7356. RemoveDir(ApiPath(FDragFakeDirectory));
  7357. FDragFakeDirectory = L"";
  7358. FFakeFileDropTarget = UnicodeString();
  7359. }
  7360. }
  7361. if (FDragDropSshTerminate.get() != NULL)
  7362. {
  7363. FDragDropSshTerminate->Rethrow();
  7364. }
  7365. }
  7366. //---------------------------------------------------------------------------
  7367. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7368. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7369. {
  7370. // Remember drop effect so we know (when user drops files), if we copy or move
  7371. FLastDropEffect = dwEffect;
  7372. }
  7373. //---------------------------------------------------------------------------
  7374. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7375. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7376. {
  7377. bool Result;
  7378. if (FDDTargetControl == QueueView3)
  7379. {
  7380. Directory = DefaultDownloadTargetDirectory();
  7381. Result = true;
  7382. CounterName = L"DownloadsDragDropQueue";
  7383. ForceQueue = true;
  7384. }
  7385. else if (!FFakeFileDropTarget.IsEmpty())
  7386. {
  7387. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7388. Result = true;
  7389. ForceQueue = false;
  7390. CounterName = L"DownloadsDragDropFakeFile";
  7391. }
  7392. else
  7393. {
  7394. ForceQueue = false;
  7395. Enabled = false;
  7396. try
  7397. {
  7398. int Timer = 0;
  7399. Result = false;
  7400. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7401. {
  7402. {
  7403. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7404. TDragExtCommStruct* CommStruct;
  7405. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7406. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7407. DebugAssert(CommStruct != NULL);
  7408. Result = !CommStruct->Dragging;
  7409. if (Result)
  7410. {
  7411. Directory = ExtractFilePath(CommStruct->DropDest);
  7412. CounterName = L"DownloadsDragDropExternalExt";
  7413. }
  7414. UnmapViewOfFile(CommStruct);
  7415. }
  7416. if (!Result)
  7417. {
  7418. Sleep(50);
  7419. Timer += 50;
  7420. Application->ProcessMessages();
  7421. }
  7422. }
  7423. }
  7424. __finally
  7425. {
  7426. Enabled = true;
  7427. }
  7428. }
  7429. return Result;
  7430. }
  7431. //---------------------------------------------------------------------------
  7432. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7433. const UnicodeString TempDir, int SecDelay)
  7434. {
  7435. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7436. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7437. if (FDelayedDeletionTimer == NULL)
  7438. {
  7439. DebugAssert(HandleAllocated());
  7440. FDelayedDeletionTimer = new TTimer(this);
  7441. FDelayedDeletionTimer->Interval = 10000;
  7442. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7443. }
  7444. else
  7445. {
  7446. FDelayedDeletionTimer->Enabled = true;
  7447. }
  7448. }
  7449. //---------------------------------------------------------------------------
  7450. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7451. {
  7452. DebugAssert(FDelayedDeletionList != NULL);
  7453. TDateTime N = Now();
  7454. TDateTime Alert;
  7455. UnicodeString Directory;
  7456. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7457. {
  7458. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7459. if ((N >= Alert) || (Sender == NULL))
  7460. {
  7461. Directory = FDelayedDeletionList->Strings[Index];
  7462. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7463. {
  7464. FDelayedDeletionList->Delete(Index);
  7465. }
  7466. }
  7467. }
  7468. if (FDelayedDeletionList->Count == 0)
  7469. {
  7470. FDelayedDeletionTimer->Enabled = false;
  7471. }
  7472. }
  7473. //---------------------------------------------------------------------------
  7474. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7475. {
  7476. DebugAssert(!IsLocalBrowserMode());
  7477. if (IsFileControl(FDDTargetControl, osRemote) ||
  7478. (FDDTargetControl == SessionsPageControl))
  7479. {
  7480. TManagedTerminal * TargetTerminal = NULL;
  7481. TFileOperation Operation = foNone;
  7482. if (FDDTargetControl == SessionsPageControl)
  7483. {
  7484. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7485. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7486. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7487. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7488. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7489. {
  7490. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7491. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7492. {
  7493. Operation = foRemoteMove;
  7494. }
  7495. else
  7496. {
  7497. Operation = foRemoteCopy;
  7498. }
  7499. }
  7500. }
  7501. else
  7502. {
  7503. if (FLastDropEffect == DROPEFFECT_MOVE)
  7504. {
  7505. Operation = foRemoteMove;
  7506. }
  7507. else if (FLastDropEffect == DROPEFFECT_COPY)
  7508. {
  7509. Operation = foRemoteCopy;
  7510. }
  7511. }
  7512. if (Operation != foNone)
  7513. {
  7514. RemoteFileControlFileOperation(DropSourceControl,
  7515. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7516. }
  7517. // abort drag&drop
  7518. Abort();
  7519. }
  7520. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7521. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7522. (FDDTargetControl != QueueView3))
  7523. {
  7524. DebugAssert(!FDragTempDir.IsEmpty());
  7525. TTransferType Type;
  7526. UnicodeString TempDir = FDragTempDir;
  7527. // We clear FDragTempDir before calling
  7528. // just in case it fail (raises exception)
  7529. FDragTempDir = L"";
  7530. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7531. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7532. // empty directory parameter means temp directory -> don't display it!
  7533. UnicodeString TargetDir = L"";
  7534. int Options = 0;
  7535. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7536. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7537. {
  7538. Abort();
  7539. }
  7540. // TargetDir is set when dropped on local file control
  7541. // (this was workaround for legacy dirview event handling, now it should be
  7542. // made prettier)
  7543. if (TargetDir.IsEmpty())
  7544. {
  7545. TargetDir = TempDir;
  7546. if (ForceDirectories(ApiPath(TargetDir)))
  7547. {
  7548. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7549. FPendingTempSpaceWarn = true;
  7550. try
  7551. {
  7552. FDragDropOperation = true;
  7553. int Params = cpTemporary |
  7554. (Type == ttMove ? cpDelete : 0);
  7555. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7556. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7557. }
  7558. __finally
  7559. {
  7560. FDragDropOperation = false;
  7561. FPendingTempSpaceWarn = false;
  7562. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7563. }
  7564. }
  7565. else
  7566. {
  7567. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7568. }
  7569. }
  7570. }
  7571. }
  7572. //---------------------------------------------------------------------------
  7573. void __fastcall TCustomScpExplorerForm::DDDownload(
  7574. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7575. {
  7576. DebugAssert(!IsLocalBrowserMode());
  7577. TAutoBatch AutoBatch(this);
  7578. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7579. UpdateCopyParamCounters(*CopyParam);
  7580. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7581. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7582. {
  7583. RemoteDriveView->UpdateDropSource();
  7584. }
  7585. }
  7586. //---------------------------------------------------------------------------
  7587. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7588. {
  7589. public:
  7590. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7591. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7592. {
  7593. }
  7594. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7595. {
  7596. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7597. TDataObjectFilesEx::AllowData(FormatEtc);
  7598. }
  7599. };
  7600. //---------------------------------------------------------------------------
  7601. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7602. TObject * Sender, TDataObject *& DataObject)
  7603. {
  7604. if (FDDExtMapFile != NULL)
  7605. {
  7606. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7607. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7608. {
  7609. Abort();
  7610. }
  7611. if (FileList->Count > 0)
  7612. {
  7613. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7614. if (!FilesObject->IsValid(true, true))
  7615. {
  7616. FilesObject->_Release();
  7617. }
  7618. else
  7619. {
  7620. DataObject = FilesObject;
  7621. }
  7622. }
  7623. }
  7624. }
  7625. //---------------------------------------------------------------------------
  7626. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7627. {
  7628. DebugFail();
  7629. }
  7630. //---------------------------------------------------------------------------
  7631. void __fastcall TCustomScpExplorerForm::GoToTree()
  7632. {
  7633. ComponentVisible[fcRemoteTree] = true;
  7634. DriveView(osRemote)->SetFocus();
  7635. }
  7636. //---------------------------------------------------------------------------
  7637. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7638. TPanelExport Export)
  7639. {
  7640. TCustomDirView * DirView = this->DirView(Side);
  7641. std::unique_ptr<TStrings> ExportData(new TStringList());
  7642. switch (Export)
  7643. {
  7644. case pePath:
  7645. ExportData->Add(DirView->PathName);
  7646. break;
  7647. case peFileList:
  7648. case peFullFileList:
  7649. {
  7650. bool FullPath = (Export == peFullFileList);
  7651. DirView->CreateFileList(false, FullPath, ExportData.get());
  7652. for (int Index = 0; Index < ExportData->Count; Index++)
  7653. {
  7654. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7655. {
  7656. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7657. }
  7658. }
  7659. }
  7660. break;
  7661. default:
  7662. DebugFail();
  7663. }
  7664. return ExportData.release();
  7665. }
  7666. //---------------------------------------------------------------------------
  7667. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7668. TPanelExport Export, TPanelExportDestination Destination)
  7669. {
  7670. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7671. PanelExportStore(Side, Export, Destination, ExportData.get());
  7672. }
  7673. //---------------------------------------------------------------------------
  7674. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7675. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7676. TStrings * ExportData)
  7677. {
  7678. if (Destination == pedClipboard)
  7679. {
  7680. TInstantOperationVisualizer Visualizer;
  7681. CopyToClipboard(ExportData);
  7682. }
  7683. else
  7684. {
  7685. DebugFail();
  7686. }
  7687. }
  7688. //---------------------------------------------------------------------------
  7689. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7690. {
  7691. TCustomDirView * DirView = this->DirView(Side);
  7692. UnicodeString Mask = DirView->Mask;
  7693. if (DoFilterMaskDialog(DirView, Mask))
  7694. {
  7695. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7696. Configuration->Usage->Inc(L"Filters");
  7697. }
  7698. }
  7699. //---------------------------------------------------------------------------
  7700. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7701. {
  7702. return FQueueController->DefaultOperation();
  7703. }
  7704. //---------------------------------------------------------------------------
  7705. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7706. TQueueOperation Operation, void ** Param)
  7707. {
  7708. switch (Operation)
  7709. {
  7710. case qoPreferences:
  7711. return true;
  7712. case qoGoTo:
  7713. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7714. case qoOnceEmpty:
  7715. return IsAnythingQueued();
  7716. default:
  7717. return FQueueController->AllowOperation(Operation, Param);
  7718. }
  7719. }
  7720. //---------------------------------------------------------------------------
  7721. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7722. {
  7723. if (DebugAlwaysTrue(QueueView3->Visible))
  7724. {
  7725. QueueView3->SetFocus();
  7726. }
  7727. }
  7728. //---------------------------------------------------------------------------
  7729. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7730. TQueueOperation Operation, void * Param)
  7731. {
  7732. if (Operation == qoGoTo)
  7733. {
  7734. GoToQueue();
  7735. }
  7736. else if (Operation == qoPreferences)
  7737. {
  7738. PreferencesDialog(pmQueue);
  7739. }
  7740. else
  7741. {
  7742. FQueueController->ExecuteOperation(Operation, Param);
  7743. }
  7744. }
  7745. //---------------------------------------------------------------------------
  7746. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7747. {
  7748. return (Queue != NULL) && Queue->Enabled;
  7749. }
  7750. //---------------------------------------------------------------------------
  7751. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7752. {
  7753. DebugAssert(Queue != NULL);
  7754. if (Queue != NULL)
  7755. {
  7756. Queue->Enabled = !Queue->Enabled;
  7757. }
  7758. }
  7759. //---------------------------------------------------------------------------
  7760. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7761. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7762. {
  7763. FQueueActedItem = QueueView3->ItemFocused;
  7764. }
  7765. //---------------------------------------------------------------------------
  7766. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7767. {
  7768. return TopDock->Height + QueueSplitter->Height;
  7769. }
  7770. //---------------------------------------------------------------------------
  7771. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7772. {
  7773. return
  7774. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7775. (QueueDock->Visible ? QueueDock->Height : 0) +
  7776. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7777. }
  7778. //---------------------------------------------------------------------------
  7779. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7780. {
  7781. return ScaleByTextHeight(this, 48);
  7782. }
  7783. //---------------------------------------------------------------------------
  7784. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7785. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7786. {
  7787. // when queue is hidden by double-clicking splitter, stray attempt to
  7788. // resize the panel with strange value arrives, make sure it is ignored
  7789. if (ComponentVisible[fcQueueView])
  7790. {
  7791. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7792. if (NewSize > MaxHeightLimit)
  7793. {
  7794. NewSize = MaxHeightLimit;
  7795. }
  7796. int MinHeightLimit =
  7797. GetStaticQueuePanelComponentsHeight() +
  7798. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7799. GetMinQueueViewHeight();
  7800. if (NewSize < MinHeightLimit)
  7801. {
  7802. NewSize = MinHeightLimit;
  7803. }
  7804. }
  7805. else
  7806. {
  7807. Accept = false;
  7808. }
  7809. }
  7810. //---------------------------------------------------------------------------
  7811. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7812. {
  7813. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7814. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7815. if (QueueViewHeight < GetMinQueueViewHeight())
  7816. {
  7817. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7818. }
  7819. }
  7820. //---------------------------------------------------------------------------
  7821. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7822. TDragObject *& /*DragObject*/)
  7823. {
  7824. FQueueActedItem = QueueView3->ItemFocused;
  7825. }
  7826. //---------------------------------------------------------------------------
  7827. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7828. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7829. {
  7830. Accept = true;
  7831. if (Source == QueueView3)
  7832. {
  7833. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7834. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7835. if (Accept)
  7836. {
  7837. TQueueItemProxy * QueueItem;
  7838. TQueueItemProxy * DestQueueItem;
  7839. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7840. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7841. Accept = (QueueItem != DestQueueItem) &&
  7842. (QueueItem->Status == TQueueItem::qsPending) &&
  7843. (DestQueueItem->Status == TQueueItem::qsPending);
  7844. }
  7845. }
  7846. }
  7847. //---------------------------------------------------------------------------
  7848. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7849. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7850. {
  7851. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7852. {
  7853. TQueueItemProxy * QueueItem;
  7854. TQueueItemProxy * DestQueueItem;
  7855. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7856. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7857. QueueItem->Move(DestQueueItem);
  7858. }
  7859. }
  7860. //---------------------------------------------------------------------------
  7861. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7862. {
  7863. if ((QueueView3->ItemFocused == NULL) &&
  7864. (QueueView3->Items->Count > 0))
  7865. {
  7866. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7867. }
  7868. QueueLabelUpdateStatus();
  7869. }
  7870. //---------------------------------------------------------------------------
  7871. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7872. {
  7873. QueueLabelUpdateStatus();
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7877. {
  7878. QueueLabel->UpdateStatus();
  7879. }
  7880. //---------------------------------------------------------------------------
  7881. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7882. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7883. {
  7884. if (Selected)
  7885. {
  7886. NonVisualDataModule->UpdateNonVisibleActions();
  7887. }
  7888. }
  7889. //---------------------------------------------------------------------------
  7890. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7891. {
  7892. TDragDropFilesEx * Result = NULL;
  7893. if (Sender == SessionsPageControl)
  7894. {
  7895. Result = FSessionsDragDropFilesEx;
  7896. }
  7897. else if (Sender == QueueView3)
  7898. {
  7899. Result = FQueueDragDropFilesEx;
  7900. }
  7901. else
  7902. {
  7903. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7904. if (DirView != NULL)
  7905. {
  7906. Result = DirView->DragDropFilesEx;
  7907. }
  7908. else
  7909. {
  7910. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7911. if (DriveView != NULL)
  7912. {
  7913. Result = DriveView->DragDropFilesEx;
  7914. }
  7915. }
  7916. }
  7917. DebugAssert(Result != NULL);
  7918. return Result;
  7919. }
  7920. //---------------------------------------------------------------------------
  7921. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7922. {
  7923. return (DView->SelCount == DView->FilesCount);
  7924. }
  7925. //---------------------------------------------------------------------------
  7926. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7927. {
  7928. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7929. }
  7930. //---------------------------------------------------------------------------
  7931. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7932. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7933. {
  7934. DebugAssert(!IsLocalBrowserMode());
  7935. TFileOperation Operation;
  7936. switch (Effect)
  7937. {
  7938. case DROPEFFECT_MOVE:
  7939. Operation = foMove;
  7940. break;
  7941. case DROPEFFECT_COPY:
  7942. // occures on WinXP (reported by user)
  7943. default:
  7944. Operation = foCopy;
  7945. break;
  7946. };
  7947. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7948. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7949. if (DragDropFilesEx->FileList->Count > 0)
  7950. {
  7951. TStrings * FileList = new TStringList();
  7952. try
  7953. {
  7954. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7955. {
  7956. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7957. }
  7958. FDragDropOperation = true;
  7959. TTransferOperationParam Param;
  7960. Param.TargetDirectory = TargetPath;
  7961. // upload, no temp dirs
  7962. Param.Temp = false;
  7963. Param.DragDrop = DragDrop;
  7964. Param.Options =
  7965. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7966. if (ForceQueue)
  7967. {
  7968. Param.Queue = asOn;
  7969. }
  7970. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7971. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7972. {
  7973. if (IsFileControl(DropSourceControl, osLocal))
  7974. {
  7975. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7976. }
  7977. else
  7978. {
  7979. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7980. }
  7981. }
  7982. }
  7983. __finally
  7984. {
  7985. FDragDropOperation = false;
  7986. delete FileList;
  7987. }
  7988. }
  7989. }
  7990. //---------------------------------------------------------------------------
  7991. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7992. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7993. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7994. {
  7995. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7996. }
  7997. //---------------------------------------------------------------------------
  7998. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7999. TObject * Sender, int grfKeyState, int & dwEffect)
  8000. {
  8001. DebugAssert(!IsLocalBrowserMode());
  8002. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8003. if ((dwEffect != DROPEFFECT_NONE) &&
  8004. IsFileControl(DropSourceControl, osRemote))
  8005. {
  8006. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8007. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8008. (RemoteDirView->DropTarget == NULL))
  8009. {
  8010. dwEffect = DROPEFFECT_NONE;
  8011. }
  8012. else
  8013. {
  8014. if (dwEffect == DROPEFFECT_COPY)
  8015. {
  8016. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8017. // currently we support copying always (at least via temporary directory);
  8018. // remove associated checks once this all proves stable and working
  8019. bool CopyCapable = true;
  8020. // if we do not support neither of operations, there's no discussion
  8021. if (!MoveCapable && !CopyCapable)
  8022. {
  8023. dwEffect = DROPEFFECT_NONE;
  8024. }
  8025. else
  8026. {
  8027. // The default effect inside remote panel is move,
  8028. // unless we do not support it, but support copy
  8029. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8030. {
  8031. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8032. }
  8033. else
  8034. {
  8035. // with ctrl-down, we want copy unless it is not supported
  8036. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8037. }
  8038. }
  8039. }
  8040. }
  8041. }
  8042. }
  8043. //---------------------------------------------------------------------------
  8044. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8045. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8046. {
  8047. DebugAssert(!IsLocalBrowserMode());
  8048. if (FDDTotalSize >= 0)
  8049. {
  8050. if (File->IsDirectory)
  8051. {
  8052. FDDTotalSize = -1;
  8053. }
  8054. else
  8055. {
  8056. FDDTotalSize += File->Size;
  8057. }
  8058. }
  8059. DebugAssert(!FDragTempDir.IsEmpty());
  8060. // TODO: this is quite ineffective
  8061. // TODO: what if invalid character replacement is disabled?
  8062. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8063. UnicodeString TransferFileName;
  8064. if (Sender == RemoteDriveView)
  8065. {
  8066. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8067. }
  8068. else
  8069. {
  8070. TransferFileName = File->FileName;
  8071. }
  8072. FDDFileList->AddObject(TransferFileName, File);
  8073. }
  8074. //---------------------------------------------------------------------------
  8075. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8076. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8077. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8078. {
  8079. // sometimes we do not get DDEnd so the list is not released
  8080. SAFE_DESTROY(FDDFileList);
  8081. FDDFileList = new TStringList();
  8082. FDragTempDir = WinConfiguration->TemporaryDir();
  8083. FDDTotalSize = 0;
  8084. FDragDropSshTerminate.reset(NULL);
  8085. }
  8086. //---------------------------------------------------------------------------
  8087. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8088. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8089. HRESULT & Result)
  8090. {
  8091. if (Result == DRAGDROP_S_DROP)
  8092. {
  8093. try
  8094. {
  8095. GlobalDragImageList->HideDragImage();
  8096. try
  8097. {
  8098. RemoteFileControlDDTargetDrop();
  8099. }
  8100. catch(ESshTerminate & E)
  8101. {
  8102. FDragDropSshTerminate.reset(E.Clone());
  8103. }
  8104. }
  8105. catch (Exception &E)
  8106. {
  8107. // If downloading fails we need to cancel drag&drop, otherwise
  8108. // Explorer shows error
  8109. // But by the way exception probably never reach this point as
  8110. // it's catched on way.
  8111. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8112. Result = DRAGDROP_S_CANCEL;
  8113. ShowExtendedException(Terminal, &E);
  8114. }
  8115. }
  8116. }
  8117. //---------------------------------------------------------------------------
  8118. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8119. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8120. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8121. {
  8122. TFileMasks::TParams MaskParams;
  8123. MaskParams.Size = Size;
  8124. MaskParams.Modification = Modification;
  8125. // this does not re-parse the mask if it is the same as the last time
  8126. FDirViewMatchMask = Masks;
  8127. bool ImplicitMatch;
  8128. Matches =
  8129. // Local and RecurseInclude parameters have no effect as there's no path.
  8130. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8131. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8132. (AllowImplicitMatches || !ImplicitMatch);
  8133. }
  8134. //---------------------------------------------------------------------------
  8135. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8136. TObject * Sender, TListItem * Item, WORD & Indexes)
  8137. {
  8138. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8139. UnicodeString Ext;
  8140. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8141. {
  8142. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8143. }
  8144. else
  8145. {
  8146. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8147. }
  8148. if (SameText(Ext, Configuration->PartialExt))
  8149. {
  8150. Indexes |= oiPartial;
  8151. }
  8152. }
  8153. //---------------------------------------------------------------------------
  8154. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8155. {
  8156. return
  8157. DirViewEnabled(osCurrent) &&
  8158. DirView(osCurrent)->CanPasteFromClipBoard();
  8159. }
  8160. //---------------------------------------------------------------------------
  8161. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8162. {
  8163. bool Result = false;
  8164. if (CanPasteToDirViewFromClipBoard())
  8165. {
  8166. Result = true;
  8167. }
  8168. else
  8169. {
  8170. UnicodeString ClipboardText;
  8171. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8172. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8173. {
  8174. if (StoredSessions->IsUrl(ClipboardText))
  8175. {
  8176. Result = true;
  8177. }
  8178. else
  8179. {
  8180. Result = DirViewEnabled(osCurrent);
  8181. }
  8182. }
  8183. }
  8184. return Result;
  8185. }
  8186. //---------------------------------------------------------------------------
  8187. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8188. {
  8189. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8190. if (DoesClipboardContainOurFiles())
  8191. {
  8192. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8193. {
  8194. DebugAssert(!IsLocalBrowserMode());
  8195. TTerminalManager * Manager = TTerminalManager::Instance();
  8196. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8197. Manager->ActiveSession = FClipboardTerminal;
  8198. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8199. }
  8200. }
  8201. else if (CanPasteToDirViewFromClipBoard())
  8202. {
  8203. DirView(osCurrent)->PasteFromClipBoard();
  8204. }
  8205. else
  8206. {
  8207. UnicodeString ClipboardText;
  8208. if (NonEmptyTextFromClipboard(ClipboardText))
  8209. {
  8210. if (StoredSessions->IsUrl(ClipboardText))
  8211. {
  8212. NewSession(ClipboardText);
  8213. }
  8214. else
  8215. {
  8216. DirView(osCurrent)->Path = ClipboardText;
  8217. }
  8218. }
  8219. }
  8220. }
  8221. //---------------------------------------------------------------------------
  8222. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8223. {
  8224. // TBD
  8225. }
  8226. //---------------------------------------------------------------------------
  8227. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8228. {
  8229. TCustomDirView * ADirView = DirView(Side);
  8230. ADirView->SelectAll(Mode);
  8231. ADirView->SetFocus();
  8232. }
  8233. //---------------------------------------------------------------------------
  8234. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8235. {
  8236. TCustomDirView * ADirView = DirView(Side);
  8237. TFileFilter Filter;
  8238. DefaultFileFilter(Filter);
  8239. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8240. {
  8241. Configuration->Usage->Inc(L"MaskSelections");
  8242. ADirView->SelectFiles(Filter, Select);
  8243. ADirView->SetFocus();
  8244. }
  8245. }
  8246. //---------------------------------------------------------------------------
  8247. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8248. {
  8249. TCustomDirView * ADirView = DirView(Side);
  8250. ADirView->RestoreSelectedNames();
  8251. ADirView->SetFocus();
  8252. }
  8253. //---------------------------------------------------------------------------
  8254. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8255. {
  8256. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8257. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8258. {
  8259. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8260. UnicodeString Ext;
  8261. if (!IsSideLocalBrowser(osCurrent))
  8262. {
  8263. Ext = UnixExtractFileExt(FileName);
  8264. }
  8265. else
  8266. {
  8267. Ext = ExtractFileExt(FileName);
  8268. }
  8269. if (Ext.IsEmpty())
  8270. {
  8271. Ext = L".";
  8272. }
  8273. TFileFilter Filter;
  8274. Filter.Masks = FORMAT(L"*%s", (Ext));
  8275. Filter.Directories = false;
  8276. CurrentDirView->SelectFiles(Filter, Select);
  8277. }
  8278. }
  8279. //---------------------------------------------------------------------------
  8280. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8281. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8282. {
  8283. UnicodeString Result;
  8284. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8285. {
  8286. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8287. }
  8288. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8289. {
  8290. // noop
  8291. }
  8292. else
  8293. {
  8294. Result =
  8295. FMTLOAD(FILE_INFO_FORMAT,
  8296. (FormatBytes(FileInfo.SelectedSize),
  8297. FormatBytes(FileInfo.FilesSize),
  8298. FormatNumber(FileInfo.SelectedCount),
  8299. FormatNumber(FileInfo.FilesCount)));
  8300. }
  8301. return Result;
  8302. }
  8303. //---------------------------------------------------------------------------
  8304. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8305. TTBXStatusPanel * Panel, TOperationSide Side)
  8306. {
  8307. if (Panel->Index == 1)
  8308. {
  8309. ToggleShowHiddenFiles();
  8310. }
  8311. else if (Panel->Index == 2)
  8312. {
  8313. Filter(Side);
  8314. }
  8315. }
  8316. //---------------------------------------------------------------------------
  8317. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8318. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8319. {
  8320. DebugAssert(!StatusBar->SimplePanel);
  8321. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8322. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8323. }
  8324. //---------------------------------------------------------------------------
  8325. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8326. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8327. {
  8328. DebugAssert(StatusBar->Panels->Count >= 3);
  8329. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8330. if (FileInfo.HiddenCount > 0)
  8331. {
  8332. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8333. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8334. }
  8335. else
  8336. {
  8337. HiddenFilesPanel->ViewPriority = 0;
  8338. // not really necessary, just to cleanup no-longer-valid data
  8339. HiddenFilesPanel->Caption = L"";
  8340. }
  8341. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8342. if (FileInfo.FilteredCount > 0)
  8343. {
  8344. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8345. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8346. }
  8347. else
  8348. {
  8349. FilteredFilesPanel->ViewPriority = 0;
  8350. // not really necessary, just to cleanup no-longer-valid data
  8351. FilteredFilesPanel->Caption = L"";
  8352. }
  8353. }
  8354. //---------------------------------------------------------------------------
  8355. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8356. TObject * /*Sender*/)
  8357. {
  8358. if (DirView(osOther)->Enabled)
  8359. {
  8360. DirView(osOther)->SetFocus();
  8361. }
  8362. }
  8363. //---------------------------------------------------------------------------
  8364. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8365. {
  8366. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8367. switch (Config.Show)
  8368. {
  8369. case qvShow:
  8370. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8371. {
  8372. Config.Show = qvHide;
  8373. }
  8374. else
  8375. {
  8376. Config.Show = Config.LastHideShow;
  8377. }
  8378. break;
  8379. case qvHideWhenEmpty:
  8380. if (ComponentVisible[fcQueueView])
  8381. {
  8382. Config.Show = qvHide;
  8383. }
  8384. else
  8385. {
  8386. Config.LastHideShow = Config.Show;
  8387. Config.Show = qvShow;
  8388. }
  8389. break;
  8390. case qvHide:
  8391. Config.LastHideShow = Config.Show;
  8392. Config.Show = qvShow;
  8393. break;
  8394. }
  8395. WinConfiguration->QueueView = Config;
  8396. }
  8397. //---------------------------------------------------------------------------
  8398. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8399. {
  8400. UnicodeString Result;
  8401. if (Terminal != NULL)
  8402. {
  8403. switch (WinConfiguration->PathInCaption)
  8404. {
  8405. case picShort:
  8406. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8407. break;
  8408. case picFull:
  8409. Result = Terminal->CurrentDirectory;
  8410. break;
  8411. }
  8412. }
  8413. return Result;
  8414. }
  8415. //---------------------------------------------------------------------------
  8416. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8417. {
  8418. TColor Result = GetWindowColor(FSessionColor);
  8419. return Result;
  8420. }
  8421. //---------------------------------------------------------------------------
  8422. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8423. {
  8424. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8425. return Result;
  8426. }
  8427. //---------------------------------------------------------------------------
  8428. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8429. {
  8430. if (!FSessionChanging)
  8431. {
  8432. TTerminalManager::Instance()->UpdateAppTitle();
  8433. }
  8434. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8435. // background to gray on Windows 7 (works on Windows 10).
  8436. // See also EnableControl
  8437. if (Showing)
  8438. {
  8439. if (ManagedSession != NULL)
  8440. {
  8441. // Update path when it changes
  8442. if ((SessionsPageControl->ActivePage != NULL) &&
  8443. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8444. {
  8445. UpdateSessionTab(SessionsPageControl->ActivePage);
  8446. }
  8447. }
  8448. NonVisualDataModule->ReconnectSessionAction->Update();
  8449. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8450. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8451. CenterReconnectToolbar();
  8452. bool HasTerminal = HasActiveTerminal();
  8453. RemoteDirView->Enabled = HasTerminal;
  8454. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8455. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8456. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8457. RemoteDriveView->Color = RemoteDirView->Color;
  8458. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8459. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8460. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8461. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8462. QueueFileList->Enabled = QueueView3->Enabled;
  8463. QueueFileList->Color = QueueView3->Color;
  8464. QueueFileList->Font->Color = QueueView3->Font->Color;
  8465. QueueLabelUpdateStatus();
  8466. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8467. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8468. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8469. {
  8470. UpdateDarkMode();
  8471. }
  8472. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8473. }
  8474. }
  8475. //---------------------------------------------------------------------------
  8476. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8477. {
  8478. UpdateControls();
  8479. }
  8480. //---------------------------------------------------------------------------
  8481. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8482. TObject * Sender)
  8483. {
  8484. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8485. DebugAssert(DirView != NULL);
  8486. DoDirViewLoaded(DirView);
  8487. TransferPresetAutoSelect();
  8488. }
  8489. //---------------------------------------------------------------------------
  8490. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8491. {
  8492. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8493. // first run after installation
  8494. if (double(Updates.LastCheck) == 0)
  8495. {
  8496. // make sure next time there will be an update (if enabled)
  8497. Updates.LastCheck = TDateTime(1);
  8498. WinConfiguration->Updates = Updates;
  8499. }
  8500. else if ((double(Updates.Period) > 0) &&
  8501. (Now() - Updates.LastCheck >= Updates.Period))
  8502. {
  8503. TThreadMethod OnUpdatesChecked = NULL;
  8504. if (!IsUWP())
  8505. {
  8506. OnUpdatesChecked = UpdatesChecked;
  8507. }
  8508. StartUpdateThread(OnUpdatesChecked);
  8509. }
  8510. }
  8511. //---------------------------------------------------------------------------
  8512. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8513. {
  8514. UnicodeString Message;
  8515. bool New;
  8516. TQueryType Type;
  8517. GetUpdatesMessage(Message, New, Type, false);
  8518. if (!Message.IsEmpty())
  8519. {
  8520. if (New)
  8521. {
  8522. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8523. }
  8524. if (!New && (Type != qtWarning))
  8525. {
  8526. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8527. }
  8528. else
  8529. {
  8530. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8531. }
  8532. if (New)
  8533. {
  8534. Configuration->Usage->Inc(L"UpdateNotifications");
  8535. }
  8536. }
  8537. }
  8538. //---------------------------------------------------------------------------
  8539. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8540. {
  8541. RestoreApp();
  8542. bool CanDisplay = !NonVisualDataModule->Busy;
  8543. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8544. {
  8545. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8546. }
  8547. if (CanDisplay)
  8548. {
  8549. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8550. CheckForUpdates(true);
  8551. }
  8552. else
  8553. {
  8554. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8555. }
  8556. }
  8557. //---------------------------------------------------------------------------
  8558. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8559. TCopyParamRuleData & Data)
  8560. {
  8561. DebugAssert(Terminal != NULL);
  8562. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8563. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8564. Data.RemoteDirectory = RemoteDirView->PathName;
  8565. }
  8566. //---------------------------------------------------------------------------
  8567. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8568. {
  8569. // Terminal can be null when we are changing local directory implicitly,
  8570. // because it has been deleted, while no session is connected
  8571. // (Login dialog is open)
  8572. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8573. {
  8574. DebugAssert(!IsLocalBrowserMode());
  8575. TCopyParamRuleData Data;
  8576. GetTransferPresetAutoSelectData(Data);
  8577. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8578. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8579. if (CopyParamIndex < 0)
  8580. {
  8581. // there is no preset that matches autoselection
  8582. // set preset that we consider "default"
  8583. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8584. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8585. }
  8586. else
  8587. {
  8588. // there is preset matching autoselection
  8589. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8590. if (CopyParamName == FCopyParamAutoSelected)
  8591. {
  8592. // autoselected the same preset as the last time
  8593. // make no change (i.e. preserve custom user preset, if any)
  8594. }
  8595. else
  8596. {
  8597. // autoselected the different preset then the last time (or there
  8598. // was default preset set)
  8599. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8600. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8601. }
  8602. }
  8603. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8604. {
  8605. if (CopyParamIndex >= 0)
  8606. {
  8607. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8608. }
  8609. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8610. try
  8611. {
  8612. int Fmt =
  8613. (CopyParamIndex < 0) ?
  8614. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8615. COPY_PARAM_AUTOSELECTED;
  8616. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8617. Data->Message = MainInstructions(Message);
  8618. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8619. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8620. CopyParamAttrs);
  8621. if (CopyParamIndex >= 0)
  8622. {
  8623. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8624. Info = FORMAT(L"%s\n \n%s", (Info,
  8625. FMTLOAD(COPY_PARAM_RULE,
  8626. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8627. }
  8628. Data->Message += L"\n\n" + Info;
  8629. if (WinConfiguration->CopyParamAutoSelectNotice)
  8630. {
  8631. TransferPresetNoteMessage(Data, true);
  8632. }
  8633. else
  8634. {
  8635. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8636. Data = NULL; // ownership passed
  8637. }
  8638. }
  8639. __finally
  8640. {
  8641. delete Data;
  8642. }
  8643. }
  8644. }
  8645. }
  8646. //---------------------------------------------------------------------------
  8647. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8648. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8649. {
  8650. DebugAssert(NoteData != NULL);
  8651. DebugAssert(!IsLocalBrowserMode());
  8652. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8653. TQueryButtonAlias Aliases[1];
  8654. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8655. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8656. Params.Aliases = Aliases;
  8657. Params.AliasesCount = LENOF(Aliases);
  8658. unsigned int Result =
  8659. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8660. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8661. switch (Result)
  8662. {
  8663. case qaNeverAskAgain:
  8664. DebugAssert(AllowNeverAskAgain);
  8665. WinConfiguration->CopyParamAutoSelectNotice = false;
  8666. break;
  8667. case qaIgnore:
  8668. PreferencesDialog(pmPresets);
  8669. break;
  8670. }
  8671. }
  8672. //---------------------------------------------------------------------------
  8673. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8674. {
  8675. // as of now useless, as this is used for notes only, never for balloons, ...
  8676. RestoreApp();
  8677. // .. and we should never be busy here
  8678. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8679. {
  8680. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8681. }
  8682. }
  8683. //---------------------------------------------------------------------------
  8684. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8685. TPreferencesMode APreferencesMode)
  8686. {
  8687. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8688. TCopyParamRuleData Data;
  8689. if (Terminal != NULL)
  8690. {
  8691. PreferencesData.reset(new TPreferencesDialogData());
  8692. GetTransferPresetAutoSelectData(Data);
  8693. PreferencesData->CopyParamRuleData = &Data;
  8694. }
  8695. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8696. }
  8697. //---------------------------------------------------------------------------
  8698. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8699. const TCustomCommandType & Command)
  8700. {
  8701. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8702. {
  8703. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8704. }
  8705. }
  8706. //---------------------------------------------------------------------------
  8707. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8708. {
  8709. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8710. TCustomCommandType Command;
  8711. // make sure we use local custom command when remote are not supported
  8712. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8713. {
  8714. Command = FLastCustomCommand;
  8715. }
  8716. else
  8717. {
  8718. Command.Params = Command.Params | ccLocal;
  8719. }
  8720. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8721. FEditingFocusedAdHocCommand = OnFocused;
  8722. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8723. int Options =
  8724. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8725. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8726. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8727. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8728. {
  8729. FLastCustomCommand = Command;
  8730. UpdateCustomCommandsToolbar();
  8731. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8732. }
  8733. }
  8734. //---------------------------------------------------------------------------
  8735. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8736. {
  8737. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8738. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8739. DebugAssert(State > 0);
  8740. if (State <= 0)
  8741. {
  8742. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8743. }
  8744. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8745. }
  8746. //---------------------------------------------------------------------------
  8747. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8748. TCustomCommandType & Command, int & State)
  8749. {
  8750. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8751. if (Result)
  8752. {
  8753. Command = FLastCustomCommand;
  8754. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8755. }
  8756. return Result;
  8757. }
  8758. //---------------------------------------------------------------------------
  8759. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8760. {
  8761. ResetIncrementalSearch();
  8762. }
  8763. //---------------------------------------------------------------------------
  8764. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8765. {
  8766. DebugAssert(ComponentVisible[Component]);
  8767. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8768. }
  8769. //---------------------------------------------------------------------------
  8770. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8771. {
  8772. // when queue panel is resized here directly, the status bar is stretched
  8773. // over whole space the panel occupied
  8774. PostComponentHide(fcQueueView);
  8775. }
  8776. //---------------------------------------------------------------------------
  8777. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8778. {
  8779. ComponentVisible[fcQueueFileList] = false;
  8780. }
  8781. //---------------------------------------------------------------------------
  8782. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8783. {
  8784. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8785. RefreshColorMode();
  8786. ResetSysDarkTheme();
  8787. ConfigurationChanged();
  8788. ConfigureInterface();
  8789. // Should be called for all controls
  8790. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8791. }
  8792. //---------------------------------------------------------------------------
  8793. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8794. {
  8795. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8796. if (!FInvalid &&
  8797. (Message.LParam != 0) &&
  8798. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8799. {
  8800. ThemeChanged();
  8801. }
  8802. TForm::Dispatch(&Message);
  8803. }
  8804. //---------------------------------------------------------------------------
  8805. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8806. {
  8807. TMessage * M = static_cast<TMessage*>(Message);
  8808. switch (M->Msg)
  8809. {
  8810. case CM_DIALOGCHAR:
  8811. CMDialogChar(*M);
  8812. break;
  8813. case WM_APPCOMMAND:
  8814. WMAppCommand(*M);
  8815. break;
  8816. case WM_SYSCOMMAND:
  8817. WMSysCommand(*M);
  8818. break;
  8819. case WM_QUERYENDSESSION:
  8820. WMQueryEndSession(*M);
  8821. break;
  8822. case WM_ENDSESSION:
  8823. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8824. break;
  8825. #ifdef _DEBUG
  8826. case WM_WINDOWPOSCHANGED:
  8827. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8828. break;
  8829. case CM_CUSTOMSTYLECHANGED:
  8830. TForm::Dispatch(Message);
  8831. break;
  8832. #endif
  8833. case WM_COMPONENT_HIDE:
  8834. {
  8835. Byte Component = static_cast<Byte>(M->WParam);
  8836. // sanity check
  8837. if (ComponentVisible[Component])
  8838. {
  8839. // special treatment
  8840. if (Component == fcQueueView)
  8841. {
  8842. ToggleQueueVisibility();
  8843. DebugAssert(!ComponentVisible[fcQueueView]);
  8844. }
  8845. else
  8846. {
  8847. ComponentVisible[Component] = false;
  8848. }
  8849. }
  8850. }
  8851. break;
  8852. case WM_COPYDATA:
  8853. WMCopyData(*M);
  8854. break;
  8855. case WM_MANAGES_CAPTION:
  8856. // caption managed in TTerminalManager::UpdateAppTitle()
  8857. M->Result = 1;
  8858. break;
  8859. case WM_WANTS_MOUSEWHEEL:
  8860. M->Result = 1;
  8861. break;
  8862. case CM_SHOWINGCHANGED:
  8863. CMShowingChanged(*M);
  8864. break;
  8865. case WM_CLOSE:
  8866. WMClose(*M);
  8867. break;
  8868. case WM_DPICHANGED:
  8869. WMDpiChanged(*M);
  8870. break;
  8871. case CM_DPICHANGED:
  8872. CMDpiChanged(*M);
  8873. break;
  8874. case WM_SETTINGCHANGE:
  8875. WMSettingChange(*M);
  8876. break;
  8877. case CM_DIALOGKEY:
  8878. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8879. break;
  8880. default:
  8881. TForm::Dispatch(Message);
  8882. break;
  8883. }
  8884. }
  8885. //---------------------------------------------------------------------------
  8886. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8887. {
  8888. // noop
  8889. }
  8890. //---------------------------------------------------------------------------
  8891. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8892. {
  8893. TForm::Dispatch(&Message);
  8894. FSessionColors->Clear();
  8895. AddFixedSessionImages();
  8896. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8897. UpdateImages();
  8898. }
  8899. //---------------------------------------------------------------------------
  8900. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8901. {
  8902. TForm::Dispatch(&Message);
  8903. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8904. UpdatePixelsPerInchMainWindowCounter();
  8905. }
  8906. //---------------------------------------------------------------------------
  8907. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8908. {
  8909. // Cannot close window while we are busy.
  8910. // We cannot test this in FormCloseQuery as that is called also from
  8911. // Close(), which is called by CloseApplicationAction. So we can be busy
  8912. // there already even, when it is legitimate to close the application.
  8913. // Possibly a better place to handle this would be WMSysCommand.
  8914. if (NonVisualDataModule->Busy)
  8915. {
  8916. Message.Result = 1;
  8917. }
  8918. else
  8919. {
  8920. TForm::Dispatch(&Message);
  8921. }
  8922. }
  8923. //---------------------------------------------------------------------------
  8924. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8925. {
  8926. if (!FShowingChanged)
  8927. {
  8928. AddStartupSequence(L"D");
  8929. FShowingChanged = true;
  8930. }
  8931. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8932. bool NoSession = (ManagedSession == NULL);
  8933. // This calls TCustomScpExplorerForm::DoShow
  8934. TForm::Dispatch(&Message);
  8935. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8936. if (Showing && !FStarted)
  8937. {
  8938. FStarted = true;
  8939. InterfaceStarted();
  8940. }
  8941. if (Showing && NoSession)
  8942. {
  8943. // When we are starting minimized (i.e. from an installer),
  8944. // postpone showing Login dialog until we get restored.
  8945. // Otherwise the Login dialog (and Authentication window) show restored
  8946. // over invisible (minimized) main window.
  8947. if (WindowState == wsMinimized)
  8948. {
  8949. FNeedSession = true;
  8950. }
  8951. else
  8952. {
  8953. // This happens before application ever goes idle, so the toolbars would
  8954. // stay enabled (initial state) until the Login dialog is dismissed.
  8955. UpdateActions();
  8956. NonVisualDataModule->StartBusy();
  8957. try
  8958. {
  8959. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8960. // otherwise the dialog does not receive focus.
  8961. // With Commander interface the ProcessMessages is called already
  8962. // by TDriveView, but with Explorer interface, we need to call it explicily
  8963. Application->ProcessMessages();
  8964. // do not reload sessions, they have been loaded just now (optimization)
  8965. NeedSession(true);
  8966. }
  8967. __finally
  8968. {
  8969. NonVisualDataModule->EndBusy();
  8970. }
  8971. }
  8972. }
  8973. }
  8974. //---------------------------------------------------------------------------
  8975. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8976. {
  8977. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8978. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8979. }
  8980. //---------------------------------------------------------------------------
  8981. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8982. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8983. int & MaxHeight)
  8984. {
  8985. // workaround for bug in TWinControl.CalcConstraints
  8986. // Check for empty rect (restore from iconic state) is done there only after
  8987. // call to AdjustClientRect, which enlarges the rect (for forms).
  8988. TRect R = GetClientRect();
  8989. // when restoring from iconic state, place no restrictions
  8990. if (IsRectEmpty(R))
  8991. {
  8992. MinWidth = 0;
  8993. MinHeight = 0;
  8994. MaxWidth = 32000;
  8995. MaxHeight = 32000;
  8996. }
  8997. }
  8998. //---------------------------------------------------------------------------
  8999. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9000. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9001. {
  9002. // terminal can be already closed (e.g. dropped connection)
  9003. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9004. {
  9005. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9006. try
  9007. {
  9008. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9009. }
  9010. catch(...)
  9011. {
  9012. if (!Terminal->Active)
  9013. {
  9014. Close = true;
  9015. }
  9016. throw;
  9017. }
  9018. }
  9019. }
  9020. //---------------------------------------------------------------------------
  9021. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9022. {
  9023. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9024. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9025. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9026. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9027. {
  9028. OnGetSpaceAvailable = GetSpaceAvailable;
  9029. }
  9030. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9031. OnGetSpaceAvailable);
  9032. }
  9033. //---------------------------------------------------------------------------
  9034. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9035. {
  9036. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9037. Terminal->FillSessionDataForCode(Data.get());
  9038. return Data.release();
  9039. }
  9040. //---------------------------------------------------------------------------
  9041. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9042. {
  9043. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9044. DoGenerateUrlDialog(Data.get(), Paths);
  9045. }
  9046. //---------------------------------------------------------------------------
  9047. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9048. {
  9049. GenerateUrl(NULL);
  9050. }
  9051. //---------------------------------------------------------------------------
  9052. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9053. {
  9054. std::unique_ptr<TStrings> Paths(new TStringList());
  9055. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9056. GenerateUrl(Paths.get());
  9057. }
  9058. //---------------------------------------------------------------------------
  9059. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9060. {
  9061. FSessionColor = value;
  9062. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9063. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9064. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9065. DebugAssert(ColorItem != NULL);
  9066. ColorItem->Color = C;
  9067. NonVisualDataModule->ColorMenuItem->Color = C;
  9068. UpdateControls();
  9069. }
  9070. //---------------------------------------------------------------------------
  9071. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9072. {
  9073. if (value != FSessionColor)
  9074. {
  9075. UpdateSessionColor(value);
  9076. // Is null when called from LastTerminalClosed
  9077. if (Terminal != NULL)
  9078. {
  9079. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9080. }
  9081. }
  9082. }
  9083. //---------------------------------------------------------------------------
  9084. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9085. {
  9086. bool Result = FNoteTimer->Enabled;
  9087. if (Result)
  9088. {
  9089. // cannot cancel note too early
  9090. bool NotEarly =
  9091. (Now() - FNoteShown >
  9092. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9093. if (Force || NotEarly)
  9094. {
  9095. FNoteTimer->Enabled = false;
  9096. FNote = L"";
  9097. SAFE_DESTROY(FNoteData);
  9098. FOnNoteClick = NULL;
  9099. FNoteHints = FNotes->Text;
  9100. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9101. UpdateStatusBar();
  9102. }
  9103. }
  9104. return Result;
  9105. }
  9106. //---------------------------------------------------------------------------
  9107. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9108. {
  9109. DebugAssert(FNoteTimer->Enabled);
  9110. CancelNote(true);
  9111. }
  9112. //---------------------------------------------------------------------------
  9113. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9114. {
  9115. FNoteHints = FNotes->Text.TrimRight();
  9116. }
  9117. //---------------------------------------------------------------------------
  9118. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9119. {
  9120. int P = Note.Pos(L"\n");
  9121. if (P > 0)
  9122. {
  9123. Note.SetLength(P - 1);
  9124. }
  9125. FNotes->Add(FORMAT(L"[%s] %s",
  9126. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9127. while (FNotes->Count > 10)
  9128. {
  9129. FNotes->Delete(0);
  9130. }
  9131. if (UpdateNow)
  9132. {
  9133. UpdateNoteHints();
  9134. UpdateStatusBar();
  9135. }
  9136. }
  9137. //---------------------------------------------------------------------------
  9138. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9139. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9140. {
  9141. int P = Note.Pos(L"\n");
  9142. if (P > 0)
  9143. {
  9144. Note.SetLength(P - 1);
  9145. }
  9146. UpdateNoteHints();
  9147. FNote = Note;
  9148. SAFE_DESTROY(FNoteData);
  9149. FNoteData = NoteData;
  9150. FOnNoteClick = OnNoteClick;
  9151. AddNote(Note, false);
  9152. UpdateStatusBar();
  9153. FNoteShown = Now();
  9154. FNoteTimer->Enabled = false;
  9155. if (Seconds == 0)
  9156. {
  9157. Seconds = WinConfiguration->NotificationsTimeout;
  9158. }
  9159. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9160. FNoteTimer->Enabled = true;
  9161. }
  9162. //---------------------------------------------------------------------------
  9163. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9164. {
  9165. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9166. }
  9167. //---------------------------------------------------------------------------
  9168. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9169. {
  9170. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9171. {
  9172. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9173. }
  9174. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9175. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9176. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9177. 0, NULL, NULL);
  9178. }
  9179. //---------------------------------------------------------------------------
  9180. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9181. {
  9182. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9183. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9184. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9185. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9186. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9187. }
  9188. //---------------------------------------------------------------------------
  9189. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9190. {
  9191. WinConfiguration->FormatSizeBytes = Style;
  9192. }
  9193. //---------------------------------------------------------------------------
  9194. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9195. {
  9196. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9197. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9198. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9199. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9200. }
  9201. //---------------------------------------------------------------------------
  9202. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9203. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9204. {
  9205. if (Panel->Index == 0)
  9206. {
  9207. if (FOnNoteClick != NULL)
  9208. {
  9209. // prevent the user data from being freed by possible call
  9210. // to CancelNote or PostNote during call to OnNoteClick
  9211. std::unique_ptr<TObject> NoteData(FNoteData);
  9212. TNotifyEvent OnNoteClick = FOnNoteClick;
  9213. FNoteData = NULL;
  9214. // need to cancel the note as we are going to delete its user data
  9215. CancelNote(true);
  9216. OnNoteClick(NoteData.get());
  9217. }
  9218. }
  9219. if (HasActiveTerminal() &&
  9220. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9221. {
  9222. FileSystemInfo();
  9223. }
  9224. }
  9225. //---------------------------------------------------------------------------
  9226. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9227. {
  9228. // workaround:
  9229. // 1) for unknown reason, disabling window, while minimized,
  9230. // prevents it from restoring, even if it was enabled again meanwhile
  9231. // 2) when disabling the main window, while another has focus
  9232. // minimize is no longer possible
  9233. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9234. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9235. // Shouldn't we use IsApplicationMinimized() here?
  9236. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9237. {
  9238. Enabled = false;
  9239. }
  9240. FLockLevel++;
  9241. }
  9242. //---------------------------------------------------------------------------
  9243. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9244. {
  9245. DebugAssert(FLockLevel > 0);
  9246. FLockLevel--;
  9247. if (FLockLevel == 0)
  9248. {
  9249. DebugAssert(FLockSuspendLevel == 0);
  9250. Enabled = true;
  9251. }
  9252. }
  9253. //---------------------------------------------------------------------------
  9254. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9255. {
  9256. // We need to make sure that window is enabled when the last modal window closes
  9257. // otherwise focus is not restored correctly.
  9258. // So we re-enable the window when modal window opens and
  9259. // disable it back after is closes
  9260. if (FLockLevel > 0)
  9261. {
  9262. // while we have nesting counter, we know that we never be called
  9263. // recursivelly as Application->OnModalBegin is called only
  9264. // for the top-level modal window
  9265. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9266. {
  9267. // won't be disabled when conditions in LockWindow() were not satisfied
  9268. FDisabledOnLockSuspend = !Enabled;
  9269. // When minimized to tray (or actually when set to SW_HIDE),
  9270. // setting Enabled makes the window focusable even when there's
  9271. // modal window over it
  9272. if (!FTrayIcon->Visible)
  9273. {
  9274. Enabled = true;
  9275. }
  9276. }
  9277. FLockSuspendLevel++;
  9278. }
  9279. }
  9280. //---------------------------------------------------------------------------
  9281. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9282. {
  9283. if (FLockSuspendLevel > 0)
  9284. {
  9285. DebugAssert(FLockLevel > 0);
  9286. FLockSuspendLevel--;
  9287. // see comment in SuspendWindowLock
  9288. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9289. {
  9290. // Note that window can be enabled here, when we were minized to tray when
  9291. // was SuspendWindowLock() called.
  9292. // We should possibly do the same check as in LockWindow(),
  9293. // if it is ever possible that the consitions change between
  9294. // SuspendWindowLock() and ResumeWindowLock()
  9295. Enabled = !FDisabledOnLockSuspend;
  9296. }
  9297. }
  9298. }
  9299. //---------------------------------------------------------------------------
  9300. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9301. {
  9302. TTBXComboBoxItem * RemotePathComboBox =
  9303. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9304. if (!TextOnly)
  9305. {
  9306. TStrings * Items = RemotePathComboBox->Strings;
  9307. Items->BeginUpdate();
  9308. try
  9309. {
  9310. Items->Clear();
  9311. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9312. {
  9313. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9314. while (!IsUnixRootPath(APath))
  9315. {
  9316. int P = APath.LastDelimiter(L'/');
  9317. DebugAssert(P >= 0);
  9318. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9319. APath.SetLength(P - 1);
  9320. }
  9321. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9322. }
  9323. }
  9324. __finally
  9325. {
  9326. Items->EndUpdate();
  9327. }
  9328. }
  9329. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9330. // Setting ItemIndex to -1 does not reset its text
  9331. if (RemotePathComboBox->Strings->Count == 0)
  9332. {
  9333. RemotePathComboBox->Text = L"";
  9334. }
  9335. }
  9336. //---------------------------------------------------------------------------
  9337. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9338. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9339. {
  9340. if (AIndex < 0)
  9341. {
  9342. AIndex = Sender->ItemIndex;
  9343. }
  9344. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9345. }
  9346. //---------------------------------------------------------------------------
  9347. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9348. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9349. int & ImageIndex)
  9350. {
  9351. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9352. }
  9353. //---------------------------------------------------------------------------
  9354. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9355. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9356. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9357. {
  9358. if (!IsLocalBrowserMode())
  9359. {
  9360. ARect.Left += (10 * AIndex);
  9361. }
  9362. }
  9363. //---------------------------------------------------------------------------
  9364. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9365. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9366. {
  9367. AWidth += (10 * AIndex);
  9368. }
  9369. //---------------------------------------------------------------------------
  9370. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9371. {
  9372. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9373. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9374. int Index = RemotePathComboBox->ItemIndex;
  9375. while (Index < RemotePathComboBox->Strings->Count - 1)
  9376. {
  9377. APath = UnixExtractFileDir(APath);
  9378. Index++;
  9379. }
  9380. // VanDyke style paths
  9381. if (APath.IsEmpty())
  9382. {
  9383. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9384. APath = ROOTDIRECTORY;
  9385. }
  9386. if (RemoteDirView->Path != APath)
  9387. {
  9388. RemoteDirView->Path = APath;
  9389. }
  9390. }
  9391. //---------------------------------------------------------------------------
  9392. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9393. {
  9394. DoRemotePathComboBoxItemClick(Sender);
  9395. }
  9396. //---------------------------------------------------------------------------
  9397. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9398. {
  9399. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9400. DebugUsedParam(Sender);
  9401. UpdateRemotePathComboBox(true);
  9402. }
  9403. //---------------------------------------------------------------------------
  9404. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9405. {
  9406. DoRemotePathComboBoxCancel(Sender);
  9407. }
  9408. //---------------------------------------------------------------------------
  9409. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9410. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9411. {
  9412. ResetIncrementalSearch();
  9413. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9414. DebugAssert(DirView != NULL);
  9415. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9416. {
  9417. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9418. // OnEditing is called also from TCustomListView::CanEdit
  9419. if (Edit != NULL)
  9420. {
  9421. EditSelectBaseName(Edit);
  9422. }
  9423. }
  9424. }
  9425. //---------------------------------------------------------------------------
  9426. //---------------------------------------------------------------------------
  9427. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9428. {
  9429. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9430. Result->AutoDetectDnD = false;
  9431. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9432. Result->RenderDataOn = rdoEnterAndDropSync;
  9433. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9434. return Result;
  9435. }
  9436. //---------------------------------------------------------------------------
  9437. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9438. {
  9439. if (IsWin10Build(2004))
  9440. {
  9441. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9442. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9443. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9444. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9445. }
  9446. }
  9447. //---------------------------------------------------------------------------
  9448. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9449. {
  9450. TForm::CreateWnd();
  9451. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9452. UpdateDarkMode();
  9453. if (FSessionsDragDropFilesEx == NULL)
  9454. {
  9455. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9456. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9457. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9458. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9459. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9460. }
  9461. if (FQueueDragDropFilesEx == NULL)
  9462. {
  9463. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9464. // No need to set OnDragOver as we do not have any restrictions
  9465. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9466. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9467. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9468. }
  9469. }
  9470. //---------------------------------------------------------------------------
  9471. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9472. {
  9473. TForm::DestroyWnd();
  9474. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9475. FQueueDragDropFilesEx->DragDropControl = NULL;
  9476. }
  9477. //---------------------------------------------------------------------------
  9478. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9479. {
  9480. SideEnter(FCurrentSide);
  9481. FEverShown = true;
  9482. }
  9483. //---------------------------------------------------------------------------
  9484. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9485. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9486. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9487. {
  9488. if (!NonVisualDataModule->Busy)
  9489. {
  9490. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9491. Configuration->Usage->Inc(L"FileFinds");
  9492. LockWindow(true);
  9493. NonVisualDataModule->StartBusy();
  9494. WinConfiguration->LockedInterface = true;
  9495. try
  9496. {
  9497. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9498. }
  9499. __finally
  9500. {
  9501. WinConfiguration->LockedInterface = false;
  9502. NonVisualDataModule->EndBusy();
  9503. UnlockWindow();
  9504. }
  9505. }
  9506. }
  9507. //---------------------------------------------------------------------------
  9508. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9509. {
  9510. if (!NonVisualDataModule->Busy)
  9511. {
  9512. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9513. SetFocus();
  9514. RemoteDirView->Path = UnixExtractFilePath(Path);
  9515. UnicodeString FileName = UnixExtractFileName(Path);
  9516. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9517. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9518. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9519. if (Item == NULL)
  9520. {
  9521. ReloadDirectory(osRemote);
  9522. Item = RemoteDirView->FindFileItem(FileName);
  9523. }
  9524. if (Item != NULL)
  9525. {
  9526. RemoteDirView->ItemFocused = Item;
  9527. RemoteDirView->ItemFocused->MakeVisible(false);
  9528. RemoteDirView->SetFocus();
  9529. }
  9530. }
  9531. }
  9532. //---------------------------------------------------------------------------
  9533. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9534. {
  9535. bool Result = !NonVisualDataModule->Busy;
  9536. if (Result)
  9537. {
  9538. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9539. }
  9540. return Result;
  9541. }
  9542. //---------------------------------------------------------------------------
  9543. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9544. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9545. {
  9546. if (CanOperateOnFoundFiles(ATerminal))
  9547. {
  9548. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9549. FOnFileOperationFinished = OnFileOperationFinished;
  9550. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9551. }
  9552. }
  9553. //---------------------------------------------------------------------------
  9554. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9555. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9556. {
  9557. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9558. }
  9559. //---------------------------------------------------------------------------
  9560. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9561. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9562. {
  9563. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9564. }
  9565. //---------------------------------------------------------------------------
  9566. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9567. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9568. {
  9569. if (CanOperateOnFoundFiles(ATerminal))
  9570. {
  9571. for (int Index = 0; Index < FileList->Count; Index++)
  9572. {
  9573. UnicodeString FileName = FileList->Strings[Index];
  9574. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9575. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9576. OnFileOperationFinished(osRemote, FileName, true);
  9577. }
  9578. }
  9579. }
  9580. //---------------------------------------------------------------------------
  9581. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9582. {
  9583. FFileFindTerminal = Terminal;
  9584. ShowFileFindDialog(
  9585. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9586. DoDownloadFoundFiles, DoEditFoundFiles);
  9587. }
  9588. //---------------------------------------------------------------------------
  9589. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9590. {
  9591. FTaskbarList = TaskbarList;
  9592. }
  9593. //---------------------------------------------------------------------------
  9594. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9595. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9596. {
  9597. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9598. if (Index >= 0)
  9599. {
  9600. if (Button == mbLeft)
  9601. {
  9602. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9603. // If switching tab (switching session) takes long enough for user
  9604. // to actually release the button, "mouse down" is still raised,
  9605. // but we do not get "mouse up" event, so dragging is not cancelled,
  9606. // prevent that by not beginning dragging in the first place.
  9607. if (IsKeyPressed(VK_LBUTTON))
  9608. {
  9609. // when user clicks the "+", we get mouse down only after the session
  9610. // is closed, when new session tab is already on X:Y, so dragging
  9611. // starts, prevent that
  9612. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9613. {
  9614. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9615. if (Terminal != NULL)
  9616. {
  9617. SessionsPageControl->BeginDrag(false);
  9618. }
  9619. }
  9620. }
  9621. }
  9622. else if (Button == mbMiddle)
  9623. {
  9624. // ignore middle-click for "New session tab" and the last local-local tab
  9625. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9626. if ((Session != NULL) && CanCloseSession(Session))
  9627. {
  9628. CloseSessionTab(Index);
  9629. }
  9630. }
  9631. }
  9632. }
  9633. //---------------------------------------------------------------------------
  9634. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9635. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9636. {
  9637. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9638. // do not allow dropping on the "+" tab
  9639. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9640. if ((TargetTerminal != NULL) &&
  9641. (SessionsPageControl->ActivePage->PageIndex != Index))
  9642. {
  9643. Configuration->Usage->Inc(L"SessionTabMoves");
  9644. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9645. // but we want to actually prevent that to avoid flicker
  9646. SessionsPageControl->ActivePage->PageIndex = Index;
  9647. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9648. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9649. }
  9650. }
  9651. //---------------------------------------------------------------------------
  9652. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9653. TObject * Sender, TObject * Source, int X, int Y,
  9654. TDragState /*State*/, bool & Accept)
  9655. {
  9656. Accept = (Sender == Source);
  9657. if (Accept)
  9658. {
  9659. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9660. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9661. // do not allow dragging to the "+" tab
  9662. Accept = (Terminal != NULL);
  9663. }
  9664. }
  9665. //---------------------------------------------------------------------------
  9666. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9667. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9668. {
  9669. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9670. if (Index < 0)
  9671. {
  9672. Effect = DROPEFFECT_NONE;
  9673. }
  9674. else
  9675. {
  9676. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9677. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9678. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9679. {
  9680. Effect = DROPEFFECT_NONE;
  9681. }
  9682. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9683. {
  9684. Effect = DROPEFFECT_COPY;
  9685. }
  9686. }
  9687. }
  9688. //---------------------------------------------------------------------------
  9689. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9690. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9691. {
  9692. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9693. // do not allow dropping on local-local, disconnected and the "+" tab
  9694. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9695. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9696. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9697. {
  9698. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9699. if (!IsFileControl(DropSourceControl, osRemote))
  9700. {
  9701. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9702. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9703. // Why don't we use Terminal->CurrentDirectory directly?
  9704. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9705. // do not force queue, drag drop
  9706. false, true);
  9707. }
  9708. }
  9709. }
  9710. //---------------------------------------------------------------------------
  9711. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9712. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9713. {
  9714. // Downloads are handled in RemoteFileControlDDEnd
  9715. if (!IsFileControl(DropSourceControl, osRemote))
  9716. {
  9717. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9718. Terminal->CurrentDirectory,
  9719. // force queue, drag drop
  9720. true, true);
  9721. }
  9722. }
  9723. //---------------------------------------------------------------------------
  9724. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9725. {
  9726. FShowing = false;
  9727. // Do not save empty workspace
  9728. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9729. {
  9730. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9731. UnicodeString Name = WorkspaceName();
  9732. DoSaveWorkspace(
  9733. Name, DataList.get(),
  9734. !Configuration->DisablePasswordStoring &&
  9735. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9736. WinConfiguration->LastStoredSession = Name;
  9737. }
  9738. }
  9739. //---------------------------------------------------------------------------
  9740. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9741. {
  9742. TManagedTerminal * ManagedTerminal =
  9743. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9744. if (ManagedTerminal != NULL)
  9745. {
  9746. ManagedTerminal->DirectoryLoaded = Now();
  9747. }
  9748. }
  9749. //---------------------------------------------------------------------------
  9750. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9751. TListItem * /*Item*/, bool /*Selected*/)
  9752. {
  9753. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9754. switch (DirView->LastSelectMethod)
  9755. {
  9756. case smKeyboard:
  9757. Configuration->Usage->Inc(L"KeyboardSelections");
  9758. break;
  9759. case smMouse:
  9760. Configuration->Usage->Inc(L"MouseSelections");
  9761. break;
  9762. }
  9763. }
  9764. //---------------------------------------------------------------------------
  9765. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9766. {
  9767. if (Color != 0)
  9768. {
  9769. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9770. }
  9771. else
  9772. {
  9773. return FSessionTabImageIndex;
  9774. }
  9775. }
  9776. //---------------------------------------------------------------------------
  9777. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9778. {
  9779. TPngImageCollectionItem * Item =
  9780. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9781. FSessionColors->AddPng(Item->PngImage);
  9782. return FSessionColors->Count - 1;
  9783. }
  9784. //---------------------------------------------------------------------------
  9785. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9786. {
  9787. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9788. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9789. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9790. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9791. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9792. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9793. }
  9794. //---------------------------------------------------------------------------
  9795. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9796. {
  9797. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9798. }
  9799. //---------------------------------------------------------------------------
  9800. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9801. {
  9802. switch (Side)
  9803. {
  9804. case osLocal:
  9805. return FNewLocalTabTabImageIndex;
  9806. case osRemote:
  9807. case osCurrent:
  9808. return FNewRemoteTabTabImageIndex;
  9809. default:
  9810. DebugFail();
  9811. return -1;
  9812. }
  9813. }
  9814. //---------------------------------------------------------------------------
  9815. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9816. {
  9817. for (int Index = 0; Index < Control->ControlCount; Index++)
  9818. {
  9819. TControl * AControl = Control->Controls[Index];
  9820. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9821. if (WinControl != NULL)
  9822. {
  9823. CollectItemsWithTextDisplayMode(WinControl);
  9824. }
  9825. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9826. if (Toolbar != NULL)
  9827. {
  9828. // we care for top-level items only
  9829. TTBCustomItem * Items = Toolbar->Items;
  9830. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9831. {
  9832. TTBCustomItem * Item = Items->Items[ItemIndex];
  9833. if (((Item->DisplayMode == nbdmImageAndText) ||
  9834. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9835. EligibleForImageDisplayMode(Item))
  9836. {
  9837. FItemsWithTextDisplayMode.insert(Item);
  9838. }
  9839. }
  9840. }
  9841. }
  9842. }
  9843. //---------------------------------------------------------------------------
  9844. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9845. {
  9846. return true;
  9847. }
  9848. //---------------------------------------------------------------------------
  9849. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9850. {
  9851. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9852. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9853. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9854. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9855. bool Result = true;
  9856. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9857. {
  9858. TTBCustomItem * Item = *i;
  9859. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9860. if (Label != NULL)
  9861. {
  9862. // optimization
  9863. if (Label->Visible == SelectiveToolbarText)
  9864. {
  9865. Result = false;
  9866. }
  9867. else
  9868. {
  9869. Label->Visible = SelectiveToolbarText;
  9870. }
  9871. }
  9872. else
  9873. {
  9874. // optimization
  9875. if (Item->DisplayMode == DisplayMode)
  9876. {
  9877. Result = false;
  9878. }
  9879. else
  9880. {
  9881. Item->DisplayMode = DisplayMode;
  9882. }
  9883. }
  9884. ++i;
  9885. }
  9886. UpdateNewTabTab();
  9887. return Result;
  9888. }
  9889. //---------------------------------------------------------------------------
  9890. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9891. {
  9892. DebugFail();
  9893. }
  9894. //---------------------------------------------------------------------------
  9895. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9896. {
  9897. // Among other, a lock level is non zero, while directory is being loaded,
  9898. // even when it happens as a result of dirview navigation,
  9899. // i.e. when TNonVisualDataModule is NOT FBusy.
  9900. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9901. // Among other this prevents a panel auto update to occur while
  9902. // directory is changing.
  9903. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9904. }
  9905. //---------------------------------------------------------------------------
  9906. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9907. {
  9908. // While the window is disabled, we still seem to process menu shortcuts at least,
  9909. // so stop it at least here.
  9910. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9911. bool Result;
  9912. if (Allowed == aaUpdate)
  9913. {
  9914. Result = true;
  9915. }
  9916. else
  9917. {
  9918. Result = !NonVisualDataModule->Busy;
  9919. if (Result && (Allowed == aaShortCut))
  9920. {
  9921. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9922. Action->Update();
  9923. Result = Action->Enabled;
  9924. }
  9925. }
  9926. return Result;
  9927. }
  9928. //---------------------------------------------------------------------------
  9929. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9930. {
  9931. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9932. }
  9933. //---------------------------------------------------------------------------
  9934. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9935. {
  9936. // This is somewhat redundant to LockWindow() call from
  9937. // TTerminalManager::TerminalReadDirectoryProgress.
  9938. // But disabling window is known not to block keyboard shorcuts
  9939. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9940. if (Busy > 0)
  9941. {
  9942. if (NonVisualDataModule->Busy)
  9943. {
  9944. State = false;
  9945. }
  9946. else
  9947. {
  9948. NonVisualDataModule->StartBusy();
  9949. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9950. LockWindow();
  9951. }
  9952. }
  9953. else if (Busy < 0)
  9954. {
  9955. UnlockWindow();
  9956. NonVisualDataModule->EndBusy();
  9957. }
  9958. else
  9959. {
  9960. State = NonVisualDataModule->Busy;
  9961. }
  9962. }
  9963. //---------------------------------------------------------------------------
  9964. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9965. {
  9966. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9967. if (Index >= 0)
  9968. {
  9969. TPopupMenu * PopupMenu = NULL;
  9970. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9971. if (Session != NULL)
  9972. {
  9973. SessionsPageControl->ActivePageIndex = Index;
  9974. if (DebugAlwaysTrue(SessionTabSwitched()))
  9975. {
  9976. // copied from TControl.WMContextMenu
  9977. SendCancelMode(SessionsPageControl);
  9978. // explicit popup instead of using PopupMenu property
  9979. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9980. // while connecting yet not-connected session and hence
  9981. // allowing an access to commands over not-completelly connected session
  9982. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9983. }
  9984. }
  9985. else
  9986. {
  9987. if (SupportsLocalBrowser())
  9988. {
  9989. PopupMenu = NonVisualDataModule->NewTabPopup;
  9990. }
  9991. }
  9992. if (PopupMenu != NULL)
  9993. {
  9994. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9995. PopupMenu->PopupComponent = SessionsPageControl;
  9996. PopupMenu->Popup(Point.x, Point.y);
  9997. }
  9998. }
  9999. Handled = true;
  10000. }
  10001. //---------------------------------------------------------------------------
  10002. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10003. {
  10004. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10005. }
  10006. //---------------------------------------------------------------------------
  10007. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10008. {
  10009. Configuration->Usage->Inc(L"PasswordChanges");
  10010. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10011. ChangePasswordData->DisableAuthentationsExceptPassword();
  10012. ChangePasswordData->ChangePassword = true;
  10013. std::unique_ptr<TTerminal> ChangePasswordSession(
  10014. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10015. ChangePasswordSession->AutoReadDirectory = false;
  10016. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10017. {
  10018. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10019. }
  10020. else
  10021. {
  10022. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10023. }
  10024. }
  10025. //---------------------------------------------------------------------------
  10026. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10027. {
  10028. // No nice way to assert SSH2
  10029. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10030. }
  10031. //---------------------------------------------------------------------------
  10032. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10033. {
  10034. TOperationVisualizer OperationVisualizer;
  10035. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10036. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10037. }
  10038. //---------------------------------------------------------------------------
  10039. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10040. {
  10041. TForm::ChangeScale(M, D);
  10042. int APixelsPerInch = GetControlPixelsPerInch(this);
  10043. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10044. try
  10045. {
  10046. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10047. }
  10048. __finally
  10049. {
  10050. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10051. }
  10052. }
  10053. //---------------------------------------------------------------------------
  10054. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10055. {
  10056. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10057. }
  10058. //---------------------------------------------------------------------
  10059. class TPublicControl : public TControl
  10060. {
  10061. friend class TCustomScpExplorerForm;
  10062. };
  10063. //---------------------------------------------------------------------------
  10064. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10065. {
  10066. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10067. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10068. }
  10069. //---------------------------------------------------------------------------
  10070. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10071. {
  10072. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10073. OpenBookmark(Side, Bookmark);
  10074. }
  10075. //---------------------------------------------------------------------------
  10076. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10077. {
  10078. DoBookmarkClick(osLocal, Sender);
  10079. }
  10080. //---------------------------------------------------------------------------
  10081. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10082. {
  10083. DoBookmarkClick(osRemote, Sender);
  10084. }
  10085. //---------------------------------------------------------------------------
  10086. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10087. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10088. {
  10089. if (BookmarkList != NULL)
  10090. {
  10091. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10092. if (IsLocalBrowserMode())
  10093. {
  10094. Side = osLocal;
  10095. }
  10096. if (!WinConfiguration->UseLocationProfiles)
  10097. {
  10098. std::unique_ptr<TStringList> Directories(new TStringList());
  10099. Directories->CaseSensitive = (Side == osRemote);
  10100. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10101. {
  10102. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10103. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10104. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10105. {
  10106. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10107. Item->Caption = EscapeHotkey(Directory);
  10108. Item->ShortCut = Bookmark->ShortCut;
  10109. Item->OnClick = OnBookmarkClick;
  10110. Item->Tag = reinterpret_cast<int>(Bookmark);
  10111. Directories->Add(Directory);
  10112. Menu->Add(Item.release());
  10113. }
  10114. }
  10115. }
  10116. else
  10117. {
  10118. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10119. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10120. {
  10121. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10122. if (!Bookmark->Node.IsEmpty())
  10123. {
  10124. Folders->Add(Bookmark->Node);
  10125. }
  10126. }
  10127. for (int Index = 0; Index < Folders->Count; Index++)
  10128. {
  10129. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10130. Item->Caption = Folders->Strings[Index];
  10131. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10132. Folders->Objects[Index] = Item.get();
  10133. Menu->Add(Item.release());
  10134. }
  10135. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10136. {
  10137. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10138. TTBCustomItem * Parent;
  10139. if (!Bookmark->Node.IsEmpty())
  10140. {
  10141. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10142. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10143. }
  10144. else
  10145. {
  10146. Parent = Menu;
  10147. }
  10148. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10149. Item->Caption = Bookmark->Name;
  10150. Item->ShortCut = Bookmark->ShortCut;
  10151. Item->OnClick = OnBookmarkClick;
  10152. Item->Tag = reinterpret_cast<int>(Bookmark);
  10153. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10154. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10155. {
  10156. // No hint for location profiles that are actually mere "bookmarks"
  10157. }
  10158. else
  10159. {
  10160. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10161. UnicodeString LongHint;
  10162. if (!Bookmark->Local.IsEmpty())
  10163. {
  10164. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10165. }
  10166. if (!Bookmark->Remote.IsEmpty())
  10167. {
  10168. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10169. }
  10170. AddToList(Hint, LongHint, L"|");
  10171. Item->Hint = Hint;
  10172. }
  10173. Parent->Add(Item.release());
  10174. }
  10175. }
  10176. AddMenuSeparator(Menu);
  10177. }
  10178. }
  10179. //---------------------------------------------------------------------------
  10180. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10181. {
  10182. Menu->Clear();
  10183. std::unique_ptr<TTBCustomItem> Item;
  10184. Item.reset(new TTBXItem(Owner));
  10185. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10186. Item->Options = Item->Options << tboDefault;
  10187. Menu->Add(Item.release());
  10188. AddMenuSeparator(Menu);
  10189. if (Terminal != NULL)
  10190. {
  10191. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10192. }
  10193. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10194. Item.reset(new TTBXItem(Owner));
  10195. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10196. Menu->Add(Item.release());
  10197. }
  10198. //---------------------------------------------------------------------------
  10199. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10200. {
  10201. bool Result = true;
  10202. bool Remote = !IsSideLocalBrowser(Side);
  10203. try
  10204. {
  10205. UnicodeString APath = Path;
  10206. if (Remote)
  10207. {
  10208. Terminal->ExceptionOnFail = true;
  10209. }
  10210. else
  10211. {
  10212. APath = ExpandEnvironmentVariables(Path);
  10213. }
  10214. try
  10215. {
  10216. DirView(Side)->Path = APath;
  10217. }
  10218. __finally
  10219. {
  10220. if (Remote)
  10221. {
  10222. Terminal->ExceptionOnFail = false;
  10223. }
  10224. }
  10225. }
  10226. catch (Exception & E)
  10227. {
  10228. if (!Remote || Terminal->Active)
  10229. {
  10230. ShowExtendedExceptionEx(Terminal, &E);
  10231. Result = false;
  10232. }
  10233. else
  10234. {
  10235. throw;
  10236. }
  10237. }
  10238. return Result;
  10239. }
  10240. //---------------------------------------------------------------------------
  10241. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10242. {
  10243. // Make sure there some feedback even when loading tiny local folders
  10244. TInstantOperationVisualizer Visualizer;
  10245. DirView(Side)->ReloadDirectory();
  10246. }
  10247. //---------------------------------------------------------------------------
  10248. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10249. {
  10250. if (Index == SessionsPageControl->TabIndex)
  10251. {
  10252. CloseSession();
  10253. }
  10254. else
  10255. {
  10256. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10257. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10258. TTerminalManager * Manager = TTerminalManager::Instance();
  10259. if (!Terminal->Active)
  10260. {
  10261. Manager->FreeTerminal(Terminal);
  10262. }
  10263. else
  10264. {
  10265. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10266. if (CanCloseQueue(Queue))
  10267. {
  10268. Manager->FreeTerminal(Terminal);
  10269. }
  10270. }
  10271. }
  10272. }
  10273. //---------------------------------------------------------------------------
  10274. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10275. {
  10276. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10277. if (Session == NULL)
  10278. {
  10279. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10280. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10281. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10282. //P = SessionsPageControl->ClientToScreen(P);
  10283. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10284. }
  10285. else
  10286. {
  10287. CloseSessionTab(Index);
  10288. }
  10289. }
  10290. //---------------------------------------------------------------------------
  10291. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10292. {
  10293. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10294. {
  10295. TInstantOperationVisualizer Visualizer;
  10296. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10297. // before we create a new data object
  10298. ClipboardClear();
  10299. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10300. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10301. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10302. // Intermediate variable to WORKAROUND a crash in compiler
  10303. TObjectList * ClipboardFakeMonitors =
  10304. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10305. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10306. if (FClipboardDragDropFilesEx.get() == NULL)
  10307. {
  10308. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10309. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10310. }
  10311. FClipboardDragDropFilesEx->FileList->Clear();
  10312. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10313. FClipboardDragDropFilesEx->CopyToClipboard();
  10314. FClipboardTerminal = Terminal;
  10315. // Need full paths, as cwd can be different once files are pasted
  10316. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10317. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10318. {
  10319. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10320. }
  10321. }
  10322. }
  10323. //---------------------------------------------------------------------------
  10324. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10325. {
  10326. if (OpenClipboard(0))
  10327. {
  10328. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10329. CloseClipboard();
  10330. }
  10331. }
  10332. //---------------------------------------------------------------------------
  10333. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10334. {
  10335. return !FClipboardFakeDirectory.IsEmpty();
  10336. }
  10337. //---------------------------------------------------------------------------
  10338. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10339. {
  10340. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10341. FClipboardFakeDirectory = UnicodeString();
  10342. FClipboardTerminal = NULL;
  10343. if (FDownloadingFromClipboard)
  10344. {
  10345. // We are called by the monitor, so attempt to release it would deadlock
  10346. FClipboardFakeMonitorsPendingReset = true;
  10347. }
  10348. else
  10349. {
  10350. FClipboardFakeMonitors.reset(NULL);
  10351. }
  10352. }
  10353. //---------------------------------------------------------------------------
  10354. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10355. {
  10356. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10357. {
  10358. ClipboardStop();
  10359. }
  10360. }
  10361. //---------------------------------------------------------------------------
  10362. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10363. {
  10364. TTerminalManager * Manager = TTerminalManager::Instance();
  10365. Manager->ActiveSession = FClipboardTerminal;
  10366. TTransferOperationParam Params;
  10367. Params.TargetDirectory = TargetDirectory;
  10368. Params.DragDrop = DragDrop;
  10369. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10370. }
  10371. //---------------------------------------------------------------------------
  10372. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10373. {
  10374. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10375. // (as it pumps a Windows message queue while being shutted down)
  10376. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10377. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10378. {
  10379. // Can fail as it can be e.g. locked by AV, so we retry that later
  10380. bool Removed = RemoveDir(ApiPath(FileName));
  10381. try
  10382. {
  10383. if (!NonVisualDataModule->Busy)
  10384. {
  10385. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10386. FClipboardFakeMonitorsPendingReset = false;
  10387. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10388. TAutoFlag Flag(FDownloadingFromClipboard);
  10389. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10390. }
  10391. }
  10392. __finally
  10393. {
  10394. if (!Removed)
  10395. {
  10396. RemoveDir(ApiPath(FileName));
  10397. }
  10398. }
  10399. }
  10400. }
  10401. //---------------------------------------------------------------------------
  10402. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10403. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10404. {
  10405. TFileMasks::TParams MaskParams;
  10406. MaskParams.Size = Size;
  10407. MaskParams.Modification = Modification;
  10408. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10409. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10410. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10411. {
  10412. bool ImplicitMatch;
  10413. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10414. !ImplicitMatch)
  10415. {
  10416. Color = Iter->Color;
  10417. break;
  10418. }
  10419. }
  10420. }
  10421. //---------------------------------------------------------------------------
  10422. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10423. {
  10424. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10425. if (!DirView->IsEditing())
  10426. {
  10427. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10428. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10429. // while searching
  10430. if (Key == VK_BACK)
  10431. {
  10432. if (!FIncrementalSearch.IsEmpty())
  10433. {
  10434. if (FIncrementalSearch.Length() == 1)
  10435. {
  10436. ResetIncrementalSearch();
  10437. }
  10438. else
  10439. {
  10440. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10441. IncrementalSearch(NewText, false, false);
  10442. }
  10443. Key = 0;
  10444. }
  10445. }
  10446. }
  10447. }
  10448. //---------------------------------------------------------------------------
  10449. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10450. {
  10451. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10452. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10453. {
  10454. // Filter control sequences.
  10455. // When not searching yet, prefer use of the space for toggling file selection
  10456. // (so we cannot incrementally search for a string starting with a space).
  10457. if ((Key > VK_SPACE) ||
  10458. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10459. {
  10460. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10461. Key = 0;
  10462. }
  10463. }
  10464. }
  10465. //---------------------------------------------------------------------------
  10466. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10467. {
  10468. if (!FIncrementalSearch.IsEmpty())
  10469. {
  10470. FIncrementalSearch = L"";
  10471. DirView(osCurrent)->UpdateStatusBar();
  10472. }
  10473. }
  10474. //---------------------------------------------------------------------------
  10475. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10476. {
  10477. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10478. if (Item == NULL)
  10479. {
  10480. MessageBeep(MB_ICONHAND);
  10481. }
  10482. else
  10483. {
  10484. TCustomDirView * ADirView = DirView(osCurrent);
  10485. {
  10486. TAutoNestingCounter Guard(FIncrementalSearching);
  10487. ADirView->FocusItem(Item);
  10488. if (ADirView->NortonLike == nlOff)
  10489. {
  10490. if ((ADirView->Selected != Item) ||
  10491. (ADirView->SelCount > 1))
  10492. {
  10493. ADirView->ClearSelection();
  10494. ADirView->Selected = Item;
  10495. }
  10496. }
  10497. }
  10498. FIncrementalSearch = Text;
  10499. Item->MakeVisible(false);
  10500. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10501. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10502. ADirView->UpdateStatusBar();
  10503. }
  10504. }
  10505. //---------------------------------------------------------------------------
  10506. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10507. {
  10508. int Index = Item->Index;
  10509. if (!Reverse)
  10510. {
  10511. Index++;
  10512. if (Index >= Item->Owner->Count)
  10513. {
  10514. Index = 0;
  10515. }
  10516. }
  10517. else
  10518. {
  10519. Index--;
  10520. if (Index < 0)
  10521. {
  10522. Index = Item->Owner->Count - 1;
  10523. }
  10524. }
  10525. return Item->Owner->Item[Index];
  10526. }
  10527. //---------------------------------------------------------------------------
  10528. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10529. {
  10530. TCustomDirView * ADirView = DirView(osCurrent);
  10531. if (ADirView->Items->Count == 0)
  10532. {
  10533. return NULL;
  10534. }
  10535. else
  10536. {
  10537. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10538. TListItem * Item = CurrentItem;
  10539. if (SkipCurrent)
  10540. {
  10541. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10542. }
  10543. while (true)
  10544. {
  10545. bool Matches = false;
  10546. switch (WinConfiguration->PanelSearch)
  10547. {
  10548. case isNameStartOnly:
  10549. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10550. break;
  10551. case isName:
  10552. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10553. break;
  10554. case isAll:
  10555. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10556. int Index = 0;
  10557. while ((Index < ColCount) && !Matches)
  10558. {
  10559. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10560. if (Visible)
  10561. {
  10562. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10563. }
  10564. Index++;
  10565. }
  10566. break;
  10567. }
  10568. if (Matches)
  10569. {
  10570. return Item;
  10571. }
  10572. Item = GetNextFile(Item, Reverse);
  10573. if (Item == CurrentItem)
  10574. {
  10575. return NULL;
  10576. }
  10577. }
  10578. }
  10579. }
  10580. //---------------------------------------------------------------------------
  10581. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10582. {
  10583. ResetIncrementalSearch();
  10584. }
  10585. //---------------------------------------------------------------------------
  10586. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10587. {
  10588. if (Message.CharCode == VK_TAB)
  10589. {
  10590. if (!FIncrementalSearch.IsEmpty())
  10591. {
  10592. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10593. bool Reverse = Shift.Contains(ssShift);
  10594. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10595. Message.Result = 1;
  10596. return;
  10597. }
  10598. }
  10599. else if (Message.CharCode == VK_ESCAPE)
  10600. {
  10601. if (!FIncrementalSearch.IsEmpty())
  10602. {
  10603. ResetIncrementalSearch();
  10604. Message.Result = 1;
  10605. return;
  10606. }
  10607. }
  10608. TForm::Dispatch(&Message);
  10609. }
  10610. //---------------------------------------------------------------------------
  10611. void __fastcall TCustomScpExplorerForm::Deactivate()
  10612. {
  10613. TForm::Deactivate();
  10614. // E.g. when switching to an internal editor window
  10615. ResetIncrementalSearch();
  10616. }
  10617. //---------------------------------------------------------------------------
  10618. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10619. {
  10620. // When switching to another application
  10621. ResetIncrementalSearch();
  10622. }
  10623. //---------------------------------------------------------------------------
  10624. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10625. {
  10626. ResetIncrementalSearch();
  10627. }
  10628. //---------------------------------------------------------------------------
  10629. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10630. {
  10631. if (FIncrementalSearching <= 0)
  10632. {
  10633. ResetIncrementalSearch();
  10634. }
  10635. }
  10636. //---------------------------------------------------------------------------
  10637. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10638. {
  10639. CountClicksForWindowPrint(this);
  10640. }
  10641. //---------------------------------------------------------------------------
  10642. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10643. {
  10644. CenterReconnectToolbar();
  10645. }
  10646. //---------------------------------------------------------------------------
  10647. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10648. {
  10649. if (RemoteDirView->ItemFocused != NULL)
  10650. {
  10651. RemoteDirView->ItemFocused->Selected = true;
  10652. }
  10653. }
  10654. //---------------------------------------------------------------------------
  10655. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10656. {
  10657. if (!FQueueStatusUpdating)
  10658. {
  10659. bool Refresh;
  10660. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10661. {
  10662. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10663. }
  10664. else
  10665. {
  10666. if (FQueueFileList->GetCount() > 0)
  10667. {
  10668. FQueueFileList->Clear();
  10669. Refresh = true;
  10670. }
  10671. }
  10672. int Count = FQueueFileList->GetCount();
  10673. if (QueueFileList->Items->Count != Count)
  10674. {
  10675. DebugAssert(Refresh);
  10676. QueueFileList->Items->Count = Count;
  10677. }
  10678. if (Refresh)
  10679. {
  10680. QueueFileList->Invalidate();
  10681. }
  10682. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10683. }
  10684. }
  10685. //---------------------------------------------------------------------------
  10686. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10687. {
  10688. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10689. }
  10690. //---------------------------------------------------------------------------
  10691. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10692. {
  10693. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10694. // See TSynchronizeChecklistDialog::UpdateTimer
  10695. if (QueueFileList->Items->Count > 0)
  10696. {
  10697. QueueFileList->Items->Item[0]->MakeVisible(false);
  10698. }
  10699. UpdateQueueFileList();
  10700. }
  10701. //---------------------------------------------------------------------------
  10702. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10703. {
  10704. Active = QueueView3->Focused() || QueueFileList->Focused();
  10705. }
  10706. //---------------------------------------------------------------------------
  10707. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10708. {
  10709. QueueLabelUpdateStatus();
  10710. }
  10711. //---------------------------------------------------------------------------
  10712. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10713. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10714. {
  10715. int State = FQueueFileList->GetState(Item->Index);
  10716. if (State == qfsProcessed)
  10717. {
  10718. Sender->Canvas->Font->Color = clGrayText;
  10719. }
  10720. }
  10721. //---------------------------------------------------------------------------
  10722. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10723. {
  10724. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10725. QueueFileList->Column[0]->Width =
  10726. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10727. }
  10728. //---------------------------------------------------------------------------
  10729. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10730. {
  10731. QueueFileListColumnAutoSize();
  10732. }
  10733. //---------------------------------------------------------------------------
  10734. void __fastcall TCustomScpExplorerForm::CloseApp()
  10735. {
  10736. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10737. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10738. // So we clear the flag here.
  10739. DebugAssert(NonVisualDataModule->Busy);
  10740. NonVisualDataModule->EndBusy();
  10741. try
  10742. {
  10743. Close();
  10744. }
  10745. __finally
  10746. {
  10747. NonVisualDataModule->StartBusy();
  10748. }
  10749. }
  10750. //---------------------------------------------------------------------------
  10751. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10752. {
  10753. return (Terminal != NULL) && Terminal->Active;
  10754. }
  10755. //---------------------------------------------------------------------------
  10756. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10757. {
  10758. return (ManagedSession != NULL);
  10759. }
  10760. //---------------------------------------------------------------------------
  10761. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10762. {
  10763. return IsActiveTerminal(Terminal);
  10764. }
  10765. //---------------------------------------------------------------------------
  10766. void TCustomScpExplorerForm::LocalLocalCopy(
  10767. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10768. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10769. {
  10770. DebugFail();
  10771. }
  10772. //---------------------------------------------------------------------------
  10773. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10774. {
  10775. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10776. }
  10777. //---------------------------------------------------------------------------
  10778. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10779. {
  10780. // Changing tab list triggers the OnResize.
  10781. if (!FUpdatingSessionTabs)
  10782. {
  10783. // UpdateTabsCaptionTruncation should be enough
  10784. SessionListChanged(true);
  10785. }
  10786. }
  10787. //---------------------------------------------------------------------------
  10788. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10789. {
  10790. if (ASession == ManagedSession)
  10791. {
  10792. UpdateSession(ASession);
  10793. }
  10794. TSessionData * Data = ASession->StateData;
  10795. UnicodeString Result;
  10796. if (Side == osLocal)
  10797. {
  10798. Result = Data->LocalDirectory;
  10799. }
  10800. else if (DebugAlwaysTrue(Side == osOther))
  10801. {
  10802. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10803. }
  10804. return Result;
  10805. }
  10806. //---------------------------------------------------------------------------
  10807. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10808. {
  10809. UnicodeString Result;
  10810. if (!ASession->Active)
  10811. {
  10812. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10813. }
  10814. else
  10815. {
  10816. Result = GetSessionPath(ASession, osRemote);
  10817. }
  10818. return Result;
  10819. }
  10820. //---------------------------------------------------------------------------
  10821. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10822. {
  10823. return LoadStr(NEW_REMOTE_TAB_HINT);
  10824. }
  10825. //---------------------------------------------------------------------------
  10826. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10827. {
  10828. if (Index >= 0)
  10829. {
  10830. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10831. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10832. if (TabSession == NULL)
  10833. {
  10834. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10835. if (NewTabTabCaption != TabSheet->Caption)
  10836. {
  10837. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10838. }
  10839. }
  10840. else
  10841. {
  10842. TTerminalManager * Manager = TTerminalManager::Instance();
  10843. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10844. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10845. }
  10846. }
  10847. }
  10848. //---------------------------------------------------------------------------
  10849. typedef std::vector<TListItem *> TListItemsVector;
  10850. struct TCalculateSizeOperation
  10851. {
  10852. TCalculateSizeStats Stats;
  10853. TListItemsVector ListItems;
  10854. size_t Index;
  10855. };
  10856. //---------------------------------------------------------------------------
  10857. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10858. {
  10859. TCalculateSizeOperation CalculateSizeOperation;
  10860. TOperationSide Side = GetSide(osCurrent);
  10861. TCustomDirView * ADirView = DirView(Side);
  10862. bool FullPath = IsSideLocalBrowser(Side);
  10863. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10864. std::unique_ptr<TStrings> FileList(new TStringList());
  10865. for (int Index = 0; Index < AllFileList->Count; Index++)
  10866. {
  10867. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10868. if (ADirView->ItemIsDirectory(Item))
  10869. {
  10870. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10871. CalculateSizeOperation.ListItems.push_back(Item);
  10872. }
  10873. }
  10874. CalculateSizeOperation.Index = 0;
  10875. __int64 Size = 0;
  10876. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10877. FCalculateSizeOperation = &CalculateSizeOperation;
  10878. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10879. FOnFileOperationFinished = DirectorySizeCalculated;
  10880. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10881. FPrimaryOperation = foCalculateSize;
  10882. TCalculatedSizes CalculatedSizes;
  10883. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10884. if (Side == osLocal)
  10885. {
  10886. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10887. }
  10888. else
  10889. {
  10890. TCalculateSizeParams Params;
  10891. Params.Stats = &CalculateSizeOperation.Stats;
  10892. Params.UseCache = true;
  10893. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10894. }
  10895. }
  10896. //---------------------------------------------------------------------------
  10897. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10898. {
  10899. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10900. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10901. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10902. // optimization
  10903. bool GotExpectedItem =
  10904. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10905. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10906. int Index = -1;
  10907. if (DebugAlwaysTrue(GotExpectedItem))
  10908. {
  10909. Index = FCalculateSizeOperation->Index;
  10910. FCalculateSizeOperation->Index++;
  10911. }
  10912. else
  10913. {
  10914. TListItemsVector::iterator I =
  10915. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  10916. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  10917. {
  10918. Index = I - FCalculateSizeOperation->ListItems.begin();
  10919. }
  10920. }
  10921. if (DebugAlwaysTrue(Index >= 0))
  10922. {
  10923. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  10924. ADirView->SetItemCalculatedSize(Item, Size);
  10925. }
  10926. }