CustomScpExplorer.cpp 352 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736
  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 = 4;
  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. class TAutoBatch
  100. {
  101. public:
  102. TAutoBatch(TCustomScpExplorerForm * Form) :
  103. FForm(Form)
  104. {
  105. FForm->BatchStart(FBatchStorage);
  106. }
  107. ~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. private:
  112. TCustomScpExplorerForm * FForm;
  113. void * FBatchStorage;
  114. };
  115. //---------------------------------------------------------------------------
  116. struct TTransferOperationParam
  117. {
  118. TTransferOperationParam();
  119. UnicodeString TargetDirectory;
  120. bool Temp;
  121. bool DragDrop;
  122. int Options;
  123. TAutoSwitch Queue;
  124. };
  125. //---------------------------------------------------------------------------
  126. TTransferOperationParam::TTransferOperationParam()
  127. {
  128. Temp = false;
  129. DragDrop = false;
  130. Options = 0;
  131. Queue = asAuto;
  132. }
  133. //---------------------------------------------------------------------------
  134. class TTransferPresetNoteData : public TObject
  135. {
  136. public:
  137. UnicodeString Message;
  138. };
  139. //---------------------------------------------------------------------------
  140. class TTerminalNoteData : public TObject
  141. {
  142. public:
  143. TManagedTerminal * Terminal;
  144. };
  145. //---------------------------------------------------------------------------
  146. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  147. FFormRestored(false),
  148. TForm(Owner)
  149. {
  150. FInvalid = false;
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. InitControls();
  155. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  156. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  157. NonVisualDataModule->ScpExplorer = this;
  158. RestoreParams();
  159. ConfigurationChanged();
  160. RemoteDirView->Invalidate();
  161. FAutoOperation = false;
  162. FOnFileOperationFinished = NULL;
  163. FForceExecution = false;
  164. FIgnoreNextDialogChar = 0;
  165. FErrorList = NULL;
  166. FSynchronizeProgressForm = NULL;
  167. FProgressForm = NULL;
  168. FRefreshLocalDirectory = false;
  169. FRefreshRemoteDirectory = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. FStandaloneEditing = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FNeedSession = false;
  199. FDoNotIdleCurrentTerminal = 0;
  200. FIncrementalSearching = 0;
  201. FIncrementalSearchHaveNext = false;
  202. FEditorManager = new TEditorManager();
  203. FEditorManager->OnFileChange = ExecutedFileChanged;
  204. FEditorManager->OnFileReload = ExecutedFileReload;
  205. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  206. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  207. FLocalEditors = new TList();
  208. FQueueStatus = NULL;
  209. FQueueStatusSection = new TCriticalSection();
  210. FQueueStatusInvalidated = false;
  211. FQueueItemInvalidated = false;
  212. FQueueActedItem = NULL;
  213. FQueueController = new TQueueController(QueueView3);
  214. FUserActionTimer = new TTimer(this);
  215. FUserActionTimer->Enabled = false;
  216. FUserActionTimer->Interval = 10;
  217. FUserActionTimer->OnTimer = UserActionTimer;
  218. FNotes = new TStringList();
  219. FNoteTimer = new TTimer(this);
  220. FNoteTimer->Enabled = false;
  221. FNoteTimer->OnTimer = NoteTimer;
  222. FOnNoteClick = NULL;
  223. FOle32Library = LoadLibrary(L"Ole32.dll");
  224. FDragMoveCursor = FOle32Library != NULL ?
  225. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  226. UseSystemSettings(this);
  227. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  228. static_cast<TObject*>(GetComponent(fcTransferList)));
  229. DebugAssert(TransferList != NULL);
  230. FTransferListHoverIndex = -1;
  231. TransferList->OnChange = TransferListChange;
  232. TransferList->OnDrawItem = TransferListDrawItem;
  233. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  234. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  235. UseDesktopFont(SessionsPageControl);
  236. UpdateSessionsPageControlHeight();
  237. UseDesktopFont(RemoteDirView);
  238. UseDesktopFont(RemoteDriveView);
  239. UseDesktopFont(QueueView3);
  240. UseDesktopFont(QueueLabel);
  241. UseDesktopFont(RemoteStatusBar);
  242. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  243. QueueSplitter->ShowHint = true;
  244. RemotePanelSplitter->ShowHint = true;
  245. UpdateImages();
  246. FCustomCommandMenu = CreateTBXPopupMenu(this);
  247. FCustomCommandLocalFileList = NULL;
  248. FCustomCommandRemoteFileList = NULL;
  249. FSessionColors = new TPngImageList(this);
  250. FSessionColors->ColorDepth = cd32Bit;
  251. AddFixedSessionImages();
  252. SessionsPageControl->Images = FSessionColors;
  253. QueueLabel->FocusControl = QueueView3;
  254. UpdateQueueLabel();
  255. FRemoteDirViewWasFocused = true;
  256. CreateHiddenWindow();
  257. StartUpdates();
  258. }
  259. //---------------------------------------------------------------------------
  260. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  261. {
  262. FInvalid = true;
  263. // this has to be one of the very first things to do
  264. StopUpdateThread();
  265. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  266. TTerminalManager::Instance()->ScpExplorer = NULL;
  267. delete FCustomCommandLocalFileList;
  268. delete FCustomCommandRemoteFileList;
  269. delete FCustomCommandMenu;
  270. delete FTrayIcon;
  271. FTrayIcon = NULL;
  272. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  273. delete FEditorManager;
  274. ForceCloseLocalEditors();
  275. delete FLocalEditors;
  276. HideFileFindDialog();
  277. if (FDelayedDeletionTimer)
  278. {
  279. DoDelayedDeletion(NULL);
  280. SAFE_DESTROY(FDelayedDeletionTimer);
  281. }
  282. SAFE_DESTROY(FDelayedDeletionList);
  283. // sometimes we do not get DDEnd so the list is not released
  284. SAFE_DESTROY(FDDFileList);
  285. DebugAssert(FSynchronizeController == NULL);
  286. CloseHandle(FDDExtMutex);
  287. FDDExtMutex = NULL;
  288. FreeLibrary(FOle32Library);
  289. FOle32Library = NULL;
  290. FDragMoveCursor = NULL;
  291. DebugAssert(!FErrorList);
  292. if (FEverShown)
  293. {
  294. // when window is never shown (like when running command-line operation),
  295. // particularly window site is not restored correctly (BoundsRect value set
  296. // in RestoreForm gets lost during handle allocation), so we do not want
  297. // it to be stored
  298. StoreParams();
  299. }
  300. Terminal = NULL;
  301. Queue = NULL;
  302. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  303. NonVisualDataModule->ScpExplorer = NULL;
  304. delete FQueueController;
  305. FQueueController = NULL;
  306. delete FQueueStatusSection;
  307. FQueueStatusSection = NULL;
  308. delete FQueueStatus;
  309. FQueueStatus = NULL;
  310. delete FUserActionTimer;
  311. FUserActionTimer = NULL;
  312. delete FNoteTimer;
  313. delete FNotes;
  314. delete FNoteData;
  315. SAFE_DESTROY(FDocks);
  316. SessionsPageControl->Images = NULL;
  317. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  318. SAFE_DESTROY(FSessionColors);
  319. SAFE_DESTROY(FSessionsDragDropFilesEx);
  320. SAFE_DESTROY(FHistoryMenu[0][0]);
  321. SAFE_DESTROY(FHistoryMenu[0][1]);
  322. SAFE_DESTROY(FHistoryMenu[1][0]);
  323. SAFE_DESTROY(FHistoryMenu[1][1]);
  324. if (FHiddenWindow != NULL)
  325. {
  326. DestroyWindow(FHiddenWindow);
  327. FHiddenWindow = NULL;
  328. }
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  332. {
  333. std::unique_ptr<TSessionData> Data;
  334. if (!Session.IsEmpty())
  335. {
  336. bool DefaultsOnly;
  337. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  338. }
  339. TTerminalManager * Manager = TTerminalManager::Instance();
  340. for (int Index = 0; Index < Manager->Count; Index++)
  341. {
  342. TTerminal * Terminal = Manager->Terminals[Index];
  343. if (Session.IsEmpty() ||
  344. Terminal->SessionData->IsSameSite(Data.get()))
  345. {
  346. if (Path.IsEmpty())
  347. {
  348. Terminal->ClearCaches();
  349. }
  350. else
  351. {
  352. Terminal->DirectoryModified(Path, true);
  353. }
  354. }
  355. }
  356. // We should flag a pending refresh for the background terminals or busy foreground terminals
  357. if ((Terminal != NULL) && Terminal->Active &&
  358. CanCommandLineFromAnotherInstance() &&
  359. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  360. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  361. {
  362. Terminal->ReloadDirectory();
  363. }
  364. }
  365. //---------------------------------------------------------------------------
  366. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  367. {
  368. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  369. size_t MessageSize = sizeof(TCopyDataMessage);
  370. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  371. if (Result)
  372. {
  373. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  374. Result = (Message.Version == TCopyDataMessage::Version1);
  375. if (Result)
  376. {
  377. switch (Message.Command)
  378. {
  379. case TCopyDataMessage::CommandCanCommandLine:
  380. Result = CanCommandLineFromAnotherInstance();
  381. break;
  382. case TCopyDataMessage::CommandCommandLine:
  383. {
  384. UnicodeString CommandLine(Message.CommandLine);
  385. Result = CommandLineFromAnotherInstance(CommandLine);
  386. }
  387. break;
  388. case TCopyDataMessage::RefreshPanel:
  389. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  390. break;
  391. case TCopyDataMessage::MainWindowCheck:
  392. Result = true;
  393. break;
  394. default:
  395. Result = false;
  396. break;
  397. }
  398. }
  399. }
  400. Message.Result = Result ? 1 : 0;
  401. }
  402. //---------------------------------------------------------------------------
  403. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  404. {
  405. WNDCLASS WindowClass = {0};
  406. WindowClass.lpfnWndProc = DefWindowProc;
  407. WindowClass.hInstance = HInstance;
  408. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  409. FHiddenWindow = NULL;
  410. if (RegisterClass(&WindowClass))
  411. {
  412. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  413. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  414. }
  415. }
  416. //---------------------------------------------------------------------------
  417. bool __fastcall TCustomScpExplorerForm::CanConsole()
  418. {
  419. return
  420. (Terminal != NULL) &&
  421. Terminal->Active &&
  422. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  426. {
  427. bool Result = Visible && !NonVisualDataModule->Busy;
  428. return Result;
  429. }
  430. //---------------------------------------------------------------------------
  431. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  432. const UnicodeString & CommandLine)
  433. {
  434. TProgramParams Params(CommandLine);
  435. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  436. if (Result)
  437. {
  438. NonVisualDataModule->StartBusy();
  439. try
  440. {
  441. // this action is initiated from another process,
  442. // so it's likely that our window is not visible,
  443. // and user won't see what is going on
  444. Application->BringToFront();
  445. // reload sessions as we may be asked to open a session
  446. // just stored by another instance
  447. StoredSessions->Reload();
  448. UnicodeString SessionName = Params.Param[1];
  449. std::unique_ptr<TObjectList> DataList(new TObjectList());
  450. UnicodeString DownloadFile; // unused
  451. try
  452. {
  453. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  454. }
  455. catch (EAbort &)
  456. {
  457. // Happens when opening session in PuTTY and in other situations,
  458. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  459. // to process the command-line on its own (or to pass it to yet another instance)
  460. }
  461. if (DataList->Count > 0)
  462. {
  463. TTerminalManager * Manager = TTerminalManager::Instance();
  464. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  465. }
  466. }
  467. __finally
  468. {
  469. NonVisualDataModule->EndBusy();
  470. }
  471. }
  472. return Result;
  473. }
  474. //---------------------------------------------------------------------------
  475. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  476. {
  477. if (FTerminal != value)
  478. {
  479. TerminalChanging();
  480. DoSetTerminal(value, false);
  481. }
  482. }
  483. //---------------------------------------------------------------------------
  484. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  485. {
  486. FTerminal = value;
  487. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  488. FAllowTransferPresetAutoSelect = false;
  489. try
  490. {
  491. TerminalChanged(Replace);
  492. }
  493. __finally
  494. {
  495. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  496. }
  497. if (Terminal != NULL)
  498. {
  499. TransferPresetAutoSelect();
  500. }
  501. }
  502. //---------------------------------------------------------------------------
  503. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  504. {
  505. DoSetTerminal(value, true);
  506. }
  507. //---------------------------------------------------------------------------
  508. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  509. {
  510. if (FTerminal != NULL)
  511. {
  512. UpdateTerminal(Terminal);
  513. }
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  517. {
  518. if (Terminal != NULL)
  519. {
  520. UpdateSessionColor((TColor)Terminal->StateData->Color);
  521. }
  522. DoTerminalListChanged();
  523. DebugAssert(!IsLocalBrowserMode()); // TODO
  524. if (Replaced)
  525. {
  526. RemoteDirView->ReplaceTerminal(Terminal);
  527. }
  528. else
  529. {
  530. RemoteDirView->Terminal = Terminal;
  531. }
  532. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  533. if (Terminal != NULL)
  534. {
  535. if (Terminal->Active)
  536. {
  537. Terminal->RefreshDirectory();
  538. }
  539. if (WinConfiguration->PreservePanelState)
  540. {
  541. if (Terminal->RemoteExplorerState != NULL)
  542. {
  543. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  544. }
  545. else
  546. {
  547. DirView(osRemote)->ClearState();
  548. }
  549. }
  550. if (!Replaced && Terminal->Active)
  551. {
  552. InitStatusBar();
  553. }
  554. }
  555. UpdateTransferList();
  556. // Update panels Enable state before refreshing the labels
  557. UpdateControls();
  558. }
  559. //---------------------------------------------------------------------------
  560. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  561. {
  562. if (Queue != value)
  563. {
  564. if (FQueue != NULL)
  565. {
  566. FQueue->OnListUpdate = NULL;
  567. FQueue->OnQueueItemUpdate = NULL;
  568. }
  569. FQueue = value;
  570. if (FQueue != NULL)
  571. {
  572. DebugAssert(FQueue->OnListUpdate == NULL);
  573. FQueue->OnListUpdate = QueueListUpdate;
  574. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  575. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  576. }
  577. QueueChanged();
  578. }
  579. }
  580. //---------------------------------------------------------------------------
  581. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  582. TListItem * Item)
  583. {
  584. if (FQueueActedItem == Item)
  585. {
  586. FQueueActedItem = NULL;
  587. if ((QueueView3->PopupMenu != NULL) &&
  588. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  589. {
  590. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  591. ReleaseCapture();
  592. }
  593. }
  594. if (Item->Data == FPendingQueueActionItem)
  595. {
  596. FPendingQueueActionItem = NULL;
  597. }
  598. }
  599. //---------------------------------------------------------------------------
  600. bool TCustomScpExplorerForm::IsAnythingQueued()
  601. {
  602. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  603. }
  604. //---------------------------------------------------------------------------
  605. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  606. {
  607. {
  608. TGuard Guard(FQueueStatusSection);
  609. FQueueStatusInvalidated = false;
  610. if (FQueue != NULL)
  611. {
  612. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  613. }
  614. }
  615. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  616. {
  617. FMaxQueueLength = FQueueStatus->Count;
  618. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  619. }
  620. FQueueController->UpdateQueueStatus(FQueueStatus);
  621. SetQueueProgress();
  622. UpdateQueueView();
  623. bool IsEmpty = !IsAnythingQueued();
  624. if (IsEmpty && (Terminal != NULL))
  625. {
  626. TOnceDoneOperation OnceDoneOperation =
  627. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  628. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  629. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  630. {
  631. FQueue->Enabled = false;
  632. }
  633. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  634. {
  635. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  636. }
  637. }
  638. }
  639. //---------------------------------------------------------------------------
  640. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  641. {
  642. UnicodeString Result;
  643. if (FQueueStatus != NULL)
  644. {
  645. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  646. {
  647. TFileOperationProgressType * ProgressData =
  648. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  649. if ((ProgressData != NULL) && ProgressData->InProgress)
  650. {
  651. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  652. }
  653. }
  654. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  655. {
  656. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  657. }
  658. }
  659. return Result;
  660. }
  661. //---------------------------------------------------------------------------
  662. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  663. {
  664. ComponentVisible[fcQueueView] =
  665. (WinConfiguration->QueueView.Show == qvShow) ||
  666. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  667. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  668. }
  669. //---------------------------------------------------------------------------
  670. void __fastcall TCustomScpExplorerForm::QueueChanged()
  671. {
  672. if (FQueueStatus != NULL)
  673. {
  674. delete FQueueStatus;
  675. FQueueStatus = NULL;
  676. }
  677. UpdateQueueStatus(true);
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  681. {
  682. if (FQueue == Queue)
  683. {
  684. FQueueStatusInvalidated = true;
  685. }
  686. }
  687. //---------------------------------------------------------------------------
  688. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  689. TQueueItem * Item)
  690. {
  691. if (FQueue == Queue)
  692. {
  693. TGuard Guard(FQueueStatusSection);
  694. // this may be running in parallel with QueueChanged
  695. if (FQueueStatus != NULL)
  696. {
  697. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  698. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  699. {
  700. FRefreshLocalDirectory = (QueueItem == NULL) ||
  701. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  702. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  703. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  704. }
  705. if (QueueItem != NULL)
  706. {
  707. QueueItem->UserData = (void*)true;
  708. FQueueItemInvalidated = true;
  709. }
  710. }
  711. }
  712. }
  713. //---------------------------------------------------------------------------
  714. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  715. {
  716. // during standalone editing, we have no way to see/control queue,
  717. // so we have to always popup prompts automatically
  718. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  719. }
  720. //---------------------------------------------------------------------------
  721. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  722. {
  723. if (FQueueStatus != NULL)
  724. {
  725. bool QueueAutoPopup = IsQueueAutoPopup();
  726. bool NeedRefresh = FQueueController->NeedRefresh();
  727. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  728. FQueueItemInvalidated = false;
  729. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  730. bool Updated = false;
  731. TQueueItemProxy * QueueItem;
  732. bool WasUserAction;
  733. for (int Index = 0; Index < Limit; Index++)
  734. {
  735. bool Update = false;
  736. QueueItem = FQueueStatus->Items[Index];
  737. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  738. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  739. (FPendingQueueActionItem == NULL))
  740. {
  741. FPendingQueueActionItem = QueueItem;
  742. FUserActionTimer->Enabled = true;
  743. }
  744. if ((bool)QueueItem->UserData)
  745. {
  746. QueueItem->UserData = (void*)false;
  747. QueueItem->Update();
  748. Updated = true;
  749. Update = true;
  750. }
  751. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  752. {
  753. Update = true;
  754. }
  755. if (Update)
  756. {
  757. FQueueController->RefreshQueueItem(QueueItem);
  758. }
  759. }
  760. if (Updated)
  761. {
  762. NonVisualDataModule->UpdateNonVisibleActions();
  763. SetQueueProgress();
  764. }
  765. }
  766. }
  767. //---------------------------------------------------------------------------
  768. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  769. {
  770. if (FTaskbarList != NULL)
  771. {
  772. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  773. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  774. }
  775. }
  776. //---------------------------------------------------------------------------
  777. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  778. {
  779. if (Progress >= 0)
  780. {
  781. if (FTaskbarList != NULL)
  782. {
  783. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  784. }
  785. }
  786. else
  787. {
  788. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  789. }
  790. }
  791. //---------------------------------------------------------------------------
  792. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  793. {
  794. int OverallProgress;
  795. // FProgressForm is null when this is called from SetQueueProgress
  796. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  797. {
  798. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  799. }
  800. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  801. {
  802. OverallProgress = ProgressData->OverallProgress();
  803. }
  804. else
  805. {
  806. OverallProgress = -1;
  807. }
  808. SetTaskbarListProgressValue(OverallProgress);
  809. }
  810. //---------------------------------------------------------------------------
  811. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  812. {
  813. TTerminalManager::Instance()->QueueStatusUpdated();
  814. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  815. {
  816. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  817. {
  818. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  819. {
  820. TFileOperationProgressType * ProgressData = NULL;
  821. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  822. {
  823. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  824. }
  825. if ((ProgressData != NULL) &&
  826. ProgressData->InProgress)
  827. {
  828. SetTaskbarListProgressValue(ProgressData);
  829. }
  830. else
  831. {
  832. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  833. }
  834. }
  835. else
  836. {
  837. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  838. }
  839. }
  840. else
  841. {
  842. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  843. }
  844. }
  845. UpdateQueueLabel();
  846. }
  847. //---------------------------------------------------------------------------
  848. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  849. {
  850. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  851. if (FQueueStatus != NULL)
  852. {
  853. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  854. if (ActiveAndPendingPrimaryCount > 0)
  855. {
  856. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  857. }
  858. }
  859. QueueLabel->Caption = Caption;
  860. }
  861. //---------------------------------------------------------------------------
  862. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  863. {
  864. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  865. static_cast<TComponent*>(GetComponent(fcTransferList)));
  866. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  867. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  868. TransferList->Strings->BeginUpdate();
  869. try
  870. {
  871. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  872. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  873. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  874. if (FTransferDropDownHint.IsEmpty())
  875. {
  876. FTransferDropDownHint = TransferDropDown->Hint;
  877. }
  878. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  879. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  880. TransferDropDown->Text = StripHotkey(Name);
  881. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  882. (FTransferDropDownHint, StripHotkey(Name),
  883. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  884. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  885. // update the label, otherwise when it is updated only on the first draw
  886. // of the list, it is drawn "bold" for some reason
  887. FTransferListHoverIndex = TransferList->ItemIndex;
  888. UpdateTransferLabel();
  889. }
  890. __finally
  891. {
  892. TransferList->Strings->EndUpdate();
  893. }
  894. }
  895. //---------------------------------------------------------------------------
  896. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  897. {
  898. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  899. DebugAssert(Toolbar != NULL);
  900. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  901. }
  902. //---------------------------------------------------------------------------
  903. void __fastcall TCustomScpExplorerForm::UpdateActions()
  904. {
  905. TForm::UpdateActions();
  906. if (ComponentVisible[fcCustomCommandsBand])
  907. {
  908. UpdateCustomCommandsToolbar();
  909. }
  910. }
  911. //---------------------------------------------------------------------------
  912. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  913. {
  914. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  915. }
  916. //---------------------------------------------------------------------------
  917. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  918. {
  919. Color = GetBtnFaceColor();
  920. DebugAssert(Configuration && RemoteDirView);
  921. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  922. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  923. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  924. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  925. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  926. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  927. {
  928. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  929. // selection is not redrawn automatically when RowSelect changes
  930. RemoteDirView->Invalidate();
  931. }
  932. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  933. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  934. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  935. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  936. UpdateSessionsPageControlHeight();
  937. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  938. UpdateToolbarDisplayMode();
  939. UpdateTransferList();
  940. if (Terminal != NULL)
  941. {
  942. TransferPresetAutoSelect();
  943. }
  944. UpdateQueueView();
  945. UpdateControls();
  946. // this can be called even before constuctor finishes.
  947. if (FEditorManager != NULL)
  948. {
  949. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  950. }
  951. // this can be called even before constuctor finishes.
  952. if (FLocalEditors != NULL)
  953. {
  954. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  955. {
  956. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  957. }
  958. }
  959. if (ComponentVisible[fcCustomCommandsBand])
  960. {
  961. UpdateCustomCommandsToolbar();
  962. }
  963. // show only when keeping queue items forever,
  964. // otherwise, its enough to have in in the context menu
  965. QueueDeleteAllDoneQueueToolbarItem->Visible =
  966. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  967. if (FFileColorsCurrent != WinConfiguration->FileColors)
  968. {
  969. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  970. FFileColorsCurrent = WinConfiguration->FileColors;
  971. FileColorsChanged();
  972. }
  973. }
  974. //---------------------------------------------------------------------------
  975. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  976. {
  977. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  978. DirView->Invalidate();
  979. }
  980. //---------------------------------------------------------------------------
  981. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  982. {
  983. DoFileColorsChanged(RemoteDirView);
  984. }
  985. //---------------------------------------------------------------------------
  986. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  987. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  988. void * /*Arg*/)
  989. {
  990. if (Token != NULL)
  991. {
  992. TForm * Editor = dynamic_cast<TForm*>(Token);
  993. DebugAssert(Editor != NULL);
  994. ReconfigureEditorForm(Editor);
  995. }
  996. }
  997. //---------------------------------------------------------------------------
  998. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  999. {
  1000. WinConfiguration->DDTransferConfirmation = asOn;
  1001. }
  1002. //---------------------------------------------------------------------------
  1003. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1004. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1005. {
  1006. // noop
  1007. }
  1008. //---------------------------------------------------------------------------
  1009. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1010. TTransferDirection Direction, TTransferType Type, bool Temp,
  1011. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1012. bool Confirm, bool DragDrop, int Options)
  1013. {
  1014. bool Result = true;
  1015. DebugAssert(Terminal && Terminal->Active);
  1016. // these parameters are known in advance
  1017. int Params =
  1018. FLAGMASK(Type == ttMove, cpDelete);
  1019. bool ToTemp = (Temp && (Direction == tdToLocal));
  1020. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1021. {
  1022. bool DisableNewerOnly =
  1023. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1024. ToTemp;
  1025. Options |=
  1026. FLAGMASK(ToTemp, coTemp) |
  1027. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1028. coDoNotShowAgain;
  1029. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1030. int CopyParamAttrs = (Direction == tdToRemote ?
  1031. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1032. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1033. int OutputOptions =
  1034. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1035. cooDoNotShowAgain);
  1036. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1037. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1038. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1039. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1040. if (Result)
  1041. {
  1042. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1043. {
  1044. if (DragDrop)
  1045. {
  1046. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1047. {
  1048. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1049. NULL, 0, EnableDDTransferConfirmation, NULL);
  1050. }
  1051. WinConfiguration->DDTransferConfirmation = asOff;
  1052. }
  1053. else
  1054. {
  1055. WinConfiguration->ConfirmTransferring = false;
  1056. }
  1057. }
  1058. else
  1059. {
  1060. // User explicitly unchecked "do not show again",
  1061. // so show him the dialog the next time
  1062. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1063. {
  1064. WinConfiguration->DDTransferConfirmation = asOn;
  1065. }
  1066. }
  1067. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1068. }
  1069. }
  1070. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1071. {
  1072. Configuration->Usage->Inc(L"TransfersOnBackground");
  1073. // these parameter are known only after transfer dialog
  1074. Params |=
  1075. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1076. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1077. Result = false;
  1078. ClearTransferSourceSelection(Direction);
  1079. }
  1080. return Result;
  1081. }
  1082. //---------------------------------------------------------------------------
  1083. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1084. {
  1085. if (FOnFileOperationFinished != NULL)
  1086. {
  1087. FOnFileOperationFinished(UnicodeString(), true);
  1088. }
  1089. else
  1090. {
  1091. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1092. if (HasDirView[Side])
  1093. {
  1094. DirView(Side)->SelectAll(smNone);
  1095. }
  1096. }
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1100. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1101. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1102. int Params)
  1103. {
  1104. DebugAssert(Queue != NULL);
  1105. bool SingleFile = false;
  1106. if (FileList->Count == 1)
  1107. {
  1108. if (Direction == tdToRemote)
  1109. {
  1110. UnicodeString FileName = FileList->Strings[0];
  1111. SingleFile = FileExists(ApiPath(FileName));
  1112. }
  1113. else
  1114. {
  1115. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1116. SingleFile = !File->IsDirectory;
  1117. }
  1118. }
  1119. TQueueItem * QueueItem;
  1120. if (Direction == tdToRemote)
  1121. {
  1122. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1123. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1124. }
  1125. else
  1126. {
  1127. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1128. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1129. }
  1130. AddQueueItem(Queue, QueueItem, Terminal);
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1134. {
  1135. if (Queue->IsEmpty)
  1136. {
  1137. Terminal->QueueOperationStart = Now();
  1138. }
  1139. Queue->AddItem(QueueItem);
  1140. }
  1141. //---------------------------------------------------------------------------
  1142. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1143. {
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. void __fastcall TCustomScpExplorerForm::InitControls()
  1147. {
  1148. DebugAssert(FDocks != NULL);
  1149. for (int Index = 0; Index < ComponentCount; Index++)
  1150. {
  1151. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1152. if ((Dock != NULL) && (Dock->Tag == 0))
  1153. {
  1154. FDocks->Add(Dock);
  1155. }
  1156. }
  1157. CollectItemsWithTextDisplayMode(this);
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1161. {
  1162. ConfigurationChanged();
  1163. QueuePanel->Height =
  1164. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1165. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1166. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1167. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1168. UpdateCustomCommandsToolbar();
  1169. }
  1170. //---------------------------------------------------------------------------
  1171. void __fastcall TCustomScpExplorerForm::StoreParams()
  1172. {
  1173. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1174. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1175. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1176. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1177. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1178. }
  1179. //---------------------------------------------------------------------------
  1180. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1181. {
  1182. if (!FFormRestored)
  1183. {
  1184. FFormRestored = true;
  1185. RestoreFormParams();
  1186. }
  1187. TForm::CreateParams(Params);
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1191. {
  1192. DebugAssert(FDocks != NULL);
  1193. for (int Index = 0; Index < FDocks->Count; Index++)
  1194. {
  1195. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1196. }
  1197. }
  1198. //---------------------------------------------------------------------------
  1199. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1200. {
  1201. SetDockAllowDrag(true);
  1202. while (!ButtonsStr.IsEmpty())
  1203. {
  1204. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1205. UnicodeString Name = CutToChar(S, L'=', true);
  1206. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1207. {
  1208. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1209. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1210. {
  1211. while (!S.IsEmpty())
  1212. {
  1213. UnicodeString S2 = CutToChar(S, L',', true);
  1214. UnicodeString Name = CutToChar(S2, L':', true);
  1215. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1216. {
  1217. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1218. if (SameText(GetToolbarItemName(Item), Name))
  1219. {
  1220. // forward compatibility
  1221. UnicodeString S3 = CutToChar(S2, L':', true);
  1222. bool Visible = StrToIntDef(S3, 0);
  1223. Item->Visible = Visible;
  1224. }
  1225. }
  1226. }
  1227. }
  1228. }
  1229. }
  1230. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1231. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1232. }
  1233. //---------------------------------------------------------------------------
  1234. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1235. {
  1236. UnicodeString Result;
  1237. SetDockAllowDrag(true);
  1238. Result = ::GetToolbarsLayoutStr(this);
  1239. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1240. return Result;
  1241. }
  1242. //---------------------------------------------------------------------------
  1243. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1244. {
  1245. UnicodeString Result;
  1246. if (Item->Action != NULL)
  1247. {
  1248. Result = Item->Action->Name;
  1249. Result = RemoveSuffix(Result, L"Action");
  1250. }
  1251. else
  1252. {
  1253. Result = Item->Name;
  1254. Result = RemoveSuffix(Result, L"SubmenuItem");
  1255. Result = RemoveSuffix(Result, L"Item");
  1256. }
  1257. return Result;
  1258. }
  1259. //---------------------------------------------------------------------------
  1260. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1261. {
  1262. UnicodeString Result;
  1263. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1264. {
  1265. UnicodeString ToolbarStr;
  1266. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1267. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1268. {
  1269. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1270. {
  1271. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1272. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1273. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1274. // and save the changes here only.
  1275. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1276. {
  1277. UnicodeString Name = GetToolbarItemName(Item);
  1278. DebugAssert(Name.Pos(L"TBX") == 0);
  1279. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1280. }
  1281. }
  1282. }
  1283. if (!ToolbarStr.IsEmpty())
  1284. {
  1285. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1286. }
  1287. }
  1288. return Result;
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1292. {
  1293. DebugAssert(FProgressForm == NULL);
  1294. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fcSkipTransfer] : false;
  1295. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1296. FProgressForm->DeleteLocalToRecycleBin =
  1297. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1298. FProgressForm->DeleteRemoteToRecycleBin =
  1299. (Terminal != NULL) &&
  1300. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1301. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1302. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1303. LockWindow();
  1304. // Actually, do not know what hides the progress once the operation finishes
  1305. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1306. SetTaskbarListProgressState(TBPF_NORMAL);
  1307. }
  1308. //---------------------------------------------------------------------------
  1309. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1310. {
  1311. UnlockWindow();
  1312. SAFE_DESTROY(FProgressForm);
  1313. SetQueueProgress();
  1314. }
  1315. //---------------------------------------------------------------------------
  1316. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1317. TFileOperationProgressType & ProgressData)
  1318. {
  1319. // Download to temporary local directory
  1320. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1321. {
  1322. bool Continue = true;
  1323. FPendingTempSpaceWarn = false;
  1324. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1325. if (!Continue)
  1326. {
  1327. Abort();
  1328. }
  1329. }
  1330. // operation is being executed and we still didn't show up progress form
  1331. if (ProgressData.InProgress && !FProgressForm)
  1332. {
  1333. CreateProgressForm(NULL);
  1334. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1335. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1336. }
  1337. // operation is finished (or terminated), so we hide progress form
  1338. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1339. {
  1340. DestroyProgressForm();
  1341. if (ProgressData.Operation == foCalculateSize)
  1342. {
  1343. // When calculating size before transfer, the abort caused by
  1344. // cancel flag set due to "MoveToQueue" below,
  1345. // is not propagated back to us in ExecuteFileOperation,
  1346. // as it is silently swallowed.
  1347. // So we explicitly (re)throw it here.
  1348. if (FMoveToQueue)
  1349. {
  1350. Abort();
  1351. }
  1352. }
  1353. else
  1354. {
  1355. if ((ProgressData.Cancel == csContinue) &&
  1356. !FAutoOperation)
  1357. {
  1358. OperationComplete(ProgressData.StartTime);
  1359. }
  1360. }
  1361. }
  1362. if (FProgressForm &&
  1363. // Can happen only during synchronization, when ending one of the operations
  1364. ProgressData.InProgress)
  1365. {
  1366. FProgressForm->SetProgressData(ProgressData);
  1367. DebugAssert(ProgressData.InProgress);
  1368. SetTaskbarListProgressValue(&ProgressData);
  1369. if (FProgressForm->Cancel > csContinue)
  1370. {
  1371. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1372. if (FProgressForm->Cancel == csCancelFile)
  1373. {
  1374. FProgressForm->ClearCancel();
  1375. }
  1376. // cancel cancels even the move
  1377. FMoveToQueue = false;
  1378. }
  1379. else if (FProgressForm->MoveToQueue)
  1380. {
  1381. FMoveToQueue = true;
  1382. ProgressData.SetCancelAtLeast(csCancel);
  1383. }
  1384. if ((FTransferResumeList != NULL) &&
  1385. ProgressData.InProgress &&
  1386. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1387. !ProgressData.FullFileName.IsEmpty())
  1388. {
  1389. if ((FTransferResumeList->Count == 0) ||
  1390. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1391. {
  1392. // note that we do not recognize directories from files here
  1393. FTransferResumeList->Add(ProgressData.FullFileName);
  1394. }
  1395. }
  1396. }
  1397. }
  1398. //---------------------------------------------------------------------------
  1399. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1400. const TDateTime & StartTime)
  1401. {
  1402. if (GUIConfiguration->BeepOnFinish &&
  1403. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1404. {
  1405. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1406. DWORD Sound;
  1407. if (!ExtractFileExt(BeepSound).IsEmpty())
  1408. {
  1409. Sound = SND_FILENAME;
  1410. }
  1411. else
  1412. {
  1413. Sound = SND_ALIAS;
  1414. }
  1415. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1416. }
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1420. TFileOperationProgressType & ProgressData)
  1421. {
  1422. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1423. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1424. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1425. FileOperationProgress(ProgressData);
  1426. }
  1427. //---------------------------------------------------------------------------
  1428. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1429. {
  1430. UnicodeString Result;
  1431. if (FProgressForm != NULL)
  1432. {
  1433. Result = FProgressForm->ProgressStr();
  1434. }
  1435. return Result;
  1436. }
  1437. //---------------------------------------------------------------------------
  1438. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1439. bool DragDrop)
  1440. {
  1441. return (!DragDrop && (DropSourceControl == NULL)) ||
  1442. (DropSourceControl == DirView(osOther));
  1443. }
  1444. //---------------------------------------------------------------------------
  1445. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1446. TFileOperation Operation, TOperationSide Side,
  1447. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1448. TOnceDoneOperation & OnceDoneOperation)
  1449. {
  1450. if (!FAutoOperation)
  1451. {
  1452. // no selection on "/upload", form servers only as event handler
  1453. // (it is not displayed)
  1454. if (PanelOperation(Side, FDragDropOperation) &&
  1455. Visible && (Operation != foCalculateSize) &&
  1456. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1457. {
  1458. if (FOnFileOperationFinished != NULL)
  1459. {
  1460. FOnFileOperationFinished(FileName, Success);
  1461. }
  1462. else
  1463. {
  1464. TCustomDirView * DView = DirView(Side);
  1465. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1466. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1467. // this can happen when local drive is unplugged in the middle of the operation
  1468. if (Item != NULL)
  1469. {
  1470. if (Success) Item->Selected = false;
  1471. DView->MakeProgressVisible(Item);
  1472. }
  1473. }
  1474. }
  1475. if ((Operation == foCopy) || (Operation == foMove))
  1476. {
  1477. DebugAssert(!IsLocalBrowserMode()); // TODO
  1478. if (Side == osLocal)
  1479. {
  1480. Configuration->Usage->Inc(L"UploadedFiles");
  1481. }
  1482. else
  1483. {
  1484. Configuration->Usage->Inc(L"DownloadedFiles");
  1485. }
  1486. }
  1487. }
  1488. if (Success && (FSynchronizeController != NULL))
  1489. {
  1490. DebugAssert(!IsSideLocalBrowser(osRemote));
  1491. if (Operation == foCopy)
  1492. {
  1493. DebugAssert(Side == osLocal);
  1494. FSynchronizeController->LogOperation(soUpload, FileName);
  1495. }
  1496. else if (Operation == foDelete)
  1497. {
  1498. DebugAssert(Side == osRemote);
  1499. FSynchronizeController->LogOperation(soDelete, FileName);
  1500. }
  1501. }
  1502. if (FProgressForm)
  1503. {
  1504. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1505. }
  1506. }
  1507. //---------------------------------------------------------------------------
  1508. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1509. TFileOperation Operation, TOperationSide Side,
  1510. bool Temp, const UnicodeString & FileName, Boolean Success,
  1511. TOnceDoneOperation & OnceDoneOperation)
  1512. {
  1513. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1514. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1515. OnceDoneOperation);
  1516. }
  1517. //---------------------------------------------------------------------------
  1518. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1519. {
  1520. return false;
  1521. }
  1522. //---------------------------------------------------------------------------
  1523. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1524. {
  1525. return false;
  1526. }
  1527. //---------------------------------------------------------------------------
  1528. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1529. {
  1530. return NULL;
  1531. }
  1532. //---------------------------------------------------------------------------
  1533. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1534. {
  1535. DebugAssert(GetSide(Side) == osRemote);
  1536. DebugUsedParam(Side);
  1537. return RemoteDirView;
  1538. }
  1539. //---------------------------------------------------------------------------
  1540. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1541. {
  1542. DebugAssert(GetSide(Side) == osRemote);
  1543. DebugUsedParam(Side);
  1544. return RemoteDriveView;
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1548. {
  1549. DebugAssert(GetSide(Side) == osRemote);
  1550. DebugUsedParam(Side);
  1551. return (Terminal != NULL) && Terminal->Active;
  1552. }
  1553. //---------------------------------------------------------------------------
  1554. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1555. TOperationSide Side, int FilesOnly)
  1556. {
  1557. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1558. }
  1559. //---------------------------------------------------------------------------
  1560. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1561. TOperationSide Side, int FilesOnly)
  1562. {
  1563. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1564. }
  1565. //---------------------------------------------------------------------------
  1566. struct THistoryItemData
  1567. {
  1568. short int Side;
  1569. short int Index;
  1570. };
  1571. //---------------------------------------------------------------------------
  1572. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1573. {
  1574. DirView(Side)->HistoryGo(Index);
  1575. }
  1576. //---------------------------------------------------------------------------
  1577. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1578. {
  1579. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1580. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1581. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1585. bool Back)
  1586. {
  1587. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1588. {
  1589. TCustomDirView * DView = DirView(Side);
  1590. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1591. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1592. if (ICount > 10)
  1593. {
  1594. ICount = 10;
  1595. }
  1596. Menu->Items->Clear();
  1597. THistoryItemData Data;
  1598. Data.Side = Side;
  1599. for (short int i = 1; i <= ICount; i++)
  1600. {
  1601. TTBCustomItem * Item = new TTBXItem(Menu);
  1602. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1603. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1604. Item->Hint = DView->HistoryPath[Data.Index];
  1605. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1606. Item->Tag = *reinterpret_cast<int*>(&Data);
  1607. Item->OnClick = HistoryItemClick;
  1608. Menu->Items->Add(Item);
  1609. }
  1610. }
  1611. }
  1612. //---------------------------------------------------------------------------
  1613. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1614. TOperationSide Side, bool Back)
  1615. {
  1616. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1617. {
  1618. // workaround
  1619. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1620. // operator new allocates memory in Pascal code, but calls inline
  1621. // contructor in C++, leading in problems, the function does
  1622. // both in Pascal code
  1623. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1624. UpdateHistoryMenu(Side, Back);
  1625. }
  1626. return FHistoryMenu[Side == osLocal][Back];
  1627. }
  1628. //---------------------------------------------------------------------------
  1629. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1630. TCustomDirView *Sender)
  1631. {
  1632. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1633. UpdateHistoryMenu(Side, true);
  1634. UpdateHistoryMenu(Side, false);
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1638. {
  1639. // remote custom commands can be executed only if the server supports shell commands
  1640. // or have secondary shell
  1641. return (FTerminal != NULL) && FTerminal->Active && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1642. }
  1643. //---------------------------------------------------------------------------
  1644. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1645. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1646. {
  1647. int Result;
  1648. TFileCustomCommand RemoteCustomCommand;
  1649. TLocalCustomCommand LocalCustomCommand;
  1650. TFileCustomCommand * NonInteractiveCustomCommand =
  1651. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1652. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1653. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1654. if (FLAGCLEAR(Command.Params, ccLocal))
  1655. {
  1656. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1657. // custom command that does not operate with files can be executed anytime ...
  1658. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1659. {
  1660. // ... but do not show such command in remote file menu
  1661. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1662. {
  1663. Result = AllowedState;
  1664. }
  1665. else
  1666. {
  1667. Result = -1;
  1668. }
  1669. }
  1670. else
  1671. {
  1672. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1673. {
  1674. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1675. }
  1676. else
  1677. {
  1678. Result = -1;
  1679. }
  1680. }
  1681. }
  1682. else
  1683. {
  1684. // custom command that does not operate with files can be executed anytime
  1685. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1686. {
  1687. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1688. }
  1689. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1690. {
  1691. if ((ListType == ccltAll) || (ListType == ccltFile))
  1692. {
  1693. // special case is "diff"-style command that can be executed over any side,
  1694. // if we have both sides
  1695. Result =
  1696. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1697. // directly (instead of EnableSelectedOperation) to pass
  1698. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1699. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1700. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1701. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1702. }
  1703. else if (ListType == ccltBoth)
  1704. {
  1705. DebugAssert(!IsLocalBrowserMode());
  1706. Result = 1;
  1707. }
  1708. else
  1709. {
  1710. Result = -1;
  1711. }
  1712. }
  1713. else
  1714. {
  1715. if ((ListType == ccltAll) ||
  1716. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1717. {
  1718. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1719. }
  1720. else
  1721. {
  1722. Result = -1;
  1723. }
  1724. }
  1725. }
  1726. return Result;
  1727. }
  1728. //---------------------------------------------------------------------------
  1729. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1730. TStrings * FileList, const TCustomCommandType & ACommand,
  1731. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1732. {
  1733. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1734. {
  1735. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1736. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1737. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1738. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1739. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1740. bool Capture =
  1741. FLAGSET(ACommand.Params, ccShowResults) ||
  1742. FLAGSET(ACommand.Params, ccCopyResults) ||
  1743. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1744. TCaptureOutputEvent OutputEvent = NULL;
  1745. DebugAssert(FCapturedLog == NULL);
  1746. if (Capture)
  1747. {
  1748. FCapturedLog = new TStringList();
  1749. OutputEvent = TerminalCaptureLog;
  1750. }
  1751. try
  1752. {
  1753. if (!RemoteCustomCommand.IsFileCommand(Command))
  1754. {
  1755. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1756. OutputEvent);
  1757. }
  1758. else
  1759. {
  1760. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1761. }
  1762. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1763. {
  1764. if (FLAGSET(ACommand.Params, ccCopyResults))
  1765. {
  1766. CopyToClipboard(FCapturedLog);
  1767. }
  1768. if (FLAGSET(ACommand.Params, ccShowResults))
  1769. {
  1770. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1771. }
  1772. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1773. {
  1774. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1775. }
  1776. }
  1777. }
  1778. __finally
  1779. {
  1780. SAFE_DESTROY(FCapturedLog);
  1781. }
  1782. }
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1786. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1787. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1788. {
  1789. DebugAssert(!IsLocalBrowserMode());
  1790. TStrings * LocalFileList = NULL;
  1791. TStrings * RemoteFileList = NULL;
  1792. TStrings * RemoteFileListFull = NULL;
  1793. try
  1794. {
  1795. if (LocalFileCommand)
  1796. {
  1797. if (ALocalFileList == NULL)
  1798. {
  1799. DebugAssert(HasDirView[osLocal]);
  1800. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1801. // directly (instead of EnableSelectedOperation) to pass
  1802. // false to FocusedFileOnlyWhenFocused
  1803. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1804. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1805. }
  1806. else
  1807. {
  1808. LocalFileList = ALocalFileList;
  1809. }
  1810. if (FileListCommand)
  1811. {
  1812. if (LocalFileList->Count != 1)
  1813. {
  1814. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1815. }
  1816. }
  1817. else
  1818. {
  1819. if ((LocalFileList->Count != 1) &&
  1820. (FileList->Count != 1) &&
  1821. (LocalFileList->Count != FileList->Count))
  1822. {
  1823. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1824. }
  1825. }
  1826. }
  1827. UnicodeString RootTempDir;
  1828. UnicodeString TempDir;
  1829. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1830. if (!RemoteFiles)
  1831. {
  1832. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1833. }
  1834. try
  1835. {
  1836. TDateTimes RemoteFileTimes;
  1837. if (RemoteFiles)
  1838. {
  1839. RemoteFileList = FileList;
  1840. }
  1841. else
  1842. {
  1843. RemoteFileList = new TStringList();
  1844. TMakeLocalFileListParams MakeFileListParam;
  1845. MakeFileListParam.FileList = RemoteFileList;
  1846. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1847. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1848. MakeFileListParam.Recursive =
  1849. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1850. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1851. if (!MakeFileListParam.Recursive)
  1852. {
  1853. RemoteFileTimes.clear();
  1854. RemoteFileListFull = new TStringList();
  1855. MakeFileListParam.FileList = RemoteFileListFull;
  1856. MakeFileListParam.Recursive = true;
  1857. MakeFileListParam.IncludeDirs = false;
  1858. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1859. }
  1860. else
  1861. {
  1862. RemoteFileListFull = RemoteFileList;
  1863. }
  1864. }
  1865. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1866. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1867. if (!NonBlocking)
  1868. {
  1869. int Count;
  1870. if (FileListCommand)
  1871. {
  1872. Count = 1;
  1873. }
  1874. else if (LocalFileCommand)
  1875. {
  1876. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1877. }
  1878. else
  1879. {
  1880. Count = RemoteFileList->Count;
  1881. }
  1882. Progress.Start(foCustomCommand, osRemote, Count);
  1883. DebugAssert(FProgressForm != NULL);
  1884. FProgressForm->ReadOnly = true;
  1885. }
  1886. try
  1887. {
  1888. if (FileListCommand)
  1889. {
  1890. UnicodeString LocalFile;
  1891. // MakeFileList does not delimit filenames
  1892. UnicodeString FileList = MakeFileList(RemoteFileList);
  1893. if (LocalFileCommand)
  1894. {
  1895. DebugAssert(LocalFileList->Count == 1);
  1896. LocalFile = LocalFileList->Strings[0];
  1897. }
  1898. TLocalCustomCommand CustomCommand(Data,
  1899. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1900. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1901. if (NonBlocking)
  1902. {
  1903. DebugAssert(!POutput);
  1904. ExecuteShellChecked(ShellCommand);
  1905. }
  1906. else
  1907. {
  1908. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1909. }
  1910. }
  1911. else if (LocalFileCommand)
  1912. {
  1913. if (LocalFileList->Count == 1)
  1914. {
  1915. UnicodeString LocalFile = LocalFileList->Strings[0];
  1916. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1917. {
  1918. UnicodeString FileName = RemoteFileList->Strings[Index];
  1919. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1920. Progress.SetFile(FileNameForProgress);
  1921. TLocalCustomCommand CustomCommand(Data,
  1922. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1923. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1924. TOnceDoneOperation OnceDoneOperation;
  1925. // Do not unselect anything, as it won't work with two panels anyway
  1926. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1927. }
  1928. }
  1929. else if (RemoteFileList->Count == 1)
  1930. {
  1931. UnicodeString FileName = RemoteFileList->Strings[0];
  1932. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1933. {
  1934. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1935. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1936. Progress.SetFile(LocalFileNameOnly);
  1937. TLocalCustomCommand CustomCommand(
  1938. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1939. FileName, LocalFileName, L"");
  1940. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1941. TOnceDoneOperation OnceDoneOperation;
  1942. // Do not unselect anything, as it won't work with two panels anyway
  1943. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1944. }
  1945. }
  1946. else
  1947. {
  1948. if (LocalFileList->Count != RemoteFileList->Count)
  1949. {
  1950. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1951. }
  1952. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1953. {
  1954. UnicodeString FileName = RemoteFileList->Strings[Index];
  1955. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1956. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1957. Progress.SetFile(LocalFileNameOnly); // sic
  1958. TLocalCustomCommand CustomCommand(
  1959. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1960. FileName, LocalFileName, L"");
  1961. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1962. TOnceDoneOperation OnceDoneOperation;
  1963. // Do not unselect anything, as it won't work with two panels anyway
  1964. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1965. }
  1966. }
  1967. }
  1968. else
  1969. {
  1970. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1971. {
  1972. UnicodeString FileName = RemoteFileList->Strings[Index];
  1973. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1974. Progress.SetFile(FileNameForProgress);
  1975. TLocalCustomCommand CustomCommand(Data,
  1976. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1977. FileName, L"", L"");
  1978. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1979. TOnceDoneOperation OnceDoneOperation;
  1980. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1981. }
  1982. }
  1983. }
  1984. __finally
  1985. {
  1986. if (!NonBlocking)
  1987. {
  1988. Progress.Stop();
  1989. }
  1990. }
  1991. DebugAssert(!FAutoOperation);
  1992. if (!RemoteFiles)
  1993. {
  1994. TempDir = IncludeTrailingBackslash(TempDir);
  1995. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1996. {
  1997. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1998. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1999. // Skip directories, process only nested files.
  2000. // The check is redundant as FileAge fails for directories anyway.
  2001. !DirectoryExists(FileName))
  2002. {
  2003. UnicodeString RemoteDir =
  2004. UnixExtractFileDir(
  2005. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  2006. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2007. TDateTime NewTime;
  2008. if (FileAge(FileName, NewTime) &&
  2009. (NewTime != RemoteFileTimes[Index]))
  2010. {
  2011. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2012. TemporaryFileCopyParam(CopyParam);
  2013. CopyParam.FileMask = L"";
  2014. FAutoOperation = true;
  2015. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2016. TemporaryFilesList->Add(FileName);
  2017. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2018. }
  2019. }
  2020. }
  2021. }
  2022. }
  2023. __finally
  2024. {
  2025. FAutoOperation = false;
  2026. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2027. {
  2028. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2029. }
  2030. }
  2031. }
  2032. __finally
  2033. {
  2034. if (RemoteFileList != FileList)
  2035. {
  2036. delete RemoteFileList;
  2037. }
  2038. if (LocalFileList != ALocalFileList)
  2039. {
  2040. delete LocalFileList;
  2041. }
  2042. if (RemoteFileListFull != RemoteFileList)
  2043. {
  2044. delete RemoteFileListFull;
  2045. }
  2046. }
  2047. }
  2048. //---------------------------------------------------------------------------
  2049. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2050. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2051. bool FileListCommand, UnicodeString * POutput)
  2052. {
  2053. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2054. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2055. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2056. {
  2057. UnicodeString FileName = SelectedFileList->Strings[Index];
  2058. if (DirectoryExists(FileName))
  2059. {
  2060. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2061. {
  2062. LocalFileList->Add(FileName);
  2063. }
  2064. if (FLAGSET(ACommand.Params, ccRecursive))
  2065. {
  2066. TMakeLocalFileListParams MakeFileListParam;
  2067. MakeFileListParam.FileList = LocalFileList.get();
  2068. MakeFileListParam.FileTimes = NULL;
  2069. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2070. MakeFileListParam.Recursive = true;
  2071. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2072. }
  2073. }
  2074. else
  2075. {
  2076. LocalFileList->Add(FileName);
  2077. }
  2078. }
  2079. UnicodeString RemotePath;
  2080. if (Terminal != NULL)
  2081. {
  2082. RemotePath = Terminal->CurrentDirectory;
  2083. }
  2084. if (FileListCommand)
  2085. {
  2086. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2087. TLocalCustomCommand CustomCommand(
  2088. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2089. L"", L"", FileList);
  2090. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2091. }
  2092. else
  2093. {
  2094. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2095. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2096. DebugAssert(FProgressForm != NULL);
  2097. FProgressForm->ReadOnly = true;
  2098. try
  2099. {
  2100. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2101. {
  2102. UnicodeString FileName = LocalFileList->Strings[Index];
  2103. Progress.SetFile(FileName);
  2104. TLocalCustomCommand CustomCommand(
  2105. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2106. FileName, L"", L"");
  2107. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2108. TOnceDoneOperation OnceDoneOperation;
  2109. Progress.Finish(FileName, true, OnceDoneOperation);
  2110. }
  2111. }
  2112. __finally
  2113. {
  2114. Progress.Stop();
  2115. }
  2116. }
  2117. }
  2118. //---------------------------------------------------------------------------
  2119. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2120. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2121. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2122. {
  2123. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2124. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2125. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2126. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2127. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2128. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2129. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2130. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2131. std::unique_ptr<UnicodeString> POutput;
  2132. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2133. FLAGSET(ACommand.Params, ccCopyResults))
  2134. {
  2135. POutput.reset(new UnicodeString());
  2136. }
  2137. if (!LocalCustomCommand.IsFileCommand(Command))
  2138. {
  2139. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2140. }
  2141. // remote files?
  2142. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2143. {
  2144. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2145. }
  2146. // local files
  2147. else
  2148. {
  2149. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2150. }
  2151. if (POutput.get() != NULL)
  2152. {
  2153. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2154. int P = POutput->Pos(sLineBreak);
  2155. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2156. {
  2157. POutput->SetLength(P - 1);
  2158. }
  2159. // Copy even empty output to clipboard
  2160. if (FLAGSET(ACommand.Params, ccCopyResults))
  2161. {
  2162. CopyToClipboard(*POutput);
  2163. }
  2164. // But do not show an empty message box.
  2165. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2166. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2167. !POutput->IsEmpty())
  2168. {
  2169. TClipboardHandler ClipboardHandler;
  2170. ClipboardHandler.Text = *POutput;
  2171. TMessageParams Params;
  2172. TQueryButtonAlias Aliases[1];
  2173. Aliases[0].Button = qaRetry;
  2174. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2175. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2176. Params.Aliases = Aliases;
  2177. Params.AliasesCount = LENOF(Aliases);
  2178. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2179. }
  2180. }
  2181. }
  2182. //---------------------------------------------------------------------------
  2183. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2184. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2185. {
  2186. TCustomCommandData Data;
  2187. UnicodeString Site;
  2188. UnicodeString RemotePath;
  2189. if (Terminal != NULL)
  2190. {
  2191. Data = TCustomCommandData(Terminal);
  2192. Site = Terminal->SessionData->SessionKey;
  2193. RemotePath = Terminal->CurrentDirectory;
  2194. }
  2195. UnicodeString HelpKeyword = ACommand.HomePage;
  2196. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2197. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2198. {
  2199. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2200. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2201. {
  2202. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2203. }
  2204. else
  2205. {
  2206. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2207. }
  2208. if (!DoCustomCommandOptionsDialog(
  2209. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2210. {
  2211. Abort();
  2212. }
  2213. }
  2214. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2215. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2216. {
  2217. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2218. }
  2219. else
  2220. {
  2221. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2222. }
  2223. }
  2224. //---------------------------------------------------------------------------
  2225. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2226. const TCustomCommandType & Command)
  2227. {
  2228. DebugAssert(FCustomCommandLocalFileList != NULL);
  2229. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2230. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2231. TStrings * LocalFileList = new TStringList();
  2232. TStrings * RemoteFileList = new TStringList();
  2233. try
  2234. {
  2235. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2236. {
  2237. LocalFileList->Clear();
  2238. LocalFileList->AddObject(
  2239. FCustomCommandLocalFileList->Strings[Index],
  2240. FCustomCommandLocalFileList->Objects[Index]);
  2241. RemoteFileList->Clear();
  2242. RemoteFileList->AddObject(
  2243. FCustomCommandRemoteFileList->Strings[Index],
  2244. FCustomCommandRemoteFileList->Objects[Index]);
  2245. CustomCommand(RemoteFileList, Command, LocalFileList);
  2246. }
  2247. }
  2248. __finally
  2249. {
  2250. delete LocalFileList;
  2251. delete RemoteFileList;
  2252. }
  2253. }
  2254. //---------------------------------------------------------------------------
  2255. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2256. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2257. {
  2258. delete FCustomCommandLocalFileList;
  2259. delete FCustomCommandRemoteFileList;
  2260. // takeover ownership,
  2261. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2262. FCustomCommandLocalFileList = LocalFileList;
  2263. FCustomCommandRemoteFileList = RemoteFileList;
  2264. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2265. if (Button != NULL)
  2266. {
  2267. FCustomCommandMenu->Items->Clear();
  2268. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2269. MenuPopup(FCustomCommandMenu, Button);
  2270. }
  2271. else
  2272. {
  2273. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2274. }
  2275. }
  2276. //---------------------------------------------------------------------------
  2277. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2278. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2279. {
  2280. DebugAssert(FCapturedLog != NULL);
  2281. if ((OutputType == cotOutput) || (OutputType == cotError))
  2282. {
  2283. FCapturedLog->Add(AddedLine);
  2284. }
  2285. }
  2286. //---------------------------------------------------------------------------
  2287. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2288. TOperationSide Side)
  2289. {
  2290. return
  2291. (Side == osOther) &&
  2292. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2293. }
  2294. //---------------------------------------------------------------------------
  2295. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2296. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2297. {
  2298. TTBItemOptions O;
  2299. O = Item->Options;
  2300. TCustomDirView * DView = DirView(Side);
  2301. if ((DView->ItemFocused != NULL) &&
  2302. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2303. // when resolving links is disabled, default action is to enter the directory,
  2304. // no matter what DoubleClickAction is configured to
  2305. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2306. // Can only Edit files, but can Open/Copy even directories
  2307. ((DoubleClickAction != dcaEdit) ||
  2308. !DView->ItemIsDirectory(DView->ItemFocused)))
  2309. {
  2310. Item->Options = O << tboDefault;
  2311. }
  2312. else
  2313. {
  2314. Item->Options = O >> tboDefault;
  2315. }
  2316. }
  2317. //---------------------------------------------------------------------------
  2318. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2319. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2320. {
  2321. TCustomDirView * DView = DirView(Side);
  2322. TListItem * Item = DView->ItemFocused;
  2323. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2324. EnableFocusedOperation[Side])
  2325. {
  2326. TPoint ClientPoint;
  2327. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2328. TPoint(0, 0) : MousePos;
  2329. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2330. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2331. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2332. }
  2333. }
  2334. //---------------------------------------------------------------------------
  2335. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2336. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2337. {
  2338. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2339. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2340. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2341. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2342. Handled = true;
  2343. }
  2344. //---------------------------------------------------------------------------
  2345. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2346. {
  2347. }
  2348. //---------------------------------------------------------------------------
  2349. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2350. {
  2351. DebugAssert(FErrorList == NULL);
  2352. if (WinConfiguration->ContinueOnError)
  2353. {
  2354. FErrorList = new TStringList();
  2355. Configuration->Usage->Inc(L"ContinuationsOnError");
  2356. }
  2357. NonVisualDataModule->StartBusy();
  2358. }
  2359. //---------------------------------------------------------------------------
  2360. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2361. {
  2362. NonVisualDataModule->EndBusy();
  2363. if (FErrorList)
  2364. {
  2365. HandleErrorList(FErrorList);
  2366. }
  2367. }
  2368. //---------------------------------------------------------------------------
  2369. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2370. const TCopyParamType & CopyParam)
  2371. {
  2372. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2373. {
  2374. Configuration->Usage->Inc(L"FileMaskUses");
  2375. }
  2376. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2377. {
  2378. Configuration->Usage->Inc(L"OperationMaskUses");
  2379. }
  2380. }
  2381. //---------------------------------------------------------------------------
  2382. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2383. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2384. {
  2385. DebugAssert(!IsLocalBrowserMode());
  2386. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2387. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2388. TTransferOperationParam DefaultParam;
  2389. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2390. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2391. switch (Param.Queue)
  2392. {
  2393. case asOn:
  2394. CopyParam.Queue = true;
  2395. break;
  2396. case asOff:
  2397. CopyParam.Queue = false;
  2398. break;
  2399. case asAuto:
  2400. default:
  2401. // keep default
  2402. break;
  2403. }
  2404. bool Result =
  2405. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2406. if (Result)
  2407. {
  2408. DebugAssert(Terminal);
  2409. bool SelectionRestored = false;
  2410. TCustomDirView * DView = NULL;
  2411. if (HasDirView[Side])
  2412. {
  2413. DView = DirView(Side);
  2414. DView->SaveSelection();
  2415. DView->SaveSelectedNames();
  2416. }
  2417. UpdateCopyParamCounters(CopyParam);
  2418. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2419. DebugAssert(FTransferResumeList == NULL);
  2420. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2421. FMoveToQueue = false;
  2422. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2423. try
  2424. {
  2425. TStrings * PermanentFileList;
  2426. std::unique_ptr<TStrings> PermanentFileListOwner;
  2427. try
  2428. {
  2429. if (Side == osLocal)
  2430. {
  2431. PermanentFileList = FileList;
  2432. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2433. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2434. if (Operation == foMove)
  2435. {
  2436. ReloadLocalDirectory();
  2437. if (DView != NULL)
  2438. {
  2439. DView->RestoreSelection();
  2440. }
  2441. SelectionRestored = true;
  2442. }
  2443. }
  2444. else
  2445. {
  2446. // Clone the file list as it may refer to current directory files,
  2447. // which get destroyed, when the source directory is reloaded after foMove operation.
  2448. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2449. // But for a hotfix, we are not going to do such a big change.
  2450. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2451. PermanentFileList = PermanentFileListOwner.get();
  2452. try
  2453. {
  2454. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2455. }
  2456. __finally
  2457. {
  2458. if (Operation == foMove)
  2459. {
  2460. if (DView != NULL)
  2461. {
  2462. DView->RestoreSelection();
  2463. }
  2464. SelectionRestored = true;
  2465. }
  2466. ReloadLocalDirectory(Param.TargetDirectory);
  2467. }
  2468. }
  2469. }
  2470. catch (EAbort &)
  2471. {
  2472. if (FMoveToQueue)
  2473. {
  2474. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2475. DebugAssert(CopyParam.TransferSkipList == NULL);
  2476. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2477. if (TransferResumeList->Count > 0)
  2478. {
  2479. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2480. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2481. }
  2482. CopyParam.TransferSkipList = TransferResumeList.release();
  2483. // not really needed, just to keep it consistent with TransferResumeList
  2484. FTransferResumeList = NULL;
  2485. FMoveToQueue = false;
  2486. Configuration->Usage->Inc("MovesToBackground");
  2487. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2488. ClearTransferSourceSelection(Direction);
  2489. }
  2490. throw;
  2491. }
  2492. }
  2493. __finally
  2494. {
  2495. if (!SelectionRestored && (DView != NULL))
  2496. {
  2497. DView->DiscardSavedSelection();
  2498. }
  2499. FTransferResumeList = NULL;
  2500. }
  2501. }
  2502. return Result;
  2503. }
  2504. //---------------------------------------------------------------------------
  2505. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2506. TOperationSide Side, TStrings * FileList, void * Param)
  2507. {
  2508. DebugAssert(FileList->Count);
  2509. // We deliberately do not toggle alternative flag (Param), but use OR,
  2510. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2511. // shortcut of CurrentDeleteAlternativeAction
  2512. bool Alternative =
  2513. bool(Param) || UseAlternativeFunction();
  2514. bool Recycle;
  2515. if (IsSideLocalBrowser(Side))
  2516. {
  2517. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2518. }
  2519. else
  2520. {
  2521. Recycle =
  2522. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2523. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2524. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2525. }
  2526. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2527. if (!Result)
  2528. {
  2529. UnicodeString Query;
  2530. if (FileList->Count == 1)
  2531. {
  2532. if (IsSideLocalBrowser(Side))
  2533. {
  2534. Query = ExtractFileName(FileList->Strings[0]);
  2535. }
  2536. else
  2537. {
  2538. Query = UnixExtractFileName(FileList->Strings[0]);
  2539. }
  2540. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2541. }
  2542. else
  2543. {
  2544. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2545. }
  2546. TMessageParams Params(mpNeverAskAgainCheck);
  2547. Params.ImageName = L"Delete file";
  2548. unsigned int Answer =
  2549. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2550. if (Answer == qaNeverAskAgain)
  2551. {
  2552. Result = true;
  2553. if (Recycle)
  2554. {
  2555. WinConfiguration->ConfirmRecycling = false;
  2556. }
  2557. else
  2558. {
  2559. WinConfiguration->ConfirmDeleting = false;
  2560. }
  2561. }
  2562. else
  2563. {
  2564. Result = (Answer == qaOK);
  2565. }
  2566. }
  2567. if (Result)
  2568. {
  2569. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2570. }
  2571. return Result;
  2572. }
  2573. //---------------------------------------------------------------------------
  2574. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2575. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2576. {
  2577. TAutoBatch AutoBatch(this);
  2578. bool Result;
  2579. if ((Operation == foCopy) || (Operation == foMove))
  2580. {
  2581. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2582. }
  2583. else if (Operation == foRename)
  2584. {
  2585. DebugAssert(DirView(Side)->ItemFocused);
  2586. DirView(Side)->ItemFocused->EditCaption();
  2587. Result = true;
  2588. }
  2589. else if (Operation == foDelete)
  2590. {
  2591. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2592. }
  2593. else if (Operation == foSetProperties)
  2594. {
  2595. DirView(osRemote)->SaveSelectedNames();
  2596. Result = SetProperties(Side, FileList);
  2597. }
  2598. else if (Operation == foCustomCommand)
  2599. {
  2600. DebugAssert(Param);
  2601. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2602. DirView(osRemote)->SaveSelectedNames();
  2603. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2604. CustomCommand(FileList, *Command, NULL);
  2605. Result = true;
  2606. }
  2607. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2608. {
  2609. DebugAssert(!IsSideLocalBrowser(Side));
  2610. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2611. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2612. }
  2613. else if (Operation == foLock)
  2614. {
  2615. DebugAssert(!IsSideLocalBrowser(Side));
  2616. LockFiles(FileList, true);
  2617. Result = true;
  2618. }
  2619. else if (Operation == foUnlock)
  2620. {
  2621. DebugAssert(!IsSideLocalBrowser(Side));
  2622. LockFiles(FileList, false);
  2623. Result = true;
  2624. }
  2625. else
  2626. {
  2627. DebugFail();
  2628. }
  2629. return Result;
  2630. }
  2631. //---------------------------------------------------------------------------
  2632. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2633. {
  2634. if (Side == osCurrent)
  2635. {
  2636. Side = FCurrentSide;
  2637. }
  2638. return Side;
  2639. }
  2640. //---------------------------------------------------------------------------
  2641. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2642. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2643. {
  2644. Side = GetSide(Side);
  2645. bool Result;
  2646. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2647. try
  2648. {
  2649. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2650. }
  2651. __finally
  2652. {
  2653. delete FileList;
  2654. }
  2655. return Result;
  2656. }
  2657. //---------------------------------------------------------------------------
  2658. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2659. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2660. bool NoConfirmation, void * Param)
  2661. {
  2662. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2663. {
  2664. if ((Operation == foCopy) || (Operation == foMove))
  2665. {
  2666. DebugAssert(!IsLocalBrowserMode());
  2667. if (GetSide(Side) == osLocal)
  2668. {
  2669. Configuration->Usage->Inc(L"UploadsCommand");
  2670. }
  2671. else
  2672. {
  2673. Configuration->Usage->Inc(L"DownloadsCommand");
  2674. }
  2675. }
  2676. }
  2677. }
  2678. //---------------------------------------------------------------------------
  2679. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2680. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2681. {
  2682. TTransferOperationParam Param;
  2683. if ((WinConfiguration->Interface != ifCommander) ||
  2684. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2685. {
  2686. Flags &= ~cocShortCutHint;
  2687. }
  2688. TCustomDirView * DView = DirView(Side);
  2689. Param.Options =
  2690. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2691. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2692. if (FLAGSET(Flags, cocQueue))
  2693. {
  2694. Param.Queue = asOn;
  2695. }
  2696. else if (FLAGSET(Flags, cocNonQueue))
  2697. {
  2698. Param.Queue = asOff;
  2699. }
  2700. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2701. }
  2702. //---------------------------------------------------------------------------
  2703. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2704. {
  2705. try
  2706. {
  2707. if (ErrorList->Count)
  2708. {
  2709. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2710. if (MessageDialog(Message, qtError,
  2711. qaOK | qaCancel, HELP_NONE) == qaOK)
  2712. {
  2713. unsigned int Answer;
  2714. int Index = 0;
  2715. do
  2716. {
  2717. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2718. TQueryButtonAlias Aliases[2];
  2719. Aliases[0].Button = qaYes;
  2720. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2721. Aliases[1].Button = qaNo;
  2722. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2723. TMessageParams Params;
  2724. Params.Aliases = Aliases;
  2725. Params.AliasesCount = LENOF(Aliases);
  2726. Answer = MoreMessageDialog(
  2727. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2728. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2729. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2730. qaOK, HELP_NONE, &Params);
  2731. if (Answer == qaNo)
  2732. {
  2733. Index++;
  2734. }
  2735. if (Answer == qaYes)
  2736. {
  2737. Index--;
  2738. }
  2739. }
  2740. while (Answer != qaOK);
  2741. }
  2742. }
  2743. }
  2744. __finally
  2745. {
  2746. TStrings * List = ErrorList;
  2747. ErrorList = NULL;
  2748. for (int i = 0; i < List->Count; i++)
  2749. {
  2750. delete List->Objects[i];
  2751. }
  2752. delete List;
  2753. }
  2754. }
  2755. //---------------------------------------------------------------------------
  2756. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2757. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2758. {
  2759. // needed for checking filemasks, as there's no directory object
  2760. // associated with the file object
  2761. File->FullFileName = FullFileName;
  2762. TFileMasks::TParams MaskParams;
  2763. MaskParams.Size = File->Size;
  2764. MaskParams.Modification = File->Modification;
  2765. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2766. }
  2767. //---------------------------------------------------------------------------
  2768. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2769. {
  2770. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2771. Side = GetSide(Side);
  2772. TCustomDirView * DView = DirView(Side);
  2773. TListItem * FocusedItem = DView->ItemFocused;
  2774. UnicodeString Name;
  2775. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2776. {
  2777. Name = DView->ItemFileName(FocusedItem);
  2778. }
  2779. else
  2780. {
  2781. Name = LoadStr(NEW_FILE);
  2782. }
  2783. UnicodeString Names = Name;
  2784. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2785. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2786. HELP_EDIT_NEW, History.get(), true))
  2787. {
  2788. while (!Names.IsEmpty())
  2789. {
  2790. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2791. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2792. UnicodeString TargetFileName;
  2793. UnicodeString LocalFileName;
  2794. UnicodeString RootTempDir;
  2795. UnicodeString TempDir;
  2796. UnicodeString RemoteDirectory;
  2797. bool ExistingFile = false;
  2798. if (!IsSideLocalBrowser(Side))
  2799. {
  2800. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2801. TRemoteFile * File = NULL;
  2802. if (FTerminal->FileExists(Name, &File) &&
  2803. !File->IsDirectory)
  2804. {
  2805. try
  2806. {
  2807. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2808. ExistingFile = true;
  2809. }
  2810. __finally
  2811. {
  2812. delete File;
  2813. }
  2814. }
  2815. if (!ExistingFile)
  2816. {
  2817. RemoteDirectory = UnixExtractFilePath(Name);
  2818. TemporaryDirectoryForRemoteFiles(
  2819. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2820. TargetFileName = UnixExtractFileName(Name);
  2821. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2822. LocalFileName = TempDir +
  2823. // We probably do not want to trim the VMS version here
  2824. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2825. }
  2826. }
  2827. else
  2828. {
  2829. if (ExtractFilePath(Name).IsEmpty())
  2830. {
  2831. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2832. }
  2833. else
  2834. {
  2835. LocalFileName = ExpandFileName(Name);
  2836. }
  2837. TargetFileName = ExtractFileName(Name);
  2838. }
  2839. if (!ExistingFile)
  2840. {
  2841. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2842. if (NewFile)
  2843. {
  2844. int File = FileCreate(ApiPath(LocalFileName));
  2845. if (File < 0)
  2846. {
  2847. if (!RootTempDir.IsEmpty())
  2848. {
  2849. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2850. }
  2851. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2852. }
  2853. else
  2854. {
  2855. FileClose(File);
  2856. }
  2857. }
  2858. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2859. const TEditorData * ExternalEditor = NULL;
  2860. TFileMasks::TParams MaskParams; // size not known
  2861. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2862. false, MaskParams);
  2863. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2864. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2865. }
  2866. }
  2867. }
  2868. }
  2869. //---------------------------------------------------------------------------
  2870. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2871. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2872. {
  2873. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2874. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2875. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2876. return
  2877. ((ExecuteFileBy == efInternalEditor)) ||
  2878. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2879. }
  2880. //---------------------------------------------------------------------------
  2881. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2882. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2883. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2884. UnicodeString RemoteDirectory, bool NewFile)
  2885. {
  2886. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2887. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2888. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2889. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2890. Side = GetSide(Side);
  2891. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2892. if (!IsSideLocalBrowser(Side))
  2893. {
  2894. Data->Terminal = Terminal;
  2895. Data->Queue = Queue;
  2896. Data->SessionData = CloneCurrentSessionData();
  2897. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2898. Data->RemoteDirectory = RemoteDirectory;
  2899. Data->SessionName = Terminal->SessionData->SessionName;
  2900. Data->LocalRootDirectory = LocalRootDirectory;
  2901. Data->OriginalFileName = OriginalFileName;
  2902. Data->Command = L""; // will be changed later for external editor
  2903. }
  2904. if (ExecuteFileBy == efInternalEditor)
  2905. {
  2906. if (!IsSideLocalBrowser(Side))
  2907. {
  2908. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2909. L" - " + Terminal->SessionData->SessionName;
  2910. TForm * Editor;
  2911. try
  2912. {
  2913. TNotifyEvent OnFileChanged = NULL;
  2914. TNotifyEvent OnSaveAll = NULL;
  2915. TAnyModifiedEvent OnAnyModified = NULL;
  2916. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2917. if (Terminal->IsCapable[fcBackgroundTransfers])
  2918. {
  2919. OnFileChanged = FEditorManager->FileChanged;
  2920. OnSaveAll = SaveAllInternalEditors;
  2921. OnAnyModified = AnyInternalEditorModified;
  2922. }
  2923. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2924. FEditorManager->FileReload, FEditorManager->FileClosed,
  2925. OnSaveAll, OnAnyModified,
  2926. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2927. }
  2928. catch(...)
  2929. {
  2930. if (!LocalRootDirectory.IsEmpty())
  2931. {
  2932. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2933. }
  2934. throw;
  2935. }
  2936. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2937. }
  2938. else
  2939. {
  2940. DebugAssert(!FStandaloneEditing);
  2941. TForm * Editor =
  2942. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2943. SaveAllInternalEditors, AnyInternalEditorModified,
  2944. L"", false, SessionColor, -1, NewFile);
  2945. FLocalEditors->Add(Editor);
  2946. }
  2947. }
  2948. else
  2949. {
  2950. HANDLE Process;
  2951. if (ExecuteFileBy == efExternalEditor)
  2952. {
  2953. UnicodeString Program, Params, Dir;
  2954. Data->Command = ExternalEditor->ExternalEditor;
  2955. ReformatFileNameCommand(Data->Command);
  2956. SplitCommand(Data->Command, Program, Params, Dir);
  2957. Params = ExpandFileNameCommand(Params, FileName);
  2958. Program = ExpandEnvironmentVariables(Program);
  2959. if (!ExecuteShell(Program, Params, Process))
  2960. {
  2961. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2962. }
  2963. }
  2964. else
  2965. {
  2966. DebugAssert(!IsSideLocalBrowser(Side));
  2967. if (!ExecuteShell(FileName, L"", Process))
  2968. {
  2969. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2970. }
  2971. }
  2972. if (IsSideLocalBrowser(Side) ||
  2973. ((ExecuteFileBy == efShell) &&
  2974. !WinConfiguration->Editor.SDIShellEditor) ||
  2975. ((ExecuteFileBy == efExternalEditor) &&
  2976. !ExternalEditor->SDIExternalEditor))
  2977. {
  2978. // no need for handle
  2979. if (Process != NULL)
  2980. {
  2981. DebugCheck(CloseHandle(Process));
  2982. }
  2983. Process = INVALID_HANDLE_VALUE;
  2984. }
  2985. else
  2986. {
  2987. if (Process == NULL)
  2988. {
  2989. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2990. }
  2991. }
  2992. if (!IsSideLocalBrowser(Side))
  2993. {
  2994. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2995. }
  2996. }
  2997. }
  2998. //---------------------------------------------------------------------------
  2999. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3000. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3001. void * /*Arg*/)
  3002. {
  3003. if (Token != NULL)
  3004. {
  3005. EditorFormFileSave(static_cast<TForm *>(Token));
  3006. }
  3007. }
  3008. //---------------------------------------------------------------------------
  3009. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3010. {
  3011. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3012. {
  3013. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3014. }
  3015. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3016. }
  3017. //---------------------------------------------------------------------------
  3018. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3019. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3020. void * Arg)
  3021. {
  3022. if ((Token != NULL) &&
  3023. IsEditorFormModified(static_cast<TForm *>(Token)))
  3024. {
  3025. *static_cast<bool *>(Arg) = true;
  3026. }
  3027. }
  3028. //---------------------------------------------------------------------------
  3029. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3030. TObject * /*Sender*/, bool & Modified)
  3031. {
  3032. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3033. {
  3034. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3035. {
  3036. Modified = true;
  3037. }
  3038. }
  3039. if (!Modified)
  3040. {
  3041. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3042. }
  3043. }
  3044. //---------------------------------------------------------------------------
  3045. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3046. {
  3047. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3048. }
  3049. //---------------------------------------------------------------------------
  3050. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3051. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3052. UnicodeString & Result, UnicodeString & RootDirectory)
  3053. {
  3054. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3055. {
  3056. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3057. Result = RootDirectory;
  3058. }
  3059. else
  3060. {
  3061. RootDirectory = L"";
  3062. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3063. Result = IncludeTrailingBackslash(Result);
  3064. }
  3065. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3066. {
  3067. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3068. }
  3069. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3070. {
  3071. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3072. {
  3073. RemoteDirectory.Delete(1, 1);
  3074. }
  3075. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3076. }
  3077. if (!ForceDirectories(ApiPath(Result)))
  3078. {
  3079. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3080. }
  3081. }
  3082. //---------------------------------------------------------------------------
  3083. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3084. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3085. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3086. {
  3087. DebugAssert(!IsLocalBrowserMode());
  3088. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3089. if (ForceText)
  3090. {
  3091. CopyParam.TransferMode = tmAscii;
  3092. }
  3093. TemporaryFileCopyParam(CopyParam);
  3094. if (AllFiles)
  3095. {
  3096. CopyParam.IncludeFileMask = TFileMasks();
  3097. }
  3098. if (TempDir.IsEmpty())
  3099. {
  3100. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3101. }
  3102. DebugAssert(!FAutoOperation);
  3103. FAutoOperation = AutoOperation;
  3104. Terminal->ExceptionOnFail = true;
  3105. try
  3106. {
  3107. try
  3108. {
  3109. // turn off confirmations, as for MDI editors we may possibly download
  3110. // the same file over
  3111. Terminal->CopyToLocal(
  3112. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3113. if (GetTargetNames)
  3114. {
  3115. for (int i = 0; i < FileList->Count; i++)
  3116. {
  3117. FileList->Strings[i] =
  3118. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3119. }
  3120. }
  3121. }
  3122. catch(...)
  3123. {
  3124. if (!RootTempDir.IsEmpty())
  3125. {
  3126. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3127. }
  3128. throw;
  3129. }
  3130. }
  3131. __finally
  3132. {
  3133. FAutoOperation = false;
  3134. Terminal->ExceptionOnFail = false;
  3135. }
  3136. }
  3137. //---------------------------------------------------------------------------
  3138. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3139. {
  3140. // Do not waste time checking for default editor list the next time,
  3141. // or testing if default editor is not notepad.
  3142. bool TryNextTime = false;
  3143. UnicodeString UsageState;
  3144. if (!WinConfiguration->EditorList->IsDefaultList())
  3145. {
  3146. UsageState = "H";
  3147. }
  3148. else
  3149. {
  3150. UnicodeString Executable;
  3151. UnicodeString ExecutableDescription;
  3152. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3153. {
  3154. UnicodeString Message =
  3155. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3156. unsigned int Answer =
  3157. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3158. if (Answer != qaOK)
  3159. {
  3160. UsageState = "R";
  3161. }
  3162. else
  3163. {
  3164. UsageState = "A";
  3165. TEditorData EditorData;
  3166. EditorData.Editor = edExternal;
  3167. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3168. EditorData.DecideExternalEditorText();
  3169. TEditorList EditorList;
  3170. EditorList = *WinConfiguration->EditorList;
  3171. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3172. WinConfiguration->EditorList = &EditorList;
  3173. }
  3174. }
  3175. }
  3176. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3177. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3178. }
  3179. //---------------------------------------------------------------------------
  3180. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3181. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3182. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3183. {
  3184. if (ExecuteFileBy == efDefaultEditor)
  3185. {
  3186. if (!WinConfiguration->OfferedEditorAutoConfig)
  3187. {
  3188. EditorAutoConfig();
  3189. }
  3190. const TEditorPreferences * Editor =
  3191. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3192. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3193. {
  3194. ExecuteFileBy = efInternalEditor;
  3195. ExternalEditor = NULL;
  3196. }
  3197. else if (Editor->Data->Editor == edOpen)
  3198. {
  3199. ExecuteFileBy = efShell;
  3200. ExternalEditor = NULL;
  3201. }
  3202. else
  3203. {
  3204. ExecuteFileBy = efExternalEditor;
  3205. ExternalEditor = Editor->Data;
  3206. }
  3207. }
  3208. }
  3209. //---------------------------------------------------------------------------
  3210. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3211. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3212. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3213. {
  3214. UnicodeString OriginalFileName;
  3215. UnicodeString LocalRootDirectory;
  3216. UnicodeString RemoteDirectory;
  3217. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3218. IsSideLocalBrowser(Side), MaskParams);
  3219. UnicodeString Counter;
  3220. UnicodeString LocalFileName;
  3221. bool Handled = false;
  3222. if (!IsSideLocalBrowser(Side))
  3223. {
  3224. // We need to trim VMS version here, so that we use name without version
  3225. // when uploading back to create a new version of the file
  3226. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3227. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3228. TObject * Token = NULL;
  3229. UnicodeString LocalDirectory;
  3230. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3231. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3232. LocalDirectory))
  3233. {
  3234. if (Token != NULL)
  3235. {
  3236. TForm * Form = dynamic_cast<TForm *>(Token);
  3237. if (Form->WindowState == wsMinimized)
  3238. {
  3239. ShowWindow(Form->Handle, SW_RESTORE);
  3240. }
  3241. else
  3242. {
  3243. Form->SetFocus();
  3244. }
  3245. Handled = true;
  3246. }
  3247. else
  3248. {
  3249. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3250. }
  3251. }
  3252. if (!Handled)
  3253. {
  3254. TStringList * FileList1 = new TStringList();
  3255. try
  3256. {
  3257. FileList1->AddObject(FullFileName, Object);
  3258. TemporarilyDownloadFiles(FileList1,
  3259. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3260. LocalRootDirectory, LocalDirectory, true, true, true);
  3261. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3262. }
  3263. __finally
  3264. {
  3265. delete FileList1;
  3266. }
  3267. switch (ExecuteFileBy)
  3268. {
  3269. case efShell:
  3270. Counter = "RemoteFilesExecuted";
  3271. break;
  3272. case efInternalEditor:
  3273. Counter = "RemoteFilesOpenedInInternalEditor";
  3274. break;
  3275. case efExternalEditor:
  3276. Counter = "RemoteFilesOpenedInExternalEditor";
  3277. break;
  3278. default:
  3279. DebugFail();
  3280. }
  3281. }
  3282. }
  3283. else
  3284. {
  3285. LocalFileName = FullFileName;
  3286. OriginalFileName = ExtractFileName(FullFileName);
  3287. switch (ExecuteFileBy)
  3288. {
  3289. case efShell:
  3290. Counter = "LocalFilesExecuted";
  3291. break;
  3292. case efInternalEditor:
  3293. Counter = "LocalFilesOpenedInInternalEditor";
  3294. break;
  3295. case efExternalEditor:
  3296. Counter = "LocalFilesOpenedInExternalEditor";
  3297. break;
  3298. default:
  3299. DebugFail();
  3300. }
  3301. }
  3302. if (!Handled)
  3303. {
  3304. Configuration->Usage->Inc(Counter);
  3305. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3306. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3307. }
  3308. }
  3309. //---------------------------------------------------------------------------
  3310. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3311. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3312. bool AllSelected, bool OnFocused)
  3313. {
  3314. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3315. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3316. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3317. Side = GetSide(Side);
  3318. TCustomDirView * DView = DirView(Side);
  3319. TStrings * FileList =
  3320. AllSelected ?
  3321. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3322. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3323. try
  3324. {
  3325. DebugAssert(AllSelected || (FileList->Count == 1));
  3326. for (int i = 0; i < FileList->Count; i++)
  3327. {
  3328. UnicodeString ListFileName = FileList->Strings[i];
  3329. UnicodeString FileNameOnly =
  3330. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3331. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3332. if (!DView->ItemIsDirectory(Item))
  3333. {
  3334. UnicodeString FullFileName;
  3335. if (!IsSideLocalBrowser(Side))
  3336. {
  3337. FullFileName = DirView(Side)->Path + ListFileName;
  3338. }
  3339. else
  3340. {
  3341. FullFileName = ListFileName;
  3342. }
  3343. TFileMasks::TParams MaskParams;
  3344. MaskParams.Size = DView->ItemFileSize(Item);
  3345. TDateTimePrecision Precision;
  3346. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3347. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3348. FileList->Objects[i], MaskParams);
  3349. }
  3350. }
  3351. }
  3352. __finally
  3353. {
  3354. delete FileList;
  3355. }
  3356. }
  3357. //---------------------------------------------------------------------------
  3358. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3359. {
  3360. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3361. CopyParam.FileNameCase = ncNoChange;
  3362. CopyParam.PreserveRights = false;
  3363. CopyParam.PreserveReadOnly = false;
  3364. CopyParam.ReplaceInvalidChars = true;
  3365. CopyParam.IncludeFileMask = TFileMasks();
  3366. CopyParam.NewerOnly = false;
  3367. CopyParam.FileMask = L"";
  3368. }
  3369. //---------------------------------------------------------------------------
  3370. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3371. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3372. {
  3373. TTerminalManager * Manager = TTerminalManager::Instance();
  3374. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3375. {
  3376. if (!NonVisualDataModule->Busy)
  3377. {
  3378. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3379. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3380. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3381. if (Data->Terminal == NULL)
  3382. {
  3383. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3384. if (SameSiteTerminal != NULL)
  3385. {
  3386. UnicodeString Message =
  3387. FMTLOAD(EDIT_SESSION_REATTACH,
  3388. (FileNameOnly, Data->SessionName, FileNameOnly));
  3389. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3390. {
  3391. Data->Terminal = Terminal;
  3392. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3393. Data->SessionName = Terminal->SessionData->SessionName;
  3394. // We might also overwrite session data
  3395. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3396. }
  3397. else
  3398. {
  3399. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3400. Abort();
  3401. }
  3402. }
  3403. }
  3404. // foreground session should reconnect itself
  3405. else if (Terminal != Data->Terminal)
  3406. {
  3407. UnicodeString Message =
  3408. MainInstructions(
  3409. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3410. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3411. {
  3412. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3413. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3414. }
  3415. else
  3416. {
  3417. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3418. Abort();
  3419. }
  3420. }
  3421. }
  3422. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3423. {
  3424. Configuration->Usage->Inc(L"EditInactiveSession");
  3425. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3426. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3427. (ExtractFileName(FileName), Data->SessionName)));
  3428. }
  3429. }
  3430. TStrings * FileList = new TStringList();
  3431. try
  3432. {
  3433. FileList->Add(FileName);
  3434. // consider using the same settings (preset) as when the file was downloaded
  3435. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3436. TemporaryFileCopyParam(CopyParam);
  3437. if (Data->ForceText)
  3438. {
  3439. CopyParam.TransferMode = tmAscii;
  3440. }
  3441. // so i do not need to worry if masking algorithm works in all cases
  3442. // ("" means "copy file name", no masking is actually done)
  3443. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3444. {
  3445. CopyParam.FileMask = L"";
  3446. }
  3447. else
  3448. {
  3449. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3450. }
  3451. DebugAssert(Data->Queue != NULL);
  3452. int Params = cpNoConfirmation | cpTemporary;
  3453. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3454. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3455. QueueItem->CompleteEvent = UploadCompleteEvent;
  3456. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3457. }
  3458. __finally
  3459. {
  3460. delete FileList;
  3461. }
  3462. }
  3463. //---------------------------------------------------------------------------
  3464. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3465. const UnicodeString FileName, const TEditedFileData * Data)
  3466. {
  3467. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3468. DebugAssert(!NonVisualDataModule->Busy);
  3469. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3470. {
  3471. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3472. (ExtractFileName(FileName), Data->SessionName)));
  3473. }
  3474. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3475. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3476. NonVisualDataModule->StartBusy();
  3477. try
  3478. {
  3479. std::unique_ptr<TRemoteFile> File;
  3480. UnicodeString RemoteFileName =
  3481. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3482. FTerminal->ExceptionOnFail = true;
  3483. try
  3484. {
  3485. TRemoteFile * AFile = NULL;
  3486. FTerminal->ReadFile(RemoteFileName, AFile);
  3487. File.reset(AFile);
  3488. if (!File->HaveFullFileName)
  3489. {
  3490. File->FullFileName = RemoteFileName;
  3491. }
  3492. }
  3493. __finally
  3494. {
  3495. FTerminal->ExceptionOnFail = false;
  3496. }
  3497. std::unique_ptr<TStrings> FileList(new TStringList());
  3498. FileList->AddObject(RemoteFileName, File.get());
  3499. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3500. UnicodeString TempDir = ExtractFilePath(FileName);
  3501. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3502. TempDir, true, true, true);
  3503. // sanity check, the target file name should be still the same
  3504. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3505. }
  3506. __finally
  3507. {
  3508. NonVisualDataModule->EndBusy();
  3509. // it actually may not exist anymore...
  3510. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3511. }
  3512. }
  3513. //---------------------------------------------------------------------------
  3514. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3515. const TEditedFileData * Data, bool & KeepOpen)
  3516. {
  3517. // Command is set for external editors only (not for "shell" open).
  3518. if (!Data->Command.IsEmpty())
  3519. {
  3520. bool AnyFound = false;
  3521. bool AnyMDI = false;
  3522. bool AnyNonMDI = false;
  3523. bool AnyDetect = false;
  3524. TEditorList * EditorList = new TEditorList();
  3525. try
  3526. {
  3527. *EditorList = *WinConfiguration->EditorList;
  3528. for (int i = 0; i < EditorList->Count; i++)
  3529. {
  3530. const TEditorPreferences * Editor = EditorList->Editors[i];
  3531. if ((Editor->Data->Editor == edExternal) &&
  3532. (Editor->Data->ExternalEditor == Data->Command))
  3533. {
  3534. AnyFound = true;
  3535. if (Editor->Data->SDIExternalEditor)
  3536. {
  3537. AnyNonMDI = true;
  3538. if (Editor->Data->DetectMDIExternalEditor)
  3539. {
  3540. AnyDetect = true;
  3541. }
  3542. }
  3543. else
  3544. {
  3545. AnyMDI = true;
  3546. }
  3547. }
  3548. }
  3549. bool EnableMDI = false;
  3550. bool DisableDetect = false;
  3551. if (AnyMDI)
  3552. {
  3553. KeepOpen = true;
  3554. if (AnyNonMDI)
  3555. {
  3556. // there is at least one instance of the editor with MDI support enabled,
  3557. // and one with disabled, enable it for all instances
  3558. EnableMDI = true;
  3559. }
  3560. }
  3561. else if (AnyFound && !AnyDetect)
  3562. {
  3563. // at least once instance found but all have MDI autodetection disabled
  3564. // => close the file (default action)
  3565. }
  3566. else
  3567. {
  3568. // no instance of the editor has MDI support enabled
  3569. TMessageParams Params;
  3570. if (AnyFound)
  3571. {
  3572. // there is at least one instance of the editor
  3573. Params.Params |= mpNeverAskAgainCheck;
  3574. }
  3575. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3576. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3577. switch (Answer)
  3578. {
  3579. case qaNeverAskAgain:
  3580. DisableDetect = true;
  3581. break;
  3582. case qaNo:
  3583. EnableMDI = true;
  3584. KeepOpen = true;
  3585. break;
  3586. }
  3587. }
  3588. if (AnyFound && (EnableMDI || DisableDetect))
  3589. {
  3590. bool Changed = false;
  3591. for (int i = 0; i < EditorList->Count; i++)
  3592. {
  3593. const TEditorPreferences * Editor = EditorList->Editors[i];
  3594. if ((Editor->Data->Editor == edExternal) &&
  3595. (Editor->Data->ExternalEditor == Data->Command) &&
  3596. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3597. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3598. {
  3599. Changed = true;
  3600. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3601. if (EnableMDI)
  3602. {
  3603. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3604. }
  3605. if (DisableDetect)
  3606. {
  3607. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3608. }
  3609. EditorList->Change(i, UpdatedEditor);
  3610. }
  3611. }
  3612. if (Changed)
  3613. {
  3614. WinConfiguration->EditorList = EditorList;
  3615. }
  3616. }
  3617. }
  3618. __finally
  3619. {
  3620. delete EditorList;
  3621. }
  3622. }
  3623. else
  3624. {
  3625. // "open" case
  3626. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3627. qaOK, HELP_APP_EARLY_CLOSED);
  3628. }
  3629. }
  3630. //---------------------------------------------------------------------------
  3631. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3632. {
  3633. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3634. }
  3635. //---------------------------------------------------------------------------
  3636. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3637. {
  3638. SideEnter(osRemote);
  3639. }
  3640. //---------------------------------------------------------------------------
  3641. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3642. {
  3643. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3644. SideEnter(osRemote);
  3645. }
  3646. //---------------------------------------------------------------------------
  3647. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3648. {
  3649. FCurrentSide = Side;
  3650. if (Visible)
  3651. {
  3652. UpdateControls();
  3653. }
  3654. }
  3655. //---------------------------------------------------------------------------
  3656. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3657. TStrings * FileList, bool Alternative)
  3658. {
  3659. TCustomDirView * DView = DirView(Side);
  3660. DView->SaveSelection();
  3661. DView->SaveSelectedNames();
  3662. DebugAssert(!FAlternativeDelete);
  3663. FAlternativeDelete = Alternative;
  3664. try
  3665. {
  3666. if (!IsSideLocalBrowser(Side))
  3667. {
  3668. DebugAssert(Terminal != NULL);
  3669. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3670. }
  3671. else
  3672. {
  3673. try
  3674. {
  3675. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3676. }
  3677. __finally
  3678. {
  3679. ReloadLocalDirectory();
  3680. }
  3681. }
  3682. FAlternativeDelete = false;
  3683. }
  3684. catch(...)
  3685. {
  3686. FAlternativeDelete = false;
  3687. DView->DiscardSavedSelection();
  3688. throw;
  3689. }
  3690. DView->RestoreSelection();
  3691. }
  3692. //---------------------------------------------------------------------------
  3693. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3694. {
  3695. DebugAssert(Terminal);
  3696. DebugAssert(!IsLocalBrowserMode());
  3697. RemoteDirView->SaveSelection();
  3698. RemoteDirView->SaveSelectedNames();
  3699. try
  3700. {
  3701. if (Lock)
  3702. {
  3703. Terminal->LockFiles(FileList);
  3704. }
  3705. else
  3706. {
  3707. Terminal->UnlockFiles(FileList);
  3708. }
  3709. }
  3710. catch(...)
  3711. {
  3712. RemoteDirView->DiscardSavedSelection();
  3713. throw;
  3714. }
  3715. RemoteDirView->RestoreSelection();
  3716. }
  3717. //---------------------------------------------------------------------------
  3718. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3719. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3720. bool NoConfirmation, bool Move)
  3721. {
  3722. DebugAssert(Terminal != NULL);
  3723. DebugAssert(!IsLocalBrowserMode());
  3724. // update Terminal->StateData->RemoteDirectory
  3725. UpdateTerminal(Terminal);
  3726. if (Session == NULL)
  3727. {
  3728. Session = TTerminalManager::Instance()->ActiveTerminal;
  3729. }
  3730. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3731. {
  3732. if (RemoteDriveView->DropTarget != NULL)
  3733. {
  3734. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3735. }
  3736. else if (RemoteDirView->DropTarget != NULL)
  3737. {
  3738. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3739. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3740. }
  3741. else
  3742. {
  3743. Target = RemoteDirView->Path;
  3744. }
  3745. }
  3746. else
  3747. {
  3748. Target = Session->StateData->RemoteDirectory;
  3749. }
  3750. Target = UnixIncludeTrailingBackslash(Target);
  3751. if (FileList->Count == 1)
  3752. {
  3753. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3754. }
  3755. else
  3756. {
  3757. FileMask = AnyMask;
  3758. }
  3759. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3760. bool Result = true;
  3761. if (!NoConfirmation)
  3762. {
  3763. bool Multi = (FileList->Count > 1);
  3764. if (Move)
  3765. {
  3766. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3767. }
  3768. else
  3769. {
  3770. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3771. TStrings * Directories = new TStringList;
  3772. try
  3773. {
  3774. for (int Index = 0; Index < Sessions->Count; Index++)
  3775. {
  3776. TManagedTerminal * Terminal =
  3777. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3778. Directories->Add(Terminal->StateData->RemoteDirectory);
  3779. }
  3780. TDirectRemoteCopy AllowDirectCopy;
  3781. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3782. {
  3783. DebugAssert(DirectCopy);
  3784. AllowDirectCopy = drcAllow;
  3785. }
  3786. else if (FTerminal->IsCapable[fcSecondaryShell])
  3787. {
  3788. DebugAssert(DirectCopy);
  3789. AllowDirectCopy = drcConfirmCommandSession;
  3790. }
  3791. else
  3792. {
  3793. DebugAssert(!DirectCopy);
  3794. AllowDirectCopy = drcDisallow;
  3795. }
  3796. void * ASession = Session;
  3797. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3798. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3799. Session = static_cast<TManagedTerminal *>(ASession);
  3800. }
  3801. __finally
  3802. {
  3803. delete Directories;
  3804. }
  3805. }
  3806. }
  3807. return Result;
  3808. }
  3809. //---------------------------------------------------------------------------
  3810. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3811. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3812. {
  3813. DebugAssert(!IsLocalBrowserMode());
  3814. bool DirectCopy;
  3815. UnicodeString Target, FileMask;
  3816. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3817. if (Result)
  3818. {
  3819. if (!Move && !DirectCopy)
  3820. {
  3821. Configuration->Usage->Inc("RemoteCopyTemp");
  3822. UnicodeString RootTempDir;
  3823. UnicodeString TempDir;
  3824. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3825. TStrings * TemporaryFilesList = new TStringList();
  3826. try
  3827. {
  3828. TMakeLocalFileListParams MakeFileListParam;
  3829. MakeFileListParam.FileList = TemporaryFilesList;
  3830. MakeFileListParam.FileTimes = NULL;
  3831. MakeFileListParam.IncludeDirs = true;
  3832. MakeFileListParam.Recursive = false;
  3833. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3834. TTerminalManager::Instance()->ActiveTerminal = Session;
  3835. if (TemporaryFilesList->Count > 0)
  3836. {
  3837. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3838. CopyParam.FileMask = FileMask;
  3839. DebugAssert(!FAutoOperation);
  3840. FAutoOperation = true;
  3841. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3842. }
  3843. }
  3844. __finally
  3845. {
  3846. delete TemporaryFilesList;
  3847. FAutoOperation = false;
  3848. if (!RootTempDir.IsEmpty())
  3849. {
  3850. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3851. }
  3852. }
  3853. }
  3854. else
  3855. {
  3856. RemoteDirView->SaveSelection();
  3857. RemoteDirView->SaveSelectedNames();
  3858. try
  3859. {
  3860. if (Move)
  3861. {
  3862. Configuration->Usage->Inc("RemoteMove");
  3863. Terminal->MoveFiles(FileList, Target, FileMask);
  3864. }
  3865. else
  3866. {
  3867. Configuration->Usage->Inc("RemoteCopyDirect");
  3868. DebugAssert(DirectCopy);
  3869. DebugAssert(Session == FTerminal);
  3870. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3871. FTerminal->CommandSessionOpened ||
  3872. CommandSessionFallback())
  3873. {
  3874. Terminal->CopyFiles(FileList, Target, FileMask);
  3875. }
  3876. }
  3877. }
  3878. catch(...)
  3879. {
  3880. RemoteDirView->DiscardSavedSelection();
  3881. throw;
  3882. }
  3883. RemoteDirView->RestoreSelection();
  3884. }
  3885. }
  3886. return Result;
  3887. }
  3888. //---------------------------------------------------------------------------
  3889. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3890. const UnicodeString & Path, TRemoteProperties & Properties)
  3891. {
  3892. DebugAssert(!IsLocalBrowserMode());
  3893. Properties.Valid = Properties.Valid << vpEncrypt;
  3894. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3895. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3896. }
  3897. //---------------------------------------------------------------------------
  3898. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3899. {
  3900. Side = GetSide(Side);
  3901. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3902. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3903. UnicodeString Name = LoadStr(NEW_FOLDER);
  3904. int AllowedChanges =
  3905. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3906. bool SaveSettings = false;
  3907. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3908. {
  3909. TWindowLock Lock(this);
  3910. if (!IsSideLocalBrowser(Side))
  3911. {
  3912. if (SaveSettings)
  3913. {
  3914. GUIConfiguration->NewDirectoryProperties = Properties;
  3915. }
  3916. CreateRemoteDirectory(Name, Properties);
  3917. }
  3918. else
  3919. {
  3920. DirView(Side)->CreateDirectory(Name);
  3921. }
  3922. }
  3923. }
  3924. //---------------------------------------------------------------------------
  3925. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3926. {
  3927. TWindowLock Lock(this);
  3928. DirView(Side)->ExecuteHomeDirectory();
  3929. }
  3930. //---------------------------------------------------------------------------
  3931. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3932. {
  3933. DoOpenDirectoryDialog(odBrowse, Side);
  3934. }
  3935. //---------------------------------------------------------------------------
  3936. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3937. {
  3938. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3939. bool Result = !Path.IsEmpty();
  3940. if (Result)
  3941. {
  3942. // While we might get here when the session is closed (from location profiles),
  3943. // it's not a problem as the Path setter is noop then
  3944. DirView(Side)->Path = Path;
  3945. }
  3946. return Result;
  3947. }
  3948. //---------------------------------------------------------------------------
  3949. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3950. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3951. bool & Close)
  3952. {
  3953. // terminal can be already closed (e.g. dropped connection)
  3954. if (Terminal != NULL)
  3955. {
  3956. try
  3957. {
  3958. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3959. }
  3960. catch(...)
  3961. {
  3962. if (!Terminal->Active)
  3963. {
  3964. Close = true;
  3965. }
  3966. throw;
  3967. }
  3968. }
  3969. }
  3970. //---------------------------------------------------------------------------
  3971. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3972. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3973. bool & Close)
  3974. {
  3975. // terminal can be already closed (e.g. dropped connection)
  3976. if (Terminal != NULL)
  3977. {
  3978. Configuration->Usage->Inc(L"ChecksumCalculated");
  3979. try
  3980. {
  3981. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3982. }
  3983. catch(...)
  3984. {
  3985. if (!Terminal->Active)
  3986. {
  3987. Close = true;
  3988. }
  3989. throw;
  3990. }
  3991. }
  3992. }
  3993. //---------------------------------------------------------------------------
  3994. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3995. {
  3996. bool Result;
  3997. if (!IsSideLocalBrowser(Side))
  3998. {
  3999. TRemoteTokenList * GroupList = NULL;
  4000. TRemoteTokenList * UserList = NULL;
  4001. try
  4002. {
  4003. TRemoteProperties CurrentProperties;
  4004. if (Terminal->LoadFilesProperties(FileList))
  4005. {
  4006. RemoteDirView->Invalidate();
  4007. }
  4008. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4009. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4010. if (CapableGroupChanging || CapableOwnerChanging)
  4011. {
  4012. if (CapableGroupChanging)
  4013. {
  4014. GroupList = Terminal->Groups->Duplicate();
  4015. }
  4016. if (CapableOwnerChanging)
  4017. {
  4018. UserList = Terminal->Users->Duplicate();
  4019. }
  4020. TRemoteDirectory * Files = Terminal->Files;
  4021. int Count = Files->Count;
  4022. if (Count > 100)
  4023. {
  4024. Count = 100;
  4025. }
  4026. for (int Index = 0; Index < Count; Index++)
  4027. {
  4028. TRemoteFile * File = Files->Files[Index];
  4029. if (CapableGroupChanging)
  4030. {
  4031. GroupList->AddUnique(File->Group);
  4032. }
  4033. if (CapableOwnerChanging)
  4034. {
  4035. UserList->AddUnique(File->Owner);
  4036. }
  4037. }
  4038. // if we haven't collected tokens for all files in current directory,
  4039. // make sure we collect them at least for all selected files.
  4040. // (note that so far the files in FileList has to be from current direcotry)
  4041. if (Count < Files->Count)
  4042. {
  4043. for (int Index = 0; Index < FileList->Count; Index++)
  4044. {
  4045. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4046. if (CapableGroupChanging)
  4047. {
  4048. GroupList->AddUnique(File->Group);
  4049. }
  4050. if (CapableOwnerChanging)
  4051. {
  4052. UserList->AddUnique(File->Owner);
  4053. }
  4054. }
  4055. }
  4056. }
  4057. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4058. int Flags = 0;
  4059. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4060. if (CapableOwnerChanging) Flags |= cpOwner;
  4061. if (CapableGroupChanging) Flags |= cpGroup;
  4062. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4063. if (Terminal->IsCapable[fcCalculatingChecksum])
  4064. {
  4065. CalculateChecksumEvent = CalculateChecksum;
  4066. }
  4067. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4068. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4069. TRemoteProperties NewProperties = CurrentProperties;
  4070. Result =
  4071. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4072. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4073. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4074. CalculateSize, CalculateChecksumEvent);
  4075. if (Result)
  4076. {
  4077. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4078. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4079. }
  4080. }
  4081. __finally
  4082. {
  4083. delete GroupList;
  4084. delete UserList;
  4085. }
  4086. }
  4087. else
  4088. {
  4089. DirView(Side)->DisplayPropertiesMenu();
  4090. Result = true;
  4091. }
  4092. return Result;
  4093. }
  4094. //---------------------------------------------------------------------------
  4095. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4096. {
  4097. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4098. {
  4099. FIgnoreNextDialogChar = Key;
  4100. }
  4101. Key = 0;
  4102. }
  4103. //---------------------------------------------------------------------------
  4104. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4105. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4106. {
  4107. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4108. if (Command != NULL)
  4109. {
  4110. KeyProcessed(Key, Shift);
  4111. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4112. {
  4113. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4114. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4115. false, false, const_cast<TCustomCommandType *>(Command));
  4116. }
  4117. }
  4118. }
  4119. //---------------------------------------------------------------------------
  4120. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4121. {
  4122. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4123. {
  4124. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4125. }
  4126. if (!DirView(osCurrent)->IsEditing())
  4127. {
  4128. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4129. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4130. {
  4131. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4132. if (((Action->ShortCut == KeyShortCut) ||
  4133. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4134. AllowedAction(Action, aaShortCut))
  4135. {
  4136. // Has to be called before the action as the dialog char is already in queue.
  4137. // So when the action consumes message queue, we already need to have the
  4138. // FIgnoreNextDialogChar set
  4139. KeyProcessed(Key, Shift);
  4140. // Reset reference to previous component (when menu/toolbar was clicked).
  4141. // Needed to detect that action was invoked by keyboard shortcut
  4142. // in TNonVisualDataModule::ExplorerActionsExecute
  4143. Action->ActionComponent = NULL;
  4144. Action->Execute();
  4145. return;
  4146. }
  4147. }
  4148. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4149. {
  4150. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4151. {
  4152. KeyProcessed(Key, Shift);
  4153. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4154. return;
  4155. }
  4156. }
  4157. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4158. {
  4159. KeyProcessed(Key, Shift);
  4160. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4161. }
  4162. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4163. if (IsCustomShortCut(CustomShortCut))
  4164. {
  4165. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4166. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4167. if (WinConfiguration->SharedBookmarks != NULL)
  4168. {
  4169. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4170. if ((Bookmark != NULL) &&
  4171. OpenBookmark(FCurrentSide, Bookmark))
  4172. {
  4173. KeyProcessed(Key, Shift);
  4174. }
  4175. }
  4176. }
  4177. }
  4178. TForm::KeyDown(Key, Shift);
  4179. }
  4180. //---------------------------------------------------------------------------
  4181. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4182. {
  4183. DebugAssert(Terminal != NULL);
  4184. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4185. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4186. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4187. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4188. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4189. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4190. // expanded from ?: to avoid memory leaks
  4191. if (SecurityEnabled)
  4192. {
  4193. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4194. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4195. }
  4196. else
  4197. {
  4198. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4199. }
  4200. if (FileSystemInfo.ProtocolName.IsEmpty())
  4201. {
  4202. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4203. }
  4204. else
  4205. {
  4206. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4207. }
  4208. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4209. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4210. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4211. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4212. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4213. UnicodeString CompressionStr = CSCompressionStr;
  4214. if (CSCompressionStr != SCCompressionStr)
  4215. {
  4216. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4217. }
  4218. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4219. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4220. UpdateStatusBar();
  4221. }
  4222. //---------------------------------------------------------------------------
  4223. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4224. {
  4225. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4226. DebugAssert(SessionStatusBar != NULL);
  4227. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4228. {
  4229. // note: (Terminal->Status < sshReady) currently never happens here,
  4230. // so STATUS_CONNECTING is never used
  4231. SessionStatusBar->SimplePanel = true;
  4232. SessionStatusBar->SimpleText = LoadStr(
  4233. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4234. }
  4235. else
  4236. {
  4237. DebugAssert(Terminal);
  4238. SessionStatusBar->SimplePanel = false;
  4239. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4240. if (!FNote.IsEmpty())
  4241. {
  4242. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4243. }
  4244. else
  4245. {
  4246. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4247. }
  4248. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4249. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4250. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4251. }
  4252. }
  4253. //---------------------------------------------------------------------------
  4254. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4255. {
  4256. Panel->Caption = L"";
  4257. }
  4258. //---------------------------------------------------------------------------
  4259. void __fastcall TCustomScpExplorerForm::Idle()
  4260. {
  4261. if (FShowing || FStandaloneEditing)
  4262. {
  4263. FEditorManager->Check();
  4264. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4265. // that needs to know if queue view is visible (and it may be closed after queue update)
  4266. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4267. }
  4268. if (FShowing)
  4269. {
  4270. if (!NonVisualDataModule->Busy &&
  4271. // Menu is opened or drag&drop is going on
  4272. (Mouse->Capture == NULL))
  4273. {
  4274. if (FRefreshRemoteDirectory)
  4275. {
  4276. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4277. {
  4278. Terminal->RefreshDirectory();
  4279. }
  4280. FRefreshRemoteDirectory = false;
  4281. }
  4282. if (FRefreshLocalDirectory)
  4283. {
  4284. ReloadLocalDirectory();
  4285. FRefreshLocalDirectory = false;
  4286. }
  4287. if (WinConfiguration->RefreshRemotePanel)
  4288. {
  4289. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4290. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4291. {
  4292. RemoteDirView->ReloadDirectory();
  4293. }
  4294. }
  4295. }
  4296. }
  4297. if (FShowing || FStandaloneEditing)
  4298. {
  4299. if (FQueueStatusInvalidated)
  4300. {
  4301. UpdateQueueStatus(false);
  4302. }
  4303. RefreshQueueItems();
  4304. }
  4305. if (FShowing)
  4306. {
  4307. UpdateStatusBar();
  4308. }
  4309. FIgnoreNextDialogChar = 0;
  4310. }
  4311. //---------------------------------------------------------------------------
  4312. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4313. {
  4314. try
  4315. {
  4316. FUserActionTimer->Enabled = false;
  4317. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4318. {
  4319. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4320. {
  4321. FPendingQueueActionItem->ProcessUserAction();
  4322. }
  4323. }
  4324. }
  4325. __finally
  4326. {
  4327. FPendingQueueActionItem = NULL;
  4328. }
  4329. }
  4330. //---------------------------------------------------------------------------
  4331. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4332. {
  4333. if (WinConfiguration->MinimizeToTray)
  4334. {
  4335. UpdateTrayIcon();
  4336. FTrayIcon->Visible = true;
  4337. if (Visible)
  4338. {
  4339. ShowWindow(Handle, SW_HIDE);
  4340. }
  4341. }
  4342. }
  4343. //---------------------------------------------------------------------------
  4344. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4345. {
  4346. // WORKAROUND
  4347. // When restoring maximized window from minimization,
  4348. // rarely some controls do not align properly.
  4349. // Two instances seen (both for Commander):
  4350. // - When restoring, window is temporarily narrower (not maximized),
  4351. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4352. // Once maximized already, top dock shinks back, but the session PageControl,
  4353. // do not align up, leaving space between TopDock and PageControl.
  4354. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4355. for (int Index = 0; Index < ControlCount; Index++)
  4356. {
  4357. RealignControl(Controls[Index]);
  4358. }
  4359. if (FTrayIcon->Visible)
  4360. {
  4361. FTrayIcon->Visible = false;
  4362. if (Visible)
  4363. {
  4364. ShowWindow(Handle, SW_SHOW);
  4365. }
  4366. }
  4367. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4368. {
  4369. FNeedSession = false;
  4370. NonVisualDataModule->StartBusy();
  4371. try
  4372. {
  4373. NeedSession(true);
  4374. }
  4375. __finally
  4376. {
  4377. NonVisualDataModule->EndBusy();
  4378. }
  4379. }
  4380. }
  4381. //---------------------------------------------------------------------------
  4382. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4383. {
  4384. FTrayIcon->Hint = GetMainForm()->Caption;
  4385. }
  4386. //---------------------------------------------------------------------------
  4387. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4388. {
  4389. UpdateTrayIcon();
  4390. }
  4391. //---------------------------------------------------------------------------
  4392. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4393. {
  4394. // workaround
  4395. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4396. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4397. // But WM_ACTIVATEAPP message can be received even when
  4398. // the main window is minimized to the tray and internal editor window is focused
  4399. // (after another application was previously active)
  4400. if (::IsIconic(Handle))
  4401. {
  4402. if (!IsAppIconic())
  4403. {
  4404. SetAppIconic(true);
  4405. }
  4406. }
  4407. ::ApplicationRestore();
  4408. Application->BringToFront();
  4409. }
  4410. //---------------------------------------------------------------------------
  4411. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4412. {
  4413. RestoreApp();
  4414. }
  4415. //---------------------------------------------------------------------------
  4416. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4417. {
  4418. if (OpenInNewWindow())
  4419. {
  4420. // todo: Pass FromSite
  4421. ExecuteNewInstance(SessionUrl);
  4422. }
  4423. else
  4424. {
  4425. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4426. }
  4427. }
  4428. //---------------------------------------------------------------------------
  4429. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4430. {
  4431. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4432. StoredSessions->NewSession(SessionName, SessionData);
  4433. // modified only, explicit
  4434. StoredSessions->Save(false, true);
  4435. // encode session name because of slashes in hierarchical sessions
  4436. return EncodeUrlString(SessionName);
  4437. }
  4438. //---------------------------------------------------------------------------
  4439. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4440. {
  4441. // current working directories become defaults here, what is not right
  4442. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4443. return SaveHiddenDuplicateSession(SessionData.get());
  4444. }
  4445. //---------------------------------------------------------------------------
  4446. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4447. {
  4448. if (OpenInNewWindow())
  4449. {
  4450. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4451. }
  4452. else
  4453. {
  4454. // current working directories become defaults here, what is not right
  4455. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4456. TTerminalManager * Manager = TTerminalManager::Instance();
  4457. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4458. Manager->ActiveTerminal = Terminal;
  4459. }
  4460. }
  4461. //---------------------------------------------------------------------------
  4462. void __fastcall TCustomScpExplorerForm::RenameSession()
  4463. {
  4464. UnicodeString Name = Terminal->SessionData->SessionName;
  4465. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4466. {
  4467. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4468. TSessionData::ValidateName(Name);
  4469. // This is inconsistent with how color (for example) is handled.
  4470. Terminal->SessionData->Name = Name;
  4471. UpdateControls();
  4472. // Add/Remove distinguishing paths from sessions of the same name.
  4473. DoTerminalListChanged();
  4474. }
  4475. }
  4476. //---------------------------------------------------------------------------
  4477. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4478. {
  4479. DebugAssert(Queue != NULL);
  4480. bool Result = Queue->IsEmpty;
  4481. if (!Result)
  4482. {
  4483. SetFocus();
  4484. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4485. }
  4486. return Result;
  4487. }
  4488. //---------------------------------------------------------------------------
  4489. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4490. {
  4491. return CanCloseQueue(FQueue);
  4492. }
  4493. //---------------------------------------------------------------------------
  4494. void __fastcall TCustomScpExplorerForm::CloseSession()
  4495. {
  4496. if (CanCloseQueue())
  4497. {
  4498. TTerminalManager::Instance()->FreeActiveTerminal();
  4499. }
  4500. }
  4501. //---------------------------------------------------------------------------
  4502. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4503. {
  4504. if (CanCloseQueue())
  4505. {
  4506. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4507. }
  4508. }
  4509. //---------------------------------------------------------------------------
  4510. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4511. {
  4512. RemoteDirView->Terminal = Terminal;
  4513. UpdateRemotePathComboBox(false);
  4514. UpdateControls();
  4515. }
  4516. //---------------------------------------------------------------------------
  4517. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4518. {
  4519. if (DebugAlwaysTrue(!Terminal->Active))
  4520. {
  4521. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4522. }
  4523. }
  4524. //---------------------------------------------------------------------------
  4525. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4526. {
  4527. if (OpenInNewWindow())
  4528. {
  4529. // encode session name because of slashes in hierarchical sessions
  4530. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4531. }
  4532. else
  4533. {
  4534. TTerminalManager * Manager = TTerminalManager::Instance();
  4535. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4536. Manager->ActiveTerminal = Terminal;
  4537. }
  4538. }
  4539. //---------------------------------------------------------------------------
  4540. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4541. {
  4542. TTerminalManager * Manager = TTerminalManager::Instance();
  4543. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4544. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4545. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4546. if (!ConnectFirstTerminal)
  4547. {
  4548. FirstTerminal->Disconnected = true;
  4549. FirstTerminal->DisconnectedTemporarily = true;
  4550. }
  4551. Manager->ActiveTerminal = FirstTerminal;
  4552. }
  4553. //---------------------------------------------------------------------------
  4554. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4555. {
  4556. if (OpenInNewWindow())
  4557. {
  4558. ExecuteNewInstance(Name);
  4559. }
  4560. else
  4561. {
  4562. DoOpenFolderOrWorkspace(Name, true);
  4563. }
  4564. }
  4565. //---------------------------------------------------------------------------
  4566. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4567. bool &CanClose)
  4568. {
  4569. if (Terminal != NULL)
  4570. {
  4571. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4572. {
  4573. unsigned int Result;
  4574. TMessageParams Params(mpNeverAskAgainCheck);
  4575. UnicodeString Message;
  4576. int Answers = qaOK | qaCancel;
  4577. if (TTerminalManager::Instance()->Count > 1)
  4578. {
  4579. if (!WinConfiguration->AutoSaveWorkspace)
  4580. {
  4581. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4582. }
  4583. else
  4584. {
  4585. Message = LoadStr(CLOSE_SESSIONS);
  4586. }
  4587. }
  4588. else
  4589. {
  4590. if (!WinConfiguration->AutoSaveWorkspace)
  4591. {
  4592. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4593. }
  4594. else
  4595. {
  4596. Message = LoadStr(CLOSE_SESSION);
  4597. }
  4598. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4599. }
  4600. UnicodeString Note;
  4601. if (WinConfiguration->AutoSaveWorkspace)
  4602. {
  4603. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4604. }
  4605. else
  4606. {
  4607. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4608. Answers = qaYes | qaNo | qaCancel;
  4609. }
  4610. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4611. SetFocus();
  4612. Result = MessageDialog(Message, qtConfirmation,
  4613. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4614. if (Result == qaNeverAskAgain)
  4615. {
  4616. WinConfiguration->ConfirmClosingSession = false;
  4617. }
  4618. if (Result == qaNo)
  4619. {
  4620. CanClose = SaveWorkspace(true);
  4621. // note that the workspace will be saved redundatly again from FormClose
  4622. }
  4623. else
  4624. {
  4625. CanClose =
  4626. (Result == qaOK) ||
  4627. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4628. (Result == qaNeverAskAgain);
  4629. }
  4630. }
  4631. if (CanClose)
  4632. {
  4633. CanClose = CanCloseQueue();
  4634. }
  4635. }
  4636. if (CanClose)
  4637. {
  4638. CanClose =
  4639. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4640. FEditorManager->CloseExternalFilesWithoutProcess();
  4641. if (CanClose)
  4642. {
  4643. while (CanClose && (FLocalEditors->Count > 0))
  4644. {
  4645. int PrevCount = FLocalEditors->Count;
  4646. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4647. CanClose = (FLocalEditors->Count < PrevCount);
  4648. }
  4649. if (CanClose)
  4650. {
  4651. CanClose = FEditorManager->Empty(true);
  4652. if (!CanClose)
  4653. {
  4654. SetFocus();
  4655. CanClose =
  4656. (MessageDialog(
  4657. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4658. }
  4659. }
  4660. }
  4661. }
  4662. }
  4663. //---------------------------------------------------------------------------
  4664. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4665. {
  4666. TForm * Form = dynamic_cast<TForm *>(Sender);
  4667. DebugAssert(Form != NULL);
  4668. Form->Close();
  4669. }
  4670. //---------------------------------------------------------------------------
  4671. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4672. {
  4673. TForm * Form = dynamic_cast<TForm *>(Sender);
  4674. delete Form;
  4675. }
  4676. //---------------------------------------------------------------------------
  4677. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4678. {
  4679. while (FLocalEditors->Count > 0)
  4680. {
  4681. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4682. }
  4683. }
  4684. //---------------------------------------------------------------------------
  4685. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4686. TObject *Sender)
  4687. {
  4688. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4689. try
  4690. {
  4691. SetProperties(osRemote, FileList);
  4692. }
  4693. __finally
  4694. {
  4695. delete FileList;
  4696. }
  4697. }
  4698. //---------------------------------------------------------------------------
  4699. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4700. {
  4701. if (value)
  4702. {
  4703. if (Component == fcCustomCommandsBand)
  4704. {
  4705. UpdateCustomCommandsToolbar();
  4706. }
  4707. }
  4708. }
  4709. //---------------------------------------------------------------------------
  4710. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4711. {
  4712. TControl * Control = GetComponent(Component);
  4713. DebugAssert(Control);
  4714. bool Changed = (Control->Visible != value);
  4715. if (Changed)
  4716. {
  4717. ComponentShowing(Component, value);
  4718. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4719. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4720. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4721. if (value)
  4722. {
  4723. int RemainingHeight = Control->Parent->ClientHeight;
  4724. int RemainingWidth = Control->Parent->ClientWidth;
  4725. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4726. {
  4727. TControl * ChildControl = Control->Parent->Controls[i];
  4728. if (ChildControl->Visible)
  4729. {
  4730. switch (ChildControl->Align)
  4731. {
  4732. case alTop:
  4733. case alBottom:
  4734. RemainingHeight -= ChildControl->Height;
  4735. break;
  4736. case alLeft:
  4737. case alRight:
  4738. RemainingWidth -= ChildControl->Width;
  4739. break;
  4740. }
  4741. }
  4742. }
  4743. int Reserve = ScaleByTextHeight(this, 32);
  4744. // queue in explorer, trees in commander
  4745. if (Control->Height > RemainingHeight - Reserve)
  4746. {
  4747. Control->Height = RemainingHeight / 2;
  4748. }
  4749. if (Control->Width > RemainingWidth - Reserve)
  4750. {
  4751. Control->Width = RemainingWidth / 2;
  4752. }
  4753. }
  4754. Control->Visible = value;
  4755. if (WasFocused && Visible)
  4756. {
  4757. DirView(osCurrent)->SetFocus();
  4758. }
  4759. FixControlsPlacement();
  4760. }
  4761. }
  4762. //---------------------------------------------------------------------------
  4763. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4764. {
  4765. TControl * Control = GetComponent(Component);
  4766. if (Control == NULL)
  4767. {
  4768. return false;
  4769. }
  4770. else
  4771. {
  4772. return Control->Visible;
  4773. }
  4774. }
  4775. //---------------------------------------------------------------------------
  4776. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4777. {
  4778. bool Result;
  4779. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4780. {
  4781. Result = !IsUWP();
  4782. }
  4783. else
  4784. {
  4785. Result = true;
  4786. }
  4787. return Result;
  4788. }
  4789. //---------------------------------------------------------------------------
  4790. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4791. {
  4792. if (DirView(osOther)->ItemFocused != NULL)
  4793. {
  4794. DirView(osOther)->ItemFocused->MakeVisible(false);
  4795. }
  4796. QueueSplitter->Visible = QueuePanel->Visible;
  4797. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4798. }
  4799. //---------------------------------------------------------------------------
  4800. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4801. {
  4802. switch (Component) {
  4803. case fcStatusBar: return RemoteStatusBar;
  4804. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4805. case fcQueueView: return QueuePanel;
  4806. case fcQueueToolbar: return QueueDock;
  4807. case fcRemoteTree: return RemoteDrivePanel;
  4808. case fcSessionsTabs: return SessionsPageControl;
  4809. default: return NULL;
  4810. }
  4811. }
  4812. //---------------------------------------------------------------------------
  4813. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4814. TObject *Sender, TListColumn *Column, TPoint &Point)
  4815. {
  4816. DebugAssert(NonVisualDataModule && Column && Sender);
  4817. NonVisualDataModule->ListColumn = Column;
  4818. TPopupMenu * DirViewColumnMenu;
  4819. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4820. {
  4821. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4822. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4823. (Column->Index == uvName);
  4824. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4825. (Column->Index == uvSize);
  4826. }
  4827. else
  4828. {
  4829. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4830. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4831. (Column->Index == dvName);
  4832. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4833. (Column->Index == dvSize);
  4834. }
  4835. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4836. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4837. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4838. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4839. }
  4840. //---------------------------------------------------------------------------
  4841. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4842. TObject *Sender, TListItem *Item, bool &AllowExec)
  4843. {
  4844. DoDirViewExecFile(Sender, Item, AllowExec);
  4845. }
  4846. //---------------------------------------------------------------------------
  4847. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4848. TListItem * Item, bool & AllowExec)
  4849. {
  4850. DebugAssert(Sender && Item && Configuration);
  4851. DebugAssert(AllowExec);
  4852. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4853. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4854. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4855. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4856. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4857. // on files only (not directories)
  4858. // and only when symlinks are resolved (apply to remote panel only)
  4859. if (!ADirView->ItemIsDirectory(Item) &&
  4860. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4861. {
  4862. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4863. !FForceExecution &&
  4864. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4865. {
  4866. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4867. {
  4868. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4869. UnlockWindow();
  4870. try
  4871. {
  4872. ExecuteFileOperation(
  4873. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4874. }
  4875. __finally
  4876. {
  4877. LockWindow();
  4878. }
  4879. AllowExec = false;
  4880. }
  4881. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4882. {
  4883. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4884. {
  4885. ExecuteFile(Side, efDefaultEditor);
  4886. AllowExec = false;
  4887. }
  4888. }
  4889. else
  4890. {
  4891. DebugFail();
  4892. }
  4893. }
  4894. // if we have not done anything special, fall back to default behavior
  4895. if (AllowExec)
  4896. {
  4897. if (Remote && !WinConfiguration->DisableOpenEdit)
  4898. {
  4899. ExecuteFile(osRemote, efShell);
  4900. AllowExec = false;
  4901. }
  4902. }
  4903. }
  4904. }
  4905. //---------------------------------------------------------------------------
  4906. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4907. {
  4908. return ((Side == osRemote) || (Side == osCurrent));
  4909. }
  4910. //---------------------------------------------------------------------------
  4911. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4912. {
  4913. DebugFail();
  4914. }
  4915. //---------------------------------------------------------------------------
  4916. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4917. {
  4918. DebugFail();
  4919. }
  4920. //---------------------------------------------------------------------------
  4921. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4922. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  4923. {
  4924. TSynchronizeParamType Params;
  4925. Params.LocalDirectory = LocalDirectory;
  4926. Params.RemoteDirectory = RemoteDirectory;
  4927. int UnusedParams =
  4928. (GUIConfiguration->SynchronizeParams &
  4929. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4930. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4931. Params.Options = GUIConfiguration->SynchronizeOptions;
  4932. bool SaveSettings = false;
  4933. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4934. &DoSynchronizeTooManyDirectories);
  4935. DebugAssert(FSynchronizeController == NULL);
  4936. FSynchronizeController = &Controller;
  4937. bool Result;
  4938. try
  4939. {
  4940. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4941. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4942. int Options =
  4943. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4944. DebugAssert(FOnFeedSynchronizeError == NULL);
  4945. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4946. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4947. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4948. if (Result)
  4949. {
  4950. if (SaveSettings)
  4951. {
  4952. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4953. GUIConfiguration->SynchronizeOptions = Params.Options;
  4954. }
  4955. else
  4956. {
  4957. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4958. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4959. FLAGSET(Params.Options, soSynchronize))
  4960. {
  4961. GUIConfiguration->SynchronizeOptions =
  4962. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4963. soSynchronize;
  4964. }
  4965. }
  4966. LocalDirectory = Params.LocalDirectory;
  4967. RemoteDirectory = Params.RemoteDirectory;
  4968. }
  4969. }
  4970. __finally
  4971. {
  4972. FSynchronizeController = NULL;
  4973. DebugAssert(FOnFeedSynchronizeError == NULL);
  4974. FOnFeedSynchronizeError = NULL;
  4975. }
  4976. return Result;
  4977. }
  4978. //---------------------------------------------------------------------------
  4979. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4980. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4981. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4982. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4983. TSynchronizeOptions * Options, bool Full)
  4984. {
  4985. // Keep-up-to-date mode
  4986. if (Terminal->Status == ssOpened)
  4987. {
  4988. try
  4989. {
  4990. int PParams = Params.Params;
  4991. if (!Full)
  4992. {
  4993. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4994. if (FLAGSET(Params.Options, soRecurse))
  4995. {
  4996. PParams |= TTerminal::spSubDirs;
  4997. }
  4998. }
  4999. else
  5000. {
  5001. // if keepuptodate is non-recursive,
  5002. // full sync before has to be non-recursive as well
  5003. if (FLAGCLEAR(Params.Options, soRecurse))
  5004. {
  5005. PParams |= TTerminal::spNoRecurse;
  5006. }
  5007. }
  5008. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5009. PParams, Checklist, Options);
  5010. }
  5011. catch (Exception & E)
  5012. {
  5013. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5014. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5015. {
  5016. // noop, already logged in MoreMessageDialog
  5017. }
  5018. else
  5019. {
  5020. ShowExtendedExceptionEx(Terminal, &E);
  5021. throw;
  5022. }
  5023. }
  5024. }
  5025. }
  5026. //---------------------------------------------------------------------------
  5027. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5028. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5029. const UnicodeString ErrorStr)
  5030. {
  5031. if (!Directory.IsEmpty())
  5032. {
  5033. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5034. }
  5035. else
  5036. {
  5037. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5038. }
  5039. }
  5040. //---------------------------------------------------------------------------
  5041. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5042. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5043. {
  5044. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5045. {
  5046. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5047. }
  5048. else
  5049. {
  5050. TMessageParams Params(mpNeverAskAgainCheck);
  5051. unsigned int Result = MessageDialog(
  5052. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5053. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5054. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5055. {
  5056. MaxDirectories *= 2;
  5057. if (Result == qaNeverAskAgain)
  5058. {
  5059. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5060. }
  5061. }
  5062. else
  5063. {
  5064. Abort();
  5065. }
  5066. }
  5067. }
  5068. //---------------------------------------------------------------------------
  5069. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5070. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5071. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5072. TSynchronizeOptions * Options)
  5073. {
  5074. // Keep-up-to-date mode
  5075. DebugAssert(!FAutoOperation);
  5076. void * BatchStorage;
  5077. BatchStart(BatchStorage);
  5078. FAutoOperation = true;
  5079. bool AnyOperation = false;
  5080. TDateTime StartTime = Now();
  5081. TSynchronizeChecklist * AChecklist = NULL;
  5082. try
  5083. {
  5084. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5085. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5086. {
  5087. FSynchronizeProgressForm->Start();
  5088. }
  5089. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5090. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5091. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5092. Options);
  5093. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5094. SAFE_DESTROY(FSynchronizeProgressForm);
  5095. AnyOperation = (AChecklist->CheckedCount > 0);
  5096. if (AnyOperation)
  5097. {
  5098. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5099. CreateProgressForm(&SynchronizeProgress);
  5100. try
  5101. {
  5102. Terminal->SynchronizeApply(
  5103. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5104. SynchronizeProcessedItem, NULL, NULL, NULL);
  5105. }
  5106. __finally
  5107. {
  5108. DestroyProgressForm();
  5109. }
  5110. }
  5111. }
  5112. __finally
  5113. {
  5114. if (Checklist == NULL)
  5115. {
  5116. delete AChecklist;
  5117. }
  5118. else
  5119. {
  5120. *Checklist = AChecklist;
  5121. }
  5122. FAutoOperation = false;
  5123. SAFE_DESTROY(FSynchronizeProgressForm);
  5124. BatchEnd(BatchStorage);
  5125. ReloadLocalDirectory();
  5126. if (AnyOperation)
  5127. {
  5128. OperationComplete(StartTime);
  5129. }
  5130. }
  5131. }
  5132. //---------------------------------------------------------------------------
  5133. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5134. {
  5135. // can be called from command line
  5136. return Visible &&
  5137. ((DirView(osOther)->SelCount > 0) ||
  5138. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5139. }
  5140. //---------------------------------------------------------------------------
  5141. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5142. {
  5143. LogSynchronizeEvent(Terminal, Message);
  5144. }
  5145. //---------------------------------------------------------------------------
  5146. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5147. int Params, TSynchronizeOptions & Options)
  5148. {
  5149. DebugAssert(!IsLocalBrowserMode());
  5150. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5151. {
  5152. Options.Filter = new TStringList();
  5153. Options.Filter->CaseSensitive = false;
  5154. Options.Filter->Duplicates = Types::dupAccept;
  5155. if (DirView(osRemote)->SelCount > 0)
  5156. {
  5157. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5158. }
  5159. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5160. {
  5161. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5162. }
  5163. Options.Filter->Sort();
  5164. }
  5165. }
  5166. //---------------------------------------------------------------------------
  5167. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5168. {
  5169. TCopyParamType Defaults;
  5170. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5171. ConfigStorage->AccessMode = smRead;
  5172. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5173. {
  5174. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5175. }
  5176. ConfigStorage.reset(NULL);
  5177. std::unique_ptr<TStringList> Options(new TStringList());
  5178. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5179. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5180. UnicodeString Result;
  5181. if (Options->Count > 0)
  5182. {
  5183. Result =
  5184. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5185. }
  5186. return Result;
  5187. }
  5188. //---------------------------------------------------------------------------
  5189. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5190. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5191. {
  5192. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5193. UnicodeString AdditionalParams =
  5194. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5195. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5196. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5197. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5198. SerializeCopyParamForCommandLine(CopyParams)));
  5199. ExecuteNewInstance(SessionName, AdditionalParams);
  5200. }
  5201. //---------------------------------------------------------------------------
  5202. struct TSynchronizeParams
  5203. {
  5204. UnicodeString LocalDirectory;
  5205. UnicodeString RemoteDirectory;
  5206. TSynchronizeMode Mode;
  5207. TSynchronizeChecklist * Checklist;
  5208. int Params;
  5209. TCopyParamType * CopyParam;
  5210. TDateTime CollectElapsed;
  5211. TDateTime * StartTime;
  5212. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5213. };
  5214. //---------------------------------------------------------------------------
  5215. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5216. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5217. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5218. {
  5219. DebugAssert(!FAutoOperation);
  5220. void * BatchStorage;
  5221. BatchStart(BatchStorage);
  5222. FAutoOperation = true;
  5223. TFileOperationStatistics Statistics;
  5224. TDateTime Start = Now();
  5225. try
  5226. {
  5227. Params.OnProcessedItem = OnProcessedItem;
  5228. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5229. CreateProgressForm(&SynchronizeProgress);
  5230. Terminal->SynchronizeApply(
  5231. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5232. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5233. &Statistics);
  5234. }
  5235. __finally
  5236. {
  5237. Params.OnProcessedItem = NULL;
  5238. FAutoOperation = false;
  5239. DestroyProgressForm();
  5240. BatchEnd(BatchStorage);
  5241. ReloadLocalDirectory();
  5242. }
  5243. if (WinConfiguration->SynchronizeSummary)
  5244. {
  5245. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5246. // The statistics should be 0 anyway in this case
  5247. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5248. {
  5249. Message += L"\n";
  5250. if (Statistics.FilesUploaded > 0)
  5251. {
  5252. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5253. }
  5254. if (Statistics.FilesDownloaded > 0)
  5255. {
  5256. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5257. }
  5258. if (Statistics.FilesDeletedLocal > 0)
  5259. {
  5260. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5261. }
  5262. if (Statistics.FilesDeletedRemote > 0)
  5263. {
  5264. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5265. }
  5266. }
  5267. TDateTime Elapsed = (Now() - Start);
  5268. Message +=
  5269. L"\n" +
  5270. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5271. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5272. TMessageParams Params(mpNeverAskAgainCheck);
  5273. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5274. if (Result == qaNeverAskAgain)
  5275. {
  5276. WinConfiguration->SynchronizeSummary = false;
  5277. }
  5278. }
  5279. }
  5280. //---------------------------------------------------------------------------
  5281. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5282. {
  5283. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5284. {
  5285. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5286. }
  5287. // Not set in keep-up-to-date mode - Synchronize() method
  5288. if (Token != NULL)
  5289. {
  5290. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5291. if (Params.OnProcessedItem != NULL)
  5292. {
  5293. Params.OnProcessedItem(NULL, ChecklistItem);
  5294. }
  5295. }
  5296. }
  5297. //---------------------------------------------------------------------------
  5298. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5299. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5300. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5301. {
  5302. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5303. *Params.StartTime = Now();
  5304. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5305. }
  5306. //---------------------------------------------------------------------------
  5307. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5308. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5309. {
  5310. // terminal can be already closed (e.g. dropped connection)
  5311. if (Terminal != NULL)
  5312. {
  5313. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5314. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5315. }
  5316. }
  5317. //---------------------------------------------------------------------------
  5318. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5319. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5320. {
  5321. DebugAssert(!IsLocalBrowserMode());
  5322. TAutoBatch AutoBatch(this);
  5323. TAutoFlag AutoOperationFlag(FAutoOperation);
  5324. if (Side == osRemote)
  5325. {
  5326. std::unique_ptr<TStrings> FileList(new TStringList());
  5327. FileList->AddObject(FileName, RemoteFile);
  5328. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5329. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5330. RemoteDirView->SaveSelection();
  5331. RemoteDirView->SaveSelectedNames();
  5332. try
  5333. {
  5334. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5335. }
  5336. catch(...)
  5337. {
  5338. RemoteDirView->DiscardSavedSelection();
  5339. throw;
  5340. }
  5341. RemoteDirView->RestoreSelection();
  5342. }
  5343. else if (DebugAlwaysTrue(Side == osLocal))
  5344. {
  5345. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5346. {
  5347. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5348. }
  5349. UnicodeString Directory = ExtractFileDir(FileName);
  5350. ReloadLocalDirectory(Directory);
  5351. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5352. if (!SamePaths(Directory, NewDirectory))
  5353. {
  5354. ReloadLocalDirectory(NewDirectory);
  5355. }
  5356. }
  5357. }
  5358. //---------------------------------------------------------------------------
  5359. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5360. {
  5361. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5362. if (Side == osLocal)
  5363. {
  5364. if (Action == TSynchronizeChecklist::saDownloadNew)
  5365. {
  5366. OpenFolderInExplorer(LocalPath);
  5367. }
  5368. else
  5369. {
  5370. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5371. }
  5372. }
  5373. else if (DebugAlwaysTrue(Side == osRemote))
  5374. {
  5375. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5376. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5377. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5378. if (!LocalPath.IsEmpty())
  5379. {
  5380. SessionData->LocalDirectory = LocalPath;
  5381. }
  5382. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5383. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5384. }
  5385. }
  5386. //---------------------------------------------------------------------------
  5387. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5388. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5389. const TCopyParamType * CopyParams)
  5390. {
  5391. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5392. UnicodeString AdditionalParams =
  5393. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5394. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5395. LocalDirectory, RemoteDirectory, Mode, Params,
  5396. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5397. SerializeCopyParamForCommandLine(CopyParams)));
  5398. ExecuteNewInstance(SessionName, AdditionalParams);
  5399. }
  5400. //---------------------------------------------------------------------------
  5401. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5402. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5403. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5404. {
  5405. int Result;
  5406. bool SaveSettings = false;
  5407. int Options =
  5408. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5409. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5410. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5411. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5412. bool Continue =
  5413. (UseDefaults == 0) ||
  5414. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5415. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5416. if (Continue)
  5417. {
  5418. Configuration->Usage->Inc(L"Synchronizations");
  5419. UpdateCopyParamCounters(CopyParam);
  5420. TSynchronizeOptions SynchronizeOptions;
  5421. GetSynchronizeOptions(Params, SynchronizeOptions);
  5422. if (SaveSettings)
  5423. {
  5424. GUIConfiguration->SynchronizeParams = Params;
  5425. }
  5426. else
  5427. {
  5428. SaveMode = false;
  5429. }
  5430. TDateTime StartTime = Now();
  5431. TSynchronizeChecklist * Checklist = NULL;
  5432. try
  5433. {
  5434. DebugAssert(!FAutoOperation);
  5435. FAutoOperation = true;
  5436. try
  5437. {
  5438. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5439. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5440. int Files = -1;
  5441. if (DataList->Count >= 1)
  5442. {
  5443. Files = StrToIntDef(DataList->Strings[0], Files);
  5444. }
  5445. else
  5446. {
  5447. DataList->Add(UnicodeString());
  5448. }
  5449. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5450. FSynchronizeProgressForm->Start();
  5451. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5452. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5453. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5454. &SynchronizeOptions);
  5455. if (Terminal->SessionData->CacheDirectories)
  5456. {
  5457. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5458. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5459. }
  5460. }
  5461. __finally
  5462. {
  5463. FAutoOperation = false;
  5464. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5465. SAFE_DESTROY(FSynchronizeProgressForm);
  5466. }
  5467. TSynchronizeParams SynchronizeParams;
  5468. SynchronizeParams.LocalDirectory = LocalDirectory;
  5469. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5470. SynchronizeParams.Mode = Mode;
  5471. SynchronizeParams.CopyParam = &CopyParam;
  5472. SynchronizeParams.Params = Params;
  5473. SynchronizeParams.Checklist = Checklist;
  5474. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5475. SynchronizeParams.StartTime = &StartTime;
  5476. SynchronizeParams.OnProcessedItem = NULL;
  5477. Result = Checklist->Count;
  5478. if (Checklist->Count > 0)
  5479. {
  5480. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5481. {
  5482. if (!DoSynchronizeChecklistDialog(
  5483. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5484. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5485. {
  5486. Result = -1;
  5487. }
  5488. }
  5489. else
  5490. {
  5491. FullSynchronize(SynchronizeParams, NULL, NULL);
  5492. }
  5493. }
  5494. else
  5495. {
  5496. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5497. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5498. }
  5499. }
  5500. __finally
  5501. {
  5502. delete Checklist;
  5503. }
  5504. OperationComplete(StartTime);
  5505. }
  5506. else
  5507. {
  5508. Result = -1;
  5509. }
  5510. return Result;
  5511. }
  5512. //---------------------------------------------------------------------------
  5513. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5514. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5515. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5516. {
  5517. if (Collect)
  5518. {
  5519. DebugAssert(FSynchronizeProgressForm != NULL);
  5520. if (!FSynchronizeProgressForm->Started)
  5521. {
  5522. FSynchronizeProgressForm->Start();
  5523. }
  5524. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5525. SetTaskbarListProgressValue(CompareProgress);
  5526. }
  5527. else
  5528. {
  5529. DebugAssert(FSynchronizeProgressForm == NULL);
  5530. DebugAssert(FProgressForm != NULL);
  5531. }
  5532. }
  5533. //---------------------------------------------------------------------------
  5534. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5535. {
  5536. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5537. TRemoteFile * File = NULL;
  5538. Terminal->ReadFile(FullFileName, File);
  5539. if (File != NULL)
  5540. {
  5541. std::unique_ptr<TRemoteFile> FileOwner(File);
  5542. TAutoFlag Flag(FStandaloneEditing);
  5543. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5544. Application->ShowMainForm = false;
  5545. Application->Run();
  5546. }
  5547. }
  5548. //---------------------------------------------------------------------------
  5549. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5550. {
  5551. DebugFail();
  5552. }
  5553. //---------------------------------------------------------------------------
  5554. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5555. {
  5556. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5557. SessionData->Assign(Terminal->SessionData);
  5558. UpdateSessionData(SessionData.get());
  5559. Terminal->UpdateSessionCredentials(SessionData.get());
  5560. if (!Terminal->SessionData->HasSessionName())
  5561. {
  5562. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5563. // once IsWorkspace is cleared
  5564. SessionData->Name = UnicodeString();
  5565. SessionData->IsWorkspace = false;
  5566. }
  5567. return SessionData.release();
  5568. }
  5569. //---------------------------------------------------------------------------
  5570. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5571. {
  5572. TSessionData * SessionData = CloneCurrentSessionData();
  5573. try
  5574. {
  5575. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5576. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5577. }
  5578. __finally
  5579. {
  5580. delete SessionData;
  5581. }
  5582. }
  5583. //---------------------------------------------------------------------------
  5584. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5585. {
  5586. TTerminalManager * Manager = TTerminalManager::Instance();
  5587. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5588. if (DebugAlwaysTrue(Terminal != NULL))
  5589. {
  5590. // Update (Managed)Terminal->StateData
  5591. UpdateTerminal(Terminal);
  5592. }
  5593. Manager->SaveWorkspace(DataList.get());
  5594. return DataList.release();
  5595. }
  5596. //---------------------------------------------------------------------------
  5597. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5598. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5599. {
  5600. WinConfiguration->LastWorkspace = Name;
  5601. if (!SavePasswords)
  5602. {
  5603. for (int Index = 0; Index < DataList->Count; Index++)
  5604. {
  5605. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5606. if (SessionData->Link.IsEmpty())
  5607. {
  5608. SessionData->ClearSessionPasswords();
  5609. }
  5610. }
  5611. }
  5612. StoredSessions->NewWorkspace(Name, DataList);
  5613. // modified only
  5614. StoredSessions->Save(false, Explicit);
  5615. }
  5616. //---------------------------------------------------------------------------
  5617. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5618. {
  5619. return
  5620. DefaultStr(WinConfiguration->LastWorkspace,
  5621. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5622. }
  5623. //---------------------------------------------------------------------------
  5624. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5625. {
  5626. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5627. bool AnyNonStoredSessionWithPassword = false;
  5628. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5629. bool AllNonStoredSessionsAnonymous = true;
  5630. TTerminalManager * Manager = TTerminalManager::Instance();
  5631. for (int Index = 0; Index < DataList->Count; Index++)
  5632. {
  5633. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5634. if (SessionData->Link.IsEmpty())
  5635. {
  5636. if (SessionData->HasAnySessionPassword())
  5637. {
  5638. AnyNonStoredSessionWithPassword = true;
  5639. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5640. {
  5641. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5642. }
  5643. }
  5644. if (!SameText(SessionData->UserName, AnonymousUserName))
  5645. {
  5646. AllNonStoredSessionsAnonymous = false;
  5647. }
  5648. }
  5649. }
  5650. bool SavePasswords;
  5651. bool * PSavePasswords;
  5652. bool NotRecommendedSavingPasswords =
  5653. !CustomWinConfiguration->UseMasterPassword &&
  5654. !AllNonStoredSessionsAnonymous;
  5655. if (Configuration->DisablePasswordStoring ||
  5656. !AnyNonStoredSessionWithPassword)
  5657. {
  5658. PSavePasswords = NULL;
  5659. SavePasswords = false;
  5660. }
  5661. else
  5662. {
  5663. PSavePasswords = &SavePasswords;
  5664. SavePasswords =
  5665. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5666. !NotRecommendedSavingPasswords;
  5667. }
  5668. UnicodeString Name = WorkspaceName();
  5669. bool CreateShortcut = false;
  5670. bool Result =
  5671. DoSaveWorkspaceDialog(
  5672. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5673. EnableAutoSave);
  5674. if (Result)
  5675. {
  5676. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5677. if (CreateShortcut)
  5678. {
  5679. TOperationVisualizer Visualizer;
  5680. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5681. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5682. }
  5683. if (EnableAutoSave)
  5684. {
  5685. WinConfiguration->AutoSaveWorkspace = true;
  5686. WinConfiguration->AutoWorkspace = Name;
  5687. if (PSavePasswords != NULL)
  5688. {
  5689. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5690. }
  5691. }
  5692. }
  5693. return Result;
  5694. }
  5695. //---------------------------------------------------------------------------
  5696. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5697. {
  5698. DebugAssert(!IsLocalBrowserMode());
  5699. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5700. if (WinConfiguration->PreservePanelState)
  5701. {
  5702. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5703. }
  5704. UpdateSessionData(Terminal->StateData);
  5705. }
  5706. //---------------------------------------------------------------------------
  5707. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5708. {
  5709. // Keep in sync with TSessionData::CopyStateData
  5710. DebugAssert(!IsLocalBrowserMode());
  5711. DebugAssert(Data != NULL);
  5712. // This is inconsistent with how color (for example) is handled.
  5713. Data->Name = Terminal->SessionData->Name;
  5714. // cannot use RemoteDirView->Path, because it is empty if connection
  5715. // was already closed
  5716. // also only peek, we may not be connected at all atm
  5717. // so make sure we do not try retrieving current directory from the server
  5718. // (particularly with FTP)
  5719. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5720. if (!ACurrentDirectory.IsEmpty())
  5721. {
  5722. Data->RemoteDirectory = ACurrentDirectory;
  5723. }
  5724. Data->Color = SessionColor;
  5725. }
  5726. //---------------------------------------------------------------------------
  5727. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5728. {
  5729. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5730. DebugAssert(Toolbar != NULL);
  5731. for (int i = 0; i < Toolbar->Items->Count; i++)
  5732. {
  5733. TTBXCustomDropDownItem * DropDownItem;
  5734. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5735. if (DropDownItem != NULL)
  5736. {
  5737. ToolbarItemResize(DropDownItem,
  5738. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5739. Toolbar->NonClientWidth);
  5740. break;
  5741. }
  5742. }
  5743. }
  5744. //---------------------------------------------------------------------------
  5745. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5746. {
  5747. Item->EditWidth = Width;
  5748. }
  5749. //---------------------------------------------------------------------------
  5750. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5751. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5752. {
  5753. for (int i = 0; i < Toolbar->Items->Count; i++)
  5754. {
  5755. TTBXCustomDropDownItem * DropDownItem;
  5756. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5757. if (DropDownItem != NULL)
  5758. {
  5759. ASize.x -= DropDownItem->EditWidth;
  5760. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5761. }
  5762. }
  5763. }
  5764. //---------------------------------------------------------------------------
  5765. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5766. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5767. {
  5768. if (WinConfiguration->DDWarnLackOfTempSpace)
  5769. {
  5770. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5771. if (!ADrive.IsEmpty())
  5772. {
  5773. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5774. DebugAssert(RequiredSpace >= 0);
  5775. __int64 RequiredWithReserve;
  5776. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5777. if (FreeSpace < RequiredWithReserve)
  5778. {
  5779. unsigned int Result;
  5780. TMessageParams Params(mpNeverAskAgainCheck);
  5781. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5782. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5783. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5784. if (Result == qaNeverAskAgain)
  5785. {
  5786. WinConfiguration->DDWarnLackOfTempSpace = false;
  5787. }
  5788. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5789. }
  5790. }
  5791. }
  5792. }
  5793. //---------------------------------------------------------------------------
  5794. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5795. {
  5796. DoOpenDirectoryDialog(odAddBookmark, Side);
  5797. }
  5798. //---------------------------------------------------------------------------
  5799. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5800. {
  5801. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5802. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5803. try
  5804. {
  5805. TCustomDirView * DView = DirView(Side);
  5806. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5807. {
  5808. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5809. }
  5810. }
  5811. catch (...)
  5812. {
  5813. delete VisitedDirectories;
  5814. throw;
  5815. }
  5816. return VisitedDirectories;
  5817. }
  5818. //---------------------------------------------------------------------------
  5819. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5820. TOpenDirectoryMode Mode, TOperationSide Side)
  5821. {
  5822. bool Continue = true;
  5823. if (Mode == odAddBookmark)
  5824. {
  5825. TMessageParams Params(mpNeverAskAgainCheck);
  5826. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5827. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5828. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5829. unsigned int Answer =
  5830. MessageDialog(Message,
  5831. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5832. if (Answer == qaNeverAskAgain)
  5833. {
  5834. Continue = true;
  5835. WinConfiguration->UseSharedBookmarks = true;
  5836. }
  5837. else if (Answer == qaOK)
  5838. {
  5839. Continue = true;
  5840. WinConfiguration->UseSharedBookmarks = false;
  5841. }
  5842. else
  5843. {
  5844. Continue = false;
  5845. }
  5846. }
  5847. if (Continue)
  5848. {
  5849. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5850. try
  5851. {
  5852. UnicodeString Name = DirView(Side)->PathName;
  5853. bool Repeat;
  5854. do
  5855. {
  5856. Repeat = false;
  5857. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5858. // do not allow switching to location profiles,
  5859. // if we are not connected
  5860. HasDirView[osLocal] && (Terminal != NULL)))
  5861. {
  5862. TWindowLock Lock(this);
  5863. if (!TryOpenDirectory(Side, Name))
  5864. {
  5865. Repeat = true;
  5866. Mode = odBrowse;
  5867. }
  5868. }
  5869. }
  5870. while (Repeat);
  5871. }
  5872. __finally
  5873. {
  5874. delete VisitedDirectories;
  5875. }
  5876. }
  5877. }
  5878. //---------------------------------------------------------------------------
  5879. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5880. {
  5881. DebugAssert(!FTerminal->CommandSessionOpened);
  5882. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5883. }
  5884. //---------------------------------------------------------------------------
  5885. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5886. {
  5887. bool Result = FTerminal->IsCapable[Capability] ||
  5888. FTerminal->CommandSessionOpened;
  5889. if (!Result)
  5890. {
  5891. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5892. if (!GUIConfiguration->ConfirmCommandSession)
  5893. {
  5894. Result = true;
  5895. }
  5896. else
  5897. {
  5898. TMessageParams Params(mpNeverAskAgainCheck);
  5899. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5900. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5901. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5902. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5903. if (Answer == qaNeverAskAgain)
  5904. {
  5905. GUIConfiguration->ConfirmCommandSession = false;
  5906. Result = true;
  5907. }
  5908. else if (Answer == qaOK)
  5909. {
  5910. Result = true;
  5911. }
  5912. }
  5913. if (Result)
  5914. {
  5915. Result = CommandSessionFallback();
  5916. }
  5917. }
  5918. return Result;
  5919. }
  5920. //---------------------------------------------------------------------------
  5921. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5922. {
  5923. if (EnsureCommandSessionFallback(fcAnyCommand))
  5924. {
  5925. DoConsoleDialog(Terminal, Command);
  5926. }
  5927. }
  5928. //---------------------------------------------------------------------------
  5929. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5930. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5931. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5932. {
  5933. if (!IsSideLocalBrowser(osRemote) &&
  5934. IsFileControl(DropSourceControl, osRemote) &&
  5935. (FDDExtMapFile != NULL))
  5936. {
  5937. Accept = true;
  5938. }
  5939. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5940. DebugAssert(FDDTargetControl != NULL);
  5941. }
  5942. //---------------------------------------------------------------------------
  5943. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5944. _di_IDataObject DataObj, int KeyState,
  5945. const TPoint & Point, int & Effect, bool & Accept)
  5946. {
  5947. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5948. }
  5949. //---------------------------------------------------------------------------
  5950. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5951. _di_IDataObject DataObj, int KeyState,
  5952. const TPoint & Point, int & Effect, bool & Accept)
  5953. {
  5954. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5955. }
  5956. //---------------------------------------------------------------------------
  5957. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5958. TObject * Sender)
  5959. {
  5960. DebugUsedParam(Sender);
  5961. DebugAssert(FDDTargetControl == Sender);
  5962. FDDTargetControl = NULL;
  5963. }
  5964. //---------------------------------------------------------------------------
  5965. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5966. {
  5967. FileControlDDDragLeave(SessionsPageControl);
  5968. }
  5969. //---------------------------------------------------------------------------
  5970. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5971. {
  5972. FileControlDDDragLeave(QueueView3);
  5973. }
  5974. //---------------------------------------------------------------------------
  5975. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5976. {
  5977. DebugAssert(!IsSideLocalBrowser(Side));
  5978. DebugUsedParam(Side);
  5979. bool Edit = false;
  5980. TRemoteFile * File = NULL;
  5981. UnicodeString FileName;
  5982. UnicodeString PointTo;
  5983. bool SymbolicLink = true;
  5984. if (RemoteDirView->ItemFocused)
  5985. {
  5986. DebugAssert(RemoteDirView->ItemFocused->Data);
  5987. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5988. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5989. if (Edit)
  5990. {
  5991. FileName = File->FileName;
  5992. PointTo = File->LinkTo;
  5993. }
  5994. else
  5995. {
  5996. PointTo = File->FileName;
  5997. }
  5998. }
  5999. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6000. Terminal->IsCapable[fcHardLink]))
  6001. {
  6002. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6003. if (Edit)
  6004. {
  6005. DebugAssert(File->FileName == FileName);
  6006. int Params = dfNoRecursive;
  6007. Terminal->ExceptionOnFail = true;
  6008. try
  6009. {
  6010. Terminal->DeleteFile(L"", File, &Params);
  6011. }
  6012. __finally
  6013. {
  6014. Terminal->ExceptionOnFail = false;
  6015. }
  6016. }
  6017. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6018. }
  6019. }
  6020. //---------------------------------------------------------------------------
  6021. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6022. {
  6023. return
  6024. (IsSideLocalBrowser(Side) ||
  6025. ((Terminal != NULL) &&
  6026. Terminal->Active &&
  6027. Terminal->ResolvingSymlinks &&
  6028. Terminal->IsCapable[fcSymbolicLink]));
  6029. }
  6030. //---------------------------------------------------------------------------
  6031. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6032. {
  6033. return
  6034. !IsSideLocalBrowser(FCurrentSide) &&
  6035. (RemoteDirView->ItemFocused != NULL) &&
  6036. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6037. Terminal->SessionData->ResolveSymlinks;
  6038. }
  6039. //---------------------------------------------------------------------------
  6040. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6041. {
  6042. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6043. FForceExecution = true;
  6044. try
  6045. {
  6046. DirView(osCurrent)->ExecuteCurrentFile();
  6047. }
  6048. __finally
  6049. {
  6050. FForceExecution = false;
  6051. }
  6052. }
  6053. //---------------------------------------------------------------------------
  6054. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6055. {
  6056. TEditorData ExternalEditor;
  6057. ExternalEditor.Editor = edExternal;
  6058. bool Remember = false;
  6059. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6060. {
  6061. if (Remember)
  6062. {
  6063. TEditorList * EditorList = new TEditorList;
  6064. try
  6065. {
  6066. *EditorList = *WinConfiguration->EditorList;
  6067. bool Found = false;
  6068. int i = 0;
  6069. while (!Found && (i < EditorList->Count))
  6070. {
  6071. const TEditorPreferences * Editor = EditorList->Editors[i];
  6072. if ((Editor->Data->Editor == edExternal) &&
  6073. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6074. {
  6075. Found = true;
  6076. }
  6077. i++;
  6078. }
  6079. if (!Found)
  6080. {
  6081. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6082. WinConfiguration->EditorList = EditorList;
  6083. }
  6084. }
  6085. __finally
  6086. {
  6087. delete EditorList;
  6088. }
  6089. }
  6090. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6091. }
  6092. }
  6093. //---------------------------------------------------------------------------
  6094. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6095. {
  6096. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6097. if (FFileFindTerminal == Sender)
  6098. {
  6099. FFileFindTerminal = NULL;
  6100. HideFileFindDialog();
  6101. }
  6102. if (FClipboardTerminal == Sender)
  6103. {
  6104. ClipboardClear(); // implies ClipboardStop
  6105. }
  6106. }
  6107. //---------------------------------------------------------------------------
  6108. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6109. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6110. {
  6111. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6112. DebugAssert(Terminal != NULL);
  6113. if (Data->Terminal == Terminal)
  6114. {
  6115. Data->Terminal = NULL;
  6116. }
  6117. }
  6118. //---------------------------------------------------------------------------
  6119. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6120. {
  6121. UpdateControls();
  6122. SessionColor = TColor(0);
  6123. UpdateRemotePathComboBox(false);
  6124. NeedSession(false);
  6125. }
  6126. //---------------------------------------------------------------------------
  6127. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6128. {
  6129. try
  6130. {
  6131. // Cache, as the login dialog can change its value
  6132. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6133. try
  6134. {
  6135. if (ShowLogin)
  6136. {
  6137. bool ReloadSessions = !Startup;
  6138. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6139. }
  6140. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6141. // This detects if workspace was saved the last time the main widow was closed
  6142. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6143. {
  6144. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6145. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6146. }
  6147. }
  6148. __finally
  6149. {
  6150. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6151. // (so there was no chance for the user to open any session yet)
  6152. if ((ShowLogin || !Startup) &&
  6153. !WinConfiguration->KeepOpenWhenNoSession &&
  6154. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6155. {
  6156. TerminateApplication();
  6157. }
  6158. }
  6159. }
  6160. catch (EAbort &)
  6161. {
  6162. // swallow
  6163. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6164. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6165. }
  6166. }
  6167. //---------------------------------------------------------------------------
  6168. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6169. {
  6170. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6171. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6172. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6173. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6174. try
  6175. {
  6176. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6177. // Clear the design time unthemed tab
  6178. ((SessionsPageControl->PageCount > 0) &&
  6179. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6180. {
  6181. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6182. }
  6183. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6184. {
  6185. TThemeTabSheet * TabSheet;
  6186. if (Index >= SessionsPageControl->PageCount)
  6187. {
  6188. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6189. TabSheet->PageControl = SessionsPageControl;
  6190. }
  6191. else
  6192. {
  6193. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6194. }
  6195. bool IsSessionTab = (Index < TerminalList->Count);
  6196. if (IsSessionTab)
  6197. {
  6198. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6199. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6200. UpdateSessionTab(TabSheet);
  6201. }
  6202. else
  6203. {
  6204. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6205. TabSheet->Tag = 0; // not really needed
  6206. TabSheet->Shadowed = false;
  6207. // 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)
  6208. UpdateNewSessionTab();
  6209. }
  6210. TabSheet->ShowCloseButton = IsSessionTab;
  6211. }
  6212. }
  6213. __finally
  6214. {
  6215. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6216. }
  6217. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6218. }
  6219. //---------------------------------------------------------------------------
  6220. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6221. {
  6222. DoTerminalListChanged();
  6223. }
  6224. //---------------------------------------------------------------------------
  6225. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6226. {
  6227. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6228. DebugAssert(TabSheet->ImageIndex == 0);
  6229. TabSheet->Caption =
  6230. WinConfiguration->SelectiveToolbarText ?
  6231. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6232. UnicodeString();
  6233. }
  6234. //---------------------------------------------------------------------------
  6235. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6236. {
  6237. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6238. }
  6239. //---------------------------------------------------------------------------
  6240. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6241. {
  6242. if (DebugAlwaysTrue(TabSheet != NULL))
  6243. {
  6244. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6245. if (DebugAlwaysTrue(ATerminal != NULL))
  6246. {
  6247. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6248. TabSheet->ImageIndex = AddSessionColor(Color);
  6249. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6250. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6251. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6252. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6253. {
  6254. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6255. }
  6256. }
  6257. }
  6258. }
  6259. //---------------------------------------------------------------------------
  6260. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6261. {
  6262. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6263. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6264. bool Result = (Terminal != NULL);
  6265. if (Result)
  6266. {
  6267. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6268. }
  6269. else
  6270. {
  6271. try
  6272. {
  6273. NewSession(false);
  6274. }
  6275. __finally
  6276. {
  6277. DoTerminalListChanged();
  6278. }
  6279. FSessionsPageControlNewSessionTime = Now();
  6280. }
  6281. return Result;
  6282. }
  6283. //---------------------------------------------------------------------------
  6284. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6285. {
  6286. SessionTabSwitched();
  6287. }
  6288. //---------------------------------------------------------------------------
  6289. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6290. {
  6291. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6292. DebugAssert(TransferList != NULL);
  6293. UnicodeString Name;
  6294. if (TransferList->ItemIndex <= 0)
  6295. {
  6296. Name = L"";
  6297. }
  6298. else
  6299. {
  6300. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6301. }
  6302. if (FCopyParamAutoSelected.IsEmpty())
  6303. {
  6304. // if previous preset was not autoselected, make new preset the "default"
  6305. FCopyParamDefault = Name;
  6306. }
  6307. GUIConfiguration->CopyParamCurrent = Name;
  6308. }
  6309. //---------------------------------------------------------------------------
  6310. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6311. {
  6312. // sanity check
  6313. bool ExistingPreset =
  6314. (FTransferListHoverIndex >= 0) &&
  6315. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6316. DebugAssert(ExistingPreset);
  6317. if (ExistingPreset)
  6318. {
  6319. HDC DC = GetDC(0);
  6320. TCanvas * Canvas = new TCanvas();
  6321. try
  6322. {
  6323. Canvas->Handle = DC;
  6324. Canvas->Font = GetToolbarFont(this);
  6325. UnicodeString Name;
  6326. if (FTransferListHoverIndex == 0)
  6327. {
  6328. Name = L"";
  6329. }
  6330. else
  6331. {
  6332. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6333. }
  6334. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6335. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6336. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6337. static_cast<TObject*>(GetComponent(fcTransferList)));
  6338. UnicodeString InfoStr =
  6339. GUIConfiguration->CopyParamPreset[Name].
  6340. GetInfoStr(L"; ",
  6341. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6342. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6343. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6344. {
  6345. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6346. {
  6347. InfoStr.SetLength(InfoStr.Length() - 1);
  6348. }
  6349. InfoStr += Ellipsis;
  6350. }
  6351. // UpdateCaption does not cause invalidation of whole submenu, while
  6352. // setting Caption property does.
  6353. // also it probably does not resize the label, even if necessary
  6354. // (we do not want that anyway)
  6355. TransferLabel->UpdateCaption(InfoStr);
  6356. }
  6357. __finally
  6358. {
  6359. Canvas->Handle = NULL;
  6360. ReleaseDC(0, DC);
  6361. delete Canvas;
  6362. }
  6363. }
  6364. }
  6365. //---------------------------------------------------------------------------
  6366. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6367. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6368. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6369. {
  6370. if (FTransferListHoverIndex != AHoverIndex)
  6371. {
  6372. FTransferListHoverIndex = AHoverIndex;
  6373. UpdateTransferLabel();
  6374. }
  6375. }
  6376. //---------------------------------------------------------------------------
  6377. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6378. {
  6379. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6380. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6381. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6382. {
  6383. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6384. {
  6385. OpenDirectory(GetSide(osCurrent));
  6386. Message.Result = 1;
  6387. }
  6388. else
  6389. {
  6390. TForm::Dispatch(&Message);
  6391. }
  6392. }
  6393. else
  6394. {
  6395. TForm::Dispatch(&Message);
  6396. }
  6397. }
  6398. //---------------------------------------------------------------------------
  6399. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6400. {
  6401. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6402. if ((FIgnoreNextDialogChar != 0) &&
  6403. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6404. {
  6405. Message.Result = 1;
  6406. }
  6407. else
  6408. {
  6409. TForm::Dispatch(&Message);
  6410. }
  6411. FIgnoreNextDialogChar = 0;
  6412. }
  6413. //---------------------------------------------------------------------------
  6414. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6415. {
  6416. // The four low-order bits are used internally by Windows
  6417. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6418. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6419. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6420. // 61730 - restore thru double click - undocumented
  6421. // 61490 - maximize thru double click - undocumented
  6422. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6423. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6424. {
  6425. SysResizing(Cmd);
  6426. }
  6427. TForm::Dispatch(&Message);
  6428. }
  6429. //---------------------------------------------------------------------------
  6430. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6431. {
  6432. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6433. // Also there no point returning TRUE as we are not able to
  6434. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6435. // Hence the process termination might be safer :)
  6436. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6437. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6438. {
  6439. Message.Result = FALSE;
  6440. }
  6441. else
  6442. {
  6443. Message.Result = TRUE;
  6444. }
  6445. // Do not call default handling as that triggers OnCloseQuery,
  6446. // where our implementation will popup confirmation dialogs, what we do not want,
  6447. // as per Vista guidelines:
  6448. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6449. }
  6450. //---------------------------------------------------------------------------
  6451. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6452. {
  6453. if (Message.EndSession && IsApplicationMinimized())
  6454. {
  6455. // WORKAROUND
  6456. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6457. // when it receives WM_ENDSESSION,
  6458. // but that sometimes (particularly when application is minimized) cause crashes.
  6459. // Crash popups message that blocks log off.
  6460. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6461. // so we call ExitProcess() immediately, not even trying to cleanup.
  6462. // It still causes beep, so there's likely some popup, but it does not block
  6463. // log off.
  6464. ExitProcess(0);
  6465. }
  6466. TForm::Dispatch(&Message);
  6467. }
  6468. #ifdef _DEBUG
  6469. //---------------------------------------------------------------------------
  6470. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6471. {
  6472. TForm::Dispatch(&Message);
  6473. }
  6474. #endif
  6475. //---------------------------------------------------------------------------
  6476. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6477. {
  6478. }
  6479. //---------------------------------------------------------------------------
  6480. void __fastcall TCustomScpExplorerForm::DoShow()
  6481. {
  6482. // only now are the controls resized finally, so the size constraints
  6483. // will not conflict with possibly very small window size
  6484. RestoreFormParams();
  6485. // This indicates that the form was rescaled after parameters were loaded and
  6486. // as most probably the parameters were actually saved with the current form scaling
  6487. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6488. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6489. // and no rounding problems will occur.
  6490. // See also RestoreForm.
  6491. // (we should be safe to call RestoreParams unconditionally)
  6492. if (PixelsPerInch != Screen->PixelsPerInch)
  6493. {
  6494. RestoreParams();
  6495. }
  6496. FixControlsPlacement();
  6497. if (Position == poDefaultPosOnly)
  6498. {
  6499. CutFormToDesktop(this);
  6500. }
  6501. TForm::DoShow();
  6502. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6503. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6504. if (Terminal == NULL)
  6505. {
  6506. StartingDisconnected();
  6507. }
  6508. UpdatePixelsPerInchMainWindowCounter();
  6509. FShowing = true;
  6510. }
  6511. //---------------------------------------------------------------------------
  6512. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6513. {
  6514. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6515. }
  6516. //---------------------------------------------------------------------------
  6517. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6518. {
  6519. DoTerminalListChanged();
  6520. }
  6521. //---------------------------------------------------------------------------
  6522. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6523. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6524. TNotifyEvent OnBalloonClick, TObject * UserData)
  6525. {
  6526. bool Do;
  6527. UnicodeString Message;
  6528. if (E == NULL)
  6529. {
  6530. Message = Str;
  6531. Do = true;
  6532. }
  6533. else
  6534. {
  6535. Do = ExceptionMessage(E, Message);
  6536. }
  6537. if (Do && WinConfiguration->BalloonNotifications)
  6538. {
  6539. UnicodeString Title;
  6540. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6541. {
  6542. Message = TrimLeft(Message);
  6543. }
  6544. else
  6545. {
  6546. Message = UnformatMessage(Message);
  6547. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6548. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6549. Title = LoadResourceString(Captions[Type]);
  6550. if (Terminal != NULL)
  6551. {
  6552. Title = FORMAT(L"%s - %s",
  6553. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6554. }
  6555. }
  6556. if (Seconds == 0)
  6557. {
  6558. Seconds = WinConfiguration->NotificationsTimeout;
  6559. }
  6560. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6561. }
  6562. }
  6563. //---------------------------------------------------------------------------
  6564. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6565. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6566. UnicodeString HelpKeyword, const TMessageParams * Params,
  6567. TTerminal * Terminal)
  6568. {
  6569. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6570. (FOnFeedSynchronizeError != NULL)) &&
  6571. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6572. {
  6573. if (FOnFeedSynchronizeError != NULL)
  6574. {
  6575. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6576. }
  6577. else
  6578. {
  6579. DebugAssert(FErrorList != NULL);
  6580. TStringList * MoreMessagesCopy = NULL;
  6581. if (MoreMessages)
  6582. {
  6583. MoreMessagesCopy = new TStringList();
  6584. MoreMessagesCopy->Assign(MoreMessages);
  6585. }
  6586. FErrorList->AddObject(Message, MoreMessagesCopy);
  6587. }
  6588. PopupTrayBalloon(Terminal, Message, Type);
  6589. return ContinueAnswer(Answers);
  6590. }
  6591. else
  6592. {
  6593. bool UseBalloon = FTrayIcon->Visible;
  6594. if (UseBalloon)
  6595. {
  6596. PopupTrayBalloon(Terminal, Message, Type);
  6597. }
  6598. unsigned int Result;
  6599. try
  6600. {
  6601. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6602. }
  6603. __finally
  6604. {
  6605. // cancel only balloon we popped up, otherwise we may cancel notification
  6606. // balloon that was there before the message dialog
  6607. // (such as "dd confirmation opt in balloon)
  6608. if (UseBalloon)
  6609. {
  6610. FTrayIcon->CancelBalloon();
  6611. }
  6612. }
  6613. return Result;
  6614. }
  6615. }
  6616. //---------------------------------------------------------------------------
  6617. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6618. TTerminal * Terminal, Exception * E)
  6619. {
  6620. if (FTrayIcon->Visible)
  6621. {
  6622. PopupTrayBalloon(Terminal, L"", qtError, E);
  6623. }
  6624. // particularly prevent opening new session from jump list,
  6625. // while exception is shown
  6626. NonVisualDataModule->StartBusy();
  6627. try
  6628. {
  6629. ShowExtendedExceptionEx(Terminal, E);
  6630. }
  6631. __finally
  6632. {
  6633. NonVisualDataModule->EndBusy();
  6634. FTrayIcon->CancelBalloon();
  6635. }
  6636. }
  6637. //---------------------------------------------------------------------------
  6638. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6639. {
  6640. if (Terminal->Active)
  6641. {
  6642. InitStatusBar();
  6643. }
  6644. // cannot rely on active page being page for active terminal,
  6645. // as it can happen that active page is the "new session" page
  6646. // (e.g. when reconnecting active terminal, while login dialog
  6647. // invoked from "new session" page is modal)
  6648. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6649. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6650. }
  6651. //---------------------------------------------------------------------------
  6652. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6653. TTerminal * Terminal, Exception * E)
  6654. {
  6655. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6656. if (!Terminal->Active)
  6657. {
  6658. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6659. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6660. {
  6661. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6662. }
  6663. }
  6664. }
  6665. //---------------------------------------------------------------------------
  6666. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6667. UnicodeString Message, TQueryType Type,
  6668. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6669. {
  6670. if ((E == NULL) ||
  6671. ExceptionMessage(E, Message))
  6672. {
  6673. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6674. if (Important)
  6675. {
  6676. Seconds *= 5;
  6677. }
  6678. UnicodeString NoteMessage(UnformatMessage(Message));
  6679. if (Terminal != NULL)
  6680. {
  6681. NoteMessage = FORMAT(L"%s: %s",
  6682. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6683. }
  6684. if (WinConfiguration->BalloonNotifications)
  6685. {
  6686. AddNote(NoteMessage);
  6687. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6688. }
  6689. else
  6690. {
  6691. FlashOnBackground();
  6692. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6693. }
  6694. }
  6695. }
  6696. //---------------------------------------------------------------------------
  6697. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6698. {
  6699. RestoreApp();
  6700. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6701. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6702. !NonVisualDataModule->Busy)
  6703. {
  6704. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6705. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6706. if (!ComponentVisible[fcQueueView])
  6707. {
  6708. ToggleQueueVisibility();
  6709. GoToQueue();
  6710. }
  6711. }
  6712. }
  6713. //---------------------------------------------------------------------------
  6714. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6715. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6716. {
  6717. UnicodeString Message;
  6718. TNotifyEvent OnClick = NULL;
  6719. TObject * UserData = NULL;
  6720. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6721. switch (Event)
  6722. {
  6723. case qeEmptyButMonitored:
  6724. if ((ATerminal != Terminal) || QueueInvisible)
  6725. {
  6726. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6727. OnClick = QueueEmptyNoteClicked;
  6728. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6729. TerminalNoteData->Terminal = ATerminal;
  6730. UserData = TerminalNoteData;
  6731. }
  6732. break;
  6733. case qeEmpty:
  6734. OperationComplete(ATerminal->QueueOperationStart);
  6735. break;
  6736. case qePendingUserAction:
  6737. if ((ATerminal != Terminal) ||
  6738. (QueueInvisible && !IsQueueAutoPopup()))
  6739. {
  6740. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6741. }
  6742. break;
  6743. default:
  6744. DebugFail();
  6745. }
  6746. if (!Message.IsEmpty())
  6747. {
  6748. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6749. }
  6750. }
  6751. //---------------------------------------------------------------------------
  6752. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6753. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6754. {
  6755. if (FDDExtMapFile != NULL)
  6756. {
  6757. CloseHandle(FDDExtMapFile);
  6758. FDDExtMapFile = NULL;
  6759. }
  6760. if (WinConfiguration->DDFakeFile)
  6761. {
  6762. DDFakeFileInitDrag(FileList, Created);
  6763. }
  6764. }
  6765. //---------------------------------------------------------------------------
  6766. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6767. {
  6768. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6769. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6770. {
  6771. FFakeFileDropTarget = FileName;
  6772. }
  6773. }
  6774. //---------------------------------------------------------------------------
  6775. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6776. {
  6777. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6778. if (!ForceDirectories(ApiPath(Result)))
  6779. {
  6780. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6781. }
  6782. FileSetAttr(ApiPath(Result), faHidden);
  6783. return Result;
  6784. }
  6785. //---------------------------------------------------------------------------
  6786. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6787. bool & Created)
  6788. {
  6789. FFakeFileDropTarget = UnicodeString();
  6790. FDragFakeDirectory = CreateFakeTransferDirectory();
  6791. FileList->AddItem(NULL, FDragFakeDirectory);
  6792. Created = true;
  6793. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6794. {
  6795. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6796. }
  6797. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6798. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6799. {
  6800. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6801. TDragExtCommStruct* CommStruct;
  6802. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6803. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6804. DebugAssert(CommStruct != NULL);
  6805. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6806. CommStruct->Dragging = true;
  6807. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6808. LENOF(CommStruct->DropDest));
  6809. NULL_TERMINATE(CommStruct->DropDest);
  6810. UnmapViewOfFile(CommStruct);
  6811. }
  6812. }
  6813. //---------------------------------------------------------------------------
  6814. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6815. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6816. {
  6817. bool Result;
  6818. DebugAssert(!IsLocalBrowserMode());
  6819. if (Sender == RemoteDirView)
  6820. {
  6821. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6822. }
  6823. else
  6824. {
  6825. DebugAssert(Sender == RemoteDriveView);
  6826. TStrings * FileList = RemoteDriveView->DragFileList();
  6827. try
  6828. {
  6829. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6830. }
  6831. __finally
  6832. {
  6833. delete FileList;
  6834. }
  6835. }
  6836. if (FDDTargetControl == RemoteDriveView)
  6837. {
  6838. RemoteDriveView->UpdateDropTarget();
  6839. }
  6840. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6841. // or from tree view to dir view.
  6842. // foMove happens when file/dir is dragged from remote tree view outside of
  6843. // application via dragex
  6844. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6845. (DropSourceControl == RemoteDriveView))
  6846. {
  6847. RemoteDriveView->UpdateDropSource();
  6848. }
  6849. return Result;
  6850. }
  6851. //---------------------------------------------------------------------------
  6852. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6853. {
  6854. DebugAssert(!IsLocalBrowserMode());
  6855. // This also handles drops of remote files to queue.
  6856. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6857. SAFE_DESTROY(FDDFileList);
  6858. if (!FFakeFileDropTarget.IsEmpty())
  6859. {
  6860. RemoveDir(ApiPath(FFakeFileDropTarget));
  6861. }
  6862. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6863. {
  6864. try
  6865. {
  6866. TDragResult DDResult = (Sender == RemoteDirView) ?
  6867. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6868. // Focus is moved to the target application,
  6869. // but as we are going to present the UI, we need to steal the focus back.
  6870. // This most likely won't work though (windows does not allow application
  6871. // to steal focus most of the time)
  6872. Application->BringToFront();
  6873. // On older version of Windows we never got drMove here, see also comment below.
  6874. // On Windows 10, we get the "move".
  6875. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6876. {
  6877. UnicodeString TargetDirectory;
  6878. TFileOperation Operation;
  6879. // drInvalid may mean drMove, see comment below
  6880. switch (DDResult)
  6881. {
  6882. case drCopy:
  6883. Operation = foCopy;
  6884. break;
  6885. case drMove:
  6886. Operation = foMove;
  6887. break;
  6888. default:
  6889. DebugFail();
  6890. case drInvalid:
  6891. // prefer "copy" for safety
  6892. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6893. break;
  6894. }
  6895. TTransferOperationParam Param;
  6896. UnicodeString CounterName;
  6897. bool ForceQueue;
  6898. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6899. {
  6900. // we get drInvalid both if d&d was intercepted by ddext,
  6901. // and when users drops on no-drop location.
  6902. // we tell the difference by existence of response from ddext,
  6903. // so we ignore absence of response in this case
  6904. if (DDResult != drInvalid)
  6905. {
  6906. // here we know that the extension is installed,
  6907. // as it is checked as soon as drag&drop starts from
  6908. // RemoteFileControlDDCreateDragFileList
  6909. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6910. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6911. {
  6912. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6913. }
  6914. else
  6915. {
  6916. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6917. }
  6918. }
  6919. }
  6920. else
  6921. {
  6922. // download using fake file
  6923. Param.Temp = false;
  6924. Param.DragDrop = true;
  6925. if (ForceQueue)
  6926. {
  6927. Param.Queue = asOn;
  6928. }
  6929. if (Sender == RemoteDirView)
  6930. {
  6931. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6932. }
  6933. if (RemoteFileControlFileOperation(Sender, Operation,
  6934. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6935. {
  6936. Configuration->Usage->Inc(CounterName);
  6937. }
  6938. }
  6939. }
  6940. }
  6941. __finally
  6942. {
  6943. CloseHandle(FDDExtMapFile);
  6944. FDDExtMapFile = NULL;
  6945. delete FDragFakeMonitors;
  6946. FDragFakeMonitors = NULL;
  6947. RemoveDir(ApiPath(FDragFakeDirectory));
  6948. FDragFakeDirectory = L"";
  6949. FFakeFileDropTarget = UnicodeString();
  6950. }
  6951. }
  6952. if (FDragDropSshTerminate.get() != NULL)
  6953. {
  6954. FDragDropSshTerminate->Rethrow();
  6955. }
  6956. }
  6957. //---------------------------------------------------------------------------
  6958. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6959. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6960. {
  6961. // Remember drop effect so we know (when user drops files), if we copy or move
  6962. FLastDropEffect = dwEffect;
  6963. }
  6964. //---------------------------------------------------------------------------
  6965. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6966. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6967. {
  6968. bool Result;
  6969. if (FDDTargetControl == QueueView3)
  6970. {
  6971. Directory = DefaultDownloadTargetDirectory();
  6972. Result = true;
  6973. CounterName = L"DownloadsDragDropQueue";
  6974. ForceQueue = true;
  6975. }
  6976. else if (!FFakeFileDropTarget.IsEmpty())
  6977. {
  6978. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6979. Result = true;
  6980. ForceQueue = false;
  6981. CounterName = L"DownloadsDragDropFakeFile";
  6982. }
  6983. else
  6984. {
  6985. ForceQueue = false;
  6986. Enabled = false;
  6987. try
  6988. {
  6989. int Timer = 0;
  6990. Result = false;
  6991. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6992. {
  6993. {
  6994. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6995. TDragExtCommStruct* CommStruct;
  6996. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6997. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6998. DebugAssert(CommStruct != NULL);
  6999. Result = !CommStruct->Dragging;
  7000. if (Result)
  7001. {
  7002. Directory = ExtractFilePath(CommStruct->DropDest);
  7003. CounterName = L"DownloadsDragDropExternalExt";
  7004. }
  7005. UnmapViewOfFile(CommStruct);
  7006. }
  7007. if (!Result)
  7008. {
  7009. Sleep(50);
  7010. Timer += 50;
  7011. Application->ProcessMessages();
  7012. }
  7013. }
  7014. }
  7015. __finally
  7016. {
  7017. Enabled = true;
  7018. }
  7019. }
  7020. return Result;
  7021. }
  7022. //---------------------------------------------------------------------------
  7023. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7024. const UnicodeString TempDir, int SecDelay)
  7025. {
  7026. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7027. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7028. if (FDelayedDeletionTimer == NULL)
  7029. {
  7030. DebugAssert(HandleAllocated());
  7031. FDelayedDeletionTimer = new TTimer(this);
  7032. FDelayedDeletionTimer->Interval = 10000;
  7033. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7034. }
  7035. else
  7036. {
  7037. FDelayedDeletionTimer->Enabled = true;
  7038. }
  7039. }
  7040. //---------------------------------------------------------------------------
  7041. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7042. {
  7043. DebugAssert(FDelayedDeletionList != NULL);
  7044. TDateTime N = Now();
  7045. TDateTime Alert;
  7046. UnicodeString Directory;
  7047. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7048. {
  7049. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7050. if ((N >= Alert) || (Sender == NULL))
  7051. {
  7052. Directory = FDelayedDeletionList->Strings[Index];
  7053. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7054. {
  7055. FDelayedDeletionList->Delete(Index);
  7056. }
  7057. }
  7058. }
  7059. if (FDelayedDeletionList->Count == 0)
  7060. {
  7061. FDelayedDeletionTimer->Enabled = false;
  7062. }
  7063. }
  7064. //---------------------------------------------------------------------------
  7065. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7066. {
  7067. DebugAssert(!IsLocalBrowserMode());
  7068. if (IsFileControl(FDDTargetControl, osRemote) ||
  7069. (FDDTargetControl == SessionsPageControl))
  7070. {
  7071. TTerminal * TargetTerminal = NULL;
  7072. TFileOperation Operation = foNone;
  7073. if (FDDTargetControl == SessionsPageControl)
  7074. {
  7075. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7076. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7077. // do not allow dropping on the "+" tab
  7078. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7079. if (TargetTerminal != NULL)
  7080. {
  7081. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7082. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7083. {
  7084. Operation = foRemoteMove;
  7085. }
  7086. else
  7087. {
  7088. Operation = foRemoteCopy;
  7089. }
  7090. }
  7091. }
  7092. else
  7093. {
  7094. if (FLastDropEffect == DROPEFFECT_MOVE)
  7095. {
  7096. Operation = foRemoteMove;
  7097. }
  7098. else if (FLastDropEffect == DROPEFFECT_COPY)
  7099. {
  7100. Operation = foRemoteCopy;
  7101. }
  7102. }
  7103. if (Operation != foNone)
  7104. {
  7105. RemoteFileControlFileOperation(DropSourceControl,
  7106. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7107. }
  7108. // abort drag&drop
  7109. Abort();
  7110. }
  7111. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7112. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7113. (FDDTargetControl != QueueView3))
  7114. {
  7115. DebugAssert(!FDragTempDir.IsEmpty());
  7116. TTransferType Type;
  7117. UnicodeString TempDir = FDragTempDir;
  7118. // We clear FDragTempDir before calling
  7119. // just in case it fail (raises exception)
  7120. FDragTempDir = L"";
  7121. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7122. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7123. // empty directory parameter means temp directory -> don't display it!
  7124. UnicodeString TargetDir = L"";
  7125. int Options = 0;
  7126. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7127. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7128. {
  7129. Abort();
  7130. }
  7131. // TargetDir is set when dropped on local file control
  7132. // (this was workaround for legacy dirview event handling, now it should be
  7133. // made prettier)
  7134. if (TargetDir.IsEmpty())
  7135. {
  7136. TargetDir = TempDir;
  7137. if (ForceDirectories(ApiPath(TargetDir)))
  7138. {
  7139. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7140. FPendingTempSpaceWarn = true;
  7141. try
  7142. {
  7143. FDragDropOperation = true;
  7144. int Params = cpTemporary |
  7145. (Type == ttMove ? cpDelete : 0);
  7146. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7147. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7148. }
  7149. __finally
  7150. {
  7151. FDragDropOperation = false;
  7152. FPendingTempSpaceWarn = false;
  7153. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7154. }
  7155. }
  7156. else
  7157. {
  7158. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7159. }
  7160. }
  7161. }
  7162. }
  7163. //---------------------------------------------------------------------------
  7164. void __fastcall TCustomScpExplorerForm::DDDownload(
  7165. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7166. {
  7167. DebugAssert(!IsLocalBrowserMode());
  7168. TAutoBatch AutoBatch(this);
  7169. UpdateCopyParamCounters(*CopyParam);
  7170. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7171. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7172. {
  7173. RemoteDriveView->UpdateDropSource();
  7174. }
  7175. }
  7176. //---------------------------------------------------------------------------
  7177. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7178. {
  7179. public:
  7180. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7181. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7182. {
  7183. }
  7184. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7185. {
  7186. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7187. TDataObjectFilesEx::AllowData(FormatEtc);
  7188. }
  7189. };
  7190. //---------------------------------------------------------------------------
  7191. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7192. TObject * Sender, TDataObject *& DataObject)
  7193. {
  7194. if (FDDExtMapFile != NULL)
  7195. {
  7196. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7197. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7198. {
  7199. Abort();
  7200. }
  7201. if (FileList->Count > 0)
  7202. {
  7203. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7204. if (!FilesObject->IsValid(true, true))
  7205. {
  7206. FilesObject->_Release();
  7207. }
  7208. else
  7209. {
  7210. DataObject = FilesObject;
  7211. }
  7212. }
  7213. }
  7214. }
  7215. //---------------------------------------------------------------------------
  7216. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7217. {
  7218. DebugFail();
  7219. }
  7220. //---------------------------------------------------------------------------
  7221. void __fastcall TCustomScpExplorerForm::GoToTree()
  7222. {
  7223. ComponentVisible[fcRemoteTree] = true;
  7224. DriveView(osRemote)->SetFocus();
  7225. }
  7226. //---------------------------------------------------------------------------
  7227. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7228. TPanelExport Export)
  7229. {
  7230. TCustomDirView * DirView = this->DirView(Side);
  7231. std::unique_ptr<TStrings> ExportData(new TStringList());
  7232. switch (Export)
  7233. {
  7234. case pePath:
  7235. ExportData->Add(DirView->PathName);
  7236. break;
  7237. case peFileList:
  7238. case peFullFileList:
  7239. {
  7240. bool FullPath = (Export == peFullFileList);
  7241. DirView->CreateFileList(false, FullPath, ExportData.get());
  7242. for (int Index = 0; Index < ExportData->Count; Index++)
  7243. {
  7244. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7245. {
  7246. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7247. }
  7248. }
  7249. }
  7250. break;
  7251. default:
  7252. DebugFail();
  7253. }
  7254. return ExportData.release();
  7255. }
  7256. //---------------------------------------------------------------------------
  7257. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7258. TPanelExport Export, TPanelExportDestination Destination)
  7259. {
  7260. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7261. PanelExportStore(Side, Export, Destination, ExportData.get());
  7262. }
  7263. //---------------------------------------------------------------------------
  7264. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7265. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7266. TStrings * ExportData)
  7267. {
  7268. if (Destination == pedClipboard)
  7269. {
  7270. TInstantOperationVisualizer Visualizer;
  7271. CopyToClipboard(ExportData);
  7272. }
  7273. else
  7274. {
  7275. DebugFail();
  7276. }
  7277. }
  7278. //---------------------------------------------------------------------------
  7279. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7280. {
  7281. TCustomDirView * DirView = this->DirView(Side);
  7282. UnicodeString Mask = DirView->Mask;
  7283. if (DoFilterMaskDialog(DirView, Mask))
  7284. {
  7285. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7286. Configuration->Usage->Inc(L"Filters");
  7287. }
  7288. }
  7289. //---------------------------------------------------------------------------
  7290. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7291. {
  7292. return FQueueController->DefaultOperation();
  7293. }
  7294. //---------------------------------------------------------------------------
  7295. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7296. TQueueOperation Operation, void ** Param)
  7297. {
  7298. switch (Operation)
  7299. {
  7300. case qoPreferences:
  7301. return true;
  7302. case qoGoTo:
  7303. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7304. case qoOnceEmpty:
  7305. return IsAnythingQueued();
  7306. default:
  7307. return FQueueController->AllowOperation(Operation, Param);
  7308. }
  7309. }
  7310. //---------------------------------------------------------------------------
  7311. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7312. {
  7313. if (DebugAlwaysTrue(QueueView3->Visible))
  7314. {
  7315. QueueView3->SetFocus();
  7316. }
  7317. }
  7318. //---------------------------------------------------------------------------
  7319. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7320. TQueueOperation Operation, void * Param)
  7321. {
  7322. if (Operation == qoGoTo)
  7323. {
  7324. GoToQueue();
  7325. }
  7326. else if (Operation == qoPreferences)
  7327. {
  7328. PreferencesDialog(pmQueue);
  7329. }
  7330. else
  7331. {
  7332. FQueueController->ExecuteOperation(Operation, Param);
  7333. }
  7334. }
  7335. //---------------------------------------------------------------------------
  7336. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7337. {
  7338. return (Queue != NULL) && Queue->Enabled;
  7339. }
  7340. //---------------------------------------------------------------------------
  7341. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7342. {
  7343. DebugAssert(Queue != NULL);
  7344. if (Queue != NULL)
  7345. {
  7346. Queue->Enabled = !Queue->Enabled;
  7347. }
  7348. }
  7349. //---------------------------------------------------------------------------
  7350. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7351. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7352. {
  7353. FQueueActedItem = QueueView3->ItemFocused;
  7354. }
  7355. //---------------------------------------------------------------------------
  7356. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7357. {
  7358. return TopDock->Height + QueueSplitter->Height;
  7359. }
  7360. //---------------------------------------------------------------------------
  7361. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7362. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7363. {
  7364. // when queue is hidden by double-clicking splitter, stray attempt to
  7365. // resize the panel with strange value arrives, make sure it is ignored
  7366. if (ComponentVisible[fcQueueView])
  7367. {
  7368. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7369. RemotePanel->Constraints->MinHeight;
  7370. if (NewSize > HeightLimit)
  7371. {
  7372. NewSize = HeightLimit;
  7373. }
  7374. }
  7375. else
  7376. {
  7377. Accept = false;
  7378. }
  7379. }
  7380. //---------------------------------------------------------------------------
  7381. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7382. TDragObject *& /*DragObject*/)
  7383. {
  7384. FQueueActedItem = QueueView3->ItemFocused;
  7385. }
  7386. //---------------------------------------------------------------------------
  7387. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7388. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7389. {
  7390. Accept = true;
  7391. if (Source == QueueView3)
  7392. {
  7393. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7394. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7395. if (Accept)
  7396. {
  7397. TQueueItemProxy * QueueItem;
  7398. TQueueItemProxy * DestQueueItem;
  7399. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7400. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7401. Accept = (QueueItem != DestQueueItem) &&
  7402. (QueueItem->Status == TQueueItem::qsPending) &&
  7403. (DestQueueItem->Status == TQueueItem::qsPending);
  7404. }
  7405. }
  7406. }
  7407. //---------------------------------------------------------------------------
  7408. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7409. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7410. {
  7411. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7412. {
  7413. TQueueItemProxy * QueueItem;
  7414. TQueueItemProxy * DestQueueItem;
  7415. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7416. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7417. QueueItem->Move(DestQueueItem);
  7418. }
  7419. }
  7420. //---------------------------------------------------------------------------
  7421. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7422. {
  7423. if ((QueueView3->ItemFocused == NULL) &&
  7424. (QueueView3->Items->Count > 0))
  7425. {
  7426. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7427. }
  7428. QueueLabelUpdateStatus();
  7429. }
  7430. //---------------------------------------------------------------------------
  7431. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7432. {
  7433. QueueLabelUpdateStatus();
  7434. }
  7435. //---------------------------------------------------------------------------
  7436. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7437. {
  7438. QueueLabel->UpdateStatus();
  7439. }
  7440. //---------------------------------------------------------------------------
  7441. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7442. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7443. {
  7444. if (Selected)
  7445. {
  7446. NonVisualDataModule->UpdateNonVisibleActions();
  7447. }
  7448. }
  7449. //---------------------------------------------------------------------------
  7450. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7451. {
  7452. TDragDropFilesEx * Result = NULL;
  7453. if (Sender == SessionsPageControl)
  7454. {
  7455. Result = FSessionsDragDropFilesEx;
  7456. }
  7457. else if (Sender == QueueView3)
  7458. {
  7459. Result = FQueueDragDropFilesEx;
  7460. }
  7461. else
  7462. {
  7463. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7464. if (DirView != NULL)
  7465. {
  7466. Result = DirView->DragDropFilesEx;
  7467. }
  7468. else
  7469. {
  7470. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7471. if (DriveView != NULL)
  7472. {
  7473. Result = DriveView->DragDropFilesEx;
  7474. }
  7475. }
  7476. }
  7477. DebugAssert(Result != NULL);
  7478. return Result;
  7479. }
  7480. //---------------------------------------------------------------------------
  7481. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7482. {
  7483. return (DView->SelCount == DView->FilesCount);
  7484. }
  7485. //---------------------------------------------------------------------------
  7486. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7487. {
  7488. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7489. }
  7490. //---------------------------------------------------------------------------
  7491. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7492. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7493. {
  7494. DebugAssert(!IsLocalBrowserMode());
  7495. TFileOperation Operation;
  7496. switch (Effect)
  7497. {
  7498. case DROPEFFECT_MOVE:
  7499. Operation = foMove;
  7500. break;
  7501. case DROPEFFECT_COPY:
  7502. // occures on WinXP (reported by user)
  7503. default:
  7504. Operation = foCopy;
  7505. break;
  7506. };
  7507. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7508. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7509. if (DragDropFilesEx->FileList->Count > 0)
  7510. {
  7511. TStrings * FileList = new TStringList();
  7512. try
  7513. {
  7514. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7515. {
  7516. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7517. }
  7518. FDragDropOperation = true;
  7519. TTransferOperationParam Param;
  7520. Param.TargetDirectory = TargetPath;
  7521. // upload, no temp dirs
  7522. Param.Temp = false;
  7523. Param.DragDrop = DragDrop;
  7524. Param.Options =
  7525. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7526. if (ForceQueue)
  7527. {
  7528. Param.Queue = asOn;
  7529. }
  7530. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7531. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7532. {
  7533. if (IsFileControl(DropSourceControl, osLocal))
  7534. {
  7535. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7536. }
  7537. else
  7538. {
  7539. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7540. }
  7541. }
  7542. }
  7543. __finally
  7544. {
  7545. FDragDropOperation = false;
  7546. delete FileList;
  7547. }
  7548. }
  7549. }
  7550. //---------------------------------------------------------------------------
  7551. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7552. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7553. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7554. {
  7555. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7556. }
  7557. //---------------------------------------------------------------------------
  7558. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7559. TObject * Sender, int grfKeyState, int & dwEffect)
  7560. {
  7561. DebugAssert(!IsLocalBrowserMode());
  7562. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7563. if ((dwEffect != DROPEFFECT_NONE) &&
  7564. IsFileControl(DropSourceControl, osRemote))
  7565. {
  7566. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7567. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7568. (RemoteDirView->DropTarget == NULL))
  7569. {
  7570. dwEffect = DROPEFFECT_NONE;
  7571. }
  7572. else
  7573. {
  7574. if (dwEffect == DROPEFFECT_COPY)
  7575. {
  7576. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7577. // currently we support copying always (at least via temporary directory);
  7578. // remove associated checks once this all proves stable and working
  7579. bool CopyCapable = true;
  7580. // if we do not support neither of operations, there's no discussion
  7581. if (!MoveCapable && !CopyCapable)
  7582. {
  7583. dwEffect = DROPEFFECT_NONE;
  7584. }
  7585. else
  7586. {
  7587. // The default effect inside remote panel is move,
  7588. // unless we do not support it, but support copy
  7589. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7590. {
  7591. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7592. }
  7593. else
  7594. {
  7595. // with ctrl-down, we want copy unless it is not supported
  7596. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7597. }
  7598. }
  7599. }
  7600. }
  7601. }
  7602. }
  7603. //---------------------------------------------------------------------------
  7604. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7605. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7606. {
  7607. DebugAssert(!IsLocalBrowserMode());
  7608. if (FDDTotalSize >= 0)
  7609. {
  7610. if (File->IsDirectory)
  7611. {
  7612. FDDTotalSize = -1;
  7613. }
  7614. else
  7615. {
  7616. FDDTotalSize += File->Size;
  7617. }
  7618. }
  7619. DebugAssert(!FDragTempDir.IsEmpty());
  7620. // TODO: this is quite ineffective
  7621. // TODO: what if invalid character replacement is disabled?
  7622. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7623. UnicodeString TransferFileName;
  7624. if (Sender == RemoteDriveView)
  7625. {
  7626. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7627. }
  7628. else
  7629. {
  7630. TransferFileName = File->FileName;
  7631. }
  7632. FDDFileList->AddObject(TransferFileName, File);
  7633. }
  7634. //---------------------------------------------------------------------------
  7635. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7636. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7637. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7638. {
  7639. // sometimes we do not get DDEnd so the list is not released
  7640. SAFE_DESTROY(FDDFileList);
  7641. FDDFileList = new TStringList();
  7642. FDragTempDir = WinConfiguration->TemporaryDir();
  7643. FDDTotalSize = 0;
  7644. FDragDropSshTerminate.reset(NULL);
  7645. }
  7646. //---------------------------------------------------------------------------
  7647. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7648. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7649. HRESULT & Result)
  7650. {
  7651. if (Result == DRAGDROP_S_DROP)
  7652. {
  7653. try
  7654. {
  7655. GlobalDragImageList->HideDragImage();
  7656. try
  7657. {
  7658. RemoteFileControlDDTargetDrop();
  7659. }
  7660. catch(ESshTerminate & E)
  7661. {
  7662. FDragDropSshTerminate.reset(E.Clone());
  7663. }
  7664. }
  7665. catch (Exception &E)
  7666. {
  7667. // If downloading fails we need to cancel drag&drop, otherwise
  7668. // Explorer shows error
  7669. // But by the way exception probably never reach this point as
  7670. // it's catched on way.
  7671. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7672. Result = DRAGDROP_S_CANCEL;
  7673. ShowExtendedException(Terminal, &E);
  7674. }
  7675. }
  7676. }
  7677. //---------------------------------------------------------------------------
  7678. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7679. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7680. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7681. {
  7682. TFileMasks::TParams MaskParams;
  7683. MaskParams.Size = Size;
  7684. MaskParams.Modification = Modification;
  7685. // this does not re-parse the mask if it is the same as the last time
  7686. FDirViewMatchMask = Masks;
  7687. bool ImplicitMatch;
  7688. Matches =
  7689. // RecurseInclude parameter has no effect as the Path is empty
  7690. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7691. (AllowImplicitMatches || !ImplicitMatch);
  7692. }
  7693. //---------------------------------------------------------------------------
  7694. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7695. TObject * Sender, TListItem * Item, WORD & Indexes)
  7696. {
  7697. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7698. UnicodeString Ext;
  7699. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7700. {
  7701. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7702. }
  7703. else
  7704. {
  7705. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7706. }
  7707. if (SameText(Ext, Configuration->PartialExt))
  7708. {
  7709. Indexes |= oiPartial;
  7710. }
  7711. }
  7712. //---------------------------------------------------------------------------
  7713. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7714. {
  7715. return
  7716. DirViewEnabled(osCurrent) &&
  7717. DirView(osCurrent)->CanPasteFromClipBoard();
  7718. }
  7719. //---------------------------------------------------------------------------
  7720. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7721. {
  7722. bool Result = false;
  7723. if (CanPasteToDirViewFromClipBoard())
  7724. {
  7725. Result = true;
  7726. }
  7727. else
  7728. {
  7729. UnicodeString ClipboardText;
  7730. if (NonEmptyTextFromClipboard(ClipboardText))
  7731. {
  7732. if (StoredSessions->IsUrl(ClipboardText))
  7733. {
  7734. Result = true;
  7735. }
  7736. else
  7737. {
  7738. Result = DirViewEnabled(osCurrent);
  7739. }
  7740. }
  7741. }
  7742. return Result;
  7743. }
  7744. //---------------------------------------------------------------------------
  7745. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7746. {
  7747. if (DoesClipboardContainOurFiles())
  7748. {
  7749. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7750. {
  7751. DebugAssert(!IsLocalBrowserMode());
  7752. TTerminalManager * Manager = TTerminalManager::Instance();
  7753. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7754. Manager->ActiveTerminal = FClipboardTerminal;
  7755. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7756. }
  7757. }
  7758. else if (CanPasteToDirViewFromClipBoard())
  7759. {
  7760. DirView(osCurrent)->PasteFromClipBoard();
  7761. }
  7762. else
  7763. {
  7764. UnicodeString ClipboardText;
  7765. if (NonEmptyTextFromClipboard(ClipboardText))
  7766. {
  7767. if (StoredSessions->IsUrl(ClipboardText))
  7768. {
  7769. NewSession(false, ClipboardText);
  7770. }
  7771. else
  7772. {
  7773. DirView(osCurrent)->Path = ClipboardText;
  7774. }
  7775. }
  7776. }
  7777. }
  7778. //---------------------------------------------------------------------------
  7779. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7780. {
  7781. // TBD
  7782. }
  7783. //---------------------------------------------------------------------------
  7784. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7785. {
  7786. TCustomDirView * ADirView = DirView(Side);
  7787. ADirView->SelectAll(Mode);
  7788. ADirView->SetFocus();
  7789. }
  7790. //---------------------------------------------------------------------------
  7791. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7792. {
  7793. TCustomDirView * ADirView = DirView(Side);
  7794. TFileFilter Filter;
  7795. DefaultFileFilter(Filter);
  7796. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7797. {
  7798. Configuration->Usage->Inc(L"MaskSelections");
  7799. ADirView->SelectFiles(Filter, Select);
  7800. ADirView->SetFocus();
  7801. }
  7802. }
  7803. //---------------------------------------------------------------------------
  7804. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7805. {
  7806. TCustomDirView * ADirView = DirView(Side);
  7807. ADirView->RestoreSelectedNames();
  7808. ADirView->SetFocus();
  7809. }
  7810. //---------------------------------------------------------------------------
  7811. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7812. {
  7813. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7814. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7815. {
  7816. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7817. UnicodeString Ext;
  7818. if (!IsSideLocalBrowser(osCurrent))
  7819. {
  7820. Ext = UnixExtractFileExt(FileName);
  7821. }
  7822. else
  7823. {
  7824. Ext = ExtractFileExt(FileName);
  7825. }
  7826. if (Ext.IsEmpty())
  7827. {
  7828. Ext = L".";
  7829. }
  7830. TFileFilter Filter;
  7831. Filter.Masks = FORMAT(L"*%s", (Ext));
  7832. Filter.Directories = false;
  7833. CurrentDirView->SelectFiles(Filter, Select);
  7834. }
  7835. }
  7836. //---------------------------------------------------------------------------
  7837. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7838. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7839. {
  7840. UnicodeString Result;
  7841. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7842. {
  7843. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7844. }
  7845. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7846. {
  7847. // noop
  7848. }
  7849. else
  7850. {
  7851. Result =
  7852. FMTLOAD(FILE_INFO_FORMAT,
  7853. (FormatBytes(FileInfo.SelectedSize),
  7854. FormatBytes(FileInfo.FilesSize),
  7855. FormatNumber(FileInfo.SelectedCount),
  7856. FormatNumber(FileInfo.FilesCount)));
  7857. }
  7858. return Result;
  7859. }
  7860. //---------------------------------------------------------------------------
  7861. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7862. TTBXStatusPanel * Panel, TOperationSide Side)
  7863. {
  7864. if (Panel->Index == 1)
  7865. {
  7866. ToggleShowHiddenFiles();
  7867. }
  7868. else if (Panel->Index == 2)
  7869. {
  7870. Filter(Side);
  7871. }
  7872. }
  7873. //---------------------------------------------------------------------------
  7874. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7875. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7876. {
  7877. DebugAssert(!StatusBar->SimplePanel);
  7878. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7879. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7880. }
  7881. //---------------------------------------------------------------------------
  7882. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7883. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7884. {
  7885. DebugAssert(StatusBar->Panels->Count >= 3);
  7886. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7887. if (FileInfo.HiddenCount > 0)
  7888. {
  7889. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7890. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7891. }
  7892. else
  7893. {
  7894. HiddenFilesPanel->ViewPriority = 0;
  7895. // not really necessary, just to cleanup no-longer-valid data
  7896. HiddenFilesPanel->Caption = L"";
  7897. }
  7898. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7899. if (FileInfo.FilteredCount > 0)
  7900. {
  7901. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7902. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7903. }
  7904. else
  7905. {
  7906. FilteredFilesPanel->ViewPriority = 0;
  7907. // not really necessary, just to cleanup no-longer-valid data
  7908. FilteredFilesPanel->Caption = L"";
  7909. }
  7910. }
  7911. //---------------------------------------------------------------------------
  7912. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7913. TObject * /*Sender*/)
  7914. {
  7915. if (DirView(osRemote)->Enabled)
  7916. {
  7917. DirView(osRemote)->SetFocus();
  7918. }
  7919. }
  7920. //---------------------------------------------------------------------------
  7921. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7922. {
  7923. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7924. switch (Config.Show)
  7925. {
  7926. case qvShow:
  7927. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7928. {
  7929. Config.Show = qvHide;
  7930. }
  7931. else
  7932. {
  7933. Config.Show = Config.LastHideShow;
  7934. }
  7935. break;
  7936. case qvHideWhenEmpty:
  7937. if (ComponentVisible[fcQueueView])
  7938. {
  7939. Config.Show = qvHide;
  7940. }
  7941. else
  7942. {
  7943. Config.LastHideShow = Config.Show;
  7944. Config.Show = qvShow;
  7945. }
  7946. break;
  7947. case qvHide:
  7948. Config.LastHideShow = Config.Show;
  7949. Config.Show = qvShow;
  7950. break;
  7951. }
  7952. WinConfiguration->QueueView = Config;
  7953. }
  7954. //---------------------------------------------------------------------------
  7955. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7956. {
  7957. UnicodeString Result;
  7958. if (FTerminal != NULL)
  7959. {
  7960. switch (WinConfiguration->PathInCaption)
  7961. {
  7962. case picShort:
  7963. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7964. break;
  7965. case picFull:
  7966. Result = FTerminal->CurrentDirectory;
  7967. break;
  7968. }
  7969. }
  7970. return Result;
  7971. }
  7972. //---------------------------------------------------------------------------
  7973. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7974. {
  7975. TColor Result = GetWindowColor(FSessionColor);
  7976. return Result;
  7977. }
  7978. //---------------------------------------------------------------------------
  7979. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7980. {
  7981. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7982. return Result;
  7983. }
  7984. //---------------------------------------------------------------------------
  7985. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7986. {
  7987. TTerminalManager::Instance()->UpdateAppTitle();
  7988. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7989. // background to gray on Windows 7 (works on Windows 10).
  7990. // See also EnableControl
  7991. if (Showing)
  7992. {
  7993. if (Terminal != NULL)
  7994. {
  7995. // Update path when it changes
  7996. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7997. {
  7998. UpdateSessionTab(SessionsPageControl->ActivePage);
  7999. }
  8000. }
  8001. NonVisualDataModule->ReconnectSessionAction->Update();
  8002. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8003. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8004. CenterReconnectToolbar();
  8005. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  8006. if (HasTerminal)
  8007. {
  8008. // TODO needed yet with second local browser?
  8009. if (!RemoteDirView->Enabled)
  8010. {
  8011. RemoteDirView->Enabled = true;
  8012. if (FRemoteDirViewWasFocused)
  8013. {
  8014. ActiveControl = RemoteDirView;
  8015. }
  8016. }
  8017. RemoteDirView->Color = PanelColor();
  8018. }
  8019. else
  8020. {
  8021. if (RemoteDirView->Enabled)
  8022. {
  8023. // This is first called when the form is being constructed
  8024. // (not anymore due to Showing test above)
  8025. // but the false is overriden in the constructor later.
  8026. // An even later in TScpCommanderForm::DoShow()
  8027. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8028. RemoteDirView->Enabled = false;
  8029. }
  8030. RemoteDirView->Color = DisabledPanelColor();
  8031. }
  8032. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8033. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8034. RemoteDriveView->Color = RemoteDirView->Color;
  8035. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8036. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8037. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8038. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8039. QueueLabelUpdateStatus();
  8040. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8041. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8042. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8043. }
  8044. }
  8045. //---------------------------------------------------------------------------
  8046. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8047. {
  8048. UpdateControls();
  8049. }
  8050. //---------------------------------------------------------------------------
  8051. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8052. TObject * Sender)
  8053. {
  8054. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8055. DebugAssert(DirView != NULL);
  8056. DoDirViewLoaded(DirView);
  8057. TransferPresetAutoSelect();
  8058. }
  8059. //---------------------------------------------------------------------------
  8060. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8061. {
  8062. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8063. // first run after installation
  8064. if (double(Updates.LastCheck) == 0)
  8065. {
  8066. // make sure next time there will be an update (if enabled)
  8067. Updates.LastCheck = TDateTime(1);
  8068. WinConfiguration->Updates = Updates;
  8069. }
  8070. else if ((double(Updates.Period) > 0) &&
  8071. (Now() - Updates.LastCheck >= Updates.Period))
  8072. {
  8073. TThreadMethod OnUpdatesChecked = NULL;
  8074. if (!IsUWP())
  8075. {
  8076. OnUpdatesChecked = UpdatesChecked;
  8077. }
  8078. StartUpdateThread(OnUpdatesChecked);
  8079. }
  8080. }
  8081. //---------------------------------------------------------------------------
  8082. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8083. {
  8084. UnicodeString Message;
  8085. bool New;
  8086. TQueryType Type;
  8087. GetUpdatesMessage(Message, New, Type, false);
  8088. if (!Message.IsEmpty())
  8089. {
  8090. if (New)
  8091. {
  8092. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8093. }
  8094. if (!New && (Type != qtWarning))
  8095. {
  8096. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8097. }
  8098. else
  8099. {
  8100. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8101. }
  8102. if (New)
  8103. {
  8104. Configuration->Usage->Inc(L"UpdateNotifications");
  8105. }
  8106. }
  8107. }
  8108. //---------------------------------------------------------------------------
  8109. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8110. {
  8111. RestoreApp();
  8112. bool CanDisplay = !NonVisualDataModule->Busy;
  8113. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8114. {
  8115. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8116. }
  8117. if (CanDisplay)
  8118. {
  8119. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8120. CheckForUpdates(true);
  8121. }
  8122. else
  8123. {
  8124. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8125. }
  8126. }
  8127. //---------------------------------------------------------------------------
  8128. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8129. TCopyParamRuleData & Data)
  8130. {
  8131. DebugAssert(Terminal != NULL);
  8132. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8133. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8134. Data.RemoteDirectory = RemoteDirView->PathName;
  8135. }
  8136. //---------------------------------------------------------------------------
  8137. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8138. {
  8139. // Terminal can be null when we are changing local directory implicitly,
  8140. // because it has been deleted, while no session is connected
  8141. // (Login dialog is open)
  8142. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8143. {
  8144. DebugAssert(!IsLocalBrowserMode());
  8145. TCopyParamRuleData Data;
  8146. GetTransferPresetAutoSelectData(Data);
  8147. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8148. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8149. if (CopyParamIndex < 0)
  8150. {
  8151. // there is no preset that matches autoselection
  8152. // set preset that we consider "default"
  8153. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8154. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8155. }
  8156. else
  8157. {
  8158. // there is preset matching autoselection
  8159. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8160. if (CopyParamName == FCopyParamAutoSelected)
  8161. {
  8162. // autoselected the same preset as the last time
  8163. // make no change (i.e. preserve custom user preset, if any)
  8164. }
  8165. else
  8166. {
  8167. // autoselected the different preset then the last time (or there
  8168. // was default preset set)
  8169. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8170. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8171. }
  8172. }
  8173. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8174. {
  8175. if (CopyParamIndex >= 0)
  8176. {
  8177. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8178. }
  8179. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8180. try
  8181. {
  8182. int Fmt =
  8183. (CopyParamIndex < 0) ?
  8184. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8185. COPY_PARAM_AUTOSELECTED;
  8186. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8187. Data->Message = MainInstructions(Message);
  8188. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8189. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8190. CopyParamAttrs);
  8191. if (CopyParamIndex >= 0)
  8192. {
  8193. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8194. Info = FORMAT(L"%s\n \n%s", (Info,
  8195. FMTLOAD(COPY_PARAM_RULE,
  8196. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8197. }
  8198. Data->Message += L"\n\n" + Info;
  8199. if (WinConfiguration->CopyParamAutoSelectNotice)
  8200. {
  8201. TransferPresetNoteMessage(Data, true);
  8202. }
  8203. else
  8204. {
  8205. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8206. Data = NULL; // ownership passed
  8207. }
  8208. }
  8209. __finally
  8210. {
  8211. delete Data;
  8212. }
  8213. }
  8214. }
  8215. }
  8216. //---------------------------------------------------------------------------
  8217. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8218. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8219. {
  8220. DebugAssert(NoteData != NULL);
  8221. DebugAssert(!IsLocalBrowserMode());
  8222. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8223. TQueryButtonAlias Aliases[1];
  8224. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8225. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8226. Params.Aliases = Aliases;
  8227. Params.AliasesCount = LENOF(Aliases);
  8228. unsigned int Result =
  8229. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8230. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8231. switch (Result)
  8232. {
  8233. case qaNeverAskAgain:
  8234. DebugAssert(AllowNeverAskAgain);
  8235. WinConfiguration->CopyParamAutoSelectNotice = false;
  8236. break;
  8237. case qaIgnore:
  8238. PreferencesDialog(pmPresets);
  8239. break;
  8240. }
  8241. }
  8242. //---------------------------------------------------------------------------
  8243. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8244. {
  8245. // as of now useless, as this is used for notes only, never for balloons, ...
  8246. RestoreApp();
  8247. // .. and we should never be busy here
  8248. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8249. {
  8250. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8251. }
  8252. }
  8253. //---------------------------------------------------------------------------
  8254. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8255. TPreferencesMode APreferencesMode)
  8256. {
  8257. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8258. TCopyParamRuleData Data;
  8259. if (Terminal != NULL)
  8260. {
  8261. PreferencesData.reset(new TPreferencesDialogData());
  8262. GetTransferPresetAutoSelectData(Data);
  8263. PreferencesData->CopyParamRuleData = &Data;
  8264. }
  8265. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8266. }
  8267. //---------------------------------------------------------------------------
  8268. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8269. const TCustomCommandType & Command)
  8270. {
  8271. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8272. {
  8273. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8274. }
  8275. }
  8276. //---------------------------------------------------------------------------
  8277. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8278. {
  8279. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8280. TCustomCommandType Command;
  8281. // make sure we use local custom command when remote are not supported
  8282. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8283. {
  8284. Command = FLastCustomCommand;
  8285. }
  8286. else
  8287. {
  8288. Command.Params = Command.Params | ccLocal;
  8289. }
  8290. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8291. FEditingFocusedAdHocCommand = OnFocused;
  8292. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8293. int Options =
  8294. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8295. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8296. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8297. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8298. {
  8299. FLastCustomCommand = Command;
  8300. UpdateCustomCommandsToolbar();
  8301. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8302. }
  8303. }
  8304. //---------------------------------------------------------------------------
  8305. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8306. {
  8307. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8308. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8309. DebugAssert(State > 0);
  8310. if (State <= 0)
  8311. {
  8312. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8313. }
  8314. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8315. }
  8316. //---------------------------------------------------------------------------
  8317. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8318. TCustomCommandType & Command, int & State)
  8319. {
  8320. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8321. if (Result)
  8322. {
  8323. Command = FLastCustomCommand;
  8324. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8325. }
  8326. return Result;
  8327. }
  8328. //---------------------------------------------------------------------------
  8329. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8330. {
  8331. ResetIncrementalSearch();
  8332. }
  8333. //---------------------------------------------------------------------------
  8334. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8335. {
  8336. DebugAssert(ComponentVisible[Component]);
  8337. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8338. }
  8339. //---------------------------------------------------------------------------
  8340. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8341. {
  8342. // when queue panel is resized here directly, the status bar is stretched
  8343. // over whole space the panel occupied
  8344. PostComponentHide(fcQueueView);
  8345. }
  8346. //---------------------------------------------------------------------------
  8347. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8348. {
  8349. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8350. RefreshColorMode();
  8351. WinConfiguration->ResetSysDarkTheme();
  8352. ConfigurationChanged();
  8353. ConfigureInterface();
  8354. // Should be called for all controls
  8355. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8356. }
  8357. //---------------------------------------------------------------------------
  8358. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8359. {
  8360. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8361. if (!FInvalid &&
  8362. (Message.LParam != 0) &&
  8363. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8364. {
  8365. ThemeChanged();
  8366. }
  8367. TForm::Dispatch(&Message);
  8368. }
  8369. //---------------------------------------------------------------------------
  8370. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8371. {
  8372. TMessage * M = static_cast<TMessage*>(Message);
  8373. switch (M->Msg)
  8374. {
  8375. case CM_DIALOGCHAR:
  8376. CMDialogChar(*M);
  8377. break;
  8378. case WM_APPCOMMAND:
  8379. WMAppCommand(*M);
  8380. break;
  8381. case WM_SYSCOMMAND:
  8382. WMSysCommand(*M);
  8383. break;
  8384. case WM_QUERYENDSESSION:
  8385. WMQueryEndSession(*M);
  8386. break;
  8387. case WM_ENDSESSION:
  8388. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8389. break;
  8390. #ifdef _DEBUG
  8391. case WM_WINDOWPOSCHANGED:
  8392. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8393. break;
  8394. case CM_CUSTOMSTYLECHANGED:
  8395. TForm::Dispatch(Message);
  8396. break;
  8397. #endif
  8398. case WM_COMPONENT_HIDE:
  8399. {
  8400. Byte Component = static_cast<Byte>(M->WParam);
  8401. // sanity check
  8402. if (ComponentVisible[Component])
  8403. {
  8404. // special treatment
  8405. if (Component == fcQueueView)
  8406. {
  8407. ToggleQueueVisibility();
  8408. DebugAssert(!ComponentVisible[fcQueueView]);
  8409. }
  8410. else
  8411. {
  8412. ComponentVisible[Component] = false;
  8413. }
  8414. }
  8415. }
  8416. break;
  8417. case WM_COPYDATA:
  8418. WMCopyData(*M);
  8419. break;
  8420. case WM_MANAGES_CAPTION:
  8421. // caption managed in TTerminalManager::UpdateAppTitle()
  8422. M->Result = 1;
  8423. break;
  8424. case WM_WANTS_MOUSEWHEEL:
  8425. M->Result = 1;
  8426. break;
  8427. case CM_SHOWINGCHANGED:
  8428. CMShowingChanged(*M);
  8429. break;
  8430. case WM_CLOSE:
  8431. WMClose(*M);
  8432. break;
  8433. case WM_DPICHANGED:
  8434. WMDpiChanged(*M);
  8435. break;
  8436. case CM_DPICHANGED:
  8437. CMDpiChanged(*M);
  8438. break;
  8439. case WM_SETTINGCHANGE:
  8440. WMSettingChange(*M);
  8441. break;
  8442. case CM_DIALOGKEY:
  8443. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8444. break;
  8445. default:
  8446. TForm::Dispatch(Message);
  8447. break;
  8448. }
  8449. }
  8450. //---------------------------------------------------------------------------
  8451. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8452. {
  8453. // noop
  8454. }
  8455. //---------------------------------------------------------------------------
  8456. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8457. {
  8458. TForm::Dispatch(&Message);
  8459. FSessionColors->Clear();
  8460. AddFixedSessionImages();
  8461. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8462. UpdateImages();
  8463. }
  8464. //---------------------------------------------------------------------------
  8465. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8466. {
  8467. TForm::Dispatch(&Message);
  8468. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8469. UpdatePixelsPerInchMainWindowCounter();
  8470. }
  8471. //---------------------------------------------------------------------------
  8472. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8473. {
  8474. // Cannot close window while we are busy.
  8475. // We cannot test this in FormCloseQuery as that is called also from
  8476. // Close(), which is called by CloseApplicationAction. So we can be busy
  8477. // there already even, when it is legitimate to close the application.
  8478. // Possibly a better place to handle this would be WMSysCommand.
  8479. if (NonVisualDataModule->Busy)
  8480. {
  8481. Message.Result = 1;
  8482. }
  8483. else
  8484. {
  8485. TForm::Dispatch(&Message);
  8486. }
  8487. }
  8488. //---------------------------------------------------------------------------
  8489. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8490. {
  8491. TForm::Dispatch(&Message);
  8492. if (Showing && (Terminal == NULL))
  8493. {
  8494. // When we are starting minimized (i.e. from an installer),
  8495. // postpone showing Login dialog until we get restored.
  8496. // Otherwise the Login dialog (and Authentication window) show restored
  8497. // over invisible (minimized) main window.
  8498. if (WindowState == wsMinimized)
  8499. {
  8500. FNeedSession = true;
  8501. }
  8502. else
  8503. {
  8504. // This happens before application ever goes idle, so the toolbars would
  8505. // stay enabled (initial state) until the Login dialog is dismissed.
  8506. UpdateActions();
  8507. NonVisualDataModule->StartBusy();
  8508. try
  8509. {
  8510. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8511. // otherwise the dialog does not receive focus.
  8512. // With Commander interface the ProcessMessages is called already
  8513. // by TDriveView, but with Explorer interface, we need to call it explicily
  8514. Application->ProcessMessages();
  8515. // do not reload sessions, they have been loaded just now (optimization)
  8516. NeedSession(true);
  8517. }
  8518. __finally
  8519. {
  8520. NonVisualDataModule->EndBusy();
  8521. }
  8522. }
  8523. }
  8524. }
  8525. //---------------------------------------------------------------------------
  8526. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8527. {
  8528. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8529. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8530. }
  8531. //---------------------------------------------------------------------------
  8532. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8533. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8534. int & MaxHeight)
  8535. {
  8536. // workaround for bug in TWinControl.CalcConstraints
  8537. // Check for empty rect (restore from iconic state) is done there only after
  8538. // call to AdjustClientRect, which enlarges the rect (for forms).
  8539. TRect R = GetClientRect();
  8540. // when restoring from iconic state, place no restrictions
  8541. if (IsRectEmpty(R))
  8542. {
  8543. MinWidth = 0;
  8544. MinHeight = 0;
  8545. MaxWidth = 32000;
  8546. MaxHeight = 32000;
  8547. }
  8548. }
  8549. //---------------------------------------------------------------------------
  8550. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8551. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8552. {
  8553. // terminal can be already closed (e.g. dropped connection)
  8554. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8555. {
  8556. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8557. try
  8558. {
  8559. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8560. }
  8561. catch(...)
  8562. {
  8563. if (!Terminal->Active)
  8564. {
  8565. Close = true;
  8566. }
  8567. throw;
  8568. }
  8569. }
  8570. }
  8571. //---------------------------------------------------------------------------
  8572. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8573. {
  8574. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8575. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8576. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8577. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8578. {
  8579. OnGetSpaceAvailable = GetSpaceAvailable;
  8580. }
  8581. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8582. OnGetSpaceAvailable);
  8583. }
  8584. //---------------------------------------------------------------------------
  8585. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8586. {
  8587. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8588. Terminal->FillSessionDataForCode(Data.get());
  8589. return Data.release();
  8590. }
  8591. //---------------------------------------------------------------------------
  8592. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8593. {
  8594. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8595. DoGenerateUrlDialog(Data.get(), Paths);
  8596. }
  8597. //---------------------------------------------------------------------------
  8598. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8599. {
  8600. GenerateUrl(NULL);
  8601. }
  8602. //---------------------------------------------------------------------------
  8603. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8604. {
  8605. std::unique_ptr<TStrings> Paths(new TStringList());
  8606. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8607. GenerateUrl(Paths.get());
  8608. }
  8609. //---------------------------------------------------------------------------
  8610. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8611. {
  8612. FSessionColor = value;
  8613. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8614. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8615. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8616. DebugAssert(ColorItem != NULL);
  8617. ColorItem->Color = C;
  8618. NonVisualDataModule->ColorMenuItem->Color = C;
  8619. UpdateControls();
  8620. }
  8621. //---------------------------------------------------------------------------
  8622. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8623. {
  8624. if (value != FSessionColor)
  8625. {
  8626. UpdateSessionColor(value);
  8627. // Is null when called from LastTerminalClosed
  8628. if (Terminal != NULL)
  8629. {
  8630. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8631. }
  8632. }
  8633. }
  8634. //---------------------------------------------------------------------------
  8635. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8636. {
  8637. bool Result = FNoteTimer->Enabled;
  8638. if (Result)
  8639. {
  8640. // cannot cancel note too early
  8641. bool NotEarly =
  8642. (Now() - FNoteShown >
  8643. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8644. if (Force || NotEarly)
  8645. {
  8646. FNoteTimer->Enabled = false;
  8647. FNote = L"";
  8648. SAFE_DESTROY(FNoteData);
  8649. FOnNoteClick = NULL;
  8650. FNoteHints = FNotes->Text;
  8651. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8652. UpdateStatusBar();
  8653. }
  8654. }
  8655. return Result;
  8656. }
  8657. //---------------------------------------------------------------------------
  8658. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8659. {
  8660. DebugAssert(FNoteTimer->Enabled);
  8661. CancelNote(true);
  8662. }
  8663. //---------------------------------------------------------------------------
  8664. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8665. {
  8666. int P = Note.Pos(L"\n");
  8667. if (P > 0)
  8668. {
  8669. Note.SetLength(P - 1);
  8670. }
  8671. FNotes->Add(FORMAT(L"[%s] %s",
  8672. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8673. while (FNotes->Count > 10)
  8674. {
  8675. FNotes->Delete(0);
  8676. }
  8677. if (UpdateNow)
  8678. {
  8679. FNoteHints = FNotes->Text;
  8680. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8681. UpdateStatusBar();
  8682. }
  8683. }
  8684. //---------------------------------------------------------------------------
  8685. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8686. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8687. {
  8688. int P = Note.Pos(L"\n");
  8689. if (P > 0)
  8690. {
  8691. Note.SetLength(P - 1);
  8692. }
  8693. FNoteHints = FNotes->Text;
  8694. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8695. FNote = Note;
  8696. SAFE_DESTROY(FNoteData);
  8697. FNoteData = NoteData;
  8698. FOnNoteClick = OnNoteClick;
  8699. AddNote(Note, false);
  8700. UpdateStatusBar();
  8701. FNoteShown = Now();
  8702. FNoteTimer->Enabled = false;
  8703. if (Seconds == 0)
  8704. {
  8705. Seconds = WinConfiguration->NotificationsTimeout;
  8706. }
  8707. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8708. FNoteTimer->Enabled = true;
  8709. }
  8710. //---------------------------------------------------------------------------
  8711. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8712. {
  8713. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8714. }
  8715. //---------------------------------------------------------------------------
  8716. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8717. {
  8718. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8719. {
  8720. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8721. }
  8722. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8723. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8724. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8725. 0, NULL, NULL);
  8726. }
  8727. //---------------------------------------------------------------------------
  8728. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8729. {
  8730. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8731. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8732. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8733. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8734. }
  8735. //---------------------------------------------------------------------------
  8736. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8737. {
  8738. WinConfiguration->FormatSizeBytes = Style;
  8739. }
  8740. //---------------------------------------------------------------------------
  8741. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8742. {
  8743. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8744. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8745. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8746. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8747. }
  8748. //---------------------------------------------------------------------------
  8749. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8750. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8751. {
  8752. if (Panel->Index == 0)
  8753. {
  8754. if (FOnNoteClick != NULL)
  8755. {
  8756. // prevent the user data from being freed by possible call
  8757. // to CancelNote or PostNote during call to OnNoteClick
  8758. std::unique_ptr<TObject> NoteData(FNoteData);
  8759. TNotifyEvent OnNoteClick = FOnNoteClick;
  8760. FNoteData = NULL;
  8761. // need to cancel the note as we are going to delete its user data
  8762. CancelNote(true);
  8763. OnNoteClick(NoteData.get());
  8764. }
  8765. }
  8766. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8767. {
  8768. FileSystemInfo();
  8769. }
  8770. }
  8771. //---------------------------------------------------------------------------
  8772. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8773. {
  8774. // workaround:
  8775. // 1) for unknown reason, disabling window, while minimized,
  8776. // prevents it from restoring, even if it was enabled again meanwhile
  8777. // 2) when disabling the main window, while another has focus
  8778. // minimize is no longer possible
  8779. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8780. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8781. // Shouldn't we use IsApplicationMinimized() here?
  8782. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8783. {
  8784. Enabled = false;
  8785. }
  8786. FLockLevel++;
  8787. }
  8788. //---------------------------------------------------------------------------
  8789. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8790. {
  8791. DebugAssert(FLockLevel > 0);
  8792. FLockLevel--;
  8793. if (FLockLevel == 0)
  8794. {
  8795. DebugAssert(FLockSuspendLevel == 0);
  8796. Enabled = true;
  8797. }
  8798. }
  8799. //---------------------------------------------------------------------------
  8800. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8801. {
  8802. // We need to make sure that window is enabled when the last modal window closes
  8803. // otherwise focus is not restored correctly.
  8804. // So we re-enable the window when modal window opens and
  8805. // disable it back after is closes
  8806. if (FLockLevel > 0)
  8807. {
  8808. // while we have nesting counter, we know that we never be called
  8809. // recursivelly as Application->OnModalBegin is called only
  8810. // for the top-level modal window
  8811. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8812. {
  8813. // won't be disabled when conditions in LockWindow() were not satisfied
  8814. FDisabledOnLockSuspend = !Enabled;
  8815. // When minimized to tray (or actually when set to SW_HIDE),
  8816. // setting Enabled makes the window focusable even when there's
  8817. // modal window over it
  8818. if (!FTrayIcon->Visible)
  8819. {
  8820. Enabled = true;
  8821. }
  8822. }
  8823. FLockSuspendLevel++;
  8824. }
  8825. }
  8826. //---------------------------------------------------------------------------
  8827. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8828. {
  8829. if (FLockSuspendLevel > 0)
  8830. {
  8831. DebugAssert(FLockLevel > 0);
  8832. FLockSuspendLevel--;
  8833. // see comment in SuspendWindowLock
  8834. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8835. {
  8836. // Note that window can be enabled here, when we were minized to tray when
  8837. // was SuspendWindowLock() called.
  8838. // We should possibly do the same check as in LockWindow(),
  8839. // if it is ever possible that the consitions change between
  8840. // SuspendWindowLock() and ResumeWindowLock()
  8841. Enabled = !FDisabledOnLockSuspend;
  8842. }
  8843. }
  8844. }
  8845. //---------------------------------------------------------------------------
  8846. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8847. {
  8848. if (!TextOnly)
  8849. {
  8850. TTBXComboBoxItem * RemotePathComboBox =
  8851. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8852. TStrings * Items = RemotePathComboBox->Strings;
  8853. Items->BeginUpdate();
  8854. try
  8855. {
  8856. Items->Clear();
  8857. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8858. {
  8859. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8860. while (!IsUnixRootPath(APath))
  8861. {
  8862. int P = APath.LastDelimiter(L'/');
  8863. DebugAssert(P >= 0);
  8864. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8865. APath.SetLength(P - 1);
  8866. }
  8867. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8868. }
  8869. }
  8870. __finally
  8871. {
  8872. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8873. // Setting ItemIndex to -1 does not reset its text
  8874. if (Items->Count == 0)
  8875. {
  8876. RemotePathComboBox->Text = L"";
  8877. }
  8878. Items->EndUpdate();
  8879. }
  8880. }
  8881. }
  8882. //---------------------------------------------------------------------------
  8883. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8884. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8885. int & ImageIndex)
  8886. {
  8887. if (AIndex < 0)
  8888. {
  8889. AIndex = Sender->ItemIndex;
  8890. }
  8891. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8892. }
  8893. //---------------------------------------------------------------------------
  8894. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8895. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8896. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8897. {
  8898. ARect.Left += (10 * AIndex);
  8899. }
  8900. //---------------------------------------------------------------------------
  8901. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8902. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8903. {
  8904. AWidth += (10 * AIndex);
  8905. }
  8906. //---------------------------------------------------------------------------
  8907. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8908. TObject * Sender)
  8909. {
  8910. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8911. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8912. int Index = RemotePathComboBox->ItemIndex;
  8913. while (Index < RemotePathComboBox->Strings->Count - 1)
  8914. {
  8915. APath = UnixExtractFileDir(APath);
  8916. Index++;
  8917. }
  8918. // VanDyke style paths
  8919. if (APath.IsEmpty())
  8920. {
  8921. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8922. APath = ROOTDIRECTORY;
  8923. }
  8924. if (RemoteDirView->Path != APath)
  8925. {
  8926. RemoteDirView->Path = APath;
  8927. }
  8928. }
  8929. //---------------------------------------------------------------------------
  8930. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8931. {
  8932. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8933. DebugUsedParam(Sender);
  8934. UpdateRemotePathComboBox(true);
  8935. }
  8936. //---------------------------------------------------------------------------
  8937. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8938. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8939. {
  8940. ResetIncrementalSearch();
  8941. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8942. DebugAssert(DirView != NULL);
  8943. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8944. {
  8945. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8946. // OnEditing is called also from TCustomListView::CanEdit
  8947. if (Edit != NULL)
  8948. {
  8949. EditSelectBaseName(Edit);
  8950. }
  8951. }
  8952. }
  8953. //---------------------------------------------------------------------------
  8954. //---------------------------------------------------------------------------
  8955. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8956. {
  8957. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8958. Result->AutoDetectDnD = false;
  8959. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8960. Result->RenderDataOn = rdoEnterAndDropSync;
  8961. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8962. return Result;
  8963. }
  8964. //---------------------------------------------------------------------------
  8965. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8966. {
  8967. TForm::CreateWnd();
  8968. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  8969. if (FSessionsDragDropFilesEx == NULL)
  8970. {
  8971. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8972. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8973. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8974. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8975. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8976. }
  8977. if (FQueueDragDropFilesEx == NULL)
  8978. {
  8979. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8980. // No need to set OnDragOver as we do not have any restrictions
  8981. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8982. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8983. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8984. }
  8985. }
  8986. //---------------------------------------------------------------------------
  8987. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8988. {
  8989. TForm::DestroyWnd();
  8990. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8991. FQueueDragDropFilesEx->DragDropControl = NULL;
  8992. }
  8993. //---------------------------------------------------------------------------
  8994. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8995. {
  8996. SideEnter(FCurrentSide);
  8997. FEverShown = true;
  8998. }
  8999. //---------------------------------------------------------------------------
  9000. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9001. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9002. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9003. {
  9004. if (!NonVisualDataModule->Busy)
  9005. {
  9006. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9007. Configuration->Usage->Inc(L"FileFinds");
  9008. LockWindow(true);
  9009. NonVisualDataModule->StartBusy();
  9010. WinConfiguration->LockedInterface = true;
  9011. try
  9012. {
  9013. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9014. }
  9015. __finally
  9016. {
  9017. WinConfiguration->LockedInterface = false;
  9018. NonVisualDataModule->EndBusy();
  9019. UnlockWindow();
  9020. }
  9021. }
  9022. }
  9023. //---------------------------------------------------------------------------
  9024. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9025. {
  9026. if (!NonVisualDataModule->Busy)
  9027. {
  9028. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9029. SetFocus();
  9030. RemoteDirView->Path = UnixExtractFilePath(Path);
  9031. UnicodeString FileName = UnixExtractFileName(Path);
  9032. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9033. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9034. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9035. if (Item == NULL)
  9036. {
  9037. ReloadDirectory(osRemote);
  9038. Item = RemoteDirView->FindFileItem(FileName);
  9039. }
  9040. if (Item != NULL)
  9041. {
  9042. RemoteDirView->ItemFocused = Item;
  9043. RemoteDirView->ItemFocused->MakeVisible(false);
  9044. RemoteDirView->SetFocus();
  9045. }
  9046. }
  9047. }
  9048. //---------------------------------------------------------------------------
  9049. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9050. {
  9051. bool Result = !NonVisualDataModule->Busy;
  9052. if (Result)
  9053. {
  9054. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9055. }
  9056. return Result;
  9057. }
  9058. //---------------------------------------------------------------------------
  9059. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9060. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9061. {
  9062. if (CanOperateOnFoundFiles(ATerminal))
  9063. {
  9064. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9065. FOnFileOperationFinished = OnFileOperationFinished;
  9066. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9067. }
  9068. }
  9069. //---------------------------------------------------------------------------
  9070. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9071. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9072. {
  9073. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9074. }
  9075. //---------------------------------------------------------------------------
  9076. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9077. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9078. {
  9079. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9080. }
  9081. //---------------------------------------------------------------------------
  9082. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9083. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9084. {
  9085. if (CanOperateOnFoundFiles(ATerminal))
  9086. {
  9087. for (int Index = 0; Index < FileList->Count; Index++)
  9088. {
  9089. UnicodeString FileName = FileList->Strings[Index];
  9090. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9091. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9092. OnFileOperationFinished(FileName, true);
  9093. }
  9094. }
  9095. }
  9096. //---------------------------------------------------------------------------
  9097. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9098. {
  9099. FFileFindTerminal = Terminal;
  9100. ShowFileFindDialog(
  9101. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9102. DoDownloadFoundFiles, DoEditFoundFiles);
  9103. }
  9104. //---------------------------------------------------------------------------
  9105. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9106. {
  9107. FTaskbarList = TaskbarList;
  9108. }
  9109. //---------------------------------------------------------------------------
  9110. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9111. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9112. {
  9113. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9114. if (Index >= 0)
  9115. {
  9116. if (Button == mbLeft)
  9117. {
  9118. // "Mouse down" is raised only after tab is switched.
  9119. // If switching tab (switching session) takes long enough for user
  9120. // to actually release the button, "mouse down" is still raised,
  9121. // but we do not get "mouse up" event, so dragging is not cancelled,
  9122. // prevent that by not beginning dragging in the first place.
  9123. if (IsKeyPressed(VK_LBUTTON))
  9124. {
  9125. // when user clicks the "+", we get mouse down only after the session
  9126. // is closed, when new session tab is already on X:Y, so dragging
  9127. // starts, prevent that
  9128. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9129. {
  9130. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9131. if (Terminal != NULL)
  9132. {
  9133. SessionsPageControl->BeginDrag(false);
  9134. }
  9135. }
  9136. }
  9137. }
  9138. else if (Button == mbMiddle)
  9139. {
  9140. // ignore middle-click for "New session tab"
  9141. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9142. if (Terminal != NULL)
  9143. {
  9144. CloseSessionTab(Index);
  9145. }
  9146. }
  9147. }
  9148. }
  9149. //---------------------------------------------------------------------------
  9150. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9151. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9152. {
  9153. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9154. // do not allow dropping on the "+" tab
  9155. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9156. if ((TargetTerminal != NULL) &&
  9157. (SessionsPageControl->ActivePage->PageIndex != Index))
  9158. {
  9159. Configuration->Usage->Inc(L"SessionTabMoves");
  9160. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9161. // but we want to actually prevent that to avoid flicker
  9162. SessionsPageControl->ActivePage->PageIndex = Index;
  9163. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9164. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9165. }
  9166. }
  9167. //---------------------------------------------------------------------------
  9168. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9169. TObject * Sender, TObject * Source, int X, int Y,
  9170. TDragState /*State*/, bool & Accept)
  9171. {
  9172. Accept = (Sender == Source);
  9173. if (Accept)
  9174. {
  9175. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9176. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9177. // do not allow dragging to the "+" tab
  9178. Accept = (Terminal != NULL);
  9179. }
  9180. }
  9181. //---------------------------------------------------------------------------
  9182. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9183. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9184. {
  9185. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9186. if (Index < 0)
  9187. {
  9188. Effect = DROPEFFECT_NONE;
  9189. }
  9190. else
  9191. {
  9192. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9193. // do not allow dropping on the "+" tab
  9194. if (TargetTerminal == NULL)
  9195. {
  9196. Effect = DROPEFFECT_NONE;
  9197. }
  9198. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9199. {
  9200. Effect = DROPEFFECT_COPY;
  9201. }
  9202. }
  9203. }
  9204. //---------------------------------------------------------------------------
  9205. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9206. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9207. {
  9208. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9209. // do not allow dropping on the "+" tab
  9210. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9211. if (TargetTerminal != NULL)
  9212. {
  9213. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9214. if (!IsFileControl(DropSourceControl, osRemote))
  9215. {
  9216. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9217. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9218. // Why don't we use Terminal->CurrentDirectory directly?
  9219. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9220. // do not force queue, drag drop
  9221. false, true);
  9222. }
  9223. }
  9224. }
  9225. //---------------------------------------------------------------------------
  9226. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9227. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9228. {
  9229. // Downloads are handled in RemoteFileControlDDEnd
  9230. if (!IsFileControl(DropSourceControl, osRemote))
  9231. {
  9232. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9233. Terminal->CurrentDirectory,
  9234. // force queue, drag drop
  9235. true, true);
  9236. }
  9237. }
  9238. //---------------------------------------------------------------------------
  9239. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9240. {
  9241. FShowing = false;
  9242. // Do not save empty workspace
  9243. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9244. {
  9245. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9246. UnicodeString Name = WorkspaceName();
  9247. DoSaveWorkspace(
  9248. Name, DataList.get(),
  9249. !Configuration->DisablePasswordStoring &&
  9250. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9251. WinConfiguration->LastStoredSession = Name;
  9252. }
  9253. }
  9254. //---------------------------------------------------------------------------
  9255. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9256. {
  9257. TManagedTerminal * ManagedTerminal =
  9258. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9259. if (ManagedTerminal != NULL)
  9260. {
  9261. ManagedTerminal->DirectoryLoaded = Now();
  9262. }
  9263. }
  9264. //---------------------------------------------------------------------------
  9265. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9266. TListItem * /*Item*/, bool /*Selected*/)
  9267. {
  9268. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9269. switch (DirView->LastSelectMethod)
  9270. {
  9271. case smKeyboard:
  9272. Configuration->Usage->Inc(L"KeyboardSelections");
  9273. break;
  9274. case smMouse:
  9275. Configuration->Usage->Inc(L"MouseSelections");
  9276. break;
  9277. }
  9278. }
  9279. //---------------------------------------------------------------------------
  9280. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9281. {
  9282. if (Color != 0)
  9283. {
  9284. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9285. }
  9286. else
  9287. {
  9288. return FSessionTabImageIndex;
  9289. }
  9290. }
  9291. //---------------------------------------------------------------------------
  9292. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9293. {
  9294. TPngImageCollectionItem * Item =
  9295. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9296. FSessionColors->AddPng(Item->PngImage);
  9297. return FSessionColors->Count - 1;
  9298. }
  9299. //---------------------------------------------------------------------------
  9300. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9301. {
  9302. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9303. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9304. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9305. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9306. }
  9307. //---------------------------------------------------------------------------
  9308. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9309. {
  9310. for (int Index = 0; Index < Control->ControlCount; Index++)
  9311. {
  9312. TControl * AControl = Control->Controls[Index];
  9313. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9314. if (WinControl != NULL)
  9315. {
  9316. CollectItemsWithTextDisplayMode(WinControl);
  9317. }
  9318. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9319. if (Toolbar != NULL)
  9320. {
  9321. // we care for top-level items only
  9322. TTBCustomItem * Items = Toolbar->Items;
  9323. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9324. {
  9325. TTBCustomItem * Item = Items->Items[ItemIndex];
  9326. if (((Item->DisplayMode == nbdmImageAndText) ||
  9327. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9328. EligibleForImageDisplayMode(Item))
  9329. {
  9330. FItemsWithTextDisplayMode.insert(Item);
  9331. }
  9332. }
  9333. }
  9334. }
  9335. }
  9336. //---------------------------------------------------------------------------
  9337. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9338. {
  9339. return true;
  9340. }
  9341. //---------------------------------------------------------------------------
  9342. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9343. {
  9344. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9345. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9346. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9347. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9348. bool Result = true;
  9349. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9350. {
  9351. TTBCustomItem * Item = *i;
  9352. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9353. if (Label != NULL)
  9354. {
  9355. // optimization
  9356. if (Label->Visible == SelectiveToolbarText)
  9357. {
  9358. Result = false;
  9359. }
  9360. else
  9361. {
  9362. Label->Visible = SelectiveToolbarText;
  9363. }
  9364. }
  9365. else
  9366. {
  9367. // optimization
  9368. if (Item->DisplayMode == DisplayMode)
  9369. {
  9370. Result = false;
  9371. }
  9372. else
  9373. {
  9374. Item->DisplayMode = DisplayMode;
  9375. }
  9376. }
  9377. ++i;
  9378. }
  9379. if (Result)
  9380. {
  9381. UpdateNewSessionTab();
  9382. }
  9383. return Result;
  9384. }
  9385. //---------------------------------------------------------------------------
  9386. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9387. {
  9388. DebugFail();
  9389. }
  9390. //---------------------------------------------------------------------------
  9391. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9392. {
  9393. // Among other, a lock level is non zero, while directory is being loaded,
  9394. // even when it happens as a result of dirview navigation,
  9395. // i.e. when TNonVisualDataModule is NOT FBusy.
  9396. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9397. // Among other this prevents a panel auto update to occur while
  9398. // directory is changing.
  9399. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9400. }
  9401. //---------------------------------------------------------------------------
  9402. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9403. {
  9404. // While the window is disabled, we still seem to process menu shortcuts at least,
  9405. // so stop it at least here.
  9406. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9407. return
  9408. (Allowed == aaUpdate) ||
  9409. !NonVisualDataModule->Busy;
  9410. }
  9411. //---------------------------------------------------------------------------
  9412. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9413. {
  9414. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9415. }
  9416. //---------------------------------------------------------------------------
  9417. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9418. {
  9419. // This is somewhat redundant to LockWindow() call from
  9420. // TTerminalManager::TerminalReadDirectoryProgress.
  9421. // But disabling window is known not to block keyboard shorcuts
  9422. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9423. if (Busy > 0)
  9424. {
  9425. if (NonVisualDataModule->Busy)
  9426. {
  9427. State = false;
  9428. }
  9429. else
  9430. {
  9431. NonVisualDataModule->StartBusy();
  9432. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9433. LockWindow();
  9434. }
  9435. }
  9436. else if (Busy < 0)
  9437. {
  9438. UnlockWindow();
  9439. NonVisualDataModule->EndBusy();
  9440. }
  9441. else
  9442. {
  9443. State = NonVisualDataModule->Busy;
  9444. }
  9445. }
  9446. //---------------------------------------------------------------------------
  9447. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9448. {
  9449. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9450. // no context menu for "New session tab"
  9451. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9452. {
  9453. SessionsPageControl->ActivePageIndex = Index;
  9454. if (DebugAlwaysTrue(SessionTabSwitched()))
  9455. {
  9456. // copied from TControl.WMContextMenu
  9457. SendCancelMode(SessionsPageControl);
  9458. // explicit popup instead of using PopupMenu property
  9459. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9460. // while connecting yet not-connected session and hence
  9461. // allowing an access to commands over not-completelly connected session
  9462. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9463. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9464. PopupMenu->PopupComponent = SessionsPageControl;
  9465. PopupMenu->Popup(Point.x, Point.y);
  9466. }
  9467. }
  9468. Handled = true;
  9469. }
  9470. //---------------------------------------------------------------------------
  9471. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9472. {
  9473. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9474. }
  9475. //---------------------------------------------------------------------------
  9476. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9477. {
  9478. Configuration->Usage->Inc(L"PasswordChanges");
  9479. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9480. ChangePasswordData->DisableAuthentationsExceptPassword();
  9481. ChangePasswordData->ChangePassword = true;
  9482. std::unique_ptr<TTerminal> ChangePasswordSession(
  9483. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9484. ChangePasswordSession->AutoReadDirectory = false;
  9485. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9486. {
  9487. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9488. }
  9489. else
  9490. {
  9491. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9492. }
  9493. }
  9494. //---------------------------------------------------------------------------
  9495. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9496. {
  9497. // No nice way to assert SSH2
  9498. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9499. }
  9500. //---------------------------------------------------------------------------
  9501. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9502. {
  9503. TOperationVisualizer OperationVisualizer;
  9504. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9505. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9506. }
  9507. //---------------------------------------------------------------------------
  9508. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9509. {
  9510. TForm::ChangeScale(M, D);
  9511. int APixelsPerInch = GetControlPixelsPerInch(this);
  9512. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9513. try
  9514. {
  9515. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9516. }
  9517. __finally
  9518. {
  9519. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9520. }
  9521. }
  9522. //---------------------------------------------------------------------------
  9523. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9524. {
  9525. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9526. }
  9527. //---------------------------------------------------------------------
  9528. class TPublicControl : public TControl
  9529. {
  9530. friend class TCustomScpExplorerForm;
  9531. };
  9532. //---------------------------------------------------------------------------
  9533. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9534. {
  9535. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9536. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9537. }
  9538. //---------------------------------------------------------------------------
  9539. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9540. {
  9541. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9542. OpenBookmark(Side, Bookmark);
  9543. }
  9544. //---------------------------------------------------------------------------
  9545. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9546. {
  9547. DoBookmarkClick(osLocal, Sender);
  9548. }
  9549. //---------------------------------------------------------------------------
  9550. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9551. {
  9552. DoBookmarkClick(osRemote, Sender);
  9553. }
  9554. //---------------------------------------------------------------------------
  9555. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9556. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9557. {
  9558. // TODO
  9559. if (BookmarkList != NULL)
  9560. {
  9561. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9562. if (!WinConfiguration->UseLocationProfiles)
  9563. {
  9564. std::unique_ptr<TStringList> Directories(new TStringList());
  9565. Directories->CaseSensitive = (Side == osRemote);
  9566. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9567. {
  9568. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9569. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9570. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9571. {
  9572. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9573. Item->Caption = EscapeHotkey(Directory);
  9574. Item->ShortCut = Bookmark->ShortCut;
  9575. Item->OnClick = OnBookmarkClick;
  9576. Item->Tag = reinterpret_cast<int>(Bookmark);
  9577. Directories->Add(Directory);
  9578. Menu->Add(Item.release());
  9579. }
  9580. }
  9581. }
  9582. else
  9583. {
  9584. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9585. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9586. {
  9587. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9588. if (!Bookmark->Node.IsEmpty())
  9589. {
  9590. Folders->Add(Bookmark->Node);
  9591. }
  9592. }
  9593. for (int Index = 0; Index < Folders->Count; Index++)
  9594. {
  9595. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9596. Item->Caption = Folders->Strings[Index];
  9597. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9598. Folders->Objects[Index] = Item.get();
  9599. Menu->Add(Item.release());
  9600. }
  9601. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9602. {
  9603. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9604. TTBCustomItem * Parent;
  9605. if (!Bookmark->Node.IsEmpty())
  9606. {
  9607. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9608. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9609. }
  9610. else
  9611. {
  9612. Parent = Menu;
  9613. }
  9614. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9615. Item->Caption = Bookmark->Name;
  9616. Item->ShortCut = Bookmark->ShortCut;
  9617. Item->OnClick = OnBookmarkClick;
  9618. Item->Tag = reinterpret_cast<int>(Bookmark);
  9619. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9620. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9621. {
  9622. // No hint for location profiles that are actually mere "bookmarks"
  9623. }
  9624. else
  9625. {
  9626. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9627. UnicodeString LongHint;
  9628. if (!Bookmark->Local.IsEmpty())
  9629. {
  9630. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9631. }
  9632. if (!Bookmark->Remote.IsEmpty())
  9633. {
  9634. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9635. }
  9636. AddToList(Hint, LongHint, L"|");
  9637. Item->Hint = Hint;
  9638. }
  9639. Parent->Add(Item.release());
  9640. }
  9641. }
  9642. AddMenuSeparator(Menu);
  9643. }
  9644. }
  9645. //---------------------------------------------------------------------------
  9646. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9647. {
  9648. Menu->Clear();
  9649. std::unique_ptr<TTBCustomItem> Item;
  9650. Item.reset(new TTBXItem(Owner));
  9651. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9652. Item->Options = Item->Options << tboDefault;
  9653. Menu->Add(Item.release());
  9654. AddMenuSeparator(Menu);
  9655. if (Terminal != NULL)
  9656. {
  9657. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9658. }
  9659. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9660. Item.reset(new TTBXItem(Owner));
  9661. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9662. Menu->Add(Item.release());
  9663. }
  9664. //---------------------------------------------------------------------------
  9665. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9666. {
  9667. bool Result = true;
  9668. try
  9669. {
  9670. if (Side == osRemote)
  9671. {
  9672. Terminal->ExceptionOnFail = true;
  9673. }
  9674. try
  9675. {
  9676. DirView(Side)->Path = Path;
  9677. }
  9678. __finally
  9679. {
  9680. if (Side == osRemote)
  9681. {
  9682. Terminal->ExceptionOnFail = false;
  9683. }
  9684. }
  9685. }
  9686. catch (Exception & E)
  9687. {
  9688. if ((Side != osRemote) || Terminal->Active)
  9689. {
  9690. ShowExtendedExceptionEx(Terminal, &E);
  9691. Result = false;
  9692. }
  9693. else
  9694. {
  9695. throw;
  9696. }
  9697. }
  9698. return Result;
  9699. }
  9700. //---------------------------------------------------------------------------
  9701. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9702. {
  9703. // Make sure there some feedback even when loading tiny local folders
  9704. TInstantOperationVisualizer Visualizer;
  9705. DirView(Side)->ReloadDirectory();
  9706. }
  9707. //---------------------------------------------------------------------------
  9708. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9709. {
  9710. if (Index == SessionsPageControl->TabIndex)
  9711. {
  9712. CloseSession();
  9713. }
  9714. else
  9715. {
  9716. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9717. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9718. TTerminalManager * Manager = TTerminalManager::Instance();
  9719. if (!Terminal->Active)
  9720. {
  9721. Manager->FreeTerminal(Terminal);
  9722. }
  9723. else
  9724. {
  9725. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9726. if (CanCloseQueue(Queue))
  9727. {
  9728. Manager->FreeTerminal(Terminal);
  9729. }
  9730. }
  9731. }
  9732. }
  9733. //---------------------------------------------------------------------------
  9734. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9735. {
  9736. CloseSessionTab(Index);
  9737. }
  9738. //---------------------------------------------------------------------------
  9739. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9740. {
  9741. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9742. {
  9743. TInstantOperationVisualizer Visualizer;
  9744. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9745. // before we create a new data object
  9746. ClipboardClear();
  9747. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9748. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9749. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9750. // Intermediate variable to WORKAROUND a crash in compiler
  9751. TObjectList * ClipboardFakeMonitors =
  9752. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9753. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9754. if (FClipboardDragDropFilesEx.get() == NULL)
  9755. {
  9756. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9757. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9758. }
  9759. FClipboardDragDropFilesEx->FileList->Clear();
  9760. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9761. FClipboardDragDropFilesEx->CopyToClipboard();
  9762. FClipboardTerminal = FTerminal;
  9763. // Need full paths, as cwd can be different once files are pasted
  9764. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9765. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9766. {
  9767. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9768. }
  9769. }
  9770. }
  9771. //---------------------------------------------------------------------------
  9772. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9773. {
  9774. if (OpenClipboard(0))
  9775. {
  9776. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9777. CloseClipboard();
  9778. }
  9779. }
  9780. //---------------------------------------------------------------------------
  9781. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9782. {
  9783. return !FClipboardFakeDirectory.IsEmpty();
  9784. }
  9785. //---------------------------------------------------------------------------
  9786. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9787. {
  9788. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9789. FClipboardFakeDirectory = UnicodeString();
  9790. FClipboardTerminal = NULL;
  9791. // Also called in Idle()
  9792. FClipboardFakeMonitors.reset(NULL);
  9793. }
  9794. //---------------------------------------------------------------------------
  9795. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9796. {
  9797. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9798. {
  9799. ClipboardStop();
  9800. }
  9801. }
  9802. //---------------------------------------------------------------------------
  9803. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9804. {
  9805. TTerminalManager * Manager = TTerminalManager::Instance();
  9806. Manager->ActiveTerminal = FClipboardTerminal;
  9807. TTransferOperationParam Params;
  9808. Params.TargetDirectory = TargetDirectory;
  9809. Params.DragDrop = DragDrop;
  9810. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9811. }
  9812. //---------------------------------------------------------------------------
  9813. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9814. {
  9815. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9816. // (as it pumps a Windows message queue while being shutted down)
  9817. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9818. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9819. {
  9820. // Can fail as it can be e.g. locked by AV, so we retry that later
  9821. bool Removed = RemoveDir(ApiPath(FileName));
  9822. try
  9823. {
  9824. if (!NonVisualDataModule->Busy)
  9825. {
  9826. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9827. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9828. }
  9829. }
  9830. __finally
  9831. {
  9832. if (!Removed)
  9833. {
  9834. RemoveDir(ApiPath(FileName));
  9835. }
  9836. }
  9837. }
  9838. }
  9839. //---------------------------------------------------------------------------
  9840. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9841. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9842. {
  9843. TFileMasks::TParams MaskParams;
  9844. MaskParams.Size = Size;
  9845. MaskParams.Modification = Modification;
  9846. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9847. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9848. {
  9849. bool ImplicitMatch;
  9850. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9851. !ImplicitMatch)
  9852. {
  9853. Color = Iter->Color;
  9854. break;
  9855. }
  9856. }
  9857. }
  9858. //---------------------------------------------------------------------------
  9859. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9860. {
  9861. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9862. if (!DirView->IsEditing())
  9863. {
  9864. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9865. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9866. // while searching
  9867. if (Key == VK_BACK)
  9868. {
  9869. if (!FIncrementalSearch.IsEmpty())
  9870. {
  9871. if (FIncrementalSearch.Length() == 1)
  9872. {
  9873. ResetIncrementalSearch();
  9874. }
  9875. else
  9876. {
  9877. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9878. IncrementalSearch(NewText, false, false);
  9879. }
  9880. Key = 0;
  9881. }
  9882. }
  9883. }
  9884. }
  9885. //---------------------------------------------------------------------------
  9886. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9887. {
  9888. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9889. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9890. {
  9891. // Filter control sequences.
  9892. // When not searching yet, prefer use of the space for toggling file selection
  9893. // (so we cannot incrementally search for a string starting with a space).
  9894. if ((Key > VK_SPACE) ||
  9895. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9896. {
  9897. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9898. Key = 0;
  9899. }
  9900. }
  9901. }
  9902. //---------------------------------------------------------------------------
  9903. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9904. {
  9905. if (!FIncrementalSearch.IsEmpty())
  9906. {
  9907. FIncrementalSearch = L"";
  9908. DirView(osCurrent)->UpdateStatusBar();
  9909. }
  9910. }
  9911. //---------------------------------------------------------------------------
  9912. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9913. {
  9914. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9915. if (Item == NULL)
  9916. {
  9917. MessageBeep(MB_ICONHAND);
  9918. }
  9919. else
  9920. {
  9921. TCustomDirView * ADirView = DirView(osCurrent);
  9922. {
  9923. TAutoNestingCounter Guard(FIncrementalSearching);
  9924. ADirView->FocusItem(Item);
  9925. if (ADirView->NortonLike == nlOff)
  9926. {
  9927. if ((ADirView->Selected != Item) ||
  9928. (ADirView->SelCount > 1))
  9929. {
  9930. ADirView->ClearSelection();
  9931. ADirView->Selected = Item;
  9932. }
  9933. }
  9934. }
  9935. FIncrementalSearch = Text;
  9936. Item->MakeVisible(false);
  9937. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9938. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9939. ADirView->UpdateStatusBar();
  9940. }
  9941. }
  9942. //---------------------------------------------------------------------------
  9943. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9944. {
  9945. int Index = Item->Index;
  9946. if (!Reverse)
  9947. {
  9948. Index++;
  9949. if (Index >= Item->Owner->Count)
  9950. {
  9951. Index = 0;
  9952. }
  9953. }
  9954. else
  9955. {
  9956. Index--;
  9957. if (Index < 0)
  9958. {
  9959. Index = Item->Owner->Count - 1;
  9960. }
  9961. }
  9962. return Item->Owner->Item[Index];
  9963. }
  9964. //---------------------------------------------------------------------------
  9965. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9966. {
  9967. TCustomDirView * ADirView = DirView(osCurrent);
  9968. if (ADirView->Items->Count == 0)
  9969. {
  9970. return NULL;
  9971. }
  9972. else
  9973. {
  9974. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9975. TListItem * Item = CurrentItem;
  9976. if (SkipCurrent)
  9977. {
  9978. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9979. }
  9980. while (true)
  9981. {
  9982. bool Matches = false;
  9983. switch (WinConfiguration->PanelSearch)
  9984. {
  9985. case isNameStartOnly:
  9986. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9987. break;
  9988. case isName:
  9989. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9990. break;
  9991. case isAll:
  9992. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9993. int Index = 0;
  9994. while ((Index < ColCount) && !Matches)
  9995. {
  9996. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9997. if (Visible)
  9998. {
  9999. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10000. }
  10001. Index++;
  10002. }
  10003. break;
  10004. }
  10005. if (Matches)
  10006. {
  10007. return Item;
  10008. }
  10009. Item = GetNextFile(Item, Reverse);
  10010. if (Item == CurrentItem)
  10011. {
  10012. return NULL;
  10013. }
  10014. }
  10015. }
  10016. }
  10017. //---------------------------------------------------------------------------
  10018. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10019. {
  10020. ResetIncrementalSearch();
  10021. }
  10022. //---------------------------------------------------------------------------
  10023. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10024. {
  10025. if (Message.CharCode == VK_TAB)
  10026. {
  10027. if (!FIncrementalSearch.IsEmpty())
  10028. {
  10029. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10030. bool Reverse = Shift.Contains(ssShift);
  10031. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10032. Message.Result = 1;
  10033. return;
  10034. }
  10035. }
  10036. else if (Message.CharCode == VK_ESCAPE)
  10037. {
  10038. if (!FIncrementalSearch.IsEmpty())
  10039. {
  10040. ResetIncrementalSearch();
  10041. Message.Result = 1;
  10042. return;
  10043. }
  10044. }
  10045. TForm::Dispatch(&Message);
  10046. }
  10047. //---------------------------------------------------------------------------
  10048. void __fastcall TCustomScpExplorerForm::Deactivate()
  10049. {
  10050. TForm::Deactivate();
  10051. // E.g. when switching to an internal editor window
  10052. ResetIncrementalSearch();
  10053. }
  10054. //---------------------------------------------------------------------------
  10055. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10056. {
  10057. // When switching to another application
  10058. ResetIncrementalSearch();
  10059. }
  10060. //---------------------------------------------------------------------------
  10061. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10062. {
  10063. ResetIncrementalSearch();
  10064. }
  10065. //---------------------------------------------------------------------------
  10066. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10067. {
  10068. if (FIncrementalSearching <= 0)
  10069. {
  10070. ResetIncrementalSearch();
  10071. }
  10072. }
  10073. //---------------------------------------------------------------------------
  10074. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10075. {
  10076. CountClicksForWindowPrint(this);
  10077. }
  10078. //---------------------------------------------------------------------------
  10079. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10080. {
  10081. CenterReconnectToolbar();
  10082. }
  10083. //---------------------------------------------------------------------------
  10084. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10085. {
  10086. if (RemoteDirView->ItemFocused != NULL)
  10087. {
  10088. RemoteDirView->ItemFocused->Selected = true;
  10089. }
  10090. }
  10091. //---------------------------------------------------------------------------