CustomScpExplorer.cpp 374 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330
  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 <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. //---------------------------------------------------------------------------
  35. #pragma package(smart_init)
  36. #pragma link "CustomDirView"
  37. #pragma link "CustomUnixDirView"
  38. #pragma link "IEListView"
  39. #pragma link "NortonLikeListView"
  40. #pragma link "UnixDirView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "UnixDriveView"
  43. #pragma link "CustomDriveView"
  44. #pragma link "TB2Dock"
  45. #pragma link "TBXStatusBars"
  46. #pragma link "TB2Item"
  47. #pragma link "TB2Toolbar"
  48. #pragma link "ThemePageControl"
  49. #pragma link "PathLabel"
  50. #pragma resource "*.dfm"
  51. //---------------------------------------------------------------------------
  52. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  53. static const int SessionPanelCount = 3;
  54. //---------------------------------------------------------------------------
  55. class TMutexGuard
  56. {
  57. public:
  58. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  59. int Timeout = 5000)
  60. {
  61. FMutex = NULL;
  62. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  63. if (WaitResult == WAIT_TIMEOUT)
  64. {
  65. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  66. }
  67. else
  68. {
  69. FMutex = AMutex;
  70. }
  71. }
  72. ~TMutexGuard()
  73. {
  74. if (FMutex != NULL)
  75. {
  76. ReleaseMutex(FMutex);
  77. }
  78. }
  79. private:
  80. HANDLE FMutex;
  81. };
  82. //---------------------------------------------------------------------------
  83. class TWindowLock
  84. {
  85. public:
  86. TWindowLock(TCustomScpExplorerForm * Form) :
  87. FForm(Form)
  88. {
  89. FForm->LockWindow();
  90. }
  91. ~TWindowLock()
  92. {
  93. FForm->UnlockWindow();
  94. }
  95. private:
  96. TCustomScpExplorerForm * FForm;
  97. };
  98. //---------------------------------------------------------------------------
  99. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  100. FForm(Form)
  101. {
  102. FForm->BatchStart(FBatchStorage);
  103. }
  104. //---------------------------------------------------------------------------
  105. TAutoBatch::~TAutoBatch()
  106. {
  107. FForm->BatchEnd(FBatchStorage);
  108. }
  109. //---------------------------------------------------------------------------
  110. struct TTransferOperationParam
  111. {
  112. TTransferOperationParam();
  113. UnicodeString TargetDirectory;
  114. bool Temp;
  115. bool DragDrop;
  116. int Options;
  117. TAutoSwitch Queue;
  118. };
  119. //---------------------------------------------------------------------------
  120. TTransferOperationParam::TTransferOperationParam()
  121. {
  122. Temp = false;
  123. DragDrop = false;
  124. Options = 0;
  125. Queue = asAuto;
  126. }
  127. //---------------------------------------------------------------------------
  128. class TTransferPresetNoteData : public TObject
  129. {
  130. public:
  131. UnicodeString Message;
  132. };
  133. //---------------------------------------------------------------------------
  134. class TTerminalNoteData : public TObject
  135. {
  136. public:
  137. TManagedTerminal * Terminal;
  138. };
  139. //---------------------------------------------------------------------------
  140. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  141. FFormRestored(false),
  142. TForm(Owner)
  143. {
  144. FInvalid = false;
  145. FCurrentSide = osRemote;
  146. FEverShown = false;
  147. FDocks = new TList();
  148. InitControls();
  149. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  150. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  151. NonVisualDataModule->ScpExplorer = this;
  152. RestoreParams();
  153. ConfigurationChanged();
  154. RemoteDirView->Invalidate();
  155. FAutoOperation = false;
  156. FOnFileOperationFinished = NULL;
  157. FForceExecution = false;
  158. FIgnoreNextDialogChar = 0;
  159. FErrorList = NULL;
  160. FSynchronizeProgressForm = NULL;
  161. FProgressForm = NULL;
  162. FRefreshLocalDirectory = false;
  163. FRefreshRemoteDirectory = false;
  164. FDDExtMapFile = NULL;
  165. // CreateMutexW keeps failing with ERROR_NOACCESS
  166. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  167. DebugAssert(FDDExtMutex != NULL);
  168. FDDTargetControl = NULL;
  169. FDelayedDeletionTimer = NULL;
  170. FDelayedDeletionList = new TStringList();
  171. FDDFileList = NULL;
  172. FPendingTempSpaceWarn = false;
  173. FCapturedLog = NULL;
  174. FDragDropOperation = false;
  175. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  176. FAllowTransferPresetAutoSelect = true;
  177. FSessionChanging = false;
  178. FCopyParamDefault = L"";
  179. FSynchronizeController = NULL;
  180. FPendingQueueActionItem = NULL;
  181. FLockLevel = 0;
  182. FLockSuspendLevel = 0;
  183. FDisabledOnLockSuspend = false;
  184. FAlternativeDelete = false;
  185. FTrayIcon = new ::TTrayIcon(0);
  186. FTrayIcon->OnClick = TrayIconClick;
  187. FMaxQueueLength = 0;
  188. FLastContextPopupScreenPoint = TPoint(-1, -1);
  189. FTransferResumeList = NULL;
  190. FMoveToQueue = false;
  191. FStandaloneEditing = false;
  192. FOnFeedSynchronizeError = NULL;
  193. FNeedSession = false;
  194. FDoNotIdleCurrentTerminal = 0;
  195. FIncrementalSearching = 0;
  196. FIncrementalSearchHaveNext = false;
  197. FQueueFileList.reset(new TQueueFileList());
  198. FProgressSide = osCurrent;
  199. FEditorManager = new TEditorManager();
  200. FEditorManager->OnFileChange = ExecutedFileChanged;
  201. FEditorManager->OnFileReload = ExecutedFileReload;
  202. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  203. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  204. FLocalEditors = new TList();
  205. FQueueStatus = NULL;
  206. FQueueStatusSection = new TCriticalSection();
  207. FQueueStatusInvalidated = false;
  208. FQueueItemInvalidated = false;
  209. FQueueStatusUpdating = false;
  210. FQueueActedItem = NULL;
  211. FQueueController = new TQueueController(QueueView3);
  212. FUserActionTimer = new TTimer(this);
  213. FUserActionTimer->Enabled = false;
  214. FUserActionTimer->Interval = 10;
  215. FUserActionTimer->OnTimer = UserActionTimer;
  216. FNotes = new TStringList();
  217. FNoteTimer = new TTimer(this);
  218. FNoteTimer->Enabled = false;
  219. FNoteTimer->OnTimer = NoteTimer;
  220. FOnNoteClick = NULL;
  221. FOle32Library = LoadLibrary(L"Ole32.dll");
  222. FDragMoveCursor = FOle32Library != NULL ?
  223. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  224. UseSystemSettings(this);
  225. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  226. static_cast<TObject*>(GetComponent(fcTransferList)));
  227. DebugAssert(TransferList != NULL);
  228. FTransferListHoverIndex = -1;
  229. TransferList->OnChange = TransferListChange;
  230. TransferList->OnDrawItem = TransferListDrawItem;
  231. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueFileList);
  239. UseDesktopFont(QueueLabel);
  240. UseDesktopFont(RemoteStatusBar);
  241. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  242. QueueSplitter->ShowHint = true;
  243. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  244. QueueFileListSplitter->ShowHint = true;
  245. RemotePanelSplitter->ShowHint = true;
  246. UpdateImages();
  247. FCustomCommandMenu = CreateTBXPopupMenu(this);
  248. FCustomCommandLocalFileList = NULL;
  249. FCustomCommandRemoteFileList = NULL;
  250. FSessionColors = new TPngImageList(this);
  251. FSessionColors->ColorDepth = cd32Bit;
  252. AddFixedSessionImages();
  253. SessionsPageControl->Images = FSessionColors;
  254. UpdateQueueLabel();
  255. CreateHiddenWindow();
  256. StartUpdates();
  257. }
  258. //---------------------------------------------------------------------------
  259. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  260. {
  261. FInvalid = true;
  262. // this has to be one of the very first things to do
  263. StopUpdateThread();
  264. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  265. TTerminalManager::Instance()->ScpExplorer = NULL;
  266. delete FCustomCommandLocalFileList;
  267. delete FCustomCommandRemoteFileList;
  268. delete FCustomCommandMenu;
  269. delete FTrayIcon;
  270. FTrayIcon = NULL;
  271. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  272. delete FEditorManager;
  273. ForceCloseLocalEditors();
  274. delete FLocalEditors;
  275. HideFileFindDialog();
  276. if (FDelayedDeletionTimer)
  277. {
  278. DoDelayedDeletion(NULL);
  279. SAFE_DESTROY(FDelayedDeletionTimer);
  280. }
  281. SAFE_DESTROY(FDelayedDeletionList);
  282. // sometimes we do not get DDEnd so the list is not released
  283. SAFE_DESTROY(FDDFileList);
  284. DebugAssert(FSynchronizeController == NULL);
  285. CloseHandle(FDDExtMutex);
  286. FDDExtMutex = NULL;
  287. FreeLibrary(FOle32Library);
  288. FOle32Library = NULL;
  289. FDragMoveCursor = NULL;
  290. DebugAssert(!FErrorList);
  291. if (FEverShown)
  292. {
  293. // when window is never shown (like when running command-line operation),
  294. // particularly window site is not restored correctly (BoundsRect value set
  295. // in RestoreForm gets lost during handle allocation), so we do not want
  296. // it to be stored
  297. StoreParams();
  298. }
  299. ManagedSession = NULL;
  300. Queue = NULL;
  301. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  302. NonVisualDataModule->ScpExplorer = NULL;
  303. delete FQueueController;
  304. FQueueController = NULL;
  305. delete FQueueStatusSection;
  306. FQueueStatusSection = NULL;
  307. delete FQueueStatus;
  308. FQueueStatus = NULL;
  309. delete FUserActionTimer;
  310. FUserActionTimer = NULL;
  311. delete FNoteTimer;
  312. delete FNotes;
  313. delete FNoteData;
  314. SAFE_DESTROY(FDocks);
  315. SessionsPageControl->Images = NULL;
  316. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  317. SAFE_DESTROY(FSessionColors);
  318. SAFE_DESTROY(FSessionsDragDropFilesEx);
  319. SAFE_DESTROY(FHistoryMenu[0][0]);
  320. SAFE_DESTROY(FHistoryMenu[0][1]);
  321. SAFE_DESTROY(FHistoryMenu[1][0]);
  322. SAFE_DESTROY(FHistoryMenu[1][1]);
  323. if (FHiddenWindow != NULL)
  324. {
  325. DestroyWindow(FHiddenWindow);
  326. FHiddenWindow = NULL;
  327. }
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  331. {
  332. std::unique_ptr<TSessionData> Data;
  333. if (!Session.IsEmpty())
  334. {
  335. bool DefaultsOnly;
  336. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  337. }
  338. TTerminalManager * Manager = TTerminalManager::Instance();
  339. for (int Index = 0; Index < Manager->Count; Index++)
  340. {
  341. TTerminal * Terminal = Manager->Sessions[Index];
  342. if (Session.IsEmpty() ||
  343. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  344. {
  345. if (Path.IsEmpty())
  346. {
  347. Terminal->ClearCaches();
  348. }
  349. else
  350. {
  351. Terminal->DirectoryModified(Path, true);
  352. }
  353. }
  354. }
  355. // We should flag a pending refresh for the background terminals or busy foreground terminals
  356. if (HasActiveTerminal() &&
  357. CanCommandLineFromAnotherInstance() &&
  358. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  359. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  360. {
  361. Terminal->ReloadDirectory();
  362. }
  363. }
  364. //---------------------------------------------------------------------------
  365. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  366. {
  367. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  368. size_t MessageSize = sizeof(TCopyDataMessage);
  369. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  370. if (Result)
  371. {
  372. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  373. Result = (Message.Version == TCopyDataMessage::Version1);
  374. if (Result)
  375. {
  376. switch (Message.Command)
  377. {
  378. case TCopyDataMessage::CommandCanCommandLine:
  379. Result = CanCommandLineFromAnotherInstance();
  380. break;
  381. case TCopyDataMessage::CommandCommandLine:
  382. {
  383. UnicodeString CommandLine(Message.CommandLine);
  384. Result = CommandLineFromAnotherInstance(CommandLine);
  385. }
  386. break;
  387. case TCopyDataMessage::RefreshPanel:
  388. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  389. break;
  390. case TCopyDataMessage::MainWindowCheck:
  391. Result = true;
  392. break;
  393. default:
  394. Result = false;
  395. break;
  396. }
  397. }
  398. }
  399. Message.Result = Result ? 1 : 0;
  400. }
  401. //---------------------------------------------------------------------------
  402. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  403. {
  404. WNDCLASS WindowClass = {0};
  405. WindowClass.lpfnWndProc = DefWindowProc;
  406. WindowClass.hInstance = HInstance;
  407. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  408. FHiddenWindow = NULL;
  409. if (RegisterClass(&WindowClass))
  410. {
  411. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  412. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  413. }
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanConsole()
  417. {
  418. return
  419. HasActiveTerminal() &&
  420. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  421. }
  422. //---------------------------------------------------------------------------
  423. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  424. {
  425. bool Result = Visible && !NonVisualDataModule->Busy;
  426. return Result;
  427. }
  428. //---------------------------------------------------------------------------
  429. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  430. const UnicodeString & CommandLine)
  431. {
  432. TProgramParams Params(CommandLine);
  433. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  434. if (Result)
  435. {
  436. NonVisualDataModule->StartBusy();
  437. try
  438. {
  439. // this action is initiated from another process,
  440. // so it's likely that our window is not visible,
  441. // and user won't see what is going on
  442. Application->BringToFront();
  443. // reload sessions as we may be asked to open a session
  444. // just stored by another instance
  445. StoredSessions->Reload();
  446. UnicodeString SessionName = Params.Param[1];
  447. std::unique_ptr<TObjectList> DataList(new TObjectList());
  448. UnicodeString DownloadFile; // unused
  449. try
  450. {
  451. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  452. }
  453. catch (EAbort &)
  454. {
  455. // Happens when opening session in PuTTY and in other situations,
  456. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  457. // to process the command-line on its own (or to pass it to yet another instance)
  458. }
  459. if (DataList->Count > 0)
  460. {
  461. TTerminalManager * Manager = TTerminalManager::Instance();
  462. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  463. }
  464. }
  465. __finally
  466. {
  467. NonVisualDataModule->EndBusy();
  468. }
  469. }
  470. return Result;
  471. }
  472. //---------------------------------------------------------------------------
  473. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  474. {
  475. TManagedTerminal * Result;
  476. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  477. {
  478. Result = FManagedSession;
  479. }
  480. else
  481. {
  482. Result = NULL;
  483. }
  484. return Result;
  485. }
  486. //---------------------------------------------------------------------------
  487. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  488. {
  489. if (FManagedSession != value)
  490. {
  491. SessionChanging();
  492. DoSetManagedSession(value, false);
  493. }
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  497. {
  498. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  499. FManagedSession = value;
  500. {
  501. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  502. FAllowTransferPresetAutoSelect = false;
  503. TAutoFlag SessionChangingFlag(FSessionChanging);
  504. SessionChanged(Replace);
  505. }
  506. if (Terminal != NULL)
  507. {
  508. TransferPresetAutoSelect();
  509. }
  510. // Update app and tab titles, prevented above by FSessionChanging.
  511. UpdateControls();
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  515. {
  516. DoSetManagedSession(value, true);
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TCustomScpExplorerForm::SessionChanging()
  520. {
  521. if (ManagedSession != NULL)
  522. {
  523. UpdateSession(ManagedSession);
  524. }
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  528. {
  529. if (Terminal != NULL)
  530. {
  531. UpdateSessionColor((TColor)Terminal->StateData->Color);
  532. }
  533. if (!TTerminalManager::Instance()->Updating)
  534. {
  535. SessionListChanged();
  536. }
  537. if (Terminal != NULL)
  538. {
  539. TObject * State = NULL;
  540. if (WinConfiguration->PreservePanelState)
  541. {
  542. State = Terminal->RemoteExplorerState;
  543. DirView(osRemote)->AnnounceState(State);
  544. }
  545. try
  546. {
  547. if (Replaced)
  548. {
  549. RemoteDirView->ReplaceTerminal(Terminal);
  550. }
  551. else
  552. {
  553. RemoteDirView->Terminal = Terminal;
  554. }
  555. if (Terminal->Active)
  556. {
  557. Terminal->RefreshDirectory();
  558. }
  559. if (State != NULL)
  560. {
  561. DirView(osRemote)->RestoreState(State);
  562. }
  563. }
  564. __finally
  565. {
  566. DirView(osRemote)->AnnounceState(NULL);
  567. }
  568. }
  569. else
  570. {
  571. RemoteDirView->Terminal = NULL;
  572. }
  573. InitStatusBar();
  574. UpdateTransferList();
  575. // Update panels Enable state before refreshing the labels
  576. UpdateControls();
  577. }
  578. //---------------------------------------------------------------------------
  579. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  580. {
  581. if (Queue != value)
  582. {
  583. if (FQueue != NULL)
  584. {
  585. FQueue->OnListUpdate = NULL;
  586. FQueue->OnQueueItemUpdate = NULL;
  587. }
  588. FQueue = value;
  589. if (FQueue != NULL)
  590. {
  591. DebugAssert(FQueue->OnListUpdate == NULL);
  592. FQueue->OnListUpdate = QueueListUpdate;
  593. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  594. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  595. }
  596. QueueChanged();
  597. }
  598. }
  599. //---------------------------------------------------------------------------
  600. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  601. TListItem * Item)
  602. {
  603. if (FQueueActedItem == Item)
  604. {
  605. FQueueActedItem = NULL;
  606. if ((QueueView3->PopupMenu != NULL) &&
  607. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  608. {
  609. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  610. ReleaseCapture();
  611. }
  612. }
  613. if (Item->Data == FPendingQueueActionItem)
  614. {
  615. FPendingQueueActionItem = NULL;
  616. }
  617. }
  618. //---------------------------------------------------------------------------
  619. bool TCustomScpExplorerForm::IsAnythingQueued()
  620. {
  621. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  622. }
  623. //---------------------------------------------------------------------------
  624. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  625. {
  626. {
  627. TGuard Guard(FQueueStatusSection);
  628. FQueueStatusInvalidated = false;
  629. if (FQueue != NULL)
  630. {
  631. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  632. }
  633. }
  634. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  635. {
  636. FMaxQueueLength = FQueueStatus->Count;
  637. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  638. }
  639. {
  640. TAutoFlag Flag(FQueueStatusUpdating);
  641. FQueueController->UpdateQueueStatus(FQueueStatus);
  642. }
  643. UpdateQueueFileList();
  644. SetQueueProgress();
  645. UpdateQueueView();
  646. bool IsEmpty = !IsAnythingQueued();
  647. if (IsEmpty && (Terminal != NULL))
  648. {
  649. TOnceDoneOperation OnceDoneOperation =
  650. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  651. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  652. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  653. {
  654. FQueue->Enabled = false;
  655. }
  656. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  657. {
  658. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  659. }
  660. }
  661. }
  662. //---------------------------------------------------------------------------
  663. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  664. {
  665. UnicodeString Result;
  666. if (FQueueStatus != NULL)
  667. {
  668. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  669. {
  670. TFileOperationProgressType * ProgressData =
  671. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  672. if ((ProgressData != NULL) && ProgressData->InProgress)
  673. {
  674. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  675. }
  676. }
  677. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  678. {
  679. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  680. }
  681. }
  682. return Result;
  683. }
  684. //---------------------------------------------------------------------------
  685. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  686. {
  687. ComponentVisible[fcQueueView] =
  688. !IsLocalBrowserMode() &&
  689. ((WinConfiguration->QueueView.Show == qvShow) ||
  690. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  691. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  692. }
  693. //---------------------------------------------------------------------------
  694. void __fastcall TCustomScpExplorerForm::QueueChanged()
  695. {
  696. if (FQueueStatus != NULL)
  697. {
  698. // UpdateFileList implementation relies on the status being recreated when session changes
  699. delete FQueueStatus;
  700. FQueueStatus = NULL;
  701. }
  702. UpdateQueueStatus(true);
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  706. {
  707. if (FQueue == Queue)
  708. {
  709. FQueueStatusInvalidated = true;
  710. }
  711. }
  712. //---------------------------------------------------------------------------
  713. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  714. TQueueItem * Item)
  715. {
  716. if (FQueue == Queue)
  717. {
  718. TGuard Guard(FQueueStatusSection);
  719. // this may be running in parallel with QueueChanged
  720. if (FQueueStatus != NULL)
  721. {
  722. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  723. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  724. {
  725. FRefreshLocalDirectory = (QueueItem == NULL) ||
  726. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  727. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  728. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  729. }
  730. if (QueueItem != NULL)
  731. {
  732. QueueItem->UserData = (void*)true;
  733. FQueueItemInvalidated = true;
  734. }
  735. }
  736. }
  737. }
  738. //---------------------------------------------------------------------------
  739. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  740. {
  741. // during standalone editing, we have no way to see/control queue,
  742. // so we have to always popup prompts automatically
  743. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  744. }
  745. //---------------------------------------------------------------------------
  746. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  747. {
  748. if (FQueueStatus != NULL)
  749. {
  750. bool QueueAutoPopup = IsQueueAutoPopup();
  751. bool NeedRefresh = FQueueController->NeedRefresh();
  752. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  753. FQueueItemInvalidated = false;
  754. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  755. bool Updated = false;
  756. TQueueItemProxy * QueueItem;
  757. bool WasUserAction;
  758. for (int Index = 0; Index < Limit; Index++)
  759. {
  760. bool Update = false;
  761. QueueItem = FQueueStatus->Items[Index];
  762. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  763. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  764. (FPendingQueueActionItem == NULL))
  765. {
  766. FPendingQueueActionItem = QueueItem;
  767. FUserActionTimer->Enabled = true;
  768. }
  769. if ((bool)QueueItem->UserData)
  770. {
  771. QueueItem->UserData = (void*)false;
  772. QueueItem->Update();
  773. Updated = true;
  774. Update = true;
  775. }
  776. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  777. {
  778. Update = true;
  779. }
  780. if (Update)
  781. {
  782. FQueueController->RefreshQueueItem(QueueItem);
  783. }
  784. }
  785. if (Updated)
  786. {
  787. NonVisualDataModule->UpdateNonVisibleActions();
  788. SetQueueProgress();
  789. UpdateQueueFileList();
  790. }
  791. }
  792. }
  793. //---------------------------------------------------------------------------
  794. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  795. {
  796. if (FTaskbarList != NULL)
  797. {
  798. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  799. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  800. }
  801. }
  802. //---------------------------------------------------------------------------
  803. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  804. {
  805. if (Progress >= 0)
  806. {
  807. if (FTaskbarList != NULL)
  808. {
  809. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  810. }
  811. }
  812. else
  813. {
  814. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  815. }
  816. }
  817. //---------------------------------------------------------------------------
  818. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  819. {
  820. int OverallProgress;
  821. // FProgressForm is null when this is called from SetQueueProgress
  822. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  823. {
  824. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  825. }
  826. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  827. {
  828. OverallProgress = ProgressData->OverallProgress();
  829. }
  830. else
  831. {
  832. OverallProgress = -1;
  833. }
  834. SetTaskbarListProgressValue(OverallProgress);
  835. }
  836. //---------------------------------------------------------------------------
  837. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  838. {
  839. TTerminalManager::Instance()->QueueStatusUpdated();
  840. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  841. {
  842. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  843. {
  844. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  845. {
  846. TFileOperationProgressType * ProgressData = NULL;
  847. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  848. {
  849. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  850. }
  851. if ((ProgressData != NULL) &&
  852. ProgressData->InProgress)
  853. {
  854. SetTaskbarListProgressValue(ProgressData);
  855. }
  856. else
  857. {
  858. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  859. }
  860. }
  861. else
  862. {
  863. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  864. }
  865. }
  866. else
  867. {
  868. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  869. }
  870. }
  871. UpdateQueueLabel();
  872. }
  873. //---------------------------------------------------------------------------
  874. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  875. {
  876. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  877. if (FQueueStatus != NULL)
  878. {
  879. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  880. if (ActiveAndPendingPrimaryCount > 0)
  881. {
  882. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  883. }
  884. }
  885. QueueLabel->Caption = Caption;
  886. }
  887. //---------------------------------------------------------------------------
  888. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  889. {
  890. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  891. static_cast<TComponent*>(GetComponent(fcTransferList)));
  892. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  893. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  894. TransferList->Strings->BeginUpdate();
  895. try
  896. {
  897. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  898. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  899. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  900. if (FTransferDropDownHint.IsEmpty())
  901. {
  902. FTransferDropDownHint = TransferDropDown->Hint;
  903. }
  904. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  905. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  906. TransferDropDown->Text = StripHotkey(Name);
  907. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  908. (FTransferDropDownHint, StripHotkey(Name),
  909. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  910. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  911. // update the label, otherwise when it is updated only on the first draw
  912. // of the list, it is drawn "bold" for some reason
  913. FTransferListHoverIndex = TransferList->ItemIndex;
  914. UpdateTransferLabel();
  915. }
  916. __finally
  917. {
  918. TransferList->Strings->EndUpdate();
  919. }
  920. }
  921. //---------------------------------------------------------------------------
  922. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  923. {
  924. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  925. DebugAssert(Toolbar != NULL);
  926. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  927. }
  928. //---------------------------------------------------------------------------
  929. void __fastcall TCustomScpExplorerForm::UpdateActions()
  930. {
  931. TForm::UpdateActions();
  932. if (ComponentVisible[fcCustomCommandsBand])
  933. {
  934. UpdateCustomCommandsToolbar();
  935. }
  936. }
  937. //---------------------------------------------------------------------------
  938. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  939. {
  940. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  941. }
  942. //---------------------------------------------------------------------------
  943. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  944. {
  945. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  946. {
  947. DirView->RowSelect = WinConfiguration->FullRowSelect;
  948. // selection is not redrawn automatically when RowSelect changes
  949. DirView->Invalidate();
  950. }
  951. }
  952. //---------------------------------------------------------------------------
  953. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  954. {
  955. Color = GetBtnFaceColor();
  956. DebugAssert(Configuration && RemoteDirView);
  957. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  958. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  959. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  960. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  961. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  962. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  963. UpdateRowSelect(RemoteDirView);
  964. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  965. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  966. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  967. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  968. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  969. UpdateSessionsPageControlHeight();
  970. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  971. UpdateToolbarDisplayMode();
  972. UpdateTransferList();
  973. if (Terminal != NULL)
  974. {
  975. TransferPresetAutoSelect();
  976. }
  977. UpdateQueueView();
  978. UpdateControls();
  979. // this can be called even before constuctor finishes.
  980. if (FEditorManager != NULL)
  981. {
  982. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  983. }
  984. // this can be called even before constuctor finishes.
  985. if (FLocalEditors != NULL)
  986. {
  987. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  988. {
  989. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  990. }
  991. }
  992. if (ComponentVisible[fcCustomCommandsBand])
  993. {
  994. UpdateCustomCommandsToolbar();
  995. }
  996. // show only when keeping queue items forever,
  997. // otherwise, its enough to have in in the context menu
  998. QueueDeleteAllDoneQueueToolbarItem->Visible =
  999. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1000. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1001. {
  1002. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1003. FFileColorsCurrent = WinConfiguration->FileColors;
  1004. FileColorsChanged();
  1005. }
  1006. }
  1007. //---------------------------------------------------------------------------
  1008. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1009. {
  1010. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1011. DirView->Invalidate();
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1015. {
  1016. DoFileColorsChanged(RemoteDirView);
  1017. }
  1018. //---------------------------------------------------------------------------
  1019. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1020. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1021. void * /*Arg*/)
  1022. {
  1023. if (Token != NULL)
  1024. {
  1025. TForm * Editor = dynamic_cast<TForm*>(Token);
  1026. DebugAssert(Editor != NULL);
  1027. ReconfigureEditorForm(Editor);
  1028. }
  1029. }
  1030. //---------------------------------------------------------------------------
  1031. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1032. {
  1033. WinConfiguration->DDTransferConfirmation = asOn;
  1034. }
  1035. //---------------------------------------------------------------------------
  1036. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1037. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1038. {
  1039. // noop
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1043. {
  1044. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1045. }
  1046. //---------------------------------------------------------------------------
  1047. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1048. {
  1049. if (DoNotShowAgain)
  1050. {
  1051. if (DragDrop)
  1052. {
  1053. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1054. {
  1055. PopupTrayBalloon(
  1056. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1057. }
  1058. WinConfiguration->DDTransferConfirmation = asOff;
  1059. }
  1060. else
  1061. {
  1062. WinConfiguration->ConfirmTransferring = false;
  1063. }
  1064. }
  1065. else
  1066. {
  1067. // User explicitly unchecked "do not show again",
  1068. // so show him the dialog the next time
  1069. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1070. {
  1071. WinConfiguration->DDTransferConfirmation = asOn;
  1072. }
  1073. }
  1074. }
  1075. //---------------------------------------------------------------------------
  1076. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1077. TTransferDirection Direction, TTransferType Type, bool Temp,
  1078. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1079. bool Confirm, bool DragDrop, int Options)
  1080. {
  1081. bool Result = true;
  1082. DebugAssert(HasActiveTerminal());
  1083. // these parameters are known in advance
  1084. int Params =
  1085. FLAGMASK(Type == ttMove, cpDelete);
  1086. bool ToTemp = (Temp && (Direction == tdToLocal));
  1087. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1088. {
  1089. bool DisableNewerOnly =
  1090. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1091. ToTemp;
  1092. Options |=
  1093. FLAGMASK(ToTemp, coTemp) |
  1094. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1095. coDoNotShowAgain;
  1096. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1097. int CopyParamAttrs = (Direction == tdToRemote ?
  1098. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1099. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1100. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1101. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1102. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1103. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1104. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1105. if (Result)
  1106. {
  1107. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1108. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1109. }
  1110. }
  1111. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1112. {
  1113. Configuration->Usage->Inc(L"TransfersOnBackground");
  1114. // these parameter are known only after transfer dialog
  1115. Params |=
  1116. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1117. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1118. Result = false;
  1119. ClearTransferSourceSelection(Direction);
  1120. }
  1121. return Result;
  1122. }
  1123. //---------------------------------------------------------------------------
  1124. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1125. {
  1126. if (FOnFileOperationFinished != NULL)
  1127. {
  1128. FOnFileOperationFinished(UnicodeString(), true);
  1129. }
  1130. else
  1131. {
  1132. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1133. if (HasDirView[Side])
  1134. {
  1135. DirView(Side)->SelectAll(smNone);
  1136. }
  1137. }
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1141. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1142. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1143. int Params)
  1144. {
  1145. DebugAssert(Queue != NULL);
  1146. bool SingleFile = false;
  1147. if (FileList->Count == 1)
  1148. {
  1149. if (Direction == tdToRemote)
  1150. {
  1151. UnicodeString FileName = FileList->Strings[0];
  1152. SingleFile = FileExists(ApiPath(FileName));
  1153. }
  1154. else
  1155. {
  1156. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1157. SingleFile = !File->IsDirectory;
  1158. }
  1159. }
  1160. TQueueItem * QueueItem;
  1161. if (Direction == tdToRemote)
  1162. {
  1163. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1164. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1165. }
  1166. else
  1167. {
  1168. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1169. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1170. }
  1171. AddQueueItem(Queue, QueueItem, Terminal);
  1172. }
  1173. //---------------------------------------------------------------------------
  1174. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1175. {
  1176. if (Queue->IsEmpty)
  1177. {
  1178. Terminal->QueueOperationStart = Now();
  1179. }
  1180. Queue->AddItem(QueueItem);
  1181. }
  1182. //---------------------------------------------------------------------------
  1183. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1184. {
  1185. }
  1186. //---------------------------------------------------------------------------
  1187. void __fastcall TCustomScpExplorerForm::InitControls()
  1188. {
  1189. DebugAssert(FDocks != NULL);
  1190. for (int Index = 0; Index < ComponentCount; Index++)
  1191. {
  1192. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1193. if ((Dock != NULL) && (Dock->Tag == 0))
  1194. {
  1195. FDocks->Add(Dock);
  1196. }
  1197. }
  1198. CollectItemsWithTextDisplayMode(this);
  1199. }
  1200. //---------------------------------------------------------------------------
  1201. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1202. {
  1203. ConfigurationChanged();
  1204. QueuePanel->Height =
  1205. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1206. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1207. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1208. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1209. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1210. QueueFileList->Height =
  1211. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1212. if (QueueFileList->Visible)
  1213. {
  1214. AdjustQueueLayout();
  1215. }
  1216. UpdateCustomCommandsToolbar();
  1217. }
  1218. //---------------------------------------------------------------------------
  1219. void __fastcall TCustomScpExplorerForm::StoreParams()
  1220. {
  1221. int APixelsPerInch = GetControlPixelsPerInch(this);
  1222. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1223. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1224. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1225. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1226. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1227. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1228. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1229. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1230. }
  1231. //---------------------------------------------------------------------------
  1232. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1233. {
  1234. if (!FFormRestored)
  1235. {
  1236. FFormRestored = true;
  1237. RestoreFormParams();
  1238. }
  1239. TForm::CreateParams(Params);
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1243. {
  1244. DebugAssert(FDocks != NULL);
  1245. for (int Index = 0; Index < FDocks->Count; Index++)
  1246. {
  1247. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1248. }
  1249. }
  1250. //---------------------------------------------------------------------------
  1251. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1252. {
  1253. SetDockAllowDrag(true);
  1254. while (!ButtonsStr.IsEmpty())
  1255. {
  1256. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1257. UnicodeString Name = CutToChar(S, L'=', true);
  1258. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1259. {
  1260. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1261. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1262. {
  1263. while (!S.IsEmpty())
  1264. {
  1265. UnicodeString S2 = CutToChar(S, L',', true);
  1266. UnicodeString Name = CutToChar(S2, L':', true);
  1267. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1268. {
  1269. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1270. if (SameText(GetToolbarItemName(Item), Name))
  1271. {
  1272. // forward compatibility
  1273. UnicodeString S3 = CutToChar(S2, L':', true);
  1274. bool Visible = StrToIntDef(S3, 0);
  1275. Item->Visible = Visible;
  1276. }
  1277. }
  1278. }
  1279. }
  1280. }
  1281. }
  1282. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1283. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1284. }
  1285. //---------------------------------------------------------------------------
  1286. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1287. {
  1288. UnicodeString Result;
  1289. SetDockAllowDrag(true);
  1290. Result = ::GetToolbarsLayoutStr(this);
  1291. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1292. return Result;
  1293. }
  1294. //---------------------------------------------------------------------------
  1295. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1296. {
  1297. UnicodeString Result;
  1298. if (Item->Action != NULL)
  1299. {
  1300. Result = Item->Action->Name;
  1301. Result = RemoveSuffix(Result, L"Action", true);
  1302. }
  1303. else
  1304. {
  1305. Result = Item->Name;
  1306. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1307. Result = RemoveSuffix(Result, L"Item", true);
  1308. }
  1309. return Result;
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1313. {
  1314. UnicodeString Result;
  1315. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1316. {
  1317. UnicodeString ToolbarStr;
  1318. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1319. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1320. {
  1321. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1322. {
  1323. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1324. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1325. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1326. // and save the changes here only.
  1327. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1328. {
  1329. UnicodeString Name = GetToolbarItemName(Item);
  1330. DebugAssert(Name.Pos(L"TBX") == 0);
  1331. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1332. }
  1333. }
  1334. }
  1335. if (!ToolbarStr.IsEmpty())
  1336. {
  1337. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1338. }
  1339. }
  1340. return Result;
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1344. {
  1345. DebugAssert(FProgressForm == NULL);
  1346. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1347. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1348. FProgressForm->DeleteLocalToRecycleBin =
  1349. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1350. FProgressForm->DeleteRemoteToRecycleBin =
  1351. HasActiveTerminal() &&
  1352. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1353. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1354. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1355. LockWindow();
  1356. // Actually, do not know what hides the progress once the operation finishes
  1357. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1358. SetTaskbarListProgressState(TBPF_NORMAL);
  1359. }
  1360. //---------------------------------------------------------------------------
  1361. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1362. {
  1363. UnlockWindow();
  1364. SAFE_DESTROY(FProgressForm);
  1365. SetQueueProgress();
  1366. }
  1367. //---------------------------------------------------------------------------
  1368. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1369. TFileOperationProgressType & ProgressData)
  1370. {
  1371. // Download to temporary local directory
  1372. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1373. {
  1374. bool Continue = true;
  1375. FPendingTempSpaceWarn = false;
  1376. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1377. if (!Continue)
  1378. {
  1379. Abort();
  1380. }
  1381. }
  1382. // operation is being executed and we still didn't show up progress form
  1383. if (ProgressData.InProgress && !FProgressForm)
  1384. {
  1385. CreateProgressForm(NULL);
  1386. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1387. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1388. }
  1389. // operation is finished (or terminated), so we hide progress form
  1390. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1391. {
  1392. DestroyProgressForm();
  1393. if (ProgressData.Operation == foCalculateSize)
  1394. {
  1395. // When calculating size before transfer, the abort caused by
  1396. // cancel flag set due to "MoveToQueue" below,
  1397. // is not propagated back to us in ExecuteFileOperation,
  1398. // as it is silently swallowed.
  1399. // So we explicitly (re)throw it here.
  1400. if (FMoveToQueue)
  1401. {
  1402. Abort();
  1403. }
  1404. }
  1405. else
  1406. {
  1407. if ((ProgressData.Cancel == csContinue) &&
  1408. !FAutoOperation)
  1409. {
  1410. OperationComplete(ProgressData.StartTime);
  1411. }
  1412. }
  1413. }
  1414. if (FProgressForm &&
  1415. // Can happen only during synchronization, when ending one of the operations
  1416. ProgressData.InProgress)
  1417. {
  1418. FProgressForm->SetProgressData(ProgressData);
  1419. DebugAssert(ProgressData.InProgress);
  1420. SetTaskbarListProgressValue(&ProgressData);
  1421. if (FProgressForm->Cancel > csContinue)
  1422. {
  1423. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1424. if (FProgressForm->Cancel == csCancelFile)
  1425. {
  1426. FProgressForm->ClearCancel();
  1427. }
  1428. // cancel cancels even the move
  1429. FMoveToQueue = false;
  1430. }
  1431. else if (FProgressForm->MoveToQueue)
  1432. {
  1433. FMoveToQueue = true;
  1434. ProgressData.SetCancelAtLeast(csCancel);
  1435. }
  1436. if ((FTransferResumeList != NULL) &&
  1437. ProgressData.InProgress &&
  1438. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1439. !ProgressData.FullFileName.IsEmpty())
  1440. {
  1441. if ((FTransferResumeList->Count == 0) ||
  1442. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1443. {
  1444. // note that we do not recognize directories from files here
  1445. FTransferResumeList->Add(ProgressData.FullFileName);
  1446. }
  1447. }
  1448. }
  1449. }
  1450. //---------------------------------------------------------------------------
  1451. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1452. const TDateTime & StartTime)
  1453. {
  1454. if (GUIConfiguration->BeepOnFinish &&
  1455. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1456. {
  1457. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1458. DWORD Sound;
  1459. if (!ExtractFileExt(BeepSound).IsEmpty())
  1460. {
  1461. Sound = SND_FILENAME;
  1462. }
  1463. else
  1464. {
  1465. Sound = SND_ALIAS;
  1466. }
  1467. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1468. }
  1469. }
  1470. //---------------------------------------------------------------------------
  1471. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1472. TFileOperationProgressType & ProgressData)
  1473. {
  1474. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1475. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1476. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1477. FileOperationProgress(ProgressData);
  1478. }
  1479. //---------------------------------------------------------------------------
  1480. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1481. {
  1482. UnicodeString Result;
  1483. if (FProgressForm != NULL)
  1484. {
  1485. Result = FProgressForm->ProgressStr();
  1486. }
  1487. return Result;
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1491. bool DragDrop)
  1492. {
  1493. return (!DragDrop && (DropSourceControl == NULL)) ||
  1494. (DropSourceControl == DirView(osOther));
  1495. }
  1496. //---------------------------------------------------------------------------
  1497. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1498. TFileOperation Operation, TOperationSide Side,
  1499. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1500. TOnceDoneOperation & OnceDoneOperation)
  1501. {
  1502. if (!FAutoOperation)
  1503. {
  1504. // no selection on "/upload", form serves only as event handler
  1505. // (it is not displayed)
  1506. if (PanelOperation(Side, FDragDropOperation) &&
  1507. Visible && (Operation != foCalculateSize) &&
  1508. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1509. {
  1510. if (FOnFileOperationFinished != NULL)
  1511. {
  1512. FOnFileOperationFinished(FileName, Success);
  1513. }
  1514. else
  1515. {
  1516. TOperationSide DViewSide;
  1517. if ((FProgressSide != osCurrent) &&
  1518. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1519. {
  1520. // assume the operation is over the focused panel
  1521. DViewSide = FProgressSide;
  1522. }
  1523. else
  1524. {
  1525. DViewSide = Side;
  1526. }
  1527. TCustomDirView * DView = DirView(DViewSide);
  1528. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(DViewSide));
  1529. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1530. // this can happen when local drive is unplugged in the middle of the operation
  1531. if (Item != NULL)
  1532. {
  1533. if (Success) Item->Selected = false;
  1534. DView->MakeProgressVisible(Item);
  1535. }
  1536. }
  1537. }
  1538. if ((Operation == foCopy) || (Operation == foMove))
  1539. {
  1540. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1541. {
  1542. if (Side == osLocal)
  1543. {
  1544. Configuration->Usage->Inc(L"UploadedFiles");
  1545. }
  1546. else
  1547. {
  1548. Configuration->Usage->Inc(L"DownloadedFiles");
  1549. }
  1550. }
  1551. }
  1552. }
  1553. if (Success && (FSynchronizeController != NULL))
  1554. {
  1555. DebugAssert(!IsSideLocalBrowser(osRemote));
  1556. if (Operation == foCopy)
  1557. {
  1558. DebugAssert(Side == osLocal);
  1559. FSynchronizeController->LogOperation(soUpload, FileName);
  1560. }
  1561. else if (Operation == foDelete)
  1562. {
  1563. DebugAssert(Side == osRemote);
  1564. FSynchronizeController->LogOperation(soDelete, FileName);
  1565. }
  1566. }
  1567. if (FProgressForm)
  1568. {
  1569. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1570. }
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1574. TFileOperation Operation, TOperationSide Side,
  1575. bool Temp, const UnicodeString & FileName, Boolean Success,
  1576. TOnceDoneOperation & OnceDoneOperation)
  1577. {
  1578. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1579. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1580. OnceDoneOperation);
  1581. }
  1582. //---------------------------------------------------------------------------
  1583. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1584. {
  1585. return false;
  1586. }
  1587. //---------------------------------------------------------------------------
  1588. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1589. {
  1590. return false;
  1591. }
  1592. //---------------------------------------------------------------------------
  1593. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1594. {
  1595. return false;
  1596. }
  1597. //---------------------------------------------------------------------------
  1598. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1599. {
  1600. return NULL;
  1601. }
  1602. //---------------------------------------------------------------------------
  1603. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1604. {
  1605. DebugAssert(GetSide(Side) == osRemote);
  1606. DebugUsedParam(Side);
  1607. return RemoteDirView;
  1608. }
  1609. //---------------------------------------------------------------------------
  1610. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1611. {
  1612. DebugAssert(GetSide(Side) == osRemote);
  1613. DebugUsedParam(Side);
  1614. return RemoteDriveView;
  1615. }
  1616. //---------------------------------------------------------------------------
  1617. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1618. {
  1619. DebugAssert(GetSide(Side) == osRemote);
  1620. DebugUsedParam(Side);
  1621. return HasActiveTerminal();
  1622. }
  1623. //---------------------------------------------------------------------------
  1624. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1625. TOperationSide Side, int FilesOnly)
  1626. {
  1627. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1628. }
  1629. //---------------------------------------------------------------------------
  1630. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1631. TOperationSide Side, int FilesOnly)
  1632. {
  1633. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1634. }
  1635. //---------------------------------------------------------------------------
  1636. struct THistoryItemData
  1637. {
  1638. short int Side;
  1639. short int Index;
  1640. };
  1641. //---------------------------------------------------------------------------
  1642. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1643. {
  1644. DirView(Side)->HistoryGo(Index);
  1645. }
  1646. //---------------------------------------------------------------------------
  1647. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1648. {
  1649. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1650. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1651. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1652. }
  1653. //---------------------------------------------------------------------------
  1654. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1655. bool Back)
  1656. {
  1657. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1658. {
  1659. TCustomDirView * DView = DirView(Side);
  1660. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1661. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1662. if (ICount > 10)
  1663. {
  1664. ICount = 10;
  1665. }
  1666. Menu->Items->Clear();
  1667. THistoryItemData Data;
  1668. Data.Side = Side;
  1669. for (short int i = 1; i <= ICount; i++)
  1670. {
  1671. TTBCustomItem * Item = new TTBXItem(Menu);
  1672. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1673. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1674. Item->Hint = DView->HistoryPath[Data.Index];
  1675. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1676. Item->Tag = *reinterpret_cast<int*>(&Data);
  1677. Item->OnClick = HistoryItemClick;
  1678. Menu->Items->Add(Item);
  1679. }
  1680. }
  1681. }
  1682. //---------------------------------------------------------------------------
  1683. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1684. TOperationSide Side, bool Back)
  1685. {
  1686. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1687. {
  1688. // workaround
  1689. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1690. // operator new allocates memory in Pascal code, but calls inline
  1691. // contructor in C++, leading in problems, the function does
  1692. // both in Pascal code
  1693. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1694. UpdateHistoryMenu(Side, Back);
  1695. }
  1696. return FHistoryMenu[Side == osLocal][Back];
  1697. }
  1698. //---------------------------------------------------------------------------
  1699. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1700. TCustomDirView *Sender)
  1701. {
  1702. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1703. UpdateHistoryMenu(Side, true);
  1704. UpdateHistoryMenu(Side, false);
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1708. {
  1709. // remote custom commands can be executed only if the server supports shell commands
  1710. // or have secondary shell
  1711. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1715. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1716. {
  1717. // -1 = hidden, 0 = disabled, 1 = enabled
  1718. int Result;
  1719. TFileCustomCommand RemoteCustomCommand;
  1720. TLocalCustomCommand LocalCustomCommand;
  1721. TFileCustomCommand * NonInteractiveCustomCommand =
  1722. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1723. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1724. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1725. if (FLAGCLEAR(Command.Params, ccLocal))
  1726. {
  1727. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1728. // custom command that does not operate with files can be executed anytime ...
  1729. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1730. {
  1731. // ... but do not show such command in remote file menu
  1732. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1733. {
  1734. Result = AllowedState;
  1735. }
  1736. else
  1737. {
  1738. Result = -1;
  1739. }
  1740. }
  1741. else
  1742. {
  1743. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1744. {
  1745. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1746. }
  1747. else
  1748. {
  1749. Result = -1;
  1750. }
  1751. }
  1752. }
  1753. else
  1754. {
  1755. // custom command that does not operate with files can be executed anytime
  1756. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1757. {
  1758. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1759. }
  1760. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1761. {
  1762. if ((ListType == ccltAll) || (ListType == ccltFile))
  1763. {
  1764. // special case is "diff"-style command that can be executed over any side,
  1765. // if we have both sides
  1766. Result =
  1767. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1768. // directly (instead of EnableSelectedOperation) to pass
  1769. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1770. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1771. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1772. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1773. }
  1774. else if (ListType == ccltBoth)
  1775. {
  1776. DebugAssert(!IsLocalBrowserMode());
  1777. Result = 1;
  1778. }
  1779. else
  1780. {
  1781. Result = -1;
  1782. }
  1783. }
  1784. else
  1785. {
  1786. if ((ListType == ccltAll) ||
  1787. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1788. {
  1789. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1790. }
  1791. else
  1792. {
  1793. Result = -1;
  1794. }
  1795. }
  1796. if ((Result > 0) &&
  1797. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1798. !HasActiveTerminal())
  1799. {
  1800. Result = 0;
  1801. }
  1802. }
  1803. return Result;
  1804. }
  1805. //---------------------------------------------------------------------------
  1806. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1807. TStrings * FileList, const TCustomCommandType & ACommand,
  1808. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1809. {
  1810. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1811. {
  1812. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1813. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1814. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1815. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1816. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1817. bool Capture =
  1818. FLAGSET(ACommand.Params, ccShowResults) ||
  1819. FLAGSET(ACommand.Params, ccCopyResults) ||
  1820. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1821. TCaptureOutputEvent OutputEvent = NULL;
  1822. DebugAssert(FCapturedLog == NULL);
  1823. if (Capture)
  1824. {
  1825. FCapturedLog = new TStringList();
  1826. OutputEvent = TerminalCaptureLog;
  1827. }
  1828. try
  1829. {
  1830. if (!RemoteCustomCommand.IsFileCommand(Command))
  1831. {
  1832. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1833. OutputEvent);
  1834. }
  1835. else
  1836. {
  1837. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1838. }
  1839. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1840. {
  1841. if (FLAGSET(ACommand.Params, ccCopyResults))
  1842. {
  1843. CopyToClipboard(FCapturedLog);
  1844. }
  1845. if (FLAGSET(ACommand.Params, ccShowResults))
  1846. {
  1847. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1848. }
  1849. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1850. {
  1851. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1852. }
  1853. }
  1854. }
  1855. __finally
  1856. {
  1857. SAFE_DESTROY(FCapturedLog);
  1858. }
  1859. }
  1860. }
  1861. //---------------------------------------------------------------------------
  1862. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1863. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1864. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1865. {
  1866. DebugAssert(!IsLocalBrowserMode());
  1867. TStrings * LocalFileList = NULL;
  1868. TStrings * RemoteFileList = NULL;
  1869. TStrings * RemoteFileListFull = NULL;
  1870. try
  1871. {
  1872. if (LocalFileCommand)
  1873. {
  1874. if (ALocalFileList == NULL)
  1875. {
  1876. DebugAssert(HasDirView[osLocal]);
  1877. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1878. // directly (instead of EnableSelectedOperation) to pass
  1879. // false to FocusedFileOnlyWhenFocused
  1880. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1881. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1882. }
  1883. else
  1884. {
  1885. LocalFileList = ALocalFileList;
  1886. }
  1887. if (FileListCommand)
  1888. {
  1889. if (LocalFileList->Count != 1)
  1890. {
  1891. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1892. }
  1893. }
  1894. else
  1895. {
  1896. if ((LocalFileList->Count != 1) &&
  1897. (FileList->Count != 1) &&
  1898. (LocalFileList->Count != FileList->Count))
  1899. {
  1900. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1901. }
  1902. }
  1903. }
  1904. UnicodeString RootTempDir;
  1905. UnicodeString TempDir;
  1906. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1907. if (!RemoteFiles)
  1908. {
  1909. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1910. }
  1911. try
  1912. {
  1913. TDateTimes RemoteFileTimes;
  1914. if (RemoteFiles)
  1915. {
  1916. RemoteFileList = FileList;
  1917. }
  1918. else
  1919. {
  1920. RemoteFileList = new TStringList();
  1921. TMakeLocalFileListParams MakeFileListParam;
  1922. MakeFileListParam.FileList = RemoteFileList;
  1923. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1924. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1925. MakeFileListParam.Recursive =
  1926. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1927. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1928. if (!MakeFileListParam.Recursive)
  1929. {
  1930. RemoteFileTimes.clear();
  1931. RemoteFileListFull = new TStringList();
  1932. MakeFileListParam.FileList = RemoteFileListFull;
  1933. MakeFileListParam.Recursive = true;
  1934. MakeFileListParam.IncludeDirs = false;
  1935. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1936. }
  1937. else
  1938. {
  1939. RemoteFileListFull = RemoteFileList;
  1940. }
  1941. }
  1942. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1943. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1944. if (!NonBlocking)
  1945. {
  1946. int Count;
  1947. if (FileListCommand)
  1948. {
  1949. Count = 1;
  1950. }
  1951. else if (LocalFileCommand)
  1952. {
  1953. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1954. }
  1955. else
  1956. {
  1957. Count = RemoteFileList->Count;
  1958. }
  1959. Progress.Start(foCustomCommand, osRemote, Count);
  1960. DebugAssert(FProgressForm != NULL);
  1961. FProgressForm->ReadOnly = true;
  1962. }
  1963. try
  1964. {
  1965. if (FileListCommand)
  1966. {
  1967. UnicodeString LocalFile;
  1968. // MakeFileList does not delimit filenames
  1969. UnicodeString FileList = MakeFileList(RemoteFileList);
  1970. if (LocalFileCommand)
  1971. {
  1972. DebugAssert(LocalFileList->Count == 1);
  1973. LocalFile = LocalFileList->Strings[0];
  1974. }
  1975. TLocalCustomCommand CustomCommand(Data,
  1976. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1977. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1978. if (NonBlocking)
  1979. {
  1980. DebugAssert(!POutput);
  1981. ExecuteShellChecked(ShellCommand);
  1982. }
  1983. else
  1984. {
  1985. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1986. }
  1987. }
  1988. else if (LocalFileCommand)
  1989. {
  1990. if (LocalFileList->Count == 1)
  1991. {
  1992. UnicodeString LocalFile = LocalFileList->Strings[0];
  1993. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1994. {
  1995. UnicodeString FileName = RemoteFileList->Strings[Index];
  1996. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1997. Progress.SetFile(FileNameForProgress);
  1998. TLocalCustomCommand CustomCommand(Data,
  1999. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2000. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2001. TOnceDoneOperation OnceDoneOperation;
  2002. // Do not unselect anything, as it won't work with two panels anyway
  2003. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2004. }
  2005. }
  2006. else if (RemoteFileList->Count == 1)
  2007. {
  2008. UnicodeString FileName = RemoteFileList->Strings[0];
  2009. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2010. {
  2011. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2012. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2013. Progress.SetFile(LocalFileNameOnly);
  2014. TLocalCustomCommand CustomCommand(
  2015. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2016. FileName, LocalFileName, L"");
  2017. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2018. TOnceDoneOperation OnceDoneOperation;
  2019. // Do not unselect anything, as it won't work with two panels anyway
  2020. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2021. }
  2022. }
  2023. else
  2024. {
  2025. if (LocalFileList->Count != RemoteFileList->Count)
  2026. {
  2027. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2028. }
  2029. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2030. {
  2031. UnicodeString FileName = RemoteFileList->Strings[Index];
  2032. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2033. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2034. Progress.SetFile(LocalFileNameOnly); // sic
  2035. TLocalCustomCommand CustomCommand(
  2036. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2037. FileName, LocalFileName, L"");
  2038. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2039. TOnceDoneOperation OnceDoneOperation;
  2040. // Do not unselect anything, as it won't work with two panels anyway
  2041. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2042. }
  2043. }
  2044. }
  2045. else
  2046. {
  2047. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2048. {
  2049. UnicodeString FileName = RemoteFileList->Strings[Index];
  2050. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2051. Progress.SetFile(FileNameForProgress);
  2052. TLocalCustomCommand CustomCommand(Data,
  2053. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2054. FileName, L"", L"");
  2055. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2056. TOnceDoneOperation OnceDoneOperation;
  2057. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2058. }
  2059. }
  2060. }
  2061. __finally
  2062. {
  2063. if (!NonBlocking)
  2064. {
  2065. Progress.Stop();
  2066. }
  2067. }
  2068. DebugAssert(!FAutoOperation);
  2069. if (!RemoteFiles)
  2070. {
  2071. TempDir = IncludeTrailingBackslash(TempDir);
  2072. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2073. {
  2074. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2075. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2076. // Skip directories, process only nested files.
  2077. // The check is redundant as FileAge fails for directories anyway.
  2078. !DirectoryExists(FileName))
  2079. {
  2080. UnicodeString RemoteDir =
  2081. UnixExtractFileDir(
  2082. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2083. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2084. TDateTime NewTime;
  2085. if (FileAge(FileName, NewTime) &&
  2086. (NewTime != RemoteFileTimes[Index]))
  2087. {
  2088. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2089. TemporaryFileCopyParam(CopyParam);
  2090. CopyParam.FileMask = L"";
  2091. FAutoOperation = true;
  2092. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2093. TemporaryFilesList->Add(FileName);
  2094. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2095. }
  2096. }
  2097. }
  2098. }
  2099. }
  2100. __finally
  2101. {
  2102. FAutoOperation = false;
  2103. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2104. {
  2105. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2106. }
  2107. }
  2108. }
  2109. __finally
  2110. {
  2111. if (RemoteFileList != FileList)
  2112. {
  2113. delete RemoteFileList;
  2114. }
  2115. if (LocalFileList != ALocalFileList)
  2116. {
  2117. delete LocalFileList;
  2118. }
  2119. if (RemoteFileListFull != RemoteFileList)
  2120. {
  2121. delete RemoteFileListFull;
  2122. }
  2123. }
  2124. }
  2125. //---------------------------------------------------------------------------
  2126. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2127. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2128. bool FileListCommand, UnicodeString * POutput)
  2129. {
  2130. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2131. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2132. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2133. {
  2134. UnicodeString FileName = SelectedFileList->Strings[Index];
  2135. if (DirectoryExists(FileName))
  2136. {
  2137. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2138. {
  2139. LocalFileList->Add(FileName);
  2140. }
  2141. if (FLAGSET(ACommand.Params, ccRecursive))
  2142. {
  2143. TMakeLocalFileListParams MakeFileListParam;
  2144. MakeFileListParam.FileList = LocalFileList.get();
  2145. MakeFileListParam.FileTimes = NULL;
  2146. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2147. MakeFileListParam.Recursive = true;
  2148. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2149. }
  2150. }
  2151. else
  2152. {
  2153. LocalFileList->Add(FileName);
  2154. }
  2155. }
  2156. UnicodeString RemotePath;
  2157. if (Terminal != NULL)
  2158. {
  2159. RemotePath = Terminal->CurrentDirectory;
  2160. }
  2161. if (FileListCommand)
  2162. {
  2163. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2164. TLocalCustomCommand CustomCommand(
  2165. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2166. L"", L"", FileList);
  2167. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2168. }
  2169. else
  2170. {
  2171. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2172. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2173. DebugAssert(FProgressForm != NULL);
  2174. FProgressForm->ReadOnly = true;
  2175. try
  2176. {
  2177. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2178. {
  2179. UnicodeString FileName = LocalFileList->Strings[Index];
  2180. Progress.SetFile(FileName);
  2181. TLocalCustomCommand CustomCommand(
  2182. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2183. FileName, L"", L"");
  2184. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2185. TOnceDoneOperation OnceDoneOperation;
  2186. Progress.Finish(FileName, true, OnceDoneOperation);
  2187. }
  2188. }
  2189. __finally
  2190. {
  2191. Progress.Stop();
  2192. }
  2193. }
  2194. }
  2195. //---------------------------------------------------------------------------
  2196. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2197. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2198. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2199. {
  2200. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2201. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2202. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2203. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2204. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2205. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2206. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2207. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2208. std::unique_ptr<UnicodeString> POutput;
  2209. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2210. FLAGSET(ACommand.Params, ccCopyResults))
  2211. {
  2212. POutput.reset(new UnicodeString());
  2213. }
  2214. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2215. FProgressSide = FCurrentSide;
  2216. if (!LocalCustomCommand.IsFileCommand(Command))
  2217. {
  2218. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2219. }
  2220. // remote files?
  2221. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2222. {
  2223. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2224. }
  2225. // local files
  2226. else
  2227. {
  2228. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2229. }
  2230. if (POutput.get() != NULL)
  2231. {
  2232. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2233. int P = POutput->Pos(sLineBreak);
  2234. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2235. {
  2236. POutput->SetLength(P - 1);
  2237. }
  2238. // Copy even empty output to clipboard
  2239. if (FLAGSET(ACommand.Params, ccCopyResults))
  2240. {
  2241. CopyToClipboard(*POutput);
  2242. }
  2243. // But do not show an empty message box.
  2244. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2245. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2246. !POutput->IsEmpty())
  2247. {
  2248. TClipboardHandler ClipboardHandler;
  2249. ClipboardHandler.Text = *POutput;
  2250. TMessageParams Params;
  2251. TQueryButtonAlias Aliases[1];
  2252. Aliases[0].Button = qaRetry;
  2253. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2254. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2255. Params.Aliases = Aliases;
  2256. Params.AliasesCount = LENOF(Aliases);
  2257. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2258. }
  2259. }
  2260. }
  2261. //---------------------------------------------------------------------------
  2262. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2263. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2264. {
  2265. TCustomCommandData Data;
  2266. UnicodeString Site;
  2267. UnicodeString RemotePath;
  2268. if (HasActiveTerminal())
  2269. {
  2270. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2271. Data = TCustomCommandData(SessionData.get());
  2272. Site = Terminal->SessionData->SessionKey;
  2273. RemotePath = Terminal->CurrentDirectory;
  2274. }
  2275. UnicodeString HelpKeyword = ACommand.HomePage;
  2276. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2277. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2278. {
  2279. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2280. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2281. {
  2282. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2283. }
  2284. else
  2285. {
  2286. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2287. }
  2288. if (!DoCustomCommandOptionsDialog(
  2289. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2290. {
  2291. Abort();
  2292. }
  2293. }
  2294. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2295. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2296. {
  2297. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2298. }
  2299. else
  2300. {
  2301. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2302. }
  2303. }
  2304. //---------------------------------------------------------------------------
  2305. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2306. const TCustomCommandType & Command)
  2307. {
  2308. DebugAssert(FCustomCommandLocalFileList != NULL);
  2309. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2310. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2311. TStrings * LocalFileList = new TStringList();
  2312. TStrings * RemoteFileList = new TStringList();
  2313. try
  2314. {
  2315. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2316. {
  2317. LocalFileList->Clear();
  2318. LocalFileList->AddObject(
  2319. FCustomCommandLocalFileList->Strings[Index],
  2320. FCustomCommandLocalFileList->Objects[Index]);
  2321. RemoteFileList->Clear();
  2322. RemoteFileList->AddObject(
  2323. FCustomCommandRemoteFileList->Strings[Index],
  2324. FCustomCommandRemoteFileList->Objects[Index]);
  2325. CustomCommand(RemoteFileList, Command, LocalFileList);
  2326. }
  2327. }
  2328. __finally
  2329. {
  2330. delete LocalFileList;
  2331. delete RemoteFileList;
  2332. }
  2333. }
  2334. //---------------------------------------------------------------------------
  2335. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2336. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2337. {
  2338. delete FCustomCommandLocalFileList;
  2339. delete FCustomCommandRemoteFileList;
  2340. // takeover ownership,
  2341. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2342. FCustomCommandLocalFileList = LocalFileList;
  2343. FCustomCommandRemoteFileList = RemoteFileList;
  2344. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2345. if (Button != NULL)
  2346. {
  2347. FCustomCommandMenu->Items->Clear();
  2348. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2349. MenuPopup(FCustomCommandMenu, Button);
  2350. }
  2351. else
  2352. {
  2353. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2354. }
  2355. }
  2356. //---------------------------------------------------------------------------
  2357. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2358. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2359. {
  2360. DebugAssert(FCapturedLog != NULL);
  2361. if ((OutputType == cotOutput) || (OutputType == cotError))
  2362. {
  2363. FCapturedLog->Add(AddedLine);
  2364. }
  2365. }
  2366. //---------------------------------------------------------------------------
  2367. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2368. TOperationSide Side)
  2369. {
  2370. return
  2371. (Side == osOther) &&
  2372. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2373. }
  2374. //---------------------------------------------------------------------------
  2375. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2376. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2377. {
  2378. TTBItemOptions O;
  2379. O = Item->Options;
  2380. TCustomDirView * DView = DirView(Side);
  2381. if ((DView->ItemFocused != NULL) &&
  2382. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2383. // when resolving links is disabled, default action is to enter the directory,
  2384. // no matter what DoubleClickAction is configured to
  2385. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2386. // Can only Edit files, but can Open/Copy even directories
  2387. ((DoubleClickAction != dcaEdit) ||
  2388. !DView->ItemIsDirectory(DView->ItemFocused)))
  2389. {
  2390. Item->Options = O << tboDefault;
  2391. }
  2392. else
  2393. {
  2394. Item->Options = O >> tboDefault;
  2395. }
  2396. }
  2397. //---------------------------------------------------------------------------
  2398. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2399. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2400. {
  2401. TCustomDirView * DView = DirView(Side);
  2402. TListItem * Item = DView->ItemFocused;
  2403. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2404. EnableFocusedOperation[Side])
  2405. {
  2406. TPoint ClientPoint;
  2407. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2408. TPoint(0, 0) : MousePos;
  2409. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2410. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2411. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2412. }
  2413. }
  2414. //---------------------------------------------------------------------------
  2415. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2416. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2417. {
  2418. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2419. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2420. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2421. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2422. Handled = true;
  2423. }
  2424. //---------------------------------------------------------------------------
  2425. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2426. {
  2427. }
  2428. //---------------------------------------------------------------------------
  2429. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2430. {
  2431. DebugAssert(FErrorList == NULL);
  2432. if (WinConfiguration->ContinueOnError)
  2433. {
  2434. FErrorList = new TStringList();
  2435. Configuration->Usage->Inc(L"ContinuationsOnError");
  2436. }
  2437. NonVisualDataModule->StartBusy();
  2438. }
  2439. //---------------------------------------------------------------------------
  2440. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2441. {
  2442. NonVisualDataModule->EndBusy();
  2443. if (FErrorList)
  2444. {
  2445. HandleErrorList(FErrorList);
  2446. }
  2447. }
  2448. //---------------------------------------------------------------------------
  2449. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2450. const TCopyParamType & CopyParam)
  2451. {
  2452. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2453. {
  2454. Configuration->Usage->Inc(L"FileMaskUses");
  2455. }
  2456. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2457. {
  2458. Configuration->Usage->Inc(L"OperationMaskUses");
  2459. }
  2460. }
  2461. //---------------------------------------------------------------------------
  2462. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2463. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2464. {
  2465. DebugAssert(!IsLocalBrowserMode());
  2466. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2467. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2468. TTransferOperationParam DefaultParam;
  2469. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2470. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2471. switch (Param.Queue)
  2472. {
  2473. case asOn:
  2474. CopyParam.Queue = true;
  2475. break;
  2476. case asOff:
  2477. CopyParam.Queue = false;
  2478. break;
  2479. case asAuto:
  2480. default:
  2481. // keep default
  2482. break;
  2483. }
  2484. bool Result =
  2485. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2486. if (Result)
  2487. {
  2488. DebugAssert(Terminal);
  2489. bool SelectionRestored = false;
  2490. TCustomDirView * DView = NULL;
  2491. if (HasDirView[Side])
  2492. {
  2493. DView = DirView(Side);
  2494. DView->SaveSelection();
  2495. DView->SaveSelectedNames();
  2496. }
  2497. UpdateCopyParamCounters(CopyParam);
  2498. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2499. DebugAssert(FTransferResumeList == NULL);
  2500. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2501. FMoveToQueue = false;
  2502. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2503. try
  2504. {
  2505. TStrings * PermanentFileList;
  2506. std::unique_ptr<TStrings> PermanentFileListOwner;
  2507. try
  2508. {
  2509. if (Side == osLocal)
  2510. {
  2511. PermanentFileList = FileList;
  2512. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2513. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2514. if (Operation == foMove)
  2515. {
  2516. ReloadLocalDirectory();
  2517. if (DView != NULL)
  2518. {
  2519. DView->RestoreSelection();
  2520. }
  2521. SelectionRestored = true;
  2522. }
  2523. }
  2524. else
  2525. {
  2526. // Clone the file list as it may refer to current directory files,
  2527. // which get destroyed, when the source directory is reloaded after foMove operation.
  2528. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2529. // But for a hotfix, we are not going to do such a big change.
  2530. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2531. PermanentFileList = PermanentFileListOwner.get();
  2532. try
  2533. {
  2534. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2535. }
  2536. __finally
  2537. {
  2538. if (Operation == foMove)
  2539. {
  2540. if (DView != NULL)
  2541. {
  2542. DView->RestoreSelection();
  2543. }
  2544. SelectionRestored = true;
  2545. }
  2546. ReloadLocalDirectory(Param.TargetDirectory);
  2547. }
  2548. }
  2549. }
  2550. catch (EAbort &)
  2551. {
  2552. if (FMoveToQueue)
  2553. {
  2554. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2555. DebugAssert(CopyParam.TransferSkipList == NULL);
  2556. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2557. if (TransferResumeList->Count > 0)
  2558. {
  2559. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2560. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2561. }
  2562. CopyParam.TransferSkipList = TransferResumeList.release();
  2563. // not really needed, just to keep it consistent with TransferResumeList
  2564. FTransferResumeList = NULL;
  2565. FMoveToQueue = false;
  2566. Configuration->Usage->Inc("MovesToBackground");
  2567. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2568. ClearTransferSourceSelection(Direction);
  2569. }
  2570. throw;
  2571. }
  2572. }
  2573. __finally
  2574. {
  2575. if (!SelectionRestored && (DView != NULL))
  2576. {
  2577. DView->DiscardSavedSelection();
  2578. }
  2579. FTransferResumeList = NULL;
  2580. }
  2581. }
  2582. return Result;
  2583. }
  2584. //---------------------------------------------------------------------------
  2585. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2586. TOperationSide Side, TStrings * FileList, void * Param)
  2587. {
  2588. DebugAssert(FileList->Count);
  2589. // We deliberately do not toggle alternative flag (Param), but use OR,
  2590. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2591. // shortcut of CurrentDeleteAlternativeAction
  2592. bool Alternative =
  2593. bool(Param) || UseAlternativeFunction();
  2594. bool Recycle;
  2595. if (IsSideLocalBrowser(Side))
  2596. {
  2597. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2598. }
  2599. else
  2600. {
  2601. Recycle =
  2602. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2603. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2604. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2605. }
  2606. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2607. if (!Result)
  2608. {
  2609. UnicodeString Query;
  2610. if (FileList->Count == 1)
  2611. {
  2612. if (IsSideLocalBrowser(Side))
  2613. {
  2614. Query = ExtractFileName(FileList->Strings[0]);
  2615. }
  2616. else
  2617. {
  2618. Query = UnixExtractFileName(FileList->Strings[0]);
  2619. }
  2620. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2621. }
  2622. else
  2623. {
  2624. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2625. }
  2626. TMessageParams Params(mpNeverAskAgainCheck);
  2627. Params.ImageName = L"Delete file";
  2628. unsigned int Answer =
  2629. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2630. if (Answer == qaNeverAskAgain)
  2631. {
  2632. Result = true;
  2633. if (Recycle)
  2634. {
  2635. WinConfiguration->ConfirmRecycling = false;
  2636. }
  2637. else
  2638. {
  2639. WinConfiguration->ConfirmDeleting = false;
  2640. }
  2641. }
  2642. else
  2643. {
  2644. Result = (Answer == qaOK);
  2645. }
  2646. }
  2647. if (Result)
  2648. {
  2649. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2650. }
  2651. return Result;
  2652. }
  2653. //---------------------------------------------------------------------------
  2654. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2655. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2656. {
  2657. TAutoBatch AutoBatch(this);
  2658. bool Result;
  2659. if ((Operation == foCopy) || (Operation == foMove))
  2660. {
  2661. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2662. }
  2663. else if (Operation == foRename)
  2664. {
  2665. DebugAssert(DirView(Side)->ItemFocused);
  2666. DirView(Side)->ItemFocused->EditCaption();
  2667. Result = true;
  2668. }
  2669. else if (Operation == foDelete)
  2670. {
  2671. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2672. }
  2673. else if (Operation == foSetProperties)
  2674. {
  2675. DirView(osRemote)->SaveSelectedNames();
  2676. Result = SetProperties(Side, FileList);
  2677. }
  2678. else if (Operation == foCustomCommand)
  2679. {
  2680. DebugAssert(Param);
  2681. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2682. DirView(osRemote)->SaveSelectedNames();
  2683. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2684. CustomCommand(FileList, *Command, NULL);
  2685. Result = true;
  2686. }
  2687. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2688. {
  2689. DebugAssert(!IsSideLocalBrowser(Side));
  2690. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2691. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2692. }
  2693. else if (Operation == foLock)
  2694. {
  2695. DebugAssert(!IsSideLocalBrowser(Side));
  2696. LockFiles(FileList, true);
  2697. Result = true;
  2698. }
  2699. else if (Operation == foUnlock)
  2700. {
  2701. DebugAssert(!IsSideLocalBrowser(Side));
  2702. LockFiles(FileList, false);
  2703. Result = true;
  2704. }
  2705. else
  2706. {
  2707. DebugFail();
  2708. }
  2709. return Result;
  2710. }
  2711. //---------------------------------------------------------------------------
  2712. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2713. {
  2714. if (Side == osCurrent)
  2715. {
  2716. Side = FCurrentSide;
  2717. }
  2718. return Side;
  2719. }
  2720. //---------------------------------------------------------------------------
  2721. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2722. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2723. {
  2724. Side = GetSide(Side);
  2725. bool Result;
  2726. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2727. try
  2728. {
  2729. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2730. }
  2731. __finally
  2732. {
  2733. delete FileList;
  2734. }
  2735. return Result;
  2736. }
  2737. //---------------------------------------------------------------------------
  2738. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2739. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2740. bool NoConfirmation, void * Param)
  2741. {
  2742. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2743. {
  2744. if ((Operation == foCopy) || (Operation == foMove))
  2745. {
  2746. DebugAssert(!IsLocalBrowserMode());
  2747. if (GetSide(Side) == osLocal)
  2748. {
  2749. Configuration->Usage->Inc(L"UploadsCommand");
  2750. }
  2751. else
  2752. {
  2753. Configuration->Usage->Inc(L"DownloadsCommand");
  2754. }
  2755. }
  2756. }
  2757. }
  2758. //---------------------------------------------------------------------------
  2759. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2760. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2761. {
  2762. TTransferOperationParam Param;
  2763. if ((WinConfiguration->Interface != ifCommander) ||
  2764. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2765. {
  2766. Flags &= ~cocShortCutHint;
  2767. }
  2768. TCustomDirView * DView = DirView(Side);
  2769. Param.Options =
  2770. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2771. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2772. if (FLAGSET(Flags, cocQueue))
  2773. {
  2774. Param.Queue = asOn;
  2775. }
  2776. else if (FLAGSET(Flags, cocNonQueue))
  2777. {
  2778. Param.Queue = asOff;
  2779. }
  2780. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2781. }
  2782. //---------------------------------------------------------------------------
  2783. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2784. {
  2785. int Count = ErrorList->Count;
  2786. try
  2787. {
  2788. if (ErrorList->Count)
  2789. {
  2790. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2791. if (MessageDialog(Message, qtError,
  2792. qaOK | qaCancel, HELP_NONE) == qaOK)
  2793. {
  2794. unsigned int Answer;
  2795. int Index = 0;
  2796. do
  2797. {
  2798. DebugAssert(Index >= 0 && Index < Count);
  2799. TQueryButtonAlias Aliases[2];
  2800. Aliases[0].Button = qaYes;
  2801. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2802. Aliases[1].Button = qaNo;
  2803. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2804. TMessageParams Params;
  2805. Params.Aliases = Aliases;
  2806. Params.AliasesCount = LENOF(Aliases);
  2807. int No = Index + 1;
  2808. UnicodeString Message = ErrorList->Strings[Index];
  2809. UnicodeString AMainInstructions;
  2810. UnicodeString Details;
  2811. if (ExtractMainInstructions(Message, AMainInstructions))
  2812. {
  2813. Details = Message;
  2814. Message = AMainInstructions;
  2815. }
  2816. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2817. Answer = MoreMessageDialog(
  2818. Message,
  2819. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2820. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2821. qaOK, HELP_NONE, &Params);
  2822. if (Answer == qaNo)
  2823. {
  2824. Index++;
  2825. }
  2826. if (Answer == qaYes)
  2827. {
  2828. Index--;
  2829. }
  2830. }
  2831. while (Answer != qaOK);
  2832. }
  2833. }
  2834. }
  2835. __finally
  2836. {
  2837. TStrings * List = ErrorList;
  2838. ErrorList = NULL;
  2839. for (int i = 0; i < Count; i++)
  2840. {
  2841. delete List->Objects[i];
  2842. }
  2843. delete List;
  2844. }
  2845. }
  2846. //---------------------------------------------------------------------------
  2847. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2848. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2849. {
  2850. // needed for checking filemasks, as there's no directory object
  2851. // associated with the file object
  2852. File->FullFileName = FullFileName;
  2853. TFileMasks::TParams MaskParams;
  2854. MaskParams.Size = File->Size;
  2855. MaskParams.Modification = File->Modification;
  2856. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2857. }
  2858. //---------------------------------------------------------------------------
  2859. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2860. {
  2861. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2862. Side = GetSide(Side);
  2863. TCustomDirView * DView = DirView(Side);
  2864. TListItem * FocusedItem = DView->ItemFocused;
  2865. UnicodeString Name;
  2866. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2867. {
  2868. Name = DView->ItemFileName(FocusedItem);
  2869. }
  2870. else
  2871. {
  2872. Name = LoadStr(NEW_FILE);
  2873. }
  2874. UnicodeString Names = Name;
  2875. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2876. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2877. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2878. {
  2879. while (!Names.IsEmpty())
  2880. {
  2881. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2882. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2883. UnicodeString TargetFileName;
  2884. UnicodeString LocalFileName;
  2885. UnicodeString RootTempDir;
  2886. UnicodeString TempDir;
  2887. UnicodeString RemoteDirectory;
  2888. bool ExistingFile = false;
  2889. if (!IsSideLocalBrowser(Side))
  2890. {
  2891. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2892. TRemoteFile * File = NULL;
  2893. if (Terminal->FileExists(Name, &File) &&
  2894. !File->IsDirectory)
  2895. {
  2896. try
  2897. {
  2898. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2899. ExistingFile = true;
  2900. }
  2901. __finally
  2902. {
  2903. delete File;
  2904. }
  2905. }
  2906. if (!ExistingFile)
  2907. {
  2908. RemoteDirectory = UnixExtractFilePath(Name);
  2909. TemporaryDirectoryForRemoteFiles(
  2910. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2911. TargetFileName = UnixExtractFileName(Name);
  2912. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2913. LocalFileName = TempDir +
  2914. // We probably do not want to trim the VMS version here
  2915. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2916. }
  2917. }
  2918. else
  2919. {
  2920. if (ExtractFilePath(Name).IsEmpty())
  2921. {
  2922. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2923. }
  2924. else
  2925. {
  2926. LocalFileName = ExpandFileName(Name);
  2927. }
  2928. TargetFileName = ExtractFileName(Name);
  2929. }
  2930. if (!ExistingFile)
  2931. {
  2932. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2933. if (NewFile)
  2934. {
  2935. int File = FileCreate(ApiPath(LocalFileName));
  2936. if (File < 0)
  2937. {
  2938. if (!RootTempDir.IsEmpty())
  2939. {
  2940. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2941. }
  2942. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2943. }
  2944. else
  2945. {
  2946. FileClose(File);
  2947. }
  2948. }
  2949. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2950. const TEditorData * ExternalEditor = NULL;
  2951. TFileMasks::TParams MaskParams; // size not known
  2952. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2953. false, MaskParams);
  2954. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2955. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2956. }
  2957. }
  2958. }
  2959. }
  2960. //---------------------------------------------------------------------------
  2961. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2962. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2963. {
  2964. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2965. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2966. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2967. return
  2968. ((ExecuteFileBy == efInternalEditor)) ||
  2969. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2973. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2974. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2975. UnicodeString RemoteDirectory, bool NewFile)
  2976. {
  2977. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2978. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2979. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2980. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2981. Side = GetSide(Side);
  2982. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2983. if (!IsSideLocalBrowser(Side))
  2984. {
  2985. Data->Terminal = Terminal;
  2986. Data->Queue = Queue;
  2987. Data->SessionData = CloneCurrentSessionData();
  2988. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2989. Data->RemoteDirectory = RemoteDirectory;
  2990. Data->SessionName = Terminal->SessionData->SessionName;
  2991. Data->LocalRootDirectory = LocalRootDirectory;
  2992. Data->OriginalFileName = OriginalFileName;
  2993. Data->Command = L""; // will be changed later for external editor
  2994. }
  2995. if (ExecuteFileBy == efInternalEditor)
  2996. {
  2997. if (!IsSideLocalBrowser(Side))
  2998. {
  2999. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3000. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3001. TForm * Editor;
  3002. try
  3003. {
  3004. TNotifyEvent OnFileChanged = NULL;
  3005. TNotifyEvent OnSaveAll = NULL;
  3006. TAnyModifiedEvent OnAnyModified = NULL;
  3007. // Edited files are uploaded in background queue, what we do not support with encrypted files
  3008. if (Terminal->IsCapable[fcBackgroundTransfers])
  3009. {
  3010. OnFileChanged = FEditorManager->FileChanged;
  3011. OnSaveAll = SaveAllInternalEditors;
  3012. OnAnyModified = AnyInternalEditorModified;
  3013. }
  3014. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  3015. FEditorManager->FileReload, FEditorManager->FileClosed,
  3016. OnSaveAll, OnAnyModified,
  3017. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3018. }
  3019. catch(...)
  3020. {
  3021. if (!LocalRootDirectory.IsEmpty())
  3022. {
  3023. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3024. }
  3025. throw;
  3026. }
  3027. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3028. }
  3029. else
  3030. {
  3031. DebugAssert(!FStandaloneEditing);
  3032. TForm * Editor =
  3033. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3034. SaveAllInternalEditors, AnyInternalEditorModified,
  3035. L"", false, SessionColor, -1, NewFile);
  3036. FLocalEditors->Add(Editor);
  3037. }
  3038. }
  3039. else
  3040. {
  3041. HANDLE Process;
  3042. if (ExecuteFileBy == efExternalEditor)
  3043. {
  3044. UnicodeString Program, Params, Dir;
  3045. Data->Command = ExternalEditor->ExternalEditor;
  3046. ReformatFileNameCommand(Data->Command);
  3047. SplitCommand(Data->Command, Program, Params, Dir);
  3048. Params = ExpandFileNameCommand(Params, FileName);
  3049. Program = ExpandEnvironmentVariables(Program);
  3050. if (!ExecuteShell(Program, Params, Process))
  3051. {
  3052. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3053. }
  3054. }
  3055. else
  3056. {
  3057. DebugAssert(!IsSideLocalBrowser(Side));
  3058. if (!ExecuteShell(FileName, L"", Process))
  3059. {
  3060. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3061. }
  3062. }
  3063. if (IsSideLocalBrowser(Side) ||
  3064. ((ExecuteFileBy == efShell) &&
  3065. !WinConfiguration->Editor.SDIShellEditor) ||
  3066. ((ExecuteFileBy == efExternalEditor) &&
  3067. !ExternalEditor->SDIExternalEditor))
  3068. {
  3069. // no need for handle
  3070. if (Process != NULL)
  3071. {
  3072. DebugCheck(CloseHandle(Process));
  3073. }
  3074. Process = INVALID_HANDLE_VALUE;
  3075. }
  3076. else
  3077. {
  3078. if (Process == NULL)
  3079. {
  3080. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3081. }
  3082. }
  3083. if (!IsSideLocalBrowser(Side))
  3084. {
  3085. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3086. }
  3087. }
  3088. }
  3089. //---------------------------------------------------------------------------
  3090. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3091. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3092. void * /*Arg*/)
  3093. {
  3094. if (Token != NULL)
  3095. {
  3096. EditorFormFileSave(static_cast<TForm *>(Token));
  3097. }
  3098. }
  3099. //---------------------------------------------------------------------------
  3100. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3101. {
  3102. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3103. {
  3104. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3105. }
  3106. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3107. }
  3108. //---------------------------------------------------------------------------
  3109. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3110. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3111. void * Arg)
  3112. {
  3113. if ((Token != NULL) &&
  3114. IsEditorFormModified(static_cast<TForm *>(Token)))
  3115. {
  3116. *static_cast<bool *>(Arg) = true;
  3117. }
  3118. }
  3119. //---------------------------------------------------------------------------
  3120. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3121. TObject * /*Sender*/, bool & Modified)
  3122. {
  3123. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3124. {
  3125. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3126. {
  3127. Modified = true;
  3128. }
  3129. }
  3130. if (!Modified)
  3131. {
  3132. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3133. }
  3134. }
  3135. //---------------------------------------------------------------------------
  3136. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3137. {
  3138. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3139. }
  3140. //---------------------------------------------------------------------------
  3141. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3142. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3143. UnicodeString & Result, UnicodeString & RootDirectory)
  3144. {
  3145. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3146. {
  3147. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3148. Result = RootDirectory;
  3149. }
  3150. else
  3151. {
  3152. RootDirectory = L"";
  3153. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3154. Result = IncludeTrailingBackslash(Result);
  3155. }
  3156. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3157. {
  3158. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3159. }
  3160. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3161. {
  3162. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3163. {
  3164. RemoteDirectory.Delete(1, 1);
  3165. }
  3166. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3167. }
  3168. if (!ForceDirectories(ApiPath(Result)))
  3169. {
  3170. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3171. }
  3172. }
  3173. //---------------------------------------------------------------------------
  3174. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3175. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3176. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3177. {
  3178. DebugAssert(!IsLocalBrowserMode());
  3179. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3180. if (ForceText)
  3181. {
  3182. CopyParam.TransferMode = tmAscii;
  3183. }
  3184. TemporaryFileCopyParam(CopyParam);
  3185. if (AllFiles)
  3186. {
  3187. CopyParam.IncludeFileMask = TFileMasks();
  3188. }
  3189. if (TempDir.IsEmpty())
  3190. {
  3191. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3192. }
  3193. DebugAssert(!FAutoOperation);
  3194. FAutoOperation = AutoOperation;
  3195. Terminal->ExceptionOnFail = true;
  3196. try
  3197. {
  3198. try
  3199. {
  3200. // turn off confirmations, as for MDI editors we may possibly download
  3201. // the same file over
  3202. Terminal->CopyToLocal(
  3203. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3204. if (GetTargetNames)
  3205. {
  3206. for (int i = 0; i < FileList->Count; i++)
  3207. {
  3208. FileList->Strings[i] =
  3209. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3210. }
  3211. }
  3212. }
  3213. catch(...)
  3214. {
  3215. if (!RootTempDir.IsEmpty())
  3216. {
  3217. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3218. }
  3219. throw;
  3220. }
  3221. }
  3222. __finally
  3223. {
  3224. FAutoOperation = false;
  3225. Terminal->ExceptionOnFail = false;
  3226. }
  3227. }
  3228. //---------------------------------------------------------------------------
  3229. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3230. {
  3231. // Do not waste time checking for default editor list the next time,
  3232. // or testing if default editor is not notepad.
  3233. bool TryNextTime = false;
  3234. UnicodeString UsageState;
  3235. if (!WinConfiguration->EditorList->IsDefaultList())
  3236. {
  3237. UsageState = "H";
  3238. }
  3239. else
  3240. {
  3241. UnicodeString Executable;
  3242. UnicodeString ExecutableDescription;
  3243. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3244. {
  3245. UnicodeString Message =
  3246. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3247. unsigned int Answer =
  3248. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3249. if (Answer != qaOK)
  3250. {
  3251. UsageState = "R";
  3252. }
  3253. else
  3254. {
  3255. UsageState = "A";
  3256. TEditorData EditorData;
  3257. EditorData.Editor = edExternal;
  3258. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3259. EditorData.ExternalEditorOptionsAutodetect();
  3260. TEditorList EditorList;
  3261. EditorList = *WinConfiguration->EditorList;
  3262. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3263. WinConfiguration->EditorList = &EditorList;
  3264. }
  3265. }
  3266. }
  3267. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3268. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3269. }
  3270. //---------------------------------------------------------------------------
  3271. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3272. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3273. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3274. {
  3275. if (ExecuteFileBy == efDefaultEditor)
  3276. {
  3277. if (!WinConfiguration->OfferedEditorAutoConfig)
  3278. {
  3279. EditorAutoConfig();
  3280. }
  3281. const TEditorPreferences * Editor =
  3282. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3283. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3284. {
  3285. ExecuteFileBy = efInternalEditor;
  3286. ExternalEditor = NULL;
  3287. }
  3288. else if (Editor->Data->Editor == edOpen)
  3289. {
  3290. ExecuteFileBy = efShell;
  3291. ExternalEditor = NULL;
  3292. }
  3293. else
  3294. {
  3295. ExecuteFileBy = efExternalEditor;
  3296. ExternalEditor = Editor->Data;
  3297. }
  3298. }
  3299. }
  3300. //---------------------------------------------------------------------------
  3301. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3302. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3303. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3304. {
  3305. UnicodeString OriginalFileName;
  3306. UnicodeString LocalRootDirectory;
  3307. UnicodeString RemoteDirectory;
  3308. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3309. IsSideLocalBrowser(Side), MaskParams);
  3310. UnicodeString Counter;
  3311. UnicodeString LocalFileName;
  3312. bool Handled = false;
  3313. if (!IsSideLocalBrowser(Side))
  3314. {
  3315. // We need to trim VMS version here, so that we use name without version
  3316. // when uploading back to create a new version of the file
  3317. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3318. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3319. TObject * Token = NULL;
  3320. UnicodeString LocalDirectory;
  3321. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3322. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3323. LocalDirectory))
  3324. {
  3325. if (Token != NULL)
  3326. {
  3327. TForm * Form = dynamic_cast<TForm *>(Token);
  3328. if (Form->WindowState == wsMinimized)
  3329. {
  3330. ShowWindow(Form->Handle, SW_RESTORE);
  3331. }
  3332. else
  3333. {
  3334. Form->SetFocus();
  3335. }
  3336. Handled = true;
  3337. }
  3338. else
  3339. {
  3340. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3341. }
  3342. }
  3343. if (!Handled)
  3344. {
  3345. TStringList * FileList1 = new TStringList();
  3346. try
  3347. {
  3348. FileList1->AddObject(FullFileName, Object);
  3349. TemporarilyDownloadFiles(FileList1,
  3350. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3351. LocalRootDirectory, LocalDirectory, true, true, true);
  3352. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3353. }
  3354. __finally
  3355. {
  3356. delete FileList1;
  3357. }
  3358. switch (ExecuteFileBy)
  3359. {
  3360. case efShell:
  3361. Counter = "RemoteFilesExecuted";
  3362. break;
  3363. case efInternalEditor:
  3364. Counter = "RemoteFilesOpenedInInternalEditor";
  3365. break;
  3366. case efExternalEditor:
  3367. Counter = "RemoteFilesOpenedInExternalEditor";
  3368. break;
  3369. default:
  3370. DebugFail();
  3371. }
  3372. }
  3373. }
  3374. else
  3375. {
  3376. LocalFileName = FullFileName;
  3377. OriginalFileName = ExtractFileName(FullFileName);
  3378. switch (ExecuteFileBy)
  3379. {
  3380. case efShell:
  3381. Counter = "LocalFilesExecuted";
  3382. break;
  3383. case efInternalEditor:
  3384. Counter = "LocalFilesOpenedInInternalEditor";
  3385. break;
  3386. case efExternalEditor:
  3387. Counter = "LocalFilesOpenedInExternalEditor";
  3388. break;
  3389. default:
  3390. DebugFail();
  3391. }
  3392. }
  3393. if (!Handled)
  3394. {
  3395. Configuration->Usage->Inc(Counter);
  3396. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3397. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3398. }
  3399. }
  3400. //---------------------------------------------------------------------------
  3401. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3402. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3403. bool AllSelected, bool OnFocused)
  3404. {
  3405. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3406. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3407. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3408. Side = GetSide(Side);
  3409. TCustomDirView * DView = DirView(Side);
  3410. TStrings * FileList =
  3411. AllSelected ?
  3412. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3413. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3414. try
  3415. {
  3416. DebugAssert(AllSelected || (FileList->Count == 1));
  3417. for (int i = 0; i < FileList->Count; i++)
  3418. {
  3419. UnicodeString ListFileName = FileList->Strings[i];
  3420. UnicodeString FileNameOnly =
  3421. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3422. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3423. if (!DView->ItemIsDirectory(Item))
  3424. {
  3425. UnicodeString FullFileName;
  3426. if (!IsSideLocalBrowser(Side))
  3427. {
  3428. FullFileName = DirView(Side)->Path + ListFileName;
  3429. }
  3430. else
  3431. {
  3432. FullFileName = ListFileName;
  3433. }
  3434. TFileMasks::TParams MaskParams;
  3435. MaskParams.Size = DView->ItemFileSize(Item);
  3436. TDateTimePrecision Precision;
  3437. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3438. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3439. FileList->Objects[i], MaskParams);
  3440. }
  3441. }
  3442. }
  3443. __finally
  3444. {
  3445. delete FileList;
  3446. }
  3447. }
  3448. //---------------------------------------------------------------------------
  3449. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3450. {
  3451. CopyParam.FileNameCase = ncNoChange;
  3452. CopyParam.PreserveRights = false;
  3453. CopyParam.PreserveReadOnly = false;
  3454. CopyParam.ReplaceInvalidChars = true;
  3455. CopyParam.IncludeFileMask = TFileMasks();
  3456. CopyParam.NewerOnly = false;
  3457. CopyParam.FileMask = L"";
  3458. }
  3459. //---------------------------------------------------------------------------
  3460. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3461. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3462. {
  3463. TTerminalManager * Manager = TTerminalManager::Instance();
  3464. if (!IsActiveTerminal(Data->Terminal))
  3465. {
  3466. if (!NonVisualDataModule->Busy)
  3467. {
  3468. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3469. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3470. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3471. if (Data->Terminal == NULL)
  3472. {
  3473. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3474. if (SameSiteTerminal != NULL)
  3475. {
  3476. UnicodeString Message =
  3477. FMTLOAD(EDIT_SESSION_REATTACH,
  3478. (FileNameOnly, Data->SessionName, FileNameOnly));
  3479. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3480. {
  3481. Data->Terminal = Terminal;
  3482. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3483. Data->SessionName = Terminal->SessionData->SessionName;
  3484. // We might also overwrite session data
  3485. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3486. }
  3487. else
  3488. {
  3489. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3490. Abort();
  3491. }
  3492. }
  3493. }
  3494. // foreground session should reconnect itself
  3495. else if (Terminal != Data->Terminal)
  3496. {
  3497. UnicodeString Message =
  3498. MainInstructions(
  3499. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3500. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3501. {
  3502. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3503. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3504. }
  3505. else
  3506. {
  3507. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3508. Abort();
  3509. }
  3510. }
  3511. }
  3512. if (!IsActiveTerminal(Data->Terminal))
  3513. {
  3514. Configuration->Usage->Inc(L"EditInactiveSession");
  3515. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3516. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3517. (ExtractFileName(FileName), Data->SessionName)));
  3518. }
  3519. }
  3520. TStrings * FileList = new TStringList();
  3521. try
  3522. {
  3523. FileList->Add(FileName);
  3524. // consider using the same settings (preset) as when the file was downloaded
  3525. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3526. TemporaryFileCopyParam(CopyParam);
  3527. if (Data->ForceText)
  3528. {
  3529. CopyParam.TransferMode = tmAscii;
  3530. }
  3531. // so i do not need to worry if masking algorithm works in all cases
  3532. // ("" means "copy file name", no masking is actually done)
  3533. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3534. {
  3535. CopyParam.FileMask = L"";
  3536. }
  3537. else
  3538. {
  3539. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3540. }
  3541. DebugAssert(Data->Queue != NULL);
  3542. int Params = cpNoConfirmation | cpTemporary;
  3543. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3544. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3545. QueueItem->CompleteEvent = UploadCompleteEvent;
  3546. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3547. }
  3548. __finally
  3549. {
  3550. delete FileList;
  3551. }
  3552. }
  3553. //---------------------------------------------------------------------------
  3554. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3555. const UnicodeString FileName, const TEditedFileData * Data)
  3556. {
  3557. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3558. DebugAssert(!NonVisualDataModule->Busy);
  3559. if (!IsActiveTerminal(Data->Terminal))
  3560. {
  3561. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3562. (ExtractFileName(FileName), Data->SessionName)));
  3563. }
  3564. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3565. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3566. NonVisualDataModule->StartBusy();
  3567. try
  3568. {
  3569. std::unique_ptr<TRemoteFile> File;
  3570. UnicodeString RemoteFileName =
  3571. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3572. Terminal->ExceptionOnFail = true;
  3573. try
  3574. {
  3575. TRemoteFile * AFile = NULL;
  3576. Terminal->ReadFile(RemoteFileName, AFile);
  3577. File.reset(AFile);
  3578. if (!File->HaveFullFileName)
  3579. {
  3580. File->FullFileName = RemoteFileName;
  3581. }
  3582. }
  3583. __finally
  3584. {
  3585. Terminal->ExceptionOnFail = false;
  3586. }
  3587. std::unique_ptr<TStrings> FileList(new TStringList());
  3588. FileList->AddObject(RemoteFileName, File.get());
  3589. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3590. UnicodeString TempDir = ExtractFilePath(FileName);
  3591. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3592. TempDir, true, true, true);
  3593. // sanity check, the target file name should be still the same
  3594. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3595. }
  3596. __finally
  3597. {
  3598. NonVisualDataModule->EndBusy();
  3599. // it actually may not exist anymore...
  3600. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3601. }
  3602. }
  3603. //---------------------------------------------------------------------------
  3604. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3605. const TEditedFileData * Data, bool & KeepOpen)
  3606. {
  3607. // Command is set for external editors only (not for "shell" open).
  3608. if (!Data->Command.IsEmpty())
  3609. {
  3610. bool AnyFound = false;
  3611. bool AnyMDI = false;
  3612. bool AnyNonMDI = false;
  3613. bool AnyDetect = false;
  3614. TEditorList * EditorList = new TEditorList();
  3615. try
  3616. {
  3617. *EditorList = *WinConfiguration->EditorList;
  3618. for (int i = 0; i < EditorList->Count; i++)
  3619. {
  3620. const TEditorPreferences * Editor = EditorList->Editors[i];
  3621. if ((Editor->Data->Editor == edExternal) &&
  3622. (Editor->Data->ExternalEditor == Data->Command))
  3623. {
  3624. AnyFound = true;
  3625. if (Editor->Data->SDIExternalEditor)
  3626. {
  3627. AnyNonMDI = true;
  3628. if (Editor->Data->DetectMDIExternalEditor)
  3629. {
  3630. AnyDetect = true;
  3631. }
  3632. }
  3633. else
  3634. {
  3635. AnyMDI = true;
  3636. }
  3637. }
  3638. }
  3639. bool EnableMDI = false;
  3640. bool DisableDetect = false;
  3641. if (AnyMDI)
  3642. {
  3643. KeepOpen = true;
  3644. if (AnyNonMDI)
  3645. {
  3646. // there is at least one instance of the editor with MDI support enabled,
  3647. // and one with disabled, enable it for all instances
  3648. EnableMDI = true;
  3649. }
  3650. }
  3651. else if (AnyFound && !AnyDetect)
  3652. {
  3653. // at least once instance found but all have MDI autodetection disabled
  3654. // => close the file (default action)
  3655. }
  3656. else
  3657. {
  3658. // no instance of the editor has MDI support enabled
  3659. TMessageParams Params;
  3660. if (AnyFound)
  3661. {
  3662. // there is at least one instance of the editor
  3663. Params.Params |= mpNeverAskAgainCheck;
  3664. }
  3665. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3666. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3667. switch (Answer)
  3668. {
  3669. case qaNeverAskAgain:
  3670. DisableDetect = true;
  3671. break;
  3672. case qaNo:
  3673. EnableMDI = true;
  3674. KeepOpen = true;
  3675. break;
  3676. }
  3677. }
  3678. if (AnyFound && (EnableMDI || DisableDetect))
  3679. {
  3680. bool Changed = false;
  3681. for (int i = 0; i < EditorList->Count; i++)
  3682. {
  3683. const TEditorPreferences * Editor = EditorList->Editors[i];
  3684. if ((Editor->Data->Editor == edExternal) &&
  3685. (Editor->Data->ExternalEditor == Data->Command) &&
  3686. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3687. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3688. {
  3689. Changed = true;
  3690. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3691. if (EnableMDI)
  3692. {
  3693. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3694. }
  3695. if (DisableDetect)
  3696. {
  3697. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3698. }
  3699. EditorList->Change(i, UpdatedEditor);
  3700. }
  3701. }
  3702. if (Changed)
  3703. {
  3704. WinConfiguration->EditorList = EditorList;
  3705. }
  3706. }
  3707. }
  3708. __finally
  3709. {
  3710. delete EditorList;
  3711. }
  3712. }
  3713. else
  3714. {
  3715. // "open" case
  3716. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3717. qaOK, HELP_APP_EARLY_CLOSED);
  3718. }
  3719. }
  3720. //---------------------------------------------------------------------------
  3721. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3722. {
  3723. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3724. }
  3725. //---------------------------------------------------------------------------
  3726. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3727. {
  3728. SideEnter(osRemote);
  3729. }
  3730. //---------------------------------------------------------------------------
  3731. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3732. {
  3733. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3734. SideEnter(osRemote);
  3735. }
  3736. //---------------------------------------------------------------------------
  3737. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3738. {
  3739. FCurrentSide = Side;
  3740. if (Visible)
  3741. {
  3742. UpdateControls();
  3743. }
  3744. }
  3745. //---------------------------------------------------------------------------
  3746. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3747. TStrings * FileList, bool Alternative)
  3748. {
  3749. TCustomDirView * DView = DirView(Side);
  3750. DView->SaveSelection();
  3751. DView->SaveSelectedNames();
  3752. DebugAssert(!FAlternativeDelete);
  3753. FAlternativeDelete = Alternative;
  3754. try
  3755. {
  3756. if (!IsSideLocalBrowser(Side))
  3757. {
  3758. DebugAssert(Terminal != NULL);
  3759. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3760. }
  3761. else
  3762. {
  3763. if (IsLocalBrowserMode())
  3764. {
  3765. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3766. }
  3767. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3768. FProgressSide = FCurrentSide;
  3769. try
  3770. {
  3771. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3772. }
  3773. __finally
  3774. {
  3775. ReloadLocalDirectory();
  3776. }
  3777. }
  3778. FAlternativeDelete = false;
  3779. }
  3780. catch(...)
  3781. {
  3782. FAlternativeDelete = false;
  3783. DView->DiscardSavedSelection();
  3784. throw;
  3785. }
  3786. DView->RestoreSelection();
  3787. }
  3788. //---------------------------------------------------------------------------
  3789. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3790. {
  3791. DebugAssert(Terminal);
  3792. DebugAssert(!IsLocalBrowserMode());
  3793. RemoteDirView->SaveSelection();
  3794. RemoteDirView->SaveSelectedNames();
  3795. try
  3796. {
  3797. if (Lock)
  3798. {
  3799. Terminal->LockFiles(FileList);
  3800. }
  3801. else
  3802. {
  3803. Terminal->UnlockFiles(FileList);
  3804. }
  3805. }
  3806. catch(...)
  3807. {
  3808. RemoteDirView->DiscardSavedSelection();
  3809. throw;
  3810. }
  3811. RemoteDirView->RestoreSelection();
  3812. }
  3813. //---------------------------------------------------------------------------
  3814. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3815. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3816. bool NoConfirmation, bool Move)
  3817. {
  3818. DebugAssert(Terminal != NULL);
  3819. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3820. DebugAssert(!IsLocalBrowserMode());
  3821. if (Session == NULL)
  3822. {
  3823. Session = Terminal;
  3824. }
  3825. if (Session == Terminal)
  3826. {
  3827. if (RemoteDriveView->DropTarget != NULL)
  3828. {
  3829. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3830. }
  3831. else if (RemoteDirView->DropTarget != NULL)
  3832. {
  3833. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3834. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3835. }
  3836. else
  3837. {
  3838. Target = RemoteDirView->Path;
  3839. }
  3840. }
  3841. else
  3842. {
  3843. Target = GetSessionPath(Session, osRemote);
  3844. }
  3845. Target = UnixIncludeTrailingBackslash(Target);
  3846. if (FileList->Count == 1)
  3847. {
  3848. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3849. }
  3850. else
  3851. {
  3852. FileMask = AnyMask;
  3853. }
  3854. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3855. bool Result = true;
  3856. if (!NoConfirmation)
  3857. {
  3858. bool Multi = (FileList->Count > 1);
  3859. if (Move)
  3860. {
  3861. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3862. }
  3863. else
  3864. {
  3865. std::unique_ptr<TStrings> Sessions(new TStringList);
  3866. std::unique_ptr<TStrings> Directories(new TStringList);
  3867. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3868. for (int Index = 0; Index < SessionList->Count; Index++)
  3869. {
  3870. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3871. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3872. {
  3873. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3874. Directories->Add(GetSessionPath(ASession, osRemote));
  3875. }
  3876. }
  3877. TDirectRemoteCopy AllowDirectCopy;
  3878. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3879. {
  3880. DebugAssert(DirectCopy);
  3881. AllowDirectCopy = drcAllow;
  3882. }
  3883. else if (Terminal->IsCapable[fcSecondaryShell])
  3884. {
  3885. DebugAssert(DirectCopy);
  3886. AllowDirectCopy = drcConfirmCommandSession;
  3887. }
  3888. else
  3889. {
  3890. DebugAssert(!DirectCopy);
  3891. AllowDirectCopy = drcDisallow;
  3892. }
  3893. void * ASession = Session;
  3894. Result = DoRemoteCopyDialog(
  3895. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3896. Session = static_cast<TManagedTerminal *>(ASession);
  3897. }
  3898. }
  3899. return Result;
  3900. }
  3901. //---------------------------------------------------------------------------
  3902. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3903. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3904. {
  3905. DebugAssert(!IsLocalBrowserMode());
  3906. bool DirectCopy;
  3907. UnicodeString Target, FileMask;
  3908. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3909. if (Result)
  3910. {
  3911. if (!Move && !DirectCopy)
  3912. {
  3913. Configuration->Usage->Inc("RemoteCopyTemp");
  3914. UnicodeString RootTempDir;
  3915. UnicodeString TempDir;
  3916. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3917. TStrings * TemporaryFilesList = new TStringList();
  3918. try
  3919. {
  3920. TMakeLocalFileListParams MakeFileListParam;
  3921. MakeFileListParam.FileList = TemporaryFilesList;
  3922. MakeFileListParam.FileTimes = NULL;
  3923. MakeFileListParam.IncludeDirs = true;
  3924. MakeFileListParam.Recursive = false;
  3925. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  3926. TTerminalManager::Instance()->ActiveSession = Session;
  3927. if (TemporaryFilesList->Count > 0)
  3928. {
  3929. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3930. CopyParam.FileMask = FileMask;
  3931. DebugAssert(!FAutoOperation);
  3932. FAutoOperation = true;
  3933. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3934. }
  3935. }
  3936. __finally
  3937. {
  3938. delete TemporaryFilesList;
  3939. FAutoOperation = false;
  3940. if (!RootTempDir.IsEmpty())
  3941. {
  3942. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3943. }
  3944. }
  3945. }
  3946. else
  3947. {
  3948. RemoteDirView->SaveSelection();
  3949. RemoteDirView->SaveSelectedNames();
  3950. try
  3951. {
  3952. if (Move)
  3953. {
  3954. Configuration->Usage->Inc("RemoteMove");
  3955. Terminal->MoveFiles(FileList, Target, FileMask);
  3956. }
  3957. else
  3958. {
  3959. Configuration->Usage->Inc("RemoteCopyDirect");
  3960. DebugAssert(DirectCopy);
  3961. DebugAssert(Session == Terminal);
  3962. if (Terminal->IsCapable[fcRemoteCopy] ||
  3963. Terminal->CommandSessionOpened ||
  3964. CommandSessionFallback())
  3965. {
  3966. Terminal->CopyFiles(FileList, Target, FileMask);
  3967. }
  3968. }
  3969. }
  3970. catch(...)
  3971. {
  3972. RemoteDirView->DiscardSavedSelection();
  3973. throw;
  3974. }
  3975. RemoteDirView->RestoreSelection();
  3976. }
  3977. }
  3978. return Result;
  3979. }
  3980. //---------------------------------------------------------------------------
  3981. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3982. const UnicodeString & Path, TRemoteProperties & Properties)
  3983. {
  3984. DebugAssert(!IsLocalBrowserMode());
  3985. Properties.Valid = Properties.Valid << vpEncrypt;
  3986. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3987. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3988. }
  3989. //---------------------------------------------------------------------------
  3990. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3991. {
  3992. Side = GetSide(Side);
  3993. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3994. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3995. UnicodeString Name = LoadStr(NEW_FOLDER);
  3996. int AllowedChanges =
  3997. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3998. bool SaveSettings = false;
  3999. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4000. {
  4001. TWindowLock Lock(this);
  4002. if (!IsSideLocalBrowser(Side))
  4003. {
  4004. if (SaveSettings)
  4005. {
  4006. GUIConfiguration->NewDirectoryProperties = Properties;
  4007. }
  4008. CreateRemoteDirectory(Name, Properties);
  4009. }
  4010. else
  4011. {
  4012. DirView(Side)->CreateDirectory(Name);
  4013. }
  4014. }
  4015. }
  4016. //---------------------------------------------------------------------------
  4017. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4018. {
  4019. TWindowLock Lock(this);
  4020. DirView(Side)->ExecuteHomeDirectory();
  4021. }
  4022. //---------------------------------------------------------------------------
  4023. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4024. {
  4025. DoOpenDirectoryDialog(odBrowse, Side);
  4026. }
  4027. //---------------------------------------------------------------------------
  4028. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4029. {
  4030. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4031. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4032. bool Result = !Path.IsEmpty();
  4033. if (Result)
  4034. {
  4035. // While we might get here when the session is closed (from location profiles),
  4036. // it's not a problem as the Path setter is noop then
  4037. DirView(Side)->Path = Path;
  4038. }
  4039. return Result;
  4040. }
  4041. //---------------------------------------------------------------------------
  4042. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4043. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4044. bool & Close)
  4045. {
  4046. // terminal can be already closed (e.g. dropped connection)
  4047. if (Terminal != NULL)
  4048. {
  4049. try
  4050. {
  4051. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  4052. }
  4053. catch(...)
  4054. {
  4055. if (!Terminal->Active)
  4056. {
  4057. Close = true;
  4058. }
  4059. throw;
  4060. }
  4061. }
  4062. }
  4063. //---------------------------------------------------------------------------
  4064. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4065. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4066. bool & Close)
  4067. {
  4068. // terminal can be already closed (e.g. dropped connection)
  4069. if (Terminal != NULL)
  4070. {
  4071. Configuration->Usage->Inc(L"ChecksumCalculated");
  4072. try
  4073. {
  4074. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4075. }
  4076. catch(...)
  4077. {
  4078. if (!Terminal->Active)
  4079. {
  4080. Close = true;
  4081. }
  4082. throw;
  4083. }
  4084. }
  4085. }
  4086. //---------------------------------------------------------------------------
  4087. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4088. {
  4089. bool Result;
  4090. if (!IsSideLocalBrowser(Side))
  4091. {
  4092. TRemoteTokenList * GroupList = NULL;
  4093. TRemoteTokenList * UserList = NULL;
  4094. try
  4095. {
  4096. TRemoteProperties CurrentProperties;
  4097. if (Terminal->LoadFilesProperties(FileList))
  4098. {
  4099. RemoteDirView->Invalidate();
  4100. }
  4101. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4102. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4103. if (CapableGroupChanging || CapableOwnerChanging)
  4104. {
  4105. if (CapableGroupChanging)
  4106. {
  4107. GroupList = Terminal->Groups->Duplicate();
  4108. }
  4109. if (CapableOwnerChanging)
  4110. {
  4111. UserList = Terminal->Users->Duplicate();
  4112. }
  4113. TRemoteDirectory * Files = Terminal->Files;
  4114. int Count = Files->Count;
  4115. if (Count > 100)
  4116. {
  4117. Count = 100;
  4118. }
  4119. for (int Index = 0; Index < Count; Index++)
  4120. {
  4121. TRemoteFile * File = Files->Files[Index];
  4122. if (CapableGroupChanging)
  4123. {
  4124. GroupList->AddUnique(File->Group);
  4125. }
  4126. if (CapableOwnerChanging)
  4127. {
  4128. UserList->AddUnique(File->Owner);
  4129. }
  4130. }
  4131. // if we haven't collected tokens for all files in current directory,
  4132. // make sure we collect them at least for all selected files.
  4133. // (note that so far the files in FileList has to be from current direcotry)
  4134. if (Count < Files->Count)
  4135. {
  4136. for (int Index = 0; Index < FileList->Count; Index++)
  4137. {
  4138. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4139. if (CapableGroupChanging)
  4140. {
  4141. GroupList->AddUnique(File->Group);
  4142. }
  4143. if (CapableOwnerChanging)
  4144. {
  4145. UserList->AddUnique(File->Owner);
  4146. }
  4147. }
  4148. }
  4149. }
  4150. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4151. int Flags = 0;
  4152. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4153. if (CapableOwnerChanging) Flags |= cpOwner;
  4154. if (CapableGroupChanging) Flags |= cpGroup;
  4155. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4156. if (Terminal->IsCapable[fcCalculatingChecksum])
  4157. {
  4158. CalculateChecksumEvent = CalculateChecksum;
  4159. }
  4160. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4161. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4162. TRemoteProperties NewProperties = CurrentProperties;
  4163. Result =
  4164. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4165. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4166. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4167. CalculateSize, CalculateChecksumEvent);
  4168. if (Result)
  4169. {
  4170. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4171. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4172. }
  4173. }
  4174. __finally
  4175. {
  4176. delete GroupList;
  4177. delete UserList;
  4178. }
  4179. }
  4180. else
  4181. {
  4182. DirView(Side)->DisplayPropertiesMenu();
  4183. Result = true;
  4184. }
  4185. return Result;
  4186. }
  4187. //---------------------------------------------------------------------------
  4188. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4189. {
  4190. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4191. {
  4192. FIgnoreNextDialogChar = Key;
  4193. }
  4194. Key = 0;
  4195. }
  4196. //---------------------------------------------------------------------------
  4197. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4198. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4199. {
  4200. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4201. if (Command != NULL)
  4202. {
  4203. KeyProcessed(Key, Shift);
  4204. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4205. {
  4206. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4207. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4208. false, false, const_cast<TCustomCommandType *>(Command));
  4209. }
  4210. }
  4211. }
  4212. //---------------------------------------------------------------------------
  4213. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4214. {
  4215. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4216. {
  4217. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4218. }
  4219. if (!DirView(osCurrent)->IsEditing())
  4220. {
  4221. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4222. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4223. {
  4224. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4225. // But so far it's not a problem, as it does what we want.
  4226. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4227. if (((Action->ShortCut == KeyShortCut) ||
  4228. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4229. AllowedAction(Action, aaShortCut))
  4230. {
  4231. // Has to be called before the action as the dialog char is already in queue.
  4232. // So when the action consumes message queue, we already need to have the
  4233. // FIgnoreNextDialogChar set
  4234. KeyProcessed(Key, Shift);
  4235. // Reset reference to previous component (when menu/toolbar was clicked).
  4236. // Needed to detect that action was invoked by keyboard shortcut
  4237. // in TNonVisualDataModule::ExplorerActionsExecute
  4238. Action->ActionComponent = NULL;
  4239. Action->Execute();
  4240. return;
  4241. }
  4242. }
  4243. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4244. {
  4245. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4246. {
  4247. KeyProcessed(Key, Shift);
  4248. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4249. return;
  4250. }
  4251. }
  4252. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4253. {
  4254. KeyProcessed(Key, Shift);
  4255. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4256. }
  4257. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4258. if (IsCustomShortCut(CustomShortCut))
  4259. {
  4260. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4261. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4262. if (WinConfiguration->SharedBookmarks != NULL)
  4263. {
  4264. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4265. if ((Bookmark != NULL) &&
  4266. OpenBookmark(FCurrentSide, Bookmark))
  4267. {
  4268. KeyProcessed(Key, Shift);
  4269. }
  4270. }
  4271. }
  4272. }
  4273. TForm::KeyDown(Key, Shift);
  4274. }
  4275. //---------------------------------------------------------------------------
  4276. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4277. {
  4278. bool OpenedTerminal = false;
  4279. UnicodeString ProtocolName;
  4280. UnicodeString ProtocolNameHint;
  4281. UnicodeString SecurityProtocolName;
  4282. UnicodeString DurationHint;
  4283. // See the comment in TScpCommanderForm::UpdateControls()
  4284. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4285. if (ActiveTerminal != NULL)
  4286. {
  4287. // Active is not enough here
  4288. if (ActiveTerminal->Status == ssOpened)
  4289. {
  4290. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4291. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4292. if (FileSystemInfo.ProtocolName.IsEmpty())
  4293. {
  4294. ProtocolName = SessionInfo.ProtocolName;
  4295. }
  4296. else
  4297. {
  4298. ProtocolName = FileSystemInfo.ProtocolName;
  4299. }
  4300. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4301. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4302. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4303. OpenedTerminal = true;
  4304. }
  4305. else if (ActiveTerminal->Status > ssClosed)
  4306. {
  4307. ProtocolName = LoadStr(STATUS_CONNECTING);
  4308. }
  4309. else
  4310. {
  4311. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4312. }
  4313. }
  4314. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4315. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4316. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4317. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4318. if (ProtocolStatusPanel->Caption != ProtocolName)
  4319. {
  4320. ProtocolStatusPanel->Caption = ProtocolName;
  4321. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4322. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4323. }
  4324. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4325. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4326. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4327. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4328. SecurityStatusPanel->Enabled = SecurityEnabled;
  4329. UnicodeString SecurityProtocolHint;
  4330. if (SecurityEnabled)
  4331. {
  4332. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4333. }
  4334. else
  4335. {
  4336. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4337. }
  4338. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4339. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4340. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4341. DurationStatusPanel->Hint = DurationHint;
  4342. UpdateStatusBar();
  4343. }
  4344. //---------------------------------------------------------------------------
  4345. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4346. {
  4347. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4348. DebugAssert(SessionStatusBar != NULL);
  4349. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4350. if (!FNote.IsEmpty())
  4351. {
  4352. NoteStatusPanel->Caption = FNote;
  4353. }
  4354. else
  4355. {
  4356. UpdateStatusPanelText(NoteStatusPanel);
  4357. }
  4358. NoteStatusPanel->Hint = FNoteHints;
  4359. UnicodeString LoginTime;
  4360. // See the comment in TScpCommanderForm::UpdateControls()
  4361. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4362. // Active is not enough here
  4363. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4364. {
  4365. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4366. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4367. }
  4368. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4369. DurationStatusPanel->Caption = LoginTime;
  4370. }
  4371. //---------------------------------------------------------------------------
  4372. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4373. {
  4374. Panel->Caption = L"";
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. void __fastcall TCustomScpExplorerForm::Idle()
  4378. {
  4379. if (FShowing || FStandaloneEditing)
  4380. {
  4381. FEditorManager->Check();
  4382. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4383. // that needs to know if queue view is visible (and it may be closed after queue update)
  4384. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4385. }
  4386. if (FShowing)
  4387. {
  4388. if (!NonVisualDataModule->Busy &&
  4389. // Menu is opened or drag&drop is going on
  4390. (Mouse->Capture == NULL))
  4391. {
  4392. if (FRefreshRemoteDirectory)
  4393. {
  4394. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4395. {
  4396. Terminal->RefreshDirectory();
  4397. }
  4398. FRefreshRemoteDirectory = false;
  4399. }
  4400. if (FRefreshLocalDirectory)
  4401. {
  4402. ReloadLocalDirectory();
  4403. FRefreshLocalDirectory = false;
  4404. }
  4405. if (WinConfiguration->RefreshRemotePanel)
  4406. {
  4407. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4408. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4409. {
  4410. RemoteDirView->ReloadDirectory();
  4411. }
  4412. }
  4413. }
  4414. }
  4415. if (FShowing || FStandaloneEditing)
  4416. {
  4417. if (FQueueStatusInvalidated)
  4418. {
  4419. UpdateQueueStatus(false);
  4420. }
  4421. RefreshQueueItems();
  4422. }
  4423. if (FShowing)
  4424. {
  4425. UpdateStatusBar();
  4426. }
  4427. FIgnoreNextDialogChar = 0;
  4428. }
  4429. //---------------------------------------------------------------------------
  4430. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4431. {
  4432. try
  4433. {
  4434. FUserActionTimer->Enabled = false;
  4435. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4436. {
  4437. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4438. {
  4439. FPendingQueueActionItem->ProcessUserAction();
  4440. }
  4441. }
  4442. }
  4443. __finally
  4444. {
  4445. FPendingQueueActionItem = NULL;
  4446. }
  4447. }
  4448. //---------------------------------------------------------------------------
  4449. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4450. {
  4451. if (WinConfiguration->MinimizeToTray)
  4452. {
  4453. UpdateTrayIcon();
  4454. FTrayIcon->Visible = true;
  4455. if (Visible)
  4456. {
  4457. ShowWindow(Handle, SW_HIDE);
  4458. }
  4459. }
  4460. }
  4461. //---------------------------------------------------------------------------
  4462. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4463. {
  4464. // WORKAROUND
  4465. // When restoring maximized window from minimization,
  4466. // rarely some controls do not align properly.
  4467. // Two instances seen (both for Commander):
  4468. // - When restoring, window is temporarily narrower (not maximized),
  4469. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4470. // Once maximized already, top dock shinks back, but the session PageControl,
  4471. // do not align up, leaving space between TopDock and PageControl.
  4472. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4473. for (int Index = 0; Index < ControlCount; Index++)
  4474. {
  4475. RealignControl(Controls[Index]);
  4476. }
  4477. if (FTrayIcon->Visible)
  4478. {
  4479. FTrayIcon->Visible = false;
  4480. if (Visible)
  4481. {
  4482. ShowWindow(Handle, SW_SHOW);
  4483. }
  4484. }
  4485. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4486. {
  4487. FNeedSession = false;
  4488. NonVisualDataModule->StartBusy();
  4489. try
  4490. {
  4491. NeedSession(true);
  4492. }
  4493. __finally
  4494. {
  4495. NonVisualDataModule->EndBusy();
  4496. }
  4497. }
  4498. }
  4499. //---------------------------------------------------------------------------
  4500. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4501. {
  4502. FTrayIcon->Hint = GetMainForm()->Caption;
  4503. }
  4504. //---------------------------------------------------------------------------
  4505. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4506. {
  4507. UpdateTrayIcon();
  4508. }
  4509. //---------------------------------------------------------------------------
  4510. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4511. {
  4512. // workaround
  4513. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4514. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4515. // But WM_ACTIVATEAPP message can be received even when
  4516. // the main window is minimized to the tray and internal editor window is focused
  4517. // (after another application was previously active)
  4518. if (::IsIconic(Handle))
  4519. {
  4520. if (!IsAppIconic())
  4521. {
  4522. SetAppIconic(true);
  4523. }
  4524. }
  4525. ::ApplicationRestore();
  4526. Application->BringToFront();
  4527. }
  4528. //---------------------------------------------------------------------------
  4529. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4530. {
  4531. RestoreApp();
  4532. }
  4533. //---------------------------------------------------------------------------
  4534. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4535. {
  4536. if (OpenInNewWindow())
  4537. {
  4538. ExecuteNewInstance(SessionUrl);
  4539. }
  4540. else
  4541. {
  4542. TTerminalManager::Instance()->NewSession(SessionUrl);
  4543. }
  4544. }
  4545. //---------------------------------------------------------------------------
  4546. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4547. {
  4548. NewSession();
  4549. }
  4550. //---------------------------------------------------------------------------
  4551. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4552. {
  4553. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4554. StoredSessions->NewSession(SessionName, SessionData);
  4555. // modified only, explicit
  4556. StoredSessions->Save(false, true);
  4557. // encode session name because of slashes in hierarchical sessions
  4558. return EncodeUrlString(SessionName);
  4559. }
  4560. //---------------------------------------------------------------------------
  4561. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4562. {
  4563. // current working directories become defaults here, what is not right
  4564. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4565. return SaveHiddenDuplicateSession(SessionData.get());
  4566. }
  4567. //---------------------------------------------------------------------------
  4568. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4569. {
  4570. if (OpenInNewWindow())
  4571. {
  4572. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4573. }
  4574. else
  4575. {
  4576. // current working directories become defaults here, what is not right
  4577. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4578. TTerminalManager * Manager = TTerminalManager::Instance();
  4579. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4580. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4581. // We definitelly want these
  4582. ASession->Disconnected = ManagedSession->Disconnected;
  4583. // Not sure about these two
  4584. ASession->Permanent = ManagedSession->Permanent;
  4585. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4586. Manager->ActiveSession = ASession;
  4587. }
  4588. }
  4589. //---------------------------------------------------------------------------
  4590. void __fastcall TCustomScpExplorerForm::RenameSession()
  4591. {
  4592. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4593. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4594. // When submitting the default name, do not fix it
  4595. (Name != ManagedSession->SessionData->SessionName))
  4596. {
  4597. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4598. TSessionData::ValidateName(Name);
  4599. // This is inconsistent with how color (for example) is handled.
  4600. ManagedSession->SessionData->Name = Name;
  4601. UpdateControls();
  4602. // Add/Remove distinguishing paths from sessions of the same name.
  4603. SessionListChanged();
  4604. }
  4605. }
  4606. //---------------------------------------------------------------------------
  4607. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4608. {
  4609. DebugAssert(Queue != NULL);
  4610. bool Result = Queue->IsEmpty;
  4611. if (!Result)
  4612. {
  4613. SetFocus();
  4614. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4615. }
  4616. return Result;
  4617. }
  4618. //---------------------------------------------------------------------------
  4619. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4620. {
  4621. return CanCloseQueue(FQueue);
  4622. }
  4623. //---------------------------------------------------------------------------
  4624. void __fastcall TCustomScpExplorerForm::CloseSession()
  4625. {
  4626. if (CanCloseQueue())
  4627. {
  4628. TTerminalManager::Instance()->FreeActiveTerminal();
  4629. }
  4630. }
  4631. //---------------------------------------------------------------------------
  4632. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4633. {
  4634. if (CanCloseQueue())
  4635. {
  4636. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4637. }
  4638. }
  4639. //---------------------------------------------------------------------------
  4640. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4641. {
  4642. DetachTerminal(Terminal);
  4643. RemoteDirView->Terminal = Terminal;
  4644. UpdateRemotePathComboBox(false);
  4645. InitStatusBar();
  4646. UpdateControls();
  4647. }
  4648. //---------------------------------------------------------------------------
  4649. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4650. {
  4651. InitStatusBar();
  4652. }
  4653. //---------------------------------------------------------------------------
  4654. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4655. {
  4656. if (DebugAlwaysTrue(!Terminal->Active))
  4657. {
  4658. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4659. }
  4660. }
  4661. //---------------------------------------------------------------------------
  4662. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4663. {
  4664. if (OpenInNewWindow())
  4665. {
  4666. // encode session name because of slashes in hierarchical sessions
  4667. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4668. }
  4669. else
  4670. {
  4671. TTerminalManager * Manager = TTerminalManager::Instance();
  4672. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4673. Manager->ActiveSession = Terminal;
  4674. }
  4675. }
  4676. //---------------------------------------------------------------------------
  4677. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4678. {
  4679. TTerminalManager * Manager = TTerminalManager::Instance();
  4680. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4681. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4682. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4683. // FirstSession can be null, if some of the
  4684. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4685. {
  4686. FirstSession->Disconnected = true;
  4687. FirstSession->DisconnectedTemporarily = true;
  4688. }
  4689. Manager->ActiveSession = FirstSession;
  4690. }
  4691. //---------------------------------------------------------------------------
  4692. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4693. {
  4694. if (OpenInNewWindow())
  4695. {
  4696. ExecuteNewInstance(Name);
  4697. }
  4698. else
  4699. {
  4700. DoOpenFolderOrWorkspace(Name, true);
  4701. }
  4702. }
  4703. //---------------------------------------------------------------------------
  4704. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4705. bool &CanClose)
  4706. {
  4707. // See the comment in CloseApp()
  4708. if (NonVisualDataModule->Busy)
  4709. {
  4710. CanClose = false;
  4711. }
  4712. else
  4713. {
  4714. TTerminalManager * Manager = TTerminalManager::Instance();
  4715. int ActiveSessions = 0;
  4716. for (int Index = 0; Index < Manager->Count; Index++)
  4717. {
  4718. if (Manager->Sessions[Index]->Active)
  4719. {
  4720. ActiveSessions++;
  4721. }
  4722. }
  4723. // Confirm, when there's at least one active remote session or
  4724. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4725. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4726. WinConfiguration->ConfirmClosingSession)
  4727. {
  4728. unsigned int Result;
  4729. TMessageParams Params(mpNeverAskAgainCheck);
  4730. UnicodeString Message;
  4731. int Answers = qaOK | qaCancel;
  4732. // The current session is the only active session
  4733. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4734. {
  4735. if (!WinConfiguration->AutoSaveWorkspace)
  4736. {
  4737. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4738. }
  4739. else
  4740. {
  4741. Message = LoadStr(CLOSE_SESSION);
  4742. }
  4743. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4744. }
  4745. // Multiple active sessions or one active session, but it's not the current one
  4746. else if (ActiveSessions > 0)
  4747. {
  4748. if (!WinConfiguration->AutoSaveWorkspace)
  4749. {
  4750. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4751. }
  4752. else
  4753. {
  4754. Message = LoadStr(CLOSE_SESSIONS);
  4755. }
  4756. }
  4757. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4758. else
  4759. {
  4760. DebugAlwaysTrue(Manager->Count > 1);
  4761. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4762. Message = LoadStr(CLOSE_WORKSPACE);
  4763. }
  4764. UnicodeString Note;
  4765. if (WinConfiguration->AutoSaveWorkspace)
  4766. {
  4767. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4768. }
  4769. else
  4770. {
  4771. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4772. Answers = qaYes | qaNo | qaCancel;
  4773. }
  4774. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4775. SetFocus();
  4776. Result = MessageDialog(Message, qtConfirmation,
  4777. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4778. if (Result == qaNeverAskAgain)
  4779. {
  4780. WinConfiguration->ConfirmClosingSession = false;
  4781. }
  4782. if (Result == qaNo)
  4783. {
  4784. CanClose = SaveWorkspace(true);
  4785. // note that the workspace will be saved redundatly again from FormClose
  4786. }
  4787. else
  4788. {
  4789. CanClose =
  4790. (Result == qaOK) ||
  4791. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4792. (Result == qaNeverAskAgain);
  4793. }
  4794. }
  4795. if (CanClose && HasActiveTerminal())
  4796. {
  4797. CanClose = CanCloseQueue();
  4798. }
  4799. }
  4800. if (CanClose)
  4801. {
  4802. CanClose =
  4803. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4804. FEditorManager->CloseExternalFilesWithoutProcess();
  4805. if (CanClose)
  4806. {
  4807. while (CanClose && (FLocalEditors->Count > 0))
  4808. {
  4809. int PrevCount = FLocalEditors->Count;
  4810. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4811. CanClose = (FLocalEditors->Count < PrevCount);
  4812. }
  4813. if (CanClose)
  4814. {
  4815. CanClose = FEditorManager->Empty(true);
  4816. if (!CanClose)
  4817. {
  4818. SetFocus();
  4819. CanClose =
  4820. (MessageDialog(
  4821. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4822. }
  4823. }
  4824. }
  4825. }
  4826. }
  4827. //---------------------------------------------------------------------------
  4828. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4829. {
  4830. TForm * Form = dynamic_cast<TForm *>(Sender);
  4831. DebugAssert(Form != NULL);
  4832. Form->Close();
  4833. }
  4834. //---------------------------------------------------------------------------
  4835. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4836. {
  4837. TForm * Form = dynamic_cast<TForm *>(Sender);
  4838. delete Form;
  4839. }
  4840. //---------------------------------------------------------------------------
  4841. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4842. {
  4843. while (FLocalEditors->Count > 0)
  4844. {
  4845. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4846. }
  4847. }
  4848. //---------------------------------------------------------------------------
  4849. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4850. TObject *Sender)
  4851. {
  4852. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4853. try
  4854. {
  4855. SetProperties(osRemote, FileList);
  4856. }
  4857. __finally
  4858. {
  4859. delete FileList;
  4860. }
  4861. }
  4862. //---------------------------------------------------------------------------
  4863. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4864. {
  4865. if (value)
  4866. {
  4867. if (Component == fcCustomCommandsBand)
  4868. {
  4869. UpdateCustomCommandsToolbar();
  4870. }
  4871. if (Component == fcQueueFileList)
  4872. {
  4873. DebugAssert(!QueueFileListSplitter->Visible);
  4874. AdjustQueueLayout();
  4875. UpdateQueueFileList();
  4876. }
  4877. }
  4878. }
  4879. //---------------------------------------------------------------------------
  4880. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4881. {
  4882. int MinHeightLimit =
  4883. GetStaticQueuePanelComponentsHeight() +
  4884. QueueFileListSplitter->Height +
  4885. QueueFileList->Height +
  4886. GetMinQueueViewHeight();
  4887. if (QueuePanel->ClientHeight < MinHeightLimit)
  4888. {
  4889. QueuePanel->ClientHeight = MinHeightLimit;
  4890. }
  4891. }
  4892. //---------------------------------------------------------------------------
  4893. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4894. {
  4895. TControl * Control = GetComponent(Component);
  4896. DebugAssert(Control);
  4897. bool Changed = (Control->Visible != value);
  4898. if (Changed)
  4899. {
  4900. ComponentShowing(Component, value);
  4901. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4902. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4903. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4904. if (value)
  4905. {
  4906. int RemainingHeight = Control->Parent->ClientHeight;
  4907. int RemainingWidth = Control->Parent->ClientWidth;
  4908. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4909. {
  4910. TControl * ChildControl = Control->Parent->Controls[i];
  4911. if (ChildControl->Visible)
  4912. {
  4913. switch (ChildControl->Align)
  4914. {
  4915. case alTop:
  4916. case alBottom:
  4917. RemainingHeight -= ChildControl->Height;
  4918. break;
  4919. case alLeft:
  4920. case alRight:
  4921. RemainingWidth -= ChildControl->Width;
  4922. break;
  4923. }
  4924. }
  4925. }
  4926. int Reserve = ScaleByTextHeight(this, 32);
  4927. // queue in explorer, trees in commander
  4928. if (Control->Height > RemainingHeight - Reserve)
  4929. {
  4930. Control->Height = RemainingHeight / 2;
  4931. }
  4932. if (Control->Width > RemainingWidth - Reserve)
  4933. {
  4934. Control->Width = RemainingWidth / 2;
  4935. }
  4936. }
  4937. Control->Visible = value;
  4938. if (WasFocused && Visible)
  4939. {
  4940. DirView(osCurrent)->SetFocus();
  4941. }
  4942. FixControlsPlacement();
  4943. }
  4944. }
  4945. //---------------------------------------------------------------------------
  4946. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4947. {
  4948. TControl * Control = GetComponent(Component);
  4949. if (Control == NULL)
  4950. {
  4951. return false;
  4952. }
  4953. else
  4954. {
  4955. return Control->Visible;
  4956. }
  4957. }
  4958. //---------------------------------------------------------------------------
  4959. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4960. {
  4961. bool Result;
  4962. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4963. {
  4964. Result = !IsUWP();
  4965. }
  4966. else
  4967. {
  4968. Result = true;
  4969. }
  4970. return Result;
  4971. }
  4972. //---------------------------------------------------------------------------
  4973. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  4974. {
  4975. if (DirView->ItemFocused != NULL)
  4976. {
  4977. DirView->ItemFocused->MakeVisible(false);
  4978. }
  4979. }
  4980. //---------------------------------------------------------------------------
  4981. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4982. {
  4983. MakeFocusedItemVisible(RemoteDirView);
  4984. QueueSplitter->Visible = QueuePanel->Visible;
  4985. QueueFileListSplitter->Visible = QueueFileList->Visible;
  4986. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4987. TControl * QueueControlsOrder[] =
  4988. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  4989. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  4990. }
  4991. //---------------------------------------------------------------------------
  4992. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4993. {
  4994. switch (Component) {
  4995. case fcStatusBar: return RemoteStatusBar;
  4996. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4997. case fcQueueView: return QueuePanel;
  4998. case fcQueueToolbar: return QueueDock;
  4999. case fcRemoteTree: return RemoteDrivePanel;
  5000. case fcSessionsTabs: return SessionsPageControl;
  5001. case fcQueueFileList: return QueueFileList;
  5002. default: return NULL;
  5003. }
  5004. }
  5005. //---------------------------------------------------------------------------
  5006. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5007. TObject *Sender, TListColumn *Column, TPoint &Point)
  5008. {
  5009. DebugAssert(NonVisualDataModule && Column && Sender);
  5010. NonVisualDataModule->ListColumn = Column;
  5011. TPopupMenu * DirViewColumnMenu;
  5012. if (Sender == DirView(osOther))
  5013. {
  5014. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5015. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5016. (Column->Index == uvName);
  5017. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5018. (Column->Index == uvSize);
  5019. }
  5020. else
  5021. {
  5022. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5023. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5024. (Column->Index == dvName);
  5025. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5026. (Column->Index == dvSize);
  5027. }
  5028. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5029. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5030. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5031. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5032. }
  5033. //---------------------------------------------------------------------------
  5034. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5035. TObject *Sender, TListItem *Item, bool &AllowExec)
  5036. {
  5037. DoDirViewExecFile(Sender, Item, AllowExec);
  5038. }
  5039. //---------------------------------------------------------------------------
  5040. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5041. TListItem * Item, bool & AllowExec)
  5042. {
  5043. DebugAssert(Sender && Item && Configuration);
  5044. DebugAssert(AllowExec);
  5045. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5046. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5047. bool Remote = !IsSideLocalBrowser(Side);
  5048. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  5049. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  5050. // on files only (not directories)
  5051. // and only when symlinks are resolved (apply to remote panel only)
  5052. if (!ADirView->ItemIsDirectory(Item) &&
  5053. (ResolvedSymlinks || FForceExecution))
  5054. {
  5055. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5056. !FForceExecution && ResolvedSymlinks)
  5057. {
  5058. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5059. {
  5060. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5061. UnlockWindow();
  5062. try
  5063. {
  5064. if (IsLocalBrowserMode())
  5065. {
  5066. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5067. }
  5068. else
  5069. {
  5070. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5071. }
  5072. }
  5073. __finally
  5074. {
  5075. LockWindow();
  5076. }
  5077. AllowExec = false;
  5078. }
  5079. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5080. {
  5081. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5082. {
  5083. ExecuteFile(Side, efDefaultEditor);
  5084. AllowExec = false;
  5085. }
  5086. }
  5087. else
  5088. {
  5089. DebugFail();
  5090. }
  5091. }
  5092. // if we have not done anything special, fall back to default behavior
  5093. if (AllowExec)
  5094. {
  5095. if (Remote && !WinConfiguration->DisableOpenEdit)
  5096. {
  5097. ExecuteFile(osRemote, efShell);
  5098. AllowExec = false;
  5099. }
  5100. }
  5101. }
  5102. }
  5103. //---------------------------------------------------------------------------
  5104. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5105. {
  5106. return ((Side == osRemote) || (Side == osCurrent));
  5107. }
  5108. //---------------------------------------------------------------------------
  5109. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5110. {
  5111. DebugFail();
  5112. }
  5113. //---------------------------------------------------------------------------
  5114. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5115. {
  5116. DebugFail();
  5117. }
  5118. //---------------------------------------------------------------------------
  5119. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5120. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5121. {
  5122. TSynchronizeParamType Params;
  5123. Params.LocalDirectory = LocalDirectory;
  5124. Params.RemoteDirectory = RemoteDirectory;
  5125. int UnusedParams =
  5126. (GUIConfiguration->SynchronizeParams &
  5127. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5128. TTerminal::spCaseSensitive));
  5129. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5130. Params.Options = GUIConfiguration->SynchronizeOptions;
  5131. bool SaveSettings = false;
  5132. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5133. &DoSynchronizeTooManyDirectories);
  5134. DebugAssert(FSynchronizeController == NULL);
  5135. FSynchronizeController = &Controller;
  5136. bool Result;
  5137. try
  5138. {
  5139. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5140. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5141. int Options =
  5142. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5143. DebugAssert(FOnFeedSynchronizeError == NULL);
  5144. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5145. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5146. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  5147. if (Result)
  5148. {
  5149. if (SaveSettings)
  5150. {
  5151. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5152. GUIConfiguration->SynchronizeOptions = Params.Options;
  5153. }
  5154. else
  5155. {
  5156. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5157. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5158. FLAGSET(Params.Options, soSynchronize))
  5159. {
  5160. GUIConfiguration->SynchronizeOptions =
  5161. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5162. soSynchronize;
  5163. }
  5164. }
  5165. LocalDirectory = Params.LocalDirectory;
  5166. RemoteDirectory = Params.RemoteDirectory;
  5167. }
  5168. }
  5169. __finally
  5170. {
  5171. FSynchronizeController = NULL;
  5172. DebugAssert(FOnFeedSynchronizeError == NULL);
  5173. FOnFeedSynchronizeError = NULL;
  5174. }
  5175. return Result;
  5176. }
  5177. //---------------------------------------------------------------------------
  5178. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5179. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5180. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5181. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5182. TSynchronizeOptions * Options, bool Full)
  5183. {
  5184. // Keep-up-to-date mode
  5185. if (Terminal->Status == ssOpened)
  5186. {
  5187. try
  5188. {
  5189. int PParams = Params.Params;
  5190. if (!Full)
  5191. {
  5192. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5193. if (FLAGSET(Params.Options, soRecurse))
  5194. {
  5195. PParams |= TTerminal::spSubDirs;
  5196. }
  5197. }
  5198. else
  5199. {
  5200. // if keepuptodate is non-recursive,
  5201. // full sync before has to be non-recursive as well
  5202. if (FLAGCLEAR(Params.Options, soRecurse))
  5203. {
  5204. PParams |= TTerminal::spNoRecurse;
  5205. }
  5206. }
  5207. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5208. PParams, Checklist, Options);
  5209. }
  5210. catch (Exception & E)
  5211. {
  5212. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5213. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5214. {
  5215. // noop, already logged in MoreMessageDialog
  5216. }
  5217. else
  5218. {
  5219. ShowExtendedExceptionEx(Terminal, &E);
  5220. throw;
  5221. }
  5222. }
  5223. }
  5224. }
  5225. //---------------------------------------------------------------------------
  5226. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5227. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5228. const UnicodeString ErrorStr)
  5229. {
  5230. if (!Directory.IsEmpty())
  5231. {
  5232. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5233. }
  5234. else
  5235. {
  5236. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5237. }
  5238. }
  5239. //---------------------------------------------------------------------------
  5240. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5241. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5242. {
  5243. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5244. {
  5245. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5246. }
  5247. else
  5248. {
  5249. TMessageParams Params(mpNeverAskAgainCheck);
  5250. unsigned int Result = MessageDialog(
  5251. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5252. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5253. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5254. {
  5255. MaxDirectories *= 2;
  5256. if (Result == qaNeverAskAgain)
  5257. {
  5258. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5259. }
  5260. }
  5261. else
  5262. {
  5263. Abort();
  5264. }
  5265. }
  5266. }
  5267. //---------------------------------------------------------------------------
  5268. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5269. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5270. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5271. TSynchronizeOptions * Options)
  5272. {
  5273. // Keep-up-to-date mode
  5274. DebugAssert(!FAutoOperation);
  5275. void * BatchStorage;
  5276. BatchStart(BatchStorage);
  5277. FAutoOperation = true;
  5278. bool AnyOperation = false;
  5279. TDateTime StartTime = Now();
  5280. TSynchronizeChecklist * AChecklist = NULL;
  5281. try
  5282. {
  5283. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5284. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5285. {
  5286. FSynchronizeProgressForm->Start();
  5287. }
  5288. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5289. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5290. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5291. Options);
  5292. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5293. SAFE_DESTROY(FSynchronizeProgressForm);
  5294. AnyOperation = (AChecklist->CheckedCount > 0);
  5295. if (AnyOperation)
  5296. {
  5297. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5298. CreateProgressForm(&SynchronizeProgress);
  5299. try
  5300. {
  5301. Terminal->SynchronizeApply(
  5302. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5303. SynchronizeProcessedItem, NULL, NULL, NULL);
  5304. }
  5305. __finally
  5306. {
  5307. DestroyProgressForm();
  5308. }
  5309. }
  5310. }
  5311. __finally
  5312. {
  5313. if (Checklist == NULL)
  5314. {
  5315. delete AChecklist;
  5316. }
  5317. else
  5318. {
  5319. *Checklist = AChecklist;
  5320. }
  5321. FAutoOperation = false;
  5322. SAFE_DESTROY(FSynchronizeProgressForm);
  5323. BatchEnd(BatchStorage);
  5324. ReloadLocalDirectory();
  5325. if (AnyOperation)
  5326. {
  5327. OperationComplete(StartTime);
  5328. }
  5329. }
  5330. }
  5331. //---------------------------------------------------------------------------
  5332. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5333. {
  5334. // can be called from command line
  5335. return Visible &&
  5336. ((DirView(osRemote)->SelCount > 0) ||
  5337. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5338. }
  5339. //---------------------------------------------------------------------------
  5340. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5341. {
  5342. LogSynchronizeEvent(Terminal, Message);
  5343. }
  5344. //---------------------------------------------------------------------------
  5345. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5346. int Params, TSynchronizeOptions & Options)
  5347. {
  5348. DebugAssert(!IsLocalBrowserMode());
  5349. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5350. {
  5351. Options.Filter = new TStringList();
  5352. Options.Filter->CaseSensitive = false;
  5353. Options.Filter->Duplicates = Types::dupAccept;
  5354. if (DirView(osRemote)->SelCount > 0)
  5355. {
  5356. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5357. }
  5358. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5359. {
  5360. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5361. }
  5362. Options.Filter->Sort();
  5363. }
  5364. }
  5365. //---------------------------------------------------------------------------
  5366. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5367. {
  5368. TCopyParamType Defaults;
  5369. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5370. ConfigStorage->AccessMode = smRead;
  5371. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5372. {
  5373. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5374. }
  5375. ConfigStorage.reset(NULL);
  5376. std::unique_ptr<TStringList> Options(new TStringList());
  5377. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5378. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5379. UnicodeString Result;
  5380. if (Options->Count > 0)
  5381. {
  5382. Result =
  5383. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5384. }
  5385. return Result;
  5386. }
  5387. //---------------------------------------------------------------------------
  5388. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5389. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5390. {
  5391. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5392. UnicodeString AdditionalParams =
  5393. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5394. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5395. SerializeCopyParamForCommandLine(CopyParams),
  5396. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5397. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5398. ExecuteNewInstance(SessionName, AdditionalParams);
  5399. }
  5400. //---------------------------------------------------------------------------
  5401. struct TSynchronizeParams
  5402. {
  5403. UnicodeString LocalDirectory;
  5404. UnicodeString RemoteDirectory;
  5405. TSynchronizeMode Mode;
  5406. TSynchronizeChecklist * Checklist;
  5407. int Params;
  5408. TCopyParamType * CopyParam;
  5409. TDateTime CollectElapsed;
  5410. TDateTime * StartTime;
  5411. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5412. };
  5413. //---------------------------------------------------------------------------
  5414. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5415. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5416. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5417. {
  5418. DebugAssert(!FAutoOperation);
  5419. void * BatchStorage;
  5420. BatchStart(BatchStorage);
  5421. FAutoOperation = true;
  5422. TFileOperationStatistics Statistics;
  5423. TDateTime Start = Now();
  5424. try
  5425. {
  5426. Params.OnProcessedItem = OnProcessedItem;
  5427. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5428. CreateProgressForm(&SynchronizeProgress);
  5429. Terminal->SynchronizeApply(
  5430. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5431. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5432. &Statistics);
  5433. }
  5434. __finally
  5435. {
  5436. Params.OnProcessedItem = NULL;
  5437. FAutoOperation = false;
  5438. DestroyProgressForm();
  5439. BatchEnd(BatchStorage);
  5440. ReloadLocalDirectory();
  5441. }
  5442. if (WinConfiguration->SynchronizeSummary)
  5443. {
  5444. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5445. // The statistics should be 0 anyway in this case
  5446. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5447. {
  5448. Message += L"\n";
  5449. if (Statistics.FilesUploaded > 0)
  5450. {
  5451. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5452. }
  5453. if (Statistics.FilesDownloaded > 0)
  5454. {
  5455. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5456. }
  5457. if (Statistics.FilesDeletedLocal > 0)
  5458. {
  5459. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5460. }
  5461. if (Statistics.FilesDeletedRemote > 0)
  5462. {
  5463. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5464. }
  5465. }
  5466. TDateTime Elapsed = (Now() - Start);
  5467. Message +=
  5468. L"\n" +
  5469. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5470. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5471. TMessageParams Params(mpNeverAskAgainCheck);
  5472. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5473. if (Result == qaNeverAskAgain)
  5474. {
  5475. WinConfiguration->SynchronizeSummary = false;
  5476. }
  5477. }
  5478. }
  5479. //---------------------------------------------------------------------------
  5480. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5481. {
  5482. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5483. {
  5484. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5485. }
  5486. // Not set in keep-up-to-date mode - Synchronize() method
  5487. if (Token != NULL)
  5488. {
  5489. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5490. if (Params.OnProcessedItem != NULL)
  5491. {
  5492. Params.OnProcessedItem(NULL, ChecklistItem);
  5493. }
  5494. }
  5495. }
  5496. //---------------------------------------------------------------------------
  5497. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5498. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5499. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5500. {
  5501. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5502. *Params.StartTime = Now();
  5503. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5504. }
  5505. //---------------------------------------------------------------------------
  5506. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5507. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5508. {
  5509. // terminal can be already closed (e.g. dropped connection)
  5510. if (Terminal != NULL)
  5511. {
  5512. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5513. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5514. }
  5515. }
  5516. //---------------------------------------------------------------------------
  5517. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5518. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5519. {
  5520. DebugAssert(!IsLocalBrowserMode());
  5521. TAutoBatch AutoBatch(this);
  5522. TAutoFlag AutoOperationFlag(FAutoOperation);
  5523. if (Side == osRemote)
  5524. {
  5525. std::unique_ptr<TStrings> FileList(new TStringList());
  5526. FileList->AddObject(FileName, RemoteFile);
  5527. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5528. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5529. RemoteDirView->SaveSelection();
  5530. RemoteDirView->SaveSelectedNames();
  5531. try
  5532. {
  5533. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5534. }
  5535. catch(...)
  5536. {
  5537. RemoteDirView->DiscardSavedSelection();
  5538. throw;
  5539. }
  5540. RemoteDirView->RestoreSelection();
  5541. }
  5542. else if (DebugAlwaysTrue(Side == osLocal))
  5543. {
  5544. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5545. {
  5546. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5547. }
  5548. UnicodeString Directory = ExtractFileDir(FileName);
  5549. ReloadLocalDirectory(Directory);
  5550. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5551. if (!SamePaths(Directory, NewDirectory))
  5552. {
  5553. ReloadLocalDirectory(NewDirectory);
  5554. }
  5555. }
  5556. }
  5557. //---------------------------------------------------------------------------
  5558. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5559. {
  5560. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5561. if (Side == osLocal)
  5562. {
  5563. if (Action == TSynchronizeChecklist::saDownloadNew)
  5564. {
  5565. OpenFolderInExplorer(LocalPath);
  5566. }
  5567. else
  5568. {
  5569. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5570. }
  5571. }
  5572. else if (DebugAlwaysTrue(Side == osRemote))
  5573. {
  5574. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5575. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5576. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5577. if (!LocalPath.IsEmpty())
  5578. {
  5579. SessionData->LocalDirectory = LocalPath;
  5580. }
  5581. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5582. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5583. }
  5584. }
  5585. //---------------------------------------------------------------------------
  5586. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5587. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5588. const TCopyParamType * CopyParams)
  5589. {
  5590. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5591. UnicodeString AdditionalParams =
  5592. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5593. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5594. SerializeCopyParamForCommandLine(CopyParams),
  5595. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5596. LocalDirectory, RemoteDirectory, Mode, Params));
  5597. ExecuteNewInstance(SessionName, AdditionalParams);
  5598. }
  5599. //---------------------------------------------------------------------------
  5600. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5601. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5602. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5603. {
  5604. int Result;
  5605. bool SaveSettings = false;
  5606. int Options =
  5607. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5608. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5609. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5610. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5611. bool Continue =
  5612. (UseDefaults == 0) ||
  5613. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5614. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5615. if (Continue)
  5616. {
  5617. Configuration->Usage->Inc(L"Synchronizations");
  5618. UpdateCopyParamCounters(CopyParam);
  5619. TSynchronizeOptions SynchronizeOptions;
  5620. GetSynchronizeOptions(Params, SynchronizeOptions);
  5621. if (SaveSettings)
  5622. {
  5623. GUIConfiguration->SynchronizeParams = Params;
  5624. }
  5625. else
  5626. {
  5627. SaveMode = false;
  5628. }
  5629. TDateTime StartTime = Now();
  5630. TSynchronizeChecklist * Checklist = NULL;
  5631. try
  5632. {
  5633. DebugAssert(!FAutoOperation);
  5634. FAutoOperation = true;
  5635. try
  5636. {
  5637. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5638. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5639. int Files = -1;
  5640. if (DataList->Count >= 1)
  5641. {
  5642. Files = StrToIntDef(DataList->Strings[0], Files);
  5643. }
  5644. else
  5645. {
  5646. DataList->Add(UnicodeString());
  5647. }
  5648. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5649. FSynchronizeProgressForm->Start();
  5650. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5651. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5652. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5653. &SynchronizeOptions);
  5654. if (Terminal->SessionData->CacheDirectories)
  5655. {
  5656. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5657. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5658. }
  5659. }
  5660. __finally
  5661. {
  5662. FAutoOperation = false;
  5663. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5664. SAFE_DESTROY(FSynchronizeProgressForm);
  5665. }
  5666. TSynchronizeParams SynchronizeParams;
  5667. SynchronizeParams.LocalDirectory = LocalDirectory;
  5668. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5669. SynchronizeParams.Mode = Mode;
  5670. SynchronizeParams.CopyParam = &CopyParam;
  5671. SynchronizeParams.Params = Params;
  5672. SynchronizeParams.Checklist = Checklist;
  5673. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5674. SynchronizeParams.StartTime = &StartTime;
  5675. SynchronizeParams.OnProcessedItem = NULL;
  5676. Result = Checklist->Count;
  5677. if (Checklist->Count > 0)
  5678. {
  5679. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5680. {
  5681. if (!DoSynchronizeChecklistDialog(
  5682. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5683. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5684. {
  5685. Result = -1;
  5686. }
  5687. }
  5688. else
  5689. {
  5690. FullSynchronize(SynchronizeParams, NULL, NULL);
  5691. }
  5692. }
  5693. else
  5694. {
  5695. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5696. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5697. }
  5698. }
  5699. __finally
  5700. {
  5701. delete Checklist;
  5702. }
  5703. OperationComplete(StartTime);
  5704. }
  5705. else
  5706. {
  5707. Result = -1;
  5708. }
  5709. return Result;
  5710. }
  5711. //---------------------------------------------------------------------------
  5712. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5713. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5714. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5715. {
  5716. if (Collect)
  5717. {
  5718. DebugAssert(FSynchronizeProgressForm != NULL);
  5719. if (!FSynchronizeProgressForm->Started)
  5720. {
  5721. FSynchronizeProgressForm->Start();
  5722. }
  5723. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5724. SetTaskbarListProgressValue(CompareProgress);
  5725. }
  5726. else
  5727. {
  5728. DebugAssert(FSynchronizeProgressForm == NULL);
  5729. DebugAssert(FProgressForm != NULL);
  5730. }
  5731. }
  5732. //---------------------------------------------------------------------------
  5733. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5734. {
  5735. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5736. TRemoteFile * File = NULL;
  5737. Terminal->ReadFile(FullFileName, File);
  5738. if (File != NULL)
  5739. {
  5740. std::unique_ptr<TRemoteFile> FileOwner(File);
  5741. TAutoFlag Flag(FStandaloneEditing);
  5742. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5743. Application->ShowMainForm = false;
  5744. Application->Run();
  5745. }
  5746. }
  5747. //---------------------------------------------------------------------------
  5748. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5749. {
  5750. DebugFail();
  5751. }
  5752. //---------------------------------------------------------------------------
  5753. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5754. {
  5755. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5756. SessionData->Assign(ManagedSession->SessionData);
  5757. UpdateSessionData(SessionData.get());
  5758. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5759. if (IsActiveTerminal(ManagedSession))
  5760. {
  5761. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5762. }
  5763. if (!ManagedSession->SessionData->HasSessionName())
  5764. {
  5765. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5766. // once IsWorkspace is cleared
  5767. SessionData->Name = UnicodeString();
  5768. }
  5769. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5770. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5771. SessionData->IsWorkspace = false;
  5772. return SessionData.release();
  5773. }
  5774. //---------------------------------------------------------------------------
  5775. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5776. {
  5777. TSessionData * SessionData = CloneCurrentSessionData();
  5778. try
  5779. {
  5780. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5781. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5782. }
  5783. __finally
  5784. {
  5785. delete SessionData;
  5786. }
  5787. }
  5788. //---------------------------------------------------------------------------
  5789. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5790. {
  5791. TTerminalManager * Manager = TTerminalManager::Instance();
  5792. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5793. if (DebugAlwaysTrue(ManagedSession != NULL))
  5794. {
  5795. // Update ManagedSession->StateData
  5796. UpdateSession(ManagedSession);
  5797. }
  5798. Manager->SaveWorkspace(DataList.get());
  5799. return DataList.release();
  5800. }
  5801. //---------------------------------------------------------------------------
  5802. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5803. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5804. {
  5805. WinConfiguration->LastWorkspace = Name;
  5806. if (!SavePasswords)
  5807. {
  5808. for (int Index = 0; Index < DataList->Count; Index++)
  5809. {
  5810. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5811. if (SessionData->Link.IsEmpty())
  5812. {
  5813. SessionData->ClearSessionPasswords();
  5814. }
  5815. }
  5816. }
  5817. StoredSessions->NewWorkspace(Name, DataList);
  5818. // modified only
  5819. StoredSessions->Save(false, Explicit);
  5820. }
  5821. //---------------------------------------------------------------------------
  5822. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5823. {
  5824. return
  5825. DefaultStr(WinConfiguration->LastWorkspace,
  5826. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5827. }
  5828. //---------------------------------------------------------------------------
  5829. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5830. {
  5831. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5832. bool AnyNonStoredSessionWithPassword = false;
  5833. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5834. bool AllNonStoredSessionsAnonymous = true;
  5835. TTerminalManager * Manager = TTerminalManager::Instance();
  5836. for (int Index = 0; Index < DataList->Count; Index++)
  5837. {
  5838. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5839. if (SessionData->Link.IsEmpty())
  5840. {
  5841. if (SessionData->HasAnySessionPassword())
  5842. {
  5843. AnyNonStoredSessionWithPassword = true;
  5844. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5845. {
  5846. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5847. }
  5848. }
  5849. if (!SameText(SessionData->UserName, AnonymousUserName))
  5850. {
  5851. AllNonStoredSessionsAnonymous = false;
  5852. }
  5853. }
  5854. }
  5855. bool SavePasswords;
  5856. bool * PSavePasswords;
  5857. bool NotRecommendedSavingPasswords =
  5858. !CustomWinConfiguration->UseMasterPassword &&
  5859. !AllNonStoredSessionsAnonymous;
  5860. if (Configuration->DisablePasswordStoring ||
  5861. !AnyNonStoredSessionWithPassword)
  5862. {
  5863. PSavePasswords = NULL;
  5864. SavePasswords = false;
  5865. }
  5866. else
  5867. {
  5868. PSavePasswords = &SavePasswords;
  5869. SavePasswords =
  5870. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5871. !NotRecommendedSavingPasswords;
  5872. }
  5873. UnicodeString Name = WorkspaceName();
  5874. bool CreateShortcut = false;
  5875. bool Result =
  5876. DoSaveWorkspaceDialog(
  5877. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5878. EnableAutoSave);
  5879. if (Result)
  5880. {
  5881. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5882. if (CreateShortcut)
  5883. {
  5884. TOperationVisualizer Visualizer;
  5885. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5886. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5887. }
  5888. if (EnableAutoSave)
  5889. {
  5890. WinConfiguration->AutoSaveWorkspace = true;
  5891. WinConfiguration->AutoWorkspace = Name;
  5892. if (PSavePasswords != NULL)
  5893. {
  5894. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5895. }
  5896. }
  5897. }
  5898. return Result;
  5899. }
  5900. //---------------------------------------------------------------------------
  5901. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  5902. {
  5903. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  5904. DebugAssert(Session == ManagedSession);
  5905. if (!IsLocalBrowserMode())
  5906. {
  5907. SAFE_DESTROY(Session->RemoteExplorerState);
  5908. if (WinConfiguration->PreservePanelState)
  5909. {
  5910. Session->RemoteExplorerState = RemoteDirView->SaveState();
  5911. }
  5912. }
  5913. UpdateSessionData(Session->StateData);
  5914. }
  5915. //---------------------------------------------------------------------------
  5916. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5917. {
  5918. // Keep in sync with TSessionData::CopyStateData
  5919. DebugAssert(Data != NULL);
  5920. // This is inconsistent with how color (for example) is handled.
  5921. Data->Name = ManagedSession->SessionData->Name;
  5922. if (!IsLocalBrowserMode())
  5923. {
  5924. // cannot use RemoteDirView->Path, because it is empty if connection
  5925. // was already closed
  5926. // also only peek, we may not be connected at all atm
  5927. // so make sure we do not try retrieving current directory from the server
  5928. // (particularly with FTP)
  5929. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5930. if (!ACurrentDirectory.IsEmpty())
  5931. {
  5932. Data->RemoteDirectory = ACurrentDirectory;
  5933. }
  5934. }
  5935. Data->Color = SessionColor;
  5936. }
  5937. //---------------------------------------------------------------------------
  5938. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5939. {
  5940. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5941. DebugAssert(Toolbar != NULL);
  5942. for (int i = 0; i < Toolbar->Items->Count; i++)
  5943. {
  5944. TTBXCustomDropDownItem * DropDownItem;
  5945. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5946. if (DropDownItem != NULL)
  5947. {
  5948. ToolbarItemResize(DropDownItem,
  5949. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5950. Toolbar->NonClientWidth);
  5951. break;
  5952. }
  5953. }
  5954. }
  5955. //---------------------------------------------------------------------------
  5956. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5957. {
  5958. Item->EditWidth = Width;
  5959. }
  5960. //---------------------------------------------------------------------------
  5961. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5962. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5963. {
  5964. for (int i = 0; i < Toolbar->Items->Count; i++)
  5965. {
  5966. TTBXCustomDropDownItem * DropDownItem;
  5967. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5968. if (DropDownItem != NULL)
  5969. {
  5970. ASize.x -= DropDownItem->EditWidth;
  5971. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5972. }
  5973. }
  5974. }
  5975. //---------------------------------------------------------------------------
  5976. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5977. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5978. {
  5979. if (WinConfiguration->DDWarnLackOfTempSpace)
  5980. {
  5981. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5982. if (!ADrive.IsEmpty())
  5983. {
  5984. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5985. DebugAssert(RequiredSpace >= 0);
  5986. __int64 RequiredWithReserve;
  5987. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5988. if (FreeSpace < RequiredWithReserve)
  5989. {
  5990. unsigned int Result;
  5991. TMessageParams Params(mpNeverAskAgainCheck);
  5992. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5993. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5994. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5995. if (Result == qaNeverAskAgain)
  5996. {
  5997. WinConfiguration->DDWarnLackOfTempSpace = false;
  5998. }
  5999. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6000. }
  6001. }
  6002. }
  6003. }
  6004. //---------------------------------------------------------------------------
  6005. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6006. {
  6007. DoOpenDirectoryDialog(odAddBookmark, Side);
  6008. }
  6009. //---------------------------------------------------------------------------
  6010. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6011. {
  6012. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6013. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6014. try
  6015. {
  6016. TCustomDirView * DView = DirView(Side);
  6017. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6018. {
  6019. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6020. }
  6021. }
  6022. catch (...)
  6023. {
  6024. delete VisitedDirectories;
  6025. throw;
  6026. }
  6027. return VisitedDirectories;
  6028. }
  6029. //---------------------------------------------------------------------------
  6030. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6031. TOpenDirectoryMode Mode, TOperationSide Side)
  6032. {
  6033. bool Continue = true;
  6034. if (Mode == odAddBookmark)
  6035. {
  6036. TMessageParams Params(mpNeverAskAgainCheck);
  6037. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6038. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6039. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6040. unsigned int Answer =
  6041. MessageDialog(Message,
  6042. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6043. if (Answer == qaNeverAskAgain)
  6044. {
  6045. Continue = true;
  6046. WinConfiguration->UseSharedBookmarks = true;
  6047. }
  6048. else if (Answer == qaOK)
  6049. {
  6050. Continue = true;
  6051. WinConfiguration->UseSharedBookmarks = false;
  6052. }
  6053. else
  6054. {
  6055. Continue = false;
  6056. }
  6057. }
  6058. if (Continue)
  6059. {
  6060. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6061. try
  6062. {
  6063. UnicodeString Name = DirView(Side)->PathName;
  6064. bool Repeat;
  6065. do
  6066. {
  6067. Repeat = false;
  6068. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6069. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6070. // do not allow switching to location profiles,
  6071. // if we are not connected
  6072. HasDirView[osLocal] && (Terminal != NULL)))
  6073. {
  6074. TWindowLock Lock(this);
  6075. if (!TryOpenDirectory(Side, Name))
  6076. {
  6077. Repeat = true;
  6078. Mode = odBrowse;
  6079. }
  6080. }
  6081. }
  6082. while (Repeat);
  6083. }
  6084. __finally
  6085. {
  6086. delete VisitedDirectories;
  6087. }
  6088. }
  6089. }
  6090. //---------------------------------------------------------------------------
  6091. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6092. {
  6093. DebugAssert(!Terminal->CommandSessionOpened);
  6094. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6095. }
  6096. //---------------------------------------------------------------------------
  6097. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6098. {
  6099. bool Result = Terminal->IsCapable[Capability] ||
  6100. Terminal->CommandSessionOpened;
  6101. if (!Result)
  6102. {
  6103. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6104. if (!GUIConfiguration->ConfirmCommandSession)
  6105. {
  6106. Result = true;
  6107. }
  6108. else
  6109. {
  6110. TMessageParams Params(mpNeverAskAgainCheck);
  6111. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6112. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6113. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6114. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6115. if (Answer == qaNeverAskAgain)
  6116. {
  6117. GUIConfiguration->ConfirmCommandSession = false;
  6118. Result = true;
  6119. }
  6120. else if (Answer == qaOK)
  6121. {
  6122. Result = true;
  6123. }
  6124. }
  6125. if (Result)
  6126. {
  6127. Result = CommandSessionFallback();
  6128. }
  6129. }
  6130. return Result;
  6131. }
  6132. //---------------------------------------------------------------------------
  6133. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6134. {
  6135. if (EnsureCommandSessionFallback(fcAnyCommand))
  6136. {
  6137. DoConsoleDialog(Terminal, Command);
  6138. }
  6139. }
  6140. //---------------------------------------------------------------------------
  6141. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6142. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6143. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6144. {
  6145. if (!IsSideLocalBrowser(osRemote) &&
  6146. IsFileControl(DropSourceControl, osRemote) &&
  6147. (FDDExtMapFile != NULL))
  6148. {
  6149. Accept = true;
  6150. }
  6151. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6152. DebugAssert(FDDTargetControl != NULL);
  6153. }
  6154. //---------------------------------------------------------------------------
  6155. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6156. _di_IDataObject DataObj, int KeyState,
  6157. const TPoint & Point, int & Effect, bool & Accept)
  6158. {
  6159. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6160. }
  6161. //---------------------------------------------------------------------------
  6162. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6163. _di_IDataObject DataObj, int KeyState,
  6164. const TPoint & Point, int & Effect, bool & Accept)
  6165. {
  6166. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6167. }
  6168. //---------------------------------------------------------------------------
  6169. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6170. TObject * Sender)
  6171. {
  6172. DebugUsedParam(Sender);
  6173. DebugAssert(FDDTargetControl == Sender);
  6174. FDDTargetControl = NULL;
  6175. }
  6176. //---------------------------------------------------------------------------
  6177. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6178. {
  6179. FileControlDDDragLeave(SessionsPageControl);
  6180. }
  6181. //---------------------------------------------------------------------------
  6182. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6183. {
  6184. FileControlDDDragLeave(QueueView3);
  6185. }
  6186. //---------------------------------------------------------------------------
  6187. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6188. {
  6189. DebugAssert(!IsSideLocalBrowser(Side));
  6190. DebugUsedParam(Side);
  6191. bool Edit = false;
  6192. TRemoteFile * File = NULL;
  6193. UnicodeString FileName;
  6194. UnicodeString PointTo;
  6195. bool SymbolicLink = true;
  6196. if (RemoteDirView->ItemFocused)
  6197. {
  6198. DebugAssert(RemoteDirView->ItemFocused->Data);
  6199. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6200. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6201. if (Edit)
  6202. {
  6203. FileName = File->FileName;
  6204. PointTo = File->LinkTo;
  6205. }
  6206. else
  6207. {
  6208. PointTo = File->FileName;
  6209. }
  6210. }
  6211. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6212. Terminal->IsCapable[fcHardLink]))
  6213. {
  6214. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6215. if (Edit)
  6216. {
  6217. DebugAssert(File->FileName == FileName);
  6218. int Params = dfNoRecursive;
  6219. Terminal->ExceptionOnFail = true;
  6220. try
  6221. {
  6222. Terminal->DeleteFile(L"", File, &Params);
  6223. }
  6224. __finally
  6225. {
  6226. Terminal->ExceptionOnFail = false;
  6227. }
  6228. }
  6229. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6230. }
  6231. }
  6232. //---------------------------------------------------------------------------
  6233. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6234. {
  6235. return
  6236. (IsSideLocalBrowser(Side) ||
  6237. (HasActiveTerminal() &&
  6238. Terminal->ResolvingSymlinks &&
  6239. Terminal->IsCapable[fcSymbolicLink]));
  6240. }
  6241. //---------------------------------------------------------------------------
  6242. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6243. {
  6244. return
  6245. !IsSideLocalBrowser(FCurrentSide) &&
  6246. (RemoteDirView->ItemFocused != NULL) &&
  6247. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6248. Terminal->SessionData->ResolveSymlinks;
  6249. }
  6250. //---------------------------------------------------------------------------
  6251. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6252. {
  6253. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6254. FForceExecution = true;
  6255. try
  6256. {
  6257. DirView(osCurrent)->ExecuteCurrentFile();
  6258. }
  6259. __finally
  6260. {
  6261. FForceExecution = false;
  6262. }
  6263. }
  6264. //---------------------------------------------------------------------------
  6265. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6266. {
  6267. TEditorData ExternalEditor;
  6268. ExternalEditor.Editor = edExternal;
  6269. bool Remember = false;
  6270. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6271. {
  6272. if (Remember)
  6273. {
  6274. TEditorList * EditorList = new TEditorList;
  6275. try
  6276. {
  6277. *EditorList = *WinConfiguration->EditorList;
  6278. bool Found = false;
  6279. int i = 0;
  6280. while (!Found && (i < EditorList->Count))
  6281. {
  6282. const TEditorPreferences * Editor = EditorList->Editors[i];
  6283. if ((Editor->Data->Editor == edExternal) &&
  6284. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6285. {
  6286. Found = true;
  6287. }
  6288. i++;
  6289. }
  6290. if (!Found)
  6291. {
  6292. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6293. WinConfiguration->EditorList = EditorList;
  6294. }
  6295. }
  6296. __finally
  6297. {
  6298. delete EditorList;
  6299. }
  6300. }
  6301. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6302. }
  6303. }
  6304. //---------------------------------------------------------------------------
  6305. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6306. {
  6307. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6308. if (FFileFindTerminal == ATerminal)
  6309. {
  6310. FFileFindTerminal = NULL;
  6311. HideFileFindDialog();
  6312. }
  6313. if (FClipboardTerminal == ATerminal)
  6314. {
  6315. ClipboardClear(); // implies ClipboardStop
  6316. }
  6317. }
  6318. //---------------------------------------------------------------------------
  6319. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6320. {
  6321. DetachTerminal(Sender);
  6322. }
  6323. //---------------------------------------------------------------------------
  6324. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6325. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6326. {
  6327. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6328. DebugAssert(Terminal != NULL);
  6329. if (Data->Terminal == Terminal)
  6330. {
  6331. Data->Terminal = NULL;
  6332. }
  6333. }
  6334. //---------------------------------------------------------------------------
  6335. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6336. {
  6337. return NULL;
  6338. }
  6339. //---------------------------------------------------------------------------
  6340. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6341. {
  6342. UpdateControls();
  6343. SessionColor = TColor(0);
  6344. UpdateRemotePathComboBox(false);
  6345. NeedSession(false);
  6346. }
  6347. //---------------------------------------------------------------------------
  6348. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6349. {
  6350. try
  6351. {
  6352. // Cache, as the login dialog can change its value.
  6353. // See also DoShow. Also see TTerminalManager::NewSession.
  6354. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6355. try
  6356. {
  6357. if (ShowLogin)
  6358. {
  6359. bool ReloadSessions = !Startup;
  6360. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6361. }
  6362. }
  6363. __finally
  6364. {
  6365. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6366. // (so there was no chance for the user to open any session yet)
  6367. if ((ShowLogin || !Startup) &&
  6368. !WinConfiguration->KeepOpenWhenNoSession &&
  6369. // this rules out the implicit Commander's local-local browser
  6370. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6371. {
  6372. TerminateApplication();
  6373. }
  6374. }
  6375. }
  6376. catch (EAbort &)
  6377. {
  6378. // swallow
  6379. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6380. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6381. }
  6382. }
  6383. //---------------------------------------------------------------------------
  6384. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6385. {
  6386. TTerminalManager * Manager = TTerminalManager::Instance();
  6387. DebugAssert(!Manager->Updating);
  6388. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6389. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6390. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6391. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6392. try
  6393. {
  6394. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6395. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6396. {
  6397. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6398. }
  6399. for (int Index = 0; Index <= Manager->Count; Index++)
  6400. {
  6401. TThemeTabSheet * TabSheet;
  6402. if (Index >= SessionsPageControl->PageCount)
  6403. {
  6404. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6405. TabSheet->PageControl = SessionsPageControl;
  6406. }
  6407. else
  6408. {
  6409. TabSheet = SessionsPageControl->Pages[Index];
  6410. }
  6411. bool IsSessionTab = (Index < Manager->Count);
  6412. if (IsSessionTab)
  6413. {
  6414. TTerminal * Terminal = Manager->Sessions[Index];
  6415. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6416. TabSheet->CaptionTruncation = tttEllipsis;
  6417. UpdateSessionTab(TabSheet);
  6418. }
  6419. else
  6420. {
  6421. TabSheet->Tag = 0; // not really needed
  6422. TabSheet->Shadowed = false;
  6423. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6424. TabSheet->CaptionTruncation = tttNoText;
  6425. // 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)
  6426. UpdateNewTabTab();
  6427. }
  6428. }
  6429. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6430. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6431. {
  6432. SessionsPageControl->UpdateTabsCaptionTruncation();
  6433. }
  6434. }
  6435. __finally
  6436. {
  6437. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6438. }
  6439. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6440. }
  6441. //---------------------------------------------------------------------------
  6442. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6443. {
  6444. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6445. }
  6446. //---------------------------------------------------------------------------
  6447. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6448. {
  6449. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6450. UnicodeString TabCaption;
  6451. if (WinConfiguration->SelectiveToolbarText)
  6452. {
  6453. TabCaption = GetNewTabTabCaption();
  6454. }
  6455. TabSheet->BaseCaption = TabCaption;
  6456. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6457. }
  6458. //---------------------------------------------------------------------------
  6459. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6460. {
  6461. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6462. }
  6463. //---------------------------------------------------------------------------
  6464. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6465. {
  6466. DebugFail();
  6467. return UnicodeString();
  6468. }
  6469. //---------------------------------------------------------------------------
  6470. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6471. {
  6472. if (DebugAlwaysTrue(TabSheet != NULL))
  6473. {
  6474. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6475. if (DebugAlwaysTrue(ASession != NULL))
  6476. {
  6477. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6478. if (ASession->LocalBrowser)
  6479. {
  6480. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6481. }
  6482. else
  6483. {
  6484. TabSheet->ImageIndex = AddSessionColor(Color);
  6485. }
  6486. TTerminalManager * Manager = TTerminalManager::Instance();
  6487. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6488. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6489. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6490. }
  6491. }
  6492. }
  6493. //---------------------------------------------------------------------------
  6494. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6495. {
  6496. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6497. }
  6498. //---------------------------------------------------------------------------
  6499. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6500. {
  6501. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6502. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6503. bool Result = (Session != NULL);
  6504. if (Result)
  6505. {
  6506. TTerminalManager::Instance()->ActiveSession = Session;
  6507. }
  6508. else
  6509. {
  6510. try
  6511. {
  6512. NewTab();
  6513. }
  6514. __finally
  6515. {
  6516. SessionListChanged();
  6517. }
  6518. FSessionsPageControlNewTabTime = Now();
  6519. }
  6520. return Result;
  6521. }
  6522. //---------------------------------------------------------------------------
  6523. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6524. {
  6525. SessionTabSwitched();
  6526. }
  6527. //---------------------------------------------------------------------------
  6528. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6529. {
  6530. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6531. DebugAssert(TransferList != NULL);
  6532. UnicodeString Name;
  6533. if (TransferList->ItemIndex <= 0)
  6534. {
  6535. Name = L"";
  6536. }
  6537. else
  6538. {
  6539. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6540. }
  6541. if (FCopyParamAutoSelected.IsEmpty())
  6542. {
  6543. // if previous preset was not autoselected, make new preset the "default"
  6544. FCopyParamDefault = Name;
  6545. }
  6546. GUIConfiguration->CopyParamCurrent = Name;
  6547. }
  6548. //---------------------------------------------------------------------------
  6549. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6550. {
  6551. // sanity check
  6552. bool ExistingPreset =
  6553. (FTransferListHoverIndex >= 0) &&
  6554. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6555. DebugAssert(ExistingPreset);
  6556. if (ExistingPreset)
  6557. {
  6558. HDC DC = GetDC(0);
  6559. TCanvas * Canvas = new TCanvas();
  6560. try
  6561. {
  6562. Canvas->Handle = DC;
  6563. Canvas->Font = GetToolbarFont(this);
  6564. UnicodeString Name;
  6565. if (FTransferListHoverIndex == 0)
  6566. {
  6567. Name = L"";
  6568. }
  6569. else
  6570. {
  6571. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6572. }
  6573. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6574. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6575. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6576. static_cast<TObject*>(GetComponent(fcTransferList)));
  6577. UnicodeString InfoStr =
  6578. GUIConfiguration->CopyParamPreset[Name].
  6579. GetInfoStr(L"; ",
  6580. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6581. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6582. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6583. {
  6584. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6585. {
  6586. InfoStr.SetLength(InfoStr.Length() - 1);
  6587. }
  6588. InfoStr += Ellipsis;
  6589. }
  6590. // UpdateCaption does not cause invalidation of whole submenu, while
  6591. // setting Caption property does.
  6592. // also it probably does not resize the label, even if necessary
  6593. // (we do not want that anyway)
  6594. TransferLabel->UpdateCaption(InfoStr);
  6595. }
  6596. __finally
  6597. {
  6598. Canvas->Handle = NULL;
  6599. ReleaseDC(0, DC);
  6600. delete Canvas;
  6601. }
  6602. }
  6603. }
  6604. //---------------------------------------------------------------------------
  6605. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6606. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6607. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6608. {
  6609. if (FTransferListHoverIndex != AHoverIndex)
  6610. {
  6611. FTransferListHoverIndex = AHoverIndex;
  6612. UpdateTransferLabel();
  6613. }
  6614. }
  6615. //---------------------------------------------------------------------------
  6616. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6617. {
  6618. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6619. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6620. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6621. {
  6622. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6623. {
  6624. if (!NonVisualDataModule->Busy)
  6625. {
  6626. OpenDirectory(GetSide(osCurrent));
  6627. }
  6628. Message.Result = 1;
  6629. }
  6630. else
  6631. {
  6632. TForm::Dispatch(&Message);
  6633. }
  6634. }
  6635. else
  6636. {
  6637. TForm::Dispatch(&Message);
  6638. }
  6639. }
  6640. //---------------------------------------------------------------------------
  6641. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6642. {
  6643. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6644. if ((FIgnoreNextDialogChar != 0) &&
  6645. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6646. {
  6647. Message.Result = 1;
  6648. }
  6649. else
  6650. {
  6651. TForm::Dispatch(&Message);
  6652. }
  6653. FIgnoreNextDialogChar = 0;
  6654. }
  6655. //---------------------------------------------------------------------------
  6656. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6657. {
  6658. // The four low-order bits are used internally by Windows
  6659. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6660. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6661. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6662. // 61730 - restore thru double click - undocumented
  6663. // 61490 - maximize thru double click - undocumented
  6664. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6665. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6666. {
  6667. SysResizing(Cmd);
  6668. }
  6669. TForm::Dispatch(&Message);
  6670. }
  6671. //---------------------------------------------------------------------------
  6672. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6673. {
  6674. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6675. // Also there no point returning TRUE as we are not able to
  6676. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6677. // Hence the process termination might be safer :)
  6678. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6679. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6680. {
  6681. Message.Result = FALSE;
  6682. }
  6683. else
  6684. {
  6685. Message.Result = TRUE;
  6686. }
  6687. // Do not call default handling as that triggers OnCloseQuery,
  6688. // where our implementation will popup confirmation dialogs, what we do not want,
  6689. // as per Vista guidelines:
  6690. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6691. }
  6692. //---------------------------------------------------------------------------
  6693. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6694. {
  6695. if (Message.EndSession && IsApplicationMinimized())
  6696. {
  6697. // WORKAROUND
  6698. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6699. // when it receives WM_ENDSESSION,
  6700. // but that sometimes (particularly when application is minimized) cause crashes.
  6701. // Crash popups message that blocks log off.
  6702. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6703. // so we call ExitProcess() immediately, not even trying to cleanup.
  6704. // It still causes beep, so there's likely some popup, but it does not block
  6705. // log off.
  6706. ExitProcess(0);
  6707. }
  6708. TForm::Dispatch(&Message);
  6709. }
  6710. #ifdef _DEBUG
  6711. //---------------------------------------------------------------------------
  6712. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6713. {
  6714. TForm::Dispatch(&Message);
  6715. }
  6716. #endif
  6717. //---------------------------------------------------------------------------
  6718. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6719. {
  6720. }
  6721. //---------------------------------------------------------------------------
  6722. void __fastcall TCustomScpExplorerForm::DoShow()
  6723. {
  6724. // Called from TCustomScpExplorerForm::CMShowingChanged
  6725. // only now are the controls resized finally, so the size constraints
  6726. // will not conflict with possibly very small window size
  6727. RestoreFormParams();
  6728. // This indicates that the form was rescaled after parameters were loaded and
  6729. // as most probably the parameters were actually saved with the current form scaling
  6730. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6731. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6732. // and no rounding problems will occur.
  6733. // See also RestoreForm.
  6734. // (we should be safe to call RestoreParams unconditionally)
  6735. if (PixelsPerInch != Screen->PixelsPerInch)
  6736. {
  6737. RestoreParams();
  6738. }
  6739. FixControlsPlacement();
  6740. if (Position == poDefaultPosOnly)
  6741. {
  6742. CutFormToDesktop(this);
  6743. }
  6744. TForm::DoShow();
  6745. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6746. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6747. // This was previously in NeedSession, where other related code is.
  6748. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6749. // unpleasanly shows briefly.
  6750. if ((ManagedSession == NULL) &&
  6751. !WinConfiguration->ShowLoginWhenNoSession &&
  6752. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6753. // This detects if workspace was saved the last time the main widow was closed
  6754. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6755. {
  6756. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6757. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6758. }
  6759. // Before main window displays
  6760. if (ManagedSession == NULL)
  6761. {
  6762. StartingWithoutSession();
  6763. }
  6764. UpdatePixelsPerInchMainWindowCounter();
  6765. FShowing = true;
  6766. }
  6767. //---------------------------------------------------------------------------
  6768. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6769. {
  6770. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6771. }
  6772. //---------------------------------------------------------------------------
  6773. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6774. {
  6775. SessionListChanged();
  6776. InitStatusBar();
  6777. UpdateControls();
  6778. }
  6779. //---------------------------------------------------------------------------
  6780. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6781. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6782. TNotifyEvent OnBalloonClick, TObject * UserData)
  6783. {
  6784. bool Do;
  6785. UnicodeString Message;
  6786. if (E == NULL)
  6787. {
  6788. Message = Str;
  6789. Do = true;
  6790. }
  6791. else
  6792. {
  6793. Do = ExceptionMessage(E, Message);
  6794. }
  6795. if (Do && WinConfiguration->BalloonNotifications)
  6796. {
  6797. UnicodeString Title;
  6798. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6799. {
  6800. Message = TrimLeft(Message);
  6801. }
  6802. else
  6803. {
  6804. Message = UnformatMessage(Message);
  6805. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6806. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6807. Title = LoadResourceString(Captions[Type]);
  6808. if (Terminal != NULL)
  6809. {
  6810. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6811. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6812. }
  6813. }
  6814. if (Seconds == 0)
  6815. {
  6816. Seconds = WinConfiguration->NotificationsTimeout;
  6817. }
  6818. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6819. }
  6820. }
  6821. //---------------------------------------------------------------------------
  6822. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6823. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6824. UnicodeString HelpKeyword, const TMessageParams * Params,
  6825. TTerminal * Terminal)
  6826. {
  6827. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6828. (FOnFeedSynchronizeError != NULL)) &&
  6829. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6830. {
  6831. if (FOnFeedSynchronizeError != NULL)
  6832. {
  6833. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6834. }
  6835. else
  6836. {
  6837. DebugAssert(FErrorList != NULL);
  6838. TStringList * MoreMessagesCopy = NULL;
  6839. if (MoreMessages)
  6840. {
  6841. MoreMessagesCopy = new TStringList();
  6842. MoreMessagesCopy->Assign(MoreMessages);
  6843. }
  6844. FErrorList->AddObject(Message, MoreMessagesCopy);
  6845. }
  6846. PopupTrayBalloon(Terminal, Message, Type);
  6847. return ContinueAnswer(Answers);
  6848. }
  6849. else
  6850. {
  6851. bool UseBalloon = FTrayIcon->Visible;
  6852. if (UseBalloon)
  6853. {
  6854. PopupTrayBalloon(Terminal, Message, Type);
  6855. }
  6856. unsigned int Result;
  6857. try
  6858. {
  6859. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6860. }
  6861. __finally
  6862. {
  6863. // cancel only balloon we popped up, otherwise we may cancel notification
  6864. // balloon that was there before the message dialog
  6865. // (such as "dd confirmation opt in balloon)
  6866. if (UseBalloon)
  6867. {
  6868. FTrayIcon->CancelBalloon();
  6869. }
  6870. }
  6871. return Result;
  6872. }
  6873. }
  6874. //---------------------------------------------------------------------------
  6875. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6876. TTerminal * Terminal, Exception * E)
  6877. {
  6878. if (FTrayIcon->Visible)
  6879. {
  6880. PopupTrayBalloon(Terminal, L"", qtError, E);
  6881. }
  6882. // particularly prevent opening new session from jump list,
  6883. // while exception is shown
  6884. NonVisualDataModule->StartBusy();
  6885. try
  6886. {
  6887. ShowExtendedExceptionEx(Terminal, E);
  6888. }
  6889. __finally
  6890. {
  6891. NonVisualDataModule->EndBusy();
  6892. FTrayIcon->CancelBalloon();
  6893. }
  6894. }
  6895. //---------------------------------------------------------------------------
  6896. void __fastcall TCustomScpExplorerForm::SessionReady()
  6897. {
  6898. InitStatusBar();
  6899. // cannot rely on active page being page for active terminal,
  6900. // as it can happen that active page is the "new session" page
  6901. // (e.g. when reconnecting active terminal, while login dialog
  6902. // invoked from "new session" page is modal)
  6903. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  6904. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  6905. }
  6906. //---------------------------------------------------------------------------
  6907. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6908. TTerminal * Terminal, Exception * E)
  6909. {
  6910. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6911. if (!Terminal->Active)
  6912. {
  6913. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6914. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6915. {
  6916. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6917. }
  6918. }
  6919. }
  6920. //---------------------------------------------------------------------------
  6921. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6922. UnicodeString Message, TQueryType Type,
  6923. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6924. {
  6925. if ((E == NULL) ||
  6926. ExceptionMessage(E, Message))
  6927. {
  6928. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6929. if (Important)
  6930. {
  6931. Seconds *= 5;
  6932. }
  6933. UnicodeString NoteMessage(UnformatMessage(Message));
  6934. if (Terminal != NULL)
  6935. {
  6936. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6937. NoteMessage = FORMAT(L"%s: %s",
  6938. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  6939. }
  6940. if (WinConfiguration->BalloonNotifications)
  6941. {
  6942. AddNote(NoteMessage);
  6943. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6944. }
  6945. else
  6946. {
  6947. FlashOnBackground();
  6948. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6949. }
  6950. }
  6951. }
  6952. //---------------------------------------------------------------------------
  6953. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6954. {
  6955. RestoreApp();
  6956. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6957. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6958. !NonVisualDataModule->Busy)
  6959. {
  6960. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6961. TTerminalManager::Instance()->ActiveSession = Terminal;
  6962. if (!ComponentVisible[fcQueueView])
  6963. {
  6964. ToggleQueueVisibility();
  6965. GoToQueue();
  6966. }
  6967. }
  6968. }
  6969. //---------------------------------------------------------------------------
  6970. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6971. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6972. {
  6973. UnicodeString Message;
  6974. TNotifyEvent OnClick = NULL;
  6975. TObject * UserData = NULL;
  6976. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6977. switch (Event)
  6978. {
  6979. case qeEmptyButMonitored:
  6980. if ((ATerminal != Terminal) || QueueInvisible)
  6981. {
  6982. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6983. OnClick = QueueEmptyNoteClicked;
  6984. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6985. TerminalNoteData->Terminal = ATerminal;
  6986. UserData = TerminalNoteData;
  6987. }
  6988. break;
  6989. case qeEmpty:
  6990. OperationComplete(ATerminal->QueueOperationStart);
  6991. break;
  6992. case qePendingUserAction:
  6993. if ((ATerminal != Terminal) ||
  6994. (QueueInvisible && !IsQueueAutoPopup()))
  6995. {
  6996. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6997. }
  6998. break;
  6999. default:
  7000. DebugFail();
  7001. }
  7002. if (!Message.IsEmpty())
  7003. {
  7004. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7005. }
  7006. }
  7007. //---------------------------------------------------------------------------
  7008. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7009. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7010. {
  7011. if (FDDExtMapFile != NULL)
  7012. {
  7013. CloseHandle(FDDExtMapFile);
  7014. FDDExtMapFile = NULL;
  7015. }
  7016. if (WinConfiguration->DDFakeFile)
  7017. {
  7018. DDFakeFileInitDrag(FileList, Created);
  7019. }
  7020. }
  7021. //---------------------------------------------------------------------------
  7022. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7023. {
  7024. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7025. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7026. {
  7027. FFakeFileDropTarget = FileName;
  7028. }
  7029. }
  7030. //---------------------------------------------------------------------------
  7031. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7032. {
  7033. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7034. if (!ForceDirectories(ApiPath(Result)))
  7035. {
  7036. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7037. }
  7038. FileSetAttr(ApiPath(Result), faHidden);
  7039. return Result;
  7040. }
  7041. //---------------------------------------------------------------------------
  7042. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7043. bool & Created)
  7044. {
  7045. FFakeFileDropTarget = UnicodeString();
  7046. FDragFakeDirectory = CreateFakeTransferDirectory();
  7047. FileList->AddItem(NULL, FDragFakeDirectory);
  7048. Created = true;
  7049. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7050. {
  7051. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7052. }
  7053. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7054. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7055. {
  7056. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7057. TDragExtCommStruct* CommStruct;
  7058. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7059. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7060. DebugAssert(CommStruct != NULL);
  7061. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7062. CommStruct->Dragging = true;
  7063. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7064. LENOF(CommStruct->DropDest));
  7065. NULL_TERMINATE(CommStruct->DropDest);
  7066. UnmapViewOfFile(CommStruct);
  7067. }
  7068. }
  7069. //---------------------------------------------------------------------------
  7070. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7071. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7072. {
  7073. bool Result;
  7074. DebugAssert(!IsLocalBrowserMode());
  7075. if (Sender == RemoteDirView)
  7076. {
  7077. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7078. }
  7079. else
  7080. {
  7081. DebugAssert(Sender == RemoteDriveView);
  7082. TStrings * FileList = RemoteDriveView->DragFileList();
  7083. try
  7084. {
  7085. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7086. }
  7087. __finally
  7088. {
  7089. delete FileList;
  7090. }
  7091. }
  7092. if (FDDTargetControl == RemoteDriveView)
  7093. {
  7094. RemoteDriveView->UpdateDropTarget();
  7095. }
  7096. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7097. // or from tree view to dir view.
  7098. // foMove happens when file/dir is dragged from remote tree view outside of
  7099. // application via dragex
  7100. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7101. (DropSourceControl == RemoteDriveView))
  7102. {
  7103. RemoteDriveView->UpdateDropSource();
  7104. }
  7105. return Result;
  7106. }
  7107. //---------------------------------------------------------------------------
  7108. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7109. {
  7110. DebugAssert(!IsLocalBrowserMode());
  7111. // This also handles drops of remote files to queue.
  7112. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7113. SAFE_DESTROY(FDDFileList);
  7114. if (!FFakeFileDropTarget.IsEmpty())
  7115. {
  7116. RemoveDir(ApiPath(FFakeFileDropTarget));
  7117. }
  7118. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7119. {
  7120. try
  7121. {
  7122. TDragResult DDResult = (Sender == RemoteDirView) ?
  7123. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7124. // Focus is moved to the target application,
  7125. // but as we are going to present the UI, we need to steal the focus back.
  7126. // This most likely won't work though (windows does not allow application
  7127. // to steal focus most of the time)
  7128. Application->BringToFront();
  7129. // On older version of Windows we never got drMove here, see also comment below.
  7130. // On Windows 10, we get the "move".
  7131. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7132. {
  7133. UnicodeString TargetDirectory;
  7134. TFileOperation Operation;
  7135. // drInvalid may mean drMove, see comment below
  7136. switch (DDResult)
  7137. {
  7138. case drCopy:
  7139. Operation = foCopy;
  7140. break;
  7141. case drMove:
  7142. Operation = foMove;
  7143. break;
  7144. default:
  7145. DebugFail();
  7146. case drInvalid:
  7147. // prefer "copy" for safety
  7148. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7149. break;
  7150. }
  7151. TTransferOperationParam Param;
  7152. UnicodeString CounterName;
  7153. bool ForceQueue;
  7154. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7155. {
  7156. // we get drInvalid both if d&d was intercepted by ddext,
  7157. // and when users drops on no-drop location.
  7158. // we tell the difference by existence of response from ddext,
  7159. // so we ignore absence of response in this case
  7160. if (DDResult != drInvalid)
  7161. {
  7162. // here we know that the extension is installed,
  7163. // as it is checked as soon as drag&drop starts from
  7164. // RemoteFileControlDDCreateDragFileList
  7165. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7166. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7167. {
  7168. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7169. }
  7170. else
  7171. {
  7172. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7173. }
  7174. }
  7175. }
  7176. else
  7177. {
  7178. // download using fake file
  7179. Param.Temp = false;
  7180. Param.DragDrop = true;
  7181. if (ForceQueue)
  7182. {
  7183. Param.Queue = asOn;
  7184. }
  7185. if (Sender == RemoteDirView)
  7186. {
  7187. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7188. }
  7189. if (RemoteFileControlFileOperation(Sender, Operation,
  7190. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7191. {
  7192. Configuration->Usage->Inc(CounterName);
  7193. }
  7194. }
  7195. }
  7196. }
  7197. __finally
  7198. {
  7199. CloseHandle(FDDExtMapFile);
  7200. FDDExtMapFile = NULL;
  7201. delete FDragFakeMonitors;
  7202. FDragFakeMonitors = NULL;
  7203. RemoveDir(ApiPath(FDragFakeDirectory));
  7204. FDragFakeDirectory = L"";
  7205. FFakeFileDropTarget = UnicodeString();
  7206. }
  7207. }
  7208. if (FDragDropSshTerminate.get() != NULL)
  7209. {
  7210. FDragDropSshTerminate->Rethrow();
  7211. }
  7212. }
  7213. //---------------------------------------------------------------------------
  7214. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7215. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7216. {
  7217. // Remember drop effect so we know (when user drops files), if we copy or move
  7218. FLastDropEffect = dwEffect;
  7219. }
  7220. //---------------------------------------------------------------------------
  7221. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7222. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7223. {
  7224. bool Result;
  7225. if (FDDTargetControl == QueueView3)
  7226. {
  7227. Directory = DefaultDownloadTargetDirectory();
  7228. Result = true;
  7229. CounterName = L"DownloadsDragDropQueue";
  7230. ForceQueue = true;
  7231. }
  7232. else if (!FFakeFileDropTarget.IsEmpty())
  7233. {
  7234. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7235. Result = true;
  7236. ForceQueue = false;
  7237. CounterName = L"DownloadsDragDropFakeFile";
  7238. }
  7239. else
  7240. {
  7241. ForceQueue = false;
  7242. Enabled = false;
  7243. try
  7244. {
  7245. int Timer = 0;
  7246. Result = false;
  7247. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7248. {
  7249. {
  7250. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7251. TDragExtCommStruct* CommStruct;
  7252. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7253. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7254. DebugAssert(CommStruct != NULL);
  7255. Result = !CommStruct->Dragging;
  7256. if (Result)
  7257. {
  7258. Directory = ExtractFilePath(CommStruct->DropDest);
  7259. CounterName = L"DownloadsDragDropExternalExt";
  7260. }
  7261. UnmapViewOfFile(CommStruct);
  7262. }
  7263. if (!Result)
  7264. {
  7265. Sleep(50);
  7266. Timer += 50;
  7267. Application->ProcessMessages();
  7268. }
  7269. }
  7270. }
  7271. __finally
  7272. {
  7273. Enabled = true;
  7274. }
  7275. }
  7276. return Result;
  7277. }
  7278. //---------------------------------------------------------------------------
  7279. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7280. const UnicodeString TempDir, int SecDelay)
  7281. {
  7282. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7283. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7284. if (FDelayedDeletionTimer == NULL)
  7285. {
  7286. DebugAssert(HandleAllocated());
  7287. FDelayedDeletionTimer = new TTimer(this);
  7288. FDelayedDeletionTimer->Interval = 10000;
  7289. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7290. }
  7291. else
  7292. {
  7293. FDelayedDeletionTimer->Enabled = true;
  7294. }
  7295. }
  7296. //---------------------------------------------------------------------------
  7297. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7298. {
  7299. DebugAssert(FDelayedDeletionList != NULL);
  7300. TDateTime N = Now();
  7301. TDateTime Alert;
  7302. UnicodeString Directory;
  7303. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7304. {
  7305. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7306. if ((N >= Alert) || (Sender == NULL))
  7307. {
  7308. Directory = FDelayedDeletionList->Strings[Index];
  7309. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7310. {
  7311. FDelayedDeletionList->Delete(Index);
  7312. }
  7313. }
  7314. }
  7315. if (FDelayedDeletionList->Count == 0)
  7316. {
  7317. FDelayedDeletionTimer->Enabled = false;
  7318. }
  7319. }
  7320. //---------------------------------------------------------------------------
  7321. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7322. {
  7323. DebugAssert(!IsLocalBrowserMode());
  7324. if (IsFileControl(FDDTargetControl, osRemote) ||
  7325. (FDDTargetControl == SessionsPageControl))
  7326. {
  7327. TManagedTerminal * TargetTerminal = NULL;
  7328. TFileOperation Operation = foNone;
  7329. if (FDDTargetControl == SessionsPageControl)
  7330. {
  7331. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7332. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7333. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7334. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7335. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7336. {
  7337. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7338. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7339. {
  7340. Operation = foRemoteMove;
  7341. }
  7342. else
  7343. {
  7344. Operation = foRemoteCopy;
  7345. }
  7346. }
  7347. }
  7348. else
  7349. {
  7350. if (FLastDropEffect == DROPEFFECT_MOVE)
  7351. {
  7352. Operation = foRemoteMove;
  7353. }
  7354. else if (FLastDropEffect == DROPEFFECT_COPY)
  7355. {
  7356. Operation = foRemoteCopy;
  7357. }
  7358. }
  7359. if (Operation != foNone)
  7360. {
  7361. RemoteFileControlFileOperation(DropSourceControl,
  7362. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7363. }
  7364. // abort drag&drop
  7365. Abort();
  7366. }
  7367. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7368. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7369. (FDDTargetControl != QueueView3))
  7370. {
  7371. DebugAssert(!FDragTempDir.IsEmpty());
  7372. TTransferType Type;
  7373. UnicodeString TempDir = FDragTempDir;
  7374. // We clear FDragTempDir before calling
  7375. // just in case it fail (raises exception)
  7376. FDragTempDir = L"";
  7377. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7378. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7379. // empty directory parameter means temp directory -> don't display it!
  7380. UnicodeString TargetDir = L"";
  7381. int Options = 0;
  7382. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7383. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7384. {
  7385. Abort();
  7386. }
  7387. // TargetDir is set when dropped on local file control
  7388. // (this was workaround for legacy dirview event handling, now it should be
  7389. // made prettier)
  7390. if (TargetDir.IsEmpty())
  7391. {
  7392. TargetDir = TempDir;
  7393. if (ForceDirectories(ApiPath(TargetDir)))
  7394. {
  7395. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7396. FPendingTempSpaceWarn = true;
  7397. try
  7398. {
  7399. FDragDropOperation = true;
  7400. int Params = cpTemporary |
  7401. (Type == ttMove ? cpDelete : 0);
  7402. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7403. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7404. }
  7405. __finally
  7406. {
  7407. FDragDropOperation = false;
  7408. FPendingTempSpaceWarn = false;
  7409. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7410. }
  7411. }
  7412. else
  7413. {
  7414. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7415. }
  7416. }
  7417. }
  7418. }
  7419. //---------------------------------------------------------------------------
  7420. void __fastcall TCustomScpExplorerForm::DDDownload(
  7421. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7422. {
  7423. DebugAssert(!IsLocalBrowserMode());
  7424. TAutoBatch AutoBatch(this);
  7425. UpdateCopyParamCounters(*CopyParam);
  7426. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7427. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7428. {
  7429. RemoteDriveView->UpdateDropSource();
  7430. }
  7431. }
  7432. //---------------------------------------------------------------------------
  7433. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7434. {
  7435. public:
  7436. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7437. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7438. {
  7439. }
  7440. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7441. {
  7442. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7443. TDataObjectFilesEx::AllowData(FormatEtc);
  7444. }
  7445. };
  7446. //---------------------------------------------------------------------------
  7447. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7448. TObject * Sender, TDataObject *& DataObject)
  7449. {
  7450. if (FDDExtMapFile != NULL)
  7451. {
  7452. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7453. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7454. {
  7455. Abort();
  7456. }
  7457. if (FileList->Count > 0)
  7458. {
  7459. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7460. if (!FilesObject->IsValid(true, true))
  7461. {
  7462. FilesObject->_Release();
  7463. }
  7464. else
  7465. {
  7466. DataObject = FilesObject;
  7467. }
  7468. }
  7469. }
  7470. }
  7471. //---------------------------------------------------------------------------
  7472. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7473. {
  7474. DebugFail();
  7475. }
  7476. //---------------------------------------------------------------------------
  7477. void __fastcall TCustomScpExplorerForm::GoToTree()
  7478. {
  7479. ComponentVisible[fcRemoteTree] = true;
  7480. DriveView(osRemote)->SetFocus();
  7481. }
  7482. //---------------------------------------------------------------------------
  7483. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7484. TPanelExport Export)
  7485. {
  7486. TCustomDirView * DirView = this->DirView(Side);
  7487. std::unique_ptr<TStrings> ExportData(new TStringList());
  7488. switch (Export)
  7489. {
  7490. case pePath:
  7491. ExportData->Add(DirView->PathName);
  7492. break;
  7493. case peFileList:
  7494. case peFullFileList:
  7495. {
  7496. bool FullPath = (Export == peFullFileList);
  7497. DirView->CreateFileList(false, FullPath, ExportData.get());
  7498. for (int Index = 0; Index < ExportData->Count; Index++)
  7499. {
  7500. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7501. {
  7502. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7503. }
  7504. }
  7505. }
  7506. break;
  7507. default:
  7508. DebugFail();
  7509. }
  7510. return ExportData.release();
  7511. }
  7512. //---------------------------------------------------------------------------
  7513. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7514. TPanelExport Export, TPanelExportDestination Destination)
  7515. {
  7516. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7517. PanelExportStore(Side, Export, Destination, ExportData.get());
  7518. }
  7519. //---------------------------------------------------------------------------
  7520. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7521. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7522. TStrings * ExportData)
  7523. {
  7524. if (Destination == pedClipboard)
  7525. {
  7526. TInstantOperationVisualizer Visualizer;
  7527. CopyToClipboard(ExportData);
  7528. }
  7529. else
  7530. {
  7531. DebugFail();
  7532. }
  7533. }
  7534. //---------------------------------------------------------------------------
  7535. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7536. {
  7537. TCustomDirView * DirView = this->DirView(Side);
  7538. UnicodeString Mask = DirView->Mask;
  7539. if (DoFilterMaskDialog(DirView, Mask))
  7540. {
  7541. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7542. Configuration->Usage->Inc(L"Filters");
  7543. }
  7544. }
  7545. //---------------------------------------------------------------------------
  7546. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7547. {
  7548. return FQueueController->DefaultOperation();
  7549. }
  7550. //---------------------------------------------------------------------------
  7551. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7552. TQueueOperation Operation, void ** Param)
  7553. {
  7554. switch (Operation)
  7555. {
  7556. case qoPreferences:
  7557. return true;
  7558. case qoGoTo:
  7559. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7560. case qoOnceEmpty:
  7561. return IsAnythingQueued();
  7562. default:
  7563. return FQueueController->AllowOperation(Operation, Param);
  7564. }
  7565. }
  7566. //---------------------------------------------------------------------------
  7567. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7568. {
  7569. if (DebugAlwaysTrue(QueueView3->Visible))
  7570. {
  7571. QueueView3->SetFocus();
  7572. }
  7573. }
  7574. //---------------------------------------------------------------------------
  7575. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7576. TQueueOperation Operation, void * Param)
  7577. {
  7578. if (Operation == qoGoTo)
  7579. {
  7580. GoToQueue();
  7581. }
  7582. else if (Operation == qoPreferences)
  7583. {
  7584. PreferencesDialog(pmQueue);
  7585. }
  7586. else
  7587. {
  7588. FQueueController->ExecuteOperation(Operation, Param);
  7589. }
  7590. }
  7591. //---------------------------------------------------------------------------
  7592. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7593. {
  7594. return (Queue != NULL) && Queue->Enabled;
  7595. }
  7596. //---------------------------------------------------------------------------
  7597. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7598. {
  7599. DebugAssert(Queue != NULL);
  7600. if (Queue != NULL)
  7601. {
  7602. Queue->Enabled = !Queue->Enabled;
  7603. }
  7604. }
  7605. //---------------------------------------------------------------------------
  7606. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7607. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7608. {
  7609. FQueueActedItem = QueueView3->ItemFocused;
  7610. }
  7611. //---------------------------------------------------------------------------
  7612. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7613. {
  7614. return TopDock->Height + QueueSplitter->Height;
  7615. }
  7616. //---------------------------------------------------------------------------
  7617. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7618. {
  7619. return
  7620. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7621. (QueueDock->Visible ? QueueDock->Height : 0) +
  7622. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7623. }
  7624. //---------------------------------------------------------------------------
  7625. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7626. {
  7627. return ScaleByTextHeight(this, 48);
  7628. }
  7629. //---------------------------------------------------------------------------
  7630. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7631. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7632. {
  7633. // when queue is hidden by double-clicking splitter, stray attempt to
  7634. // resize the panel with strange value arrives, make sure it is ignored
  7635. if (ComponentVisible[fcQueueView])
  7636. {
  7637. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7638. if (NewSize > MaxHeightLimit)
  7639. {
  7640. NewSize = MaxHeightLimit;
  7641. }
  7642. int MinHeightLimit =
  7643. GetStaticQueuePanelComponentsHeight() +
  7644. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7645. GetMinQueueViewHeight();
  7646. if (NewSize < MinHeightLimit)
  7647. {
  7648. NewSize = MinHeightLimit;
  7649. }
  7650. }
  7651. else
  7652. {
  7653. Accept = false;
  7654. }
  7655. }
  7656. //---------------------------------------------------------------------------
  7657. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7658. {
  7659. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7660. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7661. if (QueueViewHeight < GetMinQueueViewHeight())
  7662. {
  7663. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7664. }
  7665. }
  7666. //---------------------------------------------------------------------------
  7667. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7668. TDragObject *& /*DragObject*/)
  7669. {
  7670. FQueueActedItem = QueueView3->ItemFocused;
  7671. }
  7672. //---------------------------------------------------------------------------
  7673. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7674. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7675. {
  7676. Accept = true;
  7677. if (Source == QueueView3)
  7678. {
  7679. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7680. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7681. if (Accept)
  7682. {
  7683. TQueueItemProxy * QueueItem;
  7684. TQueueItemProxy * DestQueueItem;
  7685. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7686. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7687. Accept = (QueueItem != DestQueueItem) &&
  7688. (QueueItem->Status == TQueueItem::qsPending) &&
  7689. (DestQueueItem->Status == TQueueItem::qsPending);
  7690. }
  7691. }
  7692. }
  7693. //---------------------------------------------------------------------------
  7694. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7695. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7696. {
  7697. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7698. {
  7699. TQueueItemProxy * QueueItem;
  7700. TQueueItemProxy * DestQueueItem;
  7701. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7702. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7703. QueueItem->Move(DestQueueItem);
  7704. }
  7705. }
  7706. //---------------------------------------------------------------------------
  7707. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7708. {
  7709. if ((QueueView3->ItemFocused == NULL) &&
  7710. (QueueView3->Items->Count > 0))
  7711. {
  7712. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7713. }
  7714. QueueLabelUpdateStatus();
  7715. }
  7716. //---------------------------------------------------------------------------
  7717. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7718. {
  7719. QueueLabelUpdateStatus();
  7720. }
  7721. //---------------------------------------------------------------------------
  7722. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7723. {
  7724. QueueLabel->UpdateStatus();
  7725. }
  7726. //---------------------------------------------------------------------------
  7727. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7728. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7729. {
  7730. if (Selected)
  7731. {
  7732. NonVisualDataModule->UpdateNonVisibleActions();
  7733. }
  7734. }
  7735. //---------------------------------------------------------------------------
  7736. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7737. {
  7738. TDragDropFilesEx * Result = NULL;
  7739. if (Sender == SessionsPageControl)
  7740. {
  7741. Result = FSessionsDragDropFilesEx;
  7742. }
  7743. else if (Sender == QueueView3)
  7744. {
  7745. Result = FQueueDragDropFilesEx;
  7746. }
  7747. else
  7748. {
  7749. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7750. if (DirView != NULL)
  7751. {
  7752. Result = DirView->DragDropFilesEx;
  7753. }
  7754. else
  7755. {
  7756. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7757. if (DriveView != NULL)
  7758. {
  7759. Result = DriveView->DragDropFilesEx;
  7760. }
  7761. }
  7762. }
  7763. DebugAssert(Result != NULL);
  7764. return Result;
  7765. }
  7766. //---------------------------------------------------------------------------
  7767. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7768. {
  7769. return (DView->SelCount == DView->FilesCount);
  7770. }
  7771. //---------------------------------------------------------------------------
  7772. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7773. {
  7774. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7775. }
  7776. //---------------------------------------------------------------------------
  7777. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7778. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7779. {
  7780. DebugAssert(!IsLocalBrowserMode());
  7781. TFileOperation Operation;
  7782. switch (Effect)
  7783. {
  7784. case DROPEFFECT_MOVE:
  7785. Operation = foMove;
  7786. break;
  7787. case DROPEFFECT_COPY:
  7788. // occures on WinXP (reported by user)
  7789. default:
  7790. Operation = foCopy;
  7791. break;
  7792. };
  7793. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7794. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7795. if (DragDropFilesEx->FileList->Count > 0)
  7796. {
  7797. TStrings * FileList = new TStringList();
  7798. try
  7799. {
  7800. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7801. {
  7802. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7803. }
  7804. FDragDropOperation = true;
  7805. TTransferOperationParam Param;
  7806. Param.TargetDirectory = TargetPath;
  7807. // upload, no temp dirs
  7808. Param.Temp = false;
  7809. Param.DragDrop = DragDrop;
  7810. Param.Options =
  7811. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7812. if (ForceQueue)
  7813. {
  7814. Param.Queue = asOn;
  7815. }
  7816. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7817. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7818. {
  7819. if (IsFileControl(DropSourceControl, osLocal))
  7820. {
  7821. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7822. }
  7823. else
  7824. {
  7825. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7826. }
  7827. }
  7828. }
  7829. __finally
  7830. {
  7831. FDragDropOperation = false;
  7832. delete FileList;
  7833. }
  7834. }
  7835. }
  7836. //---------------------------------------------------------------------------
  7837. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7838. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7839. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7840. {
  7841. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7842. }
  7843. //---------------------------------------------------------------------------
  7844. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7845. TObject * Sender, int grfKeyState, int & dwEffect)
  7846. {
  7847. DebugAssert(!IsLocalBrowserMode());
  7848. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7849. if ((dwEffect != DROPEFFECT_NONE) &&
  7850. IsFileControl(DropSourceControl, osRemote))
  7851. {
  7852. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7853. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7854. (RemoteDirView->DropTarget == NULL))
  7855. {
  7856. dwEffect = DROPEFFECT_NONE;
  7857. }
  7858. else
  7859. {
  7860. if (dwEffect == DROPEFFECT_COPY)
  7861. {
  7862. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  7863. // currently we support copying always (at least via temporary directory);
  7864. // remove associated checks once this all proves stable and working
  7865. bool CopyCapable = true;
  7866. // if we do not support neither of operations, there's no discussion
  7867. if (!MoveCapable && !CopyCapable)
  7868. {
  7869. dwEffect = DROPEFFECT_NONE;
  7870. }
  7871. else
  7872. {
  7873. // The default effect inside remote panel is move,
  7874. // unless we do not support it, but support copy
  7875. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7876. {
  7877. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7878. }
  7879. else
  7880. {
  7881. // with ctrl-down, we want copy unless it is not supported
  7882. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7883. }
  7884. }
  7885. }
  7886. }
  7887. }
  7888. }
  7889. //---------------------------------------------------------------------------
  7890. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7891. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7892. {
  7893. DebugAssert(!IsLocalBrowserMode());
  7894. if (FDDTotalSize >= 0)
  7895. {
  7896. if (File->IsDirectory)
  7897. {
  7898. FDDTotalSize = -1;
  7899. }
  7900. else
  7901. {
  7902. FDDTotalSize += File->Size;
  7903. }
  7904. }
  7905. DebugAssert(!FDragTempDir.IsEmpty());
  7906. // TODO: this is quite ineffective
  7907. // TODO: what if invalid character replacement is disabled?
  7908. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7909. UnicodeString TransferFileName;
  7910. if (Sender == RemoteDriveView)
  7911. {
  7912. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7913. }
  7914. else
  7915. {
  7916. TransferFileName = File->FileName;
  7917. }
  7918. FDDFileList->AddObject(TransferFileName, File);
  7919. }
  7920. //---------------------------------------------------------------------------
  7921. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7922. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7923. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7924. {
  7925. // sometimes we do not get DDEnd so the list is not released
  7926. SAFE_DESTROY(FDDFileList);
  7927. FDDFileList = new TStringList();
  7928. FDragTempDir = WinConfiguration->TemporaryDir();
  7929. FDDTotalSize = 0;
  7930. FDragDropSshTerminate.reset(NULL);
  7931. }
  7932. //---------------------------------------------------------------------------
  7933. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7934. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7935. HRESULT & Result)
  7936. {
  7937. if (Result == DRAGDROP_S_DROP)
  7938. {
  7939. try
  7940. {
  7941. GlobalDragImageList->HideDragImage();
  7942. try
  7943. {
  7944. RemoteFileControlDDTargetDrop();
  7945. }
  7946. catch(ESshTerminate & E)
  7947. {
  7948. FDragDropSshTerminate.reset(E.Clone());
  7949. }
  7950. }
  7951. catch (Exception &E)
  7952. {
  7953. // If downloading fails we need to cancel drag&drop, otherwise
  7954. // Explorer shows error
  7955. // But by the way exception probably never reach this point as
  7956. // it's catched on way.
  7957. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7958. Result = DRAGDROP_S_CANCEL;
  7959. ShowExtendedException(Terminal, &E);
  7960. }
  7961. }
  7962. }
  7963. //---------------------------------------------------------------------------
  7964. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7965. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7966. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7967. {
  7968. TFileMasks::TParams MaskParams;
  7969. MaskParams.Size = Size;
  7970. MaskParams.Modification = Modification;
  7971. // this does not re-parse the mask if it is the same as the last time
  7972. FDirViewMatchMask = Masks;
  7973. bool ImplicitMatch;
  7974. Matches =
  7975. // RecurseInclude parameter has no effect as the Path is empty
  7976. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7977. (AllowImplicitMatches || !ImplicitMatch);
  7978. }
  7979. //---------------------------------------------------------------------------
  7980. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7981. TObject * Sender, TListItem * Item, WORD & Indexes)
  7982. {
  7983. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7984. UnicodeString Ext;
  7985. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7986. {
  7987. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7988. }
  7989. else
  7990. {
  7991. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7992. }
  7993. if (SameText(Ext, Configuration->PartialExt))
  7994. {
  7995. Indexes |= oiPartial;
  7996. }
  7997. }
  7998. //---------------------------------------------------------------------------
  7999. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8000. {
  8001. return
  8002. DirViewEnabled(osCurrent) &&
  8003. DirView(osCurrent)->CanPasteFromClipBoard();
  8004. }
  8005. //---------------------------------------------------------------------------
  8006. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8007. {
  8008. bool Result = false;
  8009. if (CanPasteToDirViewFromClipBoard())
  8010. {
  8011. Result = true;
  8012. }
  8013. else
  8014. {
  8015. UnicodeString ClipboardText;
  8016. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8017. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8018. {
  8019. if (StoredSessions->IsUrl(ClipboardText))
  8020. {
  8021. Result = true;
  8022. }
  8023. else
  8024. {
  8025. Result = DirViewEnabled(osCurrent);
  8026. }
  8027. }
  8028. }
  8029. return Result;
  8030. }
  8031. //---------------------------------------------------------------------------
  8032. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8033. {
  8034. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8035. if (DoesClipboardContainOurFiles())
  8036. {
  8037. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8038. {
  8039. DebugAssert(!IsLocalBrowserMode());
  8040. TTerminalManager * Manager = TTerminalManager::Instance();
  8041. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8042. Manager->ActiveSession = FClipboardTerminal;
  8043. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8044. }
  8045. }
  8046. else if (CanPasteToDirViewFromClipBoard())
  8047. {
  8048. DirView(osCurrent)->PasteFromClipBoard();
  8049. }
  8050. else
  8051. {
  8052. UnicodeString ClipboardText;
  8053. if (NonEmptyTextFromClipboard(ClipboardText))
  8054. {
  8055. if (StoredSessions->IsUrl(ClipboardText))
  8056. {
  8057. NewSession(ClipboardText);
  8058. }
  8059. else
  8060. {
  8061. DirView(osCurrent)->Path = ClipboardText;
  8062. }
  8063. }
  8064. }
  8065. }
  8066. //---------------------------------------------------------------------------
  8067. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8068. {
  8069. // TBD
  8070. }
  8071. //---------------------------------------------------------------------------
  8072. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8073. {
  8074. TCustomDirView * ADirView = DirView(Side);
  8075. ADirView->SelectAll(Mode);
  8076. ADirView->SetFocus();
  8077. }
  8078. //---------------------------------------------------------------------------
  8079. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8080. {
  8081. TCustomDirView * ADirView = DirView(Side);
  8082. TFileFilter Filter;
  8083. DefaultFileFilter(Filter);
  8084. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8085. {
  8086. Configuration->Usage->Inc(L"MaskSelections");
  8087. ADirView->SelectFiles(Filter, Select);
  8088. ADirView->SetFocus();
  8089. }
  8090. }
  8091. //---------------------------------------------------------------------------
  8092. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8093. {
  8094. TCustomDirView * ADirView = DirView(Side);
  8095. ADirView->RestoreSelectedNames();
  8096. ADirView->SetFocus();
  8097. }
  8098. //---------------------------------------------------------------------------
  8099. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8100. {
  8101. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8102. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8103. {
  8104. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8105. UnicodeString Ext;
  8106. if (!IsSideLocalBrowser(osCurrent))
  8107. {
  8108. Ext = UnixExtractFileExt(FileName);
  8109. }
  8110. else
  8111. {
  8112. Ext = ExtractFileExt(FileName);
  8113. }
  8114. if (Ext.IsEmpty())
  8115. {
  8116. Ext = L".";
  8117. }
  8118. TFileFilter Filter;
  8119. Filter.Masks = FORMAT(L"*%s", (Ext));
  8120. Filter.Directories = false;
  8121. CurrentDirView->SelectFiles(Filter, Select);
  8122. }
  8123. }
  8124. //---------------------------------------------------------------------------
  8125. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8126. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8127. {
  8128. UnicodeString Result;
  8129. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8130. {
  8131. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8132. }
  8133. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8134. {
  8135. // noop
  8136. }
  8137. else
  8138. {
  8139. Result =
  8140. FMTLOAD(FILE_INFO_FORMAT,
  8141. (FormatBytes(FileInfo.SelectedSize),
  8142. FormatBytes(FileInfo.FilesSize),
  8143. FormatNumber(FileInfo.SelectedCount),
  8144. FormatNumber(FileInfo.FilesCount)));
  8145. }
  8146. return Result;
  8147. }
  8148. //---------------------------------------------------------------------------
  8149. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8150. TTBXStatusPanel * Panel, TOperationSide Side)
  8151. {
  8152. if (Panel->Index == 1)
  8153. {
  8154. ToggleShowHiddenFiles();
  8155. }
  8156. else if (Panel->Index == 2)
  8157. {
  8158. Filter(Side);
  8159. }
  8160. }
  8161. //---------------------------------------------------------------------------
  8162. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8163. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8164. {
  8165. DebugAssert(!StatusBar->SimplePanel);
  8166. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8167. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8168. }
  8169. //---------------------------------------------------------------------------
  8170. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8171. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8172. {
  8173. DebugAssert(StatusBar->Panels->Count >= 3);
  8174. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8175. if (FileInfo.HiddenCount > 0)
  8176. {
  8177. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8178. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8179. }
  8180. else
  8181. {
  8182. HiddenFilesPanel->ViewPriority = 0;
  8183. // not really necessary, just to cleanup no-longer-valid data
  8184. HiddenFilesPanel->Caption = L"";
  8185. }
  8186. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8187. if (FileInfo.FilteredCount > 0)
  8188. {
  8189. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8190. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8191. }
  8192. else
  8193. {
  8194. FilteredFilesPanel->ViewPriority = 0;
  8195. // not really necessary, just to cleanup no-longer-valid data
  8196. FilteredFilesPanel->Caption = L"";
  8197. }
  8198. }
  8199. //---------------------------------------------------------------------------
  8200. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8201. TObject * /*Sender*/)
  8202. {
  8203. if (DirView(osOther)->Enabled)
  8204. {
  8205. DirView(osOther)->SetFocus();
  8206. }
  8207. }
  8208. //---------------------------------------------------------------------------
  8209. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8210. {
  8211. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8212. switch (Config.Show)
  8213. {
  8214. case qvShow:
  8215. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8216. {
  8217. Config.Show = qvHide;
  8218. }
  8219. else
  8220. {
  8221. Config.Show = Config.LastHideShow;
  8222. }
  8223. break;
  8224. case qvHideWhenEmpty:
  8225. if (ComponentVisible[fcQueueView])
  8226. {
  8227. Config.Show = qvHide;
  8228. }
  8229. else
  8230. {
  8231. Config.LastHideShow = Config.Show;
  8232. Config.Show = qvShow;
  8233. }
  8234. break;
  8235. case qvHide:
  8236. Config.LastHideShow = Config.Show;
  8237. Config.Show = qvShow;
  8238. break;
  8239. }
  8240. WinConfiguration->QueueView = Config;
  8241. }
  8242. //---------------------------------------------------------------------------
  8243. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8244. {
  8245. UnicodeString Result;
  8246. if (Terminal != NULL)
  8247. {
  8248. switch (WinConfiguration->PathInCaption)
  8249. {
  8250. case picShort:
  8251. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8252. break;
  8253. case picFull:
  8254. Result = Terminal->CurrentDirectory;
  8255. break;
  8256. }
  8257. }
  8258. return Result;
  8259. }
  8260. //---------------------------------------------------------------------------
  8261. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8262. {
  8263. TColor Result = GetWindowColor(FSessionColor);
  8264. return Result;
  8265. }
  8266. //---------------------------------------------------------------------------
  8267. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8268. {
  8269. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8270. return Result;
  8271. }
  8272. //---------------------------------------------------------------------------
  8273. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8274. {
  8275. if (!FSessionChanging)
  8276. {
  8277. TTerminalManager::Instance()->UpdateAppTitle();
  8278. }
  8279. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8280. // background to gray on Windows 7 (works on Windows 10).
  8281. // See also EnableControl
  8282. if (Showing)
  8283. {
  8284. if (ManagedSession != NULL)
  8285. {
  8286. // Update path when it changes
  8287. if ((SessionsPageControl->ActivePage != NULL) &&
  8288. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8289. {
  8290. UpdateSessionTab(SessionsPageControl->ActivePage);
  8291. }
  8292. }
  8293. NonVisualDataModule->ReconnectSessionAction->Update();
  8294. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8295. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8296. CenterReconnectToolbar();
  8297. bool HasTerminal = HasActiveTerminal();
  8298. RemoteDirView->Enabled = HasTerminal;
  8299. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8300. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8301. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8302. RemoteDriveView->Color = RemoteDirView->Color;
  8303. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8304. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8305. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8306. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8307. QueueFileList->Enabled = QueueView3->Enabled;
  8308. QueueFileList->Color = QueueView3->Color;
  8309. QueueFileList->Font->Color = QueueView3->Font->Color;
  8310. QueueLabelUpdateStatus();
  8311. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8312. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8313. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8314. {
  8315. UpdateDarkMode();
  8316. }
  8317. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8318. }
  8319. }
  8320. //---------------------------------------------------------------------------
  8321. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8322. {
  8323. UpdateControls();
  8324. }
  8325. //---------------------------------------------------------------------------
  8326. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8327. TObject * Sender)
  8328. {
  8329. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8330. DebugAssert(DirView != NULL);
  8331. DoDirViewLoaded(DirView);
  8332. TransferPresetAutoSelect();
  8333. }
  8334. //---------------------------------------------------------------------------
  8335. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8336. {
  8337. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8338. // first run after installation
  8339. if (double(Updates.LastCheck) == 0)
  8340. {
  8341. // make sure next time there will be an update (if enabled)
  8342. Updates.LastCheck = TDateTime(1);
  8343. WinConfiguration->Updates = Updates;
  8344. }
  8345. else if ((double(Updates.Period) > 0) &&
  8346. (Now() - Updates.LastCheck >= Updates.Period))
  8347. {
  8348. TThreadMethod OnUpdatesChecked = NULL;
  8349. if (!IsUWP())
  8350. {
  8351. OnUpdatesChecked = UpdatesChecked;
  8352. }
  8353. StartUpdateThread(OnUpdatesChecked);
  8354. }
  8355. }
  8356. //---------------------------------------------------------------------------
  8357. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8358. {
  8359. UnicodeString Message;
  8360. bool New;
  8361. TQueryType Type;
  8362. GetUpdatesMessage(Message, New, Type, false);
  8363. if (!Message.IsEmpty())
  8364. {
  8365. if (New)
  8366. {
  8367. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8368. }
  8369. if (!New && (Type != qtWarning))
  8370. {
  8371. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8372. }
  8373. else
  8374. {
  8375. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8376. }
  8377. if (New)
  8378. {
  8379. Configuration->Usage->Inc(L"UpdateNotifications");
  8380. }
  8381. }
  8382. }
  8383. //---------------------------------------------------------------------------
  8384. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8385. {
  8386. RestoreApp();
  8387. bool CanDisplay = !NonVisualDataModule->Busy;
  8388. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8389. {
  8390. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8391. }
  8392. if (CanDisplay)
  8393. {
  8394. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8395. CheckForUpdates(true);
  8396. }
  8397. else
  8398. {
  8399. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8400. }
  8401. }
  8402. //---------------------------------------------------------------------------
  8403. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8404. TCopyParamRuleData & Data)
  8405. {
  8406. DebugAssert(Terminal != NULL);
  8407. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8408. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8409. Data.RemoteDirectory = RemoteDirView->PathName;
  8410. }
  8411. //---------------------------------------------------------------------------
  8412. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8413. {
  8414. // Terminal can be null when we are changing local directory implicitly,
  8415. // because it has been deleted, while no session is connected
  8416. // (Login dialog is open)
  8417. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8418. {
  8419. DebugAssert(!IsLocalBrowserMode());
  8420. TCopyParamRuleData Data;
  8421. GetTransferPresetAutoSelectData(Data);
  8422. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8423. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8424. if (CopyParamIndex < 0)
  8425. {
  8426. // there is no preset that matches autoselection
  8427. // set preset that we consider "default"
  8428. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8429. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8430. }
  8431. else
  8432. {
  8433. // there is preset matching autoselection
  8434. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8435. if (CopyParamName == FCopyParamAutoSelected)
  8436. {
  8437. // autoselected the same preset as the last time
  8438. // make no change (i.e. preserve custom user preset, if any)
  8439. }
  8440. else
  8441. {
  8442. // autoselected the different preset then the last time (or there
  8443. // was default preset set)
  8444. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8445. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8446. }
  8447. }
  8448. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8449. {
  8450. if (CopyParamIndex >= 0)
  8451. {
  8452. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8453. }
  8454. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8455. try
  8456. {
  8457. int Fmt =
  8458. (CopyParamIndex < 0) ?
  8459. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8460. COPY_PARAM_AUTOSELECTED;
  8461. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8462. Data->Message = MainInstructions(Message);
  8463. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8464. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8465. CopyParamAttrs);
  8466. if (CopyParamIndex >= 0)
  8467. {
  8468. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8469. Info = FORMAT(L"%s\n \n%s", (Info,
  8470. FMTLOAD(COPY_PARAM_RULE,
  8471. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8472. }
  8473. Data->Message += L"\n\n" + Info;
  8474. if (WinConfiguration->CopyParamAutoSelectNotice)
  8475. {
  8476. TransferPresetNoteMessage(Data, true);
  8477. }
  8478. else
  8479. {
  8480. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8481. Data = NULL; // ownership passed
  8482. }
  8483. }
  8484. __finally
  8485. {
  8486. delete Data;
  8487. }
  8488. }
  8489. }
  8490. }
  8491. //---------------------------------------------------------------------------
  8492. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8493. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8494. {
  8495. DebugAssert(NoteData != NULL);
  8496. DebugAssert(!IsLocalBrowserMode());
  8497. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8498. TQueryButtonAlias Aliases[1];
  8499. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8500. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8501. Params.Aliases = Aliases;
  8502. Params.AliasesCount = LENOF(Aliases);
  8503. unsigned int Result =
  8504. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8505. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8506. switch (Result)
  8507. {
  8508. case qaNeverAskAgain:
  8509. DebugAssert(AllowNeverAskAgain);
  8510. WinConfiguration->CopyParamAutoSelectNotice = false;
  8511. break;
  8512. case qaIgnore:
  8513. PreferencesDialog(pmPresets);
  8514. break;
  8515. }
  8516. }
  8517. //---------------------------------------------------------------------------
  8518. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8519. {
  8520. // as of now useless, as this is used for notes only, never for balloons, ...
  8521. RestoreApp();
  8522. // .. and we should never be busy here
  8523. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8524. {
  8525. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8526. }
  8527. }
  8528. //---------------------------------------------------------------------------
  8529. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8530. TPreferencesMode APreferencesMode)
  8531. {
  8532. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8533. TCopyParamRuleData Data;
  8534. if (Terminal != NULL)
  8535. {
  8536. PreferencesData.reset(new TPreferencesDialogData());
  8537. GetTransferPresetAutoSelectData(Data);
  8538. PreferencesData->CopyParamRuleData = &Data;
  8539. }
  8540. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8541. }
  8542. //---------------------------------------------------------------------------
  8543. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8544. const TCustomCommandType & Command)
  8545. {
  8546. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8547. {
  8548. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8549. }
  8550. }
  8551. //---------------------------------------------------------------------------
  8552. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8553. {
  8554. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8555. TCustomCommandType Command;
  8556. // make sure we use local custom command when remote are not supported
  8557. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8558. {
  8559. Command = FLastCustomCommand;
  8560. }
  8561. else
  8562. {
  8563. Command.Params = Command.Params | ccLocal;
  8564. }
  8565. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8566. FEditingFocusedAdHocCommand = OnFocused;
  8567. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8568. int Options =
  8569. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8570. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8571. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8572. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8573. {
  8574. FLastCustomCommand = Command;
  8575. UpdateCustomCommandsToolbar();
  8576. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8577. }
  8578. }
  8579. //---------------------------------------------------------------------------
  8580. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8581. {
  8582. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8583. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8584. DebugAssert(State > 0);
  8585. if (State <= 0)
  8586. {
  8587. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8588. }
  8589. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8590. }
  8591. //---------------------------------------------------------------------------
  8592. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8593. TCustomCommandType & Command, int & State)
  8594. {
  8595. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8596. if (Result)
  8597. {
  8598. Command = FLastCustomCommand;
  8599. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8600. }
  8601. return Result;
  8602. }
  8603. //---------------------------------------------------------------------------
  8604. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8605. {
  8606. ResetIncrementalSearch();
  8607. }
  8608. //---------------------------------------------------------------------------
  8609. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8610. {
  8611. DebugAssert(ComponentVisible[Component]);
  8612. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8613. }
  8614. //---------------------------------------------------------------------------
  8615. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8616. {
  8617. // when queue panel is resized here directly, the status bar is stretched
  8618. // over whole space the panel occupied
  8619. PostComponentHide(fcQueueView);
  8620. }
  8621. //---------------------------------------------------------------------------
  8622. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8623. {
  8624. ComponentVisible[fcQueueFileList] = false;
  8625. }
  8626. //---------------------------------------------------------------------------
  8627. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8628. {
  8629. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8630. RefreshColorMode();
  8631. ResetSysDarkTheme();
  8632. ConfigurationChanged();
  8633. ConfigureInterface();
  8634. // Should be called for all controls
  8635. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8636. }
  8637. //---------------------------------------------------------------------------
  8638. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8639. {
  8640. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8641. if (!FInvalid &&
  8642. (Message.LParam != 0) &&
  8643. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8644. {
  8645. ThemeChanged();
  8646. }
  8647. TForm::Dispatch(&Message);
  8648. }
  8649. //---------------------------------------------------------------------------
  8650. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8651. {
  8652. TMessage * M = static_cast<TMessage*>(Message);
  8653. switch (M->Msg)
  8654. {
  8655. case CM_DIALOGCHAR:
  8656. CMDialogChar(*M);
  8657. break;
  8658. case WM_APPCOMMAND:
  8659. WMAppCommand(*M);
  8660. break;
  8661. case WM_SYSCOMMAND:
  8662. WMSysCommand(*M);
  8663. break;
  8664. case WM_QUERYENDSESSION:
  8665. WMQueryEndSession(*M);
  8666. break;
  8667. case WM_ENDSESSION:
  8668. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8669. break;
  8670. #ifdef _DEBUG
  8671. case WM_WINDOWPOSCHANGED:
  8672. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8673. break;
  8674. case CM_CUSTOMSTYLECHANGED:
  8675. TForm::Dispatch(Message);
  8676. break;
  8677. #endif
  8678. case WM_COMPONENT_HIDE:
  8679. {
  8680. Byte Component = static_cast<Byte>(M->WParam);
  8681. // sanity check
  8682. if (ComponentVisible[Component])
  8683. {
  8684. // special treatment
  8685. if (Component == fcQueueView)
  8686. {
  8687. ToggleQueueVisibility();
  8688. DebugAssert(!ComponentVisible[fcQueueView]);
  8689. }
  8690. else
  8691. {
  8692. ComponentVisible[Component] = false;
  8693. }
  8694. }
  8695. }
  8696. break;
  8697. case WM_COPYDATA:
  8698. WMCopyData(*M);
  8699. break;
  8700. case WM_MANAGES_CAPTION:
  8701. // caption managed in TTerminalManager::UpdateAppTitle()
  8702. M->Result = 1;
  8703. break;
  8704. case WM_WANTS_MOUSEWHEEL:
  8705. M->Result = 1;
  8706. break;
  8707. case CM_SHOWINGCHANGED:
  8708. CMShowingChanged(*M);
  8709. break;
  8710. case WM_CLOSE:
  8711. WMClose(*M);
  8712. break;
  8713. case WM_DPICHANGED:
  8714. WMDpiChanged(*M);
  8715. break;
  8716. case CM_DPICHANGED:
  8717. CMDpiChanged(*M);
  8718. break;
  8719. case WM_SETTINGCHANGE:
  8720. WMSettingChange(*M);
  8721. break;
  8722. case CM_DIALOGKEY:
  8723. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8724. break;
  8725. default:
  8726. TForm::Dispatch(Message);
  8727. break;
  8728. }
  8729. }
  8730. //---------------------------------------------------------------------------
  8731. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8732. {
  8733. // noop
  8734. }
  8735. //---------------------------------------------------------------------------
  8736. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8737. {
  8738. TForm::Dispatch(&Message);
  8739. FSessionColors->Clear();
  8740. AddFixedSessionImages();
  8741. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8742. UpdateImages();
  8743. }
  8744. //---------------------------------------------------------------------------
  8745. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8746. {
  8747. TForm::Dispatch(&Message);
  8748. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8749. UpdatePixelsPerInchMainWindowCounter();
  8750. }
  8751. //---------------------------------------------------------------------------
  8752. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8753. {
  8754. // Cannot close window while we are busy.
  8755. // We cannot test this in FormCloseQuery as that is called also from
  8756. // Close(), which is called by CloseApplicationAction. So we can be busy
  8757. // there already even, when it is legitimate to close the application.
  8758. // Possibly a better place to handle this would be WMSysCommand.
  8759. if (NonVisualDataModule->Busy)
  8760. {
  8761. Message.Result = 1;
  8762. }
  8763. else
  8764. {
  8765. TForm::Dispatch(&Message);
  8766. }
  8767. }
  8768. //---------------------------------------------------------------------------
  8769. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8770. {
  8771. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8772. bool NoSession = (ManagedSession == NULL);
  8773. // This calls TCustomScpExplorerForm::DoShow
  8774. TForm::Dispatch(&Message);
  8775. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8776. if (Showing && !FStarted)
  8777. {
  8778. FStarted = true;
  8779. InterfaceStarted();
  8780. }
  8781. if (Showing && NoSession)
  8782. {
  8783. // When we are starting minimized (i.e. from an installer),
  8784. // postpone showing Login dialog until we get restored.
  8785. // Otherwise the Login dialog (and Authentication window) show restored
  8786. // over invisible (minimized) main window.
  8787. if (WindowState == wsMinimized)
  8788. {
  8789. FNeedSession = true;
  8790. }
  8791. else
  8792. {
  8793. // This happens before application ever goes idle, so the toolbars would
  8794. // stay enabled (initial state) until the Login dialog is dismissed.
  8795. UpdateActions();
  8796. NonVisualDataModule->StartBusy();
  8797. try
  8798. {
  8799. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8800. // otherwise the dialog does not receive focus.
  8801. // With Commander interface the ProcessMessages is called already
  8802. // by TDriveView, but with Explorer interface, we need to call it explicily
  8803. Application->ProcessMessages();
  8804. // do not reload sessions, they have been loaded just now (optimization)
  8805. NeedSession(true);
  8806. }
  8807. __finally
  8808. {
  8809. NonVisualDataModule->EndBusy();
  8810. }
  8811. }
  8812. }
  8813. }
  8814. //---------------------------------------------------------------------------
  8815. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8816. {
  8817. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8818. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8819. }
  8820. //---------------------------------------------------------------------------
  8821. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8822. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8823. int & MaxHeight)
  8824. {
  8825. // workaround for bug in TWinControl.CalcConstraints
  8826. // Check for empty rect (restore from iconic state) is done there only after
  8827. // call to AdjustClientRect, which enlarges the rect (for forms).
  8828. TRect R = GetClientRect();
  8829. // when restoring from iconic state, place no restrictions
  8830. if (IsRectEmpty(R))
  8831. {
  8832. MinWidth = 0;
  8833. MinHeight = 0;
  8834. MaxWidth = 32000;
  8835. MaxHeight = 32000;
  8836. }
  8837. }
  8838. //---------------------------------------------------------------------------
  8839. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8840. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8841. {
  8842. // terminal can be already closed (e.g. dropped connection)
  8843. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8844. {
  8845. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8846. try
  8847. {
  8848. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8849. }
  8850. catch(...)
  8851. {
  8852. if (!Terminal->Active)
  8853. {
  8854. Close = true;
  8855. }
  8856. throw;
  8857. }
  8858. }
  8859. }
  8860. //---------------------------------------------------------------------------
  8861. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8862. {
  8863. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8864. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8865. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8866. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8867. {
  8868. OnGetSpaceAvailable = GetSpaceAvailable;
  8869. }
  8870. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8871. OnGetSpaceAvailable);
  8872. }
  8873. //---------------------------------------------------------------------------
  8874. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8875. {
  8876. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8877. Terminal->FillSessionDataForCode(Data.get());
  8878. return Data.release();
  8879. }
  8880. //---------------------------------------------------------------------------
  8881. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8882. {
  8883. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8884. DoGenerateUrlDialog(Data.get(), Paths);
  8885. }
  8886. //---------------------------------------------------------------------------
  8887. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8888. {
  8889. GenerateUrl(NULL);
  8890. }
  8891. //---------------------------------------------------------------------------
  8892. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8893. {
  8894. std::unique_ptr<TStrings> Paths(new TStringList());
  8895. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8896. GenerateUrl(Paths.get());
  8897. }
  8898. //---------------------------------------------------------------------------
  8899. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8900. {
  8901. FSessionColor = value;
  8902. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8903. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8904. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8905. DebugAssert(ColorItem != NULL);
  8906. ColorItem->Color = C;
  8907. NonVisualDataModule->ColorMenuItem->Color = C;
  8908. UpdateControls();
  8909. }
  8910. //---------------------------------------------------------------------------
  8911. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8912. {
  8913. if (value != FSessionColor)
  8914. {
  8915. UpdateSessionColor(value);
  8916. // Is null when called from LastTerminalClosed
  8917. if (Terminal != NULL)
  8918. {
  8919. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8920. }
  8921. }
  8922. }
  8923. //---------------------------------------------------------------------------
  8924. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8925. {
  8926. bool Result = FNoteTimer->Enabled;
  8927. if (Result)
  8928. {
  8929. // cannot cancel note too early
  8930. bool NotEarly =
  8931. (Now() - FNoteShown >
  8932. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8933. if (Force || NotEarly)
  8934. {
  8935. FNoteTimer->Enabled = false;
  8936. FNote = L"";
  8937. SAFE_DESTROY(FNoteData);
  8938. FOnNoteClick = NULL;
  8939. FNoteHints = FNotes->Text;
  8940. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8941. UpdateStatusBar();
  8942. }
  8943. }
  8944. return Result;
  8945. }
  8946. //---------------------------------------------------------------------------
  8947. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8948. {
  8949. DebugAssert(FNoteTimer->Enabled);
  8950. CancelNote(true);
  8951. }
  8952. //---------------------------------------------------------------------------
  8953. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8954. {
  8955. FNoteHints = FNotes->Text.TrimRight();
  8956. }
  8957. //---------------------------------------------------------------------------
  8958. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8959. {
  8960. int P = Note.Pos(L"\n");
  8961. if (P > 0)
  8962. {
  8963. Note.SetLength(P - 1);
  8964. }
  8965. FNotes->Add(FORMAT(L"[%s] %s",
  8966. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8967. while (FNotes->Count > 10)
  8968. {
  8969. FNotes->Delete(0);
  8970. }
  8971. if (UpdateNow)
  8972. {
  8973. UpdateNoteHints();
  8974. UpdateStatusBar();
  8975. }
  8976. }
  8977. //---------------------------------------------------------------------------
  8978. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8979. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8980. {
  8981. int P = Note.Pos(L"\n");
  8982. if (P > 0)
  8983. {
  8984. Note.SetLength(P - 1);
  8985. }
  8986. UpdateNoteHints();
  8987. FNote = Note;
  8988. SAFE_DESTROY(FNoteData);
  8989. FNoteData = NoteData;
  8990. FOnNoteClick = OnNoteClick;
  8991. AddNote(Note, false);
  8992. UpdateStatusBar();
  8993. FNoteShown = Now();
  8994. FNoteTimer->Enabled = false;
  8995. if (Seconds == 0)
  8996. {
  8997. Seconds = WinConfiguration->NotificationsTimeout;
  8998. }
  8999. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9000. FNoteTimer->Enabled = true;
  9001. }
  9002. //---------------------------------------------------------------------------
  9003. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9004. {
  9005. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9006. }
  9007. //---------------------------------------------------------------------------
  9008. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9009. {
  9010. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9011. {
  9012. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9013. }
  9014. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9015. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9016. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9017. 0, NULL, NULL);
  9018. }
  9019. //---------------------------------------------------------------------------
  9020. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9021. {
  9022. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9023. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9024. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9025. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9026. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9027. }
  9028. //---------------------------------------------------------------------------
  9029. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9030. {
  9031. WinConfiguration->FormatSizeBytes = Style;
  9032. }
  9033. //---------------------------------------------------------------------------
  9034. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9035. {
  9036. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9037. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9038. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9039. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9040. }
  9041. //---------------------------------------------------------------------------
  9042. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9043. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9044. {
  9045. if (Panel->Index == 0)
  9046. {
  9047. if (FOnNoteClick != NULL)
  9048. {
  9049. // prevent the user data from being freed by possible call
  9050. // to CancelNote or PostNote during call to OnNoteClick
  9051. std::unique_ptr<TObject> NoteData(FNoteData);
  9052. TNotifyEvent OnNoteClick = FOnNoteClick;
  9053. FNoteData = NULL;
  9054. // need to cancel the note as we are going to delete its user data
  9055. CancelNote(true);
  9056. OnNoteClick(NoteData.get());
  9057. }
  9058. }
  9059. if (HasActiveTerminal() &&
  9060. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9061. {
  9062. FileSystemInfo();
  9063. }
  9064. }
  9065. //---------------------------------------------------------------------------
  9066. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9067. {
  9068. // workaround:
  9069. // 1) for unknown reason, disabling window, while minimized,
  9070. // prevents it from restoring, even if it was enabled again meanwhile
  9071. // 2) when disabling the main window, while another has focus
  9072. // minimize is no longer possible
  9073. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9074. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9075. // Shouldn't we use IsApplicationMinimized() here?
  9076. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9077. {
  9078. Enabled = false;
  9079. }
  9080. FLockLevel++;
  9081. }
  9082. //---------------------------------------------------------------------------
  9083. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9084. {
  9085. DebugAssert(FLockLevel > 0);
  9086. FLockLevel--;
  9087. if (FLockLevel == 0)
  9088. {
  9089. DebugAssert(FLockSuspendLevel == 0);
  9090. Enabled = true;
  9091. }
  9092. }
  9093. //---------------------------------------------------------------------------
  9094. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9095. {
  9096. // We need to make sure that window is enabled when the last modal window closes
  9097. // otherwise focus is not restored correctly.
  9098. // So we re-enable the window when modal window opens and
  9099. // disable it back after is closes
  9100. if (FLockLevel > 0)
  9101. {
  9102. // while we have nesting counter, we know that we never be called
  9103. // recursivelly as Application->OnModalBegin is called only
  9104. // for the top-level modal window
  9105. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9106. {
  9107. // won't be disabled when conditions in LockWindow() were not satisfied
  9108. FDisabledOnLockSuspend = !Enabled;
  9109. // When minimized to tray (or actually when set to SW_HIDE),
  9110. // setting Enabled makes the window focusable even when there's
  9111. // modal window over it
  9112. if (!FTrayIcon->Visible)
  9113. {
  9114. Enabled = true;
  9115. }
  9116. }
  9117. FLockSuspendLevel++;
  9118. }
  9119. }
  9120. //---------------------------------------------------------------------------
  9121. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9122. {
  9123. if (FLockSuspendLevel > 0)
  9124. {
  9125. DebugAssert(FLockLevel > 0);
  9126. FLockSuspendLevel--;
  9127. // see comment in SuspendWindowLock
  9128. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9129. {
  9130. // Note that window can be enabled here, when we were minized to tray when
  9131. // was SuspendWindowLock() called.
  9132. // We should possibly do the same check as in LockWindow(),
  9133. // if it is ever possible that the consitions change between
  9134. // SuspendWindowLock() and ResumeWindowLock()
  9135. Enabled = !FDisabledOnLockSuspend;
  9136. }
  9137. }
  9138. }
  9139. //---------------------------------------------------------------------------
  9140. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9141. {
  9142. TTBXComboBoxItem * RemotePathComboBox =
  9143. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9144. if (!TextOnly)
  9145. {
  9146. TStrings * Items = RemotePathComboBox->Strings;
  9147. Items->BeginUpdate();
  9148. try
  9149. {
  9150. Items->Clear();
  9151. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9152. {
  9153. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9154. while (!IsUnixRootPath(APath))
  9155. {
  9156. int P = APath.LastDelimiter(L'/');
  9157. DebugAssert(P >= 0);
  9158. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9159. APath.SetLength(P - 1);
  9160. }
  9161. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9162. }
  9163. }
  9164. __finally
  9165. {
  9166. Items->EndUpdate();
  9167. }
  9168. }
  9169. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9170. // Setting ItemIndex to -1 does not reset its text
  9171. if (RemotePathComboBox->Strings->Count == 0)
  9172. {
  9173. RemotePathComboBox->Text = L"";
  9174. }
  9175. }
  9176. //---------------------------------------------------------------------------
  9177. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9178. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9179. {
  9180. if (AIndex < 0)
  9181. {
  9182. AIndex = Sender->ItemIndex;
  9183. }
  9184. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9185. }
  9186. //---------------------------------------------------------------------------
  9187. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9188. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9189. int & ImageIndex)
  9190. {
  9191. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9192. }
  9193. //---------------------------------------------------------------------------
  9194. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9195. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9196. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9197. {
  9198. if (!IsLocalBrowserMode())
  9199. {
  9200. ARect.Left += (10 * AIndex);
  9201. }
  9202. }
  9203. //---------------------------------------------------------------------------
  9204. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9205. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9206. {
  9207. AWidth += (10 * AIndex);
  9208. }
  9209. //---------------------------------------------------------------------------
  9210. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9211. {
  9212. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9213. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9214. int Index = RemotePathComboBox->ItemIndex;
  9215. while (Index < RemotePathComboBox->Strings->Count - 1)
  9216. {
  9217. APath = UnixExtractFileDir(APath);
  9218. Index++;
  9219. }
  9220. // VanDyke style paths
  9221. if (APath.IsEmpty())
  9222. {
  9223. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9224. APath = ROOTDIRECTORY;
  9225. }
  9226. if (RemoteDirView->Path != APath)
  9227. {
  9228. RemoteDirView->Path = APath;
  9229. }
  9230. }
  9231. //---------------------------------------------------------------------------
  9232. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9233. {
  9234. DoRemotePathComboBoxItemClick(Sender);
  9235. }
  9236. //---------------------------------------------------------------------------
  9237. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9238. {
  9239. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9240. DebugUsedParam(Sender);
  9241. UpdateRemotePathComboBox(true);
  9242. }
  9243. //---------------------------------------------------------------------------
  9244. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9245. {
  9246. DoRemotePathComboBoxCancel(Sender);
  9247. }
  9248. //---------------------------------------------------------------------------
  9249. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9250. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9251. {
  9252. ResetIncrementalSearch();
  9253. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9254. DebugAssert(DirView != NULL);
  9255. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9256. {
  9257. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9258. // OnEditing is called also from TCustomListView::CanEdit
  9259. if (Edit != NULL)
  9260. {
  9261. EditSelectBaseName(Edit);
  9262. }
  9263. }
  9264. }
  9265. //---------------------------------------------------------------------------
  9266. //---------------------------------------------------------------------------
  9267. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9268. {
  9269. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9270. Result->AutoDetectDnD = false;
  9271. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9272. Result->RenderDataOn = rdoEnterAndDropSync;
  9273. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9274. return Result;
  9275. }
  9276. //---------------------------------------------------------------------------
  9277. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9278. {
  9279. if (IsWin10Build(2004))
  9280. {
  9281. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9282. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9283. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9284. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9285. }
  9286. }
  9287. //---------------------------------------------------------------------------
  9288. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9289. {
  9290. TForm::CreateWnd();
  9291. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9292. UpdateDarkMode();
  9293. if (FSessionsDragDropFilesEx == NULL)
  9294. {
  9295. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9296. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9297. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9298. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9299. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9300. }
  9301. if (FQueueDragDropFilesEx == NULL)
  9302. {
  9303. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9304. // No need to set OnDragOver as we do not have any restrictions
  9305. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9306. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9307. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9308. }
  9309. }
  9310. //---------------------------------------------------------------------------
  9311. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9312. {
  9313. TForm::DestroyWnd();
  9314. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9315. FQueueDragDropFilesEx->DragDropControl = NULL;
  9316. }
  9317. //---------------------------------------------------------------------------
  9318. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9319. {
  9320. SideEnter(FCurrentSide);
  9321. FEverShown = true;
  9322. }
  9323. //---------------------------------------------------------------------------
  9324. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9325. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9326. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9327. {
  9328. if (!NonVisualDataModule->Busy)
  9329. {
  9330. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9331. Configuration->Usage->Inc(L"FileFinds");
  9332. LockWindow(true);
  9333. NonVisualDataModule->StartBusy();
  9334. WinConfiguration->LockedInterface = true;
  9335. try
  9336. {
  9337. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9338. }
  9339. __finally
  9340. {
  9341. WinConfiguration->LockedInterface = false;
  9342. NonVisualDataModule->EndBusy();
  9343. UnlockWindow();
  9344. }
  9345. }
  9346. }
  9347. //---------------------------------------------------------------------------
  9348. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9349. {
  9350. if (!NonVisualDataModule->Busy)
  9351. {
  9352. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9353. SetFocus();
  9354. RemoteDirView->Path = UnixExtractFilePath(Path);
  9355. UnicodeString FileName = UnixExtractFileName(Path);
  9356. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9357. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9358. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9359. if (Item == NULL)
  9360. {
  9361. ReloadDirectory(osRemote);
  9362. Item = RemoteDirView->FindFileItem(FileName);
  9363. }
  9364. if (Item != NULL)
  9365. {
  9366. RemoteDirView->ItemFocused = Item;
  9367. RemoteDirView->ItemFocused->MakeVisible(false);
  9368. RemoteDirView->SetFocus();
  9369. }
  9370. }
  9371. }
  9372. //---------------------------------------------------------------------------
  9373. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9374. {
  9375. bool Result = !NonVisualDataModule->Busy;
  9376. if (Result)
  9377. {
  9378. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9379. }
  9380. return Result;
  9381. }
  9382. //---------------------------------------------------------------------------
  9383. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9384. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9385. {
  9386. if (CanOperateOnFoundFiles(ATerminal))
  9387. {
  9388. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9389. FOnFileOperationFinished = OnFileOperationFinished;
  9390. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9391. }
  9392. }
  9393. //---------------------------------------------------------------------------
  9394. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9395. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9396. {
  9397. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9398. }
  9399. //---------------------------------------------------------------------------
  9400. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9401. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9402. {
  9403. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9404. }
  9405. //---------------------------------------------------------------------------
  9406. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9407. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9408. {
  9409. if (CanOperateOnFoundFiles(ATerminal))
  9410. {
  9411. for (int Index = 0; Index < FileList->Count; Index++)
  9412. {
  9413. UnicodeString FileName = FileList->Strings[Index];
  9414. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9415. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9416. OnFileOperationFinished(FileName, true);
  9417. }
  9418. }
  9419. }
  9420. //---------------------------------------------------------------------------
  9421. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9422. {
  9423. FFileFindTerminal = Terminal;
  9424. ShowFileFindDialog(
  9425. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9426. DoDownloadFoundFiles, DoEditFoundFiles);
  9427. }
  9428. //---------------------------------------------------------------------------
  9429. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9430. {
  9431. FTaskbarList = TaskbarList;
  9432. }
  9433. //---------------------------------------------------------------------------
  9434. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9435. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9436. {
  9437. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9438. if (Index >= 0)
  9439. {
  9440. if (Button == mbLeft)
  9441. {
  9442. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9443. // If switching tab (switching session) takes long enough for user
  9444. // to actually release the button, "mouse down" is still raised,
  9445. // but we do not get "mouse up" event, so dragging is not cancelled,
  9446. // prevent that by not beginning dragging in the first place.
  9447. if (IsKeyPressed(VK_LBUTTON))
  9448. {
  9449. // when user clicks the "+", we get mouse down only after the session
  9450. // is closed, when new session tab is already on X:Y, so dragging
  9451. // starts, prevent that
  9452. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9453. {
  9454. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9455. if (Terminal != NULL)
  9456. {
  9457. SessionsPageControl->BeginDrag(false);
  9458. }
  9459. }
  9460. }
  9461. }
  9462. else if (Button == mbMiddle)
  9463. {
  9464. // ignore middle-click for "New session tab" and the last local-local tab
  9465. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9466. if ((Session != NULL) && CanCloseSession(Session))
  9467. {
  9468. CloseSessionTab(Index);
  9469. }
  9470. }
  9471. }
  9472. }
  9473. //---------------------------------------------------------------------------
  9474. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9475. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9476. {
  9477. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9478. // do not allow dropping on the "+" tab
  9479. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9480. if ((TargetTerminal != NULL) &&
  9481. (SessionsPageControl->ActivePage->PageIndex != Index))
  9482. {
  9483. Configuration->Usage->Inc(L"SessionTabMoves");
  9484. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9485. // but we want to actually prevent that to avoid flicker
  9486. SessionsPageControl->ActivePage->PageIndex = Index;
  9487. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9488. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9489. }
  9490. }
  9491. //---------------------------------------------------------------------------
  9492. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9493. TObject * Sender, TObject * Source, int X, int Y,
  9494. TDragState /*State*/, bool & Accept)
  9495. {
  9496. Accept = (Sender == Source);
  9497. if (Accept)
  9498. {
  9499. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9500. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9501. // do not allow dragging to the "+" tab
  9502. Accept = (Terminal != NULL);
  9503. }
  9504. }
  9505. //---------------------------------------------------------------------------
  9506. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9507. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9508. {
  9509. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9510. if (Index < 0)
  9511. {
  9512. Effect = DROPEFFECT_NONE;
  9513. }
  9514. else
  9515. {
  9516. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9517. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9518. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9519. {
  9520. Effect = DROPEFFECT_NONE;
  9521. }
  9522. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9523. {
  9524. Effect = DROPEFFECT_COPY;
  9525. }
  9526. }
  9527. }
  9528. //---------------------------------------------------------------------------
  9529. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9530. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9531. {
  9532. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9533. // do not allow dropping on local-local, disconnected and the "+" tab
  9534. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9535. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9536. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9537. {
  9538. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9539. if (!IsFileControl(DropSourceControl, osRemote))
  9540. {
  9541. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9542. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9543. // Why don't we use Terminal->CurrentDirectory directly?
  9544. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9545. // do not force queue, drag drop
  9546. false, true);
  9547. }
  9548. }
  9549. }
  9550. //---------------------------------------------------------------------------
  9551. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9552. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9553. {
  9554. // Downloads are handled in RemoteFileControlDDEnd
  9555. if (!IsFileControl(DropSourceControl, osRemote))
  9556. {
  9557. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9558. Terminal->CurrentDirectory,
  9559. // force queue, drag drop
  9560. true, true);
  9561. }
  9562. }
  9563. //---------------------------------------------------------------------------
  9564. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9565. {
  9566. FShowing = false;
  9567. // Do not save empty workspace
  9568. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9569. {
  9570. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9571. UnicodeString Name = WorkspaceName();
  9572. DoSaveWorkspace(
  9573. Name, DataList.get(),
  9574. !Configuration->DisablePasswordStoring &&
  9575. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9576. WinConfiguration->LastStoredSession = Name;
  9577. }
  9578. }
  9579. //---------------------------------------------------------------------------
  9580. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9581. {
  9582. TManagedTerminal * ManagedTerminal =
  9583. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9584. if (ManagedTerminal != NULL)
  9585. {
  9586. ManagedTerminal->DirectoryLoaded = Now();
  9587. }
  9588. }
  9589. //---------------------------------------------------------------------------
  9590. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9591. TListItem * /*Item*/, bool /*Selected*/)
  9592. {
  9593. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9594. switch (DirView->LastSelectMethod)
  9595. {
  9596. case smKeyboard:
  9597. Configuration->Usage->Inc(L"KeyboardSelections");
  9598. break;
  9599. case smMouse:
  9600. Configuration->Usage->Inc(L"MouseSelections");
  9601. break;
  9602. }
  9603. }
  9604. //---------------------------------------------------------------------------
  9605. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9606. {
  9607. if (Color != 0)
  9608. {
  9609. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9610. }
  9611. else
  9612. {
  9613. return FSessionTabImageIndex;
  9614. }
  9615. }
  9616. //---------------------------------------------------------------------------
  9617. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9618. {
  9619. TPngImageCollectionItem * Item =
  9620. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9621. FSessionColors->AddPng(Item->PngImage);
  9622. return FSessionColors->Count - 1;
  9623. }
  9624. //---------------------------------------------------------------------------
  9625. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9626. {
  9627. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9628. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9629. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9630. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9631. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9632. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9633. }
  9634. //---------------------------------------------------------------------------
  9635. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9636. {
  9637. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9638. }
  9639. //---------------------------------------------------------------------------
  9640. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9641. {
  9642. switch (Side)
  9643. {
  9644. case osLocal:
  9645. return FNewLocalTabTabImageIndex;
  9646. case osRemote:
  9647. case osCurrent:
  9648. return FNewRemoteTabTabImageIndex;
  9649. default:
  9650. DebugFail();
  9651. return -1;
  9652. }
  9653. }
  9654. //---------------------------------------------------------------------------
  9655. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9656. {
  9657. for (int Index = 0; Index < Control->ControlCount; Index++)
  9658. {
  9659. TControl * AControl = Control->Controls[Index];
  9660. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9661. if (WinControl != NULL)
  9662. {
  9663. CollectItemsWithTextDisplayMode(WinControl);
  9664. }
  9665. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9666. if (Toolbar != NULL)
  9667. {
  9668. // we care for top-level items only
  9669. TTBCustomItem * Items = Toolbar->Items;
  9670. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9671. {
  9672. TTBCustomItem * Item = Items->Items[ItemIndex];
  9673. if (((Item->DisplayMode == nbdmImageAndText) ||
  9674. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9675. EligibleForImageDisplayMode(Item))
  9676. {
  9677. FItemsWithTextDisplayMode.insert(Item);
  9678. }
  9679. }
  9680. }
  9681. }
  9682. }
  9683. //---------------------------------------------------------------------------
  9684. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9685. {
  9686. return true;
  9687. }
  9688. //---------------------------------------------------------------------------
  9689. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9690. {
  9691. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9692. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9693. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9694. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9695. bool Result = true;
  9696. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9697. {
  9698. TTBCustomItem * Item = *i;
  9699. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9700. if (Label != NULL)
  9701. {
  9702. // optimization
  9703. if (Label->Visible == SelectiveToolbarText)
  9704. {
  9705. Result = false;
  9706. }
  9707. else
  9708. {
  9709. Label->Visible = SelectiveToolbarText;
  9710. }
  9711. }
  9712. else
  9713. {
  9714. // optimization
  9715. if (Item->DisplayMode == DisplayMode)
  9716. {
  9717. Result = false;
  9718. }
  9719. else
  9720. {
  9721. Item->DisplayMode = DisplayMode;
  9722. }
  9723. }
  9724. ++i;
  9725. }
  9726. UpdateNewTabTab();
  9727. return Result;
  9728. }
  9729. //---------------------------------------------------------------------------
  9730. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9731. {
  9732. DebugFail();
  9733. }
  9734. //---------------------------------------------------------------------------
  9735. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9736. {
  9737. // Among other, a lock level is non zero, while directory is being loaded,
  9738. // even when it happens as a result of dirview navigation,
  9739. // i.e. when TNonVisualDataModule is NOT FBusy.
  9740. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9741. // Among other this prevents a panel auto update to occur while
  9742. // directory is changing.
  9743. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9744. }
  9745. //---------------------------------------------------------------------------
  9746. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9747. {
  9748. // While the window is disabled, we still seem to process menu shortcuts at least,
  9749. // so stop it at least here.
  9750. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9751. bool Result;
  9752. if (Allowed == aaUpdate)
  9753. {
  9754. Result = true;
  9755. }
  9756. else
  9757. {
  9758. Result = !NonVisualDataModule->Busy;
  9759. if (Result && (Allowed == aaShortCut))
  9760. {
  9761. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9762. Action->Update();
  9763. Result = Action->Enabled;
  9764. }
  9765. }
  9766. return Result;
  9767. }
  9768. //---------------------------------------------------------------------------
  9769. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9770. {
  9771. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9772. }
  9773. //---------------------------------------------------------------------------
  9774. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9775. {
  9776. // This is somewhat redundant to LockWindow() call from
  9777. // TTerminalManager::TerminalReadDirectoryProgress.
  9778. // But disabling window is known not to block keyboard shorcuts
  9779. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9780. if (Busy > 0)
  9781. {
  9782. if (NonVisualDataModule->Busy)
  9783. {
  9784. State = false;
  9785. }
  9786. else
  9787. {
  9788. NonVisualDataModule->StartBusy();
  9789. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9790. LockWindow();
  9791. }
  9792. }
  9793. else if (Busy < 0)
  9794. {
  9795. UnlockWindow();
  9796. NonVisualDataModule->EndBusy();
  9797. }
  9798. else
  9799. {
  9800. State = NonVisualDataModule->Busy;
  9801. }
  9802. }
  9803. //---------------------------------------------------------------------------
  9804. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9805. {
  9806. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9807. if (Index >= 0)
  9808. {
  9809. TPopupMenu * PopupMenu = NULL;
  9810. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9811. if (Session != NULL)
  9812. {
  9813. SessionsPageControl->ActivePageIndex = Index;
  9814. if (DebugAlwaysTrue(SessionTabSwitched()))
  9815. {
  9816. // copied from TControl.WMContextMenu
  9817. SendCancelMode(SessionsPageControl);
  9818. // explicit popup instead of using PopupMenu property
  9819. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9820. // while connecting yet not-connected session and hence
  9821. // allowing an access to commands over not-completelly connected session
  9822. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9823. }
  9824. }
  9825. else
  9826. {
  9827. if (SupportsLocalBrowser())
  9828. {
  9829. PopupMenu = NonVisualDataModule->NewTabPopup;
  9830. }
  9831. }
  9832. if (PopupMenu != NULL)
  9833. {
  9834. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9835. PopupMenu->PopupComponent = SessionsPageControl;
  9836. PopupMenu->Popup(Point.x, Point.y);
  9837. }
  9838. }
  9839. Handled = true;
  9840. }
  9841. //---------------------------------------------------------------------------
  9842. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9843. {
  9844. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9845. }
  9846. //---------------------------------------------------------------------------
  9847. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9848. {
  9849. Configuration->Usage->Inc(L"PasswordChanges");
  9850. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9851. ChangePasswordData->DisableAuthentationsExceptPassword();
  9852. ChangePasswordData->ChangePassword = true;
  9853. std::unique_ptr<TTerminal> ChangePasswordSession(
  9854. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9855. ChangePasswordSession->AutoReadDirectory = false;
  9856. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9857. {
  9858. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9859. }
  9860. else
  9861. {
  9862. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9863. }
  9864. }
  9865. //---------------------------------------------------------------------------
  9866. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9867. {
  9868. // No nice way to assert SSH2
  9869. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9870. }
  9871. //---------------------------------------------------------------------------
  9872. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9873. {
  9874. TOperationVisualizer OperationVisualizer;
  9875. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9876. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9877. }
  9878. //---------------------------------------------------------------------------
  9879. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9880. {
  9881. TForm::ChangeScale(M, D);
  9882. int APixelsPerInch = GetControlPixelsPerInch(this);
  9883. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9884. try
  9885. {
  9886. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9887. }
  9888. __finally
  9889. {
  9890. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9891. }
  9892. }
  9893. //---------------------------------------------------------------------------
  9894. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9895. {
  9896. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9897. }
  9898. //---------------------------------------------------------------------
  9899. class TPublicControl : public TControl
  9900. {
  9901. friend class TCustomScpExplorerForm;
  9902. };
  9903. //---------------------------------------------------------------------------
  9904. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9905. {
  9906. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9907. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9908. }
  9909. //---------------------------------------------------------------------------
  9910. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9911. {
  9912. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9913. OpenBookmark(Side, Bookmark);
  9914. }
  9915. //---------------------------------------------------------------------------
  9916. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9917. {
  9918. DoBookmarkClick(osLocal, Sender);
  9919. }
  9920. //---------------------------------------------------------------------------
  9921. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9922. {
  9923. DoBookmarkClick(osRemote, Sender);
  9924. }
  9925. //---------------------------------------------------------------------------
  9926. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9927. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9928. {
  9929. if (BookmarkList != NULL)
  9930. {
  9931. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9932. if (IsLocalBrowserMode())
  9933. {
  9934. Side = osLocal;
  9935. }
  9936. if (!WinConfiguration->UseLocationProfiles)
  9937. {
  9938. std::unique_ptr<TStringList> Directories(new TStringList());
  9939. Directories->CaseSensitive = (Side == osRemote);
  9940. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9941. {
  9942. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9943. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9944. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9945. {
  9946. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9947. Item->Caption = EscapeHotkey(Directory);
  9948. Item->ShortCut = Bookmark->ShortCut;
  9949. Item->OnClick = OnBookmarkClick;
  9950. Item->Tag = reinterpret_cast<int>(Bookmark);
  9951. Directories->Add(Directory);
  9952. Menu->Add(Item.release());
  9953. }
  9954. }
  9955. }
  9956. else
  9957. {
  9958. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9959. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9960. {
  9961. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9962. if (!Bookmark->Node.IsEmpty())
  9963. {
  9964. Folders->Add(Bookmark->Node);
  9965. }
  9966. }
  9967. for (int Index = 0; Index < Folders->Count; Index++)
  9968. {
  9969. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9970. Item->Caption = Folders->Strings[Index];
  9971. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  9972. Folders->Objects[Index] = Item.get();
  9973. Menu->Add(Item.release());
  9974. }
  9975. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9976. {
  9977. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9978. TTBCustomItem * Parent;
  9979. if (!Bookmark->Node.IsEmpty())
  9980. {
  9981. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9982. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9983. }
  9984. else
  9985. {
  9986. Parent = Menu;
  9987. }
  9988. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9989. Item->Caption = Bookmark->Name;
  9990. Item->ShortCut = Bookmark->ShortCut;
  9991. Item->OnClick = OnBookmarkClick;
  9992. Item->Tag = reinterpret_cast<int>(Bookmark);
  9993. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9994. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9995. {
  9996. // No hint for location profiles that are actually mere "bookmarks"
  9997. }
  9998. else
  9999. {
  10000. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10001. UnicodeString LongHint;
  10002. if (!Bookmark->Local.IsEmpty())
  10003. {
  10004. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10005. }
  10006. if (!Bookmark->Remote.IsEmpty())
  10007. {
  10008. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10009. }
  10010. AddToList(Hint, LongHint, L"|");
  10011. Item->Hint = Hint;
  10012. }
  10013. Parent->Add(Item.release());
  10014. }
  10015. }
  10016. AddMenuSeparator(Menu);
  10017. }
  10018. }
  10019. //---------------------------------------------------------------------------
  10020. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10021. {
  10022. Menu->Clear();
  10023. std::unique_ptr<TTBCustomItem> Item;
  10024. Item.reset(new TTBXItem(Owner));
  10025. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10026. Item->Options = Item->Options << tboDefault;
  10027. Menu->Add(Item.release());
  10028. AddMenuSeparator(Menu);
  10029. if (Terminal != NULL)
  10030. {
  10031. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10032. }
  10033. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10034. Item.reset(new TTBXItem(Owner));
  10035. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10036. Menu->Add(Item.release());
  10037. }
  10038. //---------------------------------------------------------------------------
  10039. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10040. {
  10041. bool Result = true;
  10042. bool Remote = !IsSideLocalBrowser(Side);
  10043. try
  10044. {
  10045. if (Remote)
  10046. {
  10047. Terminal->ExceptionOnFail = true;
  10048. }
  10049. try
  10050. {
  10051. DirView(Side)->Path = Path;
  10052. }
  10053. __finally
  10054. {
  10055. if (Remote)
  10056. {
  10057. Terminal->ExceptionOnFail = false;
  10058. }
  10059. }
  10060. }
  10061. catch (Exception & E)
  10062. {
  10063. if (!Remote || Terminal->Active)
  10064. {
  10065. ShowExtendedExceptionEx(Terminal, &E);
  10066. Result = false;
  10067. }
  10068. else
  10069. {
  10070. throw;
  10071. }
  10072. }
  10073. return Result;
  10074. }
  10075. //---------------------------------------------------------------------------
  10076. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10077. {
  10078. // Make sure there some feedback even when loading tiny local folders
  10079. TInstantOperationVisualizer Visualizer;
  10080. DirView(Side)->ReloadDirectory();
  10081. }
  10082. //---------------------------------------------------------------------------
  10083. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10084. {
  10085. if (Index == SessionsPageControl->TabIndex)
  10086. {
  10087. CloseSession();
  10088. }
  10089. else
  10090. {
  10091. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10092. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10093. TTerminalManager * Manager = TTerminalManager::Instance();
  10094. if (!Terminal->Active)
  10095. {
  10096. Manager->FreeTerminal(Terminal);
  10097. }
  10098. else
  10099. {
  10100. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10101. if (CanCloseQueue(Queue))
  10102. {
  10103. Manager->FreeTerminal(Terminal);
  10104. }
  10105. }
  10106. }
  10107. }
  10108. //---------------------------------------------------------------------------
  10109. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10110. {
  10111. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10112. if (Session == NULL)
  10113. {
  10114. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10115. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10116. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10117. //P = SessionsPageControl->ClientToScreen(P);
  10118. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10119. }
  10120. else
  10121. {
  10122. CloseSessionTab(Index);
  10123. }
  10124. }
  10125. //---------------------------------------------------------------------------
  10126. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10127. {
  10128. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10129. {
  10130. TInstantOperationVisualizer Visualizer;
  10131. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10132. // before we create a new data object
  10133. ClipboardClear();
  10134. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10135. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10136. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10137. // Intermediate variable to WORKAROUND a crash in compiler
  10138. TObjectList * ClipboardFakeMonitors =
  10139. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10140. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10141. if (FClipboardDragDropFilesEx.get() == NULL)
  10142. {
  10143. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10144. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10145. }
  10146. FClipboardDragDropFilesEx->FileList->Clear();
  10147. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10148. FClipboardDragDropFilesEx->CopyToClipboard();
  10149. FClipboardTerminal = Terminal;
  10150. // Need full paths, as cwd can be different once files are pasted
  10151. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10152. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10153. {
  10154. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10155. }
  10156. }
  10157. }
  10158. //---------------------------------------------------------------------------
  10159. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10160. {
  10161. if (OpenClipboard(0))
  10162. {
  10163. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10164. CloseClipboard();
  10165. }
  10166. }
  10167. //---------------------------------------------------------------------------
  10168. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10169. {
  10170. return !FClipboardFakeDirectory.IsEmpty();
  10171. }
  10172. //---------------------------------------------------------------------------
  10173. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10174. {
  10175. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10176. FClipboardFakeDirectory = UnicodeString();
  10177. FClipboardTerminal = NULL;
  10178. // Also called in Idle()
  10179. FClipboardFakeMonitors.reset(NULL);
  10180. }
  10181. //---------------------------------------------------------------------------
  10182. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10183. {
  10184. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10185. {
  10186. ClipboardStop();
  10187. }
  10188. }
  10189. //---------------------------------------------------------------------------
  10190. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10191. {
  10192. TTerminalManager * Manager = TTerminalManager::Instance();
  10193. Manager->ActiveSession = FClipboardTerminal;
  10194. TTransferOperationParam Params;
  10195. Params.TargetDirectory = TargetDirectory;
  10196. Params.DragDrop = DragDrop;
  10197. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10198. }
  10199. //---------------------------------------------------------------------------
  10200. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10201. {
  10202. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10203. // (as it pumps a Windows message queue while being shutted down)
  10204. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10205. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10206. {
  10207. // Can fail as it can be e.g. locked by AV, so we retry that later
  10208. bool Removed = RemoveDir(ApiPath(FileName));
  10209. try
  10210. {
  10211. if (!NonVisualDataModule->Busy)
  10212. {
  10213. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10214. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10215. }
  10216. }
  10217. __finally
  10218. {
  10219. if (!Removed)
  10220. {
  10221. RemoveDir(ApiPath(FileName));
  10222. }
  10223. }
  10224. }
  10225. }
  10226. //---------------------------------------------------------------------------
  10227. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10228. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10229. {
  10230. TFileMasks::TParams MaskParams;
  10231. MaskParams.Size = Size;
  10232. MaskParams.Modification = Modification;
  10233. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10234. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10235. {
  10236. bool ImplicitMatch;
  10237. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10238. !ImplicitMatch)
  10239. {
  10240. Color = Iter->Color;
  10241. break;
  10242. }
  10243. }
  10244. }
  10245. //---------------------------------------------------------------------------
  10246. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10247. {
  10248. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10249. if (!DirView->IsEditing())
  10250. {
  10251. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10252. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10253. // while searching
  10254. if (Key == VK_BACK)
  10255. {
  10256. if (!FIncrementalSearch.IsEmpty())
  10257. {
  10258. if (FIncrementalSearch.Length() == 1)
  10259. {
  10260. ResetIncrementalSearch();
  10261. }
  10262. else
  10263. {
  10264. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10265. IncrementalSearch(NewText, false, false);
  10266. }
  10267. Key = 0;
  10268. }
  10269. }
  10270. }
  10271. }
  10272. //---------------------------------------------------------------------------
  10273. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10274. {
  10275. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10276. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10277. {
  10278. // Filter control sequences.
  10279. // When not searching yet, prefer use of the space for toggling file selection
  10280. // (so we cannot incrementally search for a string starting with a space).
  10281. if ((Key > VK_SPACE) ||
  10282. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10283. {
  10284. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10285. Key = 0;
  10286. }
  10287. }
  10288. }
  10289. //---------------------------------------------------------------------------
  10290. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10291. {
  10292. if (!FIncrementalSearch.IsEmpty())
  10293. {
  10294. FIncrementalSearch = L"";
  10295. DirView(osCurrent)->UpdateStatusBar();
  10296. }
  10297. }
  10298. //---------------------------------------------------------------------------
  10299. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10300. {
  10301. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10302. if (Item == NULL)
  10303. {
  10304. MessageBeep(MB_ICONHAND);
  10305. }
  10306. else
  10307. {
  10308. TCustomDirView * ADirView = DirView(osCurrent);
  10309. {
  10310. TAutoNestingCounter Guard(FIncrementalSearching);
  10311. ADirView->FocusItem(Item);
  10312. if (ADirView->NortonLike == nlOff)
  10313. {
  10314. if ((ADirView->Selected != Item) ||
  10315. (ADirView->SelCount > 1))
  10316. {
  10317. ADirView->ClearSelection();
  10318. ADirView->Selected = Item;
  10319. }
  10320. }
  10321. }
  10322. FIncrementalSearch = Text;
  10323. Item->MakeVisible(false);
  10324. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10325. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10326. ADirView->UpdateStatusBar();
  10327. }
  10328. }
  10329. //---------------------------------------------------------------------------
  10330. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10331. {
  10332. int Index = Item->Index;
  10333. if (!Reverse)
  10334. {
  10335. Index++;
  10336. if (Index >= Item->Owner->Count)
  10337. {
  10338. Index = 0;
  10339. }
  10340. }
  10341. else
  10342. {
  10343. Index--;
  10344. if (Index < 0)
  10345. {
  10346. Index = Item->Owner->Count - 1;
  10347. }
  10348. }
  10349. return Item->Owner->Item[Index];
  10350. }
  10351. //---------------------------------------------------------------------------
  10352. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10353. {
  10354. TCustomDirView * ADirView = DirView(osCurrent);
  10355. if (ADirView->Items->Count == 0)
  10356. {
  10357. return NULL;
  10358. }
  10359. else
  10360. {
  10361. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10362. TListItem * Item = CurrentItem;
  10363. if (SkipCurrent)
  10364. {
  10365. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10366. }
  10367. while (true)
  10368. {
  10369. bool Matches = false;
  10370. switch (WinConfiguration->PanelSearch)
  10371. {
  10372. case isNameStartOnly:
  10373. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10374. break;
  10375. case isName:
  10376. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10377. break;
  10378. case isAll:
  10379. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10380. int Index = 0;
  10381. while ((Index < ColCount) && !Matches)
  10382. {
  10383. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10384. if (Visible)
  10385. {
  10386. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10387. }
  10388. Index++;
  10389. }
  10390. break;
  10391. }
  10392. if (Matches)
  10393. {
  10394. return Item;
  10395. }
  10396. Item = GetNextFile(Item, Reverse);
  10397. if (Item == CurrentItem)
  10398. {
  10399. return NULL;
  10400. }
  10401. }
  10402. }
  10403. }
  10404. //---------------------------------------------------------------------------
  10405. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10406. {
  10407. ResetIncrementalSearch();
  10408. }
  10409. //---------------------------------------------------------------------------
  10410. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10411. {
  10412. if (Message.CharCode == VK_TAB)
  10413. {
  10414. if (!FIncrementalSearch.IsEmpty())
  10415. {
  10416. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10417. bool Reverse = Shift.Contains(ssShift);
  10418. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10419. Message.Result = 1;
  10420. return;
  10421. }
  10422. }
  10423. else if (Message.CharCode == VK_ESCAPE)
  10424. {
  10425. if (!FIncrementalSearch.IsEmpty())
  10426. {
  10427. ResetIncrementalSearch();
  10428. Message.Result = 1;
  10429. return;
  10430. }
  10431. }
  10432. TForm::Dispatch(&Message);
  10433. }
  10434. //---------------------------------------------------------------------------
  10435. void __fastcall TCustomScpExplorerForm::Deactivate()
  10436. {
  10437. TForm::Deactivate();
  10438. // E.g. when switching to an internal editor window
  10439. ResetIncrementalSearch();
  10440. }
  10441. //---------------------------------------------------------------------------
  10442. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10443. {
  10444. // When switching to another application
  10445. ResetIncrementalSearch();
  10446. }
  10447. //---------------------------------------------------------------------------
  10448. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10449. {
  10450. ResetIncrementalSearch();
  10451. }
  10452. //---------------------------------------------------------------------------
  10453. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10454. {
  10455. if (FIncrementalSearching <= 0)
  10456. {
  10457. ResetIncrementalSearch();
  10458. }
  10459. }
  10460. //---------------------------------------------------------------------------
  10461. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10462. {
  10463. CountClicksForWindowPrint(this);
  10464. }
  10465. //---------------------------------------------------------------------------
  10466. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10467. {
  10468. CenterReconnectToolbar();
  10469. }
  10470. //---------------------------------------------------------------------------
  10471. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10472. {
  10473. if (RemoteDirView->ItemFocused != NULL)
  10474. {
  10475. RemoteDirView->ItemFocused->Selected = true;
  10476. }
  10477. }
  10478. //---------------------------------------------------------------------------
  10479. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10480. {
  10481. if (!FQueueStatusUpdating)
  10482. {
  10483. bool Refresh;
  10484. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10485. {
  10486. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10487. }
  10488. else
  10489. {
  10490. if (FQueueFileList->GetCount() > 0)
  10491. {
  10492. FQueueFileList->Clear();
  10493. Refresh = true;
  10494. }
  10495. }
  10496. int Count = FQueueFileList->GetCount();
  10497. if (QueueFileList->Items->Count != Count)
  10498. {
  10499. DebugAssert(Refresh);
  10500. QueueFileList->Items->Count = Count;
  10501. }
  10502. if (Refresh)
  10503. {
  10504. QueueFileList->Invalidate();
  10505. }
  10506. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10507. }
  10508. }
  10509. //---------------------------------------------------------------------------
  10510. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10511. {
  10512. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10513. }
  10514. //---------------------------------------------------------------------------
  10515. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10516. {
  10517. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10518. // See TSynchronizeChecklistDialog::UpdateTimer
  10519. if (QueueFileList->Items->Count > 0)
  10520. {
  10521. QueueFileList->Items->Item[0]->MakeVisible(false);
  10522. }
  10523. UpdateQueueFileList();
  10524. }
  10525. //---------------------------------------------------------------------------
  10526. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10527. {
  10528. Active = QueueView3->Focused() || QueueFileList->Focused();
  10529. }
  10530. //---------------------------------------------------------------------------
  10531. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10532. {
  10533. QueueLabelUpdateStatus();
  10534. }
  10535. //---------------------------------------------------------------------------
  10536. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10537. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10538. {
  10539. int State = FQueueFileList->GetState(Item->Index);
  10540. if (State == qfsProcessed)
  10541. {
  10542. Sender->Canvas->Font->Color = clGrayText;
  10543. }
  10544. }
  10545. //---------------------------------------------------------------------------
  10546. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10547. {
  10548. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10549. QueueFileList->Column[0]->Width =
  10550. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10551. }
  10552. //---------------------------------------------------------------------------
  10553. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10554. {
  10555. QueueFileListColumnAutoSize();
  10556. }
  10557. //---------------------------------------------------------------------------
  10558. void __fastcall TCustomScpExplorerForm::CloseApp()
  10559. {
  10560. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10561. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10562. // So we clear the flag here.
  10563. DebugAssert(NonVisualDataModule->Busy);
  10564. NonVisualDataModule->EndBusy();
  10565. try
  10566. {
  10567. Close();
  10568. }
  10569. __finally
  10570. {
  10571. NonVisualDataModule->StartBusy();
  10572. }
  10573. }
  10574. //---------------------------------------------------------------------------
  10575. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10576. {
  10577. return (Terminal != NULL) && Terminal->Active;
  10578. }
  10579. //---------------------------------------------------------------------------
  10580. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10581. {
  10582. return (ManagedSession != NULL);
  10583. }
  10584. //---------------------------------------------------------------------------
  10585. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10586. {
  10587. return IsActiveTerminal(Terminal);
  10588. }
  10589. //---------------------------------------------------------------------------
  10590. void TCustomScpExplorerForm::LocalLocalCopy(
  10591. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10592. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10593. {
  10594. DebugFail();
  10595. }
  10596. //---------------------------------------------------------------------------
  10597. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10598. {
  10599. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10600. }
  10601. //---------------------------------------------------------------------------
  10602. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10603. {
  10604. // Changing tab list triggers the OnResize.
  10605. if (!FUpdatingSessionTabs)
  10606. {
  10607. // UpdateTabsCaptionTruncation should be enough
  10608. SessionListChanged(true);
  10609. }
  10610. }
  10611. //---------------------------------------------------------------------------
  10612. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10613. {
  10614. if (ASession == ManagedSession)
  10615. {
  10616. UpdateSession(ASession);
  10617. }
  10618. TSessionData * Data = ASession->StateData;
  10619. UnicodeString Result;
  10620. if (Side == osLocal)
  10621. {
  10622. Result = Data->LocalDirectory;
  10623. }
  10624. else if (DebugAlwaysTrue(Side == osOther))
  10625. {
  10626. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10627. }
  10628. return Result;
  10629. }
  10630. //---------------------------------------------------------------------------
  10631. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10632. {
  10633. UnicodeString Result;
  10634. if (!ASession->Active)
  10635. {
  10636. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10637. }
  10638. else
  10639. {
  10640. Result = GetSessionPath(ASession, osRemote);
  10641. }
  10642. return Result;
  10643. }
  10644. //---------------------------------------------------------------------------
  10645. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10646. {
  10647. return LoadStr(NEW_REMOTE_TAB_HINT);
  10648. }
  10649. //---------------------------------------------------------------------------
  10650. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10651. {
  10652. if (Index >= 0)
  10653. {
  10654. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10655. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10656. if (TabSession == NULL)
  10657. {
  10658. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10659. if (NewTabTabCaption != TabSheet->Caption)
  10660. {
  10661. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10662. }
  10663. }
  10664. else
  10665. {
  10666. TTerminalManager * Manager = TTerminalManager::Instance();
  10667. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10668. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10669. }
  10670. }
  10671. }
  10672. //---------------------------------------------------------------------------