CustomScpExplorer.cpp 353 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772
  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[fsBackgroundTransfers], 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[fsBackgroundTransfers])
  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[fsSkipTransfer] : 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[fsBackgroundTransfers])
  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. if (!FStarted)
  4296. {
  4297. FStarted = true;
  4298. InterfaceStarted();
  4299. }
  4300. }
  4301. }
  4302. if (FShowing || FStandaloneEditing)
  4303. {
  4304. if (FQueueStatusInvalidated)
  4305. {
  4306. UpdateQueueStatus(false);
  4307. }
  4308. RefreshQueueItems();
  4309. }
  4310. if (FShowing)
  4311. {
  4312. UpdateStatusBar();
  4313. }
  4314. FIgnoreNextDialogChar = 0;
  4315. }
  4316. //---------------------------------------------------------------------------
  4317. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4318. {
  4319. try
  4320. {
  4321. FUserActionTimer->Enabled = false;
  4322. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4323. {
  4324. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4325. {
  4326. FPendingQueueActionItem->ProcessUserAction();
  4327. }
  4328. }
  4329. }
  4330. __finally
  4331. {
  4332. FPendingQueueActionItem = NULL;
  4333. }
  4334. }
  4335. //---------------------------------------------------------------------------
  4336. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4337. {
  4338. if (WinConfiguration->MinimizeToTray)
  4339. {
  4340. UpdateTrayIcon();
  4341. FTrayIcon->Visible = true;
  4342. if (Visible)
  4343. {
  4344. ShowWindow(Handle, SW_HIDE);
  4345. }
  4346. }
  4347. }
  4348. //---------------------------------------------------------------------------
  4349. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4350. {
  4351. // WORKAROUND
  4352. // When restoring maximized window from minimization,
  4353. // rarely some controls do not align properly.
  4354. // Two instances seen (both for Commander):
  4355. // - When restoring, window is temporarily narrower (not maximized),
  4356. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4357. // Once maximized already, top dock shinks back, but the session PageControl,
  4358. // do not align up, leaving space between TopDock and PageControl.
  4359. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4360. for (int Index = 0; Index < ControlCount; Index++)
  4361. {
  4362. RealignControl(Controls[Index]);
  4363. }
  4364. if (FTrayIcon->Visible)
  4365. {
  4366. FTrayIcon->Visible = false;
  4367. if (Visible)
  4368. {
  4369. ShowWindow(Handle, SW_SHOW);
  4370. }
  4371. }
  4372. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4373. {
  4374. FNeedSession = false;
  4375. NonVisualDataModule->StartBusy();
  4376. try
  4377. {
  4378. NeedSession(true);
  4379. }
  4380. __finally
  4381. {
  4382. NonVisualDataModule->EndBusy();
  4383. }
  4384. }
  4385. }
  4386. //---------------------------------------------------------------------------
  4387. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4388. {
  4389. FTrayIcon->Hint = GetMainForm()->Caption;
  4390. }
  4391. //---------------------------------------------------------------------------
  4392. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4393. {
  4394. UpdateTrayIcon();
  4395. }
  4396. //---------------------------------------------------------------------------
  4397. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4398. {
  4399. // workaround
  4400. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4401. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4402. // But WM_ACTIVATEAPP message can be received even when
  4403. // the main window is minimized to the tray and internal editor window is focused
  4404. // (after another application was previously active)
  4405. if (::IsIconic(Handle))
  4406. {
  4407. if (!IsAppIconic())
  4408. {
  4409. SetAppIconic(true);
  4410. }
  4411. }
  4412. ::ApplicationRestore();
  4413. Application->BringToFront();
  4414. }
  4415. //---------------------------------------------------------------------------
  4416. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4417. {
  4418. RestoreApp();
  4419. }
  4420. //---------------------------------------------------------------------------
  4421. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4422. {
  4423. if (OpenInNewWindow())
  4424. {
  4425. // todo: Pass FromSite
  4426. ExecuteNewInstance(SessionUrl);
  4427. }
  4428. else
  4429. {
  4430. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4431. }
  4432. }
  4433. //---------------------------------------------------------------------------
  4434. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4435. {
  4436. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4437. StoredSessions->NewSession(SessionName, SessionData);
  4438. // modified only, explicit
  4439. StoredSessions->Save(false, true);
  4440. // encode session name because of slashes in hierarchical sessions
  4441. return EncodeUrlString(SessionName);
  4442. }
  4443. //---------------------------------------------------------------------------
  4444. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4445. {
  4446. // current working directories become defaults here, what is not right
  4447. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4448. return SaveHiddenDuplicateSession(SessionData.get());
  4449. }
  4450. //---------------------------------------------------------------------------
  4451. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4452. {
  4453. if (OpenInNewWindow())
  4454. {
  4455. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4456. }
  4457. else
  4458. {
  4459. // current working directories become defaults here, what is not right
  4460. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4461. TTerminalManager * Manager = TTerminalManager::Instance();
  4462. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4463. Manager->ActiveTerminal = Terminal;
  4464. }
  4465. }
  4466. //---------------------------------------------------------------------------
  4467. void __fastcall TCustomScpExplorerForm::RenameSession()
  4468. {
  4469. UnicodeString Name = Terminal->SessionData->SessionName;
  4470. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4471. {
  4472. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4473. TSessionData::ValidateName(Name);
  4474. // This is inconsistent with how color (for example) is handled.
  4475. Terminal->SessionData->Name = Name;
  4476. UpdateControls();
  4477. // Add/Remove distinguishing paths from sessions of the same name.
  4478. DoTerminalListChanged();
  4479. }
  4480. }
  4481. //---------------------------------------------------------------------------
  4482. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4483. {
  4484. DebugAssert(Queue != NULL);
  4485. bool Result = Queue->IsEmpty;
  4486. if (!Result)
  4487. {
  4488. SetFocus();
  4489. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4490. }
  4491. return Result;
  4492. }
  4493. //---------------------------------------------------------------------------
  4494. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4495. {
  4496. return CanCloseQueue(FQueue);
  4497. }
  4498. //---------------------------------------------------------------------------
  4499. void __fastcall TCustomScpExplorerForm::CloseSession()
  4500. {
  4501. if (CanCloseQueue())
  4502. {
  4503. TTerminalManager::Instance()->FreeActiveTerminal();
  4504. }
  4505. }
  4506. //---------------------------------------------------------------------------
  4507. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4508. {
  4509. if (CanCloseQueue())
  4510. {
  4511. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4512. }
  4513. }
  4514. //---------------------------------------------------------------------------
  4515. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4516. {
  4517. DetachTerminal(Terminal);
  4518. RemoteDirView->Terminal = Terminal;
  4519. UpdateRemotePathComboBox(false);
  4520. UpdateControls();
  4521. }
  4522. //---------------------------------------------------------------------------
  4523. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4524. {
  4525. if (DebugAlwaysTrue(!Terminal->Active))
  4526. {
  4527. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4528. }
  4529. }
  4530. //---------------------------------------------------------------------------
  4531. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4532. {
  4533. if (OpenInNewWindow())
  4534. {
  4535. // encode session name because of slashes in hierarchical sessions
  4536. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4537. }
  4538. else
  4539. {
  4540. TTerminalManager * Manager = TTerminalManager::Instance();
  4541. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4542. Manager->ActiveTerminal = Terminal;
  4543. }
  4544. }
  4545. //---------------------------------------------------------------------------
  4546. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4547. {
  4548. TTerminalManager * Manager = TTerminalManager::Instance();
  4549. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4550. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4551. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4552. // FirstTerminal can be null, if some of the
  4553. if (!ConnectFirstTerminal && (FirstTerminal != NULL))
  4554. {
  4555. FirstTerminal->Disconnected = true;
  4556. FirstTerminal->DisconnectedTemporarily = true;
  4557. }
  4558. Manager->ActiveTerminal = FirstTerminal;
  4559. }
  4560. //---------------------------------------------------------------------------
  4561. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4562. {
  4563. if (OpenInNewWindow())
  4564. {
  4565. ExecuteNewInstance(Name);
  4566. }
  4567. else
  4568. {
  4569. DoOpenFolderOrWorkspace(Name, true);
  4570. }
  4571. }
  4572. //---------------------------------------------------------------------------
  4573. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4574. bool &CanClose)
  4575. {
  4576. // See the comment in CloseApp()
  4577. if (NonVisualDataModule->Busy)
  4578. {
  4579. CanClose = false;
  4580. }
  4581. else if (Terminal != NULL)
  4582. {
  4583. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4584. {
  4585. unsigned int Result;
  4586. TMessageParams Params(mpNeverAskAgainCheck);
  4587. UnicodeString Message;
  4588. int Answers = qaOK | qaCancel;
  4589. if (TTerminalManager::Instance()->Count > 1)
  4590. {
  4591. if (!WinConfiguration->AutoSaveWorkspace)
  4592. {
  4593. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4594. }
  4595. else
  4596. {
  4597. Message = LoadStr(CLOSE_SESSIONS);
  4598. }
  4599. }
  4600. else
  4601. {
  4602. if (!WinConfiguration->AutoSaveWorkspace)
  4603. {
  4604. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4605. }
  4606. else
  4607. {
  4608. Message = LoadStr(CLOSE_SESSION);
  4609. }
  4610. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4611. }
  4612. UnicodeString Note;
  4613. if (WinConfiguration->AutoSaveWorkspace)
  4614. {
  4615. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4616. }
  4617. else
  4618. {
  4619. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4620. Answers = qaYes | qaNo | qaCancel;
  4621. }
  4622. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4623. SetFocus();
  4624. Result = MessageDialog(Message, qtConfirmation,
  4625. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4626. if (Result == qaNeverAskAgain)
  4627. {
  4628. WinConfiguration->ConfirmClosingSession = false;
  4629. }
  4630. if (Result == qaNo)
  4631. {
  4632. CanClose = SaveWorkspace(true);
  4633. // note that the workspace will be saved redundatly again from FormClose
  4634. }
  4635. else
  4636. {
  4637. CanClose =
  4638. (Result == qaOK) ||
  4639. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4640. (Result == qaNeverAskAgain);
  4641. }
  4642. }
  4643. if (CanClose)
  4644. {
  4645. CanClose = CanCloseQueue();
  4646. }
  4647. }
  4648. if (CanClose)
  4649. {
  4650. CanClose =
  4651. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4652. FEditorManager->CloseExternalFilesWithoutProcess();
  4653. if (CanClose)
  4654. {
  4655. while (CanClose && (FLocalEditors->Count > 0))
  4656. {
  4657. int PrevCount = FLocalEditors->Count;
  4658. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4659. CanClose = (FLocalEditors->Count < PrevCount);
  4660. }
  4661. if (CanClose)
  4662. {
  4663. CanClose = FEditorManager->Empty(true);
  4664. if (!CanClose)
  4665. {
  4666. SetFocus();
  4667. CanClose =
  4668. (MessageDialog(
  4669. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4670. }
  4671. }
  4672. }
  4673. }
  4674. }
  4675. //---------------------------------------------------------------------------
  4676. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4677. {
  4678. TForm * Form = dynamic_cast<TForm *>(Sender);
  4679. DebugAssert(Form != NULL);
  4680. Form->Close();
  4681. }
  4682. //---------------------------------------------------------------------------
  4683. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4684. {
  4685. TForm * Form = dynamic_cast<TForm *>(Sender);
  4686. delete Form;
  4687. }
  4688. //---------------------------------------------------------------------------
  4689. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4690. {
  4691. while (FLocalEditors->Count > 0)
  4692. {
  4693. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4694. }
  4695. }
  4696. //---------------------------------------------------------------------------
  4697. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4698. TObject *Sender)
  4699. {
  4700. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4701. try
  4702. {
  4703. SetProperties(osRemote, FileList);
  4704. }
  4705. __finally
  4706. {
  4707. delete FileList;
  4708. }
  4709. }
  4710. //---------------------------------------------------------------------------
  4711. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4712. {
  4713. if (value)
  4714. {
  4715. if (Component == fcCustomCommandsBand)
  4716. {
  4717. UpdateCustomCommandsToolbar();
  4718. }
  4719. }
  4720. }
  4721. //---------------------------------------------------------------------------
  4722. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4723. {
  4724. TControl * Control = GetComponent(Component);
  4725. DebugAssert(Control);
  4726. bool Changed = (Control->Visible != value);
  4727. if (Changed)
  4728. {
  4729. ComponentShowing(Component, value);
  4730. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4731. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4732. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4733. if (value)
  4734. {
  4735. int RemainingHeight = Control->Parent->ClientHeight;
  4736. int RemainingWidth = Control->Parent->ClientWidth;
  4737. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4738. {
  4739. TControl * ChildControl = Control->Parent->Controls[i];
  4740. if (ChildControl->Visible)
  4741. {
  4742. switch (ChildControl->Align)
  4743. {
  4744. case alTop:
  4745. case alBottom:
  4746. RemainingHeight -= ChildControl->Height;
  4747. break;
  4748. case alLeft:
  4749. case alRight:
  4750. RemainingWidth -= ChildControl->Width;
  4751. break;
  4752. }
  4753. }
  4754. }
  4755. int Reserve = ScaleByTextHeight(this, 32);
  4756. // queue in explorer, trees in commander
  4757. if (Control->Height > RemainingHeight - Reserve)
  4758. {
  4759. Control->Height = RemainingHeight / 2;
  4760. }
  4761. if (Control->Width > RemainingWidth - Reserve)
  4762. {
  4763. Control->Width = RemainingWidth / 2;
  4764. }
  4765. }
  4766. Control->Visible = value;
  4767. if (WasFocused && Visible)
  4768. {
  4769. DirView(osCurrent)->SetFocus();
  4770. }
  4771. FixControlsPlacement();
  4772. }
  4773. }
  4774. //---------------------------------------------------------------------------
  4775. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4776. {
  4777. TControl * Control = GetComponent(Component);
  4778. if (Control == NULL)
  4779. {
  4780. return false;
  4781. }
  4782. else
  4783. {
  4784. return Control->Visible;
  4785. }
  4786. }
  4787. //---------------------------------------------------------------------------
  4788. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4789. {
  4790. bool Result;
  4791. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4792. {
  4793. Result = !IsUWP();
  4794. }
  4795. else
  4796. {
  4797. Result = true;
  4798. }
  4799. return Result;
  4800. }
  4801. //---------------------------------------------------------------------------
  4802. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4803. {
  4804. if (DirView(osOther)->ItemFocused != NULL)
  4805. {
  4806. DirView(osOther)->ItemFocused->MakeVisible(false);
  4807. }
  4808. QueueSplitter->Visible = QueuePanel->Visible;
  4809. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4810. }
  4811. //---------------------------------------------------------------------------
  4812. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4813. {
  4814. switch (Component) {
  4815. case fcStatusBar: return RemoteStatusBar;
  4816. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4817. case fcQueueView: return QueuePanel;
  4818. case fcQueueToolbar: return QueueDock;
  4819. case fcRemoteTree: return RemoteDrivePanel;
  4820. case fcSessionsTabs: return SessionsPageControl;
  4821. default: return NULL;
  4822. }
  4823. }
  4824. //---------------------------------------------------------------------------
  4825. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4826. TObject *Sender, TListColumn *Column, TPoint &Point)
  4827. {
  4828. DebugAssert(NonVisualDataModule && Column && Sender);
  4829. NonVisualDataModule->ListColumn = Column;
  4830. TPopupMenu * DirViewColumnMenu;
  4831. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4832. {
  4833. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4834. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4835. (Column->Index == uvName);
  4836. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4837. (Column->Index == uvSize);
  4838. }
  4839. else
  4840. {
  4841. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4842. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4843. (Column->Index == dvName);
  4844. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4845. (Column->Index == dvSize);
  4846. }
  4847. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4848. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4849. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4850. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4851. }
  4852. //---------------------------------------------------------------------------
  4853. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4854. TObject *Sender, TListItem *Item, bool &AllowExec)
  4855. {
  4856. DoDirViewExecFile(Sender, Item, AllowExec);
  4857. }
  4858. //---------------------------------------------------------------------------
  4859. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4860. TListItem * Item, bool & AllowExec)
  4861. {
  4862. DebugAssert(Sender && Item && Configuration);
  4863. DebugAssert(AllowExec);
  4864. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4865. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4866. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4867. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4868. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4869. // on files only (not directories)
  4870. // and only when symlinks are resolved (apply to remote panel only)
  4871. if (!ADirView->ItemIsDirectory(Item) &&
  4872. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4873. {
  4874. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4875. !FForceExecution &&
  4876. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4877. {
  4878. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4879. {
  4880. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4881. UnlockWindow();
  4882. try
  4883. {
  4884. ExecuteFileOperation(
  4885. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4886. }
  4887. __finally
  4888. {
  4889. LockWindow();
  4890. }
  4891. AllowExec = false;
  4892. }
  4893. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4894. {
  4895. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4896. {
  4897. ExecuteFile(Side, efDefaultEditor);
  4898. AllowExec = false;
  4899. }
  4900. }
  4901. else
  4902. {
  4903. DebugFail();
  4904. }
  4905. }
  4906. // if we have not done anything special, fall back to default behavior
  4907. if (AllowExec)
  4908. {
  4909. if (Remote && !WinConfiguration->DisableOpenEdit)
  4910. {
  4911. ExecuteFile(osRemote, efShell);
  4912. AllowExec = false;
  4913. }
  4914. }
  4915. }
  4916. }
  4917. //---------------------------------------------------------------------------
  4918. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4919. {
  4920. return ((Side == osRemote) || (Side == osCurrent));
  4921. }
  4922. //---------------------------------------------------------------------------
  4923. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4924. {
  4925. DebugFail();
  4926. }
  4927. //---------------------------------------------------------------------------
  4928. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4929. {
  4930. DebugFail();
  4931. }
  4932. //---------------------------------------------------------------------------
  4933. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4934. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  4935. {
  4936. TSynchronizeParamType Params;
  4937. Params.LocalDirectory = LocalDirectory;
  4938. Params.RemoteDirectory = RemoteDirectory;
  4939. int UnusedParams =
  4940. (GUIConfiguration->SynchronizeParams &
  4941. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4942. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4943. Params.Options = GUIConfiguration->SynchronizeOptions;
  4944. bool SaveSettings = false;
  4945. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4946. &DoSynchronizeTooManyDirectories);
  4947. DebugAssert(FSynchronizeController == NULL);
  4948. FSynchronizeController = &Controller;
  4949. bool Result;
  4950. try
  4951. {
  4952. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4953. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4954. int Options =
  4955. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4956. DebugAssert(FOnFeedSynchronizeError == NULL);
  4957. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4958. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4959. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4960. if (Result)
  4961. {
  4962. if (SaveSettings)
  4963. {
  4964. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4965. GUIConfiguration->SynchronizeOptions = Params.Options;
  4966. }
  4967. else
  4968. {
  4969. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4970. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4971. FLAGSET(Params.Options, soSynchronize))
  4972. {
  4973. GUIConfiguration->SynchronizeOptions =
  4974. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4975. soSynchronize;
  4976. }
  4977. }
  4978. LocalDirectory = Params.LocalDirectory;
  4979. RemoteDirectory = Params.RemoteDirectory;
  4980. }
  4981. }
  4982. __finally
  4983. {
  4984. FSynchronizeController = NULL;
  4985. DebugAssert(FOnFeedSynchronizeError == NULL);
  4986. FOnFeedSynchronizeError = NULL;
  4987. }
  4988. return Result;
  4989. }
  4990. //---------------------------------------------------------------------------
  4991. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4992. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4993. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4994. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4995. TSynchronizeOptions * Options, bool Full)
  4996. {
  4997. // Keep-up-to-date mode
  4998. if (Terminal->Status == ssOpened)
  4999. {
  5000. try
  5001. {
  5002. int PParams = Params.Params;
  5003. if (!Full)
  5004. {
  5005. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5006. if (FLAGSET(Params.Options, soRecurse))
  5007. {
  5008. PParams |= TTerminal::spSubDirs;
  5009. }
  5010. }
  5011. else
  5012. {
  5013. // if keepuptodate is non-recursive,
  5014. // full sync before has to be non-recursive as well
  5015. if (FLAGCLEAR(Params.Options, soRecurse))
  5016. {
  5017. PParams |= TTerminal::spNoRecurse;
  5018. }
  5019. }
  5020. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5021. PParams, Checklist, Options);
  5022. }
  5023. catch (Exception & E)
  5024. {
  5025. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5026. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5027. {
  5028. // noop, already logged in MoreMessageDialog
  5029. }
  5030. else
  5031. {
  5032. ShowExtendedExceptionEx(Terminal, &E);
  5033. throw;
  5034. }
  5035. }
  5036. }
  5037. }
  5038. //---------------------------------------------------------------------------
  5039. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5040. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5041. const UnicodeString ErrorStr)
  5042. {
  5043. if (!Directory.IsEmpty())
  5044. {
  5045. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5046. }
  5047. else
  5048. {
  5049. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5050. }
  5051. }
  5052. //---------------------------------------------------------------------------
  5053. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5054. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5055. {
  5056. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5057. {
  5058. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5059. }
  5060. else
  5061. {
  5062. TMessageParams Params(mpNeverAskAgainCheck);
  5063. unsigned int Result = MessageDialog(
  5064. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5065. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5066. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5067. {
  5068. MaxDirectories *= 2;
  5069. if (Result == qaNeverAskAgain)
  5070. {
  5071. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5072. }
  5073. }
  5074. else
  5075. {
  5076. Abort();
  5077. }
  5078. }
  5079. }
  5080. //---------------------------------------------------------------------------
  5081. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5082. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5083. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5084. TSynchronizeOptions * Options)
  5085. {
  5086. // Keep-up-to-date mode
  5087. DebugAssert(!FAutoOperation);
  5088. void * BatchStorage;
  5089. BatchStart(BatchStorage);
  5090. FAutoOperation = true;
  5091. bool AnyOperation = false;
  5092. TDateTime StartTime = Now();
  5093. TSynchronizeChecklist * AChecklist = NULL;
  5094. try
  5095. {
  5096. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5097. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5098. {
  5099. FSynchronizeProgressForm->Start();
  5100. }
  5101. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5102. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5103. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5104. Options);
  5105. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5106. SAFE_DESTROY(FSynchronizeProgressForm);
  5107. AnyOperation = (AChecklist->CheckedCount > 0);
  5108. if (AnyOperation)
  5109. {
  5110. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5111. CreateProgressForm(&SynchronizeProgress);
  5112. try
  5113. {
  5114. Terminal->SynchronizeApply(
  5115. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5116. SynchronizeProcessedItem, NULL, NULL, NULL);
  5117. }
  5118. __finally
  5119. {
  5120. DestroyProgressForm();
  5121. }
  5122. }
  5123. }
  5124. __finally
  5125. {
  5126. if (Checklist == NULL)
  5127. {
  5128. delete AChecklist;
  5129. }
  5130. else
  5131. {
  5132. *Checklist = AChecklist;
  5133. }
  5134. FAutoOperation = false;
  5135. SAFE_DESTROY(FSynchronizeProgressForm);
  5136. BatchEnd(BatchStorage);
  5137. ReloadLocalDirectory();
  5138. if (AnyOperation)
  5139. {
  5140. OperationComplete(StartTime);
  5141. }
  5142. }
  5143. }
  5144. //---------------------------------------------------------------------------
  5145. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5146. {
  5147. // can be called from command line
  5148. return Visible &&
  5149. ((DirView(osOther)->SelCount > 0) ||
  5150. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5151. }
  5152. //---------------------------------------------------------------------------
  5153. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5154. {
  5155. LogSynchronizeEvent(Terminal, Message);
  5156. }
  5157. //---------------------------------------------------------------------------
  5158. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5159. int Params, TSynchronizeOptions & Options)
  5160. {
  5161. DebugAssert(!IsLocalBrowserMode());
  5162. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5163. {
  5164. Options.Filter = new TStringList();
  5165. Options.Filter->CaseSensitive = false;
  5166. Options.Filter->Duplicates = Types::dupAccept;
  5167. if (DirView(osRemote)->SelCount > 0)
  5168. {
  5169. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5170. }
  5171. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5172. {
  5173. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5174. }
  5175. Options.Filter->Sort();
  5176. }
  5177. }
  5178. //---------------------------------------------------------------------------
  5179. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5180. {
  5181. TCopyParamType Defaults;
  5182. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5183. ConfigStorage->AccessMode = smRead;
  5184. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5185. {
  5186. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5187. }
  5188. ConfigStorage.reset(NULL);
  5189. std::unique_ptr<TStringList> Options(new TStringList());
  5190. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5191. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5192. UnicodeString Result;
  5193. if (Options->Count > 0)
  5194. {
  5195. Result =
  5196. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5197. }
  5198. return Result;
  5199. }
  5200. //---------------------------------------------------------------------------
  5201. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5202. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5203. {
  5204. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5205. UnicodeString AdditionalParams =
  5206. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5207. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5208. SerializeCopyParamForCommandLine(CopyParams),
  5209. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5210. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5211. ExecuteNewInstance(SessionName, AdditionalParams);
  5212. }
  5213. //---------------------------------------------------------------------------
  5214. struct TSynchronizeParams
  5215. {
  5216. UnicodeString LocalDirectory;
  5217. UnicodeString RemoteDirectory;
  5218. TSynchronizeMode Mode;
  5219. TSynchronizeChecklist * Checklist;
  5220. int Params;
  5221. TCopyParamType * CopyParam;
  5222. TDateTime CollectElapsed;
  5223. TDateTime * StartTime;
  5224. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5225. };
  5226. //---------------------------------------------------------------------------
  5227. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5228. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5229. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5230. {
  5231. DebugAssert(!FAutoOperation);
  5232. void * BatchStorage;
  5233. BatchStart(BatchStorage);
  5234. FAutoOperation = true;
  5235. TFileOperationStatistics Statistics;
  5236. TDateTime Start = Now();
  5237. try
  5238. {
  5239. Params.OnProcessedItem = OnProcessedItem;
  5240. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5241. CreateProgressForm(&SynchronizeProgress);
  5242. Terminal->SynchronizeApply(
  5243. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5244. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5245. &Statistics);
  5246. }
  5247. __finally
  5248. {
  5249. Params.OnProcessedItem = NULL;
  5250. FAutoOperation = false;
  5251. DestroyProgressForm();
  5252. BatchEnd(BatchStorage);
  5253. ReloadLocalDirectory();
  5254. }
  5255. if (WinConfiguration->SynchronizeSummary)
  5256. {
  5257. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5258. // The statistics should be 0 anyway in this case
  5259. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5260. {
  5261. Message += L"\n";
  5262. if (Statistics.FilesUploaded > 0)
  5263. {
  5264. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5265. }
  5266. if (Statistics.FilesDownloaded > 0)
  5267. {
  5268. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5269. }
  5270. if (Statistics.FilesDeletedLocal > 0)
  5271. {
  5272. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5273. }
  5274. if (Statistics.FilesDeletedRemote > 0)
  5275. {
  5276. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5277. }
  5278. }
  5279. TDateTime Elapsed = (Now() - Start);
  5280. Message +=
  5281. L"\n" +
  5282. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5283. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5284. TMessageParams Params(mpNeverAskAgainCheck);
  5285. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5286. if (Result == qaNeverAskAgain)
  5287. {
  5288. WinConfiguration->SynchronizeSummary = false;
  5289. }
  5290. }
  5291. }
  5292. //---------------------------------------------------------------------------
  5293. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5294. {
  5295. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5296. {
  5297. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5298. }
  5299. // Not set in keep-up-to-date mode - Synchronize() method
  5300. if (Token != NULL)
  5301. {
  5302. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5303. if (Params.OnProcessedItem != NULL)
  5304. {
  5305. Params.OnProcessedItem(NULL, ChecklistItem);
  5306. }
  5307. }
  5308. }
  5309. //---------------------------------------------------------------------------
  5310. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5311. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5312. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5313. {
  5314. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5315. *Params.StartTime = Now();
  5316. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5317. }
  5318. //---------------------------------------------------------------------------
  5319. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5320. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5321. {
  5322. // terminal can be already closed (e.g. dropped connection)
  5323. if (Terminal != NULL)
  5324. {
  5325. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5326. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5327. }
  5328. }
  5329. //---------------------------------------------------------------------------
  5330. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5331. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5332. {
  5333. DebugAssert(!IsLocalBrowserMode());
  5334. TAutoBatch AutoBatch(this);
  5335. TAutoFlag AutoOperationFlag(FAutoOperation);
  5336. if (Side == osRemote)
  5337. {
  5338. std::unique_ptr<TStrings> FileList(new TStringList());
  5339. FileList->AddObject(FileName, RemoteFile);
  5340. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5341. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5342. RemoteDirView->SaveSelection();
  5343. RemoteDirView->SaveSelectedNames();
  5344. try
  5345. {
  5346. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5347. }
  5348. catch(...)
  5349. {
  5350. RemoteDirView->DiscardSavedSelection();
  5351. throw;
  5352. }
  5353. RemoteDirView->RestoreSelection();
  5354. }
  5355. else if (DebugAlwaysTrue(Side == osLocal))
  5356. {
  5357. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5358. {
  5359. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5360. }
  5361. UnicodeString Directory = ExtractFileDir(FileName);
  5362. ReloadLocalDirectory(Directory);
  5363. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5364. if (!SamePaths(Directory, NewDirectory))
  5365. {
  5366. ReloadLocalDirectory(NewDirectory);
  5367. }
  5368. }
  5369. }
  5370. //---------------------------------------------------------------------------
  5371. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5372. {
  5373. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5374. if (Side == osLocal)
  5375. {
  5376. if (Action == TSynchronizeChecklist::saDownloadNew)
  5377. {
  5378. OpenFolderInExplorer(LocalPath);
  5379. }
  5380. else
  5381. {
  5382. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5383. }
  5384. }
  5385. else if (DebugAlwaysTrue(Side == osRemote))
  5386. {
  5387. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5388. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5389. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5390. if (!LocalPath.IsEmpty())
  5391. {
  5392. SessionData->LocalDirectory = LocalPath;
  5393. }
  5394. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5395. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5396. }
  5397. }
  5398. //---------------------------------------------------------------------------
  5399. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5400. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5401. const TCopyParamType * CopyParams)
  5402. {
  5403. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5404. UnicodeString AdditionalParams =
  5405. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5406. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5407. SerializeCopyParamForCommandLine(CopyParams),
  5408. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5409. LocalDirectory, RemoteDirectory, Mode, Params));
  5410. ExecuteNewInstance(SessionName, AdditionalParams);
  5411. }
  5412. //---------------------------------------------------------------------------
  5413. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5414. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5415. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5416. {
  5417. int Result;
  5418. bool SaveSettings = false;
  5419. int Options =
  5420. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5421. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5422. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5423. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5424. bool Continue =
  5425. (UseDefaults == 0) ||
  5426. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5427. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5428. if (Continue)
  5429. {
  5430. Configuration->Usage->Inc(L"Synchronizations");
  5431. UpdateCopyParamCounters(CopyParam);
  5432. TSynchronizeOptions SynchronizeOptions;
  5433. GetSynchronizeOptions(Params, SynchronizeOptions);
  5434. if (SaveSettings)
  5435. {
  5436. GUIConfiguration->SynchronizeParams = Params;
  5437. }
  5438. else
  5439. {
  5440. SaveMode = false;
  5441. }
  5442. TDateTime StartTime = Now();
  5443. TSynchronizeChecklist * Checklist = NULL;
  5444. try
  5445. {
  5446. DebugAssert(!FAutoOperation);
  5447. FAutoOperation = true;
  5448. try
  5449. {
  5450. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5451. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5452. int Files = -1;
  5453. if (DataList->Count >= 1)
  5454. {
  5455. Files = StrToIntDef(DataList->Strings[0], Files);
  5456. }
  5457. else
  5458. {
  5459. DataList->Add(UnicodeString());
  5460. }
  5461. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5462. FSynchronizeProgressForm->Start();
  5463. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5464. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5465. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5466. &SynchronizeOptions);
  5467. if (Terminal->SessionData->CacheDirectories)
  5468. {
  5469. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5470. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5471. }
  5472. }
  5473. __finally
  5474. {
  5475. FAutoOperation = false;
  5476. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5477. SAFE_DESTROY(FSynchronizeProgressForm);
  5478. }
  5479. TSynchronizeParams SynchronizeParams;
  5480. SynchronizeParams.LocalDirectory = LocalDirectory;
  5481. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5482. SynchronizeParams.Mode = Mode;
  5483. SynchronizeParams.CopyParam = &CopyParam;
  5484. SynchronizeParams.Params = Params;
  5485. SynchronizeParams.Checklist = Checklist;
  5486. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5487. SynchronizeParams.StartTime = &StartTime;
  5488. SynchronizeParams.OnProcessedItem = NULL;
  5489. Result = Checklist->Count;
  5490. if (Checklist->Count > 0)
  5491. {
  5492. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5493. {
  5494. if (!DoSynchronizeChecklistDialog(
  5495. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5496. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5497. {
  5498. Result = -1;
  5499. }
  5500. }
  5501. else
  5502. {
  5503. FullSynchronize(SynchronizeParams, NULL, NULL);
  5504. }
  5505. }
  5506. else
  5507. {
  5508. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5509. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5510. }
  5511. }
  5512. __finally
  5513. {
  5514. delete Checklist;
  5515. }
  5516. OperationComplete(StartTime);
  5517. }
  5518. else
  5519. {
  5520. Result = -1;
  5521. }
  5522. return Result;
  5523. }
  5524. //---------------------------------------------------------------------------
  5525. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5526. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5527. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5528. {
  5529. if (Collect)
  5530. {
  5531. DebugAssert(FSynchronizeProgressForm != NULL);
  5532. if (!FSynchronizeProgressForm->Started)
  5533. {
  5534. FSynchronizeProgressForm->Start();
  5535. }
  5536. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5537. SetTaskbarListProgressValue(CompareProgress);
  5538. }
  5539. else
  5540. {
  5541. DebugAssert(FSynchronizeProgressForm == NULL);
  5542. DebugAssert(FProgressForm != NULL);
  5543. }
  5544. }
  5545. //---------------------------------------------------------------------------
  5546. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5547. {
  5548. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5549. TRemoteFile * File = NULL;
  5550. Terminal->ReadFile(FullFileName, File);
  5551. if (File != NULL)
  5552. {
  5553. std::unique_ptr<TRemoteFile> FileOwner(File);
  5554. TAutoFlag Flag(FStandaloneEditing);
  5555. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5556. Application->ShowMainForm = false;
  5557. Application->Run();
  5558. }
  5559. }
  5560. //---------------------------------------------------------------------------
  5561. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5562. {
  5563. DebugFail();
  5564. }
  5565. //---------------------------------------------------------------------------
  5566. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5567. {
  5568. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5569. SessionData->Assign(Terminal->SessionData);
  5570. UpdateSessionData(SessionData.get());
  5571. Terminal->UpdateSessionCredentials(SessionData.get());
  5572. if (!Terminal->SessionData->HasSessionName())
  5573. {
  5574. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5575. // once IsWorkspace is cleared
  5576. SessionData->Name = UnicodeString();
  5577. SessionData->IsWorkspace = false;
  5578. }
  5579. return SessionData.release();
  5580. }
  5581. //---------------------------------------------------------------------------
  5582. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5583. {
  5584. TSessionData * SessionData = CloneCurrentSessionData();
  5585. try
  5586. {
  5587. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5588. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5589. }
  5590. __finally
  5591. {
  5592. delete SessionData;
  5593. }
  5594. }
  5595. //---------------------------------------------------------------------------
  5596. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5597. {
  5598. TTerminalManager * Manager = TTerminalManager::Instance();
  5599. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5600. if (DebugAlwaysTrue(Terminal != NULL))
  5601. {
  5602. // Update (Managed)Terminal->StateData
  5603. UpdateTerminal(Terminal);
  5604. }
  5605. Manager->SaveWorkspace(DataList.get());
  5606. return DataList.release();
  5607. }
  5608. //---------------------------------------------------------------------------
  5609. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5610. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5611. {
  5612. WinConfiguration->LastWorkspace = Name;
  5613. if (!SavePasswords)
  5614. {
  5615. for (int Index = 0; Index < DataList->Count; Index++)
  5616. {
  5617. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5618. if (SessionData->Link.IsEmpty())
  5619. {
  5620. SessionData->ClearSessionPasswords();
  5621. }
  5622. }
  5623. }
  5624. StoredSessions->NewWorkspace(Name, DataList);
  5625. // modified only
  5626. StoredSessions->Save(false, Explicit);
  5627. }
  5628. //---------------------------------------------------------------------------
  5629. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5630. {
  5631. return
  5632. DefaultStr(WinConfiguration->LastWorkspace,
  5633. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5634. }
  5635. //---------------------------------------------------------------------------
  5636. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5637. {
  5638. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5639. bool AnyNonStoredSessionWithPassword = false;
  5640. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5641. bool AllNonStoredSessionsAnonymous = true;
  5642. TTerminalManager * Manager = TTerminalManager::Instance();
  5643. for (int Index = 0; Index < DataList->Count; Index++)
  5644. {
  5645. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5646. if (SessionData->Link.IsEmpty())
  5647. {
  5648. if (SessionData->HasAnySessionPassword())
  5649. {
  5650. AnyNonStoredSessionWithPassword = true;
  5651. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5652. {
  5653. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5654. }
  5655. }
  5656. if (!SameText(SessionData->UserName, AnonymousUserName))
  5657. {
  5658. AllNonStoredSessionsAnonymous = false;
  5659. }
  5660. }
  5661. }
  5662. bool SavePasswords;
  5663. bool * PSavePasswords;
  5664. bool NotRecommendedSavingPasswords =
  5665. !CustomWinConfiguration->UseMasterPassword &&
  5666. !AllNonStoredSessionsAnonymous;
  5667. if (Configuration->DisablePasswordStoring ||
  5668. !AnyNonStoredSessionWithPassword)
  5669. {
  5670. PSavePasswords = NULL;
  5671. SavePasswords = false;
  5672. }
  5673. else
  5674. {
  5675. PSavePasswords = &SavePasswords;
  5676. SavePasswords =
  5677. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5678. !NotRecommendedSavingPasswords;
  5679. }
  5680. UnicodeString Name = WorkspaceName();
  5681. bool CreateShortcut = false;
  5682. bool Result =
  5683. DoSaveWorkspaceDialog(
  5684. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5685. EnableAutoSave);
  5686. if (Result)
  5687. {
  5688. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5689. if (CreateShortcut)
  5690. {
  5691. TOperationVisualizer Visualizer;
  5692. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5693. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5694. }
  5695. if (EnableAutoSave)
  5696. {
  5697. WinConfiguration->AutoSaveWorkspace = true;
  5698. WinConfiguration->AutoWorkspace = Name;
  5699. if (PSavePasswords != NULL)
  5700. {
  5701. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5702. }
  5703. }
  5704. }
  5705. return Result;
  5706. }
  5707. //---------------------------------------------------------------------------
  5708. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5709. {
  5710. DebugAssert(!IsLocalBrowserMode());
  5711. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5712. if (WinConfiguration->PreservePanelState)
  5713. {
  5714. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5715. }
  5716. UpdateSessionData(Terminal->StateData);
  5717. }
  5718. //---------------------------------------------------------------------------
  5719. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5720. {
  5721. // Keep in sync with TSessionData::CopyStateData
  5722. DebugAssert(!IsLocalBrowserMode());
  5723. DebugAssert(Data != NULL);
  5724. // This is inconsistent with how color (for example) is handled.
  5725. Data->Name = Terminal->SessionData->Name;
  5726. // cannot use RemoteDirView->Path, because it is empty if connection
  5727. // was already closed
  5728. // also only peek, we may not be connected at all atm
  5729. // so make sure we do not try retrieving current directory from the server
  5730. // (particularly with FTP)
  5731. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5732. if (!ACurrentDirectory.IsEmpty())
  5733. {
  5734. Data->RemoteDirectory = ACurrentDirectory;
  5735. }
  5736. Data->Color = SessionColor;
  5737. }
  5738. //---------------------------------------------------------------------------
  5739. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5740. {
  5741. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5742. DebugAssert(Toolbar != NULL);
  5743. for (int i = 0; i < Toolbar->Items->Count; i++)
  5744. {
  5745. TTBXCustomDropDownItem * DropDownItem;
  5746. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5747. if (DropDownItem != NULL)
  5748. {
  5749. ToolbarItemResize(DropDownItem,
  5750. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5751. Toolbar->NonClientWidth);
  5752. break;
  5753. }
  5754. }
  5755. }
  5756. //---------------------------------------------------------------------------
  5757. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5758. {
  5759. Item->EditWidth = Width;
  5760. }
  5761. //---------------------------------------------------------------------------
  5762. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5763. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5764. {
  5765. for (int i = 0; i < Toolbar->Items->Count; i++)
  5766. {
  5767. TTBXCustomDropDownItem * DropDownItem;
  5768. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5769. if (DropDownItem != NULL)
  5770. {
  5771. ASize.x -= DropDownItem->EditWidth;
  5772. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5773. }
  5774. }
  5775. }
  5776. //---------------------------------------------------------------------------
  5777. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5778. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5779. {
  5780. if (WinConfiguration->DDWarnLackOfTempSpace)
  5781. {
  5782. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5783. if (!ADrive.IsEmpty())
  5784. {
  5785. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5786. DebugAssert(RequiredSpace >= 0);
  5787. __int64 RequiredWithReserve;
  5788. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5789. if (FreeSpace < RequiredWithReserve)
  5790. {
  5791. unsigned int Result;
  5792. TMessageParams Params(mpNeverAskAgainCheck);
  5793. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5794. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5795. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5796. if (Result == qaNeverAskAgain)
  5797. {
  5798. WinConfiguration->DDWarnLackOfTempSpace = false;
  5799. }
  5800. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5801. }
  5802. }
  5803. }
  5804. }
  5805. //---------------------------------------------------------------------------
  5806. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5807. {
  5808. DoOpenDirectoryDialog(odAddBookmark, Side);
  5809. }
  5810. //---------------------------------------------------------------------------
  5811. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5812. {
  5813. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5814. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5815. try
  5816. {
  5817. TCustomDirView * DView = DirView(Side);
  5818. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5819. {
  5820. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5821. }
  5822. }
  5823. catch (...)
  5824. {
  5825. delete VisitedDirectories;
  5826. throw;
  5827. }
  5828. return VisitedDirectories;
  5829. }
  5830. //---------------------------------------------------------------------------
  5831. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5832. TOpenDirectoryMode Mode, TOperationSide Side)
  5833. {
  5834. bool Continue = true;
  5835. if (Mode == odAddBookmark)
  5836. {
  5837. TMessageParams Params(mpNeverAskAgainCheck);
  5838. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5839. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5840. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5841. unsigned int Answer =
  5842. MessageDialog(Message,
  5843. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5844. if (Answer == qaNeverAskAgain)
  5845. {
  5846. Continue = true;
  5847. WinConfiguration->UseSharedBookmarks = true;
  5848. }
  5849. else if (Answer == qaOK)
  5850. {
  5851. Continue = true;
  5852. WinConfiguration->UseSharedBookmarks = false;
  5853. }
  5854. else
  5855. {
  5856. Continue = false;
  5857. }
  5858. }
  5859. if (Continue)
  5860. {
  5861. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5862. try
  5863. {
  5864. UnicodeString Name = DirView(Side)->PathName;
  5865. bool Repeat;
  5866. do
  5867. {
  5868. Repeat = false;
  5869. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5870. // do not allow switching to location profiles,
  5871. // if we are not connected
  5872. HasDirView[osLocal] && (Terminal != NULL)))
  5873. {
  5874. TWindowLock Lock(this);
  5875. if (!TryOpenDirectory(Side, Name))
  5876. {
  5877. Repeat = true;
  5878. Mode = odBrowse;
  5879. }
  5880. }
  5881. }
  5882. while (Repeat);
  5883. }
  5884. __finally
  5885. {
  5886. delete VisitedDirectories;
  5887. }
  5888. }
  5889. }
  5890. //---------------------------------------------------------------------------
  5891. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5892. {
  5893. DebugAssert(!FTerminal->CommandSessionOpened);
  5894. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5895. }
  5896. //---------------------------------------------------------------------------
  5897. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5898. {
  5899. bool Result = FTerminal->IsCapable[Capability] ||
  5900. FTerminal->CommandSessionOpened;
  5901. if (!Result)
  5902. {
  5903. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5904. if (!GUIConfiguration->ConfirmCommandSession)
  5905. {
  5906. Result = true;
  5907. }
  5908. else
  5909. {
  5910. TMessageParams Params(mpNeverAskAgainCheck);
  5911. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5912. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5913. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5914. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5915. if (Answer == qaNeverAskAgain)
  5916. {
  5917. GUIConfiguration->ConfirmCommandSession = false;
  5918. Result = true;
  5919. }
  5920. else if (Answer == qaOK)
  5921. {
  5922. Result = true;
  5923. }
  5924. }
  5925. if (Result)
  5926. {
  5927. Result = CommandSessionFallback();
  5928. }
  5929. }
  5930. return Result;
  5931. }
  5932. //---------------------------------------------------------------------------
  5933. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5934. {
  5935. if (EnsureCommandSessionFallback(fcAnyCommand))
  5936. {
  5937. DoConsoleDialog(Terminal, Command);
  5938. }
  5939. }
  5940. //---------------------------------------------------------------------------
  5941. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5942. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5943. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5944. {
  5945. if (!IsSideLocalBrowser(osRemote) &&
  5946. IsFileControl(DropSourceControl, osRemote) &&
  5947. (FDDExtMapFile != NULL))
  5948. {
  5949. Accept = true;
  5950. }
  5951. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5952. DebugAssert(FDDTargetControl != NULL);
  5953. }
  5954. //---------------------------------------------------------------------------
  5955. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5956. _di_IDataObject DataObj, int KeyState,
  5957. const TPoint & Point, int & Effect, bool & Accept)
  5958. {
  5959. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5960. }
  5961. //---------------------------------------------------------------------------
  5962. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5963. _di_IDataObject DataObj, int KeyState,
  5964. const TPoint & Point, int & Effect, bool & Accept)
  5965. {
  5966. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5967. }
  5968. //---------------------------------------------------------------------------
  5969. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5970. TObject * Sender)
  5971. {
  5972. DebugUsedParam(Sender);
  5973. DebugAssert(FDDTargetControl == Sender);
  5974. FDDTargetControl = NULL;
  5975. }
  5976. //---------------------------------------------------------------------------
  5977. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5978. {
  5979. FileControlDDDragLeave(SessionsPageControl);
  5980. }
  5981. //---------------------------------------------------------------------------
  5982. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5983. {
  5984. FileControlDDDragLeave(QueueView3);
  5985. }
  5986. //---------------------------------------------------------------------------
  5987. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5988. {
  5989. DebugAssert(!IsSideLocalBrowser(Side));
  5990. DebugUsedParam(Side);
  5991. bool Edit = false;
  5992. TRemoteFile * File = NULL;
  5993. UnicodeString FileName;
  5994. UnicodeString PointTo;
  5995. bool SymbolicLink = true;
  5996. if (RemoteDirView->ItemFocused)
  5997. {
  5998. DebugAssert(RemoteDirView->ItemFocused->Data);
  5999. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6000. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6001. if (Edit)
  6002. {
  6003. FileName = File->FileName;
  6004. PointTo = File->LinkTo;
  6005. }
  6006. else
  6007. {
  6008. PointTo = File->FileName;
  6009. }
  6010. }
  6011. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6012. Terminal->IsCapable[fcHardLink]))
  6013. {
  6014. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6015. if (Edit)
  6016. {
  6017. DebugAssert(File->FileName == FileName);
  6018. int Params = dfNoRecursive;
  6019. Terminal->ExceptionOnFail = true;
  6020. try
  6021. {
  6022. Terminal->DeleteFile(L"", File, &Params);
  6023. }
  6024. __finally
  6025. {
  6026. Terminal->ExceptionOnFail = false;
  6027. }
  6028. }
  6029. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6030. }
  6031. }
  6032. //---------------------------------------------------------------------------
  6033. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6034. {
  6035. return
  6036. (IsSideLocalBrowser(Side) ||
  6037. ((Terminal != NULL) &&
  6038. Terminal->ResolvingSymlinks &&
  6039. Terminal->IsCapable[fcSymbolicLink]));
  6040. }
  6041. //---------------------------------------------------------------------------
  6042. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6043. {
  6044. return
  6045. !IsSideLocalBrowser(FCurrentSide) &&
  6046. (RemoteDirView->ItemFocused != NULL) &&
  6047. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6048. Terminal->SessionData->ResolveSymlinks;
  6049. }
  6050. //---------------------------------------------------------------------------
  6051. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6052. {
  6053. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6054. FForceExecution = true;
  6055. try
  6056. {
  6057. DirView(osCurrent)->ExecuteCurrentFile();
  6058. }
  6059. __finally
  6060. {
  6061. FForceExecution = false;
  6062. }
  6063. }
  6064. //---------------------------------------------------------------------------
  6065. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6066. {
  6067. TEditorData ExternalEditor;
  6068. ExternalEditor.Editor = edExternal;
  6069. bool Remember = false;
  6070. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6071. {
  6072. if (Remember)
  6073. {
  6074. TEditorList * EditorList = new TEditorList;
  6075. try
  6076. {
  6077. *EditorList = *WinConfiguration->EditorList;
  6078. bool Found = false;
  6079. int i = 0;
  6080. while (!Found && (i < EditorList->Count))
  6081. {
  6082. const TEditorPreferences * Editor = EditorList->Editors[i];
  6083. if ((Editor->Data->Editor == edExternal) &&
  6084. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6085. {
  6086. Found = true;
  6087. }
  6088. i++;
  6089. }
  6090. if (!Found)
  6091. {
  6092. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6093. WinConfiguration->EditorList = EditorList;
  6094. }
  6095. }
  6096. __finally
  6097. {
  6098. delete EditorList;
  6099. }
  6100. }
  6101. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6102. }
  6103. }
  6104. //---------------------------------------------------------------------------
  6105. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6106. {
  6107. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6108. if (FFileFindTerminal == ATerminal)
  6109. {
  6110. FFileFindTerminal = NULL;
  6111. HideFileFindDialog();
  6112. }
  6113. if (FClipboardTerminal == ATerminal)
  6114. {
  6115. ClipboardClear(); // implies ClipboardStop
  6116. }
  6117. }
  6118. //---------------------------------------------------------------------------
  6119. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6120. {
  6121. DetachTerminal(Sender);
  6122. }
  6123. //---------------------------------------------------------------------------
  6124. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6125. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6126. {
  6127. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6128. DebugAssert(Terminal != NULL);
  6129. if (Data->Terminal == Terminal)
  6130. {
  6131. Data->Terminal = NULL;
  6132. }
  6133. }
  6134. //---------------------------------------------------------------------------
  6135. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6136. {
  6137. UpdateControls();
  6138. SessionColor = TColor(0);
  6139. UpdateRemotePathComboBox(false);
  6140. NeedSession(false);
  6141. }
  6142. //---------------------------------------------------------------------------
  6143. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6144. {
  6145. try
  6146. {
  6147. // Cache, as the login dialog can change its value
  6148. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6149. try
  6150. {
  6151. if (ShowLogin)
  6152. {
  6153. bool ReloadSessions = !Startup;
  6154. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6155. }
  6156. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6157. // This detects if workspace was saved the last time the main widow was closed
  6158. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6159. {
  6160. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6161. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6162. }
  6163. }
  6164. __finally
  6165. {
  6166. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6167. // (so there was no chance for the user to open any session yet)
  6168. if ((ShowLogin || !Startup) &&
  6169. !WinConfiguration->KeepOpenWhenNoSession &&
  6170. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6171. {
  6172. TerminateApplication();
  6173. }
  6174. }
  6175. }
  6176. catch (EAbort &)
  6177. {
  6178. // swallow
  6179. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6180. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6181. }
  6182. }
  6183. //---------------------------------------------------------------------------
  6184. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6185. {
  6186. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6187. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6188. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6189. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6190. try
  6191. {
  6192. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6193. // Clear the design time unthemed tab
  6194. ((SessionsPageControl->PageCount > 0) &&
  6195. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6196. {
  6197. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6198. }
  6199. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6200. {
  6201. TThemeTabSheet * TabSheet;
  6202. if (Index >= SessionsPageControl->PageCount)
  6203. {
  6204. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6205. TabSheet->PageControl = SessionsPageControl;
  6206. }
  6207. else
  6208. {
  6209. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6210. }
  6211. bool IsSessionTab = (Index < TerminalList->Count);
  6212. if (IsSessionTab)
  6213. {
  6214. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6215. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6216. UpdateSessionTab(TabSheet);
  6217. }
  6218. else
  6219. {
  6220. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6221. TabSheet->Tag = 0; // not really needed
  6222. TabSheet->Shadowed = false;
  6223. // 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)
  6224. UpdateNewSessionTab();
  6225. }
  6226. TabSheet->ShowCloseButton = IsSessionTab;
  6227. }
  6228. }
  6229. __finally
  6230. {
  6231. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6232. }
  6233. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6234. }
  6235. //---------------------------------------------------------------------------
  6236. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6237. {
  6238. DoTerminalListChanged();
  6239. }
  6240. //---------------------------------------------------------------------------
  6241. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6242. {
  6243. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6244. DebugAssert(TabSheet->ImageIndex == 0);
  6245. TabSheet->Caption =
  6246. WinConfiguration->SelectiveToolbarText ?
  6247. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6248. UnicodeString();
  6249. }
  6250. //---------------------------------------------------------------------------
  6251. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6252. {
  6253. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6254. }
  6255. //---------------------------------------------------------------------------
  6256. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6257. {
  6258. if (DebugAlwaysTrue(TabSheet != NULL))
  6259. {
  6260. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6261. if (DebugAlwaysTrue(ATerminal != NULL))
  6262. {
  6263. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6264. TabSheet->ImageIndex = AddSessionColor(Color);
  6265. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6266. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6267. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6268. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6269. {
  6270. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6271. }
  6272. }
  6273. }
  6274. }
  6275. //---------------------------------------------------------------------------
  6276. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6277. {
  6278. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6279. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6280. bool Result = (Terminal != NULL);
  6281. if (Result)
  6282. {
  6283. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6284. }
  6285. else
  6286. {
  6287. try
  6288. {
  6289. NewSession(false);
  6290. }
  6291. __finally
  6292. {
  6293. DoTerminalListChanged();
  6294. }
  6295. FSessionsPageControlNewSessionTime = Now();
  6296. }
  6297. return Result;
  6298. }
  6299. //---------------------------------------------------------------------------
  6300. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6301. {
  6302. SessionTabSwitched();
  6303. }
  6304. //---------------------------------------------------------------------------
  6305. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6306. {
  6307. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6308. DebugAssert(TransferList != NULL);
  6309. UnicodeString Name;
  6310. if (TransferList->ItemIndex <= 0)
  6311. {
  6312. Name = L"";
  6313. }
  6314. else
  6315. {
  6316. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6317. }
  6318. if (FCopyParamAutoSelected.IsEmpty())
  6319. {
  6320. // if previous preset was not autoselected, make new preset the "default"
  6321. FCopyParamDefault = Name;
  6322. }
  6323. GUIConfiguration->CopyParamCurrent = Name;
  6324. }
  6325. //---------------------------------------------------------------------------
  6326. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6327. {
  6328. // sanity check
  6329. bool ExistingPreset =
  6330. (FTransferListHoverIndex >= 0) &&
  6331. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6332. DebugAssert(ExistingPreset);
  6333. if (ExistingPreset)
  6334. {
  6335. HDC DC = GetDC(0);
  6336. TCanvas * Canvas = new TCanvas();
  6337. try
  6338. {
  6339. Canvas->Handle = DC;
  6340. Canvas->Font = GetToolbarFont(this);
  6341. UnicodeString Name;
  6342. if (FTransferListHoverIndex == 0)
  6343. {
  6344. Name = L"";
  6345. }
  6346. else
  6347. {
  6348. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6349. }
  6350. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6351. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6352. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6353. static_cast<TObject*>(GetComponent(fcTransferList)));
  6354. UnicodeString InfoStr =
  6355. GUIConfiguration->CopyParamPreset[Name].
  6356. GetInfoStr(L"; ",
  6357. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6358. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6359. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6360. {
  6361. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6362. {
  6363. InfoStr.SetLength(InfoStr.Length() - 1);
  6364. }
  6365. InfoStr += Ellipsis;
  6366. }
  6367. // UpdateCaption does not cause invalidation of whole submenu, while
  6368. // setting Caption property does.
  6369. // also it probably does not resize the label, even if necessary
  6370. // (we do not want that anyway)
  6371. TransferLabel->UpdateCaption(InfoStr);
  6372. }
  6373. __finally
  6374. {
  6375. Canvas->Handle = NULL;
  6376. ReleaseDC(0, DC);
  6377. delete Canvas;
  6378. }
  6379. }
  6380. }
  6381. //---------------------------------------------------------------------------
  6382. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6383. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6384. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6385. {
  6386. if (FTransferListHoverIndex != AHoverIndex)
  6387. {
  6388. FTransferListHoverIndex = AHoverIndex;
  6389. UpdateTransferLabel();
  6390. }
  6391. }
  6392. //---------------------------------------------------------------------------
  6393. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6394. {
  6395. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6396. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6397. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6398. {
  6399. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6400. {
  6401. if (!NonVisualDataModule->Busy)
  6402. {
  6403. OpenDirectory(GetSide(osCurrent));
  6404. }
  6405. Message.Result = 1;
  6406. }
  6407. else
  6408. {
  6409. TForm::Dispatch(&Message);
  6410. }
  6411. }
  6412. else
  6413. {
  6414. TForm::Dispatch(&Message);
  6415. }
  6416. }
  6417. //---------------------------------------------------------------------------
  6418. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6419. {
  6420. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6421. if ((FIgnoreNextDialogChar != 0) &&
  6422. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6423. {
  6424. Message.Result = 1;
  6425. }
  6426. else
  6427. {
  6428. TForm::Dispatch(&Message);
  6429. }
  6430. FIgnoreNextDialogChar = 0;
  6431. }
  6432. //---------------------------------------------------------------------------
  6433. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6434. {
  6435. // The four low-order bits are used internally by Windows
  6436. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6437. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6438. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6439. // 61730 - restore thru double click - undocumented
  6440. // 61490 - maximize thru double click - undocumented
  6441. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6442. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6443. {
  6444. SysResizing(Cmd);
  6445. }
  6446. TForm::Dispatch(&Message);
  6447. }
  6448. //---------------------------------------------------------------------------
  6449. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6450. {
  6451. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6452. // Also there no point returning TRUE as we are not able to
  6453. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6454. // Hence the process termination might be safer :)
  6455. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6456. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6457. {
  6458. Message.Result = FALSE;
  6459. }
  6460. else
  6461. {
  6462. Message.Result = TRUE;
  6463. }
  6464. // Do not call default handling as that triggers OnCloseQuery,
  6465. // where our implementation will popup confirmation dialogs, what we do not want,
  6466. // as per Vista guidelines:
  6467. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6468. }
  6469. //---------------------------------------------------------------------------
  6470. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6471. {
  6472. if (Message.EndSession && IsApplicationMinimized())
  6473. {
  6474. // WORKAROUND
  6475. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6476. // when it receives WM_ENDSESSION,
  6477. // but that sometimes (particularly when application is minimized) cause crashes.
  6478. // Crash popups message that blocks log off.
  6479. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6480. // so we call ExitProcess() immediately, not even trying to cleanup.
  6481. // It still causes beep, so there's likely some popup, but it does not block
  6482. // log off.
  6483. ExitProcess(0);
  6484. }
  6485. TForm::Dispatch(&Message);
  6486. }
  6487. #ifdef _DEBUG
  6488. //---------------------------------------------------------------------------
  6489. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6490. {
  6491. TForm::Dispatch(&Message);
  6492. }
  6493. #endif
  6494. //---------------------------------------------------------------------------
  6495. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6496. {
  6497. }
  6498. //---------------------------------------------------------------------------
  6499. void __fastcall TCustomScpExplorerForm::DoShow()
  6500. {
  6501. // only now are the controls resized finally, so the size constraints
  6502. // will not conflict with possibly very small window size
  6503. RestoreFormParams();
  6504. // This indicates that the form was rescaled after parameters were loaded and
  6505. // as most probably the parameters were actually saved with the current form scaling
  6506. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6507. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6508. // and no rounding problems will occur.
  6509. // See also RestoreForm.
  6510. // (we should be safe to call RestoreParams unconditionally)
  6511. if (PixelsPerInch != Screen->PixelsPerInch)
  6512. {
  6513. RestoreParams();
  6514. }
  6515. FixControlsPlacement();
  6516. if (Position == poDefaultPosOnly)
  6517. {
  6518. CutFormToDesktop(this);
  6519. }
  6520. TForm::DoShow();
  6521. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6522. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6523. if (Terminal == NULL)
  6524. {
  6525. StartingDisconnected();
  6526. }
  6527. UpdatePixelsPerInchMainWindowCounter();
  6528. FShowing = true;
  6529. }
  6530. //---------------------------------------------------------------------------
  6531. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6532. {
  6533. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6534. }
  6535. //---------------------------------------------------------------------------
  6536. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6537. {
  6538. DoTerminalListChanged();
  6539. }
  6540. //---------------------------------------------------------------------------
  6541. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6542. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6543. TNotifyEvent OnBalloonClick, TObject * UserData)
  6544. {
  6545. bool Do;
  6546. UnicodeString Message;
  6547. if (E == NULL)
  6548. {
  6549. Message = Str;
  6550. Do = true;
  6551. }
  6552. else
  6553. {
  6554. Do = ExceptionMessage(E, Message);
  6555. }
  6556. if (Do && WinConfiguration->BalloonNotifications)
  6557. {
  6558. UnicodeString Title;
  6559. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6560. {
  6561. Message = TrimLeft(Message);
  6562. }
  6563. else
  6564. {
  6565. Message = UnformatMessage(Message);
  6566. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6567. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6568. Title = LoadResourceString(Captions[Type]);
  6569. if (Terminal != NULL)
  6570. {
  6571. Title = FORMAT(L"%s - %s",
  6572. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6573. }
  6574. }
  6575. if (Seconds == 0)
  6576. {
  6577. Seconds = WinConfiguration->NotificationsTimeout;
  6578. }
  6579. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6580. }
  6581. }
  6582. //---------------------------------------------------------------------------
  6583. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6584. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6585. UnicodeString HelpKeyword, const TMessageParams * Params,
  6586. TTerminal * Terminal)
  6587. {
  6588. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6589. (FOnFeedSynchronizeError != NULL)) &&
  6590. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6591. {
  6592. if (FOnFeedSynchronizeError != NULL)
  6593. {
  6594. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6595. }
  6596. else
  6597. {
  6598. DebugAssert(FErrorList != NULL);
  6599. TStringList * MoreMessagesCopy = NULL;
  6600. if (MoreMessages)
  6601. {
  6602. MoreMessagesCopy = new TStringList();
  6603. MoreMessagesCopy->Assign(MoreMessages);
  6604. }
  6605. FErrorList->AddObject(Message, MoreMessagesCopy);
  6606. }
  6607. PopupTrayBalloon(Terminal, Message, Type);
  6608. return ContinueAnswer(Answers);
  6609. }
  6610. else
  6611. {
  6612. bool UseBalloon = FTrayIcon->Visible;
  6613. if (UseBalloon)
  6614. {
  6615. PopupTrayBalloon(Terminal, Message, Type);
  6616. }
  6617. unsigned int Result;
  6618. try
  6619. {
  6620. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6621. }
  6622. __finally
  6623. {
  6624. // cancel only balloon we popped up, otherwise we may cancel notification
  6625. // balloon that was there before the message dialog
  6626. // (such as "dd confirmation opt in balloon)
  6627. if (UseBalloon)
  6628. {
  6629. FTrayIcon->CancelBalloon();
  6630. }
  6631. }
  6632. return Result;
  6633. }
  6634. }
  6635. //---------------------------------------------------------------------------
  6636. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6637. TTerminal * Terminal, Exception * E)
  6638. {
  6639. if (FTrayIcon->Visible)
  6640. {
  6641. PopupTrayBalloon(Terminal, L"", qtError, E);
  6642. }
  6643. // particularly prevent opening new session from jump list,
  6644. // while exception is shown
  6645. NonVisualDataModule->StartBusy();
  6646. try
  6647. {
  6648. ShowExtendedExceptionEx(Terminal, E);
  6649. }
  6650. __finally
  6651. {
  6652. NonVisualDataModule->EndBusy();
  6653. FTrayIcon->CancelBalloon();
  6654. }
  6655. }
  6656. //---------------------------------------------------------------------------
  6657. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6658. {
  6659. if (Terminal->Active)
  6660. {
  6661. InitStatusBar();
  6662. }
  6663. // cannot rely on active page being page for active terminal,
  6664. // as it can happen that active page is the "new session" page
  6665. // (e.g. when reconnecting active terminal, while login dialog
  6666. // invoked from "new session" page is modal)
  6667. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6668. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6669. }
  6670. //---------------------------------------------------------------------------
  6671. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6672. TTerminal * Terminal, Exception * E)
  6673. {
  6674. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6675. if (!Terminal->Active)
  6676. {
  6677. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6678. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6679. {
  6680. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6681. }
  6682. }
  6683. }
  6684. //---------------------------------------------------------------------------
  6685. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6686. UnicodeString Message, TQueryType Type,
  6687. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6688. {
  6689. if ((E == NULL) ||
  6690. ExceptionMessage(E, Message))
  6691. {
  6692. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6693. if (Important)
  6694. {
  6695. Seconds *= 5;
  6696. }
  6697. UnicodeString NoteMessage(UnformatMessage(Message));
  6698. if (Terminal != NULL)
  6699. {
  6700. NoteMessage = FORMAT(L"%s: %s",
  6701. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6702. }
  6703. if (WinConfiguration->BalloonNotifications)
  6704. {
  6705. AddNote(NoteMessage);
  6706. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6707. }
  6708. else
  6709. {
  6710. FlashOnBackground();
  6711. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6712. }
  6713. }
  6714. }
  6715. //---------------------------------------------------------------------------
  6716. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6717. {
  6718. RestoreApp();
  6719. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6720. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6721. !NonVisualDataModule->Busy)
  6722. {
  6723. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6724. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6725. if (!ComponentVisible[fcQueueView])
  6726. {
  6727. ToggleQueueVisibility();
  6728. GoToQueue();
  6729. }
  6730. }
  6731. }
  6732. //---------------------------------------------------------------------------
  6733. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6734. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6735. {
  6736. UnicodeString Message;
  6737. TNotifyEvent OnClick = NULL;
  6738. TObject * UserData = NULL;
  6739. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6740. switch (Event)
  6741. {
  6742. case qeEmptyButMonitored:
  6743. if ((ATerminal != Terminal) || QueueInvisible)
  6744. {
  6745. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6746. OnClick = QueueEmptyNoteClicked;
  6747. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6748. TerminalNoteData->Terminal = ATerminal;
  6749. UserData = TerminalNoteData;
  6750. }
  6751. break;
  6752. case qeEmpty:
  6753. OperationComplete(ATerminal->QueueOperationStart);
  6754. break;
  6755. case qePendingUserAction:
  6756. if ((ATerminal != Terminal) ||
  6757. (QueueInvisible && !IsQueueAutoPopup()))
  6758. {
  6759. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6760. }
  6761. break;
  6762. default:
  6763. DebugFail();
  6764. }
  6765. if (!Message.IsEmpty())
  6766. {
  6767. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6768. }
  6769. }
  6770. //---------------------------------------------------------------------------
  6771. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6772. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6773. {
  6774. if (FDDExtMapFile != NULL)
  6775. {
  6776. CloseHandle(FDDExtMapFile);
  6777. FDDExtMapFile = NULL;
  6778. }
  6779. if (WinConfiguration->DDFakeFile)
  6780. {
  6781. DDFakeFileInitDrag(FileList, Created);
  6782. }
  6783. }
  6784. //---------------------------------------------------------------------------
  6785. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6786. {
  6787. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6788. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6789. {
  6790. FFakeFileDropTarget = FileName;
  6791. }
  6792. }
  6793. //---------------------------------------------------------------------------
  6794. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6795. {
  6796. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6797. if (!ForceDirectories(ApiPath(Result)))
  6798. {
  6799. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6800. }
  6801. FileSetAttr(ApiPath(Result), faHidden);
  6802. return Result;
  6803. }
  6804. //---------------------------------------------------------------------------
  6805. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6806. bool & Created)
  6807. {
  6808. FFakeFileDropTarget = UnicodeString();
  6809. FDragFakeDirectory = CreateFakeTransferDirectory();
  6810. FileList->AddItem(NULL, FDragFakeDirectory);
  6811. Created = true;
  6812. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6813. {
  6814. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6815. }
  6816. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6817. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6818. {
  6819. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6820. TDragExtCommStruct* CommStruct;
  6821. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6822. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6823. DebugAssert(CommStruct != NULL);
  6824. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6825. CommStruct->Dragging = true;
  6826. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6827. LENOF(CommStruct->DropDest));
  6828. NULL_TERMINATE(CommStruct->DropDest);
  6829. UnmapViewOfFile(CommStruct);
  6830. }
  6831. }
  6832. //---------------------------------------------------------------------------
  6833. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6834. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6835. {
  6836. bool Result;
  6837. DebugAssert(!IsLocalBrowserMode());
  6838. if (Sender == RemoteDirView)
  6839. {
  6840. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6841. }
  6842. else
  6843. {
  6844. DebugAssert(Sender == RemoteDriveView);
  6845. TStrings * FileList = RemoteDriveView->DragFileList();
  6846. try
  6847. {
  6848. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6849. }
  6850. __finally
  6851. {
  6852. delete FileList;
  6853. }
  6854. }
  6855. if (FDDTargetControl == RemoteDriveView)
  6856. {
  6857. RemoteDriveView->UpdateDropTarget();
  6858. }
  6859. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6860. // or from tree view to dir view.
  6861. // foMove happens when file/dir is dragged from remote tree view outside of
  6862. // application via dragex
  6863. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6864. (DropSourceControl == RemoteDriveView))
  6865. {
  6866. RemoteDriveView->UpdateDropSource();
  6867. }
  6868. return Result;
  6869. }
  6870. //---------------------------------------------------------------------------
  6871. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6872. {
  6873. DebugAssert(!IsLocalBrowserMode());
  6874. // This also handles drops of remote files to queue.
  6875. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6876. SAFE_DESTROY(FDDFileList);
  6877. if (!FFakeFileDropTarget.IsEmpty())
  6878. {
  6879. RemoveDir(ApiPath(FFakeFileDropTarget));
  6880. }
  6881. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6882. {
  6883. try
  6884. {
  6885. TDragResult DDResult = (Sender == RemoteDirView) ?
  6886. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6887. // Focus is moved to the target application,
  6888. // but as we are going to present the UI, we need to steal the focus back.
  6889. // This most likely won't work though (windows does not allow application
  6890. // to steal focus most of the time)
  6891. Application->BringToFront();
  6892. // On older version of Windows we never got drMove here, see also comment below.
  6893. // On Windows 10, we get the "move".
  6894. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6895. {
  6896. UnicodeString TargetDirectory;
  6897. TFileOperation Operation;
  6898. // drInvalid may mean drMove, see comment below
  6899. switch (DDResult)
  6900. {
  6901. case drCopy:
  6902. Operation = foCopy;
  6903. break;
  6904. case drMove:
  6905. Operation = foMove;
  6906. break;
  6907. default:
  6908. DebugFail();
  6909. case drInvalid:
  6910. // prefer "copy" for safety
  6911. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6912. break;
  6913. }
  6914. TTransferOperationParam Param;
  6915. UnicodeString CounterName;
  6916. bool ForceQueue;
  6917. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6918. {
  6919. // we get drInvalid both if d&d was intercepted by ddext,
  6920. // and when users drops on no-drop location.
  6921. // we tell the difference by existence of response from ddext,
  6922. // so we ignore absence of response in this case
  6923. if (DDResult != drInvalid)
  6924. {
  6925. // here we know that the extension is installed,
  6926. // as it is checked as soon as drag&drop starts from
  6927. // RemoteFileControlDDCreateDragFileList
  6928. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6929. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6930. {
  6931. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6932. }
  6933. else
  6934. {
  6935. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6936. }
  6937. }
  6938. }
  6939. else
  6940. {
  6941. // download using fake file
  6942. Param.Temp = false;
  6943. Param.DragDrop = true;
  6944. if (ForceQueue)
  6945. {
  6946. Param.Queue = asOn;
  6947. }
  6948. if (Sender == RemoteDirView)
  6949. {
  6950. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6951. }
  6952. if (RemoteFileControlFileOperation(Sender, Operation,
  6953. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6954. {
  6955. Configuration->Usage->Inc(CounterName);
  6956. }
  6957. }
  6958. }
  6959. }
  6960. __finally
  6961. {
  6962. CloseHandle(FDDExtMapFile);
  6963. FDDExtMapFile = NULL;
  6964. delete FDragFakeMonitors;
  6965. FDragFakeMonitors = NULL;
  6966. RemoveDir(ApiPath(FDragFakeDirectory));
  6967. FDragFakeDirectory = L"";
  6968. FFakeFileDropTarget = UnicodeString();
  6969. }
  6970. }
  6971. if (FDragDropSshTerminate.get() != NULL)
  6972. {
  6973. FDragDropSshTerminate->Rethrow();
  6974. }
  6975. }
  6976. //---------------------------------------------------------------------------
  6977. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6978. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6979. {
  6980. // Remember drop effect so we know (when user drops files), if we copy or move
  6981. FLastDropEffect = dwEffect;
  6982. }
  6983. //---------------------------------------------------------------------------
  6984. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6985. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6986. {
  6987. bool Result;
  6988. if (FDDTargetControl == QueueView3)
  6989. {
  6990. Directory = DefaultDownloadTargetDirectory();
  6991. Result = true;
  6992. CounterName = L"DownloadsDragDropQueue";
  6993. ForceQueue = true;
  6994. }
  6995. else if (!FFakeFileDropTarget.IsEmpty())
  6996. {
  6997. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6998. Result = true;
  6999. ForceQueue = false;
  7000. CounterName = L"DownloadsDragDropFakeFile";
  7001. }
  7002. else
  7003. {
  7004. ForceQueue = false;
  7005. Enabled = false;
  7006. try
  7007. {
  7008. int Timer = 0;
  7009. Result = false;
  7010. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7011. {
  7012. {
  7013. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7014. TDragExtCommStruct* CommStruct;
  7015. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7016. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7017. DebugAssert(CommStruct != NULL);
  7018. Result = !CommStruct->Dragging;
  7019. if (Result)
  7020. {
  7021. Directory = ExtractFilePath(CommStruct->DropDest);
  7022. CounterName = L"DownloadsDragDropExternalExt";
  7023. }
  7024. UnmapViewOfFile(CommStruct);
  7025. }
  7026. if (!Result)
  7027. {
  7028. Sleep(50);
  7029. Timer += 50;
  7030. Application->ProcessMessages();
  7031. }
  7032. }
  7033. }
  7034. __finally
  7035. {
  7036. Enabled = true;
  7037. }
  7038. }
  7039. return Result;
  7040. }
  7041. //---------------------------------------------------------------------------
  7042. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7043. const UnicodeString TempDir, int SecDelay)
  7044. {
  7045. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7046. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7047. if (FDelayedDeletionTimer == NULL)
  7048. {
  7049. DebugAssert(HandleAllocated());
  7050. FDelayedDeletionTimer = new TTimer(this);
  7051. FDelayedDeletionTimer->Interval = 10000;
  7052. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7053. }
  7054. else
  7055. {
  7056. FDelayedDeletionTimer->Enabled = true;
  7057. }
  7058. }
  7059. //---------------------------------------------------------------------------
  7060. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7061. {
  7062. DebugAssert(FDelayedDeletionList != NULL);
  7063. TDateTime N = Now();
  7064. TDateTime Alert;
  7065. UnicodeString Directory;
  7066. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7067. {
  7068. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7069. if ((N >= Alert) || (Sender == NULL))
  7070. {
  7071. Directory = FDelayedDeletionList->Strings[Index];
  7072. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7073. {
  7074. FDelayedDeletionList->Delete(Index);
  7075. }
  7076. }
  7077. }
  7078. if (FDelayedDeletionList->Count == 0)
  7079. {
  7080. FDelayedDeletionTimer->Enabled = false;
  7081. }
  7082. }
  7083. //---------------------------------------------------------------------------
  7084. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7085. {
  7086. DebugAssert(!IsLocalBrowserMode());
  7087. if (IsFileControl(FDDTargetControl, osRemote) ||
  7088. (FDDTargetControl == SessionsPageControl))
  7089. {
  7090. TTerminal * TargetTerminal = NULL;
  7091. TFileOperation Operation = foNone;
  7092. if (FDDTargetControl == SessionsPageControl)
  7093. {
  7094. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7095. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7096. // do not allow dropping on the "+" tab
  7097. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7098. if (TargetTerminal != NULL)
  7099. {
  7100. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7101. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7102. {
  7103. Operation = foRemoteMove;
  7104. }
  7105. else
  7106. {
  7107. Operation = foRemoteCopy;
  7108. }
  7109. }
  7110. }
  7111. else
  7112. {
  7113. if (FLastDropEffect == DROPEFFECT_MOVE)
  7114. {
  7115. Operation = foRemoteMove;
  7116. }
  7117. else if (FLastDropEffect == DROPEFFECT_COPY)
  7118. {
  7119. Operation = foRemoteCopy;
  7120. }
  7121. }
  7122. if (Operation != foNone)
  7123. {
  7124. RemoteFileControlFileOperation(DropSourceControl,
  7125. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7126. }
  7127. // abort drag&drop
  7128. Abort();
  7129. }
  7130. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7131. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7132. (FDDTargetControl != QueueView3))
  7133. {
  7134. DebugAssert(!FDragTempDir.IsEmpty());
  7135. TTransferType Type;
  7136. UnicodeString TempDir = FDragTempDir;
  7137. // We clear FDragTempDir before calling
  7138. // just in case it fail (raises exception)
  7139. FDragTempDir = L"";
  7140. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7141. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7142. // empty directory parameter means temp directory -> don't display it!
  7143. UnicodeString TargetDir = L"";
  7144. int Options = 0;
  7145. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7146. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7147. {
  7148. Abort();
  7149. }
  7150. // TargetDir is set when dropped on local file control
  7151. // (this was workaround for legacy dirview event handling, now it should be
  7152. // made prettier)
  7153. if (TargetDir.IsEmpty())
  7154. {
  7155. TargetDir = TempDir;
  7156. if (ForceDirectories(ApiPath(TargetDir)))
  7157. {
  7158. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7159. FPendingTempSpaceWarn = true;
  7160. try
  7161. {
  7162. FDragDropOperation = true;
  7163. int Params = cpTemporary |
  7164. (Type == ttMove ? cpDelete : 0);
  7165. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7166. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7167. }
  7168. __finally
  7169. {
  7170. FDragDropOperation = false;
  7171. FPendingTempSpaceWarn = false;
  7172. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7173. }
  7174. }
  7175. else
  7176. {
  7177. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7178. }
  7179. }
  7180. }
  7181. }
  7182. //---------------------------------------------------------------------------
  7183. void __fastcall TCustomScpExplorerForm::DDDownload(
  7184. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7185. {
  7186. DebugAssert(!IsLocalBrowserMode());
  7187. TAutoBatch AutoBatch(this);
  7188. UpdateCopyParamCounters(*CopyParam);
  7189. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7190. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7191. {
  7192. RemoteDriveView->UpdateDropSource();
  7193. }
  7194. }
  7195. //---------------------------------------------------------------------------
  7196. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7197. {
  7198. public:
  7199. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7200. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7201. {
  7202. }
  7203. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7204. {
  7205. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7206. TDataObjectFilesEx::AllowData(FormatEtc);
  7207. }
  7208. };
  7209. //---------------------------------------------------------------------------
  7210. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7211. TObject * Sender, TDataObject *& DataObject)
  7212. {
  7213. if (FDDExtMapFile != NULL)
  7214. {
  7215. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7216. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7217. {
  7218. Abort();
  7219. }
  7220. if (FileList->Count > 0)
  7221. {
  7222. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7223. if (!FilesObject->IsValid(true, true))
  7224. {
  7225. FilesObject->_Release();
  7226. }
  7227. else
  7228. {
  7229. DataObject = FilesObject;
  7230. }
  7231. }
  7232. }
  7233. }
  7234. //---------------------------------------------------------------------------
  7235. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7236. {
  7237. DebugFail();
  7238. }
  7239. //---------------------------------------------------------------------------
  7240. void __fastcall TCustomScpExplorerForm::GoToTree()
  7241. {
  7242. ComponentVisible[fcRemoteTree] = true;
  7243. DriveView(osRemote)->SetFocus();
  7244. }
  7245. //---------------------------------------------------------------------------
  7246. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7247. TPanelExport Export)
  7248. {
  7249. TCustomDirView * DirView = this->DirView(Side);
  7250. std::unique_ptr<TStrings> ExportData(new TStringList());
  7251. switch (Export)
  7252. {
  7253. case pePath:
  7254. ExportData->Add(DirView->PathName);
  7255. break;
  7256. case peFileList:
  7257. case peFullFileList:
  7258. {
  7259. bool FullPath = (Export == peFullFileList);
  7260. DirView->CreateFileList(false, FullPath, ExportData.get());
  7261. for (int Index = 0; Index < ExportData->Count; Index++)
  7262. {
  7263. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7264. {
  7265. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7266. }
  7267. }
  7268. }
  7269. break;
  7270. default:
  7271. DebugFail();
  7272. }
  7273. return ExportData.release();
  7274. }
  7275. //---------------------------------------------------------------------------
  7276. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7277. TPanelExport Export, TPanelExportDestination Destination)
  7278. {
  7279. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7280. PanelExportStore(Side, Export, Destination, ExportData.get());
  7281. }
  7282. //---------------------------------------------------------------------------
  7283. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7284. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7285. TStrings * ExportData)
  7286. {
  7287. if (Destination == pedClipboard)
  7288. {
  7289. TInstantOperationVisualizer Visualizer;
  7290. CopyToClipboard(ExportData);
  7291. }
  7292. else
  7293. {
  7294. DebugFail();
  7295. }
  7296. }
  7297. //---------------------------------------------------------------------------
  7298. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7299. {
  7300. TCustomDirView * DirView = this->DirView(Side);
  7301. UnicodeString Mask = DirView->Mask;
  7302. if (DoFilterMaskDialog(DirView, Mask))
  7303. {
  7304. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7305. Configuration->Usage->Inc(L"Filters");
  7306. }
  7307. }
  7308. //---------------------------------------------------------------------------
  7309. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7310. {
  7311. return FQueueController->DefaultOperation();
  7312. }
  7313. //---------------------------------------------------------------------------
  7314. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7315. TQueueOperation Operation, void ** Param)
  7316. {
  7317. switch (Operation)
  7318. {
  7319. case qoPreferences:
  7320. return true;
  7321. case qoGoTo:
  7322. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7323. case qoOnceEmpty:
  7324. return IsAnythingQueued();
  7325. default:
  7326. return FQueueController->AllowOperation(Operation, Param);
  7327. }
  7328. }
  7329. //---------------------------------------------------------------------------
  7330. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7331. {
  7332. if (DebugAlwaysTrue(QueueView3->Visible))
  7333. {
  7334. QueueView3->SetFocus();
  7335. }
  7336. }
  7337. //---------------------------------------------------------------------------
  7338. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7339. TQueueOperation Operation, void * Param)
  7340. {
  7341. if (Operation == qoGoTo)
  7342. {
  7343. GoToQueue();
  7344. }
  7345. else if (Operation == qoPreferences)
  7346. {
  7347. PreferencesDialog(pmQueue);
  7348. }
  7349. else
  7350. {
  7351. FQueueController->ExecuteOperation(Operation, Param);
  7352. }
  7353. }
  7354. //---------------------------------------------------------------------------
  7355. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7356. {
  7357. return (Queue != NULL) && Queue->Enabled;
  7358. }
  7359. //---------------------------------------------------------------------------
  7360. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7361. {
  7362. DebugAssert(Queue != NULL);
  7363. if (Queue != NULL)
  7364. {
  7365. Queue->Enabled = !Queue->Enabled;
  7366. }
  7367. }
  7368. //---------------------------------------------------------------------------
  7369. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7370. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7371. {
  7372. FQueueActedItem = QueueView3->ItemFocused;
  7373. }
  7374. //---------------------------------------------------------------------------
  7375. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7376. {
  7377. return TopDock->Height + QueueSplitter->Height;
  7378. }
  7379. //---------------------------------------------------------------------------
  7380. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7381. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7382. {
  7383. // when queue is hidden by double-clicking splitter, stray attempt to
  7384. // resize the panel with strange value arrives, make sure it is ignored
  7385. if (ComponentVisible[fcQueueView])
  7386. {
  7387. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7388. RemotePanel->Constraints->MinHeight;
  7389. if (NewSize > HeightLimit)
  7390. {
  7391. NewSize = HeightLimit;
  7392. }
  7393. }
  7394. else
  7395. {
  7396. Accept = false;
  7397. }
  7398. }
  7399. //---------------------------------------------------------------------------
  7400. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7401. TDragObject *& /*DragObject*/)
  7402. {
  7403. FQueueActedItem = QueueView3->ItemFocused;
  7404. }
  7405. //---------------------------------------------------------------------------
  7406. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7407. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7408. {
  7409. Accept = true;
  7410. if (Source == QueueView3)
  7411. {
  7412. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7413. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7414. if (Accept)
  7415. {
  7416. TQueueItemProxy * QueueItem;
  7417. TQueueItemProxy * DestQueueItem;
  7418. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7419. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7420. Accept = (QueueItem != DestQueueItem) &&
  7421. (QueueItem->Status == TQueueItem::qsPending) &&
  7422. (DestQueueItem->Status == TQueueItem::qsPending);
  7423. }
  7424. }
  7425. }
  7426. //---------------------------------------------------------------------------
  7427. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7428. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7429. {
  7430. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7431. {
  7432. TQueueItemProxy * QueueItem;
  7433. TQueueItemProxy * DestQueueItem;
  7434. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7435. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7436. QueueItem->Move(DestQueueItem);
  7437. }
  7438. }
  7439. //---------------------------------------------------------------------------
  7440. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7441. {
  7442. if ((QueueView3->ItemFocused == NULL) &&
  7443. (QueueView3->Items->Count > 0))
  7444. {
  7445. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7446. }
  7447. QueueLabelUpdateStatus();
  7448. }
  7449. //---------------------------------------------------------------------------
  7450. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7451. {
  7452. QueueLabelUpdateStatus();
  7453. }
  7454. //---------------------------------------------------------------------------
  7455. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7456. {
  7457. QueueLabel->UpdateStatus();
  7458. }
  7459. //---------------------------------------------------------------------------
  7460. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7461. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7462. {
  7463. if (Selected)
  7464. {
  7465. NonVisualDataModule->UpdateNonVisibleActions();
  7466. }
  7467. }
  7468. //---------------------------------------------------------------------------
  7469. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7470. {
  7471. TDragDropFilesEx * Result = NULL;
  7472. if (Sender == SessionsPageControl)
  7473. {
  7474. Result = FSessionsDragDropFilesEx;
  7475. }
  7476. else if (Sender == QueueView3)
  7477. {
  7478. Result = FQueueDragDropFilesEx;
  7479. }
  7480. else
  7481. {
  7482. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7483. if (DirView != NULL)
  7484. {
  7485. Result = DirView->DragDropFilesEx;
  7486. }
  7487. else
  7488. {
  7489. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7490. if (DriveView != NULL)
  7491. {
  7492. Result = DriveView->DragDropFilesEx;
  7493. }
  7494. }
  7495. }
  7496. DebugAssert(Result != NULL);
  7497. return Result;
  7498. }
  7499. //---------------------------------------------------------------------------
  7500. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7501. {
  7502. return (DView->SelCount == DView->FilesCount);
  7503. }
  7504. //---------------------------------------------------------------------------
  7505. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7506. {
  7507. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7508. }
  7509. //---------------------------------------------------------------------------
  7510. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7511. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7512. {
  7513. DebugAssert(!IsLocalBrowserMode());
  7514. TFileOperation Operation;
  7515. switch (Effect)
  7516. {
  7517. case DROPEFFECT_MOVE:
  7518. Operation = foMove;
  7519. break;
  7520. case DROPEFFECT_COPY:
  7521. // occures on WinXP (reported by user)
  7522. default:
  7523. Operation = foCopy;
  7524. break;
  7525. };
  7526. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7527. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7528. if (DragDropFilesEx->FileList->Count > 0)
  7529. {
  7530. TStrings * FileList = new TStringList();
  7531. try
  7532. {
  7533. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7534. {
  7535. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7536. }
  7537. FDragDropOperation = true;
  7538. TTransferOperationParam Param;
  7539. Param.TargetDirectory = TargetPath;
  7540. // upload, no temp dirs
  7541. Param.Temp = false;
  7542. Param.DragDrop = DragDrop;
  7543. Param.Options =
  7544. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7545. if (ForceQueue)
  7546. {
  7547. Param.Queue = asOn;
  7548. }
  7549. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7550. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7551. {
  7552. if (IsFileControl(DropSourceControl, osLocal))
  7553. {
  7554. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7555. }
  7556. else
  7557. {
  7558. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7559. }
  7560. }
  7561. }
  7562. __finally
  7563. {
  7564. FDragDropOperation = false;
  7565. delete FileList;
  7566. }
  7567. }
  7568. }
  7569. //---------------------------------------------------------------------------
  7570. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7571. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7572. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7573. {
  7574. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7575. }
  7576. //---------------------------------------------------------------------------
  7577. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7578. TObject * Sender, int grfKeyState, int & dwEffect)
  7579. {
  7580. DebugAssert(!IsLocalBrowserMode());
  7581. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7582. if ((dwEffect != DROPEFFECT_NONE) &&
  7583. IsFileControl(DropSourceControl, osRemote))
  7584. {
  7585. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7586. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7587. (RemoteDirView->DropTarget == NULL))
  7588. {
  7589. dwEffect = DROPEFFECT_NONE;
  7590. }
  7591. else
  7592. {
  7593. if (dwEffect == DROPEFFECT_COPY)
  7594. {
  7595. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7596. // currently we support copying always (at least via temporary directory);
  7597. // remove associated checks once this all proves stable and working
  7598. bool CopyCapable = true;
  7599. // if we do not support neither of operations, there's no discussion
  7600. if (!MoveCapable && !CopyCapable)
  7601. {
  7602. dwEffect = DROPEFFECT_NONE;
  7603. }
  7604. else
  7605. {
  7606. // The default effect inside remote panel is move,
  7607. // unless we do not support it, but support copy
  7608. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7609. {
  7610. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7611. }
  7612. else
  7613. {
  7614. // with ctrl-down, we want copy unless it is not supported
  7615. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7616. }
  7617. }
  7618. }
  7619. }
  7620. }
  7621. }
  7622. //---------------------------------------------------------------------------
  7623. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7624. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7625. {
  7626. DebugAssert(!IsLocalBrowserMode());
  7627. if (FDDTotalSize >= 0)
  7628. {
  7629. if (File->IsDirectory)
  7630. {
  7631. FDDTotalSize = -1;
  7632. }
  7633. else
  7634. {
  7635. FDDTotalSize += File->Size;
  7636. }
  7637. }
  7638. DebugAssert(!FDragTempDir.IsEmpty());
  7639. // TODO: this is quite ineffective
  7640. // TODO: what if invalid character replacement is disabled?
  7641. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7642. UnicodeString TransferFileName;
  7643. if (Sender == RemoteDriveView)
  7644. {
  7645. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7646. }
  7647. else
  7648. {
  7649. TransferFileName = File->FileName;
  7650. }
  7651. FDDFileList->AddObject(TransferFileName, File);
  7652. }
  7653. //---------------------------------------------------------------------------
  7654. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7655. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7656. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7657. {
  7658. // sometimes we do not get DDEnd so the list is not released
  7659. SAFE_DESTROY(FDDFileList);
  7660. FDDFileList = new TStringList();
  7661. FDragTempDir = WinConfiguration->TemporaryDir();
  7662. FDDTotalSize = 0;
  7663. FDragDropSshTerminate.reset(NULL);
  7664. }
  7665. //---------------------------------------------------------------------------
  7666. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7667. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7668. HRESULT & Result)
  7669. {
  7670. if (Result == DRAGDROP_S_DROP)
  7671. {
  7672. try
  7673. {
  7674. GlobalDragImageList->HideDragImage();
  7675. try
  7676. {
  7677. RemoteFileControlDDTargetDrop();
  7678. }
  7679. catch(ESshTerminate & E)
  7680. {
  7681. FDragDropSshTerminate.reset(E.Clone());
  7682. }
  7683. }
  7684. catch (Exception &E)
  7685. {
  7686. // If downloading fails we need to cancel drag&drop, otherwise
  7687. // Explorer shows error
  7688. // But by the way exception probably never reach this point as
  7689. // it's catched on way.
  7690. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7691. Result = DRAGDROP_S_CANCEL;
  7692. ShowExtendedException(Terminal, &E);
  7693. }
  7694. }
  7695. }
  7696. //---------------------------------------------------------------------------
  7697. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7698. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7699. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7700. {
  7701. TFileMasks::TParams MaskParams;
  7702. MaskParams.Size = Size;
  7703. MaskParams.Modification = Modification;
  7704. // this does not re-parse the mask if it is the same as the last time
  7705. FDirViewMatchMask = Masks;
  7706. bool ImplicitMatch;
  7707. Matches =
  7708. // RecurseInclude parameter has no effect as the Path is empty
  7709. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7710. (AllowImplicitMatches || !ImplicitMatch);
  7711. }
  7712. //---------------------------------------------------------------------------
  7713. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7714. TObject * Sender, TListItem * Item, WORD & Indexes)
  7715. {
  7716. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7717. UnicodeString Ext;
  7718. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7719. {
  7720. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7721. }
  7722. else
  7723. {
  7724. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7725. }
  7726. if (SameText(Ext, Configuration->PartialExt))
  7727. {
  7728. Indexes |= oiPartial;
  7729. }
  7730. }
  7731. //---------------------------------------------------------------------------
  7732. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7733. {
  7734. return
  7735. DirViewEnabled(osCurrent) &&
  7736. DirView(osCurrent)->CanPasteFromClipBoard();
  7737. }
  7738. //---------------------------------------------------------------------------
  7739. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7740. {
  7741. bool Result = false;
  7742. if (CanPasteToDirViewFromClipBoard())
  7743. {
  7744. Result = true;
  7745. }
  7746. else
  7747. {
  7748. UnicodeString ClipboardText;
  7749. if (NonEmptyTextFromClipboard(ClipboardText))
  7750. {
  7751. if (StoredSessions->IsUrl(ClipboardText))
  7752. {
  7753. Result = true;
  7754. }
  7755. else
  7756. {
  7757. Result = DirViewEnabled(osCurrent);
  7758. }
  7759. }
  7760. }
  7761. return Result;
  7762. }
  7763. //---------------------------------------------------------------------------
  7764. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7765. {
  7766. if (DoesClipboardContainOurFiles())
  7767. {
  7768. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7769. {
  7770. DebugAssert(!IsLocalBrowserMode());
  7771. TTerminalManager * Manager = TTerminalManager::Instance();
  7772. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7773. Manager->ActiveTerminal = FClipboardTerminal;
  7774. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7775. }
  7776. }
  7777. else if (CanPasteToDirViewFromClipBoard())
  7778. {
  7779. DirView(osCurrent)->PasteFromClipBoard();
  7780. }
  7781. else
  7782. {
  7783. UnicodeString ClipboardText;
  7784. if (NonEmptyTextFromClipboard(ClipboardText))
  7785. {
  7786. if (StoredSessions->IsUrl(ClipboardText))
  7787. {
  7788. NewSession(false, ClipboardText);
  7789. }
  7790. else
  7791. {
  7792. DirView(osCurrent)->Path = ClipboardText;
  7793. }
  7794. }
  7795. }
  7796. }
  7797. //---------------------------------------------------------------------------
  7798. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7799. {
  7800. // TBD
  7801. }
  7802. //---------------------------------------------------------------------------
  7803. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7804. {
  7805. TCustomDirView * ADirView = DirView(Side);
  7806. ADirView->SelectAll(Mode);
  7807. ADirView->SetFocus();
  7808. }
  7809. //---------------------------------------------------------------------------
  7810. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7811. {
  7812. TCustomDirView * ADirView = DirView(Side);
  7813. TFileFilter Filter;
  7814. DefaultFileFilter(Filter);
  7815. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7816. {
  7817. Configuration->Usage->Inc(L"MaskSelections");
  7818. ADirView->SelectFiles(Filter, Select);
  7819. ADirView->SetFocus();
  7820. }
  7821. }
  7822. //---------------------------------------------------------------------------
  7823. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7824. {
  7825. TCustomDirView * ADirView = DirView(Side);
  7826. ADirView->RestoreSelectedNames();
  7827. ADirView->SetFocus();
  7828. }
  7829. //---------------------------------------------------------------------------
  7830. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7831. {
  7832. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7833. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7834. {
  7835. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7836. UnicodeString Ext;
  7837. if (!IsSideLocalBrowser(osCurrent))
  7838. {
  7839. Ext = UnixExtractFileExt(FileName);
  7840. }
  7841. else
  7842. {
  7843. Ext = ExtractFileExt(FileName);
  7844. }
  7845. if (Ext.IsEmpty())
  7846. {
  7847. Ext = L".";
  7848. }
  7849. TFileFilter Filter;
  7850. Filter.Masks = FORMAT(L"*%s", (Ext));
  7851. Filter.Directories = false;
  7852. CurrentDirView->SelectFiles(Filter, Select);
  7853. }
  7854. }
  7855. //---------------------------------------------------------------------------
  7856. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7857. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7858. {
  7859. UnicodeString Result;
  7860. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7861. {
  7862. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7863. }
  7864. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7865. {
  7866. // noop
  7867. }
  7868. else
  7869. {
  7870. Result =
  7871. FMTLOAD(FILE_INFO_FORMAT,
  7872. (FormatBytes(FileInfo.SelectedSize),
  7873. FormatBytes(FileInfo.FilesSize),
  7874. FormatNumber(FileInfo.SelectedCount),
  7875. FormatNumber(FileInfo.FilesCount)));
  7876. }
  7877. return Result;
  7878. }
  7879. //---------------------------------------------------------------------------
  7880. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7881. TTBXStatusPanel * Panel, TOperationSide Side)
  7882. {
  7883. if (Panel->Index == 1)
  7884. {
  7885. ToggleShowHiddenFiles();
  7886. }
  7887. else if (Panel->Index == 2)
  7888. {
  7889. Filter(Side);
  7890. }
  7891. }
  7892. //---------------------------------------------------------------------------
  7893. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7894. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7895. {
  7896. DebugAssert(!StatusBar->SimplePanel);
  7897. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7898. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7899. }
  7900. //---------------------------------------------------------------------------
  7901. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7902. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7903. {
  7904. DebugAssert(StatusBar->Panels->Count >= 3);
  7905. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7906. if (FileInfo.HiddenCount > 0)
  7907. {
  7908. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7909. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7910. }
  7911. else
  7912. {
  7913. HiddenFilesPanel->ViewPriority = 0;
  7914. // not really necessary, just to cleanup no-longer-valid data
  7915. HiddenFilesPanel->Caption = L"";
  7916. }
  7917. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7918. if (FileInfo.FilteredCount > 0)
  7919. {
  7920. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7921. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7922. }
  7923. else
  7924. {
  7925. FilteredFilesPanel->ViewPriority = 0;
  7926. // not really necessary, just to cleanup no-longer-valid data
  7927. FilteredFilesPanel->Caption = L"";
  7928. }
  7929. }
  7930. //---------------------------------------------------------------------------
  7931. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7932. TObject * /*Sender*/)
  7933. {
  7934. if (DirView(osRemote)->Enabled)
  7935. {
  7936. DirView(osRemote)->SetFocus();
  7937. }
  7938. }
  7939. //---------------------------------------------------------------------------
  7940. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7941. {
  7942. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7943. switch (Config.Show)
  7944. {
  7945. case qvShow:
  7946. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7947. {
  7948. Config.Show = qvHide;
  7949. }
  7950. else
  7951. {
  7952. Config.Show = Config.LastHideShow;
  7953. }
  7954. break;
  7955. case qvHideWhenEmpty:
  7956. if (ComponentVisible[fcQueueView])
  7957. {
  7958. Config.Show = qvHide;
  7959. }
  7960. else
  7961. {
  7962. Config.LastHideShow = Config.Show;
  7963. Config.Show = qvShow;
  7964. }
  7965. break;
  7966. case qvHide:
  7967. Config.LastHideShow = Config.Show;
  7968. Config.Show = qvShow;
  7969. break;
  7970. }
  7971. WinConfiguration->QueueView = Config;
  7972. }
  7973. //---------------------------------------------------------------------------
  7974. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7975. {
  7976. UnicodeString Result;
  7977. if (FTerminal != NULL)
  7978. {
  7979. switch (WinConfiguration->PathInCaption)
  7980. {
  7981. case picShort:
  7982. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7983. break;
  7984. case picFull:
  7985. Result = FTerminal->CurrentDirectory;
  7986. break;
  7987. }
  7988. }
  7989. return Result;
  7990. }
  7991. //---------------------------------------------------------------------------
  7992. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7993. {
  7994. TColor Result = GetWindowColor(FSessionColor);
  7995. return Result;
  7996. }
  7997. //---------------------------------------------------------------------------
  7998. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7999. {
  8000. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8001. return Result;
  8002. }
  8003. //---------------------------------------------------------------------------
  8004. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8005. {
  8006. TTerminalManager::Instance()->UpdateAppTitle();
  8007. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  8008. // background to gray on Windows 7 (works on Windows 10).
  8009. // See also EnableControl
  8010. if (Showing)
  8011. {
  8012. if (Terminal != NULL)
  8013. {
  8014. // Update path when it changes
  8015. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  8016. {
  8017. UpdateSessionTab(SessionsPageControl->ActivePage);
  8018. }
  8019. }
  8020. NonVisualDataModule->ReconnectSessionAction->Update();
  8021. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8022. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8023. CenterReconnectToolbar();
  8024. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  8025. if (HasTerminal)
  8026. {
  8027. // TODO needed yet with second local browser?
  8028. if (!RemoteDirView->Enabled)
  8029. {
  8030. RemoteDirView->Enabled = true;
  8031. if (FRemoteDirViewWasFocused)
  8032. {
  8033. ActiveControl = RemoteDirView;
  8034. }
  8035. }
  8036. RemoteDirView->Color = PanelColor();
  8037. }
  8038. else
  8039. {
  8040. if (RemoteDirView->Enabled)
  8041. {
  8042. // This is first called when the form is being constructed
  8043. // (not anymore due to Showing test above)
  8044. // but the false is overriden in the constructor later.
  8045. // An even later in TScpCommanderForm::DoShow()
  8046. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8047. RemoteDirView->Enabled = false;
  8048. }
  8049. RemoteDirView->Color = DisabledPanelColor();
  8050. }
  8051. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8052. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8053. RemoteDriveView->Color = RemoteDirView->Color;
  8054. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8055. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  8056. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8057. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8058. QueueLabelUpdateStatus();
  8059. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8060. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8061. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8062. }
  8063. }
  8064. //---------------------------------------------------------------------------
  8065. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8066. {
  8067. UpdateControls();
  8068. }
  8069. //---------------------------------------------------------------------------
  8070. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8071. TObject * Sender)
  8072. {
  8073. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8074. DebugAssert(DirView != NULL);
  8075. DoDirViewLoaded(DirView);
  8076. TransferPresetAutoSelect();
  8077. }
  8078. //---------------------------------------------------------------------------
  8079. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8080. {
  8081. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8082. // first run after installation
  8083. if (double(Updates.LastCheck) == 0)
  8084. {
  8085. // make sure next time there will be an update (if enabled)
  8086. Updates.LastCheck = TDateTime(1);
  8087. WinConfiguration->Updates = Updates;
  8088. }
  8089. else if ((double(Updates.Period) > 0) &&
  8090. (Now() - Updates.LastCheck >= Updates.Period))
  8091. {
  8092. TThreadMethod OnUpdatesChecked = NULL;
  8093. if (!IsUWP())
  8094. {
  8095. OnUpdatesChecked = UpdatesChecked;
  8096. }
  8097. StartUpdateThread(OnUpdatesChecked);
  8098. }
  8099. }
  8100. //---------------------------------------------------------------------------
  8101. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8102. {
  8103. UnicodeString Message;
  8104. bool New;
  8105. TQueryType Type;
  8106. GetUpdatesMessage(Message, New, Type, false);
  8107. if (!Message.IsEmpty())
  8108. {
  8109. if (New)
  8110. {
  8111. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8112. }
  8113. if (!New && (Type != qtWarning))
  8114. {
  8115. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8116. }
  8117. else
  8118. {
  8119. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8120. }
  8121. if (New)
  8122. {
  8123. Configuration->Usage->Inc(L"UpdateNotifications");
  8124. }
  8125. }
  8126. }
  8127. //---------------------------------------------------------------------------
  8128. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8129. {
  8130. RestoreApp();
  8131. bool CanDisplay = !NonVisualDataModule->Busy;
  8132. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8133. {
  8134. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8135. }
  8136. if (CanDisplay)
  8137. {
  8138. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8139. CheckForUpdates(true);
  8140. }
  8141. else
  8142. {
  8143. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8144. }
  8145. }
  8146. //---------------------------------------------------------------------------
  8147. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8148. TCopyParamRuleData & Data)
  8149. {
  8150. DebugAssert(Terminal != NULL);
  8151. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8152. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8153. Data.RemoteDirectory = RemoteDirView->PathName;
  8154. }
  8155. //---------------------------------------------------------------------------
  8156. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8157. {
  8158. // Terminal can be null when we are changing local directory implicitly,
  8159. // because it has been deleted, while no session is connected
  8160. // (Login dialog is open)
  8161. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8162. {
  8163. DebugAssert(!IsLocalBrowserMode());
  8164. TCopyParamRuleData Data;
  8165. GetTransferPresetAutoSelectData(Data);
  8166. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8167. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8168. if (CopyParamIndex < 0)
  8169. {
  8170. // there is no preset that matches autoselection
  8171. // set preset that we consider "default"
  8172. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8173. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8174. }
  8175. else
  8176. {
  8177. // there is preset matching autoselection
  8178. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8179. if (CopyParamName == FCopyParamAutoSelected)
  8180. {
  8181. // autoselected the same preset as the last time
  8182. // make no change (i.e. preserve custom user preset, if any)
  8183. }
  8184. else
  8185. {
  8186. // autoselected the different preset then the last time (or there
  8187. // was default preset set)
  8188. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8189. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8190. }
  8191. }
  8192. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8193. {
  8194. if (CopyParamIndex >= 0)
  8195. {
  8196. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8197. }
  8198. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8199. try
  8200. {
  8201. int Fmt =
  8202. (CopyParamIndex < 0) ?
  8203. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8204. COPY_PARAM_AUTOSELECTED;
  8205. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8206. Data->Message = MainInstructions(Message);
  8207. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8208. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8209. CopyParamAttrs);
  8210. if (CopyParamIndex >= 0)
  8211. {
  8212. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8213. Info = FORMAT(L"%s\n \n%s", (Info,
  8214. FMTLOAD(COPY_PARAM_RULE,
  8215. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8216. }
  8217. Data->Message += L"\n\n" + Info;
  8218. if (WinConfiguration->CopyParamAutoSelectNotice)
  8219. {
  8220. TransferPresetNoteMessage(Data, true);
  8221. }
  8222. else
  8223. {
  8224. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8225. Data = NULL; // ownership passed
  8226. }
  8227. }
  8228. __finally
  8229. {
  8230. delete Data;
  8231. }
  8232. }
  8233. }
  8234. }
  8235. //---------------------------------------------------------------------------
  8236. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8237. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8238. {
  8239. DebugAssert(NoteData != NULL);
  8240. DebugAssert(!IsLocalBrowserMode());
  8241. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8242. TQueryButtonAlias Aliases[1];
  8243. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8244. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8245. Params.Aliases = Aliases;
  8246. Params.AliasesCount = LENOF(Aliases);
  8247. unsigned int Result =
  8248. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8249. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8250. switch (Result)
  8251. {
  8252. case qaNeverAskAgain:
  8253. DebugAssert(AllowNeverAskAgain);
  8254. WinConfiguration->CopyParamAutoSelectNotice = false;
  8255. break;
  8256. case qaIgnore:
  8257. PreferencesDialog(pmPresets);
  8258. break;
  8259. }
  8260. }
  8261. //---------------------------------------------------------------------------
  8262. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8263. {
  8264. // as of now useless, as this is used for notes only, never for balloons, ...
  8265. RestoreApp();
  8266. // .. and we should never be busy here
  8267. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8268. {
  8269. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8270. }
  8271. }
  8272. //---------------------------------------------------------------------------
  8273. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8274. TPreferencesMode APreferencesMode)
  8275. {
  8276. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8277. TCopyParamRuleData Data;
  8278. if (Terminal != NULL)
  8279. {
  8280. PreferencesData.reset(new TPreferencesDialogData());
  8281. GetTransferPresetAutoSelectData(Data);
  8282. PreferencesData->CopyParamRuleData = &Data;
  8283. }
  8284. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8285. }
  8286. //---------------------------------------------------------------------------
  8287. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8288. const TCustomCommandType & Command)
  8289. {
  8290. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8291. {
  8292. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8293. }
  8294. }
  8295. //---------------------------------------------------------------------------
  8296. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8297. {
  8298. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8299. TCustomCommandType Command;
  8300. // make sure we use local custom command when remote are not supported
  8301. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8302. {
  8303. Command = FLastCustomCommand;
  8304. }
  8305. else
  8306. {
  8307. Command.Params = Command.Params | ccLocal;
  8308. }
  8309. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8310. FEditingFocusedAdHocCommand = OnFocused;
  8311. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8312. int Options =
  8313. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8314. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8315. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8316. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8317. {
  8318. FLastCustomCommand = Command;
  8319. UpdateCustomCommandsToolbar();
  8320. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8321. }
  8322. }
  8323. //---------------------------------------------------------------------------
  8324. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8325. {
  8326. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8327. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8328. DebugAssert(State > 0);
  8329. if (State <= 0)
  8330. {
  8331. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8332. }
  8333. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8334. }
  8335. //---------------------------------------------------------------------------
  8336. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8337. TCustomCommandType & Command, int & State)
  8338. {
  8339. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8340. if (Result)
  8341. {
  8342. Command = FLastCustomCommand;
  8343. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8344. }
  8345. return Result;
  8346. }
  8347. //---------------------------------------------------------------------------
  8348. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8349. {
  8350. ResetIncrementalSearch();
  8351. }
  8352. //---------------------------------------------------------------------------
  8353. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8354. {
  8355. DebugAssert(ComponentVisible[Component]);
  8356. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8357. }
  8358. //---------------------------------------------------------------------------
  8359. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8360. {
  8361. // when queue panel is resized here directly, the status bar is stretched
  8362. // over whole space the panel occupied
  8363. PostComponentHide(fcQueueView);
  8364. }
  8365. //---------------------------------------------------------------------------
  8366. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8367. {
  8368. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8369. RefreshColorMode();
  8370. WinConfiguration->ResetSysDarkTheme();
  8371. ConfigurationChanged();
  8372. ConfigureInterface();
  8373. // Should be called for all controls
  8374. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8375. }
  8376. //---------------------------------------------------------------------------
  8377. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8378. {
  8379. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8380. if (!FInvalid &&
  8381. (Message.LParam != 0) &&
  8382. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8383. {
  8384. ThemeChanged();
  8385. }
  8386. TForm::Dispatch(&Message);
  8387. }
  8388. //---------------------------------------------------------------------------
  8389. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8390. {
  8391. TMessage * M = static_cast<TMessage*>(Message);
  8392. switch (M->Msg)
  8393. {
  8394. case CM_DIALOGCHAR:
  8395. CMDialogChar(*M);
  8396. break;
  8397. case WM_APPCOMMAND:
  8398. WMAppCommand(*M);
  8399. break;
  8400. case WM_SYSCOMMAND:
  8401. WMSysCommand(*M);
  8402. break;
  8403. case WM_QUERYENDSESSION:
  8404. WMQueryEndSession(*M);
  8405. break;
  8406. case WM_ENDSESSION:
  8407. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8408. break;
  8409. #ifdef _DEBUG
  8410. case WM_WINDOWPOSCHANGED:
  8411. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8412. break;
  8413. case CM_CUSTOMSTYLECHANGED:
  8414. TForm::Dispatch(Message);
  8415. break;
  8416. #endif
  8417. case WM_COMPONENT_HIDE:
  8418. {
  8419. Byte Component = static_cast<Byte>(M->WParam);
  8420. // sanity check
  8421. if (ComponentVisible[Component])
  8422. {
  8423. // special treatment
  8424. if (Component == fcQueueView)
  8425. {
  8426. ToggleQueueVisibility();
  8427. DebugAssert(!ComponentVisible[fcQueueView]);
  8428. }
  8429. else
  8430. {
  8431. ComponentVisible[Component] = false;
  8432. }
  8433. }
  8434. }
  8435. break;
  8436. case WM_COPYDATA:
  8437. WMCopyData(*M);
  8438. break;
  8439. case WM_MANAGES_CAPTION:
  8440. // caption managed in TTerminalManager::UpdateAppTitle()
  8441. M->Result = 1;
  8442. break;
  8443. case WM_WANTS_MOUSEWHEEL:
  8444. M->Result = 1;
  8445. break;
  8446. case CM_SHOWINGCHANGED:
  8447. CMShowingChanged(*M);
  8448. break;
  8449. case WM_CLOSE:
  8450. WMClose(*M);
  8451. break;
  8452. case WM_DPICHANGED:
  8453. WMDpiChanged(*M);
  8454. break;
  8455. case CM_DPICHANGED:
  8456. CMDpiChanged(*M);
  8457. break;
  8458. case WM_SETTINGCHANGE:
  8459. WMSettingChange(*M);
  8460. break;
  8461. case CM_DIALOGKEY:
  8462. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8463. break;
  8464. default:
  8465. TForm::Dispatch(Message);
  8466. break;
  8467. }
  8468. }
  8469. //---------------------------------------------------------------------------
  8470. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8471. {
  8472. // noop
  8473. }
  8474. //---------------------------------------------------------------------------
  8475. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8476. {
  8477. TForm::Dispatch(&Message);
  8478. FSessionColors->Clear();
  8479. AddFixedSessionImages();
  8480. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8481. UpdateImages();
  8482. }
  8483. //---------------------------------------------------------------------------
  8484. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8485. {
  8486. TForm::Dispatch(&Message);
  8487. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8488. UpdatePixelsPerInchMainWindowCounter();
  8489. }
  8490. //---------------------------------------------------------------------------
  8491. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8492. {
  8493. // Cannot close window while we are busy.
  8494. // We cannot test this in FormCloseQuery as that is called also from
  8495. // Close(), which is called by CloseApplicationAction. So we can be busy
  8496. // there already even, when it is legitimate to close the application.
  8497. // Possibly a better place to handle this would be WMSysCommand.
  8498. if (NonVisualDataModule->Busy)
  8499. {
  8500. Message.Result = 1;
  8501. }
  8502. else
  8503. {
  8504. TForm::Dispatch(&Message);
  8505. }
  8506. }
  8507. //---------------------------------------------------------------------------
  8508. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8509. {
  8510. TForm::Dispatch(&Message);
  8511. if (Showing && (Terminal == NULL))
  8512. {
  8513. // When we are starting minimized (i.e. from an installer),
  8514. // postpone showing Login dialog until we get restored.
  8515. // Otherwise the Login dialog (and Authentication window) show restored
  8516. // over invisible (minimized) main window.
  8517. if (WindowState == wsMinimized)
  8518. {
  8519. FNeedSession = true;
  8520. }
  8521. else
  8522. {
  8523. // This happens before application ever goes idle, so the toolbars would
  8524. // stay enabled (initial state) until the Login dialog is dismissed.
  8525. UpdateActions();
  8526. NonVisualDataModule->StartBusy();
  8527. try
  8528. {
  8529. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8530. // otherwise the dialog does not receive focus.
  8531. // With Commander interface the ProcessMessages is called already
  8532. // by TDriveView, but with Explorer interface, we need to call it explicily
  8533. Application->ProcessMessages();
  8534. // do not reload sessions, they have been loaded just now (optimization)
  8535. NeedSession(true);
  8536. }
  8537. __finally
  8538. {
  8539. NonVisualDataModule->EndBusy();
  8540. }
  8541. }
  8542. }
  8543. }
  8544. //---------------------------------------------------------------------------
  8545. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8546. {
  8547. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8548. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8549. }
  8550. //---------------------------------------------------------------------------
  8551. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8552. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8553. int & MaxHeight)
  8554. {
  8555. // workaround for bug in TWinControl.CalcConstraints
  8556. // Check for empty rect (restore from iconic state) is done there only after
  8557. // call to AdjustClientRect, which enlarges the rect (for forms).
  8558. TRect R = GetClientRect();
  8559. // when restoring from iconic state, place no restrictions
  8560. if (IsRectEmpty(R))
  8561. {
  8562. MinWidth = 0;
  8563. MinHeight = 0;
  8564. MaxWidth = 32000;
  8565. MaxHeight = 32000;
  8566. }
  8567. }
  8568. //---------------------------------------------------------------------------
  8569. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8570. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8571. {
  8572. // terminal can be already closed (e.g. dropped connection)
  8573. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8574. {
  8575. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8576. try
  8577. {
  8578. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8579. }
  8580. catch(...)
  8581. {
  8582. if (!Terminal->Active)
  8583. {
  8584. Close = true;
  8585. }
  8586. throw;
  8587. }
  8588. }
  8589. }
  8590. //---------------------------------------------------------------------------
  8591. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8592. {
  8593. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8594. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8595. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8596. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8597. {
  8598. OnGetSpaceAvailable = GetSpaceAvailable;
  8599. }
  8600. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8601. OnGetSpaceAvailable);
  8602. }
  8603. //---------------------------------------------------------------------------
  8604. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8605. {
  8606. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8607. Terminal->FillSessionDataForCode(Data.get());
  8608. return Data.release();
  8609. }
  8610. //---------------------------------------------------------------------------
  8611. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8612. {
  8613. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8614. DoGenerateUrlDialog(Data.get(), Paths);
  8615. }
  8616. //---------------------------------------------------------------------------
  8617. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8618. {
  8619. GenerateUrl(NULL);
  8620. }
  8621. //---------------------------------------------------------------------------
  8622. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8623. {
  8624. std::unique_ptr<TStrings> Paths(new TStringList());
  8625. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8626. GenerateUrl(Paths.get());
  8627. }
  8628. //---------------------------------------------------------------------------
  8629. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8630. {
  8631. FSessionColor = value;
  8632. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8633. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8634. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8635. DebugAssert(ColorItem != NULL);
  8636. ColorItem->Color = C;
  8637. NonVisualDataModule->ColorMenuItem->Color = C;
  8638. UpdateControls();
  8639. }
  8640. //---------------------------------------------------------------------------
  8641. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8642. {
  8643. if (value != FSessionColor)
  8644. {
  8645. UpdateSessionColor(value);
  8646. // Is null when called from LastTerminalClosed
  8647. if (Terminal != NULL)
  8648. {
  8649. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8650. }
  8651. }
  8652. }
  8653. //---------------------------------------------------------------------------
  8654. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8655. {
  8656. bool Result = FNoteTimer->Enabled;
  8657. if (Result)
  8658. {
  8659. // cannot cancel note too early
  8660. bool NotEarly =
  8661. (Now() - FNoteShown >
  8662. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8663. if (Force || NotEarly)
  8664. {
  8665. FNoteTimer->Enabled = false;
  8666. FNote = L"";
  8667. SAFE_DESTROY(FNoteData);
  8668. FOnNoteClick = NULL;
  8669. FNoteHints = FNotes->Text;
  8670. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8671. UpdateStatusBar();
  8672. }
  8673. }
  8674. return Result;
  8675. }
  8676. //---------------------------------------------------------------------------
  8677. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8678. {
  8679. DebugAssert(FNoteTimer->Enabled);
  8680. CancelNote(true);
  8681. }
  8682. //---------------------------------------------------------------------------
  8683. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8684. {
  8685. int P = Note.Pos(L"\n");
  8686. if (P > 0)
  8687. {
  8688. Note.SetLength(P - 1);
  8689. }
  8690. FNotes->Add(FORMAT(L"[%s] %s",
  8691. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8692. while (FNotes->Count > 10)
  8693. {
  8694. FNotes->Delete(0);
  8695. }
  8696. if (UpdateNow)
  8697. {
  8698. FNoteHints = FNotes->Text;
  8699. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8700. UpdateStatusBar();
  8701. }
  8702. }
  8703. //---------------------------------------------------------------------------
  8704. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8705. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8706. {
  8707. int P = Note.Pos(L"\n");
  8708. if (P > 0)
  8709. {
  8710. Note.SetLength(P - 1);
  8711. }
  8712. FNoteHints = FNotes->Text;
  8713. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8714. FNote = Note;
  8715. SAFE_DESTROY(FNoteData);
  8716. FNoteData = NoteData;
  8717. FOnNoteClick = OnNoteClick;
  8718. AddNote(Note, false);
  8719. UpdateStatusBar();
  8720. FNoteShown = Now();
  8721. FNoteTimer->Enabled = false;
  8722. if (Seconds == 0)
  8723. {
  8724. Seconds = WinConfiguration->NotificationsTimeout;
  8725. }
  8726. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8727. FNoteTimer->Enabled = true;
  8728. }
  8729. //---------------------------------------------------------------------------
  8730. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8731. {
  8732. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8733. }
  8734. //---------------------------------------------------------------------------
  8735. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8736. {
  8737. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8738. {
  8739. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8740. }
  8741. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8742. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8743. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8744. 0, NULL, NULL);
  8745. }
  8746. //---------------------------------------------------------------------------
  8747. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8748. {
  8749. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8750. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8751. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8752. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8753. }
  8754. //---------------------------------------------------------------------------
  8755. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8756. {
  8757. WinConfiguration->FormatSizeBytes = Style;
  8758. }
  8759. //---------------------------------------------------------------------------
  8760. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8761. {
  8762. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8763. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8764. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8765. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8766. }
  8767. //---------------------------------------------------------------------------
  8768. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8769. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8770. {
  8771. if (Panel->Index == 0)
  8772. {
  8773. if (FOnNoteClick != NULL)
  8774. {
  8775. // prevent the user data from being freed by possible call
  8776. // to CancelNote or PostNote during call to OnNoteClick
  8777. std::unique_ptr<TObject> NoteData(FNoteData);
  8778. TNotifyEvent OnNoteClick = FOnNoteClick;
  8779. FNoteData = NULL;
  8780. // need to cancel the note as we are going to delete its user data
  8781. CancelNote(true);
  8782. OnNoteClick(NoteData.get());
  8783. }
  8784. }
  8785. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8786. {
  8787. FileSystemInfo();
  8788. }
  8789. }
  8790. //---------------------------------------------------------------------------
  8791. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8792. {
  8793. // workaround:
  8794. // 1) for unknown reason, disabling window, while minimized,
  8795. // prevents it from restoring, even if it was enabled again meanwhile
  8796. // 2) when disabling the main window, while another has focus
  8797. // minimize is no longer possible
  8798. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8799. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8800. // Shouldn't we use IsApplicationMinimized() here?
  8801. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8802. {
  8803. Enabled = false;
  8804. }
  8805. FLockLevel++;
  8806. }
  8807. //---------------------------------------------------------------------------
  8808. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8809. {
  8810. DebugAssert(FLockLevel > 0);
  8811. FLockLevel--;
  8812. if (FLockLevel == 0)
  8813. {
  8814. DebugAssert(FLockSuspendLevel == 0);
  8815. Enabled = true;
  8816. }
  8817. }
  8818. //---------------------------------------------------------------------------
  8819. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8820. {
  8821. // We need to make sure that window is enabled when the last modal window closes
  8822. // otherwise focus is not restored correctly.
  8823. // So we re-enable the window when modal window opens and
  8824. // disable it back after is closes
  8825. if (FLockLevel > 0)
  8826. {
  8827. // while we have nesting counter, we know that we never be called
  8828. // recursivelly as Application->OnModalBegin is called only
  8829. // for the top-level modal window
  8830. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8831. {
  8832. // won't be disabled when conditions in LockWindow() were not satisfied
  8833. FDisabledOnLockSuspend = !Enabled;
  8834. // When minimized to tray (or actually when set to SW_HIDE),
  8835. // setting Enabled makes the window focusable even when there's
  8836. // modal window over it
  8837. if (!FTrayIcon->Visible)
  8838. {
  8839. Enabled = true;
  8840. }
  8841. }
  8842. FLockSuspendLevel++;
  8843. }
  8844. }
  8845. //---------------------------------------------------------------------------
  8846. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8847. {
  8848. if (FLockSuspendLevel > 0)
  8849. {
  8850. DebugAssert(FLockLevel > 0);
  8851. FLockSuspendLevel--;
  8852. // see comment in SuspendWindowLock
  8853. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8854. {
  8855. // Note that window can be enabled here, when we were minized to tray when
  8856. // was SuspendWindowLock() called.
  8857. // We should possibly do the same check as in LockWindow(),
  8858. // if it is ever possible that the consitions change between
  8859. // SuspendWindowLock() and ResumeWindowLock()
  8860. Enabled = !FDisabledOnLockSuspend;
  8861. }
  8862. }
  8863. }
  8864. //---------------------------------------------------------------------------
  8865. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8866. {
  8867. if (!TextOnly)
  8868. {
  8869. TTBXComboBoxItem * RemotePathComboBox =
  8870. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8871. TStrings * Items = RemotePathComboBox->Strings;
  8872. Items->BeginUpdate();
  8873. try
  8874. {
  8875. Items->Clear();
  8876. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8877. {
  8878. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8879. while (!IsUnixRootPath(APath))
  8880. {
  8881. int P = APath.LastDelimiter(L'/');
  8882. DebugAssert(P >= 0);
  8883. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8884. APath.SetLength(P - 1);
  8885. }
  8886. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8887. }
  8888. }
  8889. __finally
  8890. {
  8891. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8892. // Setting ItemIndex to -1 does not reset its text
  8893. if (Items->Count == 0)
  8894. {
  8895. RemotePathComboBox->Text = L"";
  8896. }
  8897. Items->EndUpdate();
  8898. }
  8899. }
  8900. }
  8901. //---------------------------------------------------------------------------
  8902. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8903. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8904. int & ImageIndex)
  8905. {
  8906. if (AIndex < 0)
  8907. {
  8908. AIndex = Sender->ItemIndex;
  8909. }
  8910. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8911. }
  8912. //---------------------------------------------------------------------------
  8913. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8914. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8915. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8916. {
  8917. ARect.Left += (10 * AIndex);
  8918. }
  8919. //---------------------------------------------------------------------------
  8920. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8921. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8922. {
  8923. AWidth += (10 * AIndex);
  8924. }
  8925. //---------------------------------------------------------------------------
  8926. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8927. TObject * Sender)
  8928. {
  8929. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8930. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8931. int Index = RemotePathComboBox->ItemIndex;
  8932. while (Index < RemotePathComboBox->Strings->Count - 1)
  8933. {
  8934. APath = UnixExtractFileDir(APath);
  8935. Index++;
  8936. }
  8937. // VanDyke style paths
  8938. if (APath.IsEmpty())
  8939. {
  8940. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8941. APath = ROOTDIRECTORY;
  8942. }
  8943. if (RemoteDirView->Path != APath)
  8944. {
  8945. RemoteDirView->Path = APath;
  8946. }
  8947. }
  8948. //---------------------------------------------------------------------------
  8949. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8950. {
  8951. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8952. DebugUsedParam(Sender);
  8953. UpdateRemotePathComboBox(true);
  8954. }
  8955. //---------------------------------------------------------------------------
  8956. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8957. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8958. {
  8959. ResetIncrementalSearch();
  8960. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8961. DebugAssert(DirView != NULL);
  8962. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8963. {
  8964. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8965. // OnEditing is called also from TCustomListView::CanEdit
  8966. if (Edit != NULL)
  8967. {
  8968. EditSelectBaseName(Edit);
  8969. }
  8970. }
  8971. }
  8972. //---------------------------------------------------------------------------
  8973. //---------------------------------------------------------------------------
  8974. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8975. {
  8976. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8977. Result->AutoDetectDnD = false;
  8978. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8979. Result->RenderDataOn = rdoEnterAndDropSync;
  8980. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8981. return Result;
  8982. }
  8983. //---------------------------------------------------------------------------
  8984. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8985. {
  8986. TForm::CreateWnd();
  8987. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  8988. if (FSessionsDragDropFilesEx == NULL)
  8989. {
  8990. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8991. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8992. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8993. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8994. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8995. }
  8996. if (FQueueDragDropFilesEx == NULL)
  8997. {
  8998. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8999. // No need to set OnDragOver as we do not have any restrictions
  9000. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9001. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9002. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9003. }
  9004. }
  9005. //---------------------------------------------------------------------------
  9006. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9007. {
  9008. TForm::DestroyWnd();
  9009. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9010. FQueueDragDropFilesEx->DragDropControl = NULL;
  9011. }
  9012. //---------------------------------------------------------------------------
  9013. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9014. {
  9015. SideEnter(FCurrentSide);
  9016. FEverShown = true;
  9017. }
  9018. //---------------------------------------------------------------------------
  9019. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9020. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9021. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9022. {
  9023. if (!NonVisualDataModule->Busy)
  9024. {
  9025. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9026. Configuration->Usage->Inc(L"FileFinds");
  9027. LockWindow(true);
  9028. NonVisualDataModule->StartBusy();
  9029. WinConfiguration->LockedInterface = true;
  9030. try
  9031. {
  9032. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9033. }
  9034. __finally
  9035. {
  9036. WinConfiguration->LockedInterface = false;
  9037. NonVisualDataModule->EndBusy();
  9038. UnlockWindow();
  9039. }
  9040. }
  9041. }
  9042. //---------------------------------------------------------------------------
  9043. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9044. {
  9045. if (!NonVisualDataModule->Busy)
  9046. {
  9047. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9048. SetFocus();
  9049. RemoteDirView->Path = UnixExtractFilePath(Path);
  9050. UnicodeString FileName = UnixExtractFileName(Path);
  9051. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9052. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9053. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9054. if (Item == NULL)
  9055. {
  9056. ReloadDirectory(osRemote);
  9057. Item = RemoteDirView->FindFileItem(FileName);
  9058. }
  9059. if (Item != NULL)
  9060. {
  9061. RemoteDirView->ItemFocused = Item;
  9062. RemoteDirView->ItemFocused->MakeVisible(false);
  9063. RemoteDirView->SetFocus();
  9064. }
  9065. }
  9066. }
  9067. //---------------------------------------------------------------------------
  9068. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9069. {
  9070. bool Result = !NonVisualDataModule->Busy;
  9071. if (Result)
  9072. {
  9073. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9074. }
  9075. return Result;
  9076. }
  9077. //---------------------------------------------------------------------------
  9078. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9079. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9080. {
  9081. if (CanOperateOnFoundFiles(ATerminal))
  9082. {
  9083. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9084. FOnFileOperationFinished = OnFileOperationFinished;
  9085. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9086. }
  9087. }
  9088. //---------------------------------------------------------------------------
  9089. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9090. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9091. {
  9092. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9093. }
  9094. //---------------------------------------------------------------------------
  9095. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9096. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9097. {
  9098. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9099. }
  9100. //---------------------------------------------------------------------------
  9101. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9102. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9103. {
  9104. if (CanOperateOnFoundFiles(ATerminal))
  9105. {
  9106. for (int Index = 0; Index < FileList->Count; Index++)
  9107. {
  9108. UnicodeString FileName = FileList->Strings[Index];
  9109. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9110. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9111. OnFileOperationFinished(FileName, true);
  9112. }
  9113. }
  9114. }
  9115. //---------------------------------------------------------------------------
  9116. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9117. {
  9118. FFileFindTerminal = Terminal;
  9119. ShowFileFindDialog(
  9120. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9121. DoDownloadFoundFiles, DoEditFoundFiles);
  9122. }
  9123. //---------------------------------------------------------------------------
  9124. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9125. {
  9126. FTaskbarList = TaskbarList;
  9127. }
  9128. //---------------------------------------------------------------------------
  9129. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9130. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9131. {
  9132. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9133. if (Index >= 0)
  9134. {
  9135. if (Button == mbLeft)
  9136. {
  9137. // "Mouse down" is raised only after tab is switched.
  9138. // If switching tab (switching session) takes long enough for user
  9139. // to actually release the button, "mouse down" is still raised,
  9140. // but we do not get "mouse up" event, so dragging is not cancelled,
  9141. // prevent that by not beginning dragging in the first place.
  9142. if (IsKeyPressed(VK_LBUTTON))
  9143. {
  9144. // when user clicks the "+", we get mouse down only after the session
  9145. // is closed, when new session tab is already on X:Y, so dragging
  9146. // starts, prevent that
  9147. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9148. {
  9149. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9150. if (Terminal != NULL)
  9151. {
  9152. SessionsPageControl->BeginDrag(false);
  9153. }
  9154. }
  9155. }
  9156. }
  9157. else if (Button == mbMiddle)
  9158. {
  9159. // ignore middle-click for "New session tab"
  9160. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9161. if (Terminal != NULL)
  9162. {
  9163. CloseSessionTab(Index);
  9164. }
  9165. }
  9166. }
  9167. }
  9168. //---------------------------------------------------------------------------
  9169. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9170. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9171. {
  9172. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9173. // do not allow dropping on the "+" tab
  9174. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9175. if ((TargetTerminal != NULL) &&
  9176. (SessionsPageControl->ActivePage->PageIndex != Index))
  9177. {
  9178. Configuration->Usage->Inc(L"SessionTabMoves");
  9179. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9180. // but we want to actually prevent that to avoid flicker
  9181. SessionsPageControl->ActivePage->PageIndex = Index;
  9182. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9183. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9184. }
  9185. }
  9186. //---------------------------------------------------------------------------
  9187. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9188. TObject * Sender, TObject * Source, int X, int Y,
  9189. TDragState /*State*/, bool & Accept)
  9190. {
  9191. Accept = (Sender == Source);
  9192. if (Accept)
  9193. {
  9194. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9195. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9196. // do not allow dragging to the "+" tab
  9197. Accept = (Terminal != NULL);
  9198. }
  9199. }
  9200. //---------------------------------------------------------------------------
  9201. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9202. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9203. {
  9204. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9205. if (Index < 0)
  9206. {
  9207. Effect = DROPEFFECT_NONE;
  9208. }
  9209. else
  9210. {
  9211. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9212. // do not allow dropping on the "+" tab
  9213. if (TargetTerminal == NULL)
  9214. {
  9215. Effect = DROPEFFECT_NONE;
  9216. }
  9217. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9218. {
  9219. Effect = DROPEFFECT_COPY;
  9220. }
  9221. }
  9222. }
  9223. //---------------------------------------------------------------------------
  9224. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9225. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9226. {
  9227. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9228. // do not allow dropping on the "+" tab
  9229. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9230. if (TargetTerminal != NULL)
  9231. {
  9232. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9233. if (!IsFileControl(DropSourceControl, osRemote))
  9234. {
  9235. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9236. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9237. // Why don't we use Terminal->CurrentDirectory directly?
  9238. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9239. // do not force queue, drag drop
  9240. false, true);
  9241. }
  9242. }
  9243. }
  9244. //---------------------------------------------------------------------------
  9245. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9246. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9247. {
  9248. // Downloads are handled in RemoteFileControlDDEnd
  9249. if (!IsFileControl(DropSourceControl, osRemote))
  9250. {
  9251. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9252. Terminal->CurrentDirectory,
  9253. // force queue, drag drop
  9254. true, true);
  9255. }
  9256. }
  9257. //---------------------------------------------------------------------------
  9258. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9259. {
  9260. FShowing = false;
  9261. // Do not save empty workspace
  9262. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9263. {
  9264. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9265. UnicodeString Name = WorkspaceName();
  9266. DoSaveWorkspace(
  9267. Name, DataList.get(),
  9268. !Configuration->DisablePasswordStoring &&
  9269. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9270. WinConfiguration->LastStoredSession = Name;
  9271. }
  9272. }
  9273. //---------------------------------------------------------------------------
  9274. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9275. {
  9276. TManagedTerminal * ManagedTerminal =
  9277. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9278. if (ManagedTerminal != NULL)
  9279. {
  9280. ManagedTerminal->DirectoryLoaded = Now();
  9281. }
  9282. }
  9283. //---------------------------------------------------------------------------
  9284. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9285. TListItem * /*Item*/, bool /*Selected*/)
  9286. {
  9287. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9288. switch (DirView->LastSelectMethod)
  9289. {
  9290. case smKeyboard:
  9291. Configuration->Usage->Inc(L"KeyboardSelections");
  9292. break;
  9293. case smMouse:
  9294. Configuration->Usage->Inc(L"MouseSelections");
  9295. break;
  9296. }
  9297. }
  9298. //---------------------------------------------------------------------------
  9299. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9300. {
  9301. if (Color != 0)
  9302. {
  9303. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9304. }
  9305. else
  9306. {
  9307. return FSessionTabImageIndex;
  9308. }
  9309. }
  9310. //---------------------------------------------------------------------------
  9311. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9312. {
  9313. TPngImageCollectionItem * Item =
  9314. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9315. FSessionColors->AddPng(Item->PngImage);
  9316. return FSessionColors->Count - 1;
  9317. }
  9318. //---------------------------------------------------------------------------
  9319. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9320. {
  9321. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9322. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9323. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9324. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9325. }
  9326. //---------------------------------------------------------------------------
  9327. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9328. {
  9329. for (int Index = 0; Index < Control->ControlCount; Index++)
  9330. {
  9331. TControl * AControl = Control->Controls[Index];
  9332. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9333. if (WinControl != NULL)
  9334. {
  9335. CollectItemsWithTextDisplayMode(WinControl);
  9336. }
  9337. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9338. if (Toolbar != NULL)
  9339. {
  9340. // we care for top-level items only
  9341. TTBCustomItem * Items = Toolbar->Items;
  9342. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9343. {
  9344. TTBCustomItem * Item = Items->Items[ItemIndex];
  9345. if (((Item->DisplayMode == nbdmImageAndText) ||
  9346. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9347. EligibleForImageDisplayMode(Item))
  9348. {
  9349. FItemsWithTextDisplayMode.insert(Item);
  9350. }
  9351. }
  9352. }
  9353. }
  9354. }
  9355. //---------------------------------------------------------------------------
  9356. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9357. {
  9358. return true;
  9359. }
  9360. //---------------------------------------------------------------------------
  9361. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9362. {
  9363. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9364. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9365. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9366. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9367. bool Result = true;
  9368. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9369. {
  9370. TTBCustomItem * Item = *i;
  9371. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9372. if (Label != NULL)
  9373. {
  9374. // optimization
  9375. if (Label->Visible == SelectiveToolbarText)
  9376. {
  9377. Result = false;
  9378. }
  9379. else
  9380. {
  9381. Label->Visible = SelectiveToolbarText;
  9382. }
  9383. }
  9384. else
  9385. {
  9386. // optimization
  9387. if (Item->DisplayMode == DisplayMode)
  9388. {
  9389. Result = false;
  9390. }
  9391. else
  9392. {
  9393. Item->DisplayMode = DisplayMode;
  9394. }
  9395. }
  9396. ++i;
  9397. }
  9398. if (Result)
  9399. {
  9400. UpdateNewSessionTab();
  9401. }
  9402. return Result;
  9403. }
  9404. //---------------------------------------------------------------------------
  9405. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9406. {
  9407. DebugFail();
  9408. }
  9409. //---------------------------------------------------------------------------
  9410. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9411. {
  9412. // Among other, a lock level is non zero, while directory is being loaded,
  9413. // even when it happens as a result of dirview navigation,
  9414. // i.e. when TNonVisualDataModule is NOT FBusy.
  9415. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9416. // Among other this prevents a panel auto update to occur while
  9417. // directory is changing.
  9418. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9419. }
  9420. //---------------------------------------------------------------------------
  9421. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9422. {
  9423. // While the window is disabled, we still seem to process menu shortcuts at least,
  9424. // so stop it at least here.
  9425. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9426. return
  9427. (Allowed == aaUpdate) ||
  9428. !NonVisualDataModule->Busy;
  9429. }
  9430. //---------------------------------------------------------------------------
  9431. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9432. {
  9433. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9434. }
  9435. //---------------------------------------------------------------------------
  9436. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9437. {
  9438. // This is somewhat redundant to LockWindow() call from
  9439. // TTerminalManager::TerminalReadDirectoryProgress.
  9440. // But disabling window is known not to block keyboard shorcuts
  9441. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9442. if (Busy > 0)
  9443. {
  9444. if (NonVisualDataModule->Busy)
  9445. {
  9446. State = false;
  9447. }
  9448. else
  9449. {
  9450. NonVisualDataModule->StartBusy();
  9451. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9452. LockWindow();
  9453. }
  9454. }
  9455. else if (Busy < 0)
  9456. {
  9457. UnlockWindow();
  9458. NonVisualDataModule->EndBusy();
  9459. }
  9460. else
  9461. {
  9462. State = NonVisualDataModule->Busy;
  9463. }
  9464. }
  9465. //---------------------------------------------------------------------------
  9466. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9467. {
  9468. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9469. // no context menu for "New session tab"
  9470. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9471. {
  9472. SessionsPageControl->ActivePageIndex = Index;
  9473. if (DebugAlwaysTrue(SessionTabSwitched()))
  9474. {
  9475. // copied from TControl.WMContextMenu
  9476. SendCancelMode(SessionsPageControl);
  9477. // explicit popup instead of using PopupMenu property
  9478. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9479. // while connecting yet not-connected session and hence
  9480. // allowing an access to commands over not-completelly connected session
  9481. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9482. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9483. PopupMenu->PopupComponent = SessionsPageControl;
  9484. PopupMenu->Popup(Point.x, Point.y);
  9485. }
  9486. }
  9487. Handled = true;
  9488. }
  9489. //---------------------------------------------------------------------------
  9490. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9491. {
  9492. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9493. }
  9494. //---------------------------------------------------------------------------
  9495. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9496. {
  9497. Configuration->Usage->Inc(L"PasswordChanges");
  9498. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9499. ChangePasswordData->DisableAuthentationsExceptPassword();
  9500. ChangePasswordData->ChangePassword = true;
  9501. std::unique_ptr<TTerminal> ChangePasswordSession(
  9502. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9503. ChangePasswordSession->AutoReadDirectory = false;
  9504. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9505. {
  9506. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9507. }
  9508. else
  9509. {
  9510. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9511. }
  9512. }
  9513. //---------------------------------------------------------------------------
  9514. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9515. {
  9516. // No nice way to assert SSH2
  9517. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9518. }
  9519. //---------------------------------------------------------------------------
  9520. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9521. {
  9522. TOperationVisualizer OperationVisualizer;
  9523. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9524. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9525. }
  9526. //---------------------------------------------------------------------------
  9527. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9528. {
  9529. TForm::ChangeScale(M, D);
  9530. int APixelsPerInch = GetControlPixelsPerInch(this);
  9531. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9532. try
  9533. {
  9534. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9535. }
  9536. __finally
  9537. {
  9538. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9539. }
  9540. }
  9541. //---------------------------------------------------------------------------
  9542. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9543. {
  9544. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9545. }
  9546. //---------------------------------------------------------------------
  9547. class TPublicControl : public TControl
  9548. {
  9549. friend class TCustomScpExplorerForm;
  9550. };
  9551. //---------------------------------------------------------------------------
  9552. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9553. {
  9554. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9555. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9556. }
  9557. //---------------------------------------------------------------------------
  9558. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9559. {
  9560. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9561. OpenBookmark(Side, Bookmark);
  9562. }
  9563. //---------------------------------------------------------------------------
  9564. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9565. {
  9566. DoBookmarkClick(osLocal, Sender);
  9567. }
  9568. //---------------------------------------------------------------------------
  9569. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9570. {
  9571. DoBookmarkClick(osRemote, Sender);
  9572. }
  9573. //---------------------------------------------------------------------------
  9574. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9575. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9576. {
  9577. // TODO
  9578. if (BookmarkList != NULL)
  9579. {
  9580. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9581. if (!WinConfiguration->UseLocationProfiles)
  9582. {
  9583. std::unique_ptr<TStringList> Directories(new TStringList());
  9584. Directories->CaseSensitive = (Side == osRemote);
  9585. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9586. {
  9587. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9588. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9589. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9590. {
  9591. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9592. Item->Caption = EscapeHotkey(Directory);
  9593. Item->ShortCut = Bookmark->ShortCut;
  9594. Item->OnClick = OnBookmarkClick;
  9595. Item->Tag = reinterpret_cast<int>(Bookmark);
  9596. Directories->Add(Directory);
  9597. Menu->Add(Item.release());
  9598. }
  9599. }
  9600. }
  9601. else
  9602. {
  9603. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9604. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9605. {
  9606. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9607. if (!Bookmark->Node.IsEmpty())
  9608. {
  9609. Folders->Add(Bookmark->Node);
  9610. }
  9611. }
  9612. for (int Index = 0; Index < Folders->Count; Index++)
  9613. {
  9614. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9615. Item->Caption = Folders->Strings[Index];
  9616. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9617. Folders->Objects[Index] = Item.get();
  9618. Menu->Add(Item.release());
  9619. }
  9620. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9621. {
  9622. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9623. TTBCustomItem * Parent;
  9624. if (!Bookmark->Node.IsEmpty())
  9625. {
  9626. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9627. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9628. }
  9629. else
  9630. {
  9631. Parent = Menu;
  9632. }
  9633. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9634. Item->Caption = Bookmark->Name;
  9635. Item->ShortCut = Bookmark->ShortCut;
  9636. Item->OnClick = OnBookmarkClick;
  9637. Item->Tag = reinterpret_cast<int>(Bookmark);
  9638. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9639. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9640. {
  9641. // No hint for location profiles that are actually mere "bookmarks"
  9642. }
  9643. else
  9644. {
  9645. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9646. UnicodeString LongHint;
  9647. if (!Bookmark->Local.IsEmpty())
  9648. {
  9649. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9650. }
  9651. if (!Bookmark->Remote.IsEmpty())
  9652. {
  9653. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9654. }
  9655. AddToList(Hint, LongHint, L"|");
  9656. Item->Hint = Hint;
  9657. }
  9658. Parent->Add(Item.release());
  9659. }
  9660. }
  9661. AddMenuSeparator(Menu);
  9662. }
  9663. }
  9664. //---------------------------------------------------------------------------
  9665. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9666. {
  9667. Menu->Clear();
  9668. std::unique_ptr<TTBCustomItem> Item;
  9669. Item.reset(new TTBXItem(Owner));
  9670. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9671. Item->Options = Item->Options << tboDefault;
  9672. Menu->Add(Item.release());
  9673. AddMenuSeparator(Menu);
  9674. if (Terminal != NULL)
  9675. {
  9676. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9677. }
  9678. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9679. Item.reset(new TTBXItem(Owner));
  9680. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9681. Menu->Add(Item.release());
  9682. }
  9683. //---------------------------------------------------------------------------
  9684. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9685. {
  9686. bool Result = true;
  9687. try
  9688. {
  9689. if (Side == osRemote)
  9690. {
  9691. Terminal->ExceptionOnFail = true;
  9692. }
  9693. try
  9694. {
  9695. DirView(Side)->Path = Path;
  9696. }
  9697. __finally
  9698. {
  9699. if (Side == osRemote)
  9700. {
  9701. Terminal->ExceptionOnFail = false;
  9702. }
  9703. }
  9704. }
  9705. catch (Exception & E)
  9706. {
  9707. if ((Side != osRemote) || Terminal->Active)
  9708. {
  9709. ShowExtendedExceptionEx(Terminal, &E);
  9710. Result = false;
  9711. }
  9712. else
  9713. {
  9714. throw;
  9715. }
  9716. }
  9717. return Result;
  9718. }
  9719. //---------------------------------------------------------------------------
  9720. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9721. {
  9722. // Make sure there some feedback even when loading tiny local folders
  9723. TInstantOperationVisualizer Visualizer;
  9724. DirView(Side)->ReloadDirectory();
  9725. }
  9726. //---------------------------------------------------------------------------
  9727. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9728. {
  9729. if (Index == SessionsPageControl->TabIndex)
  9730. {
  9731. CloseSession();
  9732. }
  9733. else
  9734. {
  9735. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9736. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9737. TTerminalManager * Manager = TTerminalManager::Instance();
  9738. if (!Terminal->Active)
  9739. {
  9740. Manager->FreeTerminal(Terminal);
  9741. }
  9742. else
  9743. {
  9744. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9745. if (CanCloseQueue(Queue))
  9746. {
  9747. Manager->FreeTerminal(Terminal);
  9748. }
  9749. }
  9750. }
  9751. }
  9752. //---------------------------------------------------------------------------
  9753. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9754. {
  9755. CloseSessionTab(Index);
  9756. }
  9757. //---------------------------------------------------------------------------
  9758. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9759. {
  9760. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9761. {
  9762. TInstantOperationVisualizer Visualizer;
  9763. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9764. // before we create a new data object
  9765. ClipboardClear();
  9766. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9767. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9768. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9769. // Intermediate variable to WORKAROUND a crash in compiler
  9770. TObjectList * ClipboardFakeMonitors =
  9771. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9772. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9773. if (FClipboardDragDropFilesEx.get() == NULL)
  9774. {
  9775. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9776. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9777. }
  9778. FClipboardDragDropFilesEx->FileList->Clear();
  9779. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9780. FClipboardDragDropFilesEx->CopyToClipboard();
  9781. FClipboardTerminal = FTerminal;
  9782. // Need full paths, as cwd can be different once files are pasted
  9783. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9784. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9785. {
  9786. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9787. }
  9788. }
  9789. }
  9790. //---------------------------------------------------------------------------
  9791. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9792. {
  9793. if (OpenClipboard(0))
  9794. {
  9795. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9796. CloseClipboard();
  9797. }
  9798. }
  9799. //---------------------------------------------------------------------------
  9800. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9801. {
  9802. return !FClipboardFakeDirectory.IsEmpty();
  9803. }
  9804. //---------------------------------------------------------------------------
  9805. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9806. {
  9807. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9808. FClipboardFakeDirectory = UnicodeString();
  9809. FClipboardTerminal = NULL;
  9810. // Also called in Idle()
  9811. FClipboardFakeMonitors.reset(NULL);
  9812. }
  9813. //---------------------------------------------------------------------------
  9814. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9815. {
  9816. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9817. {
  9818. ClipboardStop();
  9819. }
  9820. }
  9821. //---------------------------------------------------------------------------
  9822. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9823. {
  9824. TTerminalManager * Manager = TTerminalManager::Instance();
  9825. Manager->ActiveTerminal = FClipboardTerminal;
  9826. TTransferOperationParam Params;
  9827. Params.TargetDirectory = TargetDirectory;
  9828. Params.DragDrop = DragDrop;
  9829. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9830. }
  9831. //---------------------------------------------------------------------------
  9832. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9833. {
  9834. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9835. // (as it pumps a Windows message queue while being shutted down)
  9836. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9837. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9838. {
  9839. // Can fail as it can be e.g. locked by AV, so we retry that later
  9840. bool Removed = RemoveDir(ApiPath(FileName));
  9841. try
  9842. {
  9843. if (!NonVisualDataModule->Busy)
  9844. {
  9845. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9846. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9847. }
  9848. }
  9849. __finally
  9850. {
  9851. if (!Removed)
  9852. {
  9853. RemoveDir(ApiPath(FileName));
  9854. }
  9855. }
  9856. }
  9857. }
  9858. //---------------------------------------------------------------------------
  9859. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9860. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9861. {
  9862. TFileMasks::TParams MaskParams;
  9863. MaskParams.Size = Size;
  9864. MaskParams.Modification = Modification;
  9865. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9866. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9867. {
  9868. bool ImplicitMatch;
  9869. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9870. !ImplicitMatch)
  9871. {
  9872. Color = Iter->Color;
  9873. break;
  9874. }
  9875. }
  9876. }
  9877. //---------------------------------------------------------------------------
  9878. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9879. {
  9880. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9881. if (!DirView->IsEditing())
  9882. {
  9883. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9884. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9885. // while searching
  9886. if (Key == VK_BACK)
  9887. {
  9888. if (!FIncrementalSearch.IsEmpty())
  9889. {
  9890. if (FIncrementalSearch.Length() == 1)
  9891. {
  9892. ResetIncrementalSearch();
  9893. }
  9894. else
  9895. {
  9896. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9897. IncrementalSearch(NewText, false, false);
  9898. }
  9899. Key = 0;
  9900. }
  9901. }
  9902. }
  9903. }
  9904. //---------------------------------------------------------------------------
  9905. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9906. {
  9907. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9908. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9909. {
  9910. // Filter control sequences.
  9911. // When not searching yet, prefer use of the space for toggling file selection
  9912. // (so we cannot incrementally search for a string starting with a space).
  9913. if ((Key > VK_SPACE) ||
  9914. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9915. {
  9916. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9917. Key = 0;
  9918. }
  9919. }
  9920. }
  9921. //---------------------------------------------------------------------------
  9922. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9923. {
  9924. if (!FIncrementalSearch.IsEmpty())
  9925. {
  9926. FIncrementalSearch = L"";
  9927. DirView(osCurrent)->UpdateStatusBar();
  9928. }
  9929. }
  9930. //---------------------------------------------------------------------------
  9931. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9932. {
  9933. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9934. if (Item == NULL)
  9935. {
  9936. MessageBeep(MB_ICONHAND);
  9937. }
  9938. else
  9939. {
  9940. TCustomDirView * ADirView = DirView(osCurrent);
  9941. {
  9942. TAutoNestingCounter Guard(FIncrementalSearching);
  9943. ADirView->FocusItem(Item);
  9944. if (ADirView->NortonLike == nlOff)
  9945. {
  9946. if ((ADirView->Selected != Item) ||
  9947. (ADirView->SelCount > 1))
  9948. {
  9949. ADirView->ClearSelection();
  9950. ADirView->Selected = Item;
  9951. }
  9952. }
  9953. }
  9954. FIncrementalSearch = Text;
  9955. Item->MakeVisible(false);
  9956. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9957. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9958. ADirView->UpdateStatusBar();
  9959. }
  9960. }
  9961. //---------------------------------------------------------------------------
  9962. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9963. {
  9964. int Index = Item->Index;
  9965. if (!Reverse)
  9966. {
  9967. Index++;
  9968. if (Index >= Item->Owner->Count)
  9969. {
  9970. Index = 0;
  9971. }
  9972. }
  9973. else
  9974. {
  9975. Index--;
  9976. if (Index < 0)
  9977. {
  9978. Index = Item->Owner->Count - 1;
  9979. }
  9980. }
  9981. return Item->Owner->Item[Index];
  9982. }
  9983. //---------------------------------------------------------------------------
  9984. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9985. {
  9986. TCustomDirView * ADirView = DirView(osCurrent);
  9987. if (ADirView->Items->Count == 0)
  9988. {
  9989. return NULL;
  9990. }
  9991. else
  9992. {
  9993. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9994. TListItem * Item = CurrentItem;
  9995. if (SkipCurrent)
  9996. {
  9997. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9998. }
  9999. while (true)
  10000. {
  10001. bool Matches = false;
  10002. switch (WinConfiguration->PanelSearch)
  10003. {
  10004. case isNameStartOnly:
  10005. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10006. break;
  10007. case isName:
  10008. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10009. break;
  10010. case isAll:
  10011. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10012. int Index = 0;
  10013. while ((Index < ColCount) && !Matches)
  10014. {
  10015. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10016. if (Visible)
  10017. {
  10018. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10019. }
  10020. Index++;
  10021. }
  10022. break;
  10023. }
  10024. if (Matches)
  10025. {
  10026. return Item;
  10027. }
  10028. Item = GetNextFile(Item, Reverse);
  10029. if (Item == CurrentItem)
  10030. {
  10031. return NULL;
  10032. }
  10033. }
  10034. }
  10035. }
  10036. //---------------------------------------------------------------------------
  10037. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10038. {
  10039. ResetIncrementalSearch();
  10040. }
  10041. //---------------------------------------------------------------------------
  10042. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10043. {
  10044. if (Message.CharCode == VK_TAB)
  10045. {
  10046. if (!FIncrementalSearch.IsEmpty())
  10047. {
  10048. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10049. bool Reverse = Shift.Contains(ssShift);
  10050. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10051. Message.Result = 1;
  10052. return;
  10053. }
  10054. }
  10055. else if (Message.CharCode == VK_ESCAPE)
  10056. {
  10057. if (!FIncrementalSearch.IsEmpty())
  10058. {
  10059. ResetIncrementalSearch();
  10060. Message.Result = 1;
  10061. return;
  10062. }
  10063. }
  10064. TForm::Dispatch(&Message);
  10065. }
  10066. //---------------------------------------------------------------------------
  10067. void __fastcall TCustomScpExplorerForm::Deactivate()
  10068. {
  10069. TForm::Deactivate();
  10070. // E.g. when switching to an internal editor window
  10071. ResetIncrementalSearch();
  10072. }
  10073. //---------------------------------------------------------------------------
  10074. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10075. {
  10076. // When switching to another application
  10077. ResetIncrementalSearch();
  10078. }
  10079. //---------------------------------------------------------------------------
  10080. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10081. {
  10082. ResetIncrementalSearch();
  10083. }
  10084. //---------------------------------------------------------------------------
  10085. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10086. {
  10087. if (FIncrementalSearching <= 0)
  10088. {
  10089. ResetIncrementalSearch();
  10090. }
  10091. }
  10092. //---------------------------------------------------------------------------
  10093. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10094. {
  10095. CountClicksForWindowPrint(this);
  10096. }
  10097. //---------------------------------------------------------------------------
  10098. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10099. {
  10100. CenterReconnectToolbar();
  10101. }
  10102. //---------------------------------------------------------------------------
  10103. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10104. {
  10105. if (RemoteDirView->ItemFocused != NULL)
  10106. {
  10107. RemoteDirView->ItemFocused->Selected = true;
  10108. }
  10109. }
  10110. //---------------------------------------------------------------------------
  10111. void __fastcall TCustomScpExplorerForm::CloseApp()
  10112. {
  10113. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10114. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10115. // So we clear the flag here.
  10116. DebugAssert(NonVisualDataModule->Busy);
  10117. NonVisualDataModule->EndBusy();
  10118. try
  10119. {
  10120. Close();
  10121. }
  10122. __finally
  10123. {
  10124. NonVisualDataModule->StartBusy();
  10125. }
  10126. }