CustomScpExplorer.cpp 351 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718
  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. #ifndef NO_RESOURCES
  51. #pragma resource "*.dfm"
  52. #endif
  53. //---------------------------------------------------------------------------
  54. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  55. static const int SessionPanelCount = 4;
  56. //---------------------------------------------------------------------------
  57. class TMutexGuard
  58. {
  59. public:
  60. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  61. int Timeout = 5000)
  62. {
  63. FMutex = NULL;
  64. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  65. if (WaitResult == WAIT_TIMEOUT)
  66. {
  67. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  68. }
  69. else
  70. {
  71. FMutex = AMutex;
  72. }
  73. }
  74. ~TMutexGuard()
  75. {
  76. if (FMutex != NULL)
  77. {
  78. ReleaseMutex(FMutex);
  79. }
  80. }
  81. private:
  82. HANDLE FMutex;
  83. };
  84. //---------------------------------------------------------------------------
  85. class TWindowLock
  86. {
  87. public:
  88. TWindowLock(TCustomScpExplorerForm * Form) :
  89. FForm(Form)
  90. {
  91. FForm->LockWindow();
  92. }
  93. ~TWindowLock()
  94. {
  95. FForm->UnlockWindow();
  96. }
  97. private:
  98. TCustomScpExplorerForm * FForm;
  99. };
  100. //---------------------------------------------------------------------------
  101. class TAutoBatch
  102. {
  103. public:
  104. TAutoBatch(TCustomScpExplorerForm * Form) :
  105. FForm(Form)
  106. {
  107. FForm->BatchStart(FBatchStorage);
  108. }
  109. ~TAutoBatch()
  110. {
  111. FForm->BatchEnd(FBatchStorage);
  112. }
  113. private:
  114. TCustomScpExplorerForm * FForm;
  115. void * FBatchStorage;
  116. };
  117. //---------------------------------------------------------------------------
  118. struct TTransferOperationParam
  119. {
  120. TTransferOperationParam();
  121. UnicodeString TargetDirectory;
  122. bool Temp;
  123. bool DragDrop;
  124. int Options;
  125. TAutoSwitch Queue;
  126. };
  127. //---------------------------------------------------------------------------
  128. TTransferOperationParam::TTransferOperationParam()
  129. {
  130. Temp = false;
  131. DragDrop = false;
  132. Options = 0;
  133. Queue = asAuto;
  134. }
  135. //---------------------------------------------------------------------------
  136. class TTransferPresetNoteData : public TObject
  137. {
  138. public:
  139. UnicodeString Message;
  140. };
  141. //---------------------------------------------------------------------------
  142. class TTerminalNoteData : public TObject
  143. {
  144. public:
  145. TManagedTerminal * Terminal;
  146. };
  147. //---------------------------------------------------------------------------
  148. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  149. FFormRestored(false),
  150. TForm(Owner)
  151. {
  152. FInvalid = false;
  153. FCurrentSide = osRemote;
  154. FEverShown = false;
  155. FDocks = new TList();
  156. InitControls();
  157. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  158. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  159. NonVisualDataModule->ScpExplorer = this;
  160. RestoreParams();
  161. ConfigurationChanged();
  162. RemoteDirView->Invalidate();
  163. FAutoOperation = false;
  164. FOnFileOperationFinished = NULL;
  165. FForceExecution = false;
  166. FIgnoreNextDialogChar = 0;
  167. FErrorList = NULL;
  168. FSynchronizeProgressForm = NULL;
  169. FProgressForm = NULL;
  170. FRefreshLocalDirectory = false;
  171. FRefreshRemoteDirectory = false;
  172. FDDExtMapFile = NULL;
  173. // CreateMutexW keeps failing with ERROR_NOACCESS
  174. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  175. DebugAssert(FDDExtMutex != NULL);
  176. FDDTargetControl = NULL;
  177. FDelayedDeletionTimer = NULL;
  178. FDelayedDeletionList = new TStringList();
  179. FDDFileList = NULL;
  180. FPendingTempSpaceWarn = false;
  181. FCapturedLog = NULL;
  182. FDragDropOperation = false;
  183. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  184. FAllowTransferPresetAutoSelect = true;
  185. FCopyParamDefault = L"";
  186. FSynchronizeController = NULL;
  187. FPendingQueueActionItem = NULL;
  188. FLockLevel = 0;
  189. FLockSuspendLevel = 0;
  190. FDisabledOnLockSuspend = false;
  191. FAlternativeDelete = false;
  192. FTrayIcon = new ::TTrayIcon(0);
  193. FTrayIcon->OnClick = TrayIconClick;
  194. FMaxQueueLength = 0;
  195. FLastContextPopupScreenPoint = TPoint(-1, -1);
  196. FTransferResumeList = NULL;
  197. FMoveToQueue = false;
  198. FStandaloneEditing = false;
  199. FOnFeedSynchronizeError = NULL;
  200. FNeedSession = false;
  201. FDoNotIdleCurrentTerminal = 0;
  202. FIncrementalSearching = 0;
  203. FIncrementalSearchHaveNext = false;
  204. FEditorManager = new TEditorManager();
  205. FEditorManager->OnFileChange = ExecutedFileChanged;
  206. FEditorManager->OnFileReload = ExecutedFileReload;
  207. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  208. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  209. FLocalEditors = new TList();
  210. FQueueStatus = NULL;
  211. FQueueStatusSection = new TCriticalSection();
  212. FQueueStatusInvalidated = false;
  213. FQueueItemInvalidated = false;
  214. FQueueActedItem = NULL;
  215. FQueueController = new TQueueController(QueueView3);
  216. FUserActionTimer = new TTimer(this);
  217. FUserActionTimer->Enabled = false;
  218. FUserActionTimer->Interval = 10;
  219. FUserActionTimer->OnTimer = UserActionTimer;
  220. FNotes = new TStringList();
  221. FNoteTimer = new TTimer(this);
  222. FNoteTimer->Enabled = false;
  223. FNoteTimer->OnTimer = NoteTimer;
  224. FOnNoteClick = NULL;
  225. FOle32Library = LoadLibrary(L"Ole32.dll");
  226. FDragMoveCursor = FOle32Library != NULL ?
  227. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  228. UseSystemSettings(this);
  229. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  230. static_cast<TObject*>(GetComponent(fcTransferList)));
  231. DebugAssert(TransferList != NULL);
  232. FTransferListHoverIndex = -1;
  233. TransferList->OnChange = TransferListChange;
  234. TransferList->OnDrawItem = TransferListDrawItem;
  235. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  236. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  237. UseDesktopFont(SessionsPageControl);
  238. UpdateSessionsPageControlHeight();
  239. UseDesktopFont(RemoteDirView);
  240. UseDesktopFont(RemoteDriveView);
  241. UseDesktopFont(QueueView3);
  242. UseDesktopFont(QueueLabel);
  243. UseDesktopFont(RemoteStatusBar);
  244. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  245. QueueSplitter->ShowHint = true;
  246. RemotePanelSplitter->ShowHint = true;
  247. UpdateImages();
  248. FCustomCommandMenu = CreateTBXPopupMenu(this);
  249. FCustomCommandLocalFileList = NULL;
  250. FCustomCommandRemoteFileList = NULL;
  251. FSessionColors = new TPngImageList(this);
  252. FSessionColors->ColorDepth = cd32Bit;
  253. AddFixedSessionImages();
  254. SessionsPageControl->Images = FSessionColors;
  255. QueueLabel->FocusControl = QueueView3;
  256. UpdateQueueLabel();
  257. FRemoteDirViewWasFocused = true;
  258. CreateHiddenWindow();
  259. StartUpdates();
  260. }
  261. //---------------------------------------------------------------------------
  262. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  263. {
  264. FInvalid = true;
  265. // this has to be one of the very first things to do
  266. StopUpdateThread();
  267. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  268. TTerminalManager::Instance()->ScpExplorer = NULL;
  269. delete FCustomCommandLocalFileList;
  270. delete FCustomCommandRemoteFileList;
  271. delete FCustomCommandMenu;
  272. delete FTrayIcon;
  273. FTrayIcon = NULL;
  274. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  275. delete FEditorManager;
  276. ForceCloseLocalEditors();
  277. delete FLocalEditors;
  278. HideFileFindDialog();
  279. if (FDelayedDeletionTimer)
  280. {
  281. DoDelayedDeletion(NULL);
  282. SAFE_DESTROY(FDelayedDeletionTimer);
  283. }
  284. SAFE_DESTROY(FDelayedDeletionList);
  285. // sometimes we do not get DDEnd so the list is not released
  286. SAFE_DESTROY(FDDFileList);
  287. DebugAssert(FSynchronizeController == NULL);
  288. CloseHandle(FDDExtMutex);
  289. FDDExtMutex = NULL;
  290. FreeLibrary(FOle32Library);
  291. FOle32Library = NULL;
  292. FDragMoveCursor = NULL;
  293. DebugAssert(!FErrorList);
  294. if (FEverShown)
  295. {
  296. // when window is never shown (like when running command-line operation),
  297. // particularly window site is not restored correctly (BoundsRect value set
  298. // in RestoreForm gets lost during handle allocation), so we do not want
  299. // it to be stored
  300. StoreParams();
  301. }
  302. Terminal = NULL;
  303. Queue = NULL;
  304. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  305. NonVisualDataModule->ScpExplorer = NULL;
  306. delete FQueueController;
  307. FQueueController = NULL;
  308. delete FQueueStatusSection;
  309. FQueueStatusSection = NULL;
  310. delete FQueueStatus;
  311. FQueueStatus = NULL;
  312. delete FUserActionTimer;
  313. FUserActionTimer = NULL;
  314. delete FNoteTimer;
  315. delete FNotes;
  316. delete FNoteData;
  317. SAFE_DESTROY(FDocks);
  318. SessionsPageControl->Images = NULL;
  319. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  320. SAFE_DESTROY(FSessionColors);
  321. SAFE_DESTROY(FSessionsDragDropFilesEx);
  322. SAFE_DESTROY(FHistoryMenu[0][0]);
  323. SAFE_DESTROY(FHistoryMenu[0][1]);
  324. SAFE_DESTROY(FHistoryMenu[1][0]);
  325. SAFE_DESTROY(FHistoryMenu[1][1]);
  326. if (FHiddenWindow != NULL)
  327. {
  328. DestroyWindow(FHiddenWindow);
  329. FHiddenWindow = NULL;
  330. }
  331. }
  332. //---------------------------------------------------------------------------
  333. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  334. {
  335. std::unique_ptr<TSessionData> Data;
  336. if (!Session.IsEmpty())
  337. {
  338. bool DefaultsOnly;
  339. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  340. }
  341. TTerminalManager * Manager = TTerminalManager::Instance();
  342. for (int Index = 0; Index < Manager->Count; Index++)
  343. {
  344. TTerminal * Terminal = Manager->Terminals[Index];
  345. if (Session.IsEmpty() ||
  346. Terminal->SessionData->IsSameSite(Data.get()))
  347. {
  348. if (Path.IsEmpty())
  349. {
  350. Terminal->ClearCaches();
  351. }
  352. else
  353. {
  354. Terminal->DirectoryModified(Path, true);
  355. }
  356. }
  357. }
  358. // We should flag a pending refresh for the background terminals or busy foreground terminals
  359. if ((Terminal != NULL) && Terminal->Active &&
  360. CanCommandLineFromAnotherInstance() &&
  361. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  362. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  363. {
  364. Terminal->ReloadDirectory();
  365. }
  366. }
  367. //---------------------------------------------------------------------------
  368. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  369. {
  370. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  371. size_t MessageSize = sizeof(TCopyDataMessage);
  372. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  373. if (Result)
  374. {
  375. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  376. Result = (Message.Version == TCopyDataMessage::Version1);
  377. if (Result)
  378. {
  379. switch (Message.Command)
  380. {
  381. case TCopyDataMessage::CommandCanCommandLine:
  382. Result = CanCommandLineFromAnotherInstance();
  383. break;
  384. case TCopyDataMessage::CommandCommandLine:
  385. {
  386. UnicodeString CommandLine(Message.CommandLine);
  387. Result = CommandLineFromAnotherInstance(CommandLine);
  388. }
  389. break;
  390. case TCopyDataMessage::RefreshPanel:
  391. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  392. break;
  393. case TCopyDataMessage::MainWindowCheck:
  394. Result = true;
  395. break;
  396. default:
  397. Result = false;
  398. break;
  399. }
  400. }
  401. }
  402. Message.Result = Result ? 1 : 0;
  403. }
  404. //---------------------------------------------------------------------------
  405. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  406. {
  407. WNDCLASS WindowClass = {0};
  408. WindowClass.lpfnWndProc = DefWindowProc;
  409. WindowClass.hInstance = HInstance;
  410. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  411. FHiddenWindow = NULL;
  412. if (RegisterClass(&WindowClass))
  413. {
  414. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  415. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  416. }
  417. }
  418. //---------------------------------------------------------------------------
  419. bool __fastcall TCustomScpExplorerForm::CanConsole()
  420. {
  421. return
  422. (Terminal != NULL) &&
  423. Terminal->Active &&
  424. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  425. }
  426. //---------------------------------------------------------------------------
  427. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  428. {
  429. bool Result = Visible && !NonVisualDataModule->Busy;
  430. return Result;
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  434. const UnicodeString & CommandLine)
  435. {
  436. TProgramParams Params(CommandLine);
  437. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  438. if (Result)
  439. {
  440. NonVisualDataModule->StartBusy();
  441. try
  442. {
  443. // this action is initiated from another process,
  444. // so it's likely that our window is not visible,
  445. // and user won't see what is going on
  446. Application->BringToFront();
  447. // reload sessions as we may be asked to open a session
  448. // just stored by another instance
  449. StoredSessions->Reload();
  450. UnicodeString SessionName = Params.Param[1];
  451. std::unique_ptr<TObjectList> DataList(new TObjectList());
  452. UnicodeString DownloadFile; // unused
  453. try
  454. {
  455. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  456. }
  457. catch (EAbort &)
  458. {
  459. // Happens when opening session in PuTTY and in other situations,
  460. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  461. // to process the command-line on its own (or to pass it to yet another instance)
  462. }
  463. if (DataList->Count > 0)
  464. {
  465. TTerminalManager * Manager = TTerminalManager::Instance();
  466. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  467. }
  468. }
  469. __finally
  470. {
  471. NonVisualDataModule->EndBusy();
  472. }
  473. }
  474. return Result;
  475. }
  476. //---------------------------------------------------------------------------
  477. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  478. {
  479. if (FTerminal != value)
  480. {
  481. TerminalChanging();
  482. DoSetTerminal(value, false);
  483. }
  484. }
  485. //---------------------------------------------------------------------------
  486. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  487. {
  488. FTerminal = value;
  489. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  490. FAllowTransferPresetAutoSelect = false;
  491. try
  492. {
  493. TerminalChanged(Replace);
  494. }
  495. __finally
  496. {
  497. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  498. }
  499. if (Terminal != NULL)
  500. {
  501. TransferPresetAutoSelect();
  502. }
  503. }
  504. //---------------------------------------------------------------------------
  505. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  506. {
  507. DoSetTerminal(value, true);
  508. }
  509. //---------------------------------------------------------------------------
  510. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  511. {
  512. if (FTerminal != NULL)
  513. {
  514. UpdateTerminal(Terminal);
  515. }
  516. }
  517. //---------------------------------------------------------------------------
  518. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  519. {
  520. if (Terminal != NULL)
  521. {
  522. UpdateSessionColor((TColor)Terminal->StateData->Color);
  523. }
  524. DoTerminalListChanged();
  525. DebugAssert(!IsLocalBrowserMode()); // TODO
  526. if (Replaced)
  527. {
  528. RemoteDirView->ReplaceTerminal(Terminal);
  529. }
  530. else
  531. {
  532. RemoteDirView->Terminal = Terminal;
  533. }
  534. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  535. if (Terminal != NULL)
  536. {
  537. if (Terminal->Active)
  538. {
  539. Terminal->RefreshDirectory();
  540. }
  541. if (WinConfiguration->PreservePanelState)
  542. {
  543. if (Terminal->RemoteExplorerState != NULL)
  544. {
  545. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  546. }
  547. else
  548. {
  549. DirView(osRemote)->ClearState();
  550. }
  551. }
  552. if (!Replaced && Terminal->Active)
  553. {
  554. InitStatusBar();
  555. }
  556. }
  557. UpdateTransferList();
  558. // Update panels Enable state before refreshing the labels
  559. UpdateControls();
  560. }
  561. //---------------------------------------------------------------------------
  562. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  563. {
  564. if (Queue != value)
  565. {
  566. if (FQueue != NULL)
  567. {
  568. FQueue->OnListUpdate = NULL;
  569. FQueue->OnQueueItemUpdate = NULL;
  570. }
  571. FQueue = value;
  572. if (FQueue != NULL)
  573. {
  574. DebugAssert(FQueue->OnListUpdate == NULL);
  575. FQueue->OnListUpdate = QueueListUpdate;
  576. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  577. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  578. }
  579. QueueChanged();
  580. }
  581. }
  582. //---------------------------------------------------------------------------
  583. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  584. TListItem * Item)
  585. {
  586. if (FQueueActedItem == Item)
  587. {
  588. FQueueActedItem = NULL;
  589. if ((QueueView3->PopupMenu != NULL) &&
  590. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  591. {
  592. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  593. ReleaseCapture();
  594. }
  595. }
  596. if (Item->Data == FPendingQueueActionItem)
  597. {
  598. FPendingQueueActionItem = NULL;
  599. }
  600. }
  601. //---------------------------------------------------------------------------
  602. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  603. {
  604. {
  605. TGuard Guard(FQueueStatusSection);
  606. FQueueStatusInvalidated = false;
  607. if (FQueue != NULL)
  608. {
  609. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  610. }
  611. }
  612. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  613. {
  614. FMaxQueueLength = FQueueStatus->Count;
  615. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  616. }
  617. FQueueController->UpdateQueueStatus(FQueueStatus);
  618. SetQueueProgress();
  619. UpdateQueueView();
  620. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  621. if (IsEmpty && (Terminal != NULL))
  622. {
  623. TOnceDoneOperation OnceDoneOperation =
  624. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  625. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  626. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  627. {
  628. FQueue->Enabled = false;
  629. }
  630. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  631. {
  632. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  633. }
  634. }
  635. }
  636. //---------------------------------------------------------------------------
  637. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  638. {
  639. UnicodeString Result;
  640. if (FQueueStatus != NULL)
  641. {
  642. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  643. {
  644. TFileOperationProgressType * ProgressData =
  645. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  646. if ((ProgressData != NULL) && ProgressData->InProgress)
  647. {
  648. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  649. }
  650. }
  651. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  652. {
  653. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  654. }
  655. }
  656. return Result;
  657. }
  658. //---------------------------------------------------------------------------
  659. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  660. {
  661. ComponentVisible[fcQueueView] =
  662. (WinConfiguration->QueueView.Show == qvShow) ||
  663. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  664. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  665. }
  666. //---------------------------------------------------------------------------
  667. void __fastcall TCustomScpExplorerForm::QueueChanged()
  668. {
  669. if (FQueueStatus != NULL)
  670. {
  671. delete FQueueStatus;
  672. FQueueStatus = NULL;
  673. }
  674. UpdateQueueStatus(true);
  675. }
  676. //---------------------------------------------------------------------------
  677. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  678. {
  679. if (FQueue == Queue)
  680. {
  681. FQueueStatusInvalidated = true;
  682. }
  683. }
  684. //---------------------------------------------------------------------------
  685. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  686. TQueueItem * Item)
  687. {
  688. if (FQueue == Queue)
  689. {
  690. TGuard Guard(FQueueStatusSection);
  691. // this may be running in parallel with QueueChanged
  692. if (FQueueStatus != NULL)
  693. {
  694. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  695. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  696. {
  697. FRefreshLocalDirectory = (QueueItem == NULL) ||
  698. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  699. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  700. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  701. }
  702. if (QueueItem != NULL)
  703. {
  704. QueueItem->UserData = (void*)true;
  705. FQueueItemInvalidated = true;
  706. }
  707. }
  708. }
  709. }
  710. //---------------------------------------------------------------------------
  711. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  712. {
  713. // during standalone editing, we have no way to see/control queue,
  714. // so we have to always popup prompts automatically
  715. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  716. }
  717. //---------------------------------------------------------------------------
  718. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  719. {
  720. if (FQueueStatus != NULL)
  721. {
  722. bool QueueAutoPopup = IsQueueAutoPopup();
  723. bool NeedRefresh = FQueueController->NeedRefresh();
  724. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  725. FQueueItemInvalidated = false;
  726. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  727. bool Updated = false;
  728. TQueueItemProxy * QueueItem;
  729. bool WasUserAction;
  730. for (int Index = 0; Index < Limit; Index++)
  731. {
  732. bool Update = false;
  733. QueueItem = FQueueStatus->Items[Index];
  734. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  735. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  736. (FPendingQueueActionItem == NULL))
  737. {
  738. FPendingQueueActionItem = QueueItem;
  739. FUserActionTimer->Enabled = true;
  740. }
  741. if ((bool)QueueItem->UserData)
  742. {
  743. QueueItem->UserData = (void*)false;
  744. QueueItem->Update();
  745. Updated = true;
  746. Update = true;
  747. }
  748. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  749. {
  750. Update = true;
  751. }
  752. if (Update)
  753. {
  754. FQueueController->RefreshQueueItem(QueueItem);
  755. }
  756. }
  757. if (Updated)
  758. {
  759. NonVisualDataModule->UpdateNonVisibleActions();
  760. SetQueueProgress();
  761. }
  762. }
  763. }
  764. //---------------------------------------------------------------------------
  765. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  766. {
  767. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  768. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  769. }
  770. //---------------------------------------------------------------------------
  771. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  772. {
  773. if (Progress >= 0)
  774. {
  775. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  776. }
  777. else
  778. {
  779. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  780. }
  781. }
  782. //---------------------------------------------------------------------------
  783. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  784. {
  785. int OverallProgress;
  786. // FProgressForm is null when this is called from SetQueueProgress
  787. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  788. {
  789. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  790. }
  791. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  792. {
  793. OverallProgress = ProgressData->OverallProgress();
  794. }
  795. else
  796. {
  797. OverallProgress = -1;
  798. }
  799. SetTaskbarListProgressValue(OverallProgress);
  800. }
  801. //---------------------------------------------------------------------------
  802. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  803. {
  804. TTerminalManager::Instance()->QueueStatusUpdated();
  805. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  806. {
  807. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  808. {
  809. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  810. {
  811. TFileOperationProgressType * ProgressData = NULL;
  812. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  813. {
  814. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  815. }
  816. if ((ProgressData != NULL) &&
  817. ProgressData->InProgress)
  818. {
  819. SetTaskbarListProgressValue(ProgressData);
  820. }
  821. else
  822. {
  823. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  824. }
  825. }
  826. else
  827. {
  828. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  829. }
  830. }
  831. else
  832. {
  833. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  834. }
  835. }
  836. UpdateQueueLabel();
  837. }
  838. //---------------------------------------------------------------------------
  839. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  840. {
  841. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  842. if (FQueueStatus != NULL)
  843. {
  844. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  845. if (ActiveAndPendingPrimaryCount > 0)
  846. {
  847. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  848. }
  849. }
  850. QueueLabel->Caption = Caption;
  851. }
  852. //---------------------------------------------------------------------------
  853. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  854. {
  855. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  856. static_cast<TComponent*>(GetComponent(fcTransferList)));
  857. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  858. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  859. TransferList->Strings->BeginUpdate();
  860. try
  861. {
  862. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  863. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  864. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  865. if (FTransferDropDownHint.IsEmpty())
  866. {
  867. FTransferDropDownHint = TransferDropDown->Hint;
  868. }
  869. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  870. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  871. TransferDropDown->Text = StripHotkey(Name);
  872. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  873. (FTransferDropDownHint, StripHotkey(Name),
  874. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  875. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  876. // update the label, otherwise when it is updated only on the first draw
  877. // of the list, it is drawn "bold" for some reason
  878. FTransferListHoverIndex = TransferList->ItemIndex;
  879. UpdateTransferLabel();
  880. }
  881. __finally
  882. {
  883. TransferList->Strings->EndUpdate();
  884. }
  885. }
  886. //---------------------------------------------------------------------------
  887. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  888. {
  889. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  890. DebugAssert(Toolbar != NULL);
  891. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  892. }
  893. //---------------------------------------------------------------------------
  894. void __fastcall TCustomScpExplorerForm::UpdateActions()
  895. {
  896. TForm::UpdateActions();
  897. if (ComponentVisible[fcCustomCommandsBand])
  898. {
  899. UpdateCustomCommandsToolbar();
  900. }
  901. }
  902. //---------------------------------------------------------------------------
  903. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  904. {
  905. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  906. }
  907. //---------------------------------------------------------------------------
  908. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  909. {
  910. Color = GetBtnFaceColor();
  911. DebugAssert(Configuration && RemoteDirView);
  912. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  913. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  914. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  915. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  916. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  917. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  918. {
  919. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  920. // selection is not redrawn automatically when RowSelect changes
  921. RemoteDirView->Invalidate();
  922. }
  923. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  924. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  925. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  926. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  927. UpdateSessionsPageControlHeight();
  928. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  929. UpdateToolbarDisplayMode();
  930. UpdateTransferList();
  931. if (Terminal != NULL)
  932. {
  933. TransferPresetAutoSelect();
  934. }
  935. UpdateQueueView();
  936. UpdateControls();
  937. // this can be called even before constuctor finishes.
  938. if (FEditorManager != NULL)
  939. {
  940. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  941. }
  942. // this can be called even before constuctor finishes.
  943. if (FLocalEditors != NULL)
  944. {
  945. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  946. {
  947. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  948. }
  949. }
  950. if (ComponentVisible[fcCustomCommandsBand])
  951. {
  952. UpdateCustomCommandsToolbar();
  953. }
  954. // show only when keeping queue items forever,
  955. // otherwise, its enough to have in in the context menu
  956. QueueDeleteAllDoneQueueToolbarItem->Visible =
  957. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  958. if (FFileColorsCurrent != WinConfiguration->FileColors)
  959. {
  960. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  961. FFileColorsCurrent = WinConfiguration->FileColors;
  962. FileColorsChanged();
  963. }
  964. }
  965. //---------------------------------------------------------------------------
  966. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  967. {
  968. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  969. DirView->Invalidate();
  970. }
  971. //---------------------------------------------------------------------------
  972. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  973. {
  974. DoFileColorsChanged(RemoteDirView);
  975. }
  976. //---------------------------------------------------------------------------
  977. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  978. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  979. void * /*Arg*/)
  980. {
  981. if (Token != NULL)
  982. {
  983. TForm * Editor = dynamic_cast<TForm*>(Token);
  984. DebugAssert(Editor != NULL);
  985. ReconfigureEditorForm(Editor);
  986. }
  987. }
  988. //---------------------------------------------------------------------------
  989. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  990. {
  991. WinConfiguration->DDTransferConfirmation = asOn;
  992. }
  993. //---------------------------------------------------------------------------
  994. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  995. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  996. {
  997. // noop
  998. }
  999. //---------------------------------------------------------------------------
  1000. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1001. TTransferDirection Direction, TTransferType Type, bool Temp,
  1002. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1003. bool Confirm, bool DragDrop, int Options)
  1004. {
  1005. bool Result = true;
  1006. DebugAssert(Terminal && Terminal->Active);
  1007. // these parameters are known in advance
  1008. int Params =
  1009. FLAGMASK(Type == ttMove, cpDelete);
  1010. bool ToTemp = (Temp && (Direction == tdToLocal));
  1011. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1012. {
  1013. bool DisableNewerOnly =
  1014. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1015. ToTemp;
  1016. Options |=
  1017. FLAGMASK(ToTemp, coTemp) |
  1018. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1019. coDoNotShowAgain;
  1020. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1021. int CopyParamAttrs = (Direction == tdToRemote ?
  1022. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1023. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1024. int OutputOptions =
  1025. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1026. cooDoNotShowAgain);
  1027. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1028. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1029. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1030. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1031. if (Result)
  1032. {
  1033. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1034. {
  1035. if (DragDrop)
  1036. {
  1037. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1038. {
  1039. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1040. NULL, 0, EnableDDTransferConfirmation, NULL);
  1041. }
  1042. WinConfiguration->DDTransferConfirmation = asOff;
  1043. }
  1044. else
  1045. {
  1046. WinConfiguration->ConfirmTransferring = false;
  1047. }
  1048. }
  1049. else
  1050. {
  1051. // User explicitly unchecked "do not show again",
  1052. // so show him the dialog the next time
  1053. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1054. {
  1055. WinConfiguration->DDTransferConfirmation = asOn;
  1056. }
  1057. }
  1058. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1059. }
  1060. }
  1061. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1062. {
  1063. Configuration->Usage->Inc(L"TransfersOnBackground");
  1064. // these parameter are known only after transfer dialog
  1065. Params |=
  1066. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1067. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1068. Result = false;
  1069. ClearTransferSourceSelection(Direction);
  1070. }
  1071. return Result;
  1072. }
  1073. //---------------------------------------------------------------------------
  1074. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1075. {
  1076. if (FOnFileOperationFinished != NULL)
  1077. {
  1078. FOnFileOperationFinished(UnicodeString(), true);
  1079. }
  1080. else
  1081. {
  1082. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1083. if (HasDirView[Side])
  1084. {
  1085. DirView(Side)->SelectAll(smNone);
  1086. }
  1087. }
  1088. }
  1089. //---------------------------------------------------------------------------
  1090. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1091. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1092. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1093. int Params)
  1094. {
  1095. DebugAssert(Queue != NULL);
  1096. bool SingleFile = false;
  1097. if (FileList->Count == 1)
  1098. {
  1099. if (Direction == tdToRemote)
  1100. {
  1101. UnicodeString FileName = FileList->Strings[0];
  1102. SingleFile = FileExists(ApiPath(FileName));
  1103. }
  1104. else
  1105. {
  1106. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1107. SingleFile = !File->IsDirectory;
  1108. }
  1109. }
  1110. TQueueItem * QueueItem;
  1111. if (Direction == tdToRemote)
  1112. {
  1113. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1114. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1115. }
  1116. else
  1117. {
  1118. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1119. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1120. }
  1121. AddQueueItem(Queue, QueueItem, Terminal);
  1122. }
  1123. //---------------------------------------------------------------------------
  1124. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1125. {
  1126. if (Queue->IsEmpty)
  1127. {
  1128. Terminal->QueueOperationStart = Now();
  1129. }
  1130. Queue->AddItem(QueueItem);
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1134. {
  1135. }
  1136. //---------------------------------------------------------------------------
  1137. void __fastcall TCustomScpExplorerForm::InitControls()
  1138. {
  1139. DebugAssert(FDocks != NULL);
  1140. for (int Index = 0; Index < ComponentCount; Index++)
  1141. {
  1142. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1143. if ((Dock != NULL) && (Dock->Tag == 0))
  1144. {
  1145. FDocks->Add(Dock);
  1146. }
  1147. }
  1148. CollectItemsWithTextDisplayMode(this);
  1149. }
  1150. //---------------------------------------------------------------------------
  1151. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1152. {
  1153. ConfigurationChanged();
  1154. QueuePanel->Height =
  1155. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1156. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1157. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1158. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1159. UpdateCustomCommandsToolbar();
  1160. }
  1161. //---------------------------------------------------------------------------
  1162. void __fastcall TCustomScpExplorerForm::StoreParams()
  1163. {
  1164. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1165. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1166. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1167. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1168. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1169. }
  1170. //---------------------------------------------------------------------------
  1171. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1172. {
  1173. if (!FFormRestored)
  1174. {
  1175. FFormRestored = true;
  1176. RestoreFormParams();
  1177. }
  1178. TForm::CreateParams(Params);
  1179. }
  1180. //---------------------------------------------------------------------------
  1181. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1182. {
  1183. DebugAssert(FDocks != NULL);
  1184. for (int Index = 0; Index < FDocks->Count; Index++)
  1185. {
  1186. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1187. }
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1191. {
  1192. SetDockAllowDrag(true);
  1193. while (!ButtonsStr.IsEmpty())
  1194. {
  1195. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1196. UnicodeString Name = CutToChar(S, L'=', true);
  1197. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1198. {
  1199. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1200. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1201. {
  1202. while (!S.IsEmpty())
  1203. {
  1204. UnicodeString S2 = CutToChar(S, L',', true);
  1205. UnicodeString Name = CutToChar(S2, L':', true);
  1206. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1207. {
  1208. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1209. if (SameText(GetToolbarItemName(Item), Name))
  1210. {
  1211. // forward compatibility
  1212. UnicodeString S3 = CutToChar(S2, L':', true);
  1213. bool Visible = StrToIntDef(S3, 0);
  1214. Item->Visible = Visible;
  1215. }
  1216. }
  1217. }
  1218. }
  1219. }
  1220. }
  1221. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1222. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1223. }
  1224. //---------------------------------------------------------------------------
  1225. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1226. {
  1227. UnicodeString Result;
  1228. SetDockAllowDrag(true);
  1229. Result = ::GetToolbarsLayoutStr(this);
  1230. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1231. return Result;
  1232. }
  1233. //---------------------------------------------------------------------------
  1234. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1235. {
  1236. UnicodeString Result;
  1237. if (Item->Action != NULL)
  1238. {
  1239. Result = Item->Action->Name;
  1240. Result = RemoveSuffix(Result, L"Action");
  1241. }
  1242. else
  1243. {
  1244. Result = Item->Name;
  1245. Result = RemoveSuffix(Result, L"SubmenuItem");
  1246. Result = RemoveSuffix(Result, L"Item");
  1247. }
  1248. return Result;
  1249. }
  1250. //---------------------------------------------------------------------------
  1251. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1252. {
  1253. UnicodeString Result;
  1254. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1255. {
  1256. UnicodeString ToolbarStr;
  1257. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1258. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1259. {
  1260. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1261. {
  1262. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1263. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1264. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1265. // and save the changes here only.
  1266. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1267. {
  1268. UnicodeString Name = GetToolbarItemName(Item);
  1269. DebugAssert(Name.Pos(L"TBX") == 0);
  1270. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1271. }
  1272. }
  1273. }
  1274. if (!ToolbarStr.IsEmpty())
  1275. {
  1276. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1277. }
  1278. }
  1279. return Result;
  1280. }
  1281. //---------------------------------------------------------------------------
  1282. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1283. {
  1284. DebugAssert(FProgressForm == NULL);
  1285. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1286. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1287. FProgressForm->DeleteLocalToRecycleBin =
  1288. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1289. FProgressForm->DeleteRemoteToRecycleBin =
  1290. (Terminal != NULL) &&
  1291. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1292. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1293. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1294. LockWindow();
  1295. if (FTaskbarList != NULL)
  1296. {
  1297. // Actually, do not know what hides the progress once the operation finishes
  1298. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1299. SetTaskbarListProgressState(TBPF_NORMAL);
  1300. }
  1301. }
  1302. //---------------------------------------------------------------------------
  1303. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1304. {
  1305. UnlockWindow();
  1306. SAFE_DESTROY(FProgressForm);
  1307. SetQueueProgress();
  1308. }
  1309. //---------------------------------------------------------------------------
  1310. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1311. TFileOperationProgressType & ProgressData)
  1312. {
  1313. // Download to temporary local directory
  1314. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1315. {
  1316. bool Continue = true;
  1317. FPendingTempSpaceWarn = false;
  1318. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1319. if (!Continue)
  1320. {
  1321. Abort();
  1322. }
  1323. }
  1324. // operation is being executed and we still didn't show up progress form
  1325. if (ProgressData.InProgress && !FProgressForm)
  1326. {
  1327. CreateProgressForm(NULL);
  1328. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1329. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1330. }
  1331. // operation is finished (or terminated), so we hide progress form
  1332. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1333. {
  1334. DestroyProgressForm();
  1335. if (ProgressData.Operation == foCalculateSize)
  1336. {
  1337. // When calculating size before transfer, the abort caused by
  1338. // cancel flag set due to "MoveToQueue" below,
  1339. // is not propagated back to us in ExecuteFileOperation,
  1340. // as it is silently swallowed.
  1341. // So we explicitly (re)throw it here.
  1342. if (FMoveToQueue)
  1343. {
  1344. Abort();
  1345. }
  1346. }
  1347. else
  1348. {
  1349. if ((ProgressData.Cancel == csContinue) &&
  1350. !FAutoOperation)
  1351. {
  1352. OperationComplete(ProgressData.StartTime);
  1353. }
  1354. }
  1355. }
  1356. if (FProgressForm &&
  1357. // Can happen only during synchronization, when ending one of the operations
  1358. ProgressData.InProgress)
  1359. {
  1360. FProgressForm->SetProgressData(ProgressData);
  1361. if (FTaskbarList != NULL)
  1362. {
  1363. DebugAssert(ProgressData.InProgress);
  1364. SetTaskbarListProgressValue(&ProgressData);
  1365. }
  1366. if (FProgressForm->Cancel > csContinue)
  1367. {
  1368. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1369. if (FProgressForm->Cancel == csCancelFile)
  1370. {
  1371. FProgressForm->ClearCancel();
  1372. }
  1373. // cancel cancels even the move
  1374. FMoveToQueue = false;
  1375. }
  1376. else if (FProgressForm->MoveToQueue)
  1377. {
  1378. FMoveToQueue = true;
  1379. ProgressData.SetCancelAtLeast(csCancel);
  1380. }
  1381. if ((FTransferResumeList != NULL) &&
  1382. ProgressData.InProgress &&
  1383. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1384. !ProgressData.FullFileName.IsEmpty())
  1385. {
  1386. if ((FTransferResumeList->Count == 0) ||
  1387. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1388. {
  1389. // note that we do not recognize directories from files here
  1390. FTransferResumeList->Add(ProgressData.FullFileName);
  1391. }
  1392. }
  1393. }
  1394. }
  1395. //---------------------------------------------------------------------------
  1396. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1397. const TDateTime & StartTime)
  1398. {
  1399. if (GUIConfiguration->BeepOnFinish &&
  1400. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1401. {
  1402. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1403. DWORD Sound;
  1404. if (!ExtractFileExt(BeepSound).IsEmpty())
  1405. {
  1406. Sound = SND_FILENAME;
  1407. }
  1408. else
  1409. {
  1410. Sound = SND_ALIAS;
  1411. }
  1412. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1413. }
  1414. }
  1415. //---------------------------------------------------------------------------
  1416. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1417. TFileOperationProgressType & ProgressData)
  1418. {
  1419. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1420. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1421. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1422. FileOperationProgress(ProgressData);
  1423. }
  1424. //---------------------------------------------------------------------------
  1425. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1426. {
  1427. UnicodeString Result;
  1428. if (FProgressForm != NULL)
  1429. {
  1430. Result = FProgressForm->ProgressStr();
  1431. }
  1432. return Result;
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1436. bool DragDrop)
  1437. {
  1438. return (!DragDrop && (DropSourceControl == NULL)) ||
  1439. (DropSourceControl == DirView(osOther));
  1440. }
  1441. //---------------------------------------------------------------------------
  1442. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1443. TFileOperation Operation, TOperationSide Side,
  1444. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1445. TOnceDoneOperation & OnceDoneOperation)
  1446. {
  1447. if (!FAutoOperation)
  1448. {
  1449. // no selection on "/upload", form servers only as event handler
  1450. // (it is not displayed)
  1451. if (PanelOperation(Side, FDragDropOperation) &&
  1452. Visible && (Operation != foCalculateSize) &&
  1453. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1454. {
  1455. if (FOnFileOperationFinished != NULL)
  1456. {
  1457. FOnFileOperationFinished(FileName, Success);
  1458. }
  1459. else
  1460. {
  1461. TCustomDirView * DView = DirView(Side);
  1462. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1463. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1464. // this can happen when local drive is unplugged in the middle of the operation
  1465. if (Item != NULL)
  1466. {
  1467. if (Success) Item->Selected = false;
  1468. DView->MakeProgressVisible(Item);
  1469. }
  1470. }
  1471. }
  1472. if ((Operation == foCopy) || (Operation == foMove))
  1473. {
  1474. DebugAssert(!IsLocalBrowserMode()); // TODO
  1475. if (Side == osLocal)
  1476. {
  1477. Configuration->Usage->Inc(L"UploadedFiles");
  1478. }
  1479. else
  1480. {
  1481. Configuration->Usage->Inc(L"DownloadedFiles");
  1482. }
  1483. }
  1484. }
  1485. if (Success && (FSynchronizeController != NULL))
  1486. {
  1487. DebugAssert(!IsSideLocalBrowser(osRemote));
  1488. if (Operation == foCopy)
  1489. {
  1490. DebugAssert(Side == osLocal);
  1491. FSynchronizeController->LogOperation(soUpload, FileName);
  1492. }
  1493. else if (Operation == foDelete)
  1494. {
  1495. DebugAssert(Side == osRemote);
  1496. FSynchronizeController->LogOperation(soDelete, FileName);
  1497. }
  1498. }
  1499. if (FProgressForm)
  1500. {
  1501. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1502. }
  1503. }
  1504. //---------------------------------------------------------------------------
  1505. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1506. TFileOperation Operation, TOperationSide Side,
  1507. bool Temp, const UnicodeString & FileName, Boolean Success,
  1508. TOnceDoneOperation & OnceDoneOperation)
  1509. {
  1510. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1511. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1512. OnceDoneOperation);
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1516. {
  1517. return false;
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1521. {
  1522. return false;
  1523. }
  1524. //---------------------------------------------------------------------------
  1525. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1526. {
  1527. return NULL;
  1528. }
  1529. //---------------------------------------------------------------------------
  1530. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1531. {
  1532. DebugAssert(GetSide(Side) == osRemote);
  1533. DebugUsedParam(Side);
  1534. return RemoteDirView;
  1535. }
  1536. //---------------------------------------------------------------------------
  1537. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1538. {
  1539. DebugAssert(GetSide(Side) == osRemote);
  1540. DebugUsedParam(Side);
  1541. return RemoteDriveView;
  1542. }
  1543. //---------------------------------------------------------------------------
  1544. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1545. {
  1546. DebugAssert(GetSide(Side) == osRemote);
  1547. DebugUsedParam(Side);
  1548. return (Terminal != NULL) && Terminal->Active;
  1549. }
  1550. //---------------------------------------------------------------------------
  1551. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1552. TOperationSide Side, int FilesOnly)
  1553. {
  1554. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1555. }
  1556. //---------------------------------------------------------------------------
  1557. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1558. TOperationSide Side, int FilesOnly)
  1559. {
  1560. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1561. }
  1562. //---------------------------------------------------------------------------
  1563. struct THistoryItemData
  1564. {
  1565. short int Side;
  1566. short int Index;
  1567. };
  1568. //---------------------------------------------------------------------------
  1569. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1570. {
  1571. DirView(Side)->HistoryGo(Index);
  1572. }
  1573. //---------------------------------------------------------------------------
  1574. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1575. {
  1576. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1577. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1578. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1579. }
  1580. //---------------------------------------------------------------------------
  1581. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1582. bool Back)
  1583. {
  1584. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1585. {
  1586. TCustomDirView * DView = DirView(Side);
  1587. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1588. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1589. if (ICount > 10)
  1590. {
  1591. ICount = 10;
  1592. }
  1593. Menu->Items->Clear();
  1594. THistoryItemData Data;
  1595. Data.Side = Side;
  1596. for (short int i = 1; i <= ICount; i++)
  1597. {
  1598. TTBCustomItem * Item = new TTBXItem(Menu);
  1599. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1600. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1601. Item->Hint = DView->HistoryPath[Data.Index];
  1602. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1603. Item->Tag = *reinterpret_cast<int*>(&Data);
  1604. Item->OnClick = HistoryItemClick;
  1605. Menu->Items->Add(Item);
  1606. }
  1607. }
  1608. }
  1609. //---------------------------------------------------------------------------
  1610. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1611. TOperationSide Side, bool Back)
  1612. {
  1613. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1614. {
  1615. // workaround
  1616. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1617. // operator new allocates memory in Pascal code, but calls inline
  1618. // contructor in C++, leading in problems, the function does
  1619. // both in Pascal code
  1620. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1621. UpdateHistoryMenu(Side, Back);
  1622. }
  1623. return FHistoryMenu[Side == osLocal][Back];
  1624. }
  1625. //---------------------------------------------------------------------------
  1626. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1627. TCustomDirView *Sender)
  1628. {
  1629. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1630. UpdateHistoryMenu(Side, true);
  1631. UpdateHistoryMenu(Side, false);
  1632. }
  1633. //---------------------------------------------------------------------------
  1634. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1635. {
  1636. // remote custom commands can be executed only if the server supports shell commands
  1637. // or have secondary shell
  1638. return (FTerminal != NULL) && FTerminal->Active && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1639. }
  1640. //---------------------------------------------------------------------------
  1641. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1642. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1643. {
  1644. int Result;
  1645. TFileCustomCommand RemoteCustomCommand;
  1646. TLocalCustomCommand LocalCustomCommand;
  1647. TFileCustomCommand * NonInteractiveCustomCommand =
  1648. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1649. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1650. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1651. if (FLAGCLEAR(Command.Params, ccLocal))
  1652. {
  1653. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1654. // custom command that does not operate with files can be executed anytime ...
  1655. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1656. {
  1657. // ... but do not show such command in remote file menu
  1658. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1659. {
  1660. Result = AllowedState;
  1661. }
  1662. else
  1663. {
  1664. Result = -1;
  1665. }
  1666. }
  1667. else
  1668. {
  1669. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1670. {
  1671. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1672. }
  1673. else
  1674. {
  1675. Result = -1;
  1676. }
  1677. }
  1678. }
  1679. else
  1680. {
  1681. // custom command that does not operate with files can be executed anytime
  1682. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1683. {
  1684. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1685. }
  1686. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1687. {
  1688. if ((ListType == ccltAll) || (ListType == ccltFile))
  1689. {
  1690. // special case is "diff"-style command that can be executed over any side,
  1691. // if we have both sides
  1692. Result =
  1693. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1694. // directly (instead of EnableSelectedOperation) to pass
  1695. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1696. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1697. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1698. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1699. }
  1700. else if (ListType == ccltBoth)
  1701. {
  1702. DebugAssert(!IsLocalBrowserMode());
  1703. Result = 1;
  1704. }
  1705. else
  1706. {
  1707. Result = -1;
  1708. }
  1709. }
  1710. else
  1711. {
  1712. if ((ListType == ccltAll) ||
  1713. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1714. {
  1715. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1716. }
  1717. else
  1718. {
  1719. Result = -1;
  1720. }
  1721. }
  1722. }
  1723. return Result;
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1727. TStrings * FileList, const TCustomCommandType & ACommand,
  1728. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1729. {
  1730. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1731. {
  1732. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1733. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1734. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1735. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1736. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1737. bool Capture =
  1738. FLAGSET(ACommand.Params, ccShowResults) ||
  1739. FLAGSET(ACommand.Params, ccCopyResults) ||
  1740. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1741. TCaptureOutputEvent OutputEvent = NULL;
  1742. DebugAssert(FCapturedLog == NULL);
  1743. if (Capture)
  1744. {
  1745. FCapturedLog = new TStringList();
  1746. OutputEvent = TerminalCaptureLog;
  1747. }
  1748. try
  1749. {
  1750. if (!RemoteCustomCommand.IsFileCommand(Command))
  1751. {
  1752. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1753. OutputEvent);
  1754. }
  1755. else
  1756. {
  1757. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1758. }
  1759. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1760. {
  1761. if (FLAGSET(ACommand.Params, ccCopyResults))
  1762. {
  1763. CopyToClipboard(FCapturedLog);
  1764. }
  1765. if (FLAGSET(ACommand.Params, ccShowResults))
  1766. {
  1767. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1768. }
  1769. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1770. {
  1771. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1772. }
  1773. }
  1774. }
  1775. __finally
  1776. {
  1777. SAFE_DESTROY(FCapturedLog);
  1778. }
  1779. }
  1780. }
  1781. //---------------------------------------------------------------------------
  1782. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1783. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1784. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1785. {
  1786. DebugAssert(!IsLocalBrowserMode());
  1787. TStrings * LocalFileList = NULL;
  1788. TStrings * RemoteFileList = NULL;
  1789. TStrings * RemoteFileListFull = NULL;
  1790. try
  1791. {
  1792. if (LocalFileCommand)
  1793. {
  1794. if (ALocalFileList == NULL)
  1795. {
  1796. DebugAssert(HasDirView[osLocal]);
  1797. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1798. // directly (instead of EnableSelectedOperation) to pass
  1799. // false to FocusedFileOnlyWhenFocused
  1800. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1801. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1802. }
  1803. else
  1804. {
  1805. LocalFileList = ALocalFileList;
  1806. }
  1807. if (FileListCommand)
  1808. {
  1809. if (LocalFileList->Count != 1)
  1810. {
  1811. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1812. }
  1813. }
  1814. else
  1815. {
  1816. if ((LocalFileList->Count != 1) &&
  1817. (FileList->Count != 1) &&
  1818. (LocalFileList->Count != FileList->Count))
  1819. {
  1820. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1821. }
  1822. }
  1823. }
  1824. UnicodeString RootTempDir;
  1825. UnicodeString TempDir;
  1826. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1827. if (!RemoteFiles)
  1828. {
  1829. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1830. }
  1831. try
  1832. {
  1833. TDateTimes RemoteFileTimes;
  1834. if (RemoteFiles)
  1835. {
  1836. RemoteFileList = FileList;
  1837. }
  1838. else
  1839. {
  1840. RemoteFileList = new TStringList();
  1841. TMakeLocalFileListParams MakeFileListParam;
  1842. MakeFileListParam.FileList = RemoteFileList;
  1843. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1844. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1845. MakeFileListParam.Recursive =
  1846. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1847. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1848. if (!MakeFileListParam.Recursive)
  1849. {
  1850. RemoteFileTimes.clear();
  1851. RemoteFileListFull = new TStringList();
  1852. MakeFileListParam.FileList = RemoteFileListFull;
  1853. MakeFileListParam.Recursive = true;
  1854. MakeFileListParam.IncludeDirs = false;
  1855. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1856. }
  1857. else
  1858. {
  1859. RemoteFileListFull = RemoteFileList;
  1860. }
  1861. }
  1862. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1863. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1864. if (!NonBlocking)
  1865. {
  1866. int Count;
  1867. if (FileListCommand)
  1868. {
  1869. Count = 1;
  1870. }
  1871. else if (LocalFileCommand)
  1872. {
  1873. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1874. }
  1875. else
  1876. {
  1877. Count = RemoteFileList->Count;
  1878. }
  1879. Progress.Start(foCustomCommand, osRemote, Count);
  1880. DebugAssert(FProgressForm != NULL);
  1881. FProgressForm->ReadOnly = true;
  1882. }
  1883. try
  1884. {
  1885. if (FileListCommand)
  1886. {
  1887. UnicodeString LocalFile;
  1888. // MakeFileList does not delimit filenames
  1889. UnicodeString FileList = MakeFileList(RemoteFileList);
  1890. if (LocalFileCommand)
  1891. {
  1892. DebugAssert(LocalFileList->Count == 1);
  1893. LocalFile = LocalFileList->Strings[0];
  1894. }
  1895. TLocalCustomCommand CustomCommand(Data,
  1896. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1897. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1898. if (NonBlocking)
  1899. {
  1900. DebugAssert(!POutput);
  1901. ExecuteShellChecked(ShellCommand);
  1902. }
  1903. else
  1904. {
  1905. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1906. }
  1907. }
  1908. else if (LocalFileCommand)
  1909. {
  1910. if (LocalFileList->Count == 1)
  1911. {
  1912. UnicodeString LocalFile = LocalFileList->Strings[0];
  1913. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1914. {
  1915. UnicodeString FileName = RemoteFileList->Strings[Index];
  1916. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1917. Progress.SetFile(FileNameForProgress);
  1918. TLocalCustomCommand CustomCommand(Data,
  1919. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1920. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1921. TOnceDoneOperation OnceDoneOperation;
  1922. // Do not unselect anything, as it won't work with two panels anyway
  1923. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1924. }
  1925. }
  1926. else if (RemoteFileList->Count == 1)
  1927. {
  1928. UnicodeString FileName = RemoteFileList->Strings[0];
  1929. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1930. {
  1931. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1932. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1933. Progress.SetFile(LocalFileNameOnly);
  1934. TLocalCustomCommand CustomCommand(
  1935. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1936. FileName, LocalFileName, L"");
  1937. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1938. TOnceDoneOperation OnceDoneOperation;
  1939. // Do not unselect anything, as it won't work with two panels anyway
  1940. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1941. }
  1942. }
  1943. else
  1944. {
  1945. if (LocalFileList->Count != RemoteFileList->Count)
  1946. {
  1947. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1948. }
  1949. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1950. {
  1951. UnicodeString FileName = RemoteFileList->Strings[Index];
  1952. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1953. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1954. Progress.SetFile(LocalFileNameOnly); // sic
  1955. TLocalCustomCommand CustomCommand(
  1956. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1957. FileName, LocalFileName, L"");
  1958. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1959. TOnceDoneOperation OnceDoneOperation;
  1960. // Do not unselect anything, as it won't work with two panels anyway
  1961. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1962. }
  1963. }
  1964. }
  1965. else
  1966. {
  1967. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1968. {
  1969. UnicodeString FileName = RemoteFileList->Strings[Index];
  1970. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1971. Progress.SetFile(FileNameForProgress);
  1972. TLocalCustomCommand CustomCommand(Data,
  1973. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1974. FileName, L"", L"");
  1975. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1976. TOnceDoneOperation OnceDoneOperation;
  1977. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1978. }
  1979. }
  1980. }
  1981. __finally
  1982. {
  1983. if (!NonBlocking)
  1984. {
  1985. Progress.Stop();
  1986. }
  1987. }
  1988. DebugAssert(!FAutoOperation);
  1989. if (!RemoteFiles)
  1990. {
  1991. TempDir = IncludeTrailingBackslash(TempDir);
  1992. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1993. {
  1994. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1995. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1996. // Skip directories, process only nested files.
  1997. // The check is redundant as FileAge fails for directories anyway.
  1998. !DirectoryExists(FileName))
  1999. {
  2000. UnicodeString RemoteDir =
  2001. UnixExtractFileDir(
  2002. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  2003. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2004. TDateTime NewTime;
  2005. if (FileAge(FileName, NewTime) &&
  2006. (NewTime != RemoteFileTimes[Index]))
  2007. {
  2008. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2009. TemporaryFileCopyParam(CopyParam);
  2010. CopyParam.FileMask = L"";
  2011. FAutoOperation = true;
  2012. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2013. TemporaryFilesList->Add(FileName);
  2014. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2015. }
  2016. }
  2017. }
  2018. }
  2019. }
  2020. __finally
  2021. {
  2022. FAutoOperation = false;
  2023. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2024. {
  2025. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2026. }
  2027. }
  2028. }
  2029. __finally
  2030. {
  2031. if (RemoteFileList != FileList)
  2032. {
  2033. delete RemoteFileList;
  2034. }
  2035. if (LocalFileList != ALocalFileList)
  2036. {
  2037. delete LocalFileList;
  2038. }
  2039. if (RemoteFileListFull != RemoteFileList)
  2040. {
  2041. delete RemoteFileListFull;
  2042. }
  2043. }
  2044. }
  2045. //---------------------------------------------------------------------------
  2046. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2047. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2048. bool FileListCommand, UnicodeString * POutput)
  2049. {
  2050. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2051. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2052. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2053. {
  2054. UnicodeString FileName = SelectedFileList->Strings[Index];
  2055. if (DirectoryExists(FileName))
  2056. {
  2057. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2058. {
  2059. LocalFileList->Add(FileName);
  2060. }
  2061. if (FLAGSET(ACommand.Params, ccRecursive))
  2062. {
  2063. TMakeLocalFileListParams MakeFileListParam;
  2064. MakeFileListParam.FileList = LocalFileList.get();
  2065. MakeFileListParam.FileTimes = NULL;
  2066. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2067. MakeFileListParam.Recursive = true;
  2068. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2069. }
  2070. }
  2071. else
  2072. {
  2073. LocalFileList->Add(FileName);
  2074. }
  2075. }
  2076. UnicodeString RemotePath;
  2077. if (Terminal != NULL)
  2078. {
  2079. RemotePath = Terminal->CurrentDirectory;
  2080. }
  2081. if (FileListCommand)
  2082. {
  2083. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2084. TLocalCustomCommand CustomCommand(
  2085. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2086. L"", L"", FileList);
  2087. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2088. }
  2089. else
  2090. {
  2091. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2092. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2093. DebugAssert(FProgressForm != NULL);
  2094. FProgressForm->ReadOnly = true;
  2095. try
  2096. {
  2097. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2098. {
  2099. UnicodeString FileName = LocalFileList->Strings[Index];
  2100. Progress.SetFile(FileName);
  2101. TLocalCustomCommand CustomCommand(
  2102. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2103. FileName, L"", L"");
  2104. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2105. TOnceDoneOperation OnceDoneOperation;
  2106. Progress.Finish(FileName, true, OnceDoneOperation);
  2107. }
  2108. }
  2109. __finally
  2110. {
  2111. Progress.Stop();
  2112. }
  2113. }
  2114. }
  2115. //---------------------------------------------------------------------------
  2116. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2117. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2118. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2119. {
  2120. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2121. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2122. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2123. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2124. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2125. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2126. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2127. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2128. std::unique_ptr<UnicodeString> POutput;
  2129. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2130. FLAGSET(ACommand.Params, ccCopyResults))
  2131. {
  2132. POutput.reset(new UnicodeString());
  2133. }
  2134. if (!LocalCustomCommand.IsFileCommand(Command))
  2135. {
  2136. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2137. }
  2138. // remote files?
  2139. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2140. {
  2141. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2142. }
  2143. // local files
  2144. else
  2145. {
  2146. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2147. }
  2148. if (POutput.get() != NULL)
  2149. {
  2150. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2151. int P = POutput->Pos(sLineBreak);
  2152. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2153. {
  2154. POutput->SetLength(P - 1);
  2155. }
  2156. // Copy even empty output to clipboard
  2157. if (FLAGSET(ACommand.Params, ccCopyResults))
  2158. {
  2159. CopyToClipboard(*POutput);
  2160. }
  2161. // But do not show an empty message box.
  2162. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2163. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2164. !POutput->IsEmpty())
  2165. {
  2166. TClipboardHandler ClipboardHandler;
  2167. ClipboardHandler.Text = *POutput;
  2168. TMessageParams Params;
  2169. TQueryButtonAlias Aliases[1];
  2170. Aliases[0].Button = qaRetry;
  2171. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2172. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2173. Params.Aliases = Aliases;
  2174. Params.AliasesCount = LENOF(Aliases);
  2175. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2176. }
  2177. }
  2178. }
  2179. //---------------------------------------------------------------------------
  2180. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2181. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2182. {
  2183. TCustomCommandData Data;
  2184. UnicodeString Site;
  2185. UnicodeString RemotePath;
  2186. if (Terminal != NULL)
  2187. {
  2188. Data = TCustomCommandData(Terminal);
  2189. Site = Terminal->SessionData->SessionKey;
  2190. RemotePath = Terminal->CurrentDirectory;
  2191. }
  2192. UnicodeString HelpKeyword = ACommand.HomePage;
  2193. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2194. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2195. {
  2196. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2197. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2198. {
  2199. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2200. }
  2201. else
  2202. {
  2203. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2204. }
  2205. if (!DoCustomCommandOptionsDialog(
  2206. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2207. {
  2208. Abort();
  2209. }
  2210. }
  2211. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2212. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2213. {
  2214. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2215. }
  2216. else
  2217. {
  2218. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2219. }
  2220. }
  2221. //---------------------------------------------------------------------------
  2222. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2223. const TCustomCommandType & Command)
  2224. {
  2225. DebugAssert(FCustomCommandLocalFileList != NULL);
  2226. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2227. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2228. TStrings * LocalFileList = new TStringList();
  2229. TStrings * RemoteFileList = new TStringList();
  2230. try
  2231. {
  2232. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2233. {
  2234. LocalFileList->Clear();
  2235. LocalFileList->AddObject(
  2236. FCustomCommandLocalFileList->Strings[Index],
  2237. FCustomCommandLocalFileList->Objects[Index]);
  2238. RemoteFileList->Clear();
  2239. RemoteFileList->AddObject(
  2240. FCustomCommandRemoteFileList->Strings[Index],
  2241. FCustomCommandRemoteFileList->Objects[Index]);
  2242. CustomCommand(RemoteFileList, Command, LocalFileList);
  2243. }
  2244. }
  2245. __finally
  2246. {
  2247. delete LocalFileList;
  2248. delete RemoteFileList;
  2249. }
  2250. }
  2251. //---------------------------------------------------------------------------
  2252. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2253. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2254. {
  2255. delete FCustomCommandLocalFileList;
  2256. delete FCustomCommandRemoteFileList;
  2257. // takeover ownership,
  2258. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2259. FCustomCommandLocalFileList = LocalFileList;
  2260. FCustomCommandRemoteFileList = RemoteFileList;
  2261. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2262. if (Button != NULL)
  2263. {
  2264. FCustomCommandMenu->Items->Clear();
  2265. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2266. MenuPopup(FCustomCommandMenu, Button);
  2267. }
  2268. else
  2269. {
  2270. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2271. }
  2272. }
  2273. //---------------------------------------------------------------------------
  2274. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2275. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2276. {
  2277. DebugAssert(FCapturedLog != NULL);
  2278. if ((OutputType == cotOutput) || (OutputType == cotError))
  2279. {
  2280. FCapturedLog->Add(AddedLine);
  2281. }
  2282. }
  2283. //---------------------------------------------------------------------------
  2284. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2285. TOperationSide Side)
  2286. {
  2287. return
  2288. (Side == osOther) &&
  2289. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2290. }
  2291. //---------------------------------------------------------------------------
  2292. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2293. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2294. {
  2295. TTBItemOptions O;
  2296. O = Item->Options;
  2297. TCustomDirView * DView = DirView(Side);
  2298. if ((DView->ItemFocused != NULL) &&
  2299. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2300. // when resolving links is disabled, default action is to enter the directory,
  2301. // no matter what DoubleClickAction is configured to
  2302. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2303. // Can only Edit files, but can Open/Copy even directories
  2304. ((DoubleClickAction != dcaEdit) ||
  2305. !DView->ItemIsDirectory(DView->ItemFocused)))
  2306. {
  2307. Item->Options = O << tboDefault;
  2308. }
  2309. else
  2310. {
  2311. Item->Options = O >> tboDefault;
  2312. }
  2313. }
  2314. //---------------------------------------------------------------------------
  2315. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2316. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2317. {
  2318. TCustomDirView * DView = DirView(Side);
  2319. TListItem * Item = DView->ItemFocused;
  2320. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2321. EnableFocusedOperation[Side])
  2322. {
  2323. TPoint ClientPoint;
  2324. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2325. TPoint(0, 0) : MousePos;
  2326. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2327. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2328. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2329. }
  2330. }
  2331. //---------------------------------------------------------------------------
  2332. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2333. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2334. {
  2335. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2336. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2337. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2338. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2339. Handled = true;
  2340. }
  2341. //---------------------------------------------------------------------------
  2342. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2343. {
  2344. }
  2345. //---------------------------------------------------------------------------
  2346. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2347. {
  2348. DebugAssert(FErrorList == NULL);
  2349. if (WinConfiguration->ContinueOnError)
  2350. {
  2351. FErrorList = new TStringList();
  2352. Configuration->Usage->Inc(L"ContinuationsOnError");
  2353. }
  2354. NonVisualDataModule->StartBusy();
  2355. }
  2356. //---------------------------------------------------------------------------
  2357. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2358. {
  2359. NonVisualDataModule->EndBusy();
  2360. if (FErrorList)
  2361. {
  2362. HandleErrorList(FErrorList);
  2363. }
  2364. }
  2365. //---------------------------------------------------------------------------
  2366. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2367. const TCopyParamType & CopyParam)
  2368. {
  2369. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2370. {
  2371. Configuration->Usage->Inc(L"FileMaskUses");
  2372. }
  2373. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2374. {
  2375. Configuration->Usage->Inc(L"OperationMaskUses");
  2376. }
  2377. }
  2378. //---------------------------------------------------------------------------
  2379. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2380. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2381. {
  2382. DebugAssert(!IsLocalBrowserMode());
  2383. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2384. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2385. TTransferOperationParam DefaultParam;
  2386. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2387. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2388. switch (Param.Queue)
  2389. {
  2390. case asOn:
  2391. CopyParam.Queue = true;
  2392. break;
  2393. case asOff:
  2394. CopyParam.Queue = false;
  2395. break;
  2396. case asAuto:
  2397. default:
  2398. // keep default
  2399. break;
  2400. }
  2401. bool Result =
  2402. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2403. if (Result)
  2404. {
  2405. DebugAssert(Terminal);
  2406. bool SelectionRestored = false;
  2407. TCustomDirView * DView = NULL;
  2408. if (HasDirView[Side])
  2409. {
  2410. DView = DirView(Side);
  2411. DView->SaveSelection();
  2412. DView->SaveSelectedNames();
  2413. }
  2414. UpdateCopyParamCounters(CopyParam);
  2415. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2416. DebugAssert(FTransferResumeList == NULL);
  2417. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2418. FMoveToQueue = false;
  2419. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2420. try
  2421. {
  2422. TStrings * PermanentFileList;
  2423. std::unique_ptr<TStrings> PermanentFileListOwner;
  2424. try
  2425. {
  2426. if (Side == osLocal)
  2427. {
  2428. PermanentFileList = FileList;
  2429. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2430. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2431. if (Operation == foMove)
  2432. {
  2433. ReloadLocalDirectory();
  2434. if (DView != NULL)
  2435. {
  2436. DView->RestoreSelection();
  2437. }
  2438. SelectionRestored = true;
  2439. }
  2440. }
  2441. else
  2442. {
  2443. // Clone the file list as it may refer to current directory files,
  2444. // which get destroyed, when the source directory is reloaded after foMove operation.
  2445. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2446. // But for a hotfix, we are not going to do such a big change.
  2447. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2448. PermanentFileList = PermanentFileListOwner.get();
  2449. try
  2450. {
  2451. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2452. }
  2453. __finally
  2454. {
  2455. if (Operation == foMove)
  2456. {
  2457. if (DView != NULL)
  2458. {
  2459. DView->RestoreSelection();
  2460. }
  2461. SelectionRestored = true;
  2462. }
  2463. ReloadLocalDirectory(Param.TargetDirectory);
  2464. }
  2465. }
  2466. }
  2467. catch (EAbort &)
  2468. {
  2469. if (FMoveToQueue)
  2470. {
  2471. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2472. DebugAssert(CopyParam.TransferSkipList == NULL);
  2473. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2474. if (TransferResumeList->Count > 0)
  2475. {
  2476. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2477. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2478. }
  2479. CopyParam.TransferSkipList = TransferResumeList.release();
  2480. // not really needed, just to keep it consistent with TransferResumeList
  2481. FTransferResumeList = NULL;
  2482. FMoveToQueue = false;
  2483. Configuration->Usage->Inc("MovesToBackground");
  2484. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2485. ClearTransferSourceSelection(Direction);
  2486. }
  2487. throw;
  2488. }
  2489. }
  2490. __finally
  2491. {
  2492. if (!SelectionRestored && (DView != NULL))
  2493. {
  2494. DView->DiscardSavedSelection();
  2495. }
  2496. FTransferResumeList = NULL;
  2497. }
  2498. }
  2499. return Result;
  2500. }
  2501. //---------------------------------------------------------------------------
  2502. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2503. TOperationSide Side, TStrings * FileList, void * Param)
  2504. {
  2505. DebugAssert(FileList->Count);
  2506. // We deliberately do not toggle alternative flag (Param), but use OR,
  2507. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2508. // shortcut of CurrentDeleteAlternativeAction
  2509. bool Alternative =
  2510. bool(Param) || UseAlternativeFunction();
  2511. bool Recycle;
  2512. if (IsSideLocalBrowser(Side))
  2513. {
  2514. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2515. }
  2516. else
  2517. {
  2518. Recycle =
  2519. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2520. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2521. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2522. }
  2523. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2524. if (!Result)
  2525. {
  2526. UnicodeString Query;
  2527. if (FileList->Count == 1)
  2528. {
  2529. if (IsSideLocalBrowser(Side))
  2530. {
  2531. Query = ExtractFileName(FileList->Strings[0]);
  2532. }
  2533. else
  2534. {
  2535. Query = UnixExtractFileName(FileList->Strings[0]);
  2536. }
  2537. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2538. }
  2539. else
  2540. {
  2541. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2542. }
  2543. TMessageParams Params(mpNeverAskAgainCheck);
  2544. Params.ImageName = L"Delete file";
  2545. unsigned int Answer =
  2546. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2547. if (Answer == qaNeverAskAgain)
  2548. {
  2549. Result = true;
  2550. if (Recycle)
  2551. {
  2552. WinConfiguration->ConfirmRecycling = false;
  2553. }
  2554. else
  2555. {
  2556. WinConfiguration->ConfirmDeleting = false;
  2557. }
  2558. }
  2559. else
  2560. {
  2561. Result = (Answer == qaOK);
  2562. }
  2563. }
  2564. if (Result)
  2565. {
  2566. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2567. }
  2568. return Result;
  2569. }
  2570. //---------------------------------------------------------------------------
  2571. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2572. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2573. {
  2574. TAutoBatch AutoBatch(this);
  2575. bool Result;
  2576. if ((Operation == foCopy) || (Operation == foMove))
  2577. {
  2578. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2579. }
  2580. else if (Operation == foRename)
  2581. {
  2582. DebugAssert(DirView(Side)->ItemFocused);
  2583. DirView(Side)->ItemFocused->EditCaption();
  2584. Result = true;
  2585. }
  2586. else if (Operation == foDelete)
  2587. {
  2588. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2589. }
  2590. else if (Operation == foSetProperties)
  2591. {
  2592. DirView(osRemote)->SaveSelectedNames();
  2593. Result = SetProperties(Side, FileList);
  2594. }
  2595. else if (Operation == foCustomCommand)
  2596. {
  2597. DebugAssert(Param);
  2598. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2599. DirView(osRemote)->SaveSelectedNames();
  2600. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2601. CustomCommand(FileList, *Command, NULL);
  2602. Result = true;
  2603. }
  2604. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2605. {
  2606. DebugAssert(!IsSideLocalBrowser(Side));
  2607. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2608. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2609. }
  2610. else if (Operation == foLock)
  2611. {
  2612. DebugAssert(!IsSideLocalBrowser(Side));
  2613. LockFiles(FileList, true);
  2614. Result = true;
  2615. }
  2616. else if (Operation == foUnlock)
  2617. {
  2618. DebugAssert(!IsSideLocalBrowser(Side));
  2619. LockFiles(FileList, false);
  2620. Result = true;
  2621. }
  2622. else
  2623. {
  2624. DebugFail();
  2625. }
  2626. return Result;
  2627. }
  2628. //---------------------------------------------------------------------------
  2629. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2630. {
  2631. if (Side == osCurrent)
  2632. {
  2633. Side = FCurrentSide;
  2634. }
  2635. return Side;
  2636. }
  2637. //---------------------------------------------------------------------------
  2638. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2639. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2640. {
  2641. Side = GetSide(Side);
  2642. bool Result;
  2643. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2644. try
  2645. {
  2646. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2647. }
  2648. __finally
  2649. {
  2650. delete FileList;
  2651. }
  2652. return Result;
  2653. }
  2654. //---------------------------------------------------------------------------
  2655. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2656. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2657. bool NoConfirmation, void * Param)
  2658. {
  2659. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2660. {
  2661. if ((Operation == foCopy) || (Operation == foMove))
  2662. {
  2663. DebugAssert(!IsLocalBrowserMode());
  2664. if (GetSide(Side) == osLocal)
  2665. {
  2666. Configuration->Usage->Inc(L"UploadsCommand");
  2667. }
  2668. else
  2669. {
  2670. Configuration->Usage->Inc(L"DownloadsCommand");
  2671. }
  2672. }
  2673. }
  2674. }
  2675. //---------------------------------------------------------------------------
  2676. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2677. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2678. {
  2679. TTransferOperationParam Param;
  2680. if ((WinConfiguration->Interface != ifCommander) ||
  2681. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2682. {
  2683. Flags &= ~cocShortCutHint;
  2684. }
  2685. TCustomDirView * DView = DirView(Side);
  2686. Param.Options =
  2687. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2688. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2689. if (FLAGSET(Flags, cocQueue))
  2690. {
  2691. Param.Queue = asOn;
  2692. }
  2693. else if (FLAGSET(Flags, cocNonQueue))
  2694. {
  2695. Param.Queue = asOff;
  2696. }
  2697. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2698. }
  2699. //---------------------------------------------------------------------------
  2700. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2701. {
  2702. try
  2703. {
  2704. if (ErrorList->Count)
  2705. {
  2706. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2707. if (MessageDialog(Message, qtError,
  2708. qaOK | qaCancel, HELP_NONE) == qaOK)
  2709. {
  2710. unsigned int Answer;
  2711. int Index = 0;
  2712. do
  2713. {
  2714. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2715. TQueryButtonAlias Aliases[2];
  2716. Aliases[0].Button = qaYes;
  2717. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2718. Aliases[1].Button = qaNo;
  2719. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2720. TMessageParams Params;
  2721. Params.Aliases = Aliases;
  2722. Params.AliasesCount = LENOF(Aliases);
  2723. Answer = MoreMessageDialog(
  2724. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2725. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2726. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2727. qaOK, HELP_NONE, &Params);
  2728. if (Answer == qaNo)
  2729. {
  2730. Index++;
  2731. }
  2732. if (Answer == qaYes)
  2733. {
  2734. Index--;
  2735. }
  2736. }
  2737. while (Answer != qaOK);
  2738. }
  2739. }
  2740. }
  2741. __finally
  2742. {
  2743. TStrings * List = ErrorList;
  2744. ErrorList = NULL;
  2745. for (int i = 0; i < List->Count; i++)
  2746. {
  2747. delete List->Objects[i];
  2748. }
  2749. delete List;
  2750. }
  2751. }
  2752. //---------------------------------------------------------------------------
  2753. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2754. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2755. {
  2756. // needed for checking filemasks, as there's no directory object
  2757. // associated with the file object
  2758. File->FullFileName = FullFileName;
  2759. TFileMasks::TParams MaskParams;
  2760. MaskParams.Size = File->Size;
  2761. MaskParams.Modification = File->Modification;
  2762. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2763. }
  2764. //---------------------------------------------------------------------------
  2765. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2766. {
  2767. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2768. Side = GetSide(Side);
  2769. TCustomDirView * DView = DirView(Side);
  2770. TListItem * FocusedItem = DView->ItemFocused;
  2771. UnicodeString Name;
  2772. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2773. {
  2774. Name = DView->ItemFileName(FocusedItem);
  2775. }
  2776. else
  2777. {
  2778. Name = LoadStr(NEW_FILE);
  2779. }
  2780. UnicodeString Names = Name;
  2781. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2782. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2783. HELP_EDIT_NEW, History.get(), true))
  2784. {
  2785. while (!Names.IsEmpty())
  2786. {
  2787. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2788. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2789. UnicodeString TargetFileName;
  2790. UnicodeString LocalFileName;
  2791. UnicodeString RootTempDir;
  2792. UnicodeString TempDir;
  2793. UnicodeString RemoteDirectory;
  2794. bool ExistingFile = false;
  2795. if (!IsSideLocalBrowser(Side))
  2796. {
  2797. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2798. TRemoteFile * File = NULL;
  2799. if (FTerminal->FileExists(Name, &File) &&
  2800. !File->IsDirectory)
  2801. {
  2802. try
  2803. {
  2804. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2805. ExistingFile = true;
  2806. }
  2807. __finally
  2808. {
  2809. delete File;
  2810. }
  2811. }
  2812. if (!ExistingFile)
  2813. {
  2814. RemoteDirectory = UnixExtractFilePath(Name);
  2815. TemporaryDirectoryForRemoteFiles(
  2816. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2817. TargetFileName = UnixExtractFileName(Name);
  2818. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2819. LocalFileName = TempDir +
  2820. // We probably do not want to trim the VMS version here
  2821. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2822. }
  2823. }
  2824. else
  2825. {
  2826. if (ExtractFilePath(Name).IsEmpty())
  2827. {
  2828. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2829. }
  2830. else
  2831. {
  2832. LocalFileName = ExpandFileName(Name);
  2833. }
  2834. TargetFileName = ExtractFileName(Name);
  2835. }
  2836. if (!ExistingFile)
  2837. {
  2838. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2839. if (NewFile)
  2840. {
  2841. int File = FileCreate(ApiPath(LocalFileName));
  2842. if (File < 0)
  2843. {
  2844. if (!RootTempDir.IsEmpty())
  2845. {
  2846. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2847. }
  2848. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2849. }
  2850. else
  2851. {
  2852. FileClose(File);
  2853. }
  2854. }
  2855. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2856. const TEditorData * ExternalEditor = NULL;
  2857. TFileMasks::TParams MaskParams; // size not known
  2858. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2859. false, MaskParams);
  2860. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2861. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2862. }
  2863. }
  2864. }
  2865. }
  2866. //---------------------------------------------------------------------------
  2867. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2868. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2869. {
  2870. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2871. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2872. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2873. return
  2874. ((ExecuteFileBy == efInternalEditor)) ||
  2875. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2876. }
  2877. //---------------------------------------------------------------------------
  2878. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2879. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2880. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2881. UnicodeString RemoteDirectory, bool NewFile)
  2882. {
  2883. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2884. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2885. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2886. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2887. Side = GetSide(Side);
  2888. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2889. if (!IsSideLocalBrowser(Side))
  2890. {
  2891. Data->Terminal = Terminal;
  2892. Data->Queue = Queue;
  2893. Data->SessionData = CloneCurrentSessionData();
  2894. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2895. Data->RemoteDirectory = RemoteDirectory;
  2896. Data->SessionName = Terminal->SessionData->SessionName;
  2897. Data->LocalRootDirectory = LocalRootDirectory;
  2898. Data->OriginalFileName = OriginalFileName;
  2899. Data->Command = L""; // will be changed later for external editor
  2900. }
  2901. if (ExecuteFileBy == efInternalEditor)
  2902. {
  2903. if (!IsSideLocalBrowser(Side))
  2904. {
  2905. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2906. L" - " + Terminal->SessionData->SessionName;
  2907. TForm * Editor;
  2908. try
  2909. {
  2910. TNotifyEvent OnFileChanged = NULL;
  2911. TNotifyEvent OnSaveAll = NULL;
  2912. TAnyModifiedEvent OnAnyModified = NULL;
  2913. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2914. if (Terminal->IsCapable[fsBackgroundTransfers])
  2915. {
  2916. OnFileChanged = FEditorManager->FileChanged;
  2917. OnSaveAll = SaveAllInternalEditors;
  2918. OnAnyModified = AnyInternalEditorModified;
  2919. }
  2920. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2921. FEditorManager->FileReload, FEditorManager->FileClosed,
  2922. OnSaveAll, OnAnyModified,
  2923. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2924. }
  2925. catch(...)
  2926. {
  2927. if (!LocalRootDirectory.IsEmpty())
  2928. {
  2929. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2930. }
  2931. throw;
  2932. }
  2933. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2934. }
  2935. else
  2936. {
  2937. DebugAssert(!FStandaloneEditing);
  2938. TForm * Editor =
  2939. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2940. SaveAllInternalEditors, AnyInternalEditorModified,
  2941. L"", false, SessionColor, -1, NewFile);
  2942. FLocalEditors->Add(Editor);
  2943. }
  2944. }
  2945. else
  2946. {
  2947. HANDLE Process;
  2948. if (ExecuteFileBy == efExternalEditor)
  2949. {
  2950. UnicodeString Program, Params, Dir;
  2951. Data->Command = ExternalEditor->ExternalEditor;
  2952. ReformatFileNameCommand(Data->Command);
  2953. SplitCommand(Data->Command, Program, Params, Dir);
  2954. Params = ExpandFileNameCommand(Params, FileName);
  2955. Program = ExpandEnvironmentVariables(Program);
  2956. if (!ExecuteShell(Program, Params, Process))
  2957. {
  2958. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2959. }
  2960. }
  2961. else
  2962. {
  2963. DebugAssert(!IsSideLocalBrowser(Side));
  2964. if (!ExecuteShell(FileName, L"", Process))
  2965. {
  2966. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2967. }
  2968. }
  2969. if (IsSideLocalBrowser(Side) ||
  2970. ((ExecuteFileBy == efShell) &&
  2971. !WinConfiguration->Editor.SDIShellEditor) ||
  2972. ((ExecuteFileBy == efExternalEditor) &&
  2973. !ExternalEditor->SDIExternalEditor))
  2974. {
  2975. // no need for handle
  2976. if (Process != NULL)
  2977. {
  2978. DebugCheck(CloseHandle(Process));
  2979. }
  2980. Process = INVALID_HANDLE_VALUE;
  2981. }
  2982. else
  2983. {
  2984. if (Process == NULL)
  2985. {
  2986. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2987. }
  2988. }
  2989. if (!IsSideLocalBrowser(Side))
  2990. {
  2991. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2992. }
  2993. }
  2994. }
  2995. //---------------------------------------------------------------------------
  2996. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2997. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2998. void * /*Arg*/)
  2999. {
  3000. if (Token != NULL)
  3001. {
  3002. EditorFormFileSave(static_cast<TForm *>(Token));
  3003. }
  3004. }
  3005. //---------------------------------------------------------------------------
  3006. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3007. {
  3008. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3009. {
  3010. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3011. }
  3012. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3013. }
  3014. //---------------------------------------------------------------------------
  3015. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3016. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3017. void * Arg)
  3018. {
  3019. if ((Token != NULL) &&
  3020. IsEditorFormModified(static_cast<TForm *>(Token)))
  3021. {
  3022. *static_cast<bool *>(Arg) = true;
  3023. }
  3024. }
  3025. //---------------------------------------------------------------------------
  3026. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3027. TObject * /*Sender*/, bool & Modified)
  3028. {
  3029. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3030. {
  3031. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3032. {
  3033. Modified = true;
  3034. }
  3035. }
  3036. if (!Modified)
  3037. {
  3038. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3039. }
  3040. }
  3041. //---------------------------------------------------------------------------
  3042. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3043. {
  3044. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3045. }
  3046. //---------------------------------------------------------------------------
  3047. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3048. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3049. UnicodeString & Result, UnicodeString & RootDirectory)
  3050. {
  3051. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3052. {
  3053. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3054. Result = RootDirectory;
  3055. }
  3056. else
  3057. {
  3058. RootDirectory = L"";
  3059. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3060. Result = IncludeTrailingBackslash(Result);
  3061. }
  3062. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3063. {
  3064. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3065. }
  3066. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3067. {
  3068. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3069. {
  3070. RemoteDirectory.Delete(1, 1);
  3071. }
  3072. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3073. }
  3074. if (!ForceDirectories(ApiPath(Result)))
  3075. {
  3076. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3077. }
  3078. }
  3079. //---------------------------------------------------------------------------
  3080. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3081. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3082. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3083. {
  3084. DebugAssert(!IsLocalBrowserMode());
  3085. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3086. if (ForceText)
  3087. {
  3088. CopyParam.TransferMode = tmAscii;
  3089. }
  3090. TemporaryFileCopyParam(CopyParam);
  3091. if (AllFiles)
  3092. {
  3093. CopyParam.IncludeFileMask = TFileMasks();
  3094. }
  3095. if (TempDir.IsEmpty())
  3096. {
  3097. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3098. }
  3099. DebugAssert(!FAutoOperation);
  3100. FAutoOperation = AutoOperation;
  3101. Terminal->ExceptionOnFail = true;
  3102. try
  3103. {
  3104. try
  3105. {
  3106. // turn off confirmations, as for MDI editors we may possibly download
  3107. // the same file over
  3108. Terminal->CopyToLocal(
  3109. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3110. if (GetTargetNames)
  3111. {
  3112. for (int i = 0; i < FileList->Count; i++)
  3113. {
  3114. FileList->Strings[i] =
  3115. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3116. }
  3117. }
  3118. }
  3119. catch(...)
  3120. {
  3121. if (!RootTempDir.IsEmpty())
  3122. {
  3123. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3124. }
  3125. throw;
  3126. }
  3127. }
  3128. __finally
  3129. {
  3130. FAutoOperation = false;
  3131. Terminal->ExceptionOnFail = false;
  3132. }
  3133. }
  3134. //---------------------------------------------------------------------------
  3135. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3136. {
  3137. // Do not waste time checking for default editor list the next time,
  3138. // or testing if default editor is not notepad.
  3139. bool TryNextTime = false;
  3140. UnicodeString UsageState;
  3141. if (!WinConfiguration->EditorList->IsDefaultList())
  3142. {
  3143. UsageState = "H";
  3144. }
  3145. else
  3146. {
  3147. UnicodeString Executable;
  3148. UnicodeString ExecutableDescription;
  3149. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3150. {
  3151. UnicodeString Message =
  3152. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3153. unsigned int Answer =
  3154. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3155. if (Answer != qaOK)
  3156. {
  3157. UsageState = "R";
  3158. }
  3159. else
  3160. {
  3161. UsageState = "A";
  3162. TEditorData EditorData;
  3163. EditorData.Editor = edExternal;
  3164. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3165. EditorData.DecideExternalEditorText();
  3166. TEditorList EditorList;
  3167. EditorList = *WinConfiguration->EditorList;
  3168. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3169. WinConfiguration->EditorList = &EditorList;
  3170. }
  3171. }
  3172. }
  3173. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3174. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3175. }
  3176. //---------------------------------------------------------------------------
  3177. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3178. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3179. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3180. {
  3181. if (ExecuteFileBy == efDefaultEditor)
  3182. {
  3183. if (!WinConfiguration->OfferedEditorAutoConfig)
  3184. {
  3185. EditorAutoConfig();
  3186. }
  3187. const TEditorPreferences * Editor =
  3188. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3189. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3190. {
  3191. ExecuteFileBy = efInternalEditor;
  3192. ExternalEditor = NULL;
  3193. }
  3194. else if (Editor->Data->Editor == edOpen)
  3195. {
  3196. ExecuteFileBy = efShell;
  3197. ExternalEditor = NULL;
  3198. }
  3199. else
  3200. {
  3201. ExecuteFileBy = efExternalEditor;
  3202. ExternalEditor = Editor->Data;
  3203. }
  3204. }
  3205. }
  3206. //---------------------------------------------------------------------------
  3207. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3208. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3209. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3210. {
  3211. UnicodeString OriginalFileName;
  3212. UnicodeString LocalRootDirectory;
  3213. UnicodeString RemoteDirectory;
  3214. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3215. IsSideLocalBrowser(Side), MaskParams);
  3216. UnicodeString Counter;
  3217. UnicodeString LocalFileName;
  3218. bool Handled = false;
  3219. if (!IsSideLocalBrowser(Side))
  3220. {
  3221. // We need to trim VMS version here, so that we use name without version
  3222. // when uploading back to create a new version of the file
  3223. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3224. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3225. TObject * Token = NULL;
  3226. UnicodeString LocalDirectory;
  3227. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3228. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3229. LocalDirectory))
  3230. {
  3231. if (Token != NULL)
  3232. {
  3233. TForm * Form = dynamic_cast<TForm *>(Token);
  3234. if (Form->WindowState == wsMinimized)
  3235. {
  3236. ShowWindow(Form->Handle, SW_RESTORE);
  3237. }
  3238. else
  3239. {
  3240. Form->SetFocus();
  3241. }
  3242. Handled = true;
  3243. }
  3244. else
  3245. {
  3246. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3247. }
  3248. }
  3249. if (!Handled)
  3250. {
  3251. TStringList * FileList1 = new TStringList();
  3252. try
  3253. {
  3254. FileList1->AddObject(FullFileName, Object);
  3255. TemporarilyDownloadFiles(FileList1,
  3256. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3257. LocalRootDirectory, LocalDirectory, true, true, true);
  3258. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3259. }
  3260. __finally
  3261. {
  3262. delete FileList1;
  3263. }
  3264. switch (ExecuteFileBy)
  3265. {
  3266. case efShell:
  3267. Counter = "RemoteFilesExecuted";
  3268. break;
  3269. case efInternalEditor:
  3270. Counter = "RemoteFilesOpenedInInternalEditor";
  3271. break;
  3272. case efExternalEditor:
  3273. Counter = "RemoteFilesOpenedInExternalEditor";
  3274. break;
  3275. default:
  3276. DebugFail();
  3277. }
  3278. }
  3279. }
  3280. else
  3281. {
  3282. LocalFileName = FullFileName;
  3283. OriginalFileName = ExtractFileName(FullFileName);
  3284. switch (ExecuteFileBy)
  3285. {
  3286. case efShell:
  3287. Counter = "LocalFilesExecuted";
  3288. break;
  3289. case efInternalEditor:
  3290. Counter = "LocalFilesOpenedInInternalEditor";
  3291. break;
  3292. case efExternalEditor:
  3293. Counter = "LocalFilesOpenedInExternalEditor";
  3294. break;
  3295. default:
  3296. DebugFail();
  3297. }
  3298. }
  3299. if (!Handled)
  3300. {
  3301. Configuration->Usage->Inc(Counter);
  3302. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3303. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3304. }
  3305. }
  3306. //---------------------------------------------------------------------------
  3307. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3308. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3309. bool AllSelected, bool OnFocused)
  3310. {
  3311. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3312. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3313. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3314. Side = GetSide(Side);
  3315. TCustomDirView * DView = DirView(Side);
  3316. TStrings * FileList =
  3317. AllSelected ?
  3318. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3319. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3320. try
  3321. {
  3322. DebugAssert(AllSelected || (FileList->Count == 1));
  3323. for (int i = 0; i < FileList->Count; i++)
  3324. {
  3325. UnicodeString ListFileName = FileList->Strings[i];
  3326. UnicodeString FileNameOnly =
  3327. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3328. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3329. if (!DView->ItemIsDirectory(Item))
  3330. {
  3331. UnicodeString FullFileName;
  3332. if (!IsSideLocalBrowser(Side))
  3333. {
  3334. FullFileName = DirView(Side)->Path + ListFileName;
  3335. }
  3336. else
  3337. {
  3338. FullFileName = ListFileName;
  3339. }
  3340. TFileMasks::TParams MaskParams;
  3341. MaskParams.Size = DView->ItemFileSize(Item);
  3342. TDateTimePrecision Precision;
  3343. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3344. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3345. FileList->Objects[i], MaskParams);
  3346. }
  3347. }
  3348. }
  3349. __finally
  3350. {
  3351. delete FileList;
  3352. }
  3353. }
  3354. //---------------------------------------------------------------------------
  3355. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3356. {
  3357. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3358. CopyParam.FileNameCase = ncNoChange;
  3359. CopyParam.PreserveRights = false;
  3360. CopyParam.PreserveReadOnly = false;
  3361. CopyParam.ReplaceInvalidChars = true;
  3362. CopyParam.IncludeFileMask = TFileMasks();
  3363. CopyParam.NewerOnly = false;
  3364. CopyParam.FileMask = L"";
  3365. }
  3366. //---------------------------------------------------------------------------
  3367. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3368. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3369. {
  3370. TTerminalManager * Manager = TTerminalManager::Instance();
  3371. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3372. {
  3373. if (!NonVisualDataModule->Busy)
  3374. {
  3375. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3376. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3377. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3378. if (Data->Terminal == NULL)
  3379. {
  3380. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3381. if (SameSiteTerminal != NULL)
  3382. {
  3383. UnicodeString Message =
  3384. FMTLOAD(EDIT_SESSION_REATTACH,
  3385. (FileNameOnly, Data->SessionName, FileNameOnly));
  3386. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3387. {
  3388. Data->Terminal = Terminal;
  3389. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3390. Data->SessionName = Terminal->SessionData->SessionName;
  3391. // We might also overwrite session data
  3392. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3393. }
  3394. else
  3395. {
  3396. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3397. Abort();
  3398. }
  3399. }
  3400. }
  3401. // foreground session should reconnect itself
  3402. else if (Terminal != Data->Terminal)
  3403. {
  3404. UnicodeString Message =
  3405. MainInstructions(
  3406. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3407. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3408. {
  3409. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3410. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3411. }
  3412. else
  3413. {
  3414. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3415. Abort();
  3416. }
  3417. }
  3418. }
  3419. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3420. {
  3421. Configuration->Usage->Inc(L"EditInactiveSession");
  3422. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3423. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3424. (ExtractFileName(FileName), Data->SessionName)));
  3425. }
  3426. }
  3427. TStrings * FileList = new TStringList();
  3428. try
  3429. {
  3430. FileList->Add(FileName);
  3431. // consider using the same settings (preset) as when the file was downloaded
  3432. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3433. TemporaryFileCopyParam(CopyParam);
  3434. if (Data->ForceText)
  3435. {
  3436. CopyParam.TransferMode = tmAscii;
  3437. }
  3438. // so i do not need to worry if masking algorithm works in all cases
  3439. // ("" means "copy file name", no masking is actually done)
  3440. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3441. {
  3442. CopyParam.FileMask = L"";
  3443. }
  3444. else
  3445. {
  3446. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3447. }
  3448. DebugAssert(Data->Queue != NULL);
  3449. int Params = cpNoConfirmation | cpTemporary;
  3450. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3451. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3452. QueueItem->CompleteEvent = UploadCompleteEvent;
  3453. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3454. }
  3455. __finally
  3456. {
  3457. delete FileList;
  3458. }
  3459. }
  3460. //---------------------------------------------------------------------------
  3461. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3462. const UnicodeString FileName, const TEditedFileData * Data)
  3463. {
  3464. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3465. DebugAssert(!NonVisualDataModule->Busy);
  3466. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3467. {
  3468. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3469. (ExtractFileName(FileName), Data->SessionName)));
  3470. }
  3471. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3472. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3473. NonVisualDataModule->StartBusy();
  3474. try
  3475. {
  3476. std::unique_ptr<TRemoteFile> File;
  3477. UnicodeString RemoteFileName =
  3478. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3479. FTerminal->ExceptionOnFail = true;
  3480. try
  3481. {
  3482. TRemoteFile * AFile = NULL;
  3483. FTerminal->ReadFile(RemoteFileName, AFile);
  3484. File.reset(AFile);
  3485. if (!File->HaveFullFileName)
  3486. {
  3487. File->FullFileName = RemoteFileName;
  3488. }
  3489. }
  3490. __finally
  3491. {
  3492. FTerminal->ExceptionOnFail = false;
  3493. }
  3494. std::unique_ptr<TStrings> FileList(new TStringList());
  3495. FileList->AddObject(RemoteFileName, File.get());
  3496. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3497. UnicodeString TempDir = ExtractFilePath(FileName);
  3498. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3499. TempDir, true, true, true);
  3500. // sanity check, the target file name should be still the same
  3501. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3502. }
  3503. __finally
  3504. {
  3505. NonVisualDataModule->EndBusy();
  3506. // it actually may not exist anymore...
  3507. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3508. }
  3509. }
  3510. //---------------------------------------------------------------------------
  3511. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3512. const TEditedFileData * Data, bool & KeepOpen)
  3513. {
  3514. // Command is set for external editors only (not for "shell" open).
  3515. if (!Data->Command.IsEmpty())
  3516. {
  3517. bool AnyFound = false;
  3518. bool AnyMDI = false;
  3519. bool AnyNonMDI = false;
  3520. bool AnyDetect = false;
  3521. TEditorList * EditorList = new TEditorList();
  3522. try
  3523. {
  3524. *EditorList = *WinConfiguration->EditorList;
  3525. for (int i = 0; i < EditorList->Count; i++)
  3526. {
  3527. const TEditorPreferences * Editor = EditorList->Editors[i];
  3528. if ((Editor->Data->Editor == edExternal) &&
  3529. (Editor->Data->ExternalEditor == Data->Command))
  3530. {
  3531. AnyFound = true;
  3532. if (Editor->Data->SDIExternalEditor)
  3533. {
  3534. AnyNonMDI = true;
  3535. if (Editor->Data->DetectMDIExternalEditor)
  3536. {
  3537. AnyDetect = true;
  3538. }
  3539. }
  3540. else
  3541. {
  3542. AnyMDI = true;
  3543. }
  3544. }
  3545. }
  3546. bool EnableMDI = false;
  3547. bool DisableDetect = false;
  3548. if (AnyMDI)
  3549. {
  3550. KeepOpen = true;
  3551. if (AnyNonMDI)
  3552. {
  3553. // there is at least one instance of the editor with MDI support enabled,
  3554. // and one with disabled, enable it for all instances
  3555. EnableMDI = true;
  3556. }
  3557. }
  3558. else if (AnyFound && !AnyDetect)
  3559. {
  3560. // at least once instance found but all have MDI autodetection disabled
  3561. // => close the file (default action)
  3562. }
  3563. else
  3564. {
  3565. // no instance of the editor has MDI support enabled
  3566. TMessageParams Params;
  3567. if (AnyFound)
  3568. {
  3569. // there is at least one instance of the editor
  3570. Params.Params |= mpNeverAskAgainCheck;
  3571. }
  3572. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3573. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3574. switch (Answer)
  3575. {
  3576. case qaNeverAskAgain:
  3577. DisableDetect = true;
  3578. break;
  3579. case qaNo:
  3580. EnableMDI = true;
  3581. KeepOpen = true;
  3582. break;
  3583. }
  3584. }
  3585. if (AnyFound && (EnableMDI || DisableDetect))
  3586. {
  3587. bool Changed = false;
  3588. for (int i = 0; i < EditorList->Count; i++)
  3589. {
  3590. const TEditorPreferences * Editor = EditorList->Editors[i];
  3591. if ((Editor->Data->Editor == edExternal) &&
  3592. (Editor->Data->ExternalEditor == Data->Command) &&
  3593. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3594. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3595. {
  3596. Changed = true;
  3597. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3598. if (EnableMDI)
  3599. {
  3600. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3601. }
  3602. if (DisableDetect)
  3603. {
  3604. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3605. }
  3606. EditorList->Change(i, UpdatedEditor);
  3607. }
  3608. }
  3609. if (Changed)
  3610. {
  3611. WinConfiguration->EditorList = EditorList;
  3612. }
  3613. }
  3614. }
  3615. __finally
  3616. {
  3617. delete EditorList;
  3618. }
  3619. }
  3620. else
  3621. {
  3622. // "open" case
  3623. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3624. qaOK, HELP_APP_EARLY_CLOSED);
  3625. }
  3626. }
  3627. //---------------------------------------------------------------------------
  3628. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3629. {
  3630. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3631. }
  3632. //---------------------------------------------------------------------------
  3633. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3634. {
  3635. SideEnter(osRemote);
  3636. }
  3637. //---------------------------------------------------------------------------
  3638. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3639. {
  3640. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3641. SideEnter(osRemote);
  3642. }
  3643. //---------------------------------------------------------------------------
  3644. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3645. {
  3646. FCurrentSide = Side;
  3647. if (Visible)
  3648. {
  3649. UpdateControls();
  3650. }
  3651. }
  3652. //---------------------------------------------------------------------------
  3653. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3654. TStrings * FileList, bool Alternative)
  3655. {
  3656. TCustomDirView * DView = DirView(Side);
  3657. DView->SaveSelection();
  3658. DView->SaveSelectedNames();
  3659. DebugAssert(!FAlternativeDelete);
  3660. FAlternativeDelete = Alternative;
  3661. try
  3662. {
  3663. if (!IsSideLocalBrowser(Side))
  3664. {
  3665. DebugAssert(Terminal != NULL);
  3666. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3667. }
  3668. else
  3669. {
  3670. try
  3671. {
  3672. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3673. }
  3674. __finally
  3675. {
  3676. ReloadLocalDirectory();
  3677. }
  3678. }
  3679. FAlternativeDelete = false;
  3680. }
  3681. catch(...)
  3682. {
  3683. FAlternativeDelete = false;
  3684. DView->DiscardSavedSelection();
  3685. throw;
  3686. }
  3687. DView->RestoreSelection();
  3688. }
  3689. //---------------------------------------------------------------------------
  3690. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3691. {
  3692. DebugAssert(Terminal);
  3693. DebugAssert(!IsLocalBrowserMode());
  3694. RemoteDirView->SaveSelection();
  3695. RemoteDirView->SaveSelectedNames();
  3696. try
  3697. {
  3698. if (Lock)
  3699. {
  3700. Terminal->LockFiles(FileList);
  3701. }
  3702. else
  3703. {
  3704. Terminal->UnlockFiles(FileList);
  3705. }
  3706. }
  3707. catch(...)
  3708. {
  3709. RemoteDirView->DiscardSavedSelection();
  3710. throw;
  3711. }
  3712. RemoteDirView->RestoreSelection();
  3713. }
  3714. //---------------------------------------------------------------------------
  3715. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3716. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3717. bool NoConfirmation, bool Move)
  3718. {
  3719. DebugAssert(Terminal != NULL);
  3720. DebugAssert(!IsLocalBrowserMode());
  3721. // update Terminal->StateData->RemoteDirectory
  3722. UpdateTerminal(Terminal);
  3723. if (Session == NULL)
  3724. {
  3725. Session = TTerminalManager::Instance()->ActiveTerminal;
  3726. }
  3727. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3728. {
  3729. if (RemoteDriveView->DropTarget != NULL)
  3730. {
  3731. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3732. }
  3733. else if (RemoteDirView->DropTarget != NULL)
  3734. {
  3735. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3736. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3737. }
  3738. else
  3739. {
  3740. Target = RemoteDirView->Path;
  3741. }
  3742. }
  3743. else
  3744. {
  3745. Target = Session->StateData->RemoteDirectory;
  3746. }
  3747. Target = UnixIncludeTrailingBackslash(Target);
  3748. if (FileList->Count == 1)
  3749. {
  3750. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3751. }
  3752. else
  3753. {
  3754. FileMask = AnyMask;
  3755. }
  3756. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3757. bool Result = true;
  3758. if (!NoConfirmation)
  3759. {
  3760. bool Multi = (FileList->Count > 1);
  3761. if (Move)
  3762. {
  3763. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3764. }
  3765. else
  3766. {
  3767. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3768. TStrings * Directories = new TStringList;
  3769. try
  3770. {
  3771. for (int Index = 0; Index < Sessions->Count; Index++)
  3772. {
  3773. TManagedTerminal * Terminal =
  3774. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3775. Directories->Add(Terminal->StateData->RemoteDirectory);
  3776. }
  3777. TDirectRemoteCopy AllowDirectCopy;
  3778. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3779. {
  3780. DebugAssert(DirectCopy);
  3781. AllowDirectCopy = drcAllow;
  3782. }
  3783. else if (FTerminal->IsCapable[fcSecondaryShell])
  3784. {
  3785. DebugAssert(DirectCopy);
  3786. AllowDirectCopy = drcConfirmCommandSession;
  3787. }
  3788. else
  3789. {
  3790. DebugAssert(!DirectCopy);
  3791. AllowDirectCopy = drcDisallow;
  3792. }
  3793. void * ASession = Session;
  3794. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3795. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3796. Session = static_cast<TManagedTerminal *>(ASession);
  3797. }
  3798. __finally
  3799. {
  3800. delete Directories;
  3801. }
  3802. }
  3803. }
  3804. return Result;
  3805. }
  3806. //---------------------------------------------------------------------------
  3807. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3808. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3809. {
  3810. DebugAssert(!IsLocalBrowserMode());
  3811. bool DirectCopy;
  3812. UnicodeString Target, FileMask;
  3813. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3814. if (Result)
  3815. {
  3816. if (!Move && !DirectCopy)
  3817. {
  3818. Configuration->Usage->Inc("RemoteCopyTemp");
  3819. UnicodeString RootTempDir;
  3820. UnicodeString TempDir;
  3821. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3822. TStrings * TemporaryFilesList = new TStringList();
  3823. try
  3824. {
  3825. TMakeLocalFileListParams MakeFileListParam;
  3826. MakeFileListParam.FileList = TemporaryFilesList;
  3827. MakeFileListParam.FileTimes = NULL;
  3828. MakeFileListParam.IncludeDirs = true;
  3829. MakeFileListParam.Recursive = false;
  3830. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3831. TTerminalManager::Instance()->ActiveTerminal = Session;
  3832. if (TemporaryFilesList->Count > 0)
  3833. {
  3834. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3835. CopyParam.FileMask = FileMask;
  3836. DebugAssert(!FAutoOperation);
  3837. FAutoOperation = true;
  3838. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3839. }
  3840. }
  3841. __finally
  3842. {
  3843. delete TemporaryFilesList;
  3844. FAutoOperation = false;
  3845. if (!RootTempDir.IsEmpty())
  3846. {
  3847. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3848. }
  3849. }
  3850. }
  3851. else
  3852. {
  3853. RemoteDirView->SaveSelection();
  3854. RemoteDirView->SaveSelectedNames();
  3855. try
  3856. {
  3857. if (Move)
  3858. {
  3859. Configuration->Usage->Inc("RemoteMove");
  3860. Terminal->MoveFiles(FileList, Target, FileMask);
  3861. }
  3862. else
  3863. {
  3864. Configuration->Usage->Inc("RemoteCopyDirect");
  3865. DebugAssert(DirectCopy);
  3866. DebugAssert(Session == FTerminal);
  3867. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3868. FTerminal->CommandSessionOpened ||
  3869. CommandSessionFallback())
  3870. {
  3871. Terminal->CopyFiles(FileList, Target, FileMask);
  3872. }
  3873. }
  3874. }
  3875. catch(...)
  3876. {
  3877. RemoteDirView->DiscardSavedSelection();
  3878. throw;
  3879. }
  3880. RemoteDirView->RestoreSelection();
  3881. }
  3882. }
  3883. return Result;
  3884. }
  3885. //---------------------------------------------------------------------------
  3886. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3887. const UnicodeString & Path, TRemoteProperties & Properties)
  3888. {
  3889. DebugAssert(!IsLocalBrowserMode());
  3890. Properties.Valid = Properties.Valid << vpEncrypt;
  3891. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3892. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3893. }
  3894. //---------------------------------------------------------------------------
  3895. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3896. {
  3897. Side = GetSide(Side);
  3898. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3899. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3900. UnicodeString Name = LoadStr(NEW_FOLDER);
  3901. int AllowedChanges =
  3902. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3903. bool SaveSettings = false;
  3904. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3905. {
  3906. TWindowLock Lock(this);
  3907. if (!IsSideLocalBrowser(Side))
  3908. {
  3909. if (SaveSettings)
  3910. {
  3911. GUIConfiguration->NewDirectoryProperties = Properties;
  3912. }
  3913. CreateRemoteDirectory(Name, Properties);
  3914. }
  3915. else
  3916. {
  3917. DirView(Side)->CreateDirectory(Name);
  3918. }
  3919. }
  3920. }
  3921. //---------------------------------------------------------------------------
  3922. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3923. {
  3924. TWindowLock Lock(this);
  3925. DirView(Side)->ExecuteHomeDirectory();
  3926. }
  3927. //---------------------------------------------------------------------------
  3928. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3929. {
  3930. DoOpenDirectoryDialog(odBrowse, Side);
  3931. }
  3932. //---------------------------------------------------------------------------
  3933. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3934. {
  3935. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3936. bool Result = !Path.IsEmpty();
  3937. if (Result)
  3938. {
  3939. // While we might get here when the session is closed (from location profiles),
  3940. // it's not a problem as the Path setter is noop then
  3941. DirView(Side)->Path = Path;
  3942. }
  3943. return Result;
  3944. }
  3945. //---------------------------------------------------------------------------
  3946. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3947. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3948. bool & Close)
  3949. {
  3950. // terminal can be already closed (e.g. dropped connection)
  3951. if (Terminal != NULL)
  3952. {
  3953. try
  3954. {
  3955. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3956. }
  3957. catch(...)
  3958. {
  3959. if (!Terminal->Active)
  3960. {
  3961. Close = true;
  3962. }
  3963. throw;
  3964. }
  3965. }
  3966. }
  3967. //---------------------------------------------------------------------------
  3968. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3969. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3970. bool & Close)
  3971. {
  3972. // terminal can be already closed (e.g. dropped connection)
  3973. if (Terminal != NULL)
  3974. {
  3975. Configuration->Usage->Inc(L"ChecksumCalculated");
  3976. try
  3977. {
  3978. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3979. }
  3980. catch(...)
  3981. {
  3982. if (!Terminal->Active)
  3983. {
  3984. Close = true;
  3985. }
  3986. throw;
  3987. }
  3988. }
  3989. }
  3990. //---------------------------------------------------------------------------
  3991. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3992. {
  3993. bool Result;
  3994. if (!IsSideLocalBrowser(Side))
  3995. {
  3996. TRemoteTokenList * GroupList = NULL;
  3997. TRemoteTokenList * UserList = NULL;
  3998. try
  3999. {
  4000. TRemoteProperties CurrentProperties;
  4001. if (Terminal->LoadFilesProperties(FileList))
  4002. {
  4003. RemoteDirView->Invalidate();
  4004. }
  4005. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4006. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4007. if (CapableGroupChanging || CapableOwnerChanging)
  4008. {
  4009. if (CapableGroupChanging)
  4010. {
  4011. GroupList = Terminal->Groups->Duplicate();
  4012. }
  4013. if (CapableOwnerChanging)
  4014. {
  4015. UserList = Terminal->Users->Duplicate();
  4016. }
  4017. TRemoteDirectory * Files = Terminal->Files;
  4018. int Count = Files->Count;
  4019. if (Count > 100)
  4020. {
  4021. Count = 100;
  4022. }
  4023. for (int Index = 0; Index < Count; Index++)
  4024. {
  4025. TRemoteFile * File = Files->Files[Index];
  4026. if (CapableGroupChanging)
  4027. {
  4028. GroupList->AddUnique(File->Group);
  4029. }
  4030. if (CapableOwnerChanging)
  4031. {
  4032. UserList->AddUnique(File->Owner);
  4033. }
  4034. }
  4035. // if we haven't collected tokens for all files in current directory,
  4036. // make sure we collect them at least for all selected files.
  4037. // (note that so far the files in FileList has to be from current direcotry)
  4038. if (Count < Files->Count)
  4039. {
  4040. for (int Index = 0; Index < FileList->Count; Index++)
  4041. {
  4042. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4043. if (CapableGroupChanging)
  4044. {
  4045. GroupList->AddUnique(File->Group);
  4046. }
  4047. if (CapableOwnerChanging)
  4048. {
  4049. UserList->AddUnique(File->Owner);
  4050. }
  4051. }
  4052. }
  4053. }
  4054. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4055. int Flags = 0;
  4056. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4057. if (CapableOwnerChanging) Flags |= cpOwner;
  4058. if (CapableGroupChanging) Flags |= cpGroup;
  4059. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4060. if (Terminal->IsCapable[fcCalculatingChecksum])
  4061. {
  4062. CalculateChecksumEvent = CalculateChecksum;
  4063. }
  4064. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4065. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4066. TRemoteProperties NewProperties = CurrentProperties;
  4067. Result =
  4068. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4069. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4070. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4071. CalculateSize, CalculateChecksumEvent);
  4072. if (Result)
  4073. {
  4074. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4075. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4076. }
  4077. }
  4078. __finally
  4079. {
  4080. delete GroupList;
  4081. delete UserList;
  4082. }
  4083. }
  4084. else
  4085. {
  4086. DirView(Side)->DisplayPropertiesMenu();
  4087. Result = true;
  4088. }
  4089. return Result;
  4090. }
  4091. //---------------------------------------------------------------------------
  4092. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4093. {
  4094. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4095. {
  4096. FIgnoreNextDialogChar = Key;
  4097. }
  4098. Key = 0;
  4099. }
  4100. //---------------------------------------------------------------------------
  4101. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4102. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4103. {
  4104. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4105. if (Command != NULL)
  4106. {
  4107. KeyProcessed(Key, Shift);
  4108. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4109. {
  4110. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4111. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4112. false, false, const_cast<TCustomCommandType *>(Command));
  4113. }
  4114. }
  4115. }
  4116. //---------------------------------------------------------------------------
  4117. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4118. {
  4119. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4120. {
  4121. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4122. }
  4123. if (!DirView(osCurrent)->IsEditing())
  4124. {
  4125. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4126. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4127. {
  4128. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4129. if (((Action->ShortCut == KeyShortCut) ||
  4130. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4131. AllowedAction(Action, aaShortCut))
  4132. {
  4133. // Has to be called before the action as the dialog char is already in queue.
  4134. // So when the action consumes message queue, we already need to have the
  4135. // FIgnoreNextDialogChar set
  4136. KeyProcessed(Key, Shift);
  4137. // Reset reference to previous component (when menu/toolbar was clicked).
  4138. // Needed to detect that action was invoked by keyboard shortcut
  4139. // in TNonVisualDataModule::ExplorerActionsExecute
  4140. Action->ActionComponent = NULL;
  4141. Action->Execute();
  4142. return;
  4143. }
  4144. }
  4145. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4146. {
  4147. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4148. {
  4149. KeyProcessed(Key, Shift);
  4150. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4151. return;
  4152. }
  4153. }
  4154. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4155. {
  4156. KeyProcessed(Key, Shift);
  4157. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4158. }
  4159. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4160. if (IsCustomShortCut(CustomShortCut))
  4161. {
  4162. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4163. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4164. if (WinConfiguration->SharedBookmarks != NULL)
  4165. {
  4166. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4167. if ((Bookmark != NULL) &&
  4168. OpenBookmark(FCurrentSide, Bookmark))
  4169. {
  4170. KeyProcessed(Key, Shift);
  4171. }
  4172. }
  4173. }
  4174. }
  4175. TForm::KeyDown(Key, Shift);
  4176. }
  4177. //---------------------------------------------------------------------------
  4178. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4179. {
  4180. DebugAssert(Terminal != NULL);
  4181. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4182. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4183. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4184. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4185. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4186. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4187. // expanded from ?: to avoid memory leaks
  4188. if (SecurityEnabled)
  4189. {
  4190. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4191. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4192. }
  4193. else
  4194. {
  4195. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4196. }
  4197. if (FileSystemInfo.ProtocolName.IsEmpty())
  4198. {
  4199. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4200. }
  4201. else
  4202. {
  4203. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4204. }
  4205. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4206. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4207. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4208. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4209. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4210. UnicodeString CompressionStr = CSCompressionStr;
  4211. if (CSCompressionStr != SCCompressionStr)
  4212. {
  4213. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4214. }
  4215. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4216. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4217. UpdateStatusBar();
  4218. }
  4219. //---------------------------------------------------------------------------
  4220. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4221. {
  4222. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4223. DebugAssert(SessionStatusBar != NULL);
  4224. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4225. {
  4226. // note: (Terminal->Status < sshReady) currently never happens here,
  4227. // so STATUS_CONNECTING is never used
  4228. SessionStatusBar->SimplePanel = true;
  4229. SessionStatusBar->SimpleText = LoadStr(
  4230. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4231. }
  4232. else
  4233. {
  4234. DebugAssert(Terminal);
  4235. SessionStatusBar->SimplePanel = false;
  4236. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4237. if (!FNote.IsEmpty())
  4238. {
  4239. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4240. }
  4241. else
  4242. {
  4243. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4244. }
  4245. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4246. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4247. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4248. }
  4249. }
  4250. //---------------------------------------------------------------------------
  4251. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4252. {
  4253. Panel->Caption = L"";
  4254. }
  4255. //---------------------------------------------------------------------------
  4256. void __fastcall TCustomScpExplorerForm::Idle()
  4257. {
  4258. if (FShowing || FStandaloneEditing)
  4259. {
  4260. FEditorManager->Check();
  4261. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4262. // that needs to know if queue view is visible (and it may be closed after queue update)
  4263. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4264. }
  4265. if (FShowing)
  4266. {
  4267. if (!NonVisualDataModule->Busy &&
  4268. // Menu is opened or drag&drop is going on
  4269. (Mouse->Capture == NULL))
  4270. {
  4271. if (FRefreshRemoteDirectory)
  4272. {
  4273. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4274. {
  4275. Terminal->RefreshDirectory();
  4276. }
  4277. FRefreshRemoteDirectory = false;
  4278. }
  4279. if (FRefreshLocalDirectory)
  4280. {
  4281. ReloadLocalDirectory();
  4282. FRefreshLocalDirectory = false;
  4283. }
  4284. if (WinConfiguration->RefreshRemotePanel)
  4285. {
  4286. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4287. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4288. {
  4289. RemoteDirView->ReloadDirectory();
  4290. }
  4291. }
  4292. }
  4293. }
  4294. if (FShowing || FStandaloneEditing)
  4295. {
  4296. if (FQueueStatusInvalidated)
  4297. {
  4298. UpdateQueueStatus(false);
  4299. }
  4300. RefreshQueueItems();
  4301. }
  4302. if (FShowing)
  4303. {
  4304. UpdateStatusBar();
  4305. }
  4306. FIgnoreNextDialogChar = 0;
  4307. }
  4308. //---------------------------------------------------------------------------
  4309. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4310. {
  4311. try
  4312. {
  4313. FUserActionTimer->Enabled = false;
  4314. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4315. {
  4316. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4317. {
  4318. FPendingQueueActionItem->ProcessUserAction();
  4319. }
  4320. }
  4321. }
  4322. __finally
  4323. {
  4324. FPendingQueueActionItem = NULL;
  4325. }
  4326. }
  4327. //---------------------------------------------------------------------------
  4328. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4329. {
  4330. if (WinConfiguration->MinimizeToTray)
  4331. {
  4332. UpdateTrayIcon();
  4333. FTrayIcon->Visible = true;
  4334. if (Visible)
  4335. {
  4336. ShowWindow(Handle, SW_HIDE);
  4337. }
  4338. }
  4339. }
  4340. //---------------------------------------------------------------------------
  4341. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4342. {
  4343. // WORKAROUND
  4344. // When restoring maximized window from minimization,
  4345. // rarely some controls do not align properly.
  4346. // Two instances seen (both for Commander):
  4347. // - When restoring, window is temporarily narrower (not maximized),
  4348. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4349. // Once maximized already, top dock shinks back, but the session PageControl,
  4350. // do not align up, leaving space between TopDock and PageControl.
  4351. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4352. for (int Index = 0; Index < ControlCount; Index++)
  4353. {
  4354. RealignControl(Controls[Index]);
  4355. }
  4356. if (FTrayIcon->Visible)
  4357. {
  4358. FTrayIcon->Visible = false;
  4359. if (Visible)
  4360. {
  4361. ShowWindow(Handle, SW_SHOW);
  4362. }
  4363. }
  4364. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4365. {
  4366. FNeedSession = false;
  4367. NonVisualDataModule->StartBusy();
  4368. try
  4369. {
  4370. NeedSession(true);
  4371. }
  4372. __finally
  4373. {
  4374. NonVisualDataModule->EndBusy();
  4375. }
  4376. }
  4377. }
  4378. //---------------------------------------------------------------------------
  4379. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4380. {
  4381. FTrayIcon->Hint = GetMainForm()->Caption;
  4382. }
  4383. //---------------------------------------------------------------------------
  4384. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4385. {
  4386. UpdateTrayIcon();
  4387. }
  4388. //---------------------------------------------------------------------------
  4389. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4390. {
  4391. // workaround
  4392. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4393. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4394. // But WM_ACTIVATEAPP message can be received even when
  4395. // the main window is minimized to the tray and internal editor window is focused
  4396. // (after another application was previously active)
  4397. if (::IsIconic(Handle))
  4398. {
  4399. if (!IsAppIconic())
  4400. {
  4401. SetAppIconic(true);
  4402. }
  4403. }
  4404. ::ApplicationRestore();
  4405. Application->BringToFront();
  4406. }
  4407. //---------------------------------------------------------------------------
  4408. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4409. {
  4410. RestoreApp();
  4411. }
  4412. //---------------------------------------------------------------------------
  4413. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4414. {
  4415. if (OpenInNewWindow())
  4416. {
  4417. // todo: Pass FromSite
  4418. ExecuteNewInstance(SessionUrl);
  4419. }
  4420. else
  4421. {
  4422. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4423. }
  4424. }
  4425. //---------------------------------------------------------------------------
  4426. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4427. {
  4428. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4429. StoredSessions->NewSession(SessionName, SessionData);
  4430. // modified only, explicit
  4431. StoredSessions->Save(false, true);
  4432. // encode session name because of slashes in hierarchical sessions
  4433. return EncodeUrlString(SessionName);
  4434. }
  4435. //---------------------------------------------------------------------------
  4436. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4437. {
  4438. // current working directories become defaults here, what is not right
  4439. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4440. return SaveHiddenDuplicateSession(SessionData.get());
  4441. }
  4442. //---------------------------------------------------------------------------
  4443. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4444. {
  4445. if (OpenInNewWindow())
  4446. {
  4447. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4448. }
  4449. else
  4450. {
  4451. // current working directories become defaults here, what is not right
  4452. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4453. TTerminalManager * Manager = TTerminalManager::Instance();
  4454. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4455. Manager->ActiveTerminal = Terminal;
  4456. }
  4457. }
  4458. //---------------------------------------------------------------------------
  4459. void __fastcall TCustomScpExplorerForm::RenameSession()
  4460. {
  4461. UnicodeString Name = Terminal->SessionData->SessionName;
  4462. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4463. {
  4464. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4465. TSessionData::ValidateName(Name);
  4466. // This is inconsistent with how color (for example) is handled.
  4467. Terminal->SessionData->Name = Name;
  4468. UpdateControls();
  4469. // Add/Remove distinguishing paths from sessions of the same name.
  4470. DoTerminalListChanged();
  4471. }
  4472. }
  4473. //---------------------------------------------------------------------------
  4474. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4475. {
  4476. DebugAssert(Queue != NULL);
  4477. bool Result = Queue->IsEmpty;
  4478. if (!Result)
  4479. {
  4480. SetFocus();
  4481. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4482. }
  4483. return Result;
  4484. }
  4485. //---------------------------------------------------------------------------
  4486. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4487. {
  4488. return CanCloseQueue(FQueue);
  4489. }
  4490. //---------------------------------------------------------------------------
  4491. void __fastcall TCustomScpExplorerForm::CloseSession()
  4492. {
  4493. if (CanCloseQueue())
  4494. {
  4495. TTerminalManager::Instance()->FreeActiveTerminal();
  4496. }
  4497. }
  4498. //---------------------------------------------------------------------------
  4499. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4500. {
  4501. if (CanCloseQueue())
  4502. {
  4503. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4504. }
  4505. }
  4506. //---------------------------------------------------------------------------
  4507. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4508. {
  4509. RemoteDirView->Terminal = Terminal;
  4510. UpdateRemotePathComboBox(false);
  4511. UpdateControls();
  4512. }
  4513. //---------------------------------------------------------------------------
  4514. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4515. {
  4516. if (DebugAlwaysTrue(!Terminal->Active))
  4517. {
  4518. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4519. }
  4520. }
  4521. //---------------------------------------------------------------------------
  4522. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4523. {
  4524. if (OpenInNewWindow())
  4525. {
  4526. // encode session name because of slashes in hierarchical sessions
  4527. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4528. }
  4529. else
  4530. {
  4531. TTerminalManager * Manager = TTerminalManager::Instance();
  4532. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4533. Manager->ActiveTerminal = Terminal;
  4534. }
  4535. }
  4536. //---------------------------------------------------------------------------
  4537. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4538. {
  4539. TTerminalManager * Manager = TTerminalManager::Instance();
  4540. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4541. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4542. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4543. if (!ConnectFirstTerminal)
  4544. {
  4545. FirstTerminal->Disconnected = true;
  4546. FirstTerminal->DisconnectedTemporarily = true;
  4547. }
  4548. Manager->ActiveTerminal = FirstTerminal;
  4549. }
  4550. //---------------------------------------------------------------------------
  4551. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4552. {
  4553. if (OpenInNewWindow())
  4554. {
  4555. ExecuteNewInstance(Name);
  4556. }
  4557. else
  4558. {
  4559. DoOpenFolderOrWorkspace(Name, true);
  4560. }
  4561. }
  4562. //---------------------------------------------------------------------------
  4563. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4564. bool &CanClose)
  4565. {
  4566. if (Terminal != NULL)
  4567. {
  4568. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4569. {
  4570. unsigned int Result;
  4571. TMessageParams Params(mpNeverAskAgainCheck);
  4572. UnicodeString Message;
  4573. int Answers = qaOK | qaCancel;
  4574. if (TTerminalManager::Instance()->Count > 1)
  4575. {
  4576. if (!WinConfiguration->AutoSaveWorkspace)
  4577. {
  4578. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4579. }
  4580. else
  4581. {
  4582. Message = LoadStr(CLOSE_SESSIONS);
  4583. }
  4584. }
  4585. else
  4586. {
  4587. if (!WinConfiguration->AutoSaveWorkspace)
  4588. {
  4589. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4590. }
  4591. else
  4592. {
  4593. Message = LoadStr(CLOSE_SESSION);
  4594. }
  4595. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4596. }
  4597. UnicodeString Note;
  4598. if (WinConfiguration->AutoSaveWorkspace)
  4599. {
  4600. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4601. }
  4602. else
  4603. {
  4604. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4605. Answers = qaYes | qaNo | qaCancel;
  4606. }
  4607. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4608. SetFocus();
  4609. Result = MessageDialog(Message, qtConfirmation,
  4610. Answers, HELP_NONE, &Params);
  4611. if (Result == qaNeverAskAgain)
  4612. {
  4613. WinConfiguration->ConfirmClosingSession = false;
  4614. }
  4615. if (Result == qaNo)
  4616. {
  4617. CanClose = SaveWorkspace(true);
  4618. // note that the workspace will be saved redundatly again from FormClose
  4619. }
  4620. else
  4621. {
  4622. CanClose =
  4623. (Result == qaOK) ||
  4624. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4625. (Result == qaNeverAskAgain);
  4626. }
  4627. }
  4628. if (CanClose)
  4629. {
  4630. CanClose = CanCloseQueue();
  4631. }
  4632. }
  4633. if (CanClose)
  4634. {
  4635. CanClose =
  4636. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4637. FEditorManager->CloseExternalFilesWithoutProcess();
  4638. if (CanClose)
  4639. {
  4640. while (CanClose && (FLocalEditors->Count > 0))
  4641. {
  4642. int PrevCount = FLocalEditors->Count;
  4643. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4644. CanClose = (FLocalEditors->Count < PrevCount);
  4645. }
  4646. if (CanClose)
  4647. {
  4648. CanClose = FEditorManager->Empty(true);
  4649. if (!CanClose)
  4650. {
  4651. SetFocus();
  4652. CanClose =
  4653. (MessageDialog(
  4654. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4655. }
  4656. }
  4657. }
  4658. }
  4659. }
  4660. //---------------------------------------------------------------------------
  4661. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4662. {
  4663. TForm * Form = dynamic_cast<TForm *>(Sender);
  4664. DebugAssert(Form != NULL);
  4665. Form->Close();
  4666. }
  4667. //---------------------------------------------------------------------------
  4668. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4669. {
  4670. TForm * Form = dynamic_cast<TForm *>(Sender);
  4671. delete Form;
  4672. }
  4673. //---------------------------------------------------------------------------
  4674. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4675. {
  4676. while (FLocalEditors->Count > 0)
  4677. {
  4678. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4679. }
  4680. }
  4681. //---------------------------------------------------------------------------
  4682. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4683. TObject *Sender)
  4684. {
  4685. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4686. try
  4687. {
  4688. SetProperties(osRemote, FileList);
  4689. }
  4690. __finally
  4691. {
  4692. delete FileList;
  4693. }
  4694. }
  4695. //---------------------------------------------------------------------------
  4696. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4697. {
  4698. if (value)
  4699. {
  4700. if (Component == fcCustomCommandsBand)
  4701. {
  4702. UpdateCustomCommandsToolbar();
  4703. }
  4704. }
  4705. }
  4706. //---------------------------------------------------------------------------
  4707. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4708. {
  4709. TControl * Control = GetComponent(Component);
  4710. DebugAssert(Control);
  4711. bool Changed = (Control->Visible != value);
  4712. if (Changed)
  4713. {
  4714. ComponentShowing(Component, value);
  4715. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4716. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4717. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4718. if (value)
  4719. {
  4720. int RemainingHeight = Control->Parent->ClientHeight;
  4721. int RemainingWidth = Control->Parent->ClientWidth;
  4722. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4723. {
  4724. TControl * ChildControl = Control->Parent->Controls[i];
  4725. if (ChildControl->Visible)
  4726. {
  4727. switch (ChildControl->Align)
  4728. {
  4729. case alTop:
  4730. case alBottom:
  4731. RemainingHeight -= ChildControl->Height;
  4732. break;
  4733. case alLeft:
  4734. case alRight:
  4735. RemainingWidth -= ChildControl->Width;
  4736. break;
  4737. }
  4738. }
  4739. }
  4740. int Reserve = ScaleByTextHeight(this, 32);
  4741. // queue in explorer, trees in commander
  4742. if (Control->Height > RemainingHeight - Reserve)
  4743. {
  4744. Control->Height = RemainingHeight / 2;
  4745. }
  4746. if (Control->Width > RemainingWidth - Reserve)
  4747. {
  4748. Control->Width = RemainingWidth / 2;
  4749. }
  4750. }
  4751. Control->Visible = value;
  4752. if (WasFocused && Visible)
  4753. {
  4754. DirView(osCurrent)->SetFocus();
  4755. }
  4756. FixControlsPlacement();
  4757. }
  4758. }
  4759. //---------------------------------------------------------------------------
  4760. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4761. {
  4762. TControl * Control = GetComponent(Component);
  4763. if (Control == NULL)
  4764. {
  4765. return false;
  4766. }
  4767. else
  4768. {
  4769. return Control->Visible;
  4770. }
  4771. }
  4772. //---------------------------------------------------------------------------
  4773. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4774. {
  4775. bool Result;
  4776. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4777. {
  4778. Result = !IsUWP();
  4779. }
  4780. else
  4781. {
  4782. Result = true;
  4783. }
  4784. return Result;
  4785. }
  4786. //---------------------------------------------------------------------------
  4787. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4788. {
  4789. if (DirView(osOther)->ItemFocused != NULL)
  4790. {
  4791. DirView(osOther)->ItemFocused->MakeVisible(false);
  4792. }
  4793. QueueSplitter->Visible = QueuePanel->Visible;
  4794. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4795. }
  4796. //---------------------------------------------------------------------------
  4797. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4798. {
  4799. switch (Component) {
  4800. case fcStatusBar: return RemoteStatusBar;
  4801. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4802. case fcQueueView: return QueuePanel;
  4803. case fcQueueToolbar: return QueueDock;
  4804. case fcRemoteTree: return RemoteDrivePanel;
  4805. case fcSessionsTabs: return SessionsPageControl;
  4806. default: return NULL;
  4807. }
  4808. }
  4809. //---------------------------------------------------------------------------
  4810. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4811. TObject *Sender, TListColumn *Column, TPoint &Point)
  4812. {
  4813. DebugAssert(NonVisualDataModule && Column && Sender);
  4814. NonVisualDataModule->ListColumn = Column;
  4815. TPopupMenu * DirViewColumnMenu;
  4816. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4817. {
  4818. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4819. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4820. (Column->Index == uvName);
  4821. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4822. (Column->Index == uvSize);
  4823. }
  4824. else
  4825. {
  4826. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4827. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4828. (Column->Index == dvName);
  4829. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4830. (Column->Index == dvSize);
  4831. }
  4832. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4833. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4834. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4835. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4836. }
  4837. //---------------------------------------------------------------------------
  4838. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4839. TObject *Sender, TListItem *Item, bool &AllowExec)
  4840. {
  4841. DoDirViewExecFile(Sender, Item, AllowExec);
  4842. }
  4843. //---------------------------------------------------------------------------
  4844. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4845. TListItem * Item, bool & AllowExec)
  4846. {
  4847. DebugAssert(Sender && Item && Configuration);
  4848. DebugAssert(AllowExec);
  4849. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4850. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4851. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4852. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4853. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4854. // on files only (not directories)
  4855. // and only when symlinks are resolved (apply to remote panel only)
  4856. if (!ADirView->ItemIsDirectory(Item) &&
  4857. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4858. {
  4859. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4860. !FForceExecution &&
  4861. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4862. {
  4863. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4864. {
  4865. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4866. UnlockWindow();
  4867. try
  4868. {
  4869. ExecuteFileOperation(
  4870. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4871. }
  4872. __finally
  4873. {
  4874. LockWindow();
  4875. }
  4876. AllowExec = false;
  4877. }
  4878. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4879. {
  4880. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4881. {
  4882. ExecuteFile(Side, efDefaultEditor);
  4883. AllowExec = false;
  4884. }
  4885. }
  4886. else
  4887. {
  4888. DebugFail();
  4889. }
  4890. }
  4891. // if we have not done anything special, fall back to default behavior
  4892. if (AllowExec)
  4893. {
  4894. if (Remote && !WinConfiguration->DisableOpenEdit)
  4895. {
  4896. ExecuteFile(osRemote, efShell);
  4897. AllowExec = false;
  4898. }
  4899. }
  4900. }
  4901. }
  4902. //---------------------------------------------------------------------------
  4903. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4904. {
  4905. return ((Side == osRemote) || (Side == osCurrent));
  4906. }
  4907. //---------------------------------------------------------------------------
  4908. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4909. {
  4910. DebugFail();
  4911. }
  4912. //---------------------------------------------------------------------------
  4913. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4914. {
  4915. DebugFail();
  4916. }
  4917. //---------------------------------------------------------------------------
  4918. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4919. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  4920. {
  4921. TSynchronizeParamType Params;
  4922. Params.LocalDirectory = LocalDirectory;
  4923. Params.RemoteDirectory = RemoteDirectory;
  4924. int UnusedParams =
  4925. (GUIConfiguration->SynchronizeParams &
  4926. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4927. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4928. Params.Options = GUIConfiguration->SynchronizeOptions;
  4929. bool SaveSettings = false;
  4930. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4931. &DoSynchronizeTooManyDirectories);
  4932. DebugAssert(FSynchronizeController == NULL);
  4933. FSynchronizeController = &Controller;
  4934. bool Result;
  4935. try
  4936. {
  4937. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4938. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4939. int Options =
  4940. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4941. DebugAssert(FOnFeedSynchronizeError == NULL);
  4942. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4943. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4944. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4945. if (Result)
  4946. {
  4947. if (SaveSettings)
  4948. {
  4949. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4950. GUIConfiguration->SynchronizeOptions = Params.Options;
  4951. }
  4952. else
  4953. {
  4954. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4955. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4956. FLAGSET(Params.Options, soSynchronize))
  4957. {
  4958. GUIConfiguration->SynchronizeOptions =
  4959. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4960. soSynchronize;
  4961. }
  4962. }
  4963. LocalDirectory = Params.LocalDirectory;
  4964. RemoteDirectory = Params.RemoteDirectory;
  4965. }
  4966. }
  4967. __finally
  4968. {
  4969. FSynchronizeController = NULL;
  4970. DebugAssert(FOnFeedSynchronizeError == NULL);
  4971. FOnFeedSynchronizeError = NULL;
  4972. }
  4973. return Result;
  4974. }
  4975. //---------------------------------------------------------------------------
  4976. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4977. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4978. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4979. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4980. TSynchronizeOptions * Options, bool Full)
  4981. {
  4982. // Keep-up-to-date mode
  4983. if (Terminal->Status == ssOpened)
  4984. {
  4985. try
  4986. {
  4987. int PParams = Params.Params;
  4988. if (!Full)
  4989. {
  4990. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4991. if (FLAGSET(Params.Options, soRecurse))
  4992. {
  4993. PParams |= TTerminal::spSubDirs;
  4994. }
  4995. }
  4996. else
  4997. {
  4998. // if keepuptodate is non-recursive,
  4999. // full sync before has to be non-recursive as well
  5000. if (FLAGCLEAR(Params.Options, soRecurse))
  5001. {
  5002. PParams |= TTerminal::spNoRecurse;
  5003. }
  5004. }
  5005. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5006. PParams, Checklist, Options);
  5007. }
  5008. catch (Exception & E)
  5009. {
  5010. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5011. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5012. {
  5013. // noop, already logged in MoreMessageDialog
  5014. }
  5015. else
  5016. {
  5017. ShowExtendedExceptionEx(Terminal, &E);
  5018. throw;
  5019. }
  5020. }
  5021. }
  5022. }
  5023. //---------------------------------------------------------------------------
  5024. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5025. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5026. const UnicodeString ErrorStr)
  5027. {
  5028. if (!Directory.IsEmpty())
  5029. {
  5030. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5031. }
  5032. else
  5033. {
  5034. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5035. }
  5036. }
  5037. //---------------------------------------------------------------------------
  5038. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5039. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5040. {
  5041. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5042. {
  5043. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5044. }
  5045. else
  5046. {
  5047. TMessageParams Params(mpNeverAskAgainCheck);
  5048. unsigned int Result = MessageDialog(
  5049. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5050. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5051. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5052. {
  5053. MaxDirectories *= 2;
  5054. if (Result == qaNeverAskAgain)
  5055. {
  5056. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5057. }
  5058. }
  5059. else
  5060. {
  5061. Abort();
  5062. }
  5063. }
  5064. }
  5065. //---------------------------------------------------------------------------
  5066. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5067. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5068. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5069. TSynchronizeOptions * Options)
  5070. {
  5071. // Keep-up-to-date mode
  5072. DebugAssert(!FAutoOperation);
  5073. void * BatchStorage;
  5074. BatchStart(BatchStorage);
  5075. FAutoOperation = true;
  5076. bool AnyOperation = false;
  5077. TDateTime StartTime = Now();
  5078. TSynchronizeChecklist * AChecklist = NULL;
  5079. try
  5080. {
  5081. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5082. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5083. {
  5084. FSynchronizeProgressForm->Start();
  5085. }
  5086. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5087. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5088. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5089. Options);
  5090. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5091. SAFE_DESTROY(FSynchronizeProgressForm);
  5092. AnyOperation = (AChecklist->CheckedCount > 0);
  5093. if (AnyOperation)
  5094. {
  5095. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5096. CreateProgressForm(&SynchronizeProgress);
  5097. try
  5098. {
  5099. Terminal->SynchronizeApply(
  5100. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5101. SynchronizeProcessedItem, NULL, NULL, NULL);
  5102. }
  5103. __finally
  5104. {
  5105. DestroyProgressForm();
  5106. }
  5107. }
  5108. }
  5109. __finally
  5110. {
  5111. if (Checklist == NULL)
  5112. {
  5113. delete AChecklist;
  5114. }
  5115. else
  5116. {
  5117. *Checklist = AChecklist;
  5118. }
  5119. FAutoOperation = false;
  5120. SAFE_DESTROY(FSynchronizeProgressForm);
  5121. BatchEnd(BatchStorage);
  5122. ReloadLocalDirectory();
  5123. if (AnyOperation)
  5124. {
  5125. OperationComplete(StartTime);
  5126. }
  5127. }
  5128. }
  5129. //---------------------------------------------------------------------------
  5130. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5131. {
  5132. // can be called from command line
  5133. return Visible &&
  5134. ((DirView(osOther)->SelCount > 0) ||
  5135. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5136. }
  5137. //---------------------------------------------------------------------------
  5138. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5139. {
  5140. LogSynchronizeEvent(Terminal, Message);
  5141. }
  5142. //---------------------------------------------------------------------------
  5143. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5144. int Params, TSynchronizeOptions & Options)
  5145. {
  5146. DebugAssert(!IsLocalBrowserMode());
  5147. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5148. {
  5149. Options.Filter = new TStringList();
  5150. Options.Filter->CaseSensitive = false;
  5151. Options.Filter->Duplicates = Types::dupAccept;
  5152. if (DirView(osRemote)->SelCount > 0)
  5153. {
  5154. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5155. }
  5156. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5157. {
  5158. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5159. }
  5160. Options.Filter->Sort();
  5161. }
  5162. }
  5163. //---------------------------------------------------------------------------
  5164. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5165. {
  5166. TCopyParamType Defaults;
  5167. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5168. ConfigStorage->AccessMode = smRead;
  5169. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5170. {
  5171. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5172. }
  5173. ConfigStorage.reset(NULL);
  5174. std::unique_ptr<TStringList> Options(new TStringList());
  5175. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5176. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5177. UnicodeString Result;
  5178. if (Options->Count > 0)
  5179. {
  5180. Result =
  5181. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5182. }
  5183. return Result;
  5184. }
  5185. //---------------------------------------------------------------------------
  5186. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5187. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5188. {
  5189. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5190. UnicodeString AdditionalParams =
  5191. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5192. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5193. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5194. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5195. SerializeCopyParamForCommandLine(CopyParams)));
  5196. ExecuteNewInstance(SessionName, AdditionalParams);
  5197. }
  5198. //---------------------------------------------------------------------------
  5199. struct TSynchronizeParams
  5200. {
  5201. UnicodeString LocalDirectory;
  5202. UnicodeString RemoteDirectory;
  5203. TSynchronizeMode Mode;
  5204. TSynchronizeChecklist * Checklist;
  5205. int Params;
  5206. TCopyParamType * CopyParam;
  5207. TDateTime CollectElapsed;
  5208. TDateTime * StartTime;
  5209. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5210. };
  5211. //---------------------------------------------------------------------------
  5212. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5213. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5214. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5215. {
  5216. DebugAssert(!FAutoOperation);
  5217. void * BatchStorage;
  5218. BatchStart(BatchStorage);
  5219. FAutoOperation = true;
  5220. TFileOperationStatistics Statistics;
  5221. TDateTime Start = Now();
  5222. try
  5223. {
  5224. Params.OnProcessedItem = OnProcessedItem;
  5225. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5226. CreateProgressForm(&SynchronizeProgress);
  5227. Terminal->SynchronizeApply(
  5228. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5229. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5230. &Statistics);
  5231. }
  5232. __finally
  5233. {
  5234. Params.OnProcessedItem = NULL;
  5235. FAutoOperation = false;
  5236. DestroyProgressForm();
  5237. BatchEnd(BatchStorage);
  5238. ReloadLocalDirectory();
  5239. }
  5240. if (WinConfiguration->SynchronizeSummary)
  5241. {
  5242. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5243. // The statistics should be 0 anyway in this case
  5244. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5245. {
  5246. Message += L"\n";
  5247. if (Statistics.FilesUploaded > 0)
  5248. {
  5249. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5250. }
  5251. if (Statistics.FilesDownloaded > 0)
  5252. {
  5253. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5254. }
  5255. if (Statistics.FilesDeletedLocal > 0)
  5256. {
  5257. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5258. }
  5259. if (Statistics.FilesDeletedRemote > 0)
  5260. {
  5261. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5262. }
  5263. }
  5264. TDateTime Elapsed = (Now() - Start);
  5265. Message +=
  5266. L"\n" +
  5267. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5268. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5269. TMessageParams Params(mpNeverAskAgainCheck);
  5270. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5271. if (Result == qaNeverAskAgain)
  5272. {
  5273. WinConfiguration->SynchronizeSummary = false;
  5274. }
  5275. }
  5276. }
  5277. //---------------------------------------------------------------------------
  5278. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5279. {
  5280. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5281. {
  5282. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5283. }
  5284. // Not set in keep-up-to-date mode - Synchronize() method
  5285. if (Token != NULL)
  5286. {
  5287. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5288. if (Params.OnProcessedItem != NULL)
  5289. {
  5290. Params.OnProcessedItem(NULL, ChecklistItem);
  5291. }
  5292. }
  5293. }
  5294. //---------------------------------------------------------------------------
  5295. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5296. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5297. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5298. {
  5299. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5300. *Params.StartTime = Now();
  5301. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5302. }
  5303. //---------------------------------------------------------------------------
  5304. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5305. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5306. {
  5307. // terminal can be already closed (e.g. dropped connection)
  5308. if (Terminal != NULL)
  5309. {
  5310. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5311. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5312. }
  5313. }
  5314. //---------------------------------------------------------------------------
  5315. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5316. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5317. {
  5318. DebugAssert(!IsLocalBrowserMode());
  5319. TAutoBatch AutoBatch(this);
  5320. TAutoFlag AutoOperationFlag(FAutoOperation);
  5321. if (Side == osRemote)
  5322. {
  5323. std::unique_ptr<TStrings> FileList(new TStringList());
  5324. FileList->AddObject(FileName, RemoteFile);
  5325. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5326. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5327. RemoteDirView->SaveSelection();
  5328. RemoteDirView->SaveSelectedNames();
  5329. try
  5330. {
  5331. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5332. }
  5333. catch(...)
  5334. {
  5335. RemoteDirView->DiscardSavedSelection();
  5336. throw;
  5337. }
  5338. RemoteDirView->RestoreSelection();
  5339. }
  5340. else if (DebugAlwaysTrue(Side == osLocal))
  5341. {
  5342. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5343. {
  5344. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5345. }
  5346. UnicodeString Directory = ExtractFileDir(FileName);
  5347. ReloadLocalDirectory(Directory);
  5348. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5349. if (!SamePaths(Directory, NewDirectory))
  5350. {
  5351. ReloadLocalDirectory(NewDirectory);
  5352. }
  5353. }
  5354. }
  5355. //---------------------------------------------------------------------------
  5356. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5357. {
  5358. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5359. if (Side == osLocal)
  5360. {
  5361. if (Action == TSynchronizeChecklist::saDownloadNew)
  5362. {
  5363. OpenFolderInExplorer(LocalPath);
  5364. }
  5365. else
  5366. {
  5367. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5368. }
  5369. }
  5370. else if (DebugAlwaysTrue(Side == osRemote))
  5371. {
  5372. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5373. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5374. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5375. if (!LocalPath.IsEmpty())
  5376. {
  5377. SessionData->LocalDirectory = LocalPath;
  5378. }
  5379. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5380. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5381. }
  5382. }
  5383. //---------------------------------------------------------------------------
  5384. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5385. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5386. const TCopyParamType * CopyParams)
  5387. {
  5388. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5389. UnicodeString AdditionalParams =
  5390. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5391. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5392. LocalDirectory, RemoteDirectory, Mode, Params,
  5393. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5394. SerializeCopyParamForCommandLine(CopyParams)));
  5395. ExecuteNewInstance(SessionName, AdditionalParams);
  5396. }
  5397. //---------------------------------------------------------------------------
  5398. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5399. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5400. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5401. {
  5402. int Result;
  5403. bool SaveSettings = false;
  5404. int Options =
  5405. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5406. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5407. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5408. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5409. bool Continue =
  5410. (UseDefaults == 0) ||
  5411. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5412. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5413. if (Continue)
  5414. {
  5415. Configuration->Usage->Inc(L"Synchronizations");
  5416. UpdateCopyParamCounters(CopyParam);
  5417. TSynchronizeOptions SynchronizeOptions;
  5418. GetSynchronizeOptions(Params, SynchronizeOptions);
  5419. if (SaveSettings)
  5420. {
  5421. GUIConfiguration->SynchronizeParams = Params;
  5422. }
  5423. else
  5424. {
  5425. SaveMode = false;
  5426. }
  5427. TDateTime StartTime = Now();
  5428. TSynchronizeChecklist * Checklist = NULL;
  5429. try
  5430. {
  5431. DebugAssert(!FAutoOperation);
  5432. FAutoOperation = true;
  5433. try
  5434. {
  5435. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5436. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5437. int Files = -1;
  5438. if (DataList->Count >= 1)
  5439. {
  5440. Files = StrToIntDef(DataList->Strings[0], Files);
  5441. }
  5442. else
  5443. {
  5444. DataList->Add(UnicodeString());
  5445. }
  5446. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5447. FSynchronizeProgressForm->Start();
  5448. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5449. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5450. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5451. &SynchronizeOptions);
  5452. if (Terminal->SessionData->CacheDirectories)
  5453. {
  5454. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5455. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5456. }
  5457. }
  5458. __finally
  5459. {
  5460. FAutoOperation = false;
  5461. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5462. SAFE_DESTROY(FSynchronizeProgressForm);
  5463. }
  5464. TSynchronizeParams SynchronizeParams;
  5465. SynchronizeParams.LocalDirectory = LocalDirectory;
  5466. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5467. SynchronizeParams.Mode = Mode;
  5468. SynchronizeParams.CopyParam = &CopyParam;
  5469. SynchronizeParams.Params = Params;
  5470. SynchronizeParams.Checklist = Checklist;
  5471. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5472. SynchronizeParams.StartTime = &StartTime;
  5473. SynchronizeParams.OnProcessedItem = NULL;
  5474. Result = Checklist->Count;
  5475. if (Checklist->Count > 0)
  5476. {
  5477. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5478. {
  5479. if (!DoSynchronizeChecklistDialog(
  5480. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5481. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5482. {
  5483. Result = -1;
  5484. }
  5485. }
  5486. else
  5487. {
  5488. FullSynchronize(SynchronizeParams, NULL, NULL);
  5489. }
  5490. }
  5491. else
  5492. {
  5493. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5494. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5495. }
  5496. }
  5497. __finally
  5498. {
  5499. delete Checklist;
  5500. }
  5501. OperationComplete(StartTime);
  5502. }
  5503. else
  5504. {
  5505. Result = -1;
  5506. }
  5507. return Result;
  5508. }
  5509. //---------------------------------------------------------------------------
  5510. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5511. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5512. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5513. {
  5514. if (Collect)
  5515. {
  5516. DebugAssert(FSynchronizeProgressForm != NULL);
  5517. if (!FSynchronizeProgressForm->Started)
  5518. {
  5519. FSynchronizeProgressForm->Start();
  5520. }
  5521. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5522. SetTaskbarListProgressValue(CompareProgress);
  5523. }
  5524. else
  5525. {
  5526. DebugAssert(FSynchronizeProgressForm == NULL);
  5527. DebugAssert(FProgressForm != NULL);
  5528. }
  5529. }
  5530. //---------------------------------------------------------------------------
  5531. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5532. {
  5533. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5534. TRemoteFile * File = NULL;
  5535. Terminal->ReadFile(FullFileName, File);
  5536. if (File != NULL)
  5537. {
  5538. std::unique_ptr<TRemoteFile> FileOwner(File);
  5539. TAutoFlag Flag(FStandaloneEditing);
  5540. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5541. Application->ShowMainForm = false;
  5542. Application->Run();
  5543. }
  5544. }
  5545. //---------------------------------------------------------------------------
  5546. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5547. {
  5548. DebugFail();
  5549. }
  5550. //---------------------------------------------------------------------------
  5551. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5552. {
  5553. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5554. SessionData->Assign(Terminal->SessionData);
  5555. UpdateSessionData(SessionData.get());
  5556. Terminal->UpdateSessionCredentials(SessionData.get());
  5557. if (!Terminal->SessionData->HasSessionName())
  5558. {
  5559. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5560. // once IsWorkspace is cleared
  5561. SessionData->Name = UnicodeString();
  5562. SessionData->IsWorkspace = false;
  5563. }
  5564. return SessionData.release();
  5565. }
  5566. //---------------------------------------------------------------------------
  5567. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5568. {
  5569. TSessionData * SessionData = CloneCurrentSessionData();
  5570. try
  5571. {
  5572. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5573. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5574. }
  5575. __finally
  5576. {
  5577. delete SessionData;
  5578. }
  5579. }
  5580. //---------------------------------------------------------------------------
  5581. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5582. {
  5583. TTerminalManager * Manager = TTerminalManager::Instance();
  5584. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5585. if (DebugAlwaysTrue(Terminal != NULL))
  5586. {
  5587. // Update (Managed)Terminal->StateData
  5588. UpdateTerminal(Terminal);
  5589. }
  5590. Manager->SaveWorkspace(DataList.get());
  5591. return DataList.release();
  5592. }
  5593. //---------------------------------------------------------------------------
  5594. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5595. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5596. {
  5597. WinConfiguration->LastWorkspace = Name;
  5598. if (!SavePasswords)
  5599. {
  5600. for (int Index = 0; Index < DataList->Count; Index++)
  5601. {
  5602. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5603. if (SessionData->Link.IsEmpty())
  5604. {
  5605. SessionData->ClearSessionPasswords();
  5606. }
  5607. }
  5608. }
  5609. StoredSessions->NewWorkspace(Name, DataList);
  5610. // modified only
  5611. StoredSessions->Save(false, Explicit);
  5612. }
  5613. //---------------------------------------------------------------------------
  5614. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5615. {
  5616. return
  5617. DefaultStr(WinConfiguration->LastWorkspace,
  5618. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5619. }
  5620. //---------------------------------------------------------------------------
  5621. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5622. {
  5623. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5624. bool AnyNonStoredSessionWithPassword = false;
  5625. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5626. bool AllNonStoredSessionsAnonymous = true;
  5627. TTerminalManager * Manager = TTerminalManager::Instance();
  5628. for (int Index = 0; Index < DataList->Count; Index++)
  5629. {
  5630. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5631. if (SessionData->Link.IsEmpty())
  5632. {
  5633. if (SessionData->HasAnySessionPassword())
  5634. {
  5635. AnyNonStoredSessionWithPassword = true;
  5636. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5637. {
  5638. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5639. }
  5640. }
  5641. if (!SameText(SessionData->UserName, AnonymousUserName))
  5642. {
  5643. AllNonStoredSessionsAnonymous = false;
  5644. }
  5645. }
  5646. }
  5647. bool SavePasswords;
  5648. bool * PSavePasswords;
  5649. bool NotRecommendedSavingPasswords =
  5650. !CustomWinConfiguration->UseMasterPassword &&
  5651. !AllNonStoredSessionsAnonymous;
  5652. if (Configuration->DisablePasswordStoring ||
  5653. !AnyNonStoredSessionWithPassword)
  5654. {
  5655. PSavePasswords = NULL;
  5656. SavePasswords = false;
  5657. }
  5658. else
  5659. {
  5660. PSavePasswords = &SavePasswords;
  5661. SavePasswords =
  5662. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5663. !NotRecommendedSavingPasswords;
  5664. }
  5665. UnicodeString Name = WorkspaceName();
  5666. bool CreateShortcut = false;
  5667. bool Result =
  5668. DoSaveWorkspaceDialog(
  5669. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5670. EnableAutoSave);
  5671. if (Result)
  5672. {
  5673. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5674. if (CreateShortcut)
  5675. {
  5676. TOperationVisualizer Visualizer;
  5677. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5678. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5679. }
  5680. if (EnableAutoSave)
  5681. {
  5682. WinConfiguration->AutoSaveWorkspace = true;
  5683. WinConfiguration->AutoWorkspace = Name;
  5684. if (PSavePasswords != NULL)
  5685. {
  5686. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5687. }
  5688. }
  5689. }
  5690. return Result;
  5691. }
  5692. //---------------------------------------------------------------------------
  5693. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5694. {
  5695. DebugAssert(!IsLocalBrowserMode());
  5696. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5697. if (WinConfiguration->PreservePanelState)
  5698. {
  5699. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5700. }
  5701. UpdateSessionData(Terminal->StateData);
  5702. }
  5703. //---------------------------------------------------------------------------
  5704. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5705. {
  5706. // Keep in sync with TSessionData::CopyStateData
  5707. DebugAssert(!IsLocalBrowserMode());
  5708. DebugAssert(Data != NULL);
  5709. // This is inconsistent with how color (for example) is handled.
  5710. Data->Name = Terminal->SessionData->Name;
  5711. // cannot use RemoteDirView->Path, because it is empty if connection
  5712. // was already closed
  5713. // also only peek, we may not be connected at all atm
  5714. // so make sure we do not try retrieving current directory from the server
  5715. // (particularly with FTP)
  5716. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5717. if (!ACurrentDirectory.IsEmpty())
  5718. {
  5719. Data->RemoteDirectory = ACurrentDirectory;
  5720. }
  5721. Data->Color = SessionColor;
  5722. }
  5723. //---------------------------------------------------------------------------
  5724. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5725. {
  5726. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5727. DebugAssert(Toolbar != NULL);
  5728. for (int i = 0; i < Toolbar->Items->Count; i++)
  5729. {
  5730. TTBXCustomDropDownItem * DropDownItem;
  5731. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5732. if (DropDownItem != NULL)
  5733. {
  5734. ToolbarItemResize(DropDownItem,
  5735. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5736. Toolbar->NonClientWidth);
  5737. break;
  5738. }
  5739. }
  5740. }
  5741. //---------------------------------------------------------------------------
  5742. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5743. {
  5744. Item->EditWidth = Width;
  5745. }
  5746. //---------------------------------------------------------------------------
  5747. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5748. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5749. {
  5750. for (int i = 0; i < Toolbar->Items->Count; i++)
  5751. {
  5752. TTBXCustomDropDownItem * DropDownItem;
  5753. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5754. if (DropDownItem != NULL)
  5755. {
  5756. ASize.x -= DropDownItem->EditWidth;
  5757. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5758. }
  5759. }
  5760. }
  5761. //---------------------------------------------------------------------------
  5762. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5763. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5764. {
  5765. if (WinConfiguration->DDWarnLackOfTempSpace)
  5766. {
  5767. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5768. if (!ADrive.IsEmpty())
  5769. {
  5770. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5771. DebugAssert(RequiredSpace >= 0);
  5772. __int64 RequiredWithReserve;
  5773. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5774. if (FreeSpace < RequiredWithReserve)
  5775. {
  5776. unsigned int Result;
  5777. TMessageParams Params(mpNeverAskAgainCheck);
  5778. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5779. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5780. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5781. if (Result == qaNeverAskAgain)
  5782. {
  5783. WinConfiguration->DDWarnLackOfTempSpace = false;
  5784. }
  5785. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5786. }
  5787. }
  5788. }
  5789. }
  5790. //---------------------------------------------------------------------------
  5791. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5792. {
  5793. DoOpenDirectoryDialog(odAddBookmark, Side);
  5794. }
  5795. //---------------------------------------------------------------------------
  5796. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5797. {
  5798. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5799. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5800. try
  5801. {
  5802. TCustomDirView * DView = DirView(Side);
  5803. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5804. {
  5805. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5806. }
  5807. }
  5808. catch (...)
  5809. {
  5810. delete VisitedDirectories;
  5811. throw;
  5812. }
  5813. return VisitedDirectories;
  5814. }
  5815. //---------------------------------------------------------------------------
  5816. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5817. TOpenDirectoryMode Mode, TOperationSide Side)
  5818. {
  5819. bool Continue = true;
  5820. if (Mode == odAddBookmark)
  5821. {
  5822. TMessageParams Params(mpNeverAskAgainCheck);
  5823. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5824. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5825. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5826. unsigned int Answer =
  5827. MessageDialog(Message,
  5828. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5829. if (Answer == qaNeverAskAgain)
  5830. {
  5831. Continue = true;
  5832. WinConfiguration->UseSharedBookmarks = true;
  5833. }
  5834. else if (Answer == qaOK)
  5835. {
  5836. Continue = true;
  5837. WinConfiguration->UseSharedBookmarks = false;
  5838. }
  5839. else
  5840. {
  5841. Continue = false;
  5842. }
  5843. }
  5844. if (Continue)
  5845. {
  5846. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5847. try
  5848. {
  5849. UnicodeString Name = DirView(Side)->PathName;
  5850. bool Repeat;
  5851. do
  5852. {
  5853. Repeat = false;
  5854. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5855. // do not allow switching to location profiles,
  5856. // if we are not connected
  5857. HasDirView[osLocal] && (Terminal != NULL)))
  5858. {
  5859. TWindowLock Lock(this);
  5860. if (!TryOpenDirectory(Side, Name))
  5861. {
  5862. Repeat = true;
  5863. Mode = odBrowse;
  5864. }
  5865. }
  5866. }
  5867. while (Repeat);
  5868. }
  5869. __finally
  5870. {
  5871. delete VisitedDirectories;
  5872. }
  5873. }
  5874. }
  5875. //---------------------------------------------------------------------------
  5876. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5877. {
  5878. DebugAssert(!FTerminal->CommandSessionOpened);
  5879. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5880. }
  5881. //---------------------------------------------------------------------------
  5882. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5883. {
  5884. bool Result = FTerminal->IsCapable[Capability] ||
  5885. FTerminal->CommandSessionOpened;
  5886. if (!Result)
  5887. {
  5888. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5889. if (!GUIConfiguration->ConfirmCommandSession)
  5890. {
  5891. Result = true;
  5892. }
  5893. else
  5894. {
  5895. TMessageParams Params(mpNeverAskAgainCheck);
  5896. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5897. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5898. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5899. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5900. if (Answer == qaNeverAskAgain)
  5901. {
  5902. GUIConfiguration->ConfirmCommandSession = false;
  5903. Result = true;
  5904. }
  5905. else if (Answer == qaOK)
  5906. {
  5907. Result = true;
  5908. }
  5909. }
  5910. if (Result)
  5911. {
  5912. Result = CommandSessionFallback();
  5913. }
  5914. }
  5915. return Result;
  5916. }
  5917. //---------------------------------------------------------------------------
  5918. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5919. {
  5920. if (EnsureCommandSessionFallback(fcAnyCommand))
  5921. {
  5922. DoConsoleDialog(Terminal, Command);
  5923. }
  5924. }
  5925. //---------------------------------------------------------------------------
  5926. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5927. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5928. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5929. {
  5930. if (!IsSideLocalBrowser(osRemote) &&
  5931. IsFileControl(DropSourceControl, osRemote) &&
  5932. (FDDExtMapFile != NULL))
  5933. {
  5934. Accept = true;
  5935. }
  5936. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5937. DebugAssert(FDDTargetControl != NULL);
  5938. }
  5939. //---------------------------------------------------------------------------
  5940. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5941. _di_IDataObject DataObj, int KeyState,
  5942. const TPoint & Point, int & Effect, bool & Accept)
  5943. {
  5944. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5945. }
  5946. //---------------------------------------------------------------------------
  5947. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5948. _di_IDataObject DataObj, int KeyState,
  5949. const TPoint & Point, int & Effect, bool & Accept)
  5950. {
  5951. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5952. }
  5953. //---------------------------------------------------------------------------
  5954. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5955. TObject * Sender)
  5956. {
  5957. DebugUsedParam(Sender);
  5958. DebugAssert(FDDTargetControl == Sender);
  5959. FDDTargetControl = NULL;
  5960. }
  5961. //---------------------------------------------------------------------------
  5962. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5963. {
  5964. FileControlDDDragLeave(SessionsPageControl);
  5965. }
  5966. //---------------------------------------------------------------------------
  5967. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5968. {
  5969. FileControlDDDragLeave(QueueView3);
  5970. }
  5971. //---------------------------------------------------------------------------
  5972. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5973. {
  5974. DebugAssert(!IsSideLocalBrowser(Side));
  5975. DebugUsedParam(Side);
  5976. bool Edit = false;
  5977. TRemoteFile * File = NULL;
  5978. UnicodeString FileName;
  5979. UnicodeString PointTo;
  5980. bool SymbolicLink = true;
  5981. if (RemoteDirView->ItemFocused)
  5982. {
  5983. DebugAssert(RemoteDirView->ItemFocused->Data);
  5984. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5985. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5986. if (Edit)
  5987. {
  5988. FileName = File->FileName;
  5989. PointTo = File->LinkTo;
  5990. }
  5991. else
  5992. {
  5993. PointTo = File->FileName;
  5994. }
  5995. }
  5996. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5997. Terminal->IsCapable[fcHardLink]))
  5998. {
  5999. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6000. if (Edit)
  6001. {
  6002. DebugAssert(File->FileName == FileName);
  6003. int Params = dfNoRecursive;
  6004. Terminal->ExceptionOnFail = true;
  6005. try
  6006. {
  6007. Terminal->DeleteFile(L"", File, &Params);
  6008. }
  6009. __finally
  6010. {
  6011. Terminal->ExceptionOnFail = false;
  6012. }
  6013. }
  6014. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6015. }
  6016. }
  6017. //---------------------------------------------------------------------------
  6018. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6019. {
  6020. return
  6021. (IsSideLocalBrowser(Side) ||
  6022. ((Terminal != NULL) &&
  6023. Terminal->ResolvingSymlinks &&
  6024. Terminal->IsCapable[fcSymbolicLink]));
  6025. }
  6026. //---------------------------------------------------------------------------
  6027. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6028. {
  6029. return
  6030. !IsSideLocalBrowser(FCurrentSide) &&
  6031. (RemoteDirView->ItemFocused != NULL) &&
  6032. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6033. Terminal->SessionData->ResolveSymlinks;
  6034. }
  6035. //---------------------------------------------------------------------------
  6036. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6037. {
  6038. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6039. FForceExecution = true;
  6040. try
  6041. {
  6042. DirView(osCurrent)->ExecuteCurrentFile();
  6043. }
  6044. __finally
  6045. {
  6046. FForceExecution = false;
  6047. }
  6048. }
  6049. //---------------------------------------------------------------------------
  6050. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6051. {
  6052. TEditorData ExternalEditor;
  6053. ExternalEditor.Editor = edExternal;
  6054. bool Remember = false;
  6055. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6056. {
  6057. if (Remember)
  6058. {
  6059. TEditorList * EditorList = new TEditorList;
  6060. try
  6061. {
  6062. *EditorList = *WinConfiguration->EditorList;
  6063. bool Found = false;
  6064. int i = 0;
  6065. while (!Found && (i < EditorList->Count))
  6066. {
  6067. const TEditorPreferences * Editor = EditorList->Editors[i];
  6068. if ((Editor->Data->Editor == edExternal) &&
  6069. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6070. {
  6071. Found = true;
  6072. }
  6073. i++;
  6074. }
  6075. if (!Found)
  6076. {
  6077. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6078. WinConfiguration->EditorList = EditorList;
  6079. }
  6080. }
  6081. __finally
  6082. {
  6083. delete EditorList;
  6084. }
  6085. }
  6086. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6087. }
  6088. }
  6089. //---------------------------------------------------------------------------
  6090. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6091. {
  6092. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6093. if (FFileFindTerminal == Sender)
  6094. {
  6095. FFileFindTerminal = NULL;
  6096. HideFileFindDialog();
  6097. }
  6098. if (FClipboardTerminal == Sender)
  6099. {
  6100. ClipboardClear(); // implies ClipboardStop
  6101. }
  6102. }
  6103. //---------------------------------------------------------------------------
  6104. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6105. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6106. {
  6107. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6108. DebugAssert(Terminal != NULL);
  6109. if (Data->Terminal == Terminal)
  6110. {
  6111. Data->Terminal = NULL;
  6112. }
  6113. }
  6114. //---------------------------------------------------------------------------
  6115. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6116. {
  6117. UpdateControls();
  6118. SessionColor = TColor(0);
  6119. UpdateRemotePathComboBox(false);
  6120. NeedSession(false);
  6121. }
  6122. //---------------------------------------------------------------------------
  6123. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6124. {
  6125. try
  6126. {
  6127. // Cache, as the login dialog can change its value
  6128. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6129. try
  6130. {
  6131. if (ShowLogin)
  6132. {
  6133. bool ReloadSessions = !Startup;
  6134. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6135. }
  6136. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6137. // This detects if workspace was saved the last time the main widow was closed
  6138. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6139. {
  6140. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6141. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6142. }
  6143. }
  6144. __finally
  6145. {
  6146. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6147. // (so there was no chance for the user to open any session yet)
  6148. if ((ShowLogin || !Startup) &&
  6149. !WinConfiguration->KeepOpenWhenNoSession &&
  6150. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6151. {
  6152. TerminateApplication();
  6153. }
  6154. }
  6155. }
  6156. catch (EAbort &)
  6157. {
  6158. // swallow
  6159. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6160. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6161. }
  6162. }
  6163. //---------------------------------------------------------------------------
  6164. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6165. {
  6166. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6167. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6168. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6169. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6170. try
  6171. {
  6172. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6173. // Clear the design time unthemed tab
  6174. ((SessionsPageControl->PageCount > 0) &&
  6175. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6176. {
  6177. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6178. }
  6179. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6180. {
  6181. TThemeTabSheet * TabSheet;
  6182. if (Index >= SessionsPageControl->PageCount)
  6183. {
  6184. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6185. TabSheet->PageControl = SessionsPageControl;
  6186. }
  6187. else
  6188. {
  6189. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6190. }
  6191. bool IsSessionTab = (Index < TerminalList->Count);
  6192. if (IsSessionTab)
  6193. {
  6194. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6195. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6196. UpdateSessionTab(TabSheet);
  6197. }
  6198. else
  6199. {
  6200. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6201. TabSheet->Tag = 0; // not really needed
  6202. TabSheet->Shadowed = false;
  6203. // 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)
  6204. UpdateNewSessionTab();
  6205. }
  6206. TabSheet->ShowCloseButton = IsSessionTab;
  6207. }
  6208. }
  6209. __finally
  6210. {
  6211. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6212. }
  6213. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6214. }
  6215. //---------------------------------------------------------------------------
  6216. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6217. {
  6218. DoTerminalListChanged();
  6219. }
  6220. //---------------------------------------------------------------------------
  6221. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6222. {
  6223. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6224. DebugAssert(TabSheet->ImageIndex == 0);
  6225. TabSheet->Caption =
  6226. WinConfiguration->SelectiveToolbarText ?
  6227. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6228. UnicodeString();
  6229. }
  6230. //---------------------------------------------------------------------------
  6231. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6232. {
  6233. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6234. }
  6235. //---------------------------------------------------------------------------
  6236. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6237. {
  6238. if (DebugAlwaysTrue(TabSheet != NULL))
  6239. {
  6240. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6241. if (DebugAlwaysTrue(ATerminal != NULL))
  6242. {
  6243. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6244. TabSheet->ImageIndex = AddSessionColor(Color);
  6245. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6246. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6247. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6248. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6249. {
  6250. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6251. }
  6252. }
  6253. }
  6254. }
  6255. //---------------------------------------------------------------------------
  6256. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6257. {
  6258. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6259. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6260. bool Result = (Terminal != NULL);
  6261. if (Result)
  6262. {
  6263. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6264. }
  6265. else
  6266. {
  6267. try
  6268. {
  6269. NewSession(false);
  6270. }
  6271. __finally
  6272. {
  6273. DoTerminalListChanged();
  6274. }
  6275. FSessionsPageControlNewSessionTime = Now();
  6276. }
  6277. return Result;
  6278. }
  6279. //---------------------------------------------------------------------------
  6280. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6281. {
  6282. SessionTabSwitched();
  6283. }
  6284. //---------------------------------------------------------------------------
  6285. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6286. {
  6287. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6288. DebugAssert(TransferList != NULL);
  6289. UnicodeString Name;
  6290. if (TransferList->ItemIndex <= 0)
  6291. {
  6292. Name = L"";
  6293. }
  6294. else
  6295. {
  6296. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6297. }
  6298. if (FCopyParamAutoSelected.IsEmpty())
  6299. {
  6300. // if previous preset was not autoselected, make new preset the "default"
  6301. FCopyParamDefault = Name;
  6302. }
  6303. GUIConfiguration->CopyParamCurrent = Name;
  6304. }
  6305. //---------------------------------------------------------------------------
  6306. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6307. {
  6308. // sanity check
  6309. bool ExistingPreset =
  6310. (FTransferListHoverIndex >= 0) &&
  6311. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6312. DebugAssert(ExistingPreset);
  6313. if (ExistingPreset)
  6314. {
  6315. HDC DC = GetDC(0);
  6316. TCanvas * Canvas = new TCanvas();
  6317. try
  6318. {
  6319. Canvas->Handle = DC;
  6320. Canvas->Font = GetToolbarFont(this);
  6321. UnicodeString Name;
  6322. if (FTransferListHoverIndex == 0)
  6323. {
  6324. Name = L"";
  6325. }
  6326. else
  6327. {
  6328. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6329. }
  6330. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6331. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6332. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6333. static_cast<TObject*>(GetComponent(fcTransferList)));
  6334. UnicodeString InfoStr =
  6335. GUIConfiguration->CopyParamPreset[Name].
  6336. GetInfoStr(L"; ",
  6337. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6338. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6339. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6340. {
  6341. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6342. {
  6343. InfoStr.SetLength(InfoStr.Length() - 1);
  6344. }
  6345. InfoStr += Ellipsis;
  6346. }
  6347. // UpdateCaption does not cause invalidation of whole submenu, while
  6348. // setting Caption property does.
  6349. // also it probably does not resize the label, even if necessary
  6350. // (we do not want that anyway)
  6351. TransferLabel->UpdateCaption(InfoStr);
  6352. }
  6353. __finally
  6354. {
  6355. Canvas->Handle = NULL;
  6356. ReleaseDC(0, DC);
  6357. delete Canvas;
  6358. }
  6359. }
  6360. }
  6361. //---------------------------------------------------------------------------
  6362. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6363. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6364. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6365. {
  6366. if (FTransferListHoverIndex != AHoverIndex)
  6367. {
  6368. FTransferListHoverIndex = AHoverIndex;
  6369. UpdateTransferLabel();
  6370. }
  6371. }
  6372. //---------------------------------------------------------------------------
  6373. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6374. {
  6375. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6376. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6377. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6378. {
  6379. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6380. {
  6381. OpenDirectory(GetSide(osCurrent));
  6382. Message.Result = 1;
  6383. }
  6384. else
  6385. {
  6386. TForm::Dispatch(&Message);
  6387. }
  6388. }
  6389. else
  6390. {
  6391. TForm::Dispatch(&Message);
  6392. }
  6393. }
  6394. //---------------------------------------------------------------------------
  6395. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6396. {
  6397. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6398. if ((FIgnoreNextDialogChar != 0) &&
  6399. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6400. {
  6401. Message.Result = 1;
  6402. }
  6403. else
  6404. {
  6405. TForm::Dispatch(&Message);
  6406. }
  6407. FIgnoreNextDialogChar = 0;
  6408. }
  6409. //---------------------------------------------------------------------------
  6410. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6411. {
  6412. // The four low-order bits are used internally by Windows
  6413. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6414. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6415. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6416. // 61730 - restore thru double click - undocumented
  6417. // 61490 - maximize thru double click - undocumented
  6418. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6419. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6420. {
  6421. SysResizing(Cmd);
  6422. }
  6423. TForm::Dispatch(&Message);
  6424. }
  6425. //---------------------------------------------------------------------------
  6426. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6427. {
  6428. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6429. // Also there no point returning TRUE as we are not able to
  6430. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6431. // Hence the process termination might be safer :)
  6432. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6433. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6434. {
  6435. Message.Result = FALSE;
  6436. }
  6437. else
  6438. {
  6439. Message.Result = TRUE;
  6440. }
  6441. // Do not call default handling as that triggers OnCloseQuery,
  6442. // where our implementation will popup confirmation dialogs, what we do not want,
  6443. // as per Vista guidelines:
  6444. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6445. }
  6446. //---------------------------------------------------------------------------
  6447. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6448. {
  6449. if (Message.EndSession && IsApplicationMinimized())
  6450. {
  6451. // WORKAROUND
  6452. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6453. // when it receives WM_ENDSESSION,
  6454. // but that sometimes (particularly when application is minimized) cause crashes.
  6455. // Crash popups message that blocks log off.
  6456. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6457. // so we call ExitProcess() immediately, not even trying to cleanup.
  6458. // It still causes beep, so there's likely some popup, but it does not block
  6459. // log off.
  6460. ExitProcess(0);
  6461. }
  6462. TForm::Dispatch(&Message);
  6463. }
  6464. #ifdef _DEBUG
  6465. //---------------------------------------------------------------------------
  6466. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6467. {
  6468. TForm::Dispatch(&Message);
  6469. }
  6470. #endif
  6471. //---------------------------------------------------------------------------
  6472. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6473. {
  6474. }
  6475. //---------------------------------------------------------------------------
  6476. void __fastcall TCustomScpExplorerForm::DoShow()
  6477. {
  6478. // only now are the controls resized finally, so the size constraints
  6479. // will not conflict with possibly very small window size
  6480. RestoreFormParams();
  6481. // This indicates that the form was rescaled after parameters were loaded and
  6482. // as most probably the parameters were actually saved with the current form scaling
  6483. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6484. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6485. // and no rounding problems will occur.
  6486. // See also RestoreForm.
  6487. // (we should be safe to call RestoreParams unconditionally)
  6488. if (PixelsPerInch != Screen->PixelsPerInch)
  6489. {
  6490. RestoreParams();
  6491. }
  6492. FixControlsPlacement();
  6493. if (Position == poDefaultPosOnly)
  6494. {
  6495. CutFormToDesktop(this);
  6496. }
  6497. TForm::DoShow();
  6498. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6499. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6500. if (Terminal == NULL)
  6501. {
  6502. StartingDisconnected();
  6503. }
  6504. UpdatePixelsPerInchMainWindowCounter();
  6505. FShowing = true;
  6506. }
  6507. //---------------------------------------------------------------------------
  6508. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6509. {
  6510. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6511. }
  6512. //---------------------------------------------------------------------------
  6513. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6514. {
  6515. DoTerminalListChanged();
  6516. }
  6517. //---------------------------------------------------------------------------
  6518. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6519. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6520. TNotifyEvent OnBalloonClick, TObject * UserData)
  6521. {
  6522. bool Do;
  6523. UnicodeString Message;
  6524. if (E == NULL)
  6525. {
  6526. Message = Str;
  6527. Do = true;
  6528. }
  6529. else
  6530. {
  6531. Do = ExceptionMessage(E, Message);
  6532. }
  6533. if (Do && WinConfiguration->BalloonNotifications)
  6534. {
  6535. UnicodeString Title;
  6536. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6537. {
  6538. Message = TrimLeft(Message);
  6539. }
  6540. else
  6541. {
  6542. Message = UnformatMessage(Message);
  6543. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6544. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6545. Title = LoadResourceString(Captions[Type]);
  6546. if (Terminal != NULL)
  6547. {
  6548. Title = FORMAT(L"%s - %s",
  6549. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6550. }
  6551. }
  6552. if (Seconds == 0)
  6553. {
  6554. Seconds = WinConfiguration->NotificationsTimeout;
  6555. }
  6556. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6557. }
  6558. }
  6559. //---------------------------------------------------------------------------
  6560. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6561. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6562. UnicodeString HelpKeyword, const TMessageParams * Params,
  6563. TTerminal * Terminal)
  6564. {
  6565. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6566. (FOnFeedSynchronizeError != NULL)) &&
  6567. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6568. {
  6569. if (FOnFeedSynchronizeError != NULL)
  6570. {
  6571. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6572. }
  6573. else
  6574. {
  6575. DebugAssert(FErrorList != NULL);
  6576. TStringList * MoreMessagesCopy = NULL;
  6577. if (MoreMessages)
  6578. {
  6579. MoreMessagesCopy = new TStringList();
  6580. MoreMessagesCopy->Assign(MoreMessages);
  6581. }
  6582. FErrorList->AddObject(Message, MoreMessagesCopy);
  6583. }
  6584. PopupTrayBalloon(Terminal, Message, Type);
  6585. return ContinueAnswer(Answers);
  6586. }
  6587. else
  6588. {
  6589. bool UseBalloon = FTrayIcon->Visible;
  6590. if (UseBalloon)
  6591. {
  6592. PopupTrayBalloon(Terminal, Message, Type);
  6593. }
  6594. unsigned int Result;
  6595. try
  6596. {
  6597. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6598. }
  6599. __finally
  6600. {
  6601. // cancel only balloon we popped up, otherwise we may cancel notification
  6602. // balloon that was there before the message dialog
  6603. // (such as "dd confirmation opt in balloon)
  6604. if (UseBalloon)
  6605. {
  6606. FTrayIcon->CancelBalloon();
  6607. }
  6608. }
  6609. return Result;
  6610. }
  6611. }
  6612. //---------------------------------------------------------------------------
  6613. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6614. TTerminal * Terminal, Exception * E)
  6615. {
  6616. if (FTrayIcon->Visible)
  6617. {
  6618. PopupTrayBalloon(Terminal, L"", qtError, E);
  6619. }
  6620. // particularly prevent opening new session from jump list,
  6621. // while exception is shown
  6622. NonVisualDataModule->StartBusy();
  6623. try
  6624. {
  6625. ShowExtendedExceptionEx(Terminal, E);
  6626. }
  6627. __finally
  6628. {
  6629. NonVisualDataModule->EndBusy();
  6630. FTrayIcon->CancelBalloon();
  6631. }
  6632. }
  6633. //---------------------------------------------------------------------------
  6634. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6635. {
  6636. if (Terminal->Active)
  6637. {
  6638. InitStatusBar();
  6639. }
  6640. // cannot rely on active page being page for active terminal,
  6641. // as it can happen that active page is the "new session" page
  6642. // (e.g. when reconnecting active terminal, while login dialog
  6643. // invoked from "new session" page is modal)
  6644. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6645. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6646. }
  6647. //---------------------------------------------------------------------------
  6648. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6649. TTerminal * Terminal, Exception * E)
  6650. {
  6651. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6652. if (!Terminal->Active)
  6653. {
  6654. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6655. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6656. {
  6657. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6658. }
  6659. }
  6660. }
  6661. //---------------------------------------------------------------------------
  6662. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6663. UnicodeString Message, TQueryType Type,
  6664. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6665. {
  6666. if ((E == NULL) ||
  6667. ExceptionMessage(E, Message))
  6668. {
  6669. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6670. if (Important)
  6671. {
  6672. Seconds *= 5;
  6673. }
  6674. UnicodeString NoteMessage(UnformatMessage(Message));
  6675. if (Terminal != NULL)
  6676. {
  6677. NoteMessage = FORMAT(L"%s: %s",
  6678. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6679. }
  6680. if (WinConfiguration->BalloonNotifications)
  6681. {
  6682. AddNote(NoteMessage);
  6683. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6684. }
  6685. else
  6686. {
  6687. FlashOnBackground();
  6688. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6689. }
  6690. }
  6691. }
  6692. //---------------------------------------------------------------------------
  6693. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6694. {
  6695. RestoreApp();
  6696. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6697. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6698. !NonVisualDataModule->Busy)
  6699. {
  6700. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6701. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6702. if (!ComponentVisible[fcQueueView])
  6703. {
  6704. ToggleQueueVisibility();
  6705. GoToQueue();
  6706. }
  6707. }
  6708. }
  6709. //---------------------------------------------------------------------------
  6710. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6711. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6712. {
  6713. UnicodeString Message;
  6714. TNotifyEvent OnClick = NULL;
  6715. TObject * UserData = NULL;
  6716. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6717. switch (Event)
  6718. {
  6719. case qeEmptyButMonitored:
  6720. if ((ATerminal != Terminal) || QueueInvisible)
  6721. {
  6722. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6723. OnClick = QueueEmptyNoteClicked;
  6724. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6725. TerminalNoteData->Terminal = ATerminal;
  6726. UserData = TerminalNoteData;
  6727. }
  6728. break;
  6729. case qeEmpty:
  6730. OperationComplete(ATerminal->QueueOperationStart);
  6731. break;
  6732. case qePendingUserAction:
  6733. if ((ATerminal != Terminal) ||
  6734. (QueueInvisible && !IsQueueAutoPopup()))
  6735. {
  6736. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6737. }
  6738. break;
  6739. default:
  6740. DebugFail();
  6741. }
  6742. if (!Message.IsEmpty())
  6743. {
  6744. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6745. }
  6746. }
  6747. //---------------------------------------------------------------------------
  6748. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6749. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6750. {
  6751. if (FDDExtMapFile != NULL)
  6752. {
  6753. CloseHandle(FDDExtMapFile);
  6754. FDDExtMapFile = NULL;
  6755. }
  6756. if (WinConfiguration->DDFakeFile)
  6757. {
  6758. DDFakeFileInitDrag(FileList, Created);
  6759. }
  6760. }
  6761. //---------------------------------------------------------------------------
  6762. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6763. {
  6764. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6765. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6766. {
  6767. FFakeFileDropTarget = FileName;
  6768. }
  6769. }
  6770. //---------------------------------------------------------------------------
  6771. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6772. {
  6773. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6774. if (!ForceDirectories(ApiPath(Result)))
  6775. {
  6776. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6777. }
  6778. FileSetAttr(ApiPath(Result), faHidden);
  6779. return Result;
  6780. }
  6781. //---------------------------------------------------------------------------
  6782. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6783. bool & Created)
  6784. {
  6785. FFakeFileDropTarget = UnicodeString();
  6786. FDragFakeDirectory = CreateFakeTransferDirectory();
  6787. FileList->AddItem(NULL, FDragFakeDirectory);
  6788. Created = true;
  6789. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6790. {
  6791. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6792. }
  6793. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6794. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6795. {
  6796. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6797. TDragExtCommStruct* CommStruct;
  6798. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6799. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6800. DebugAssert(CommStruct != NULL);
  6801. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6802. CommStruct->Dragging = true;
  6803. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6804. LENOF(CommStruct->DropDest));
  6805. NULL_TERMINATE(CommStruct->DropDest);
  6806. UnmapViewOfFile(CommStruct);
  6807. }
  6808. }
  6809. //---------------------------------------------------------------------------
  6810. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6811. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6812. {
  6813. bool Result;
  6814. DebugAssert(!IsLocalBrowserMode());
  6815. if (Sender == RemoteDirView)
  6816. {
  6817. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6818. }
  6819. else
  6820. {
  6821. DebugAssert(Sender == RemoteDriveView);
  6822. TStrings * FileList = RemoteDriveView->DragFileList();
  6823. try
  6824. {
  6825. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6826. }
  6827. __finally
  6828. {
  6829. delete FileList;
  6830. }
  6831. }
  6832. if (FDDTargetControl == RemoteDriveView)
  6833. {
  6834. RemoteDriveView->UpdateDropTarget();
  6835. }
  6836. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6837. // or from tree view to dir view.
  6838. // foMove happens when file/dir is dragged from remote tree view outside of
  6839. // application via dragex
  6840. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6841. (DropSourceControl == RemoteDriveView))
  6842. {
  6843. RemoteDriveView->UpdateDropSource();
  6844. }
  6845. return Result;
  6846. }
  6847. //---------------------------------------------------------------------------
  6848. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6849. {
  6850. DebugAssert(!IsLocalBrowserMode());
  6851. // This also handles drops of remote files to queue.
  6852. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6853. SAFE_DESTROY(FDDFileList);
  6854. if (!FFakeFileDropTarget.IsEmpty())
  6855. {
  6856. RemoveDir(ApiPath(FFakeFileDropTarget));
  6857. }
  6858. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6859. {
  6860. try
  6861. {
  6862. TDragResult DDResult = (Sender == RemoteDirView) ?
  6863. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6864. // Focus is moved to the target application,
  6865. // but as we are going to present the UI, we need to steal the focus back.
  6866. // This most likely won't work though (windows does not allow application
  6867. // to steal focus most of the time)
  6868. Application->BringToFront();
  6869. // On older version of Windows we never got drMove here, see also comment below.
  6870. // On Windows 10, we get the "move".
  6871. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6872. {
  6873. UnicodeString TargetDirectory;
  6874. TFileOperation Operation;
  6875. // drInvalid may mean drMove, see comment below
  6876. switch (DDResult)
  6877. {
  6878. case drCopy:
  6879. Operation = foCopy;
  6880. break;
  6881. case drMove:
  6882. Operation = foMove;
  6883. break;
  6884. default:
  6885. DebugFail();
  6886. case drInvalid:
  6887. // prefer "copy" for safety
  6888. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6889. break;
  6890. }
  6891. TTransferOperationParam Param;
  6892. UnicodeString CounterName;
  6893. bool ForceQueue;
  6894. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6895. {
  6896. // we get drInvalid both if d&d was intercepted by ddext,
  6897. // and when users drops on no-drop location.
  6898. // we tell the difference by existence of response from ddext,
  6899. // so we ignore absence of response in this case
  6900. if (DDResult != drInvalid)
  6901. {
  6902. // here we know that the extension is installed,
  6903. // as it is checked as soon as drag&drop starts from
  6904. // RemoteFileControlDDCreateDragFileList
  6905. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6906. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6907. {
  6908. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6909. }
  6910. else
  6911. {
  6912. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6913. }
  6914. }
  6915. }
  6916. else
  6917. {
  6918. // download using fake file
  6919. Param.Temp = false;
  6920. Param.DragDrop = true;
  6921. if (ForceQueue)
  6922. {
  6923. Param.Queue = asOn;
  6924. }
  6925. if (Sender == RemoteDirView)
  6926. {
  6927. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6928. }
  6929. if (RemoteFileControlFileOperation(Sender, Operation,
  6930. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6931. {
  6932. Configuration->Usage->Inc(CounterName);
  6933. }
  6934. }
  6935. }
  6936. }
  6937. __finally
  6938. {
  6939. CloseHandle(FDDExtMapFile);
  6940. FDDExtMapFile = NULL;
  6941. delete FDragFakeMonitors;
  6942. FDragFakeMonitors = NULL;
  6943. RemoveDir(ApiPath(FDragFakeDirectory));
  6944. FDragFakeDirectory = L"";
  6945. FFakeFileDropTarget = UnicodeString();
  6946. }
  6947. }
  6948. if (FDragDropSshTerminate.get() != NULL)
  6949. {
  6950. FDragDropSshTerminate->Rethrow();
  6951. }
  6952. }
  6953. //---------------------------------------------------------------------------
  6954. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6955. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6956. {
  6957. // Remember drop effect so we know (when user drops files), if we copy or move
  6958. FLastDropEffect = dwEffect;
  6959. }
  6960. //---------------------------------------------------------------------------
  6961. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6962. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6963. {
  6964. bool Result;
  6965. if (FDDTargetControl == QueueView3)
  6966. {
  6967. Directory = DefaultDownloadTargetDirectory();
  6968. Result = true;
  6969. CounterName = L"DownloadsDragDropQueue";
  6970. ForceQueue = true;
  6971. }
  6972. else if (!FFakeFileDropTarget.IsEmpty())
  6973. {
  6974. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6975. Result = true;
  6976. ForceQueue = false;
  6977. CounterName = L"DownloadsDragDropFakeFile";
  6978. }
  6979. else
  6980. {
  6981. ForceQueue = false;
  6982. Enabled = false;
  6983. try
  6984. {
  6985. int Timer = 0;
  6986. Result = false;
  6987. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6988. {
  6989. {
  6990. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6991. TDragExtCommStruct* CommStruct;
  6992. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6993. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6994. DebugAssert(CommStruct != NULL);
  6995. Result = !CommStruct->Dragging;
  6996. if (Result)
  6997. {
  6998. Directory = ExtractFilePath(CommStruct->DropDest);
  6999. CounterName = L"DownloadsDragDropExternalExt";
  7000. }
  7001. UnmapViewOfFile(CommStruct);
  7002. }
  7003. if (!Result)
  7004. {
  7005. Sleep(50);
  7006. Timer += 50;
  7007. Application->ProcessMessages();
  7008. }
  7009. }
  7010. }
  7011. __finally
  7012. {
  7013. Enabled = true;
  7014. }
  7015. }
  7016. return Result;
  7017. }
  7018. //---------------------------------------------------------------------------
  7019. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7020. const UnicodeString TempDir, int SecDelay)
  7021. {
  7022. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  7023. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7024. if (FDelayedDeletionTimer == NULL)
  7025. {
  7026. DebugAssert(HandleAllocated());
  7027. FDelayedDeletionTimer = new TTimer(this);
  7028. FDelayedDeletionTimer->Interval = 10000;
  7029. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7030. }
  7031. else
  7032. {
  7033. FDelayedDeletionTimer->Enabled = true;
  7034. }
  7035. }
  7036. //---------------------------------------------------------------------------
  7037. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7038. {
  7039. DebugAssert(FDelayedDeletionList != NULL);
  7040. TDateTime N = Now();
  7041. TDateTime Alert;
  7042. UnicodeString Directory;
  7043. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7044. {
  7045. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7046. if ((N >= Alert) || (Sender == NULL))
  7047. {
  7048. Directory = FDelayedDeletionList->Strings[Index];
  7049. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7050. {
  7051. FDelayedDeletionList->Delete(Index);
  7052. }
  7053. }
  7054. }
  7055. if (FDelayedDeletionList->Count == 0)
  7056. {
  7057. FDelayedDeletionTimer->Enabled = false;
  7058. }
  7059. }
  7060. //---------------------------------------------------------------------------
  7061. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7062. {
  7063. DebugAssert(!IsLocalBrowserMode());
  7064. if (IsFileControl(FDDTargetControl, osRemote) ||
  7065. (FDDTargetControl == SessionsPageControl))
  7066. {
  7067. TTerminal * TargetTerminal = NULL;
  7068. TFileOperation Operation = foNone;
  7069. if (FDDTargetControl == SessionsPageControl)
  7070. {
  7071. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7072. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7073. // do not allow dropping on the "+" tab
  7074. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7075. if (TargetTerminal != NULL)
  7076. {
  7077. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7078. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7079. {
  7080. Operation = foRemoteMove;
  7081. }
  7082. else
  7083. {
  7084. Operation = foRemoteCopy;
  7085. }
  7086. }
  7087. }
  7088. else
  7089. {
  7090. if (FLastDropEffect == DROPEFFECT_MOVE)
  7091. {
  7092. Operation = foRemoteMove;
  7093. }
  7094. else if (FLastDropEffect == DROPEFFECT_COPY)
  7095. {
  7096. Operation = foRemoteCopy;
  7097. }
  7098. }
  7099. if (Operation != foNone)
  7100. {
  7101. RemoteFileControlFileOperation(DropSourceControl,
  7102. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7103. }
  7104. // abort drag&drop
  7105. Abort();
  7106. }
  7107. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7108. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7109. (FDDTargetControl != QueueView3))
  7110. {
  7111. DebugAssert(!FDragTempDir.IsEmpty());
  7112. TTransferType Type;
  7113. UnicodeString TempDir = FDragTempDir;
  7114. // We clear FDragTempDir before calling
  7115. // just in case it fail (raises exception)
  7116. FDragTempDir = L"";
  7117. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7118. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7119. // empty directory parameter means temp directory -> don't display it!
  7120. UnicodeString TargetDir = L"";
  7121. int Options = 0;
  7122. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7123. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7124. {
  7125. Abort();
  7126. }
  7127. // TargetDir is set when dropped on local file control
  7128. // (this was workaround for legacy dirview event handling, now it should be
  7129. // made prettier)
  7130. if (TargetDir.IsEmpty())
  7131. {
  7132. TargetDir = TempDir;
  7133. if (ForceDirectories(ApiPath(TargetDir)))
  7134. {
  7135. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7136. FPendingTempSpaceWarn = true;
  7137. try
  7138. {
  7139. FDragDropOperation = true;
  7140. int Params = cpTemporary |
  7141. (Type == ttMove ? cpDelete : 0);
  7142. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7143. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7144. }
  7145. __finally
  7146. {
  7147. FDragDropOperation = false;
  7148. FPendingTempSpaceWarn = false;
  7149. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7150. }
  7151. }
  7152. else
  7153. {
  7154. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7155. }
  7156. }
  7157. }
  7158. }
  7159. //---------------------------------------------------------------------------
  7160. void __fastcall TCustomScpExplorerForm::DDDownload(
  7161. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7162. {
  7163. DebugAssert(!IsLocalBrowserMode());
  7164. TAutoBatch AutoBatch(this);
  7165. UpdateCopyParamCounters(*CopyParam);
  7166. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7167. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7168. {
  7169. RemoteDriveView->UpdateDropSource();
  7170. }
  7171. }
  7172. //---------------------------------------------------------------------------
  7173. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7174. {
  7175. public:
  7176. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7177. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7178. {
  7179. }
  7180. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7181. {
  7182. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7183. TDataObjectFilesEx::AllowData(FormatEtc);
  7184. }
  7185. };
  7186. //---------------------------------------------------------------------------
  7187. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7188. TObject * Sender, TDataObject *& DataObject)
  7189. {
  7190. if (FDDExtMapFile != NULL)
  7191. {
  7192. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7193. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7194. {
  7195. Abort();
  7196. }
  7197. if (FileList->Count > 0)
  7198. {
  7199. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7200. if (!FilesObject->IsValid(true, true))
  7201. {
  7202. FilesObject->_Release();
  7203. }
  7204. else
  7205. {
  7206. DataObject = FilesObject;
  7207. }
  7208. }
  7209. }
  7210. }
  7211. //---------------------------------------------------------------------------
  7212. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7213. {
  7214. DebugFail();
  7215. }
  7216. //---------------------------------------------------------------------------
  7217. void __fastcall TCustomScpExplorerForm::GoToTree()
  7218. {
  7219. ComponentVisible[fcRemoteTree] = true;
  7220. DriveView(osRemote)->SetFocus();
  7221. }
  7222. //---------------------------------------------------------------------------
  7223. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7224. TPanelExport Export)
  7225. {
  7226. TCustomDirView * DirView = this->DirView(Side);
  7227. std::unique_ptr<TStrings> ExportData(new TStringList());
  7228. switch (Export)
  7229. {
  7230. case pePath:
  7231. ExportData->Add(DirView->PathName);
  7232. break;
  7233. case peFileList:
  7234. case peFullFileList:
  7235. {
  7236. bool FullPath = (Export == peFullFileList);
  7237. DirView->CreateFileList(false, FullPath, ExportData.get());
  7238. for (int Index = 0; Index < ExportData->Count; Index++)
  7239. {
  7240. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7241. {
  7242. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7243. }
  7244. }
  7245. }
  7246. break;
  7247. default:
  7248. DebugFail();
  7249. }
  7250. return ExportData.release();
  7251. }
  7252. //---------------------------------------------------------------------------
  7253. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7254. TPanelExport Export, TPanelExportDestination Destination)
  7255. {
  7256. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7257. PanelExportStore(Side, Export, Destination, ExportData.get());
  7258. }
  7259. //---------------------------------------------------------------------------
  7260. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7261. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7262. TStrings * ExportData)
  7263. {
  7264. if (Destination == pedClipboard)
  7265. {
  7266. TInstantOperationVisualizer Visualizer;
  7267. CopyToClipboard(ExportData);
  7268. }
  7269. else
  7270. {
  7271. DebugFail();
  7272. }
  7273. }
  7274. //---------------------------------------------------------------------------
  7275. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7276. {
  7277. TCustomDirView * DirView = this->DirView(Side);
  7278. UnicodeString Mask = DirView->Mask;
  7279. if (DoFilterMaskDialog(DirView, Mask))
  7280. {
  7281. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7282. Configuration->Usage->Inc(L"Filters");
  7283. }
  7284. }
  7285. //---------------------------------------------------------------------------
  7286. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7287. {
  7288. return FQueueController->DefaultOperation();
  7289. }
  7290. //---------------------------------------------------------------------------
  7291. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7292. TQueueOperation Operation, void ** Param)
  7293. {
  7294. switch (Operation)
  7295. {
  7296. case qoPreferences:
  7297. return true;
  7298. case qoGoTo:
  7299. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7300. case qoOnceEmpty:
  7301. return !FQueueController->Empty;
  7302. default:
  7303. return FQueueController->AllowOperation(Operation, Param);
  7304. }
  7305. }
  7306. //---------------------------------------------------------------------------
  7307. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7308. {
  7309. if (DebugAlwaysTrue(QueueView3->Visible))
  7310. {
  7311. QueueView3->SetFocus();
  7312. }
  7313. }
  7314. //---------------------------------------------------------------------------
  7315. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7316. TQueueOperation Operation, void * Param)
  7317. {
  7318. if (Operation == qoGoTo)
  7319. {
  7320. GoToQueue();
  7321. }
  7322. else if (Operation == qoPreferences)
  7323. {
  7324. PreferencesDialog(pmQueue);
  7325. }
  7326. else
  7327. {
  7328. FQueueController->ExecuteOperation(Operation, Param);
  7329. }
  7330. }
  7331. //---------------------------------------------------------------------------
  7332. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7333. {
  7334. return (Queue != NULL) && Queue->Enabled;
  7335. }
  7336. //---------------------------------------------------------------------------
  7337. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7338. {
  7339. DebugAssert(Queue != NULL);
  7340. if (Queue != NULL)
  7341. {
  7342. Queue->Enabled = !Queue->Enabled;
  7343. }
  7344. }
  7345. //---------------------------------------------------------------------------
  7346. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7347. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7348. {
  7349. FQueueActedItem = QueueView3->ItemFocused;
  7350. }
  7351. //---------------------------------------------------------------------------
  7352. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7353. {
  7354. return TopDock->Height + QueueSplitter->Height;
  7355. }
  7356. //---------------------------------------------------------------------------
  7357. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7358. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7359. {
  7360. // when queue is hidden by double-clicking splitter, stray attempt to
  7361. // resize the panel with strange value arrives, make sure it is ignored
  7362. if (ComponentVisible[fcQueueView])
  7363. {
  7364. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7365. RemotePanel->Constraints->MinHeight;
  7366. if (NewSize > HeightLimit)
  7367. {
  7368. NewSize = HeightLimit;
  7369. }
  7370. }
  7371. else
  7372. {
  7373. Accept = false;
  7374. }
  7375. }
  7376. //---------------------------------------------------------------------------
  7377. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7378. TDragObject *& /*DragObject*/)
  7379. {
  7380. FQueueActedItem = QueueView3->ItemFocused;
  7381. }
  7382. //---------------------------------------------------------------------------
  7383. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7384. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7385. {
  7386. Accept = true;
  7387. if (Source == QueueView3)
  7388. {
  7389. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7390. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7391. if (Accept)
  7392. {
  7393. TQueueItemProxy * QueueItem;
  7394. TQueueItemProxy * DestQueueItem;
  7395. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7396. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7397. Accept = (QueueItem != DestQueueItem) &&
  7398. (QueueItem->Status == TQueueItem::qsPending) &&
  7399. (DestQueueItem->Status == TQueueItem::qsPending);
  7400. }
  7401. }
  7402. }
  7403. //---------------------------------------------------------------------------
  7404. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7405. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7406. {
  7407. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7408. {
  7409. TQueueItemProxy * QueueItem;
  7410. TQueueItemProxy * DestQueueItem;
  7411. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7412. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7413. QueueItem->Move(DestQueueItem);
  7414. }
  7415. }
  7416. //---------------------------------------------------------------------------
  7417. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7418. {
  7419. if ((QueueView3->ItemFocused == NULL) &&
  7420. (QueueView3->Items->Count > 0))
  7421. {
  7422. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7423. }
  7424. QueueLabelUpdateStatus();
  7425. }
  7426. //---------------------------------------------------------------------------
  7427. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7428. {
  7429. QueueLabelUpdateStatus();
  7430. }
  7431. //---------------------------------------------------------------------------
  7432. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7433. {
  7434. QueueLabel->UpdateStatus();
  7435. }
  7436. //---------------------------------------------------------------------------
  7437. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7438. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7439. {
  7440. if (Selected)
  7441. {
  7442. NonVisualDataModule->UpdateNonVisibleActions();
  7443. }
  7444. }
  7445. //---------------------------------------------------------------------------
  7446. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7447. {
  7448. TDragDropFilesEx * Result = NULL;
  7449. if (Sender == SessionsPageControl)
  7450. {
  7451. Result = FSessionsDragDropFilesEx;
  7452. }
  7453. else if (Sender == QueueView3)
  7454. {
  7455. Result = FQueueDragDropFilesEx;
  7456. }
  7457. else
  7458. {
  7459. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7460. if (DirView != NULL)
  7461. {
  7462. Result = DirView->DragDropFilesEx;
  7463. }
  7464. else
  7465. {
  7466. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7467. if (DriveView != NULL)
  7468. {
  7469. Result = DriveView->DragDropFilesEx;
  7470. }
  7471. }
  7472. }
  7473. DebugAssert(Result != NULL);
  7474. return Result;
  7475. }
  7476. //---------------------------------------------------------------------------
  7477. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7478. {
  7479. return (DView->SelCount == DView->FilesCount);
  7480. }
  7481. //---------------------------------------------------------------------------
  7482. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7483. {
  7484. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7485. }
  7486. //---------------------------------------------------------------------------
  7487. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7488. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7489. {
  7490. DebugAssert(!IsLocalBrowserMode());
  7491. TFileOperation Operation;
  7492. switch (Effect)
  7493. {
  7494. case DROPEFFECT_MOVE:
  7495. Operation = foMove;
  7496. break;
  7497. case DROPEFFECT_COPY:
  7498. // occures on WinXP (reported by user)
  7499. default:
  7500. Operation = foCopy;
  7501. break;
  7502. };
  7503. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7504. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7505. if (DragDropFilesEx->FileList->Count > 0)
  7506. {
  7507. TStrings * FileList = new TStringList();
  7508. try
  7509. {
  7510. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7511. {
  7512. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7513. }
  7514. FDragDropOperation = true;
  7515. TTransferOperationParam Param;
  7516. Param.TargetDirectory = TargetPath;
  7517. // upload, no temp dirs
  7518. Param.Temp = false;
  7519. Param.DragDrop = DragDrop;
  7520. Param.Options =
  7521. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7522. if (ForceQueue)
  7523. {
  7524. Param.Queue = asOn;
  7525. }
  7526. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7527. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7528. {
  7529. if (IsFileControl(DropSourceControl, osLocal))
  7530. {
  7531. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7532. }
  7533. else
  7534. {
  7535. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7536. }
  7537. }
  7538. }
  7539. __finally
  7540. {
  7541. FDragDropOperation = false;
  7542. delete FileList;
  7543. }
  7544. }
  7545. }
  7546. //---------------------------------------------------------------------------
  7547. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7548. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7549. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7550. {
  7551. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7552. }
  7553. //---------------------------------------------------------------------------
  7554. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7555. TObject * Sender, int grfKeyState, int & dwEffect)
  7556. {
  7557. DebugAssert(!IsLocalBrowserMode());
  7558. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7559. if ((dwEffect != DROPEFFECT_NONE) &&
  7560. IsFileControl(DropSourceControl, osRemote))
  7561. {
  7562. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7563. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7564. (RemoteDirView->DropTarget == NULL))
  7565. {
  7566. dwEffect = DROPEFFECT_NONE;
  7567. }
  7568. else
  7569. {
  7570. if (dwEffect == DROPEFFECT_COPY)
  7571. {
  7572. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7573. // currently we support copying always (at least via temporary directory);
  7574. // remove associated checks once this all proves stable and working
  7575. bool CopyCapable = true;
  7576. // if we do not support neither of operations, there's no discussion
  7577. if (!MoveCapable && !CopyCapable)
  7578. {
  7579. dwEffect = DROPEFFECT_NONE;
  7580. }
  7581. else
  7582. {
  7583. // The default effect inside remote panel is move,
  7584. // unless we do not support it, but support copy
  7585. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7586. {
  7587. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7588. }
  7589. else
  7590. {
  7591. // with ctrl-down, we want copy unless it is not supported
  7592. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7593. }
  7594. }
  7595. }
  7596. }
  7597. }
  7598. }
  7599. //---------------------------------------------------------------------------
  7600. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7601. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7602. {
  7603. DebugAssert(!IsLocalBrowserMode());
  7604. if (FDDTotalSize >= 0)
  7605. {
  7606. if (File->IsDirectory)
  7607. {
  7608. FDDTotalSize = -1;
  7609. }
  7610. else
  7611. {
  7612. FDDTotalSize += File->Size;
  7613. }
  7614. }
  7615. DebugAssert(!FDragTempDir.IsEmpty());
  7616. // TODO: this is quite ineffective
  7617. // TODO: what if invalid character replacement is disabled?
  7618. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7619. UnicodeString TransferFileName;
  7620. if (Sender == RemoteDriveView)
  7621. {
  7622. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7623. }
  7624. else
  7625. {
  7626. TransferFileName = File->FileName;
  7627. }
  7628. FDDFileList->AddObject(TransferFileName, File);
  7629. }
  7630. //---------------------------------------------------------------------------
  7631. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7632. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7633. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7634. {
  7635. // sometimes we do not get DDEnd so the list is not released
  7636. SAFE_DESTROY(FDDFileList);
  7637. FDDFileList = new TStringList();
  7638. FDragTempDir = WinConfiguration->TemporaryDir();
  7639. FDDTotalSize = 0;
  7640. FDragDropSshTerminate.reset(NULL);
  7641. }
  7642. //---------------------------------------------------------------------------
  7643. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7644. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7645. HRESULT & Result)
  7646. {
  7647. if (Result == DRAGDROP_S_DROP)
  7648. {
  7649. try
  7650. {
  7651. GlobalDragImageList->HideDragImage();
  7652. try
  7653. {
  7654. RemoteFileControlDDTargetDrop();
  7655. }
  7656. catch(ESshTerminate & E)
  7657. {
  7658. FDragDropSshTerminate.reset(E.Clone());
  7659. }
  7660. }
  7661. catch (Exception &E)
  7662. {
  7663. // If downloading fails we need to cancel drag&drop, otherwise
  7664. // Explorer shows error
  7665. // But by the way exception probably never reach this point as
  7666. // it's catched on way.
  7667. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7668. Result = DRAGDROP_S_CANCEL;
  7669. ShowExtendedException(Terminal, &E);
  7670. }
  7671. }
  7672. }
  7673. //---------------------------------------------------------------------------
  7674. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7675. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7676. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7677. {
  7678. TFileMasks::TParams MaskParams;
  7679. MaskParams.Size = Size;
  7680. MaskParams.Modification = Modification;
  7681. // this does not re-parse the mask if it is the same as the last time
  7682. FDirViewMatchMask = Masks;
  7683. bool ImplicitMatch;
  7684. Matches =
  7685. // RecurseInclude parameter has no effect as the Path is empty
  7686. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7687. (AllowImplicitMatches || !ImplicitMatch);
  7688. }
  7689. //---------------------------------------------------------------------------
  7690. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7691. TObject * Sender, TListItem * Item, WORD & Indexes)
  7692. {
  7693. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7694. UnicodeString Ext;
  7695. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7696. {
  7697. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7698. }
  7699. else
  7700. {
  7701. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7702. }
  7703. if (SameText(Ext, Configuration->PartialExt))
  7704. {
  7705. Indexes |= oiPartial;
  7706. }
  7707. }
  7708. //---------------------------------------------------------------------------
  7709. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7710. {
  7711. return
  7712. DirViewEnabled(osCurrent) &&
  7713. DirView(osCurrent)->CanPasteFromClipBoard();
  7714. }
  7715. //---------------------------------------------------------------------------
  7716. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7717. {
  7718. bool Result = false;
  7719. if (CanPasteToDirViewFromClipBoard())
  7720. {
  7721. Result = true;
  7722. }
  7723. else
  7724. {
  7725. UnicodeString ClipboardText;
  7726. if (NonEmptyTextFromClipboard(ClipboardText))
  7727. {
  7728. if (StoredSessions->IsUrl(ClipboardText))
  7729. {
  7730. Result = true;
  7731. }
  7732. else
  7733. {
  7734. Result = DirViewEnabled(osCurrent);
  7735. }
  7736. }
  7737. }
  7738. return Result;
  7739. }
  7740. //---------------------------------------------------------------------------
  7741. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7742. {
  7743. if (DoesClipboardContainOurFiles())
  7744. {
  7745. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7746. {
  7747. DebugAssert(!IsLocalBrowserMode());
  7748. TTerminalManager * Manager = TTerminalManager::Instance();
  7749. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7750. Manager->ActiveTerminal = FClipboardTerminal;
  7751. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7752. }
  7753. }
  7754. else if (CanPasteToDirViewFromClipBoard())
  7755. {
  7756. DirView(osCurrent)->PasteFromClipBoard();
  7757. }
  7758. else
  7759. {
  7760. UnicodeString ClipboardText;
  7761. if (NonEmptyTextFromClipboard(ClipboardText))
  7762. {
  7763. if (StoredSessions->IsUrl(ClipboardText))
  7764. {
  7765. NewSession(false, ClipboardText);
  7766. }
  7767. else
  7768. {
  7769. DirView(osCurrent)->Path = ClipboardText;
  7770. }
  7771. }
  7772. }
  7773. }
  7774. //---------------------------------------------------------------------------
  7775. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7776. {
  7777. // TBD
  7778. }
  7779. //---------------------------------------------------------------------------
  7780. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7781. {
  7782. TCustomDirView * ADirView = DirView(Side);
  7783. ADirView->SelectAll(Mode);
  7784. ADirView->SetFocus();
  7785. }
  7786. //---------------------------------------------------------------------------
  7787. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7788. {
  7789. TCustomDirView * ADirView = DirView(Side);
  7790. TFileFilter Filter;
  7791. DefaultFileFilter(Filter);
  7792. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7793. {
  7794. Configuration->Usage->Inc(L"MaskSelections");
  7795. ADirView->SelectFiles(Filter, Select);
  7796. ADirView->SetFocus();
  7797. }
  7798. }
  7799. //---------------------------------------------------------------------------
  7800. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7801. {
  7802. TCustomDirView * ADirView = DirView(Side);
  7803. ADirView->RestoreSelectedNames();
  7804. ADirView->SetFocus();
  7805. }
  7806. //---------------------------------------------------------------------------
  7807. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7808. {
  7809. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7810. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7811. {
  7812. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7813. UnicodeString Ext;
  7814. if (!IsSideLocalBrowser(osCurrent))
  7815. {
  7816. Ext = UnixExtractFileExt(FileName);
  7817. }
  7818. else
  7819. {
  7820. Ext = ExtractFileExt(FileName);
  7821. }
  7822. if (Ext.IsEmpty())
  7823. {
  7824. Ext = L".";
  7825. }
  7826. TFileFilter Filter;
  7827. Filter.Masks = FORMAT(L"*%s", (Ext));
  7828. Filter.Directories = false;
  7829. CurrentDirView->SelectFiles(Filter, Select);
  7830. }
  7831. }
  7832. //---------------------------------------------------------------------------
  7833. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7834. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7835. {
  7836. UnicodeString Result;
  7837. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7838. {
  7839. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7840. }
  7841. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7842. {
  7843. // noop
  7844. }
  7845. else
  7846. {
  7847. Result =
  7848. FMTLOAD(FILE_INFO_FORMAT,
  7849. (FormatBytes(FileInfo.SelectedSize),
  7850. FormatBytes(FileInfo.FilesSize),
  7851. FormatNumber(FileInfo.SelectedCount),
  7852. FormatNumber(FileInfo.FilesCount)));
  7853. }
  7854. return Result;
  7855. }
  7856. //---------------------------------------------------------------------------
  7857. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7858. TTBXStatusPanel * Panel, TOperationSide Side)
  7859. {
  7860. if (Panel->Index == 1)
  7861. {
  7862. ToggleShowHiddenFiles();
  7863. }
  7864. else if (Panel->Index == 2)
  7865. {
  7866. Filter(Side);
  7867. }
  7868. }
  7869. //---------------------------------------------------------------------------
  7870. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7871. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7872. {
  7873. DebugAssert(!StatusBar->SimplePanel);
  7874. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7875. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7876. }
  7877. //---------------------------------------------------------------------------
  7878. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7879. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7880. {
  7881. DebugAssert(StatusBar->Panels->Count >= 3);
  7882. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7883. if (FileInfo.HiddenCount > 0)
  7884. {
  7885. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7886. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7887. }
  7888. else
  7889. {
  7890. HiddenFilesPanel->ViewPriority = 0;
  7891. // not really necessary, just to cleanup no-longer-valid data
  7892. HiddenFilesPanel->Caption = L"";
  7893. }
  7894. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7895. if (FileInfo.FilteredCount > 0)
  7896. {
  7897. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7898. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7899. }
  7900. else
  7901. {
  7902. FilteredFilesPanel->ViewPriority = 0;
  7903. // not really necessary, just to cleanup no-longer-valid data
  7904. FilteredFilesPanel->Caption = L"";
  7905. }
  7906. }
  7907. //---------------------------------------------------------------------------
  7908. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7909. TObject * /*Sender*/)
  7910. {
  7911. if (DirView(osRemote)->Enabled)
  7912. {
  7913. DirView(osRemote)->SetFocus();
  7914. }
  7915. }
  7916. //---------------------------------------------------------------------------
  7917. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7918. {
  7919. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7920. switch (Config.Show)
  7921. {
  7922. case qvShow:
  7923. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7924. {
  7925. Config.Show = qvHide;
  7926. }
  7927. else
  7928. {
  7929. Config.Show = Config.LastHideShow;
  7930. }
  7931. break;
  7932. case qvHideWhenEmpty:
  7933. if (ComponentVisible[fcQueueView])
  7934. {
  7935. Config.Show = qvHide;
  7936. }
  7937. else
  7938. {
  7939. Config.LastHideShow = Config.Show;
  7940. Config.Show = qvShow;
  7941. }
  7942. break;
  7943. case qvHide:
  7944. Config.LastHideShow = Config.Show;
  7945. Config.Show = qvShow;
  7946. break;
  7947. }
  7948. WinConfiguration->QueueView = Config;
  7949. }
  7950. //---------------------------------------------------------------------------
  7951. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7952. {
  7953. UnicodeString Result;
  7954. if (FTerminal != NULL)
  7955. {
  7956. switch (WinConfiguration->PathInCaption)
  7957. {
  7958. case picShort:
  7959. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7960. break;
  7961. case picFull:
  7962. Result = FTerminal->CurrentDirectory;
  7963. break;
  7964. }
  7965. }
  7966. return Result;
  7967. }
  7968. //---------------------------------------------------------------------------
  7969. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7970. {
  7971. TColor Result = GetWindowColor(FSessionColor);
  7972. return Result;
  7973. }
  7974. //---------------------------------------------------------------------------
  7975. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7976. {
  7977. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7978. return Result;
  7979. }
  7980. //---------------------------------------------------------------------------
  7981. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7982. {
  7983. TTerminalManager::Instance()->UpdateAppTitle();
  7984. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7985. // background to gray on Windows 7 (works on Windows 10).
  7986. // See also EnableControl
  7987. if (Showing)
  7988. {
  7989. if (Terminal != NULL)
  7990. {
  7991. // Update path when it changes
  7992. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7993. {
  7994. UpdateSessionTab(SessionsPageControl->ActivePage);
  7995. }
  7996. }
  7997. NonVisualDataModule->ReconnectSessionAction->Update();
  7998. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  7999. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8000. CenterReconnectToolbar();
  8001. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  8002. if (HasTerminal)
  8003. {
  8004. // TODO needed yet with second local browser?
  8005. if (!RemoteDirView->Enabled)
  8006. {
  8007. RemoteDirView->Enabled = true;
  8008. if (FRemoteDirViewWasFocused)
  8009. {
  8010. ActiveControl = RemoteDirView;
  8011. }
  8012. }
  8013. RemoteDirView->Color = PanelColor();
  8014. }
  8015. else
  8016. {
  8017. if (RemoteDirView->Enabled)
  8018. {
  8019. // This is first called when the form is being constructed
  8020. // (not anymore due to Showing test above)
  8021. // but the false is overriden in the constructor later.
  8022. // An even later in TScpCommanderForm::DoShow()
  8023. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8024. RemoteDirView->Enabled = false;
  8025. }
  8026. RemoteDirView->Color = DisabledPanelColor();
  8027. }
  8028. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8029. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8030. RemoteDriveView->Color = RemoteDirView->Color;
  8031. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8032. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  8033. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8034. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8035. QueueLabelUpdateStatus();
  8036. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8037. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8038. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8039. }
  8040. }
  8041. //---------------------------------------------------------------------------
  8042. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8043. {
  8044. UpdateControls();
  8045. }
  8046. //---------------------------------------------------------------------------
  8047. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8048. TObject * Sender)
  8049. {
  8050. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8051. DebugAssert(DirView != NULL);
  8052. DoDirViewLoaded(DirView);
  8053. TransferPresetAutoSelect();
  8054. }
  8055. //---------------------------------------------------------------------------
  8056. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8057. {
  8058. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8059. // first run after installation
  8060. if (double(Updates.LastCheck) == 0)
  8061. {
  8062. // make sure next time there will be an update (if enabled)
  8063. Updates.LastCheck = TDateTime(1);
  8064. WinConfiguration->Updates = Updates;
  8065. }
  8066. else if ((double(Updates.Period) > 0) &&
  8067. (Now() - Updates.LastCheck >= Updates.Period))
  8068. {
  8069. TThreadMethod OnUpdatesChecked = NULL;
  8070. if (!IsUWP())
  8071. {
  8072. OnUpdatesChecked = UpdatesChecked;
  8073. }
  8074. StartUpdateThread(OnUpdatesChecked);
  8075. }
  8076. }
  8077. //---------------------------------------------------------------------------
  8078. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8079. {
  8080. UnicodeString Message;
  8081. bool New;
  8082. TQueryType Type;
  8083. GetUpdatesMessage(Message, New, Type, false);
  8084. if (!Message.IsEmpty())
  8085. {
  8086. if (New)
  8087. {
  8088. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8089. }
  8090. if (!New && (Type != qtWarning))
  8091. {
  8092. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8093. }
  8094. else
  8095. {
  8096. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8097. }
  8098. if (New)
  8099. {
  8100. Configuration->Usage->Inc(L"UpdateNotifications");
  8101. }
  8102. }
  8103. }
  8104. //---------------------------------------------------------------------------
  8105. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8106. {
  8107. RestoreApp();
  8108. bool CanDisplay = !NonVisualDataModule->Busy;
  8109. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8110. {
  8111. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8112. }
  8113. if (CanDisplay)
  8114. {
  8115. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8116. CheckForUpdates(true);
  8117. }
  8118. else
  8119. {
  8120. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8121. }
  8122. }
  8123. //---------------------------------------------------------------------------
  8124. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8125. TCopyParamRuleData & Data)
  8126. {
  8127. DebugAssert(Terminal != NULL);
  8128. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8129. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8130. Data.RemoteDirectory = RemoteDirView->PathName;
  8131. }
  8132. //---------------------------------------------------------------------------
  8133. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8134. {
  8135. // Terminal can be null when we are changing local directory implicitly,
  8136. // because it has been deleted, while no session is connected
  8137. // (Login dialog is open)
  8138. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8139. {
  8140. DebugAssert(!IsLocalBrowserMode());
  8141. TCopyParamRuleData Data;
  8142. GetTransferPresetAutoSelectData(Data);
  8143. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8144. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8145. if (CopyParamIndex < 0)
  8146. {
  8147. // there is no preset that matches autoselection
  8148. // set preset that we consider "default"
  8149. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8150. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8151. }
  8152. else
  8153. {
  8154. // there is preset matching autoselection
  8155. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8156. if (CopyParamName == FCopyParamAutoSelected)
  8157. {
  8158. // autoselected the same preset as the last time
  8159. // make no change (i.e. preserve custom user preset, if any)
  8160. }
  8161. else
  8162. {
  8163. // autoselected the different preset then the last time (or there
  8164. // was default preset set)
  8165. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8166. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8167. }
  8168. }
  8169. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8170. {
  8171. if (CopyParamIndex >= 0)
  8172. {
  8173. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8174. }
  8175. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8176. try
  8177. {
  8178. int Fmt =
  8179. (CopyParamIndex < 0) ?
  8180. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8181. COPY_PARAM_AUTOSELECTED;
  8182. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8183. Data->Message = MainInstructions(Message);
  8184. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8185. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8186. CopyParamAttrs);
  8187. if (CopyParamIndex >= 0)
  8188. {
  8189. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8190. Info = FORMAT(L"%s\n \n%s", (Info,
  8191. FMTLOAD(COPY_PARAM_RULE,
  8192. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8193. }
  8194. Data->Message += L"\n\n" + Info;
  8195. if (WinConfiguration->CopyParamAutoSelectNotice)
  8196. {
  8197. TransferPresetNoteMessage(Data, true);
  8198. }
  8199. else
  8200. {
  8201. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8202. Data = NULL; // ownership passed
  8203. }
  8204. }
  8205. __finally
  8206. {
  8207. delete Data;
  8208. }
  8209. }
  8210. }
  8211. }
  8212. //---------------------------------------------------------------------------
  8213. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8214. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8215. {
  8216. DebugAssert(NoteData != NULL);
  8217. DebugAssert(!IsLocalBrowserMode());
  8218. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8219. TQueryButtonAlias Aliases[1];
  8220. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8221. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8222. Params.Aliases = Aliases;
  8223. Params.AliasesCount = LENOF(Aliases);
  8224. unsigned int Result =
  8225. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8226. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8227. switch (Result)
  8228. {
  8229. case qaNeverAskAgain:
  8230. DebugAssert(AllowNeverAskAgain);
  8231. WinConfiguration->CopyParamAutoSelectNotice = false;
  8232. break;
  8233. case qaIgnore:
  8234. PreferencesDialog(pmPresets);
  8235. break;
  8236. }
  8237. }
  8238. //---------------------------------------------------------------------------
  8239. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8240. {
  8241. // as of now useless, as this is used for notes only, never for balloons, ...
  8242. RestoreApp();
  8243. // .. and we should never be busy here
  8244. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8245. {
  8246. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8247. }
  8248. }
  8249. //---------------------------------------------------------------------------
  8250. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8251. TPreferencesMode APreferencesMode)
  8252. {
  8253. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8254. TCopyParamRuleData Data;
  8255. if (Terminal != NULL)
  8256. {
  8257. PreferencesData.reset(new TPreferencesDialogData());
  8258. GetTransferPresetAutoSelectData(Data);
  8259. PreferencesData->CopyParamRuleData = &Data;
  8260. }
  8261. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8262. }
  8263. //---------------------------------------------------------------------------
  8264. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8265. const TCustomCommandType & Command)
  8266. {
  8267. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8268. {
  8269. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8270. }
  8271. }
  8272. //---------------------------------------------------------------------------
  8273. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8274. {
  8275. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8276. TCustomCommandType Command;
  8277. // make sure we use local custom command when remote are not supported
  8278. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8279. {
  8280. Command = FLastCustomCommand;
  8281. }
  8282. else
  8283. {
  8284. Command.Params = Command.Params | ccLocal;
  8285. }
  8286. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8287. FEditingFocusedAdHocCommand = OnFocused;
  8288. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8289. int Options =
  8290. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8291. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8292. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8293. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8294. {
  8295. FLastCustomCommand = Command;
  8296. UpdateCustomCommandsToolbar();
  8297. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8298. }
  8299. }
  8300. //---------------------------------------------------------------------------
  8301. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8302. {
  8303. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8304. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8305. DebugAssert(State > 0);
  8306. if (State <= 0)
  8307. {
  8308. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8309. }
  8310. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8311. }
  8312. //---------------------------------------------------------------------------
  8313. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8314. TCustomCommandType & Command, int & State)
  8315. {
  8316. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8317. if (Result)
  8318. {
  8319. Command = FLastCustomCommand;
  8320. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8321. }
  8322. return Result;
  8323. }
  8324. //---------------------------------------------------------------------------
  8325. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8326. {
  8327. ResetIncrementalSearch();
  8328. }
  8329. //---------------------------------------------------------------------------
  8330. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8331. {
  8332. DebugAssert(ComponentVisible[Component]);
  8333. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8334. }
  8335. //---------------------------------------------------------------------------
  8336. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8337. {
  8338. // when queue panel is resized here directly, the status bar is stretched
  8339. // over whole space the panel occupied
  8340. PostComponentHide(fcQueueView);
  8341. }
  8342. //---------------------------------------------------------------------------
  8343. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8344. {
  8345. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8346. if (!FInvalid)
  8347. {
  8348. WinConfiguration->ResetSysDarkTheme();
  8349. ConfigurationChanged();
  8350. ConfigureInterface();
  8351. }
  8352. TForm::Dispatch(&Message);
  8353. }
  8354. //---------------------------------------------------------------------------
  8355. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8356. {
  8357. TMessage * M = static_cast<TMessage*>(Message);
  8358. switch (M->Msg)
  8359. {
  8360. case CM_DIALOGCHAR:
  8361. CMDialogChar(*M);
  8362. break;
  8363. case WM_APPCOMMAND:
  8364. WMAppCommand(*M);
  8365. break;
  8366. case WM_SYSCOMMAND:
  8367. WMSysCommand(*M);
  8368. break;
  8369. case WM_QUERYENDSESSION:
  8370. WMQueryEndSession(*M);
  8371. break;
  8372. case WM_ENDSESSION:
  8373. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8374. break;
  8375. #ifdef _DEBUG
  8376. case WM_WINDOWPOSCHANGED:
  8377. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8378. break;
  8379. case CM_CUSTOMSTYLECHANGED:
  8380. TForm::Dispatch(Message);
  8381. break;
  8382. #endif
  8383. case WM_COMPONENT_HIDE:
  8384. {
  8385. Byte Component = static_cast<Byte>(M->WParam);
  8386. // sanity check
  8387. if (ComponentVisible[Component])
  8388. {
  8389. // special treatment
  8390. if (Component == fcQueueView)
  8391. {
  8392. ToggleQueueVisibility();
  8393. DebugAssert(!ComponentVisible[fcQueueView]);
  8394. }
  8395. else
  8396. {
  8397. ComponentVisible[Component] = false;
  8398. }
  8399. }
  8400. }
  8401. break;
  8402. case WM_COPYDATA:
  8403. WMCopyData(*M);
  8404. break;
  8405. case WM_MANAGES_CAPTION:
  8406. // caption managed in TTerminalManager::UpdateAppTitle()
  8407. M->Result = 1;
  8408. break;
  8409. case WM_WANTS_MOUSEWHEEL:
  8410. M->Result = 1;
  8411. break;
  8412. case CM_SHOWINGCHANGED:
  8413. CMShowingChanged(*M);
  8414. break;
  8415. case WM_CLOSE:
  8416. WMClose(*M);
  8417. break;
  8418. case WM_DPICHANGED:
  8419. WMDpiChanged(*M);
  8420. break;
  8421. case CM_DPICHANGED:
  8422. CMDpiChanged(*M);
  8423. break;
  8424. case WM_WININICHANGE:
  8425. WMWinIniChange(*M);
  8426. break;
  8427. case CM_DIALOGKEY:
  8428. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8429. break;
  8430. default:
  8431. TForm::Dispatch(Message);
  8432. break;
  8433. }
  8434. }
  8435. //---------------------------------------------------------------------------
  8436. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8437. {
  8438. // noop
  8439. }
  8440. //---------------------------------------------------------------------------
  8441. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8442. {
  8443. TForm::Dispatch(&Message);
  8444. FSessionColors->Clear();
  8445. AddFixedSessionImages();
  8446. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8447. UpdateImages();
  8448. }
  8449. //---------------------------------------------------------------------------
  8450. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8451. {
  8452. TForm::Dispatch(&Message);
  8453. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8454. UpdatePixelsPerInchMainWindowCounter();
  8455. }
  8456. //---------------------------------------------------------------------------
  8457. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8458. {
  8459. // Cannot close window while we are busy.
  8460. // We cannot test this in FormCloseQuery as that is called also from
  8461. // Close(), which is called by CloseApplicationAction. So we can be busy
  8462. // there already even, when it is legitimate to close the application.
  8463. // Possibly a better place to handle this would be WMSysCommand.
  8464. if (NonVisualDataModule->Busy)
  8465. {
  8466. Message.Result = 1;
  8467. }
  8468. else
  8469. {
  8470. TForm::Dispatch(&Message);
  8471. }
  8472. }
  8473. //---------------------------------------------------------------------------
  8474. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8475. {
  8476. TForm::Dispatch(&Message);
  8477. if (Showing && (Terminal == NULL))
  8478. {
  8479. // When we are starting minimized (i.e. from an installer),
  8480. // postpone showing Login dialog until we get restored.
  8481. // Otherwise the Login dialog (and Authentication window) show restored
  8482. // over invisible (minimized) main window.
  8483. if (WindowState == wsMinimized)
  8484. {
  8485. FNeedSession = true;
  8486. }
  8487. else
  8488. {
  8489. // This happens before application ever goes idle, so the toolbars would
  8490. // stay enabled (initial state) until the Login dialog is dismissed.
  8491. UpdateActions();
  8492. NonVisualDataModule->StartBusy();
  8493. try
  8494. {
  8495. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8496. // otherwise the dialog does not receive focus.
  8497. // With Commander interface the ProcessMessages is called already
  8498. // by TDriveView, but with Explorer interface, we need to call it explicily
  8499. Application->ProcessMessages();
  8500. // do not reload sessions, they have been loaded just now (optimization)
  8501. NeedSession(true);
  8502. }
  8503. __finally
  8504. {
  8505. NonVisualDataModule->EndBusy();
  8506. }
  8507. }
  8508. }
  8509. }
  8510. //---------------------------------------------------------------------------
  8511. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8512. {
  8513. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8514. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8515. }
  8516. //---------------------------------------------------------------------------
  8517. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8518. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8519. int & MaxHeight)
  8520. {
  8521. // workaround for bug in TWinControl.CalcConstraints
  8522. // Check for empty rect (restore from iconic state) is done there only after
  8523. // call to AdjustClientRect, which enlarges the rect (for forms).
  8524. TRect R = GetClientRect();
  8525. // when restoring from iconic state, place no restrictions
  8526. if (IsRectEmpty(R))
  8527. {
  8528. MinWidth = 0;
  8529. MinHeight = 0;
  8530. MaxWidth = 32000;
  8531. MaxHeight = 32000;
  8532. }
  8533. }
  8534. //---------------------------------------------------------------------------
  8535. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8536. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8537. {
  8538. // terminal can be already closed (e.g. dropped connection)
  8539. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8540. {
  8541. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8542. try
  8543. {
  8544. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8545. }
  8546. catch(...)
  8547. {
  8548. if (!Terminal->Active)
  8549. {
  8550. Close = true;
  8551. }
  8552. throw;
  8553. }
  8554. }
  8555. }
  8556. //---------------------------------------------------------------------------
  8557. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8558. {
  8559. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8560. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8561. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8562. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8563. {
  8564. OnGetSpaceAvailable = GetSpaceAvailable;
  8565. }
  8566. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8567. OnGetSpaceAvailable);
  8568. }
  8569. //---------------------------------------------------------------------------
  8570. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8571. {
  8572. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8573. Terminal->FillSessionDataForCode(Data.get());
  8574. return Data.release();
  8575. }
  8576. //---------------------------------------------------------------------------
  8577. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8578. {
  8579. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8580. DoGenerateUrlDialog(Data.get(), Paths);
  8581. }
  8582. //---------------------------------------------------------------------------
  8583. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8584. {
  8585. GenerateUrl(NULL);
  8586. }
  8587. //---------------------------------------------------------------------------
  8588. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8589. {
  8590. std::unique_ptr<TStrings> Paths(new TStringList());
  8591. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8592. GenerateUrl(Paths.get());
  8593. }
  8594. //---------------------------------------------------------------------------
  8595. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8596. {
  8597. FSessionColor = value;
  8598. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8599. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8600. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8601. DebugAssert(ColorItem != NULL);
  8602. ColorItem->Color = C;
  8603. NonVisualDataModule->ColorMenuItem->Color = C;
  8604. UpdateControls();
  8605. }
  8606. //---------------------------------------------------------------------------
  8607. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8608. {
  8609. if (value != FSessionColor)
  8610. {
  8611. UpdateSessionColor(value);
  8612. // Is null when called from LastTerminalClosed
  8613. if (Terminal != NULL)
  8614. {
  8615. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8616. }
  8617. }
  8618. }
  8619. //---------------------------------------------------------------------------
  8620. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8621. {
  8622. bool Result = FNoteTimer->Enabled;
  8623. if (Result)
  8624. {
  8625. // cannot cancel note too early
  8626. bool NotEarly =
  8627. (Now() - FNoteShown >
  8628. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8629. if (Force || NotEarly)
  8630. {
  8631. FNoteTimer->Enabled = false;
  8632. FNote = L"";
  8633. SAFE_DESTROY(FNoteData);
  8634. FOnNoteClick = NULL;
  8635. FNoteHints = FNotes->Text;
  8636. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8637. UpdateStatusBar();
  8638. }
  8639. }
  8640. return Result;
  8641. }
  8642. //---------------------------------------------------------------------------
  8643. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8644. {
  8645. DebugAssert(FNoteTimer->Enabled);
  8646. CancelNote(true);
  8647. }
  8648. //---------------------------------------------------------------------------
  8649. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8650. {
  8651. int P = Note.Pos(L"\n");
  8652. if (P > 0)
  8653. {
  8654. Note.SetLength(P - 1);
  8655. }
  8656. FNotes->Add(FORMAT(L"[%s] %s",
  8657. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8658. while (FNotes->Count > 10)
  8659. {
  8660. FNotes->Delete(0);
  8661. }
  8662. if (UpdateNow)
  8663. {
  8664. FNoteHints = FNotes->Text;
  8665. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8666. UpdateStatusBar();
  8667. }
  8668. }
  8669. //---------------------------------------------------------------------------
  8670. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8671. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8672. {
  8673. int P = Note.Pos(L"\n");
  8674. if (P > 0)
  8675. {
  8676. Note.SetLength(P - 1);
  8677. }
  8678. FNoteHints = FNotes->Text;
  8679. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8680. FNote = Note;
  8681. SAFE_DESTROY(FNoteData);
  8682. FNoteData = NoteData;
  8683. FOnNoteClick = OnNoteClick;
  8684. AddNote(Note, false);
  8685. UpdateStatusBar();
  8686. FNoteShown = Now();
  8687. FNoteTimer->Enabled = false;
  8688. if (Seconds == 0)
  8689. {
  8690. Seconds = WinConfiguration->NotificationsTimeout;
  8691. }
  8692. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8693. FNoteTimer->Enabled = true;
  8694. }
  8695. //---------------------------------------------------------------------------
  8696. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8697. {
  8698. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8699. }
  8700. //---------------------------------------------------------------------------
  8701. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8702. {
  8703. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8704. {
  8705. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8706. }
  8707. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8708. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8709. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8710. 0, NULL, NULL);
  8711. }
  8712. //---------------------------------------------------------------------------
  8713. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8714. {
  8715. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8716. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8717. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8718. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8719. }
  8720. //---------------------------------------------------------------------------
  8721. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8722. {
  8723. WinConfiguration->FormatSizeBytes = Style;
  8724. }
  8725. //---------------------------------------------------------------------------
  8726. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8727. {
  8728. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8729. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8730. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8731. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8732. }
  8733. //---------------------------------------------------------------------------
  8734. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8735. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8736. {
  8737. if (Panel->Index == 0)
  8738. {
  8739. if (FOnNoteClick != NULL)
  8740. {
  8741. // prevent the user data from being freed by possible call
  8742. // to CancelNote or PostNote during call to OnNoteClick
  8743. std::unique_ptr<TObject> NoteData(FNoteData);
  8744. TNotifyEvent OnNoteClick = FOnNoteClick;
  8745. FNoteData = NULL;
  8746. // need to cancel the note as we are going to delete its user data
  8747. CancelNote(true);
  8748. OnNoteClick(NoteData.get());
  8749. }
  8750. }
  8751. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8752. {
  8753. FileSystemInfo();
  8754. }
  8755. }
  8756. //---------------------------------------------------------------------------
  8757. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8758. {
  8759. // workaround:
  8760. // 1) for unknown reason, disabling window, while minimized,
  8761. // prevents it from restoring, even if it was enabled again meanwhile
  8762. // 2) when disabling the main window, while another has focus
  8763. // minimize is no longer possible
  8764. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8765. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8766. // Shouldn't we use IsApplicationMinimized() here?
  8767. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8768. {
  8769. Enabled = false;
  8770. }
  8771. FLockLevel++;
  8772. }
  8773. //---------------------------------------------------------------------------
  8774. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8775. {
  8776. DebugAssert(FLockLevel > 0);
  8777. FLockLevel--;
  8778. if (FLockLevel == 0)
  8779. {
  8780. DebugAssert(FLockSuspendLevel == 0);
  8781. Enabled = true;
  8782. }
  8783. }
  8784. //---------------------------------------------------------------------------
  8785. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8786. {
  8787. // We need to make sure that window is enabled when the last modal window closes
  8788. // otherwise focus is not restored correctly.
  8789. // So we re-enable the window when modal window opens and
  8790. // disable it back after is closes
  8791. if (FLockLevel > 0)
  8792. {
  8793. // while we have nesting counter, we know that we never be called
  8794. // recursivelly as Application->OnModalBegin is called only
  8795. // for the top-level modal window
  8796. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8797. {
  8798. // won't be disabled when conditions in LockWindow() were not satisfied
  8799. FDisabledOnLockSuspend = !Enabled;
  8800. // When minimized to tray (or actually when set to SW_HIDE),
  8801. // setting Enabled makes the window focusable even when there's
  8802. // modal window over it
  8803. if (!FTrayIcon->Visible)
  8804. {
  8805. Enabled = true;
  8806. }
  8807. }
  8808. FLockSuspendLevel++;
  8809. }
  8810. }
  8811. //---------------------------------------------------------------------------
  8812. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8813. {
  8814. if (FLockSuspendLevel > 0)
  8815. {
  8816. DebugAssert(FLockLevel > 0);
  8817. FLockSuspendLevel--;
  8818. // see comment in SuspendWindowLock
  8819. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8820. {
  8821. // Note that window can be enabled here, when we were minized to tray when
  8822. // was SuspendWindowLock() called.
  8823. // We should possibly do the same check as in LockWindow(),
  8824. // if it is ever possible that the consitions change between
  8825. // SuspendWindowLock() and ResumeWindowLock()
  8826. Enabled = !FDisabledOnLockSuspend;
  8827. }
  8828. }
  8829. }
  8830. //---------------------------------------------------------------------------
  8831. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8832. {
  8833. if (!TextOnly)
  8834. {
  8835. TTBXComboBoxItem * RemotePathComboBox =
  8836. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8837. TStrings * Items = RemotePathComboBox->Strings;
  8838. Items->BeginUpdate();
  8839. try
  8840. {
  8841. Items->Clear();
  8842. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8843. {
  8844. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8845. while (!IsUnixRootPath(APath))
  8846. {
  8847. int P = APath.LastDelimiter(L'/');
  8848. DebugAssert(P >= 0);
  8849. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8850. APath.SetLength(P - 1);
  8851. }
  8852. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8853. }
  8854. }
  8855. __finally
  8856. {
  8857. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8858. // Setting ItemIndex to -1 does not reset its text
  8859. if (Items->Count == 0)
  8860. {
  8861. RemotePathComboBox->Text = L"";
  8862. }
  8863. Items->EndUpdate();
  8864. }
  8865. }
  8866. }
  8867. //---------------------------------------------------------------------------
  8868. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8869. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8870. int & ImageIndex)
  8871. {
  8872. if (AIndex < 0)
  8873. {
  8874. AIndex = Sender->ItemIndex;
  8875. }
  8876. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8877. }
  8878. //---------------------------------------------------------------------------
  8879. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8880. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8881. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8882. {
  8883. ARect.Left += (10 * AIndex);
  8884. }
  8885. //---------------------------------------------------------------------------
  8886. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8887. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8888. {
  8889. AWidth += (10 * AIndex);
  8890. }
  8891. //---------------------------------------------------------------------------
  8892. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8893. TObject * Sender)
  8894. {
  8895. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8896. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8897. int Index = RemotePathComboBox->ItemIndex;
  8898. while (Index < RemotePathComboBox->Strings->Count - 1)
  8899. {
  8900. APath = UnixExtractFileDir(APath);
  8901. Index++;
  8902. }
  8903. // VanDyke style paths
  8904. if (APath.IsEmpty())
  8905. {
  8906. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8907. APath = ROOTDIRECTORY;
  8908. }
  8909. if (RemoteDirView->Path != APath)
  8910. {
  8911. RemoteDirView->Path = APath;
  8912. }
  8913. }
  8914. //---------------------------------------------------------------------------
  8915. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8916. {
  8917. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8918. DebugUsedParam(Sender);
  8919. UpdateRemotePathComboBox(true);
  8920. }
  8921. //---------------------------------------------------------------------------
  8922. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8923. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8924. {
  8925. ResetIncrementalSearch();
  8926. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8927. DebugAssert(DirView != NULL);
  8928. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8929. {
  8930. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8931. // OnEditing is called also from TCustomListView::CanEdit
  8932. if (Edit != NULL)
  8933. {
  8934. EditSelectBaseName(Edit);
  8935. }
  8936. }
  8937. }
  8938. //---------------------------------------------------------------------------
  8939. //---------------------------------------------------------------------------
  8940. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8941. {
  8942. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8943. Result->AutoDetectDnD = false;
  8944. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8945. Result->RenderDataOn = rdoEnterAndDropSync;
  8946. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8947. return Result;
  8948. }
  8949. //---------------------------------------------------------------------------
  8950. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8951. {
  8952. TForm::CreateWnd();
  8953. if (FSessionsDragDropFilesEx == NULL)
  8954. {
  8955. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8956. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8957. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8958. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8959. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8960. }
  8961. if (FQueueDragDropFilesEx == NULL)
  8962. {
  8963. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8964. // No need to set OnDragOver as we do not have any restrictions
  8965. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8966. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8967. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8968. }
  8969. }
  8970. //---------------------------------------------------------------------------
  8971. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8972. {
  8973. TForm::DestroyWnd();
  8974. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8975. FQueueDragDropFilesEx->DragDropControl = NULL;
  8976. }
  8977. //---------------------------------------------------------------------------
  8978. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8979. {
  8980. SideEnter(FCurrentSide);
  8981. FEverShown = true;
  8982. }
  8983. //---------------------------------------------------------------------------
  8984. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8985. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8986. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8987. {
  8988. if (!NonVisualDataModule->Busy)
  8989. {
  8990. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8991. Configuration->Usage->Inc(L"FileFinds");
  8992. LockWindow(true);
  8993. NonVisualDataModule->StartBusy();
  8994. WinConfiguration->LockedInterface = true;
  8995. try
  8996. {
  8997. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8998. }
  8999. __finally
  9000. {
  9001. WinConfiguration->LockedInterface = false;
  9002. NonVisualDataModule->EndBusy();
  9003. UnlockWindow();
  9004. }
  9005. }
  9006. }
  9007. //---------------------------------------------------------------------------
  9008. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9009. {
  9010. if (!NonVisualDataModule->Busy)
  9011. {
  9012. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9013. SetFocus();
  9014. RemoteDirView->Path = UnixExtractFilePath(Path);
  9015. UnicodeString FileName = UnixExtractFileName(Path);
  9016. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9017. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9018. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9019. if (Item == NULL)
  9020. {
  9021. ReloadDirectory(osRemote);
  9022. Item = RemoteDirView->FindFileItem(FileName);
  9023. }
  9024. if (Item != NULL)
  9025. {
  9026. RemoteDirView->ItemFocused = Item;
  9027. RemoteDirView->ItemFocused->MakeVisible(false);
  9028. RemoteDirView->SetFocus();
  9029. }
  9030. }
  9031. }
  9032. //---------------------------------------------------------------------------
  9033. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9034. {
  9035. bool Result = !NonVisualDataModule->Busy;
  9036. if (Result)
  9037. {
  9038. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9039. }
  9040. return Result;
  9041. }
  9042. //---------------------------------------------------------------------------
  9043. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9044. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9045. {
  9046. if (CanOperateOnFoundFiles(ATerminal))
  9047. {
  9048. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9049. FOnFileOperationFinished = OnFileOperationFinished;
  9050. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9051. }
  9052. }
  9053. //---------------------------------------------------------------------------
  9054. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9055. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9056. {
  9057. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9058. }
  9059. //---------------------------------------------------------------------------
  9060. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9061. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9062. {
  9063. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9064. }
  9065. //---------------------------------------------------------------------------
  9066. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9067. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9068. {
  9069. if (CanOperateOnFoundFiles(ATerminal))
  9070. {
  9071. for (int Index = 0; Index < FileList->Count; Index++)
  9072. {
  9073. UnicodeString FileName = FileList->Strings[Index];
  9074. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9075. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9076. OnFileOperationFinished(FileName, true);
  9077. }
  9078. }
  9079. }
  9080. //---------------------------------------------------------------------------
  9081. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9082. {
  9083. FFileFindTerminal = Terminal;
  9084. ShowFileFindDialog(
  9085. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9086. DoDownloadFoundFiles, DoEditFoundFiles);
  9087. }
  9088. //---------------------------------------------------------------------------
  9089. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9090. {
  9091. FTaskbarList = TaskbarList;
  9092. }
  9093. //---------------------------------------------------------------------------
  9094. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9095. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9096. {
  9097. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9098. if (Index >= 0)
  9099. {
  9100. if (Button == mbLeft)
  9101. {
  9102. // "Mouse down" is raised only after tab is switched.
  9103. // If switching tab (switching session) takes long enough for user
  9104. // to actually release the button, "mouse down" is still raised,
  9105. // but we do not get "mouse up" event, so dragging is not cancelled,
  9106. // prevent that by not beginning dragging in the first place.
  9107. if (IsKeyPressed(VK_LBUTTON))
  9108. {
  9109. // when user clicks the "+", we get mouse down only after the session
  9110. // is closed, when new session tab is already on X:Y, so dragging
  9111. // starts, prevent that
  9112. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9113. {
  9114. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9115. if (Terminal != NULL)
  9116. {
  9117. SessionsPageControl->BeginDrag(false);
  9118. }
  9119. }
  9120. }
  9121. }
  9122. else if (Button == mbMiddle)
  9123. {
  9124. // ignore middle-click for "New session tab"
  9125. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9126. if (Terminal != NULL)
  9127. {
  9128. CloseSessionTab(Index);
  9129. }
  9130. }
  9131. }
  9132. }
  9133. //---------------------------------------------------------------------------
  9134. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9135. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9136. {
  9137. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9138. // do not allow dropping on the "+" tab
  9139. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9140. if ((TargetTerminal != NULL) &&
  9141. (SessionsPageControl->ActivePage->PageIndex != Index))
  9142. {
  9143. Configuration->Usage->Inc(L"SessionTabMoves");
  9144. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9145. // but we want to actually prevent that to avoid flicker
  9146. SessionsPageControl->ActivePage->PageIndex = Index;
  9147. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9148. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9149. }
  9150. }
  9151. //---------------------------------------------------------------------------
  9152. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9153. TObject * Sender, TObject * Source, int X, int Y,
  9154. TDragState /*State*/, bool & Accept)
  9155. {
  9156. Accept = (Sender == Source);
  9157. if (Accept)
  9158. {
  9159. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9160. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9161. // do not allow dragging to the "+" tab
  9162. Accept = (Terminal != NULL);
  9163. }
  9164. }
  9165. //---------------------------------------------------------------------------
  9166. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9167. const TPoint & Point, int & Effect)
  9168. {
  9169. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9170. if (Index < 0)
  9171. {
  9172. Effect = DROPEFFECT_NONE;
  9173. }
  9174. else
  9175. {
  9176. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9177. // do not allow dropping on the "+" tab
  9178. if (TargetTerminal == NULL)
  9179. {
  9180. Effect = DROPEFFECT_NONE;
  9181. }
  9182. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9183. {
  9184. Effect = DROPEFFECT_COPY;
  9185. }
  9186. }
  9187. }
  9188. //---------------------------------------------------------------------------
  9189. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9190. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9191. {
  9192. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9193. // do not allow dropping on the "+" tab
  9194. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9195. if (TargetTerminal != NULL)
  9196. {
  9197. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9198. if (!IsFileControl(DropSourceControl, osRemote))
  9199. {
  9200. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9201. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9202. // Why don't we use Terminal->CurrentDirectory directly?
  9203. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9204. // do not force queue, drag drop
  9205. false, true);
  9206. }
  9207. }
  9208. }
  9209. //---------------------------------------------------------------------------
  9210. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9211. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9212. {
  9213. // Downloads are handled in RemoteFileControlDDEnd
  9214. if (!IsFileControl(DropSourceControl, osRemote))
  9215. {
  9216. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9217. Terminal->CurrentDirectory,
  9218. // force queue, drag drop
  9219. true, true);
  9220. }
  9221. }
  9222. //---------------------------------------------------------------------------
  9223. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9224. {
  9225. FShowing = false;
  9226. // Do not save empty workspace
  9227. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9228. {
  9229. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9230. UnicodeString Name = WorkspaceName();
  9231. DoSaveWorkspace(
  9232. Name, DataList.get(),
  9233. !Configuration->DisablePasswordStoring &&
  9234. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9235. WinConfiguration->LastStoredSession = Name;
  9236. }
  9237. }
  9238. //---------------------------------------------------------------------------
  9239. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9240. {
  9241. TManagedTerminal * ManagedTerminal =
  9242. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9243. if (ManagedTerminal != NULL)
  9244. {
  9245. ManagedTerminal->DirectoryLoaded = Now();
  9246. }
  9247. }
  9248. //---------------------------------------------------------------------------
  9249. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9250. TListItem * /*Item*/, bool /*Selected*/)
  9251. {
  9252. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9253. switch (DirView->LastSelectMethod)
  9254. {
  9255. case smKeyboard:
  9256. Configuration->Usage->Inc(L"KeyboardSelections");
  9257. break;
  9258. case smMouse:
  9259. Configuration->Usage->Inc(L"MouseSelections");
  9260. break;
  9261. }
  9262. }
  9263. //---------------------------------------------------------------------------
  9264. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9265. {
  9266. if (Color != 0)
  9267. {
  9268. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9269. }
  9270. else
  9271. {
  9272. return FSessionTabImageIndex;
  9273. }
  9274. }
  9275. //---------------------------------------------------------------------------
  9276. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9277. {
  9278. TPngImageCollectionItem * Item =
  9279. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9280. FSessionColors->AddPng(Item->PngImage);
  9281. return FSessionColors->Count - 1;
  9282. }
  9283. //---------------------------------------------------------------------------
  9284. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9285. {
  9286. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9287. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9288. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9289. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9290. }
  9291. //---------------------------------------------------------------------------
  9292. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9293. {
  9294. for (int Index = 0; Index < Control->ControlCount; Index++)
  9295. {
  9296. TControl * AControl = Control->Controls[Index];
  9297. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9298. if (WinControl != NULL)
  9299. {
  9300. CollectItemsWithTextDisplayMode(WinControl);
  9301. }
  9302. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9303. if (Toolbar != NULL)
  9304. {
  9305. // we care for top-level items only
  9306. TTBCustomItem * Items = Toolbar->Items;
  9307. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9308. {
  9309. TTBCustomItem * Item = Items->Items[ItemIndex];
  9310. if (((Item->DisplayMode == nbdmImageAndText) ||
  9311. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9312. EligibleForImageDisplayMode(Item))
  9313. {
  9314. FItemsWithTextDisplayMode.insert(Item);
  9315. }
  9316. }
  9317. }
  9318. }
  9319. }
  9320. //---------------------------------------------------------------------------
  9321. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9322. {
  9323. return true;
  9324. }
  9325. //---------------------------------------------------------------------------
  9326. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9327. {
  9328. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9329. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9330. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9331. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9332. bool Result = true;
  9333. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9334. {
  9335. TTBCustomItem * Item = *i;
  9336. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9337. if (Label != NULL)
  9338. {
  9339. // optimization
  9340. if (Label->Visible == SelectiveToolbarText)
  9341. {
  9342. Result = false;
  9343. }
  9344. else
  9345. {
  9346. Label->Visible = SelectiveToolbarText;
  9347. }
  9348. }
  9349. else
  9350. {
  9351. // optimization
  9352. if (Item->DisplayMode == DisplayMode)
  9353. {
  9354. Result = false;
  9355. }
  9356. else
  9357. {
  9358. Item->DisplayMode = DisplayMode;
  9359. }
  9360. }
  9361. ++i;
  9362. }
  9363. if (Result)
  9364. {
  9365. UpdateNewSessionTab();
  9366. }
  9367. return Result;
  9368. }
  9369. //---------------------------------------------------------------------------
  9370. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9371. {
  9372. DebugFail();
  9373. }
  9374. //---------------------------------------------------------------------------
  9375. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9376. {
  9377. // Among other, a lock level is non zero, while directory is being loaded,
  9378. // even when it happens as a result of dirview navigation,
  9379. // i.e. when TNonVisualDataModule is NOT FBusy.
  9380. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9381. // Among other this prevents a panel auto update to occur while
  9382. // directory is changing.
  9383. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9384. }
  9385. //---------------------------------------------------------------------------
  9386. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9387. {
  9388. // While the window is disabled, we still seem to process menu shortcuts at least,
  9389. // so stop it at least here.
  9390. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9391. return
  9392. (Allowed == aaUpdate) ||
  9393. !NonVisualDataModule->Busy;
  9394. }
  9395. //---------------------------------------------------------------------------
  9396. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9397. {
  9398. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9399. }
  9400. //---------------------------------------------------------------------------
  9401. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9402. {
  9403. // This is somewhat redundant to LockWindow() call from
  9404. // TTerminalManager::TerminalReadDirectoryProgress.
  9405. // But disabling window is known not to block keyboard shorcuts
  9406. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9407. if (Busy > 0)
  9408. {
  9409. if (NonVisualDataModule->Busy)
  9410. {
  9411. State = false;
  9412. }
  9413. else
  9414. {
  9415. NonVisualDataModule->StartBusy();
  9416. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9417. LockWindow();
  9418. }
  9419. }
  9420. else if (Busy < 0)
  9421. {
  9422. UnlockWindow();
  9423. NonVisualDataModule->EndBusy();
  9424. }
  9425. else
  9426. {
  9427. State = NonVisualDataModule->Busy;
  9428. }
  9429. }
  9430. //---------------------------------------------------------------------------
  9431. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9432. {
  9433. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9434. // no context menu for "New session tab"
  9435. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9436. {
  9437. SessionsPageControl->ActivePageIndex = Index;
  9438. if (DebugAlwaysTrue(SessionTabSwitched()))
  9439. {
  9440. // copied from TControl.WMContextMenu
  9441. SendCancelMode(SessionsPageControl);
  9442. // explicit popup instead of using PopupMenu property
  9443. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9444. // while connecting yet not-connected session and hence
  9445. // allowing an access to commands over not-completelly connected session
  9446. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9447. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9448. PopupMenu->PopupComponent = SessionsPageControl;
  9449. PopupMenu->Popup(Point.x, Point.y);
  9450. }
  9451. }
  9452. Handled = true;
  9453. }
  9454. //---------------------------------------------------------------------------
  9455. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9456. {
  9457. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9458. }
  9459. //---------------------------------------------------------------------------
  9460. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9461. {
  9462. Configuration->Usage->Inc(L"PasswordChanges");
  9463. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9464. ChangePasswordData->DisableAuthentationsExceptPassword();
  9465. ChangePasswordData->ChangePassword = true;
  9466. std::unique_ptr<TTerminal> ChangePasswordSession(
  9467. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9468. ChangePasswordSession->AutoReadDirectory = false;
  9469. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9470. {
  9471. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9472. }
  9473. else
  9474. {
  9475. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9476. }
  9477. }
  9478. //---------------------------------------------------------------------------
  9479. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9480. {
  9481. // No nice way to assert SSH2
  9482. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9483. }
  9484. //---------------------------------------------------------------------------
  9485. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9486. {
  9487. TOperationVisualizer OperationVisualizer;
  9488. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9489. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9490. }
  9491. //---------------------------------------------------------------------------
  9492. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9493. {
  9494. TForm::ChangeScale(M, D);
  9495. int APixelsPerInch = GetControlPixelsPerInch(this);
  9496. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9497. try
  9498. {
  9499. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9500. }
  9501. __finally
  9502. {
  9503. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9504. }
  9505. }
  9506. //---------------------------------------------------------------------------
  9507. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9508. {
  9509. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9510. }
  9511. //---------------------------------------------------------------------
  9512. class TPublicControl : public TControl
  9513. {
  9514. friend class TCustomScpExplorerForm;
  9515. };
  9516. //---------------------------------------------------------------------------
  9517. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9518. {
  9519. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9520. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9521. }
  9522. //---------------------------------------------------------------------------
  9523. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9524. {
  9525. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9526. OpenBookmark(Side, Bookmark);
  9527. }
  9528. //---------------------------------------------------------------------------
  9529. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9530. {
  9531. DoBookmarkClick(osLocal, Sender);
  9532. }
  9533. //---------------------------------------------------------------------------
  9534. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9535. {
  9536. DoBookmarkClick(osRemote, Sender);
  9537. }
  9538. //---------------------------------------------------------------------------
  9539. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9540. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9541. {
  9542. // TODO
  9543. if (BookmarkList != NULL)
  9544. {
  9545. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9546. if (!WinConfiguration->UseLocationProfiles)
  9547. {
  9548. std::unique_ptr<TStringList> Directories(new TStringList());
  9549. Directories->CaseSensitive = (Side == osRemote);
  9550. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9551. {
  9552. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9553. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9554. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9555. {
  9556. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9557. Item->Caption = EscapeHotkey(Directory);
  9558. Item->ShortCut = Bookmark->ShortCut;
  9559. Item->OnClick = OnBookmarkClick;
  9560. Item->Tag = reinterpret_cast<int>(Bookmark);
  9561. Directories->Add(Directory);
  9562. Menu->Add(Item.release());
  9563. }
  9564. }
  9565. }
  9566. else
  9567. {
  9568. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9569. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9570. {
  9571. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9572. if (!Bookmark->Node.IsEmpty())
  9573. {
  9574. Folders->Add(Bookmark->Node);
  9575. }
  9576. }
  9577. for (int Index = 0; Index < Folders->Count; Index++)
  9578. {
  9579. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9580. Item->Caption = Folders->Strings[Index];
  9581. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9582. Folders->Objects[Index] = Item.get();
  9583. Menu->Add(Item.release());
  9584. }
  9585. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9586. {
  9587. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9588. TTBCustomItem * Parent;
  9589. if (!Bookmark->Node.IsEmpty())
  9590. {
  9591. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9592. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9593. }
  9594. else
  9595. {
  9596. Parent = Menu;
  9597. }
  9598. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9599. Item->Caption = Bookmark->Name;
  9600. Item->ShortCut = Bookmark->ShortCut;
  9601. Item->OnClick = OnBookmarkClick;
  9602. Item->Tag = reinterpret_cast<int>(Bookmark);
  9603. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9604. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9605. {
  9606. // No hint for location profiles that are actually mere "bookmarks"
  9607. }
  9608. else
  9609. {
  9610. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9611. UnicodeString LongHint;
  9612. if (!Bookmark->Local.IsEmpty())
  9613. {
  9614. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9615. }
  9616. if (!Bookmark->Remote.IsEmpty())
  9617. {
  9618. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9619. }
  9620. AddToList(Hint, LongHint, L"|");
  9621. Item->Hint = Hint;
  9622. }
  9623. Parent->Add(Item.release());
  9624. }
  9625. }
  9626. AddMenuSeparator(Menu);
  9627. }
  9628. }
  9629. //---------------------------------------------------------------------------
  9630. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9631. {
  9632. Menu->Clear();
  9633. std::unique_ptr<TTBCustomItem> Item;
  9634. Item.reset(new TTBXItem(Owner));
  9635. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9636. Item->Options = Item->Options << tboDefault;
  9637. Menu->Add(Item.release());
  9638. AddMenuSeparator(Menu);
  9639. if (Terminal != NULL)
  9640. {
  9641. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9642. }
  9643. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9644. Item.reset(new TTBXItem(Owner));
  9645. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9646. Menu->Add(Item.release());
  9647. }
  9648. //---------------------------------------------------------------------------
  9649. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9650. {
  9651. bool Result = true;
  9652. try
  9653. {
  9654. if (Side == osRemote)
  9655. {
  9656. Terminal->ExceptionOnFail = true;
  9657. }
  9658. try
  9659. {
  9660. DirView(Side)->Path = Path;
  9661. }
  9662. __finally
  9663. {
  9664. if (Side == osRemote)
  9665. {
  9666. Terminal->ExceptionOnFail = false;
  9667. }
  9668. }
  9669. }
  9670. catch (Exception & E)
  9671. {
  9672. if ((Side != osRemote) || Terminal->Active)
  9673. {
  9674. ShowExtendedExceptionEx(Terminal, &E);
  9675. Result = false;
  9676. }
  9677. else
  9678. {
  9679. throw;
  9680. }
  9681. }
  9682. return Result;
  9683. }
  9684. //---------------------------------------------------------------------------
  9685. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9686. {
  9687. // Make sure there some feedback even when loading tiny local folders
  9688. TInstantOperationVisualizer Visualizer;
  9689. DirView(Side)->ReloadDirectory();
  9690. }
  9691. //---------------------------------------------------------------------------
  9692. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9693. {
  9694. if (Index == SessionsPageControl->TabIndex)
  9695. {
  9696. CloseSession();
  9697. }
  9698. else
  9699. {
  9700. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9701. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9702. TTerminalManager * Manager = TTerminalManager::Instance();
  9703. if (!Terminal->Active)
  9704. {
  9705. Manager->FreeTerminal(Terminal);
  9706. }
  9707. else
  9708. {
  9709. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9710. if (CanCloseQueue(Queue))
  9711. {
  9712. Manager->FreeTerminal(Terminal);
  9713. }
  9714. }
  9715. }
  9716. }
  9717. //---------------------------------------------------------------------------
  9718. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9719. {
  9720. CloseSessionTab(Index);
  9721. }
  9722. //---------------------------------------------------------------------------
  9723. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9724. {
  9725. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9726. {
  9727. TInstantOperationVisualizer Visualizer;
  9728. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9729. // before we create a new data object
  9730. ClipboardClear();
  9731. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9732. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9733. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9734. // Intermediate variable to WORKAROUND a crash in compiler
  9735. TObjectList * ClipboardFakeMonitors =
  9736. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9737. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9738. if (FClipboardDragDropFilesEx.get() == NULL)
  9739. {
  9740. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9741. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9742. }
  9743. FClipboardDragDropFilesEx->FileList->Clear();
  9744. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9745. FClipboardDragDropFilesEx->CopyToClipboard();
  9746. FClipboardTerminal = FTerminal;
  9747. // Need full paths, as cwd can be different once files are pasted
  9748. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9749. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9750. {
  9751. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9752. }
  9753. }
  9754. }
  9755. //---------------------------------------------------------------------------
  9756. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9757. {
  9758. if (OpenClipboard(0))
  9759. {
  9760. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9761. CloseClipboard();
  9762. }
  9763. }
  9764. //---------------------------------------------------------------------------
  9765. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9766. {
  9767. return !FClipboardFakeDirectory.IsEmpty();
  9768. }
  9769. //---------------------------------------------------------------------------
  9770. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9771. {
  9772. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9773. FClipboardFakeDirectory = UnicodeString();
  9774. FClipboardTerminal = NULL;
  9775. // Also called in Idle()
  9776. FClipboardFakeMonitors.reset(NULL);
  9777. }
  9778. //---------------------------------------------------------------------------
  9779. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9780. {
  9781. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9782. {
  9783. ClipboardStop();
  9784. }
  9785. }
  9786. //---------------------------------------------------------------------------
  9787. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9788. {
  9789. TTerminalManager * Manager = TTerminalManager::Instance();
  9790. Manager->ActiveTerminal = FClipboardTerminal;
  9791. TTransferOperationParam Params;
  9792. Params.TargetDirectory = TargetDirectory;
  9793. Params.DragDrop = DragDrop;
  9794. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9795. }
  9796. //---------------------------------------------------------------------------
  9797. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9798. {
  9799. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9800. // (as it pumps a Windows message queue while being shutted down)
  9801. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9802. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9803. {
  9804. // Can fail as it can be e.g. locked by AV, so we retry that later
  9805. bool Removed = RemoveDir(ApiPath(FileName));
  9806. try
  9807. {
  9808. if (!NonVisualDataModule->Busy)
  9809. {
  9810. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9811. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9812. }
  9813. }
  9814. __finally
  9815. {
  9816. if (!Removed)
  9817. {
  9818. RemoveDir(ApiPath(FileName));
  9819. }
  9820. }
  9821. }
  9822. }
  9823. //---------------------------------------------------------------------------
  9824. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9825. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9826. {
  9827. TFileMasks::TParams MaskParams;
  9828. MaskParams.Size = Size;
  9829. MaskParams.Modification = Modification;
  9830. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9831. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9832. {
  9833. bool ImplicitMatch;
  9834. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9835. !ImplicitMatch)
  9836. {
  9837. Color = Iter->Color;
  9838. break;
  9839. }
  9840. }
  9841. }
  9842. //---------------------------------------------------------------------------
  9843. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9844. {
  9845. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9846. if (!DirView->IsEditing())
  9847. {
  9848. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9849. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9850. // while searching
  9851. if (Key == VK_BACK)
  9852. {
  9853. if (!FIncrementalSearch.IsEmpty())
  9854. {
  9855. if (FIncrementalSearch.Length() == 1)
  9856. {
  9857. ResetIncrementalSearch();
  9858. }
  9859. else
  9860. {
  9861. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9862. IncrementalSearch(NewText, false, false);
  9863. }
  9864. Key = 0;
  9865. }
  9866. }
  9867. }
  9868. }
  9869. //---------------------------------------------------------------------------
  9870. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9871. {
  9872. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9873. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9874. {
  9875. // Filter control sequences.
  9876. // When not searching yet, prefer use of the space for toggling file selection
  9877. // (so we cannot incrementally search for a string starting with a space).
  9878. if ((Key > VK_SPACE) ||
  9879. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9880. {
  9881. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9882. Key = 0;
  9883. }
  9884. }
  9885. }
  9886. //---------------------------------------------------------------------------
  9887. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9888. {
  9889. if (!FIncrementalSearch.IsEmpty())
  9890. {
  9891. FIncrementalSearch = L"";
  9892. DirView(osCurrent)->UpdateStatusBar();
  9893. }
  9894. }
  9895. //---------------------------------------------------------------------------
  9896. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9897. {
  9898. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9899. if (Item == NULL)
  9900. {
  9901. MessageBeep(MB_ICONHAND);
  9902. }
  9903. else
  9904. {
  9905. TCustomDirView * ADirView = DirView(osCurrent);
  9906. {
  9907. TAutoNestingCounter Guard(FIncrementalSearching);
  9908. ADirView->FocusItem(Item);
  9909. if (ADirView->NortonLike == nlOff)
  9910. {
  9911. if ((ADirView->Selected != Item) ||
  9912. (ADirView->SelCount > 1))
  9913. {
  9914. ADirView->ClearSelection();
  9915. ADirView->Selected = Item;
  9916. }
  9917. }
  9918. }
  9919. FIncrementalSearch = Text;
  9920. Item->MakeVisible(false);
  9921. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9922. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9923. ADirView->UpdateStatusBar();
  9924. }
  9925. }
  9926. //---------------------------------------------------------------------------
  9927. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9928. {
  9929. int Index = Item->Index;
  9930. if (!Reverse)
  9931. {
  9932. Index++;
  9933. if (Index >= Item->Owner->Count)
  9934. {
  9935. Index = 0;
  9936. }
  9937. }
  9938. else
  9939. {
  9940. Index--;
  9941. if (Index < 0)
  9942. {
  9943. Index = Item->Owner->Count - 1;
  9944. }
  9945. }
  9946. return Item->Owner->Item[Index];
  9947. }
  9948. //---------------------------------------------------------------------------
  9949. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9950. {
  9951. TCustomDirView * ADirView = DirView(osCurrent);
  9952. if (ADirView->Items->Count == 0)
  9953. {
  9954. return NULL;
  9955. }
  9956. else
  9957. {
  9958. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9959. TListItem * Item = CurrentItem;
  9960. if (SkipCurrent)
  9961. {
  9962. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9963. }
  9964. while (true)
  9965. {
  9966. bool Matches = false;
  9967. switch (WinConfiguration->PanelSearch)
  9968. {
  9969. case isNameStartOnly:
  9970. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9971. break;
  9972. case isName:
  9973. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9974. break;
  9975. case isAll:
  9976. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9977. int Index = 0;
  9978. while ((Index < ColCount) && !Matches)
  9979. {
  9980. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9981. if (Visible)
  9982. {
  9983. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9984. }
  9985. Index++;
  9986. }
  9987. break;
  9988. }
  9989. if (Matches)
  9990. {
  9991. return Item;
  9992. }
  9993. Item = GetNextFile(Item, Reverse);
  9994. if (Item == CurrentItem)
  9995. {
  9996. return NULL;
  9997. }
  9998. }
  9999. }
  10000. }
  10001. //---------------------------------------------------------------------------
  10002. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10003. {
  10004. ResetIncrementalSearch();
  10005. }
  10006. //---------------------------------------------------------------------------
  10007. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10008. {
  10009. if (Message.CharCode == VK_TAB)
  10010. {
  10011. if (!FIncrementalSearch.IsEmpty())
  10012. {
  10013. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10014. bool Reverse = Shift.Contains(ssShift);
  10015. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10016. Message.Result = 1;
  10017. return;
  10018. }
  10019. }
  10020. else if (Message.CharCode == VK_ESCAPE)
  10021. {
  10022. if (!FIncrementalSearch.IsEmpty())
  10023. {
  10024. ResetIncrementalSearch();
  10025. Message.Result = 1;
  10026. return;
  10027. }
  10028. }
  10029. TForm::Dispatch(&Message);
  10030. }
  10031. //---------------------------------------------------------------------------
  10032. void __fastcall TCustomScpExplorerForm::Deactivate()
  10033. {
  10034. TForm::Deactivate();
  10035. // E.g. when switching to an internal editor window
  10036. ResetIncrementalSearch();
  10037. }
  10038. //---------------------------------------------------------------------------
  10039. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10040. {
  10041. // When switching to another application
  10042. ResetIncrementalSearch();
  10043. }
  10044. //---------------------------------------------------------------------------
  10045. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10046. {
  10047. ResetIncrementalSearch();
  10048. }
  10049. //---------------------------------------------------------------------------
  10050. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10051. {
  10052. if (FIncrementalSearching <= 0)
  10053. {
  10054. ResetIncrementalSearch();
  10055. }
  10056. }
  10057. //---------------------------------------------------------------------------
  10058. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10059. {
  10060. CountClicksForWindowPrint(this);
  10061. }
  10062. //---------------------------------------------------------------------------
  10063. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10064. {
  10065. CenterReconnectToolbar();
  10066. }
  10067. //---------------------------------------------------------------------------
  10068. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10069. {
  10070. if (RemoteDirView->ItemFocused != NULL)
  10071. {
  10072. RemoteDirView->ItemFocused->Selected = true;
  10073. }
  10074. }
  10075. //---------------------------------------------------------------------------