CustomScpExplorer.cpp 351 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712
  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. FCurrentSide = osRemote;
  153. FEverShown = false;
  154. FDocks = new TList();
  155. InitControls();
  156. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  157. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  158. NonVisualDataModule->ScpExplorer = this;
  159. RestoreParams();
  160. ConfigurationChanged();
  161. RemoteDirView->Invalidate();
  162. FAutoOperation = false;
  163. FOnFileOperationFinished = NULL;
  164. FForceExecution = false;
  165. FIgnoreNextDialogChar = 0;
  166. FErrorList = NULL;
  167. FSynchronizeProgressForm = NULL;
  168. FProgressForm = NULL;
  169. FRefreshLocalDirectory = false;
  170. FRefreshRemoteDirectory = false;
  171. FDDExtMapFile = NULL;
  172. // CreateMutexW keeps failing with ERROR_NOACCESS
  173. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  174. DebugAssert(FDDExtMutex != NULL);
  175. FDDTargetControl = NULL;
  176. FDelayedDeletionTimer = NULL;
  177. FDelayedDeletionList = new TStringList();
  178. FDDFileList = NULL;
  179. FPendingTempSpaceWarn = false;
  180. FCapturedLog = NULL;
  181. FDragDropOperation = false;
  182. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  183. FAllowTransferPresetAutoSelect = true;
  184. FCopyParamDefault = L"";
  185. FSynchronizeController = NULL;
  186. FPendingQueueActionItem = NULL;
  187. FLockLevel = 0;
  188. FLockSuspendLevel = 0;
  189. FDisabledOnLockSuspend = false;
  190. FAlternativeDelete = false;
  191. FTrayIcon = new ::TTrayIcon(0);
  192. FTrayIcon->OnClick = TrayIconClick;
  193. FMaxQueueLength = 0;
  194. FLastContextPopupScreenPoint = TPoint(-1, -1);
  195. FTransferResumeList = NULL;
  196. FMoveToQueue = false;
  197. FStandaloneEditing = false;
  198. FOnFeedSynchronizeError = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FEditorManager = new TEditorManager();
  204. FEditorManager->OnFileChange = ExecutedFileChanged;
  205. FEditorManager->OnFileReload = ExecutedFileReload;
  206. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  207. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  208. FLocalEditors = new TList();
  209. FQueueStatus = NULL;
  210. FQueueStatusSection = new TCriticalSection();
  211. FQueueStatusInvalidated = false;
  212. FQueueItemInvalidated = false;
  213. FQueueActedItem = NULL;
  214. FQueueController = new TQueueController(QueueView3);
  215. FUserActionTimer = new TTimer(this);
  216. FUserActionTimer->Enabled = false;
  217. FUserActionTimer->Interval = 10;
  218. FUserActionTimer->OnTimer = UserActionTimer;
  219. FNotes = new TStringList();
  220. FNoteTimer = new TTimer(this);
  221. FNoteTimer->Enabled = false;
  222. FNoteTimer->OnTimer = NoteTimer;
  223. FOnNoteClick = NULL;
  224. FOle32Library = LoadLibrary(L"Ole32.dll");
  225. FDragMoveCursor = FOle32Library != NULL ?
  226. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  227. UseSystemSettings(this);
  228. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  229. static_cast<TObject*>(GetComponent(fcTransferList)));
  230. DebugAssert(TransferList != NULL);
  231. FTransferListHoverIndex = -1;
  232. TransferList->OnChange = TransferListChange;
  233. TransferList->OnDrawItem = TransferListDrawItem;
  234. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  235. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  236. UseDesktopFont(SessionsPageControl);
  237. UpdateSessionsPageControlHeight();
  238. UseDesktopFont(RemoteDirView);
  239. UseDesktopFont(RemoteDriveView);
  240. UseDesktopFont(QueueView3);
  241. UseDesktopFont(QueueLabel);
  242. UseDesktopFont(RemoteStatusBar);
  243. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  244. QueueSplitter->ShowHint = true;
  245. RemotePanelSplitter->ShowHint = true;
  246. UpdateImages();
  247. FCustomCommandMenu = CreateTBXPopupMenu(this);
  248. FCustomCommandLocalFileList = NULL;
  249. FCustomCommandRemoteFileList = NULL;
  250. FSessionColors = new TPngImageList(this);
  251. FSessionColors->ColorDepth = cd32Bit;
  252. AddFixedSessionImages();
  253. SessionsPageControl->Images = FSessionColors;
  254. QueueLabel->FocusControl = QueueView3;
  255. UpdateQueueLabel();
  256. FRemoteDirViewWasFocused = true;
  257. CreateHiddenWindow();
  258. StartUpdates();
  259. }
  260. //---------------------------------------------------------------------------
  261. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  262. {
  263. // this has to be one of the very first things to do
  264. StopUpdateThread();
  265. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  266. TTerminalManager::Instance()->ScpExplorer = NULL;
  267. delete FCustomCommandLocalFileList;
  268. delete FCustomCommandRemoteFileList;
  269. delete FCustomCommandMenu;
  270. delete FTrayIcon;
  271. FTrayIcon = NULL;
  272. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  273. delete FEditorManager;
  274. ForceCloseLocalEditors();
  275. delete FLocalEditors;
  276. HideFileFindDialog();
  277. if (FDelayedDeletionTimer)
  278. {
  279. DoDelayedDeletion(NULL);
  280. SAFE_DESTROY(FDelayedDeletionTimer);
  281. }
  282. SAFE_DESTROY(FDelayedDeletionList);
  283. // sometimes we do not get DDEnd so the list is not released
  284. SAFE_DESTROY(FDDFileList);
  285. DebugAssert(FSynchronizeController == NULL);
  286. CloseHandle(FDDExtMutex);
  287. FDDExtMutex = NULL;
  288. FreeLibrary(FOle32Library);
  289. FOle32Library = NULL;
  290. FDragMoveCursor = NULL;
  291. DebugAssert(!FErrorList);
  292. if (FEverShown)
  293. {
  294. // when window is never shown (like when running command-line operation),
  295. // particularly window site is not restored correctly (BoundsRect value set
  296. // in RestoreForm gets lost during handle allocation), so we do not want
  297. // it to be stored
  298. StoreParams();
  299. }
  300. Terminal = NULL;
  301. Queue = NULL;
  302. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  303. NonVisualDataModule->ScpExplorer = NULL;
  304. delete FQueueController;
  305. FQueueController = NULL;
  306. delete FQueueStatusSection;
  307. FQueueStatusSection = NULL;
  308. delete FQueueStatus;
  309. FQueueStatus = NULL;
  310. delete FUserActionTimer;
  311. FUserActionTimer = NULL;
  312. delete FNoteTimer;
  313. delete FNotes;
  314. delete FNoteData;
  315. SAFE_DESTROY(FDocks);
  316. SessionsPageControl->Images = NULL;
  317. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  318. SAFE_DESTROY(FSessionColors);
  319. SAFE_DESTROY(FSessionsDragDropFilesEx);
  320. SAFE_DESTROY(FHistoryMenu[0][0]);
  321. SAFE_DESTROY(FHistoryMenu[0][1]);
  322. SAFE_DESTROY(FHistoryMenu[1][0]);
  323. SAFE_DESTROY(FHistoryMenu[1][1]);
  324. if (FHiddenWindow != NULL)
  325. {
  326. DestroyWindow(FHiddenWindow);
  327. FHiddenWindow = NULL;
  328. }
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  332. {
  333. std::unique_ptr<TSessionData> Data;
  334. if (!Session.IsEmpty())
  335. {
  336. bool DefaultsOnly;
  337. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  338. }
  339. TTerminalManager * Manager = TTerminalManager::Instance();
  340. for (int Index = 0; Index < Manager->Count; Index++)
  341. {
  342. TTerminal * Terminal = Manager->Terminals[Index];
  343. if (Session.IsEmpty() ||
  344. Terminal->SessionData->IsSameSite(Data.get()))
  345. {
  346. if (Path.IsEmpty())
  347. {
  348. Terminal->ClearCaches();
  349. }
  350. else
  351. {
  352. Terminal->DirectoryModified(Path, true);
  353. }
  354. }
  355. }
  356. // We should flag a pending refresh for the background terminals or busy foreground terminals
  357. if ((Terminal != NULL) && Terminal->Active &&
  358. CanCommandLineFromAnotherInstance() &&
  359. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  360. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  361. {
  362. Terminal->ReloadDirectory();
  363. }
  364. }
  365. //---------------------------------------------------------------------------
  366. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  367. {
  368. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  369. size_t MessageSize = sizeof(TCopyDataMessage);
  370. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  371. if (Result)
  372. {
  373. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  374. Result = (Message.Version == TCopyDataMessage::Version1);
  375. if (Result)
  376. {
  377. switch (Message.Command)
  378. {
  379. case TCopyDataMessage::CommandCanCommandLine:
  380. Result = CanCommandLineFromAnotherInstance();
  381. break;
  382. case TCopyDataMessage::CommandCommandLine:
  383. {
  384. UnicodeString CommandLine(Message.CommandLine);
  385. Result = CommandLineFromAnotherInstance(CommandLine);
  386. }
  387. break;
  388. case TCopyDataMessage::RefreshPanel:
  389. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  390. break;
  391. case TCopyDataMessage::MainWindowCheck:
  392. Result = true;
  393. break;
  394. default:
  395. Result = false;
  396. break;
  397. }
  398. }
  399. }
  400. Message.Result = Result ? 1 : 0;
  401. }
  402. //---------------------------------------------------------------------------
  403. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  404. {
  405. WNDCLASS WindowClass = {0};
  406. WindowClass.lpfnWndProc = DefWindowProc;
  407. WindowClass.hInstance = HInstance;
  408. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  409. FHiddenWindow = NULL;
  410. if (RegisterClass(&WindowClass))
  411. {
  412. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  413. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  414. }
  415. }
  416. //---------------------------------------------------------------------------
  417. bool __fastcall TCustomScpExplorerForm::CanConsole()
  418. {
  419. return
  420. (Terminal != NULL) &&
  421. Terminal->Active &&
  422. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  426. {
  427. bool Result = Visible && !NonVisualDataModule->Busy;
  428. return Result;
  429. }
  430. //---------------------------------------------------------------------------
  431. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  432. const UnicodeString & CommandLine)
  433. {
  434. TProgramParams Params(CommandLine);
  435. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  436. if (Result)
  437. {
  438. NonVisualDataModule->StartBusy();
  439. try
  440. {
  441. // this action is initiated from another process,
  442. // so it's likely that our window is not visible,
  443. // and user won't see what is going on
  444. Application->BringToFront();
  445. // reload sessions as we may be asked to open a session
  446. // just stored by another instance
  447. StoredSessions->Reload();
  448. UnicodeString SessionName = Params.Param[1];
  449. std::unique_ptr<TObjectList> DataList(new TObjectList());
  450. UnicodeString DownloadFile; // unused
  451. try
  452. {
  453. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  454. }
  455. catch (EAbort &)
  456. {
  457. // Happens when opening session in PuTTY and in other situations,
  458. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  459. // to process the command-line on its own (or to pass it to yet another instance)
  460. }
  461. if (DataList->Count > 0)
  462. {
  463. TTerminalManager * Manager = TTerminalManager::Instance();
  464. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  465. }
  466. }
  467. __finally
  468. {
  469. NonVisualDataModule->EndBusy();
  470. }
  471. }
  472. return Result;
  473. }
  474. //---------------------------------------------------------------------------
  475. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  476. {
  477. if (FTerminal != value)
  478. {
  479. TerminalChanging();
  480. DoSetTerminal(value, false);
  481. }
  482. }
  483. //---------------------------------------------------------------------------
  484. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  485. {
  486. FTerminal = value;
  487. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  488. FAllowTransferPresetAutoSelect = false;
  489. try
  490. {
  491. TerminalChanged(Replace);
  492. }
  493. __finally
  494. {
  495. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  496. }
  497. if (Terminal != NULL)
  498. {
  499. TransferPresetAutoSelect();
  500. }
  501. }
  502. //---------------------------------------------------------------------------
  503. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  504. {
  505. DoSetTerminal(value, true);
  506. }
  507. //---------------------------------------------------------------------------
  508. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  509. {
  510. if (FTerminal != NULL)
  511. {
  512. UpdateTerminal(Terminal);
  513. }
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  517. {
  518. if (Terminal != NULL)
  519. {
  520. UpdateSessionColor((TColor)Terminal->StateData->Color);
  521. }
  522. DoTerminalListChanged();
  523. DebugAssert(!IsLocalBrowserMode()); // TODO
  524. if (Replaced)
  525. {
  526. RemoteDirView->ReplaceTerminal(Terminal);
  527. }
  528. else
  529. {
  530. RemoteDirView->Terminal = Terminal;
  531. }
  532. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  533. if (Terminal != NULL)
  534. {
  535. if (Terminal->Active)
  536. {
  537. Terminal->RefreshDirectory();
  538. }
  539. if (WinConfiguration->PreservePanelState)
  540. {
  541. if (Terminal->RemoteExplorerState != NULL)
  542. {
  543. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  544. }
  545. else
  546. {
  547. DirView(osRemote)->ClearState();
  548. }
  549. }
  550. if (!Replaced && Terminal->Active)
  551. {
  552. InitStatusBar();
  553. }
  554. }
  555. UpdateTransferList();
  556. // Update panels Enable state before refreshing the labels
  557. UpdateControls();
  558. }
  559. //---------------------------------------------------------------------------
  560. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  561. {
  562. if (Queue != value)
  563. {
  564. if (FQueue != NULL)
  565. {
  566. FQueue->OnListUpdate = NULL;
  567. FQueue->OnQueueItemUpdate = NULL;
  568. }
  569. FQueue = value;
  570. if (FQueue != NULL)
  571. {
  572. DebugAssert(FQueue->OnListUpdate == NULL);
  573. FQueue->OnListUpdate = QueueListUpdate;
  574. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  575. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  576. }
  577. QueueChanged();
  578. }
  579. }
  580. //---------------------------------------------------------------------------
  581. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  582. TListItem * Item)
  583. {
  584. if (FQueueActedItem == Item)
  585. {
  586. FQueueActedItem = NULL;
  587. if ((QueueView3->PopupMenu != NULL) &&
  588. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  589. {
  590. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  591. ReleaseCapture();
  592. }
  593. }
  594. if (Item->Data == FPendingQueueActionItem)
  595. {
  596. FPendingQueueActionItem = NULL;
  597. }
  598. }
  599. //---------------------------------------------------------------------------
  600. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  601. {
  602. {
  603. TGuard Guard(FQueueStatusSection);
  604. FQueueStatusInvalidated = false;
  605. if (FQueue != NULL)
  606. {
  607. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  608. }
  609. }
  610. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  611. {
  612. FMaxQueueLength = FQueueStatus->Count;
  613. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  614. }
  615. FQueueController->UpdateQueueStatus(FQueueStatus);
  616. SetQueueProgress();
  617. UpdateQueueView();
  618. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  619. if (IsEmpty && (Terminal != NULL))
  620. {
  621. TOnceDoneOperation OnceDoneOperation =
  622. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  623. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  624. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  625. {
  626. FQueue->Enabled = false;
  627. }
  628. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  629. {
  630. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  631. }
  632. }
  633. }
  634. //---------------------------------------------------------------------------
  635. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  636. {
  637. UnicodeString Result;
  638. if (FQueueStatus != NULL)
  639. {
  640. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  641. {
  642. TFileOperationProgressType * ProgressData =
  643. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  644. if ((ProgressData != NULL) && ProgressData->InProgress)
  645. {
  646. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  647. }
  648. }
  649. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  650. {
  651. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  652. }
  653. }
  654. return Result;
  655. }
  656. //---------------------------------------------------------------------------
  657. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  658. {
  659. ComponentVisible[fcQueueView] =
  660. (WinConfiguration->QueueView.Show == qvShow) ||
  661. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  662. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TCustomScpExplorerForm::QueueChanged()
  666. {
  667. if (FQueueStatus != NULL)
  668. {
  669. delete FQueueStatus;
  670. FQueueStatus = NULL;
  671. }
  672. UpdateQueueStatus(true);
  673. }
  674. //---------------------------------------------------------------------------
  675. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  676. {
  677. if (FQueue == Queue)
  678. {
  679. FQueueStatusInvalidated = true;
  680. }
  681. }
  682. //---------------------------------------------------------------------------
  683. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  684. TQueueItem * Item)
  685. {
  686. if (FQueue == Queue)
  687. {
  688. TGuard Guard(FQueueStatusSection);
  689. // this may be running in parallel with QueueChanged
  690. if (FQueueStatus != NULL)
  691. {
  692. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  693. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  694. {
  695. FRefreshLocalDirectory = (QueueItem == NULL) ||
  696. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  697. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  698. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  699. }
  700. if (QueueItem != NULL)
  701. {
  702. QueueItem->UserData = (void*)true;
  703. FQueueItemInvalidated = true;
  704. }
  705. }
  706. }
  707. }
  708. //---------------------------------------------------------------------------
  709. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  710. {
  711. // during standalone editing, we have no way to see/control queue,
  712. // so we have to always popup prompts automatically
  713. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  717. {
  718. if (FQueueStatus != NULL)
  719. {
  720. bool QueueAutoPopup = IsQueueAutoPopup();
  721. bool NeedRefresh = FQueueController->NeedRefresh();
  722. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  723. FQueueItemInvalidated = false;
  724. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  725. bool Updated = false;
  726. TQueueItemProxy * QueueItem;
  727. bool WasUserAction;
  728. for (int Index = 0; Index < Limit; Index++)
  729. {
  730. bool Update = false;
  731. QueueItem = FQueueStatus->Items[Index];
  732. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  733. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  734. (FPendingQueueActionItem == NULL))
  735. {
  736. FPendingQueueActionItem = QueueItem;
  737. FUserActionTimer->Enabled = true;
  738. }
  739. if ((bool)QueueItem->UserData)
  740. {
  741. QueueItem->UserData = (void*)false;
  742. QueueItem->Update();
  743. Updated = true;
  744. Update = true;
  745. }
  746. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  747. {
  748. Update = true;
  749. }
  750. if (Update)
  751. {
  752. FQueueController->RefreshQueueItem(QueueItem);
  753. }
  754. }
  755. if (Updated)
  756. {
  757. NonVisualDataModule->UpdateNonVisibleActions();
  758. SetQueueProgress();
  759. }
  760. }
  761. }
  762. //---------------------------------------------------------------------------
  763. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  764. {
  765. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  766. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  767. }
  768. //---------------------------------------------------------------------------
  769. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  770. {
  771. if (Progress >= 0)
  772. {
  773. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  774. }
  775. else
  776. {
  777. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  778. }
  779. }
  780. //---------------------------------------------------------------------------
  781. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  782. {
  783. int OverallProgress;
  784. // FProgressForm is null when this is called from SetQueueProgress
  785. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  786. {
  787. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  788. }
  789. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  790. {
  791. OverallProgress = ProgressData->OverallProgress();
  792. }
  793. else
  794. {
  795. OverallProgress = -1;
  796. }
  797. SetTaskbarListProgressValue(OverallProgress);
  798. }
  799. //---------------------------------------------------------------------------
  800. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  801. {
  802. TTerminalManager::Instance()->QueueStatusUpdated();
  803. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  804. {
  805. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  806. {
  807. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  808. {
  809. TFileOperationProgressType * ProgressData = NULL;
  810. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  811. {
  812. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  813. }
  814. if ((ProgressData != NULL) &&
  815. ProgressData->InProgress)
  816. {
  817. SetTaskbarListProgressValue(ProgressData);
  818. }
  819. else
  820. {
  821. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  822. }
  823. }
  824. else
  825. {
  826. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  827. }
  828. }
  829. else
  830. {
  831. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  832. }
  833. }
  834. UpdateQueueLabel();
  835. }
  836. //---------------------------------------------------------------------------
  837. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  838. {
  839. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  840. if (FQueueStatus != NULL)
  841. {
  842. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  843. if (ActiveAndPendingPrimaryCount > 0)
  844. {
  845. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  846. }
  847. }
  848. QueueLabel->Caption = Caption;
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  852. {
  853. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  854. static_cast<TComponent*>(GetComponent(fcTransferList)));
  855. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  856. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  857. TransferList->Strings->BeginUpdate();
  858. try
  859. {
  860. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  861. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  862. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  863. if (FTransferDropDownHint.IsEmpty())
  864. {
  865. FTransferDropDownHint = TransferDropDown->Hint;
  866. }
  867. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  868. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  869. TransferDropDown->Text = StripHotkey(Name);
  870. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  871. (FTransferDropDownHint, StripHotkey(Name),
  872. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  873. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  874. // update the label, otherwise when it is updated only on the first draw
  875. // of the list, it is drawn "bold" for some reason
  876. FTransferListHoverIndex = TransferList->ItemIndex;
  877. UpdateTransferLabel();
  878. }
  879. __finally
  880. {
  881. TransferList->Strings->EndUpdate();
  882. }
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  886. {
  887. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  888. DebugAssert(Toolbar != NULL);
  889. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  890. }
  891. //---------------------------------------------------------------------------
  892. void __fastcall TCustomScpExplorerForm::UpdateActions()
  893. {
  894. TForm::UpdateActions();
  895. if (ComponentVisible[fcCustomCommandsBand])
  896. {
  897. UpdateCustomCommandsToolbar();
  898. }
  899. }
  900. //---------------------------------------------------------------------------
  901. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  902. {
  903. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  907. {
  908. Color = GetBtnFaceColor();
  909. DebugAssert(Configuration && RemoteDirView);
  910. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  911. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  912. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  913. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  914. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  915. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  916. {
  917. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  918. // selection is not redrawn automatically when RowSelect changes
  919. RemoteDirView->Invalidate();
  920. }
  921. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  922. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  923. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  924. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  925. UpdateSessionsPageControlHeight();
  926. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  927. UpdateToolbarDisplayMode();
  928. UpdateTransferList();
  929. if (Terminal != NULL)
  930. {
  931. TransferPresetAutoSelect();
  932. }
  933. UpdateQueueView();
  934. UpdateControls();
  935. // this can be called even before constuctor finishes.
  936. if (FEditorManager != NULL)
  937. {
  938. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  939. }
  940. // this can be called even before constuctor finishes.
  941. if (FLocalEditors != NULL)
  942. {
  943. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  944. {
  945. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  946. }
  947. }
  948. if (ComponentVisible[fcCustomCommandsBand])
  949. {
  950. UpdateCustomCommandsToolbar();
  951. }
  952. // show only when keeping queue items forever,
  953. // otherwise, its enough to have in in the context menu
  954. QueueDeleteAllDoneQueueToolbarItem->Visible =
  955. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  956. if (FFileColorsCurrent != WinConfiguration->FileColors)
  957. {
  958. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  959. FFileColorsCurrent = WinConfiguration->FileColors;
  960. FileColorsChanged();
  961. }
  962. }
  963. //---------------------------------------------------------------------------
  964. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  965. {
  966. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  967. DirView->Invalidate();
  968. }
  969. //---------------------------------------------------------------------------
  970. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  971. {
  972. DoFileColorsChanged(RemoteDirView);
  973. }
  974. //---------------------------------------------------------------------------
  975. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  976. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  977. void * /*Arg*/)
  978. {
  979. if (Token != NULL)
  980. {
  981. TForm * Editor = dynamic_cast<TForm*>(Token);
  982. DebugAssert(Editor != NULL);
  983. ReconfigureEditorForm(Editor);
  984. }
  985. }
  986. //---------------------------------------------------------------------------
  987. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  988. {
  989. WinConfiguration->DDTransferConfirmation = asOn;
  990. }
  991. //---------------------------------------------------------------------------
  992. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  993. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  994. {
  995. // noop
  996. }
  997. //---------------------------------------------------------------------------
  998. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  999. TTransferDirection Direction, TTransferType Type, bool Temp,
  1000. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1001. bool Confirm, bool DragDrop, int Options)
  1002. {
  1003. bool Result = true;
  1004. DebugAssert(Terminal && Terminal->Active);
  1005. // these parameters are known in advance
  1006. int Params =
  1007. FLAGMASK(Type == ttMove, cpDelete);
  1008. bool ToTemp = (Temp && (Direction == tdToLocal));
  1009. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1010. {
  1011. bool DisableNewerOnly =
  1012. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1013. ToTemp;
  1014. Options |=
  1015. FLAGMASK(ToTemp, coTemp) |
  1016. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1017. coDoNotShowAgain;
  1018. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1019. int CopyParamAttrs = (Direction == tdToRemote ?
  1020. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1021. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1022. int OutputOptions =
  1023. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1024. cooDoNotShowAgain);
  1025. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1026. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1027. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1028. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1029. if (Result)
  1030. {
  1031. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1032. {
  1033. if (DragDrop)
  1034. {
  1035. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1036. {
  1037. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1038. NULL, 0, EnableDDTransferConfirmation, NULL);
  1039. }
  1040. WinConfiguration->DDTransferConfirmation = asOff;
  1041. }
  1042. else
  1043. {
  1044. WinConfiguration->ConfirmTransferring = false;
  1045. }
  1046. }
  1047. else
  1048. {
  1049. // User explicitly unchecked "do not show again",
  1050. // so show him the dialog the next time
  1051. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1052. {
  1053. WinConfiguration->DDTransferConfirmation = asOn;
  1054. }
  1055. }
  1056. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1057. }
  1058. }
  1059. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1060. {
  1061. Configuration->Usage->Inc(L"TransfersOnBackground");
  1062. // these parameter are known only after transfer dialog
  1063. Params |=
  1064. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1065. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1066. Result = false;
  1067. ClearTransferSourceSelection(Direction);
  1068. }
  1069. return Result;
  1070. }
  1071. //---------------------------------------------------------------------------
  1072. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1073. {
  1074. if (FOnFileOperationFinished != NULL)
  1075. {
  1076. FOnFileOperationFinished(UnicodeString(), true);
  1077. }
  1078. else
  1079. {
  1080. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1081. if (HasDirView[Side])
  1082. {
  1083. DirView(Side)->SelectAll(smNone);
  1084. }
  1085. }
  1086. }
  1087. //---------------------------------------------------------------------------
  1088. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1089. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1090. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1091. int Params)
  1092. {
  1093. DebugAssert(Queue != NULL);
  1094. bool SingleFile = false;
  1095. if (FileList->Count == 1)
  1096. {
  1097. if (Direction == tdToRemote)
  1098. {
  1099. UnicodeString FileName = FileList->Strings[0];
  1100. SingleFile = FileExists(ApiPath(FileName));
  1101. }
  1102. else
  1103. {
  1104. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1105. SingleFile = !File->IsDirectory;
  1106. }
  1107. }
  1108. TQueueItem * QueueItem;
  1109. if (Direction == tdToRemote)
  1110. {
  1111. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1112. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1113. }
  1114. else
  1115. {
  1116. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1117. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1118. }
  1119. AddQueueItem(Queue, QueueItem, Terminal);
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1123. {
  1124. if (Queue->IsEmpty)
  1125. {
  1126. Terminal->QueueOperationStart = Now();
  1127. }
  1128. Queue->AddItem(QueueItem);
  1129. }
  1130. //---------------------------------------------------------------------------
  1131. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1132. {
  1133. }
  1134. //---------------------------------------------------------------------------
  1135. void __fastcall TCustomScpExplorerForm::InitControls()
  1136. {
  1137. DebugAssert(FDocks != NULL);
  1138. for (int Index = 0; Index < ComponentCount; Index++)
  1139. {
  1140. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1141. if ((Dock != NULL) && (Dock->Tag == 0))
  1142. {
  1143. FDocks->Add(Dock);
  1144. }
  1145. }
  1146. CollectItemsWithTextDisplayMode(this);
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1150. {
  1151. ConfigurationChanged();
  1152. QueuePanel->Height =
  1153. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1154. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1155. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1156. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1157. UpdateCustomCommandsToolbar();
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. void __fastcall TCustomScpExplorerForm::StoreParams()
  1161. {
  1162. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1163. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1164. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1165. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1166. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1170. {
  1171. if (!FFormRestored)
  1172. {
  1173. FFormRestored = true;
  1174. RestoreFormParams();
  1175. }
  1176. TForm::CreateParams(Params);
  1177. }
  1178. //---------------------------------------------------------------------------
  1179. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1180. {
  1181. DebugAssert(FDocks != NULL);
  1182. for (int Index = 0; Index < FDocks->Count; Index++)
  1183. {
  1184. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1185. }
  1186. }
  1187. //---------------------------------------------------------------------------
  1188. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1189. {
  1190. SetDockAllowDrag(true);
  1191. while (!ButtonsStr.IsEmpty())
  1192. {
  1193. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1194. UnicodeString Name = CutToChar(S, L'=', true);
  1195. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1196. {
  1197. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1198. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1199. {
  1200. while (!S.IsEmpty())
  1201. {
  1202. UnicodeString S2 = CutToChar(S, L',', true);
  1203. UnicodeString Name = CutToChar(S2, L':', true);
  1204. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1205. {
  1206. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1207. if (SameText(GetToolbarItemName(Item), Name))
  1208. {
  1209. // forward compatibility
  1210. UnicodeString S3 = CutToChar(S2, L':', true);
  1211. bool Visible = StrToIntDef(S3, 0);
  1212. Item->Visible = Visible;
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. }
  1219. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1220. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1221. }
  1222. //---------------------------------------------------------------------------
  1223. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1224. {
  1225. UnicodeString Result;
  1226. SetDockAllowDrag(true);
  1227. Result = ::GetToolbarsLayoutStr(this);
  1228. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1229. return Result;
  1230. }
  1231. //---------------------------------------------------------------------------
  1232. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1233. {
  1234. UnicodeString Result;
  1235. if (Item->Action != NULL)
  1236. {
  1237. Result = Item->Action->Name;
  1238. Result = RemoveSuffix(Result, L"Action");
  1239. }
  1240. else
  1241. {
  1242. Result = Item->Name;
  1243. Result = RemoveSuffix(Result, L"SubmenuItem");
  1244. Result = RemoveSuffix(Result, L"Item");
  1245. }
  1246. return Result;
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1250. {
  1251. UnicodeString Result;
  1252. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1253. {
  1254. UnicodeString ToolbarStr;
  1255. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1256. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1257. {
  1258. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1259. {
  1260. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1261. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1262. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1263. // and save the changes here only.
  1264. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1265. {
  1266. UnicodeString Name = GetToolbarItemName(Item);
  1267. DebugAssert(Name.Pos(L"TBX") == 0);
  1268. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1269. }
  1270. }
  1271. }
  1272. if (!ToolbarStr.IsEmpty())
  1273. {
  1274. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1275. }
  1276. }
  1277. return Result;
  1278. }
  1279. //---------------------------------------------------------------------------
  1280. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1281. {
  1282. DebugAssert(FProgressForm == NULL);
  1283. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1284. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1285. FProgressForm->DeleteLocalToRecycleBin =
  1286. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1287. FProgressForm->DeleteRemoteToRecycleBin =
  1288. (Terminal != NULL) &&
  1289. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1290. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1291. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1292. LockWindow();
  1293. if (FTaskbarList != NULL)
  1294. {
  1295. // Actually, do not know what hides the progress once the operation finishes
  1296. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1297. SetTaskbarListProgressState(TBPF_NORMAL);
  1298. }
  1299. }
  1300. //---------------------------------------------------------------------------
  1301. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1302. {
  1303. UnlockWindow();
  1304. SAFE_DESTROY(FProgressForm);
  1305. SetQueueProgress();
  1306. }
  1307. //---------------------------------------------------------------------------
  1308. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1309. TFileOperationProgressType & ProgressData)
  1310. {
  1311. // Download to temporary local directory
  1312. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1313. {
  1314. bool Continue = true;
  1315. FPendingTempSpaceWarn = false;
  1316. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1317. if (!Continue)
  1318. {
  1319. Abort();
  1320. }
  1321. }
  1322. // operation is being executed and we still didn't show up progress form
  1323. if (ProgressData.InProgress && !FProgressForm)
  1324. {
  1325. CreateProgressForm(NULL);
  1326. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1327. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1328. }
  1329. // operation is finished (or terminated), so we hide progress form
  1330. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1331. {
  1332. DestroyProgressForm();
  1333. if (ProgressData.Operation == foCalculateSize)
  1334. {
  1335. // When calculating size before transfer, the abort caused by
  1336. // cancel flag set due to "MoveToQueue" below,
  1337. // is not propagated back to us in ExecuteFileOperation,
  1338. // as it is silently swallowed.
  1339. // So we explicitly (re)throw it here.
  1340. if (FMoveToQueue)
  1341. {
  1342. Abort();
  1343. }
  1344. }
  1345. else
  1346. {
  1347. if ((ProgressData.Cancel == csContinue) &&
  1348. !FAutoOperation)
  1349. {
  1350. OperationComplete(ProgressData.StartTime);
  1351. }
  1352. }
  1353. }
  1354. if (FProgressForm &&
  1355. // Can happen only during synchronization, when ending one of the operations
  1356. ProgressData.InProgress)
  1357. {
  1358. FProgressForm->SetProgressData(ProgressData);
  1359. if (FTaskbarList != NULL)
  1360. {
  1361. DebugAssert(ProgressData.InProgress);
  1362. SetTaskbarListProgressValue(&ProgressData);
  1363. }
  1364. if (FProgressForm->Cancel > csContinue)
  1365. {
  1366. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1367. if (FProgressForm->Cancel == csCancelFile)
  1368. {
  1369. FProgressForm->ClearCancel();
  1370. }
  1371. // cancel cancels even the move
  1372. FMoveToQueue = false;
  1373. }
  1374. else if (FProgressForm->MoveToQueue)
  1375. {
  1376. FMoveToQueue = true;
  1377. ProgressData.SetCancelAtLeast(csCancel);
  1378. }
  1379. if ((FTransferResumeList != NULL) &&
  1380. ProgressData.InProgress &&
  1381. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1382. !ProgressData.FullFileName.IsEmpty())
  1383. {
  1384. if ((FTransferResumeList->Count == 0) ||
  1385. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1386. {
  1387. // note that we do not recognize directories from files here
  1388. FTransferResumeList->Add(ProgressData.FullFileName);
  1389. }
  1390. }
  1391. }
  1392. }
  1393. //---------------------------------------------------------------------------
  1394. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1395. const TDateTime & StartTime)
  1396. {
  1397. if (GUIConfiguration->BeepOnFinish &&
  1398. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1399. {
  1400. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1401. DWORD Sound;
  1402. if (!ExtractFileExt(BeepSound).IsEmpty())
  1403. {
  1404. Sound = SND_FILENAME;
  1405. }
  1406. else
  1407. {
  1408. Sound = SND_ALIAS;
  1409. }
  1410. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1411. }
  1412. }
  1413. //---------------------------------------------------------------------------
  1414. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1415. TFileOperationProgressType & ProgressData)
  1416. {
  1417. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1418. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1419. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1420. FileOperationProgress(ProgressData);
  1421. }
  1422. //---------------------------------------------------------------------------
  1423. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1424. {
  1425. UnicodeString Result;
  1426. if (FProgressForm != NULL)
  1427. {
  1428. Result = FProgressForm->ProgressStr();
  1429. }
  1430. return Result;
  1431. }
  1432. //---------------------------------------------------------------------------
  1433. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1434. bool DragDrop)
  1435. {
  1436. return (!DragDrop && (DropSourceControl == NULL)) ||
  1437. (DropSourceControl == DirView(osOther));
  1438. }
  1439. //---------------------------------------------------------------------------
  1440. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1441. TFileOperation Operation, TOperationSide Side,
  1442. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1443. TOnceDoneOperation & OnceDoneOperation)
  1444. {
  1445. if (!FAutoOperation)
  1446. {
  1447. // no selection on "/upload", form servers only as event handler
  1448. // (it is not displayed)
  1449. if (PanelOperation(Side, FDragDropOperation) &&
  1450. Visible && (Operation != foCalculateSize) &&
  1451. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1452. {
  1453. if (FOnFileOperationFinished != NULL)
  1454. {
  1455. FOnFileOperationFinished(FileName, Success);
  1456. }
  1457. else
  1458. {
  1459. TCustomDirView * DView = DirView(Side);
  1460. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1461. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1462. // this can happen when local drive is unplugged in the middle of the operation
  1463. if (Item != NULL)
  1464. {
  1465. if (Success) Item->Selected = false;
  1466. DView->MakeProgressVisible(Item);
  1467. }
  1468. }
  1469. }
  1470. if ((Operation == foCopy) || (Operation == foMove))
  1471. {
  1472. DebugAssert(!IsLocalBrowserMode()); // TODO
  1473. if (Side == osLocal)
  1474. {
  1475. Configuration->Usage->Inc(L"UploadedFiles");
  1476. }
  1477. else
  1478. {
  1479. Configuration->Usage->Inc(L"DownloadedFiles");
  1480. }
  1481. }
  1482. }
  1483. if (Success && (FSynchronizeController != NULL))
  1484. {
  1485. DebugAssert(!IsSideLocalBrowser(osRemote));
  1486. if (Operation == foCopy)
  1487. {
  1488. DebugAssert(Side == osLocal);
  1489. FSynchronizeController->LogOperation(soUpload, FileName);
  1490. }
  1491. else if (Operation == foDelete)
  1492. {
  1493. DebugAssert(Side == osRemote);
  1494. FSynchronizeController->LogOperation(soDelete, FileName);
  1495. }
  1496. }
  1497. if (FProgressForm)
  1498. {
  1499. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1500. }
  1501. }
  1502. //---------------------------------------------------------------------------
  1503. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1504. TFileOperation Operation, TOperationSide Side,
  1505. bool Temp, const UnicodeString & FileName, Boolean Success,
  1506. TOnceDoneOperation & OnceDoneOperation)
  1507. {
  1508. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1509. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1510. OnceDoneOperation);
  1511. }
  1512. //---------------------------------------------------------------------------
  1513. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1514. {
  1515. return false;
  1516. }
  1517. //---------------------------------------------------------------------------
  1518. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1519. {
  1520. return false;
  1521. }
  1522. //---------------------------------------------------------------------------
  1523. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1524. {
  1525. return NULL;
  1526. }
  1527. //---------------------------------------------------------------------------
  1528. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1529. {
  1530. DebugAssert(GetSide(Side) == osRemote);
  1531. DebugUsedParam(Side);
  1532. return RemoteDirView;
  1533. }
  1534. //---------------------------------------------------------------------------
  1535. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1536. {
  1537. DebugAssert(GetSide(Side) == osRemote);
  1538. DebugUsedParam(Side);
  1539. return RemoteDriveView;
  1540. }
  1541. //---------------------------------------------------------------------------
  1542. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1543. {
  1544. DebugAssert(GetSide(Side) == osRemote);
  1545. DebugUsedParam(Side);
  1546. return (Terminal != NULL) && Terminal->Active;
  1547. }
  1548. //---------------------------------------------------------------------------
  1549. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1550. TOperationSide Side, int FilesOnly)
  1551. {
  1552. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1553. }
  1554. //---------------------------------------------------------------------------
  1555. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1556. TOperationSide Side, int FilesOnly)
  1557. {
  1558. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1559. }
  1560. //---------------------------------------------------------------------------
  1561. struct THistoryItemData
  1562. {
  1563. short int Side;
  1564. short int Index;
  1565. };
  1566. //---------------------------------------------------------------------------
  1567. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1568. {
  1569. DirView(Side)->HistoryGo(Index);
  1570. }
  1571. //---------------------------------------------------------------------------
  1572. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1573. {
  1574. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1575. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1576. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1577. }
  1578. //---------------------------------------------------------------------------
  1579. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1580. bool Back)
  1581. {
  1582. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1583. {
  1584. TCustomDirView * DView = DirView(Side);
  1585. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1586. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1587. if (ICount > 10)
  1588. {
  1589. ICount = 10;
  1590. }
  1591. Menu->Items->Clear();
  1592. THistoryItemData Data;
  1593. Data.Side = Side;
  1594. for (short int i = 1; i <= ICount; i++)
  1595. {
  1596. TTBCustomItem * Item = new TTBXItem(Menu);
  1597. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1598. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1599. Item->Hint = DView->HistoryPath[Data.Index];
  1600. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1601. Item->Tag = *reinterpret_cast<int*>(&Data);
  1602. Item->OnClick = HistoryItemClick;
  1603. Menu->Items->Add(Item);
  1604. }
  1605. }
  1606. }
  1607. //---------------------------------------------------------------------------
  1608. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1609. TOperationSide Side, bool Back)
  1610. {
  1611. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1612. {
  1613. // workaround
  1614. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1615. // operator new allocates memory in Pascal code, but calls inline
  1616. // contructor in C++, leading in problems, the function does
  1617. // both in Pascal code
  1618. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1619. UpdateHistoryMenu(Side, Back);
  1620. }
  1621. return FHistoryMenu[Side == osLocal][Back];
  1622. }
  1623. //---------------------------------------------------------------------------
  1624. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1625. TCustomDirView *Sender)
  1626. {
  1627. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1628. UpdateHistoryMenu(Side, true);
  1629. UpdateHistoryMenu(Side, false);
  1630. }
  1631. //---------------------------------------------------------------------------
  1632. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1633. {
  1634. // remote custom commands can be executed only if the server supports shell commands
  1635. // or have secondary shell
  1636. return (FTerminal != NULL) && FTerminal->Active && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1637. }
  1638. //---------------------------------------------------------------------------
  1639. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1640. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1641. {
  1642. int Result;
  1643. TFileCustomCommand RemoteCustomCommand;
  1644. TLocalCustomCommand LocalCustomCommand;
  1645. TFileCustomCommand * NonInteractiveCustomCommand =
  1646. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1647. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1648. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1649. if (FLAGCLEAR(Command.Params, ccLocal))
  1650. {
  1651. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1652. // custom command that does not operate with files can be executed anytime ...
  1653. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1654. {
  1655. // ... but do not show such command in remote file menu
  1656. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1657. {
  1658. Result = AllowedState;
  1659. }
  1660. else
  1661. {
  1662. Result = -1;
  1663. }
  1664. }
  1665. else
  1666. {
  1667. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1668. {
  1669. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1670. }
  1671. else
  1672. {
  1673. Result = -1;
  1674. }
  1675. }
  1676. }
  1677. else
  1678. {
  1679. // custom command that does not operate with files can be executed anytime
  1680. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1681. {
  1682. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1683. }
  1684. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1685. {
  1686. if ((ListType == ccltAll) || (ListType == ccltFile))
  1687. {
  1688. // special case is "diff"-style command that can be executed over any side,
  1689. // if we have both sides
  1690. Result =
  1691. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1692. // directly (instead of EnableSelectedOperation) to pass
  1693. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1694. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1695. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1696. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1697. }
  1698. else if (ListType == ccltBoth)
  1699. {
  1700. DebugAssert(!IsLocalBrowserMode());
  1701. Result = 1;
  1702. }
  1703. else
  1704. {
  1705. Result = -1;
  1706. }
  1707. }
  1708. else
  1709. {
  1710. if ((ListType == ccltAll) ||
  1711. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1712. {
  1713. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1714. }
  1715. else
  1716. {
  1717. Result = -1;
  1718. }
  1719. }
  1720. }
  1721. return Result;
  1722. }
  1723. //---------------------------------------------------------------------------
  1724. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1725. TStrings * FileList, const TCustomCommandType & ACommand,
  1726. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1727. {
  1728. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1729. {
  1730. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1731. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1732. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1733. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1734. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1735. bool Capture =
  1736. FLAGSET(ACommand.Params, ccShowResults) ||
  1737. FLAGSET(ACommand.Params, ccCopyResults) ||
  1738. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1739. TCaptureOutputEvent OutputEvent = NULL;
  1740. DebugAssert(FCapturedLog == NULL);
  1741. if (Capture)
  1742. {
  1743. FCapturedLog = new TStringList();
  1744. OutputEvent = TerminalCaptureLog;
  1745. }
  1746. try
  1747. {
  1748. if (!RemoteCustomCommand.IsFileCommand(Command))
  1749. {
  1750. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1751. OutputEvent);
  1752. }
  1753. else
  1754. {
  1755. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1756. }
  1757. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1758. {
  1759. if (FLAGSET(ACommand.Params, ccCopyResults))
  1760. {
  1761. CopyToClipboard(FCapturedLog);
  1762. }
  1763. if (FLAGSET(ACommand.Params, ccShowResults))
  1764. {
  1765. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1766. }
  1767. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1768. {
  1769. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1770. }
  1771. }
  1772. }
  1773. __finally
  1774. {
  1775. SAFE_DESTROY(FCapturedLog);
  1776. }
  1777. }
  1778. }
  1779. //---------------------------------------------------------------------------
  1780. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1781. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1782. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1783. {
  1784. DebugAssert(!IsLocalBrowserMode());
  1785. TStrings * LocalFileList = NULL;
  1786. TStrings * RemoteFileList = NULL;
  1787. TStrings * RemoteFileListFull = NULL;
  1788. try
  1789. {
  1790. if (LocalFileCommand)
  1791. {
  1792. if (ALocalFileList == NULL)
  1793. {
  1794. DebugAssert(HasDirView[osLocal]);
  1795. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1796. // directly (instead of EnableSelectedOperation) to pass
  1797. // false to FocusedFileOnlyWhenFocused
  1798. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1799. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1800. }
  1801. else
  1802. {
  1803. LocalFileList = ALocalFileList;
  1804. }
  1805. if (FileListCommand)
  1806. {
  1807. if (LocalFileList->Count != 1)
  1808. {
  1809. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1810. }
  1811. }
  1812. else
  1813. {
  1814. if ((LocalFileList->Count != 1) &&
  1815. (FileList->Count != 1) &&
  1816. (LocalFileList->Count != FileList->Count))
  1817. {
  1818. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1819. }
  1820. }
  1821. }
  1822. UnicodeString RootTempDir;
  1823. UnicodeString TempDir;
  1824. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1825. if (!RemoteFiles)
  1826. {
  1827. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1828. }
  1829. try
  1830. {
  1831. TDateTimes RemoteFileTimes;
  1832. if (RemoteFiles)
  1833. {
  1834. RemoteFileList = FileList;
  1835. }
  1836. else
  1837. {
  1838. RemoteFileList = new TStringList();
  1839. TMakeLocalFileListParams MakeFileListParam;
  1840. MakeFileListParam.FileList = RemoteFileList;
  1841. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1842. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1843. MakeFileListParam.Recursive =
  1844. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1845. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1846. if (!MakeFileListParam.Recursive)
  1847. {
  1848. RemoteFileTimes.clear();
  1849. RemoteFileListFull = new TStringList();
  1850. MakeFileListParam.FileList = RemoteFileListFull;
  1851. MakeFileListParam.Recursive = true;
  1852. MakeFileListParam.IncludeDirs = false;
  1853. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1854. }
  1855. else
  1856. {
  1857. RemoteFileListFull = RemoteFileList;
  1858. }
  1859. }
  1860. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1861. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1862. if (!NonBlocking)
  1863. {
  1864. int Count;
  1865. if (FileListCommand)
  1866. {
  1867. Count = 1;
  1868. }
  1869. else if (LocalFileCommand)
  1870. {
  1871. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1872. }
  1873. else
  1874. {
  1875. Count = RemoteFileList->Count;
  1876. }
  1877. Progress.Start(foCustomCommand, osRemote, Count);
  1878. DebugAssert(FProgressForm != NULL);
  1879. FProgressForm->ReadOnly = true;
  1880. }
  1881. try
  1882. {
  1883. if (FileListCommand)
  1884. {
  1885. UnicodeString LocalFile;
  1886. // MakeFileList does not delimit filenames
  1887. UnicodeString FileList = MakeFileList(RemoteFileList);
  1888. if (LocalFileCommand)
  1889. {
  1890. DebugAssert(LocalFileList->Count == 1);
  1891. LocalFile = LocalFileList->Strings[0];
  1892. }
  1893. TLocalCustomCommand CustomCommand(Data,
  1894. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1895. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1896. if (NonBlocking)
  1897. {
  1898. DebugAssert(!POutput);
  1899. ExecuteShellChecked(ShellCommand);
  1900. }
  1901. else
  1902. {
  1903. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1904. }
  1905. }
  1906. else if (LocalFileCommand)
  1907. {
  1908. if (LocalFileList->Count == 1)
  1909. {
  1910. UnicodeString LocalFile = LocalFileList->Strings[0];
  1911. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1912. {
  1913. UnicodeString FileName = RemoteFileList->Strings[Index];
  1914. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1915. Progress.SetFile(FileNameForProgress);
  1916. TLocalCustomCommand CustomCommand(Data,
  1917. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1918. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1919. TOnceDoneOperation OnceDoneOperation;
  1920. // Do not unselect anything, as it won't work with two panels anyway
  1921. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1922. }
  1923. }
  1924. else if (RemoteFileList->Count == 1)
  1925. {
  1926. UnicodeString FileName = RemoteFileList->Strings[0];
  1927. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1928. {
  1929. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1930. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1931. Progress.SetFile(LocalFileNameOnly);
  1932. TLocalCustomCommand CustomCommand(
  1933. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1934. FileName, LocalFileName, L"");
  1935. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1936. TOnceDoneOperation OnceDoneOperation;
  1937. // Do not unselect anything, as it won't work with two panels anyway
  1938. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1939. }
  1940. }
  1941. else
  1942. {
  1943. if (LocalFileList->Count != RemoteFileList->Count)
  1944. {
  1945. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1946. }
  1947. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1948. {
  1949. UnicodeString FileName = RemoteFileList->Strings[Index];
  1950. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1951. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1952. Progress.SetFile(LocalFileNameOnly); // sic
  1953. TLocalCustomCommand CustomCommand(
  1954. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1955. FileName, LocalFileName, L"");
  1956. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1957. TOnceDoneOperation OnceDoneOperation;
  1958. // Do not unselect anything, as it won't work with two panels anyway
  1959. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1960. }
  1961. }
  1962. }
  1963. else
  1964. {
  1965. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1966. {
  1967. UnicodeString FileName = RemoteFileList->Strings[Index];
  1968. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1969. Progress.SetFile(FileNameForProgress);
  1970. TLocalCustomCommand CustomCommand(Data,
  1971. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1972. FileName, L"", L"");
  1973. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1974. TOnceDoneOperation OnceDoneOperation;
  1975. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1976. }
  1977. }
  1978. }
  1979. __finally
  1980. {
  1981. if (!NonBlocking)
  1982. {
  1983. Progress.Stop();
  1984. }
  1985. }
  1986. DebugAssert(!FAutoOperation);
  1987. if (!RemoteFiles)
  1988. {
  1989. TempDir = IncludeTrailingBackslash(TempDir);
  1990. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1991. {
  1992. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1993. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1994. // Skip directories, process only nested files.
  1995. // The check is redundant as FileAge fails for directories anyway.
  1996. !DirectoryExists(FileName))
  1997. {
  1998. UnicodeString RemoteDir =
  1999. UnixExtractFileDir(
  2000. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  2001. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2002. TDateTime NewTime;
  2003. if (FileAge(FileName, NewTime) &&
  2004. (NewTime != RemoteFileTimes[Index]))
  2005. {
  2006. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2007. TemporaryFileCopyParam(CopyParam);
  2008. CopyParam.FileMask = L"";
  2009. FAutoOperation = true;
  2010. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2011. TemporaryFilesList->Add(FileName);
  2012. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2013. }
  2014. }
  2015. }
  2016. }
  2017. }
  2018. __finally
  2019. {
  2020. FAutoOperation = false;
  2021. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2022. {
  2023. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2024. }
  2025. }
  2026. }
  2027. __finally
  2028. {
  2029. if (RemoteFileList != FileList)
  2030. {
  2031. delete RemoteFileList;
  2032. }
  2033. if (LocalFileList != ALocalFileList)
  2034. {
  2035. delete LocalFileList;
  2036. }
  2037. if (RemoteFileListFull != RemoteFileList)
  2038. {
  2039. delete RemoteFileListFull;
  2040. }
  2041. }
  2042. }
  2043. //---------------------------------------------------------------------------
  2044. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2045. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2046. bool FileListCommand, UnicodeString * POutput)
  2047. {
  2048. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2049. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2050. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2051. {
  2052. UnicodeString FileName = SelectedFileList->Strings[Index];
  2053. if (DirectoryExists(FileName))
  2054. {
  2055. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2056. {
  2057. LocalFileList->Add(FileName);
  2058. }
  2059. if (FLAGSET(ACommand.Params, ccRecursive))
  2060. {
  2061. TMakeLocalFileListParams MakeFileListParam;
  2062. MakeFileListParam.FileList = LocalFileList.get();
  2063. MakeFileListParam.FileTimes = NULL;
  2064. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2065. MakeFileListParam.Recursive = true;
  2066. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2067. }
  2068. }
  2069. else
  2070. {
  2071. LocalFileList->Add(FileName);
  2072. }
  2073. }
  2074. UnicodeString RemotePath;
  2075. if (Terminal != NULL)
  2076. {
  2077. RemotePath = Terminal->CurrentDirectory;
  2078. }
  2079. if (FileListCommand)
  2080. {
  2081. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2082. TLocalCustomCommand CustomCommand(
  2083. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2084. L"", L"", FileList);
  2085. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2086. }
  2087. else
  2088. {
  2089. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2090. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2091. DebugAssert(FProgressForm != NULL);
  2092. FProgressForm->ReadOnly = true;
  2093. try
  2094. {
  2095. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2096. {
  2097. UnicodeString FileName = LocalFileList->Strings[Index];
  2098. Progress.SetFile(FileName);
  2099. TLocalCustomCommand CustomCommand(
  2100. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2101. FileName, L"", L"");
  2102. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2103. TOnceDoneOperation OnceDoneOperation;
  2104. Progress.Finish(FileName, true, OnceDoneOperation);
  2105. }
  2106. }
  2107. __finally
  2108. {
  2109. Progress.Stop();
  2110. }
  2111. }
  2112. }
  2113. //---------------------------------------------------------------------------
  2114. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2115. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2116. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2117. {
  2118. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2119. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2120. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2121. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2122. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2123. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2124. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2125. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2126. std::unique_ptr<UnicodeString> POutput;
  2127. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2128. FLAGSET(ACommand.Params, ccCopyResults))
  2129. {
  2130. POutput.reset(new UnicodeString());
  2131. }
  2132. if (!LocalCustomCommand.IsFileCommand(Command))
  2133. {
  2134. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2135. }
  2136. // remote files?
  2137. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2138. {
  2139. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2140. }
  2141. // local files
  2142. else
  2143. {
  2144. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2145. }
  2146. if (POutput.get() != NULL)
  2147. {
  2148. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2149. int P = POutput->Pos(sLineBreak);
  2150. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2151. {
  2152. POutput->SetLength(P - 1);
  2153. }
  2154. // Copy even empty output to clipboard
  2155. if (FLAGSET(ACommand.Params, ccCopyResults))
  2156. {
  2157. CopyToClipboard(*POutput);
  2158. }
  2159. // But do not show an empty message box.
  2160. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2161. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2162. !POutput->IsEmpty())
  2163. {
  2164. TClipboardHandler ClipboardHandler;
  2165. ClipboardHandler.Text = *POutput;
  2166. TMessageParams Params;
  2167. TQueryButtonAlias Aliases[1];
  2168. Aliases[0].Button = qaRetry;
  2169. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2170. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2171. Params.Aliases = Aliases;
  2172. Params.AliasesCount = LENOF(Aliases);
  2173. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2174. }
  2175. }
  2176. }
  2177. //---------------------------------------------------------------------------
  2178. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2179. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2180. {
  2181. TCustomCommandData Data;
  2182. UnicodeString Site;
  2183. UnicodeString RemotePath;
  2184. if (Terminal != NULL)
  2185. {
  2186. Data = TCustomCommandData(Terminal);
  2187. Site = Terminal->SessionData->SessionKey;
  2188. RemotePath = Terminal->CurrentDirectory;
  2189. }
  2190. UnicodeString HelpKeyword = ACommand.HomePage;
  2191. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2192. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2193. {
  2194. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2195. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2196. {
  2197. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2198. }
  2199. else
  2200. {
  2201. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2202. }
  2203. if (!DoCustomCommandOptionsDialog(
  2204. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2205. {
  2206. Abort();
  2207. }
  2208. }
  2209. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2210. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2211. {
  2212. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2213. }
  2214. else
  2215. {
  2216. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2217. }
  2218. }
  2219. //---------------------------------------------------------------------------
  2220. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2221. const TCustomCommandType & Command)
  2222. {
  2223. DebugAssert(FCustomCommandLocalFileList != NULL);
  2224. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2225. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2226. TStrings * LocalFileList = new TStringList();
  2227. TStrings * RemoteFileList = new TStringList();
  2228. try
  2229. {
  2230. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2231. {
  2232. LocalFileList->Clear();
  2233. LocalFileList->AddObject(
  2234. FCustomCommandLocalFileList->Strings[Index],
  2235. FCustomCommandLocalFileList->Objects[Index]);
  2236. RemoteFileList->Clear();
  2237. RemoteFileList->AddObject(
  2238. FCustomCommandRemoteFileList->Strings[Index],
  2239. FCustomCommandRemoteFileList->Objects[Index]);
  2240. CustomCommand(RemoteFileList, Command, LocalFileList);
  2241. }
  2242. }
  2243. __finally
  2244. {
  2245. delete LocalFileList;
  2246. delete RemoteFileList;
  2247. }
  2248. }
  2249. //---------------------------------------------------------------------------
  2250. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2251. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2252. {
  2253. delete FCustomCommandLocalFileList;
  2254. delete FCustomCommandRemoteFileList;
  2255. // takeover ownership,
  2256. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2257. FCustomCommandLocalFileList = LocalFileList;
  2258. FCustomCommandRemoteFileList = RemoteFileList;
  2259. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2260. if (Button != NULL)
  2261. {
  2262. FCustomCommandMenu->Items->Clear();
  2263. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2264. MenuPopup(FCustomCommandMenu, Button);
  2265. }
  2266. else
  2267. {
  2268. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2269. }
  2270. }
  2271. //---------------------------------------------------------------------------
  2272. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2273. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2274. {
  2275. DebugAssert(FCapturedLog != NULL);
  2276. if ((OutputType == cotOutput) || (OutputType == cotError))
  2277. {
  2278. FCapturedLog->Add(AddedLine);
  2279. }
  2280. }
  2281. //---------------------------------------------------------------------------
  2282. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2283. TOperationSide Side)
  2284. {
  2285. return
  2286. (Side == osOther) &&
  2287. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2288. }
  2289. //---------------------------------------------------------------------------
  2290. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2291. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2292. {
  2293. TTBItemOptions O;
  2294. O = Item->Options;
  2295. TCustomDirView * DView = DirView(Side);
  2296. if ((DView->ItemFocused != NULL) &&
  2297. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2298. // when resolving links is disabled, default action is to enter the directory,
  2299. // no matter what DoubleClickAction is configured to
  2300. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2301. // Can only Edit files, but can Open/Copy even directories
  2302. ((DoubleClickAction != dcaEdit) ||
  2303. !DView->ItemIsDirectory(DView->ItemFocused)))
  2304. {
  2305. Item->Options = O << tboDefault;
  2306. }
  2307. else
  2308. {
  2309. Item->Options = O >> tboDefault;
  2310. }
  2311. }
  2312. //---------------------------------------------------------------------------
  2313. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2314. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2315. {
  2316. TCustomDirView * DView = DirView(Side);
  2317. TListItem * Item = DView->ItemFocused;
  2318. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2319. EnableFocusedOperation[Side])
  2320. {
  2321. TPoint ClientPoint;
  2322. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2323. TPoint(0, 0) : MousePos;
  2324. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2325. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2326. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2327. }
  2328. }
  2329. //---------------------------------------------------------------------------
  2330. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2331. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2332. {
  2333. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2334. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2335. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2336. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2337. Handled = true;
  2338. }
  2339. //---------------------------------------------------------------------------
  2340. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2341. {
  2342. }
  2343. //---------------------------------------------------------------------------
  2344. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2345. {
  2346. DebugAssert(FErrorList == NULL);
  2347. if (WinConfiguration->ContinueOnError)
  2348. {
  2349. FErrorList = new TStringList();
  2350. Configuration->Usage->Inc(L"ContinuationsOnError");
  2351. }
  2352. NonVisualDataModule->StartBusy();
  2353. }
  2354. //---------------------------------------------------------------------------
  2355. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2356. {
  2357. NonVisualDataModule->EndBusy();
  2358. if (FErrorList)
  2359. {
  2360. HandleErrorList(FErrorList);
  2361. }
  2362. }
  2363. //---------------------------------------------------------------------------
  2364. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2365. const TCopyParamType & CopyParam)
  2366. {
  2367. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2368. {
  2369. Configuration->Usage->Inc(L"FileMaskUses");
  2370. }
  2371. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2372. {
  2373. Configuration->Usage->Inc(L"OperationMaskUses");
  2374. }
  2375. }
  2376. //---------------------------------------------------------------------------
  2377. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2378. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2379. {
  2380. DebugAssert(!IsLocalBrowserMode());
  2381. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2382. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2383. TTransferOperationParam DefaultParam;
  2384. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2385. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2386. switch (Param.Queue)
  2387. {
  2388. case asOn:
  2389. CopyParam.Queue = true;
  2390. break;
  2391. case asOff:
  2392. CopyParam.Queue = false;
  2393. break;
  2394. case asAuto:
  2395. default:
  2396. // keep default
  2397. break;
  2398. }
  2399. bool Result =
  2400. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2401. if (Result)
  2402. {
  2403. DebugAssert(Terminal);
  2404. bool SelectionRestored = false;
  2405. TCustomDirView * DView = NULL;
  2406. if (HasDirView[Side])
  2407. {
  2408. DView = DirView(Side);
  2409. DView->SaveSelection();
  2410. DView->SaveSelectedNames();
  2411. }
  2412. UpdateCopyParamCounters(CopyParam);
  2413. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2414. DebugAssert(FTransferResumeList == NULL);
  2415. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2416. FMoveToQueue = false;
  2417. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2418. try
  2419. {
  2420. TStrings * PermanentFileList;
  2421. std::unique_ptr<TStrings> PermanentFileListOwner;
  2422. try
  2423. {
  2424. if (Side == osLocal)
  2425. {
  2426. PermanentFileList = FileList;
  2427. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2428. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2429. if (Operation == foMove)
  2430. {
  2431. ReloadLocalDirectory();
  2432. if (DView != NULL)
  2433. {
  2434. DView->RestoreSelection();
  2435. }
  2436. SelectionRestored = true;
  2437. }
  2438. }
  2439. else
  2440. {
  2441. // Clone the file list as it may refer to current directory files,
  2442. // which get destroyed, when the source directory is reloaded after foMove operation.
  2443. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2444. // But for a hotfix, we are not going to do such a big change.
  2445. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2446. PermanentFileList = PermanentFileListOwner.get();
  2447. try
  2448. {
  2449. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2450. }
  2451. __finally
  2452. {
  2453. if (Operation == foMove)
  2454. {
  2455. if (DView != NULL)
  2456. {
  2457. DView->RestoreSelection();
  2458. }
  2459. SelectionRestored = true;
  2460. }
  2461. ReloadLocalDirectory(Param.TargetDirectory);
  2462. }
  2463. }
  2464. }
  2465. catch (EAbort &)
  2466. {
  2467. if (FMoveToQueue)
  2468. {
  2469. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2470. DebugAssert(CopyParam.TransferSkipList == NULL);
  2471. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2472. if (TransferResumeList->Count > 0)
  2473. {
  2474. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2475. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2476. }
  2477. CopyParam.TransferSkipList = TransferResumeList.release();
  2478. // not really needed, just to keep it consistent with TransferResumeList
  2479. FTransferResumeList = NULL;
  2480. FMoveToQueue = false;
  2481. Configuration->Usage->Inc("MovesToBackground");
  2482. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2483. ClearTransferSourceSelection(Direction);
  2484. }
  2485. throw;
  2486. }
  2487. }
  2488. __finally
  2489. {
  2490. if (!SelectionRestored && (DView != NULL))
  2491. {
  2492. DView->DiscardSavedSelection();
  2493. }
  2494. FTransferResumeList = NULL;
  2495. }
  2496. }
  2497. return Result;
  2498. }
  2499. //---------------------------------------------------------------------------
  2500. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2501. TOperationSide Side, TStrings * FileList, void * Param)
  2502. {
  2503. DebugAssert(FileList->Count);
  2504. // We deliberately do not toggle alternative flag (Param), but use OR,
  2505. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2506. // shortcut of CurrentDeleteAlternativeAction
  2507. bool Alternative =
  2508. bool(Param) || UseAlternativeFunction();
  2509. bool Recycle;
  2510. if (IsSideLocalBrowser(Side))
  2511. {
  2512. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2513. }
  2514. else
  2515. {
  2516. Recycle =
  2517. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2518. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2519. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2520. }
  2521. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2522. if (!Result)
  2523. {
  2524. UnicodeString Query;
  2525. if (FileList->Count == 1)
  2526. {
  2527. if (IsSideLocalBrowser(Side))
  2528. {
  2529. Query = ExtractFileName(FileList->Strings[0]);
  2530. }
  2531. else
  2532. {
  2533. Query = UnixExtractFileName(FileList->Strings[0]);
  2534. }
  2535. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2536. }
  2537. else
  2538. {
  2539. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2540. }
  2541. TMessageParams Params(mpNeverAskAgainCheck);
  2542. Params.ImageName = L"Delete file";
  2543. unsigned int Answer =
  2544. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2545. if (Answer == qaNeverAskAgain)
  2546. {
  2547. Result = true;
  2548. if (Recycle)
  2549. {
  2550. WinConfiguration->ConfirmRecycling = false;
  2551. }
  2552. else
  2553. {
  2554. WinConfiguration->ConfirmDeleting = false;
  2555. }
  2556. }
  2557. else
  2558. {
  2559. Result = (Answer == qaOK);
  2560. }
  2561. }
  2562. if (Result)
  2563. {
  2564. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2565. }
  2566. return Result;
  2567. }
  2568. //---------------------------------------------------------------------------
  2569. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2570. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2571. {
  2572. TAutoBatch AutoBatch(this);
  2573. bool Result;
  2574. if ((Operation == foCopy) || (Operation == foMove))
  2575. {
  2576. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2577. }
  2578. else if (Operation == foRename)
  2579. {
  2580. DebugAssert(DirView(Side)->ItemFocused);
  2581. DirView(Side)->ItemFocused->EditCaption();
  2582. Result = true;
  2583. }
  2584. else if (Operation == foDelete)
  2585. {
  2586. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2587. }
  2588. else if (Operation == foSetProperties)
  2589. {
  2590. DirView(osRemote)->SaveSelectedNames();
  2591. Result = SetProperties(Side, FileList);
  2592. }
  2593. else if (Operation == foCustomCommand)
  2594. {
  2595. DebugAssert(Param);
  2596. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2597. DirView(osRemote)->SaveSelectedNames();
  2598. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2599. CustomCommand(FileList, *Command, NULL);
  2600. Result = true;
  2601. }
  2602. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2603. {
  2604. DebugAssert(!IsSideLocalBrowser(Side));
  2605. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2606. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2607. }
  2608. else if (Operation == foLock)
  2609. {
  2610. DebugAssert(!IsSideLocalBrowser(Side));
  2611. LockFiles(FileList, true);
  2612. Result = true;
  2613. }
  2614. else if (Operation == foUnlock)
  2615. {
  2616. DebugAssert(!IsSideLocalBrowser(Side));
  2617. LockFiles(FileList, false);
  2618. Result = true;
  2619. }
  2620. else
  2621. {
  2622. DebugFail();
  2623. }
  2624. return Result;
  2625. }
  2626. //---------------------------------------------------------------------------
  2627. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2628. {
  2629. if (Side == osCurrent)
  2630. {
  2631. Side = FCurrentSide;
  2632. }
  2633. return Side;
  2634. }
  2635. //---------------------------------------------------------------------------
  2636. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2637. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2638. {
  2639. Side = GetSide(Side);
  2640. bool Result;
  2641. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2642. try
  2643. {
  2644. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2645. }
  2646. __finally
  2647. {
  2648. delete FileList;
  2649. }
  2650. return Result;
  2651. }
  2652. //---------------------------------------------------------------------------
  2653. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2654. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2655. bool NoConfirmation, void * Param)
  2656. {
  2657. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2658. {
  2659. if ((Operation == foCopy) || (Operation == foMove))
  2660. {
  2661. DebugAssert(!IsLocalBrowserMode());
  2662. if (GetSide(Side) == osLocal)
  2663. {
  2664. Configuration->Usage->Inc(L"UploadsCommand");
  2665. }
  2666. else
  2667. {
  2668. Configuration->Usage->Inc(L"DownloadsCommand");
  2669. }
  2670. }
  2671. }
  2672. }
  2673. //---------------------------------------------------------------------------
  2674. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2675. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2676. {
  2677. TTransferOperationParam Param;
  2678. if ((WinConfiguration->Interface != ifCommander) ||
  2679. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2680. {
  2681. Flags &= ~cocShortCutHint;
  2682. }
  2683. TCustomDirView * DView = DirView(Side);
  2684. Param.Options =
  2685. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2686. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2687. if (FLAGSET(Flags, cocQueue))
  2688. {
  2689. Param.Queue = asOn;
  2690. }
  2691. else if (FLAGSET(Flags, cocNonQueue))
  2692. {
  2693. Param.Queue = asOff;
  2694. }
  2695. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2696. }
  2697. //---------------------------------------------------------------------------
  2698. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2699. {
  2700. try
  2701. {
  2702. if (ErrorList->Count)
  2703. {
  2704. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2705. if (MessageDialog(Message, qtError,
  2706. qaOK | qaCancel, HELP_NONE) == qaOK)
  2707. {
  2708. unsigned int Answer;
  2709. int Index = 0;
  2710. do
  2711. {
  2712. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2713. TQueryButtonAlias Aliases[2];
  2714. Aliases[0].Button = qaYes;
  2715. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2716. Aliases[1].Button = qaNo;
  2717. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2718. TMessageParams Params;
  2719. Params.Aliases = Aliases;
  2720. Params.AliasesCount = LENOF(Aliases);
  2721. Answer = MoreMessageDialog(
  2722. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2723. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2724. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2725. qaOK, HELP_NONE, &Params);
  2726. if (Answer == qaNo)
  2727. {
  2728. Index++;
  2729. }
  2730. if (Answer == qaYes)
  2731. {
  2732. Index--;
  2733. }
  2734. }
  2735. while (Answer != qaOK);
  2736. }
  2737. }
  2738. }
  2739. __finally
  2740. {
  2741. TStrings * List = ErrorList;
  2742. ErrorList = NULL;
  2743. for (int i = 0; i < List->Count; i++)
  2744. {
  2745. delete List->Objects[i];
  2746. }
  2747. delete List;
  2748. }
  2749. }
  2750. //---------------------------------------------------------------------------
  2751. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2752. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2753. {
  2754. // needed for checking filemasks, as there's no directory object
  2755. // associated with the file object
  2756. File->FullFileName = FullFileName;
  2757. TFileMasks::TParams MaskParams;
  2758. MaskParams.Size = File->Size;
  2759. MaskParams.Modification = File->Modification;
  2760. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2761. }
  2762. //---------------------------------------------------------------------------
  2763. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2764. {
  2765. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2766. Side = GetSide(Side);
  2767. TCustomDirView * DView = DirView(Side);
  2768. TListItem * FocusedItem = DView->ItemFocused;
  2769. UnicodeString Name;
  2770. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2771. {
  2772. Name = DView->ItemFileName(FocusedItem);
  2773. }
  2774. else
  2775. {
  2776. Name = LoadStr(NEW_FILE);
  2777. }
  2778. UnicodeString Names = Name;
  2779. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2780. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2781. HELP_EDIT_NEW, History.get(), true))
  2782. {
  2783. while (!Names.IsEmpty())
  2784. {
  2785. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2786. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2787. UnicodeString TargetFileName;
  2788. UnicodeString LocalFileName;
  2789. UnicodeString RootTempDir;
  2790. UnicodeString TempDir;
  2791. UnicodeString RemoteDirectory;
  2792. bool ExistingFile = false;
  2793. if (!IsSideLocalBrowser(Side))
  2794. {
  2795. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2796. TRemoteFile * File = NULL;
  2797. if (FTerminal->FileExists(Name, &File) &&
  2798. !File->IsDirectory)
  2799. {
  2800. try
  2801. {
  2802. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2803. ExistingFile = true;
  2804. }
  2805. __finally
  2806. {
  2807. delete File;
  2808. }
  2809. }
  2810. if (!ExistingFile)
  2811. {
  2812. RemoteDirectory = UnixExtractFilePath(Name);
  2813. TemporaryDirectoryForRemoteFiles(
  2814. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2815. TargetFileName = UnixExtractFileName(Name);
  2816. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2817. LocalFileName = TempDir +
  2818. // We probably do not want to trim the VMS version here
  2819. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2820. }
  2821. }
  2822. else
  2823. {
  2824. if (ExtractFilePath(Name).IsEmpty())
  2825. {
  2826. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2827. }
  2828. else
  2829. {
  2830. LocalFileName = ExpandFileName(Name);
  2831. }
  2832. TargetFileName = ExtractFileName(Name);
  2833. }
  2834. if (!ExistingFile)
  2835. {
  2836. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2837. if (NewFile)
  2838. {
  2839. int File = FileCreate(ApiPath(LocalFileName));
  2840. if (File < 0)
  2841. {
  2842. if (!RootTempDir.IsEmpty())
  2843. {
  2844. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2845. }
  2846. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2847. }
  2848. else
  2849. {
  2850. FileClose(File);
  2851. }
  2852. }
  2853. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2854. const TEditorData * ExternalEditor = NULL;
  2855. TFileMasks::TParams MaskParams; // size not known
  2856. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2857. false, MaskParams);
  2858. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2859. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2860. }
  2861. }
  2862. }
  2863. }
  2864. //---------------------------------------------------------------------------
  2865. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2866. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2867. {
  2868. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2869. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2870. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2871. return
  2872. ((ExecuteFileBy == efInternalEditor)) ||
  2873. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2874. }
  2875. //---------------------------------------------------------------------------
  2876. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2877. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2878. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2879. UnicodeString RemoteDirectory, bool NewFile)
  2880. {
  2881. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2882. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2883. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2884. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2885. Side = GetSide(Side);
  2886. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2887. if (!IsSideLocalBrowser(Side))
  2888. {
  2889. Data->Terminal = Terminal;
  2890. Data->Queue = Queue;
  2891. Data->SessionData = CloneCurrentSessionData();
  2892. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2893. Data->RemoteDirectory = RemoteDirectory;
  2894. Data->SessionName = Terminal->SessionData->SessionName;
  2895. Data->LocalRootDirectory = LocalRootDirectory;
  2896. Data->OriginalFileName = OriginalFileName;
  2897. Data->Command = L""; // will be changed later for external editor
  2898. }
  2899. if (ExecuteFileBy == efInternalEditor)
  2900. {
  2901. if (!IsSideLocalBrowser(Side))
  2902. {
  2903. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2904. L" - " + Terminal->SessionData->SessionName;
  2905. TForm * Editor;
  2906. try
  2907. {
  2908. TNotifyEvent OnFileChanged = NULL;
  2909. TNotifyEvent OnSaveAll = NULL;
  2910. TAnyModifiedEvent OnAnyModified = NULL;
  2911. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2912. if (Terminal->IsCapable[fsBackgroundTransfers])
  2913. {
  2914. OnFileChanged = FEditorManager->FileChanged;
  2915. OnSaveAll = SaveAllInternalEditors;
  2916. OnAnyModified = AnyInternalEditorModified;
  2917. }
  2918. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2919. FEditorManager->FileReload, FEditorManager->FileClosed,
  2920. OnSaveAll, OnAnyModified,
  2921. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2922. }
  2923. catch(...)
  2924. {
  2925. if (!LocalRootDirectory.IsEmpty())
  2926. {
  2927. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2928. }
  2929. throw;
  2930. }
  2931. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2932. }
  2933. else
  2934. {
  2935. DebugAssert(!FStandaloneEditing);
  2936. TForm * Editor =
  2937. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2938. SaveAllInternalEditors, AnyInternalEditorModified,
  2939. L"", false, SessionColor, -1, NewFile);
  2940. FLocalEditors->Add(Editor);
  2941. }
  2942. }
  2943. else
  2944. {
  2945. HANDLE Process;
  2946. if (ExecuteFileBy == efExternalEditor)
  2947. {
  2948. UnicodeString Program, Params, Dir;
  2949. Data->Command = ExternalEditor->ExternalEditor;
  2950. ReformatFileNameCommand(Data->Command);
  2951. SplitCommand(Data->Command, Program, Params, Dir);
  2952. Params = ExpandFileNameCommand(Params, FileName);
  2953. Program = ExpandEnvironmentVariables(Program);
  2954. if (!ExecuteShell(Program, Params, Process))
  2955. {
  2956. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2957. }
  2958. }
  2959. else
  2960. {
  2961. DebugAssert(IsSideLocalBrowser(Side));
  2962. if (!ExecuteShell(FileName, L"", Process))
  2963. {
  2964. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2965. }
  2966. }
  2967. if (IsSideLocalBrowser(Side) ||
  2968. ((ExecuteFileBy == efShell) &&
  2969. !WinConfiguration->Editor.SDIShellEditor) ||
  2970. ((ExecuteFileBy == efExternalEditor) &&
  2971. !ExternalEditor->SDIExternalEditor))
  2972. {
  2973. // no need for handle
  2974. if (Process != NULL)
  2975. {
  2976. DebugCheck(CloseHandle(Process));
  2977. }
  2978. Process = INVALID_HANDLE_VALUE;
  2979. }
  2980. else
  2981. {
  2982. if (Process == NULL)
  2983. {
  2984. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2985. }
  2986. }
  2987. if (!IsSideLocalBrowser(Side))
  2988. {
  2989. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2990. }
  2991. }
  2992. }
  2993. //---------------------------------------------------------------------------
  2994. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2995. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2996. void * /*Arg*/)
  2997. {
  2998. if (Token != NULL)
  2999. {
  3000. EditorFormFileSave(static_cast<TForm *>(Token));
  3001. }
  3002. }
  3003. //---------------------------------------------------------------------------
  3004. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3005. {
  3006. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3007. {
  3008. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3009. }
  3010. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3011. }
  3012. //---------------------------------------------------------------------------
  3013. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3014. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3015. void * Arg)
  3016. {
  3017. if ((Token != NULL) &&
  3018. IsEditorFormModified(static_cast<TForm *>(Token)))
  3019. {
  3020. *static_cast<bool *>(Arg) = true;
  3021. }
  3022. }
  3023. //---------------------------------------------------------------------------
  3024. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3025. TObject * /*Sender*/, bool & Modified)
  3026. {
  3027. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3028. {
  3029. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3030. {
  3031. Modified = true;
  3032. }
  3033. }
  3034. if (!Modified)
  3035. {
  3036. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3037. }
  3038. }
  3039. //---------------------------------------------------------------------------
  3040. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3041. {
  3042. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3043. }
  3044. //---------------------------------------------------------------------------
  3045. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3046. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3047. UnicodeString & Result, UnicodeString & RootDirectory)
  3048. {
  3049. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3050. {
  3051. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3052. Result = RootDirectory;
  3053. }
  3054. else
  3055. {
  3056. RootDirectory = L"";
  3057. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3058. Result = IncludeTrailingBackslash(Result);
  3059. }
  3060. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3061. {
  3062. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3063. }
  3064. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3065. {
  3066. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3067. {
  3068. RemoteDirectory.Delete(1, 1);
  3069. }
  3070. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3071. }
  3072. if (!ForceDirectories(ApiPath(Result)))
  3073. {
  3074. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3075. }
  3076. }
  3077. //---------------------------------------------------------------------------
  3078. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3079. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3080. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3081. {
  3082. DebugAssert(!IsLocalBrowserMode());
  3083. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3084. if (ForceText)
  3085. {
  3086. CopyParam.TransferMode = tmAscii;
  3087. }
  3088. TemporaryFileCopyParam(CopyParam);
  3089. if (AllFiles)
  3090. {
  3091. CopyParam.IncludeFileMask = TFileMasks();
  3092. }
  3093. if (TempDir.IsEmpty())
  3094. {
  3095. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3096. }
  3097. DebugAssert(!FAutoOperation);
  3098. FAutoOperation = AutoOperation;
  3099. Terminal->ExceptionOnFail = true;
  3100. try
  3101. {
  3102. try
  3103. {
  3104. // turn off confirmations, as for MDI editors we may possibly download
  3105. // the same file over
  3106. Terminal->CopyToLocal(
  3107. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3108. if (GetTargetNames)
  3109. {
  3110. for (int i = 0; i < FileList->Count; i++)
  3111. {
  3112. FileList->Strings[i] =
  3113. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3114. }
  3115. }
  3116. }
  3117. catch(...)
  3118. {
  3119. if (!RootTempDir.IsEmpty())
  3120. {
  3121. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3122. }
  3123. throw;
  3124. }
  3125. }
  3126. __finally
  3127. {
  3128. FAutoOperation = false;
  3129. Terminal->ExceptionOnFail = false;
  3130. }
  3131. }
  3132. //---------------------------------------------------------------------------
  3133. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3134. {
  3135. // Do not waste time checking for default editor list the next time,
  3136. // or testing if default editor is not notepad.
  3137. bool TryNextTime = false;
  3138. UnicodeString UsageState;
  3139. if (!WinConfiguration->EditorList->IsDefaultList())
  3140. {
  3141. UsageState = "H";
  3142. }
  3143. else
  3144. {
  3145. UnicodeString Executable;
  3146. UnicodeString ExecutableDescription;
  3147. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3148. {
  3149. UnicodeString Message =
  3150. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3151. unsigned int Answer =
  3152. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3153. if (Answer != qaOK)
  3154. {
  3155. UsageState = "R";
  3156. }
  3157. else
  3158. {
  3159. UsageState = "A";
  3160. TEditorData EditorData;
  3161. EditorData.Editor = edExternal;
  3162. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3163. EditorData.DecideExternalEditorText();
  3164. TEditorList EditorList;
  3165. EditorList = *WinConfiguration->EditorList;
  3166. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3167. WinConfiguration->EditorList = &EditorList;
  3168. }
  3169. }
  3170. }
  3171. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3172. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3173. }
  3174. //---------------------------------------------------------------------------
  3175. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3176. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3177. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3178. {
  3179. if (ExecuteFileBy == efDefaultEditor)
  3180. {
  3181. if (!WinConfiguration->OfferedEditorAutoConfig)
  3182. {
  3183. EditorAutoConfig();
  3184. }
  3185. const TEditorPreferences * Editor =
  3186. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3187. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3188. {
  3189. ExecuteFileBy = efInternalEditor;
  3190. ExternalEditor = NULL;
  3191. }
  3192. else if (Editor->Data->Editor == edOpen)
  3193. {
  3194. ExecuteFileBy = efShell;
  3195. ExternalEditor = NULL;
  3196. }
  3197. else
  3198. {
  3199. ExecuteFileBy = efExternalEditor;
  3200. ExternalEditor = Editor->Data;
  3201. }
  3202. }
  3203. }
  3204. //---------------------------------------------------------------------------
  3205. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3206. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3207. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3208. {
  3209. UnicodeString OriginalFileName;
  3210. UnicodeString LocalRootDirectory;
  3211. UnicodeString RemoteDirectory;
  3212. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3213. IsSideLocalBrowser(Side), MaskParams);
  3214. UnicodeString Counter;
  3215. UnicodeString LocalFileName;
  3216. bool Handled = false;
  3217. if (!IsSideLocalBrowser(Side))
  3218. {
  3219. // We need to trim VMS version here, so that we use name without version
  3220. // when uploading back to create a new version of the file
  3221. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3222. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3223. TObject * Token = NULL;
  3224. UnicodeString LocalDirectory;
  3225. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3226. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3227. LocalDirectory))
  3228. {
  3229. if (Token != NULL)
  3230. {
  3231. TForm * Form = dynamic_cast<TForm *>(Token);
  3232. if (Form->WindowState == wsMinimized)
  3233. {
  3234. ShowWindow(Form->Handle, SW_RESTORE);
  3235. }
  3236. else
  3237. {
  3238. Form->SetFocus();
  3239. }
  3240. Handled = true;
  3241. }
  3242. else
  3243. {
  3244. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3245. }
  3246. }
  3247. if (!Handled)
  3248. {
  3249. TStringList * FileList1 = new TStringList();
  3250. try
  3251. {
  3252. FileList1->AddObject(FullFileName, Object);
  3253. TemporarilyDownloadFiles(FileList1,
  3254. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3255. LocalRootDirectory, LocalDirectory, true, true, true);
  3256. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3257. }
  3258. __finally
  3259. {
  3260. delete FileList1;
  3261. }
  3262. switch (ExecuteFileBy)
  3263. {
  3264. case efShell:
  3265. Counter = "RemoteFilesExecuted";
  3266. break;
  3267. case efInternalEditor:
  3268. Counter = "RemoteFilesOpenedInInternalEditor";
  3269. break;
  3270. case efExternalEditor:
  3271. Counter = "RemoteFilesOpenedInExternalEditor";
  3272. break;
  3273. default:
  3274. DebugFail();
  3275. }
  3276. }
  3277. }
  3278. else
  3279. {
  3280. LocalFileName = FullFileName;
  3281. OriginalFileName = ExtractFileName(FullFileName);
  3282. switch (ExecuteFileBy)
  3283. {
  3284. case efShell:
  3285. Counter = "LocalFilesExecuted";
  3286. break;
  3287. case efInternalEditor:
  3288. Counter = "LocalFilesOpenedInInternalEditor";
  3289. break;
  3290. case efExternalEditor:
  3291. Counter = "LocalFilesOpenedInExternalEditor";
  3292. break;
  3293. default:
  3294. DebugFail();
  3295. }
  3296. }
  3297. if (!Handled)
  3298. {
  3299. Configuration->Usage->Inc(Counter);
  3300. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3301. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3302. }
  3303. }
  3304. //---------------------------------------------------------------------------
  3305. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3306. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3307. bool AllSelected, bool OnFocused)
  3308. {
  3309. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3310. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3311. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3312. Side = GetSide(Side);
  3313. TCustomDirView * DView = DirView(Side);
  3314. TStrings * FileList =
  3315. AllSelected ?
  3316. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3317. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3318. try
  3319. {
  3320. DebugAssert(AllSelected || (FileList->Count == 1));
  3321. for (int i = 0; i < FileList->Count; i++)
  3322. {
  3323. UnicodeString ListFileName = FileList->Strings[i];
  3324. UnicodeString FileNameOnly =
  3325. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3326. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3327. if (!DView->ItemIsDirectory(Item))
  3328. {
  3329. UnicodeString FullFileName;
  3330. if (!IsSideLocalBrowser(Side))
  3331. {
  3332. FullFileName = DirView(Side)->Path + ListFileName;
  3333. }
  3334. else
  3335. {
  3336. FullFileName = ListFileName;
  3337. }
  3338. TFileMasks::TParams MaskParams;
  3339. MaskParams.Size = DView->ItemFileSize(Item);
  3340. TDateTimePrecision Precision;
  3341. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3342. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3343. FileList->Objects[i], MaskParams);
  3344. }
  3345. }
  3346. }
  3347. __finally
  3348. {
  3349. delete FileList;
  3350. }
  3351. }
  3352. //---------------------------------------------------------------------------
  3353. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3354. {
  3355. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3356. CopyParam.FileNameCase = ncNoChange;
  3357. CopyParam.PreserveRights = false;
  3358. CopyParam.PreserveReadOnly = false;
  3359. CopyParam.ReplaceInvalidChars = true;
  3360. CopyParam.IncludeFileMask = TFileMasks();
  3361. CopyParam.NewerOnly = false;
  3362. CopyParam.FileMask = L"";
  3363. }
  3364. //---------------------------------------------------------------------------
  3365. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3366. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3367. {
  3368. TTerminalManager * Manager = TTerminalManager::Instance();
  3369. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3370. {
  3371. if (!NonVisualDataModule->Busy)
  3372. {
  3373. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3374. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3375. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3376. if (Data->Terminal == NULL)
  3377. {
  3378. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3379. if (SameSiteTerminal != NULL)
  3380. {
  3381. UnicodeString Message =
  3382. FMTLOAD(EDIT_SESSION_REATTACH,
  3383. (FileNameOnly, Data->SessionName, FileNameOnly));
  3384. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3385. {
  3386. Data->Terminal = Terminal;
  3387. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3388. Data->SessionName = Terminal->SessionData->SessionName;
  3389. // We might also overwrite session data
  3390. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3391. }
  3392. else
  3393. {
  3394. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3395. Abort();
  3396. }
  3397. }
  3398. }
  3399. // foreground session should reconnect itself
  3400. else if (Terminal != Data->Terminal)
  3401. {
  3402. UnicodeString Message =
  3403. MainInstructions(
  3404. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3405. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3406. {
  3407. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3408. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3409. }
  3410. else
  3411. {
  3412. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3413. Abort();
  3414. }
  3415. }
  3416. }
  3417. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3418. {
  3419. Configuration->Usage->Inc(L"EditInactiveSession");
  3420. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3421. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3422. (ExtractFileName(FileName), Data->SessionName)));
  3423. }
  3424. }
  3425. TStrings * FileList = new TStringList();
  3426. try
  3427. {
  3428. FileList->Add(FileName);
  3429. // consider using the same settings (preset) as when the file was downloaded
  3430. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3431. TemporaryFileCopyParam(CopyParam);
  3432. if (Data->ForceText)
  3433. {
  3434. CopyParam.TransferMode = tmAscii;
  3435. }
  3436. // so i do not need to worry if masking algorithm works in all cases
  3437. // ("" means "copy file name", no masking is actually done)
  3438. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3439. {
  3440. CopyParam.FileMask = L"";
  3441. }
  3442. else
  3443. {
  3444. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3445. }
  3446. DebugAssert(Data->Queue != NULL);
  3447. int Params = cpNoConfirmation | cpTemporary;
  3448. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3449. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3450. QueueItem->CompleteEvent = UploadCompleteEvent;
  3451. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3452. }
  3453. __finally
  3454. {
  3455. delete FileList;
  3456. }
  3457. }
  3458. //---------------------------------------------------------------------------
  3459. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3460. const UnicodeString FileName, const TEditedFileData * Data)
  3461. {
  3462. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3463. DebugAssert(!NonVisualDataModule->Busy);
  3464. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3465. {
  3466. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3467. (ExtractFileName(FileName), Data->SessionName)));
  3468. }
  3469. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3470. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3471. NonVisualDataModule->StartBusy();
  3472. try
  3473. {
  3474. std::unique_ptr<TRemoteFile> File;
  3475. UnicodeString RemoteFileName =
  3476. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3477. FTerminal->ExceptionOnFail = true;
  3478. try
  3479. {
  3480. TRemoteFile * AFile = NULL;
  3481. FTerminal->ReadFile(RemoteFileName, AFile);
  3482. File.reset(AFile);
  3483. if (!File->HaveFullFileName)
  3484. {
  3485. File->FullFileName = RemoteFileName;
  3486. }
  3487. }
  3488. __finally
  3489. {
  3490. FTerminal->ExceptionOnFail = false;
  3491. }
  3492. std::unique_ptr<TStrings> FileList(new TStringList());
  3493. FileList->AddObject(RemoteFileName, File.get());
  3494. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3495. UnicodeString TempDir = ExtractFilePath(FileName);
  3496. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3497. TempDir, true, true, true);
  3498. // sanity check, the target file name should be still the same
  3499. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3500. }
  3501. __finally
  3502. {
  3503. NonVisualDataModule->EndBusy();
  3504. // it actually may not exist anymore...
  3505. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3506. }
  3507. }
  3508. //---------------------------------------------------------------------------
  3509. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3510. const TEditedFileData * Data, bool & KeepOpen)
  3511. {
  3512. // Command is set for external editors only (not for "shell" open).
  3513. if (!Data->Command.IsEmpty())
  3514. {
  3515. bool AnyFound = false;
  3516. bool AnyMDI = false;
  3517. bool AnyNonMDI = false;
  3518. bool AnyDetect = false;
  3519. TEditorList * EditorList = new TEditorList();
  3520. try
  3521. {
  3522. *EditorList = *WinConfiguration->EditorList;
  3523. for (int i = 0; i < EditorList->Count; i++)
  3524. {
  3525. const TEditorPreferences * Editor = EditorList->Editors[i];
  3526. if ((Editor->Data->Editor == edExternal) &&
  3527. (Editor->Data->ExternalEditor == Data->Command))
  3528. {
  3529. AnyFound = true;
  3530. if (Editor->Data->SDIExternalEditor)
  3531. {
  3532. AnyNonMDI = true;
  3533. if (Editor->Data->DetectMDIExternalEditor)
  3534. {
  3535. AnyDetect = true;
  3536. }
  3537. }
  3538. else
  3539. {
  3540. AnyMDI = true;
  3541. }
  3542. }
  3543. }
  3544. bool EnableMDI = false;
  3545. bool DisableDetect = false;
  3546. if (AnyMDI)
  3547. {
  3548. KeepOpen = true;
  3549. if (AnyNonMDI)
  3550. {
  3551. // there is at least one instance of the editor with MDI support enabled,
  3552. // and one with disabled, enable it for all instances
  3553. EnableMDI = true;
  3554. }
  3555. }
  3556. else if (AnyFound && !AnyDetect)
  3557. {
  3558. // at least once instance found but all have MDI autodetection disabled
  3559. // => close the file (default action)
  3560. }
  3561. else
  3562. {
  3563. // no instance of the editor has MDI support enabled
  3564. TMessageParams Params;
  3565. if (AnyFound)
  3566. {
  3567. // there is at least one instance of the editor
  3568. Params.Params |= mpNeverAskAgainCheck;
  3569. }
  3570. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3571. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3572. switch (Answer)
  3573. {
  3574. case qaNeverAskAgain:
  3575. DisableDetect = true;
  3576. break;
  3577. case qaNo:
  3578. EnableMDI = true;
  3579. KeepOpen = true;
  3580. break;
  3581. }
  3582. }
  3583. if (AnyFound && (EnableMDI || DisableDetect))
  3584. {
  3585. bool Changed = false;
  3586. for (int i = 0; i < EditorList->Count; i++)
  3587. {
  3588. const TEditorPreferences * Editor = EditorList->Editors[i];
  3589. if ((Editor->Data->Editor == edExternal) &&
  3590. (Editor->Data->ExternalEditor == Data->Command) &&
  3591. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3592. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3593. {
  3594. Changed = true;
  3595. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3596. if (EnableMDI)
  3597. {
  3598. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3599. }
  3600. if (DisableDetect)
  3601. {
  3602. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3603. }
  3604. EditorList->Change(i, UpdatedEditor);
  3605. }
  3606. }
  3607. if (Changed)
  3608. {
  3609. WinConfiguration->EditorList = EditorList;
  3610. }
  3611. }
  3612. }
  3613. __finally
  3614. {
  3615. delete EditorList;
  3616. }
  3617. }
  3618. else
  3619. {
  3620. // "open" case
  3621. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3622. qaOK, HELP_APP_EARLY_CLOSED);
  3623. }
  3624. }
  3625. //---------------------------------------------------------------------------
  3626. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3627. {
  3628. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3629. }
  3630. //---------------------------------------------------------------------------
  3631. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3632. {
  3633. SideEnter(osRemote);
  3634. }
  3635. //---------------------------------------------------------------------------
  3636. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3637. {
  3638. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3639. SideEnter(osRemote);
  3640. }
  3641. //---------------------------------------------------------------------------
  3642. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3643. {
  3644. FCurrentSide = Side;
  3645. if (Visible)
  3646. {
  3647. UpdateControls();
  3648. }
  3649. }
  3650. //---------------------------------------------------------------------------
  3651. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3652. TStrings * FileList, bool Alternative)
  3653. {
  3654. TCustomDirView * DView = DirView(Side);
  3655. DView->SaveSelection();
  3656. DView->SaveSelectedNames();
  3657. DebugAssert(!FAlternativeDelete);
  3658. FAlternativeDelete = Alternative;
  3659. try
  3660. {
  3661. if (!IsSideLocalBrowser(Side))
  3662. {
  3663. DebugAssert(Terminal != NULL);
  3664. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3665. }
  3666. else
  3667. {
  3668. try
  3669. {
  3670. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3671. }
  3672. __finally
  3673. {
  3674. ReloadLocalDirectory();
  3675. }
  3676. }
  3677. FAlternativeDelete = false;
  3678. }
  3679. catch(...)
  3680. {
  3681. FAlternativeDelete = false;
  3682. DView->DiscardSavedSelection();
  3683. throw;
  3684. }
  3685. DView->RestoreSelection();
  3686. }
  3687. //---------------------------------------------------------------------------
  3688. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3689. {
  3690. DebugAssert(Terminal);
  3691. DebugAssert(!IsLocalBrowserMode());
  3692. RemoteDirView->SaveSelection();
  3693. RemoteDirView->SaveSelectedNames();
  3694. try
  3695. {
  3696. if (Lock)
  3697. {
  3698. Terminal->LockFiles(FileList);
  3699. }
  3700. else
  3701. {
  3702. Terminal->UnlockFiles(FileList);
  3703. }
  3704. }
  3705. catch(...)
  3706. {
  3707. RemoteDirView->DiscardSavedSelection();
  3708. throw;
  3709. }
  3710. RemoteDirView->RestoreSelection();
  3711. }
  3712. //---------------------------------------------------------------------------
  3713. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3714. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3715. bool NoConfirmation, bool Move)
  3716. {
  3717. DebugAssert(Terminal != NULL);
  3718. DebugAssert(!IsLocalBrowserMode());
  3719. // update Terminal->StateData->RemoteDirectory
  3720. UpdateTerminal(Terminal);
  3721. if (Session == NULL)
  3722. {
  3723. Session = TTerminalManager::Instance()->ActiveTerminal;
  3724. }
  3725. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3726. {
  3727. if (RemoteDriveView->DropTarget != NULL)
  3728. {
  3729. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3730. }
  3731. else if (RemoteDirView->DropTarget != NULL)
  3732. {
  3733. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3734. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3735. }
  3736. else
  3737. {
  3738. Target = RemoteDirView->Path;
  3739. }
  3740. }
  3741. else
  3742. {
  3743. Target = Session->StateData->RemoteDirectory;
  3744. }
  3745. Target = UnixIncludeTrailingBackslash(Target);
  3746. if (FileList->Count == 1)
  3747. {
  3748. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3749. }
  3750. else
  3751. {
  3752. FileMask = AnyMask;
  3753. }
  3754. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3755. bool Result = true;
  3756. if (!NoConfirmation)
  3757. {
  3758. bool Multi = (FileList->Count > 1);
  3759. if (Move)
  3760. {
  3761. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3762. }
  3763. else
  3764. {
  3765. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3766. TStrings * Directories = new TStringList;
  3767. try
  3768. {
  3769. for (int Index = 0; Index < Sessions->Count; Index++)
  3770. {
  3771. TManagedTerminal * Terminal =
  3772. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3773. Directories->Add(Terminal->StateData->RemoteDirectory);
  3774. }
  3775. TDirectRemoteCopy AllowDirectCopy;
  3776. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3777. {
  3778. DebugAssert(DirectCopy);
  3779. AllowDirectCopy = drcAllow;
  3780. }
  3781. else if (FTerminal->IsCapable[fcSecondaryShell])
  3782. {
  3783. DebugAssert(DirectCopy);
  3784. AllowDirectCopy = drcConfirmCommandSession;
  3785. }
  3786. else
  3787. {
  3788. DebugAssert(!DirectCopy);
  3789. AllowDirectCopy = drcDisallow;
  3790. }
  3791. void * ASession = Session;
  3792. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3793. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3794. Session = static_cast<TManagedTerminal *>(ASession);
  3795. }
  3796. __finally
  3797. {
  3798. delete Directories;
  3799. }
  3800. }
  3801. }
  3802. return Result;
  3803. }
  3804. //---------------------------------------------------------------------------
  3805. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3806. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3807. {
  3808. DebugAssert(!IsLocalBrowserMode());
  3809. bool DirectCopy;
  3810. UnicodeString Target, FileMask;
  3811. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3812. if (Result)
  3813. {
  3814. if (!Move && !DirectCopy)
  3815. {
  3816. Configuration->Usage->Inc("RemoteCopyTemp");
  3817. UnicodeString RootTempDir;
  3818. UnicodeString TempDir;
  3819. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3820. TStrings * TemporaryFilesList = new TStringList();
  3821. try
  3822. {
  3823. TMakeLocalFileListParams MakeFileListParam;
  3824. MakeFileListParam.FileList = TemporaryFilesList;
  3825. MakeFileListParam.FileTimes = NULL;
  3826. MakeFileListParam.IncludeDirs = true;
  3827. MakeFileListParam.Recursive = false;
  3828. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3829. TTerminalManager::Instance()->ActiveTerminal = Session;
  3830. if (TemporaryFilesList->Count > 0)
  3831. {
  3832. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3833. CopyParam.FileMask = FileMask;
  3834. DebugAssert(!FAutoOperation);
  3835. FAutoOperation = true;
  3836. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3837. }
  3838. }
  3839. __finally
  3840. {
  3841. delete TemporaryFilesList;
  3842. FAutoOperation = false;
  3843. if (!RootTempDir.IsEmpty())
  3844. {
  3845. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3846. }
  3847. }
  3848. }
  3849. else
  3850. {
  3851. RemoteDirView->SaveSelection();
  3852. RemoteDirView->SaveSelectedNames();
  3853. try
  3854. {
  3855. if (Move)
  3856. {
  3857. Configuration->Usage->Inc("RemoteMove");
  3858. Terminal->MoveFiles(FileList, Target, FileMask);
  3859. }
  3860. else
  3861. {
  3862. Configuration->Usage->Inc("RemoteCopyDirect");
  3863. DebugAssert(DirectCopy);
  3864. DebugAssert(Session == FTerminal);
  3865. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3866. FTerminal->CommandSessionOpened ||
  3867. CommandSessionFallback())
  3868. {
  3869. Terminal->CopyFiles(FileList, Target, FileMask);
  3870. }
  3871. }
  3872. }
  3873. catch(...)
  3874. {
  3875. RemoteDirView->DiscardSavedSelection();
  3876. throw;
  3877. }
  3878. RemoteDirView->RestoreSelection();
  3879. }
  3880. }
  3881. return Result;
  3882. }
  3883. //---------------------------------------------------------------------------
  3884. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3885. const UnicodeString & Path, TRemoteProperties & Properties)
  3886. {
  3887. DebugAssert(!IsLocalBrowserMode());
  3888. Properties.Valid = Properties.Valid << vpEncrypt;
  3889. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3890. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3891. }
  3892. //---------------------------------------------------------------------------
  3893. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3894. {
  3895. Side = GetSide(Side);
  3896. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3897. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3898. UnicodeString Name = LoadStr(NEW_FOLDER);
  3899. int AllowedChanges =
  3900. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3901. bool SaveSettings = false;
  3902. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3903. {
  3904. TWindowLock Lock(this);
  3905. if (!IsSideLocalBrowser(Side))
  3906. {
  3907. if (SaveSettings)
  3908. {
  3909. GUIConfiguration->NewDirectoryProperties = Properties;
  3910. }
  3911. CreateRemoteDirectory(Name, Properties);
  3912. }
  3913. else
  3914. {
  3915. DirView(Side)->CreateDirectory(Name);
  3916. }
  3917. }
  3918. }
  3919. //---------------------------------------------------------------------------
  3920. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3921. {
  3922. TWindowLock Lock(this);
  3923. DirView(Side)->ExecuteHomeDirectory();
  3924. }
  3925. //---------------------------------------------------------------------------
  3926. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3927. {
  3928. DoOpenDirectoryDialog(odBrowse, Side);
  3929. }
  3930. //---------------------------------------------------------------------------
  3931. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3932. {
  3933. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3934. bool Result = !Path.IsEmpty();
  3935. if (Result)
  3936. {
  3937. // While we might get here when the session is closed (from location profiles),
  3938. // it's not a problem as the Path setter is noop then
  3939. DirView(Side)->Path = Path;
  3940. }
  3941. return Result;
  3942. }
  3943. //---------------------------------------------------------------------------
  3944. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3945. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3946. bool & Close)
  3947. {
  3948. // terminal can be already closed (e.g. dropped connection)
  3949. if (Terminal != NULL)
  3950. {
  3951. try
  3952. {
  3953. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3954. }
  3955. catch(...)
  3956. {
  3957. if (!Terminal->Active)
  3958. {
  3959. Close = true;
  3960. }
  3961. throw;
  3962. }
  3963. }
  3964. }
  3965. //---------------------------------------------------------------------------
  3966. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3967. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3968. bool & Close)
  3969. {
  3970. // terminal can be already closed (e.g. dropped connection)
  3971. if (Terminal != NULL)
  3972. {
  3973. Configuration->Usage->Inc(L"ChecksumCalculated");
  3974. try
  3975. {
  3976. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3977. }
  3978. catch(...)
  3979. {
  3980. if (!Terminal->Active)
  3981. {
  3982. Close = true;
  3983. }
  3984. throw;
  3985. }
  3986. }
  3987. }
  3988. //---------------------------------------------------------------------------
  3989. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3990. {
  3991. bool Result;
  3992. if (!IsSideLocalBrowser(Side))
  3993. {
  3994. TRemoteTokenList * GroupList = NULL;
  3995. TRemoteTokenList * UserList = NULL;
  3996. try
  3997. {
  3998. TRemoteProperties CurrentProperties;
  3999. if (Terminal->LoadFilesProperties(FileList))
  4000. {
  4001. RemoteDirView->Invalidate();
  4002. }
  4003. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4004. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4005. if (CapableGroupChanging || CapableOwnerChanging)
  4006. {
  4007. if (CapableGroupChanging)
  4008. {
  4009. GroupList = Terminal->Groups->Duplicate();
  4010. }
  4011. if (CapableOwnerChanging)
  4012. {
  4013. UserList = Terminal->Users->Duplicate();
  4014. }
  4015. TRemoteDirectory * Files = Terminal->Files;
  4016. int Count = Files->Count;
  4017. if (Count > 100)
  4018. {
  4019. Count = 100;
  4020. }
  4021. for (int Index = 0; Index < Count; Index++)
  4022. {
  4023. TRemoteFile * File = Files->Files[Index];
  4024. if (CapableGroupChanging)
  4025. {
  4026. GroupList->AddUnique(File->Group);
  4027. }
  4028. if (CapableOwnerChanging)
  4029. {
  4030. UserList->AddUnique(File->Owner);
  4031. }
  4032. }
  4033. // if we haven't collected tokens for all files in current directory,
  4034. // make sure we collect them at least for all selected files.
  4035. // (note that so far the files in FileList has to be from current direcotry)
  4036. if (Count < Files->Count)
  4037. {
  4038. for (int Index = 0; Index < FileList->Count; Index++)
  4039. {
  4040. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4041. if (CapableGroupChanging)
  4042. {
  4043. GroupList->AddUnique(File->Group);
  4044. }
  4045. if (CapableOwnerChanging)
  4046. {
  4047. UserList->AddUnique(File->Owner);
  4048. }
  4049. }
  4050. }
  4051. }
  4052. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4053. int Flags = 0;
  4054. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4055. if (CapableOwnerChanging) Flags |= cpOwner;
  4056. if (CapableGroupChanging) Flags |= cpGroup;
  4057. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4058. if (Terminal->IsCapable[fcCalculatingChecksum])
  4059. {
  4060. CalculateChecksumEvent = CalculateChecksum;
  4061. }
  4062. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4063. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4064. TRemoteProperties NewProperties = CurrentProperties;
  4065. Result =
  4066. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4067. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4068. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4069. CalculateSize, CalculateChecksumEvent);
  4070. if (Result)
  4071. {
  4072. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4073. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4074. }
  4075. }
  4076. __finally
  4077. {
  4078. delete GroupList;
  4079. delete UserList;
  4080. }
  4081. }
  4082. else
  4083. {
  4084. DirView(Side)->DisplayPropertiesMenu();
  4085. Result = true;
  4086. }
  4087. return Result;
  4088. }
  4089. //---------------------------------------------------------------------------
  4090. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4091. {
  4092. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4093. {
  4094. FIgnoreNextDialogChar = Key;
  4095. }
  4096. Key = 0;
  4097. }
  4098. //---------------------------------------------------------------------------
  4099. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4100. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4101. {
  4102. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4103. if (Command != NULL)
  4104. {
  4105. KeyProcessed(Key, Shift);
  4106. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4107. {
  4108. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4109. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4110. false, false, const_cast<TCustomCommandType *>(Command));
  4111. }
  4112. }
  4113. }
  4114. //---------------------------------------------------------------------------
  4115. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4116. {
  4117. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4118. {
  4119. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4120. }
  4121. if (!DirView(osCurrent)->IsEditing())
  4122. {
  4123. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4124. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4125. {
  4126. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4127. if (((Action->ShortCut == KeyShortCut) ||
  4128. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4129. AllowedAction(Action, aaShortCut))
  4130. {
  4131. // Has to be called before the action as the dialog char is already in queue.
  4132. // So when the action consumes message queue, we already need to have the
  4133. // FIgnoreNextDialogChar set
  4134. KeyProcessed(Key, Shift);
  4135. // Reset reference to previous component (when menu/toolbar was clicked).
  4136. // Needed to detect that action was invoked by keyboard shortcut
  4137. // in TNonVisualDataModule::ExplorerActionsExecute
  4138. Action->ActionComponent = NULL;
  4139. Action->Execute();
  4140. return;
  4141. }
  4142. }
  4143. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4144. {
  4145. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4146. {
  4147. KeyProcessed(Key, Shift);
  4148. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4149. return;
  4150. }
  4151. }
  4152. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4153. {
  4154. KeyProcessed(Key, Shift);
  4155. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4156. }
  4157. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4158. if (IsCustomShortCut(CustomShortCut))
  4159. {
  4160. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4161. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4162. if (WinConfiguration->SharedBookmarks != NULL)
  4163. {
  4164. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4165. if ((Bookmark != NULL) &&
  4166. OpenBookmark(FCurrentSide, Bookmark))
  4167. {
  4168. KeyProcessed(Key, Shift);
  4169. }
  4170. }
  4171. }
  4172. }
  4173. TForm::KeyDown(Key, Shift);
  4174. }
  4175. //---------------------------------------------------------------------------
  4176. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4177. {
  4178. DebugAssert(Terminal != NULL);
  4179. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4180. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4181. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4182. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4183. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4184. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4185. // expanded from ?: to avoid memory leaks
  4186. if (SecurityEnabled)
  4187. {
  4188. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4189. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4190. }
  4191. else
  4192. {
  4193. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4194. }
  4195. if (FileSystemInfo.ProtocolName.IsEmpty())
  4196. {
  4197. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4198. }
  4199. else
  4200. {
  4201. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4202. }
  4203. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4204. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4205. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4206. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4207. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4208. UnicodeString CompressionStr = CSCompressionStr;
  4209. if (CSCompressionStr != SCCompressionStr)
  4210. {
  4211. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4212. }
  4213. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4214. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4215. UpdateStatusBar();
  4216. }
  4217. //---------------------------------------------------------------------------
  4218. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4219. {
  4220. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4221. DebugAssert(SessionStatusBar != NULL);
  4222. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4223. {
  4224. // note: (Terminal->Status < sshReady) currently never happens here,
  4225. // so STATUS_CONNECTING is never used
  4226. SessionStatusBar->SimplePanel = true;
  4227. SessionStatusBar->SimpleText = LoadStr(
  4228. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4229. }
  4230. else
  4231. {
  4232. DebugAssert(Terminal);
  4233. SessionStatusBar->SimplePanel = false;
  4234. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4235. if (!FNote.IsEmpty())
  4236. {
  4237. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4238. }
  4239. else
  4240. {
  4241. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4242. }
  4243. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4244. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4245. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4246. }
  4247. }
  4248. //---------------------------------------------------------------------------
  4249. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4250. {
  4251. Panel->Caption = L"";
  4252. }
  4253. //---------------------------------------------------------------------------
  4254. void __fastcall TCustomScpExplorerForm::Idle()
  4255. {
  4256. if (FShowing || FStandaloneEditing)
  4257. {
  4258. FEditorManager->Check();
  4259. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4260. // that needs to know if queue view is visible (and it may be closed after queue update)
  4261. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4262. }
  4263. if (FShowing)
  4264. {
  4265. if (!NonVisualDataModule->Busy &&
  4266. // Menu is opened or drag&drop is going on
  4267. (Mouse->Capture == NULL))
  4268. {
  4269. if (FRefreshRemoteDirectory)
  4270. {
  4271. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4272. {
  4273. Terminal->RefreshDirectory();
  4274. }
  4275. FRefreshRemoteDirectory = false;
  4276. }
  4277. if (FRefreshLocalDirectory)
  4278. {
  4279. ReloadLocalDirectory();
  4280. FRefreshLocalDirectory = false;
  4281. }
  4282. if (WinConfiguration->RefreshRemotePanel)
  4283. {
  4284. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4285. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4286. {
  4287. RemoteDirView->ReloadDirectory();
  4288. }
  4289. }
  4290. }
  4291. }
  4292. if (FShowing || FStandaloneEditing)
  4293. {
  4294. if (FQueueStatusInvalidated)
  4295. {
  4296. UpdateQueueStatus(false);
  4297. }
  4298. RefreshQueueItems();
  4299. }
  4300. if (FShowing)
  4301. {
  4302. UpdateStatusBar();
  4303. }
  4304. FIgnoreNextDialogChar = 0;
  4305. }
  4306. //---------------------------------------------------------------------------
  4307. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4308. {
  4309. try
  4310. {
  4311. FUserActionTimer->Enabled = false;
  4312. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4313. {
  4314. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4315. {
  4316. FPendingQueueActionItem->ProcessUserAction();
  4317. }
  4318. }
  4319. }
  4320. __finally
  4321. {
  4322. FPendingQueueActionItem = NULL;
  4323. }
  4324. }
  4325. //---------------------------------------------------------------------------
  4326. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4327. {
  4328. if (WinConfiguration->MinimizeToTray)
  4329. {
  4330. UpdateTrayIcon();
  4331. FTrayIcon->Visible = true;
  4332. if (Visible)
  4333. {
  4334. ShowWindow(Handle, SW_HIDE);
  4335. }
  4336. }
  4337. }
  4338. //---------------------------------------------------------------------------
  4339. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4340. {
  4341. // WORKAROUND
  4342. // When restoring maximized window from minimization,
  4343. // rarely some controls do not align properly.
  4344. // Two instances seen (both for Commander):
  4345. // - When restoring, window is temporarily narrower (not maximized),
  4346. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4347. // Once maximized already, top dock shinks back, but the session PageControl,
  4348. // do not align up, leaving space between TopDock and PageControl.
  4349. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4350. for (int Index = 0; Index < ControlCount; Index++)
  4351. {
  4352. RealignControl(Controls[Index]);
  4353. }
  4354. if (FTrayIcon->Visible)
  4355. {
  4356. FTrayIcon->Visible = false;
  4357. if (Visible)
  4358. {
  4359. ShowWindow(Handle, SW_SHOW);
  4360. }
  4361. }
  4362. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4363. {
  4364. FNeedSession = false;
  4365. NonVisualDataModule->StartBusy();
  4366. try
  4367. {
  4368. NeedSession(true);
  4369. }
  4370. __finally
  4371. {
  4372. NonVisualDataModule->EndBusy();
  4373. }
  4374. }
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4378. {
  4379. FTrayIcon->Hint = GetMainForm()->Caption;
  4380. }
  4381. //---------------------------------------------------------------------------
  4382. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4383. {
  4384. UpdateTrayIcon();
  4385. }
  4386. //---------------------------------------------------------------------------
  4387. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4388. {
  4389. // workaround
  4390. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4391. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4392. // But WM_ACTIVATEAPP message can be received even when
  4393. // the main window is minimized to the tray and internal editor window is focused
  4394. // (after another application was previously active)
  4395. if (::IsIconic(Handle))
  4396. {
  4397. if (!IsAppIconic())
  4398. {
  4399. SetAppIconic(true);
  4400. }
  4401. }
  4402. ::ApplicationRestore();
  4403. Application->BringToFront();
  4404. }
  4405. //---------------------------------------------------------------------------
  4406. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4407. {
  4408. RestoreApp();
  4409. }
  4410. //---------------------------------------------------------------------------
  4411. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4412. {
  4413. if (OpenInNewWindow())
  4414. {
  4415. // todo: Pass FromSite
  4416. ExecuteNewInstance(SessionUrl);
  4417. }
  4418. else
  4419. {
  4420. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4421. }
  4422. }
  4423. //---------------------------------------------------------------------------
  4424. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4425. {
  4426. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4427. StoredSessions->NewSession(SessionName, SessionData);
  4428. // modified only, explicit
  4429. StoredSessions->Save(false, true);
  4430. // encode session name because of slashes in hierarchical sessions
  4431. return EncodeUrlString(SessionName);
  4432. }
  4433. //---------------------------------------------------------------------------
  4434. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4435. {
  4436. // current working directories become defaults here, what is not right
  4437. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4438. return SaveHiddenDuplicateSession(SessionData.get());
  4439. }
  4440. //---------------------------------------------------------------------------
  4441. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4442. {
  4443. if (OpenInNewWindow())
  4444. {
  4445. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4446. }
  4447. else
  4448. {
  4449. // current working directories become defaults here, what is not right
  4450. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4451. TTerminalManager * Manager = TTerminalManager::Instance();
  4452. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4453. Manager->ActiveTerminal = Terminal;
  4454. }
  4455. }
  4456. //---------------------------------------------------------------------------
  4457. void __fastcall TCustomScpExplorerForm::RenameSession()
  4458. {
  4459. UnicodeString Name = Terminal->SessionData->SessionName;
  4460. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4461. {
  4462. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4463. TSessionData::ValidateName(Name);
  4464. // This is inconsistent with how color (for example) is handled.
  4465. Terminal->SessionData->Name = Name;
  4466. UpdateControls();
  4467. // Add/Remove distinguishing paths from sessions of the same name.
  4468. DoTerminalListChanged();
  4469. }
  4470. }
  4471. //---------------------------------------------------------------------------
  4472. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4473. {
  4474. DebugAssert(Queue != NULL);
  4475. bool Result = Queue->IsEmpty;
  4476. if (!Result)
  4477. {
  4478. SetFocus();
  4479. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4480. }
  4481. return Result;
  4482. }
  4483. //---------------------------------------------------------------------------
  4484. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4485. {
  4486. return CanCloseQueue(FQueue);
  4487. }
  4488. //---------------------------------------------------------------------------
  4489. void __fastcall TCustomScpExplorerForm::CloseSession()
  4490. {
  4491. if (CanCloseQueue())
  4492. {
  4493. TTerminalManager::Instance()->FreeActiveTerminal();
  4494. }
  4495. }
  4496. //---------------------------------------------------------------------------
  4497. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4498. {
  4499. if (CanCloseQueue())
  4500. {
  4501. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4502. }
  4503. }
  4504. //---------------------------------------------------------------------------
  4505. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4506. {
  4507. RemoteDirView->Terminal = Terminal;
  4508. UpdateRemotePathComboBox(false);
  4509. UpdateControls();
  4510. }
  4511. //---------------------------------------------------------------------------
  4512. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4513. {
  4514. if (DebugAlwaysTrue(!Terminal->Active))
  4515. {
  4516. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4517. }
  4518. }
  4519. //---------------------------------------------------------------------------
  4520. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4521. {
  4522. if (OpenInNewWindow())
  4523. {
  4524. // encode session name because of slashes in hierarchical sessions
  4525. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4526. }
  4527. else
  4528. {
  4529. TTerminalManager * Manager = TTerminalManager::Instance();
  4530. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4531. Manager->ActiveTerminal = Terminal;
  4532. }
  4533. }
  4534. //---------------------------------------------------------------------------
  4535. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4536. {
  4537. TTerminalManager * Manager = TTerminalManager::Instance();
  4538. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4539. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4540. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4541. if (!ConnectFirstTerminal)
  4542. {
  4543. FirstTerminal->Disconnected = true;
  4544. FirstTerminal->DisconnectedTemporarily = true;
  4545. }
  4546. Manager->ActiveTerminal = FirstTerminal;
  4547. }
  4548. //---------------------------------------------------------------------------
  4549. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4550. {
  4551. if (OpenInNewWindow())
  4552. {
  4553. ExecuteNewInstance(Name);
  4554. }
  4555. else
  4556. {
  4557. DoOpenFolderOrWorkspace(Name, true);
  4558. }
  4559. }
  4560. //---------------------------------------------------------------------------
  4561. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4562. bool &CanClose)
  4563. {
  4564. if (Terminal != NULL)
  4565. {
  4566. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4567. {
  4568. unsigned int Result;
  4569. TMessageParams Params(mpNeverAskAgainCheck);
  4570. UnicodeString Message;
  4571. int Answers = qaOK | qaCancel;
  4572. if (TTerminalManager::Instance()->Count > 1)
  4573. {
  4574. if (!WinConfiguration->AutoSaveWorkspace)
  4575. {
  4576. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4577. }
  4578. else
  4579. {
  4580. Message = LoadStr(CLOSE_SESSIONS);
  4581. }
  4582. }
  4583. else
  4584. {
  4585. if (!WinConfiguration->AutoSaveWorkspace)
  4586. {
  4587. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4588. }
  4589. else
  4590. {
  4591. Message = LoadStr(CLOSE_SESSION);
  4592. }
  4593. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4594. }
  4595. UnicodeString Note;
  4596. if (WinConfiguration->AutoSaveWorkspace)
  4597. {
  4598. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4599. }
  4600. else
  4601. {
  4602. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4603. Answers = qaYes | qaNo | qaCancel;
  4604. }
  4605. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4606. SetFocus();
  4607. Result = MessageDialog(Message, qtConfirmation,
  4608. Answers, HELP_NONE, &Params);
  4609. if (Result == qaNeverAskAgain)
  4610. {
  4611. WinConfiguration->ConfirmClosingSession = false;
  4612. }
  4613. if (Result == qaNo)
  4614. {
  4615. CanClose = SaveWorkspace(true);
  4616. // note that the workspace will be saved redundatly again from FormClose
  4617. }
  4618. else
  4619. {
  4620. CanClose =
  4621. (Result == qaOK) ||
  4622. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4623. (Result == qaNeverAskAgain);
  4624. }
  4625. }
  4626. if (CanClose)
  4627. {
  4628. CanClose = CanCloseQueue();
  4629. }
  4630. }
  4631. if (CanClose)
  4632. {
  4633. CanClose =
  4634. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4635. FEditorManager->CloseExternalFilesWithoutProcess();
  4636. if (CanClose)
  4637. {
  4638. while (CanClose && (FLocalEditors->Count > 0))
  4639. {
  4640. int PrevCount = FLocalEditors->Count;
  4641. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4642. CanClose = (FLocalEditors->Count < PrevCount);
  4643. }
  4644. if (CanClose)
  4645. {
  4646. CanClose = FEditorManager->Empty(true);
  4647. if (!CanClose)
  4648. {
  4649. SetFocus();
  4650. CanClose =
  4651. (MessageDialog(
  4652. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4653. }
  4654. }
  4655. }
  4656. }
  4657. }
  4658. //---------------------------------------------------------------------------
  4659. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4660. {
  4661. TForm * Form = dynamic_cast<TForm *>(Sender);
  4662. DebugAssert(Form != NULL);
  4663. Form->Close();
  4664. }
  4665. //---------------------------------------------------------------------------
  4666. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4667. {
  4668. TForm * Form = dynamic_cast<TForm *>(Sender);
  4669. delete Form;
  4670. }
  4671. //---------------------------------------------------------------------------
  4672. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4673. {
  4674. while (FLocalEditors->Count > 0)
  4675. {
  4676. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4677. }
  4678. }
  4679. //---------------------------------------------------------------------------
  4680. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4681. TObject *Sender)
  4682. {
  4683. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4684. try
  4685. {
  4686. SetProperties(osRemote, FileList);
  4687. }
  4688. __finally
  4689. {
  4690. delete FileList;
  4691. }
  4692. }
  4693. //---------------------------------------------------------------------------
  4694. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4695. {
  4696. if (value)
  4697. {
  4698. if (Component == fcCustomCommandsBand)
  4699. {
  4700. UpdateCustomCommandsToolbar();
  4701. }
  4702. }
  4703. }
  4704. //---------------------------------------------------------------------------
  4705. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4706. {
  4707. TControl * Control = GetComponent(Component);
  4708. DebugAssert(Control);
  4709. bool Changed = (Control->Visible != value);
  4710. if (Changed)
  4711. {
  4712. ComponentShowing(Component, value);
  4713. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4714. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4715. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4716. if (value)
  4717. {
  4718. int RemainingHeight = Control->Parent->ClientHeight;
  4719. int RemainingWidth = Control->Parent->ClientWidth;
  4720. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4721. {
  4722. TControl * ChildControl = Control->Parent->Controls[i];
  4723. if (ChildControl->Visible)
  4724. {
  4725. switch (ChildControl->Align)
  4726. {
  4727. case alTop:
  4728. case alBottom:
  4729. RemainingHeight -= ChildControl->Height;
  4730. break;
  4731. case alLeft:
  4732. case alRight:
  4733. RemainingWidth -= ChildControl->Width;
  4734. break;
  4735. }
  4736. }
  4737. }
  4738. int Reserve = ScaleByTextHeight(this, 32);
  4739. // queue in explorer, trees in commander
  4740. if (Control->Height > RemainingHeight - Reserve)
  4741. {
  4742. Control->Height = RemainingHeight / 2;
  4743. }
  4744. if (Control->Width > RemainingWidth - Reserve)
  4745. {
  4746. Control->Width = RemainingWidth / 2;
  4747. }
  4748. }
  4749. Control->Visible = value;
  4750. if (WasFocused && Visible)
  4751. {
  4752. DirView(osCurrent)->SetFocus();
  4753. }
  4754. FixControlsPlacement();
  4755. }
  4756. }
  4757. //---------------------------------------------------------------------------
  4758. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4759. {
  4760. TControl * Control = GetComponent(Component);
  4761. if (Control == NULL)
  4762. {
  4763. return false;
  4764. }
  4765. else
  4766. {
  4767. return Control->Visible;
  4768. }
  4769. }
  4770. //---------------------------------------------------------------------------
  4771. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4772. {
  4773. bool Result;
  4774. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4775. {
  4776. Result = !IsUWP();
  4777. }
  4778. else
  4779. {
  4780. Result = true;
  4781. }
  4782. return Result;
  4783. }
  4784. //---------------------------------------------------------------------------
  4785. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4786. {
  4787. if (DirView(osOther)->ItemFocused != NULL)
  4788. {
  4789. DirView(osOther)->ItemFocused->MakeVisible(false);
  4790. }
  4791. QueueSplitter->Visible = QueuePanel->Visible;
  4792. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4793. }
  4794. //---------------------------------------------------------------------------
  4795. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4796. {
  4797. switch (Component) {
  4798. case fcStatusBar: return RemoteStatusBar;
  4799. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4800. case fcQueueView: return QueuePanel;
  4801. case fcQueueToolbar: return QueueDock;
  4802. case fcRemoteTree: return RemoteDrivePanel;
  4803. case fcSessionsTabs: return SessionsPageControl;
  4804. default: return NULL;
  4805. }
  4806. }
  4807. //---------------------------------------------------------------------------
  4808. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4809. TObject *Sender, TListColumn *Column, TPoint &Point)
  4810. {
  4811. DebugAssert(NonVisualDataModule && Column && Sender);
  4812. NonVisualDataModule->ListColumn = Column;
  4813. TPopupMenu * DirViewColumnMenu;
  4814. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4815. {
  4816. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4817. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4818. (Column->Index == uvName);
  4819. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4820. (Column->Index == uvSize);
  4821. }
  4822. else
  4823. {
  4824. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4825. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4826. (Column->Index == dvName);
  4827. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4828. (Column->Index == dvSize);
  4829. }
  4830. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4831. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4832. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4833. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4834. }
  4835. //---------------------------------------------------------------------------
  4836. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4837. TObject *Sender, TListItem *Item, bool &AllowExec)
  4838. {
  4839. DoDirViewExecFile(Sender, Item, AllowExec);
  4840. }
  4841. //---------------------------------------------------------------------------
  4842. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4843. TListItem * Item, bool & AllowExec)
  4844. {
  4845. DebugAssert(Sender && Item && Configuration);
  4846. DebugAssert(AllowExec);
  4847. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4848. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4849. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4850. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4851. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4852. // on files only (not directories)
  4853. // and only when symlinks are resolved (apply to remote panel only)
  4854. if (!ADirView->ItemIsDirectory(Item) &&
  4855. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4856. {
  4857. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4858. !FForceExecution &&
  4859. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4860. {
  4861. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4862. {
  4863. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4864. UnlockWindow();
  4865. try
  4866. {
  4867. ExecuteFileOperation(
  4868. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4869. }
  4870. __finally
  4871. {
  4872. LockWindow();
  4873. }
  4874. AllowExec = false;
  4875. }
  4876. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4877. {
  4878. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4879. {
  4880. ExecuteFile(Side, efDefaultEditor);
  4881. AllowExec = false;
  4882. }
  4883. }
  4884. else
  4885. {
  4886. DebugFail();
  4887. }
  4888. }
  4889. // if we have not done anything special, fall back to default behavior
  4890. if (AllowExec)
  4891. {
  4892. if (Remote && !WinConfiguration->DisableOpenEdit)
  4893. {
  4894. ExecuteFile(osRemote, efShell);
  4895. AllowExec = false;
  4896. }
  4897. }
  4898. }
  4899. }
  4900. //---------------------------------------------------------------------------
  4901. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4902. {
  4903. return ((Side == osRemote) || (Side == osCurrent));
  4904. }
  4905. //---------------------------------------------------------------------------
  4906. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4907. {
  4908. DebugFail();
  4909. }
  4910. //---------------------------------------------------------------------------
  4911. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4912. {
  4913. DebugFail();
  4914. }
  4915. //---------------------------------------------------------------------------
  4916. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4917. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  4918. {
  4919. TSynchronizeParamType Params;
  4920. Params.LocalDirectory = LocalDirectory;
  4921. Params.RemoteDirectory = RemoteDirectory;
  4922. int UnusedParams =
  4923. (GUIConfiguration->SynchronizeParams &
  4924. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4925. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4926. Params.Options = GUIConfiguration->SynchronizeOptions;
  4927. bool SaveSettings = false;
  4928. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4929. &DoSynchronizeTooManyDirectories);
  4930. DebugAssert(FSynchronizeController == NULL);
  4931. FSynchronizeController = &Controller;
  4932. bool Result;
  4933. try
  4934. {
  4935. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4936. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4937. int Options =
  4938. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4939. DebugAssert(FOnFeedSynchronizeError == NULL);
  4940. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4941. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4942. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4943. if (Result)
  4944. {
  4945. if (SaveSettings)
  4946. {
  4947. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4948. GUIConfiguration->SynchronizeOptions = Params.Options;
  4949. }
  4950. else
  4951. {
  4952. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4953. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4954. FLAGSET(Params.Options, soSynchronize))
  4955. {
  4956. GUIConfiguration->SynchronizeOptions =
  4957. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4958. soSynchronize;
  4959. }
  4960. }
  4961. LocalDirectory = Params.LocalDirectory;
  4962. RemoteDirectory = Params.RemoteDirectory;
  4963. }
  4964. }
  4965. __finally
  4966. {
  4967. FSynchronizeController = NULL;
  4968. DebugAssert(FOnFeedSynchronizeError == NULL);
  4969. FOnFeedSynchronizeError = NULL;
  4970. }
  4971. return Result;
  4972. }
  4973. //---------------------------------------------------------------------------
  4974. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4975. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4976. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4977. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4978. TSynchronizeOptions * Options, bool Full)
  4979. {
  4980. // Keep-up-to-date mode
  4981. if (Terminal->Status == ssOpened)
  4982. {
  4983. try
  4984. {
  4985. int PParams = Params.Params;
  4986. if (!Full)
  4987. {
  4988. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4989. if (FLAGSET(Params.Options, soRecurse))
  4990. {
  4991. PParams |= TTerminal::spSubDirs;
  4992. }
  4993. }
  4994. else
  4995. {
  4996. // if keepuptodate is non-recursive,
  4997. // full sync before has to be non-recursive as well
  4998. if (FLAGCLEAR(Params.Options, soRecurse))
  4999. {
  5000. PParams |= TTerminal::spNoRecurse;
  5001. }
  5002. }
  5003. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5004. PParams, Checklist, Options);
  5005. }
  5006. catch (Exception & E)
  5007. {
  5008. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5009. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5010. {
  5011. // noop, already logged in MoreMessageDialog
  5012. }
  5013. else
  5014. {
  5015. ShowExtendedExceptionEx(Terminal, &E);
  5016. throw;
  5017. }
  5018. }
  5019. }
  5020. }
  5021. //---------------------------------------------------------------------------
  5022. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5023. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5024. const UnicodeString ErrorStr)
  5025. {
  5026. if (!Directory.IsEmpty())
  5027. {
  5028. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5029. }
  5030. else
  5031. {
  5032. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5033. }
  5034. }
  5035. //---------------------------------------------------------------------------
  5036. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5037. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5038. {
  5039. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5040. {
  5041. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5042. }
  5043. else
  5044. {
  5045. TMessageParams Params(mpNeverAskAgainCheck);
  5046. unsigned int Result = MessageDialog(
  5047. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5048. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5049. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5050. {
  5051. MaxDirectories *= 2;
  5052. if (Result == qaNeverAskAgain)
  5053. {
  5054. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5055. }
  5056. }
  5057. else
  5058. {
  5059. Abort();
  5060. }
  5061. }
  5062. }
  5063. //---------------------------------------------------------------------------
  5064. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5065. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5066. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5067. TSynchronizeOptions * Options)
  5068. {
  5069. // Keep-up-to-date mode
  5070. DebugAssert(!FAutoOperation);
  5071. void * BatchStorage;
  5072. BatchStart(BatchStorage);
  5073. FAutoOperation = true;
  5074. bool AnyOperation = false;
  5075. TDateTime StartTime = Now();
  5076. TSynchronizeChecklist * AChecklist = NULL;
  5077. try
  5078. {
  5079. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5080. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5081. {
  5082. FSynchronizeProgressForm->Start();
  5083. }
  5084. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5085. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5086. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5087. Options);
  5088. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5089. SAFE_DESTROY(FSynchronizeProgressForm);
  5090. AnyOperation = (AChecklist->CheckedCount > 0);
  5091. if (AnyOperation)
  5092. {
  5093. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5094. CreateProgressForm(&SynchronizeProgress);
  5095. try
  5096. {
  5097. Terminal->SynchronizeApply(
  5098. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5099. SynchronizeProcessedItem, NULL, NULL, NULL);
  5100. }
  5101. __finally
  5102. {
  5103. DestroyProgressForm();
  5104. }
  5105. }
  5106. }
  5107. __finally
  5108. {
  5109. if (Checklist == NULL)
  5110. {
  5111. delete AChecklist;
  5112. }
  5113. else
  5114. {
  5115. *Checklist = AChecklist;
  5116. }
  5117. FAutoOperation = false;
  5118. SAFE_DESTROY(FSynchronizeProgressForm);
  5119. BatchEnd(BatchStorage);
  5120. ReloadLocalDirectory();
  5121. if (AnyOperation)
  5122. {
  5123. OperationComplete(StartTime);
  5124. }
  5125. }
  5126. }
  5127. //---------------------------------------------------------------------------
  5128. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5129. {
  5130. // can be called from command line
  5131. return Visible &&
  5132. ((DirView(osOther)->SelCount > 0) ||
  5133. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5134. }
  5135. //---------------------------------------------------------------------------
  5136. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5137. {
  5138. LogSynchronizeEvent(Terminal, Message);
  5139. }
  5140. //---------------------------------------------------------------------------
  5141. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5142. int Params, TSynchronizeOptions & Options)
  5143. {
  5144. DebugAssert(!IsLocalBrowserMode());
  5145. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5146. {
  5147. Options.Filter = new TStringList();
  5148. Options.Filter->CaseSensitive = false;
  5149. Options.Filter->Duplicates = Types::dupAccept;
  5150. if (DirView(osRemote)->SelCount > 0)
  5151. {
  5152. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5153. }
  5154. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5155. {
  5156. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5157. }
  5158. Options.Filter->Sort();
  5159. }
  5160. }
  5161. //---------------------------------------------------------------------------
  5162. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5163. {
  5164. TCopyParamType Defaults;
  5165. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5166. ConfigStorage->AccessMode = smRead;
  5167. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5168. {
  5169. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5170. }
  5171. ConfigStorage.reset(NULL);
  5172. std::unique_ptr<TStringList> Options(new TStringList());
  5173. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5174. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5175. UnicodeString Result;
  5176. if (Options->Count > 0)
  5177. {
  5178. Result =
  5179. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5180. }
  5181. return Result;
  5182. }
  5183. //---------------------------------------------------------------------------
  5184. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5185. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5186. {
  5187. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5188. UnicodeString AdditionalParams =
  5189. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5190. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5191. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5192. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5193. SerializeCopyParamForCommandLine(CopyParams)));
  5194. ExecuteNewInstance(SessionName, AdditionalParams);
  5195. }
  5196. //---------------------------------------------------------------------------
  5197. struct TSynchronizeParams
  5198. {
  5199. UnicodeString LocalDirectory;
  5200. UnicodeString RemoteDirectory;
  5201. TSynchronizeMode Mode;
  5202. TSynchronizeChecklist * Checklist;
  5203. int Params;
  5204. TCopyParamType * CopyParam;
  5205. TDateTime CollectElapsed;
  5206. TDateTime * StartTime;
  5207. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5208. };
  5209. //---------------------------------------------------------------------------
  5210. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5211. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5212. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5213. {
  5214. DebugAssert(!FAutoOperation);
  5215. void * BatchStorage;
  5216. BatchStart(BatchStorage);
  5217. FAutoOperation = true;
  5218. TFileOperationStatistics Statistics;
  5219. TDateTime Start = Now();
  5220. try
  5221. {
  5222. Params.OnProcessedItem = OnProcessedItem;
  5223. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5224. CreateProgressForm(&SynchronizeProgress);
  5225. Terminal->SynchronizeApply(
  5226. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5227. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5228. &Statistics);
  5229. }
  5230. __finally
  5231. {
  5232. Params.OnProcessedItem = NULL;
  5233. FAutoOperation = false;
  5234. DestroyProgressForm();
  5235. BatchEnd(BatchStorage);
  5236. ReloadLocalDirectory();
  5237. }
  5238. if (WinConfiguration->SynchronizeSummary)
  5239. {
  5240. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5241. // The statistics should be 0 anyway in this case
  5242. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5243. {
  5244. Message += L"\n";
  5245. if (Statistics.FilesUploaded > 0)
  5246. {
  5247. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5248. }
  5249. if (Statistics.FilesDownloaded > 0)
  5250. {
  5251. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5252. }
  5253. if (Statistics.FilesDeletedLocal > 0)
  5254. {
  5255. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5256. }
  5257. if (Statistics.FilesDeletedRemote > 0)
  5258. {
  5259. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5260. }
  5261. }
  5262. TDateTime Elapsed = (Now() - Start);
  5263. Message +=
  5264. L"\n" +
  5265. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5266. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5267. TMessageParams Params(mpNeverAskAgainCheck);
  5268. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5269. if (Result == qaNeverAskAgain)
  5270. {
  5271. WinConfiguration->SynchronizeSummary = false;
  5272. }
  5273. }
  5274. }
  5275. //---------------------------------------------------------------------------
  5276. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5277. {
  5278. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5279. {
  5280. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5281. }
  5282. // Not set in keep-up-to-date mode - Synchronize() method
  5283. if (Token != NULL)
  5284. {
  5285. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5286. if (Params.OnProcessedItem != NULL)
  5287. {
  5288. Params.OnProcessedItem(NULL, ChecklistItem);
  5289. }
  5290. }
  5291. }
  5292. //---------------------------------------------------------------------------
  5293. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5294. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5295. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5296. {
  5297. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5298. *Params.StartTime = Now();
  5299. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5300. }
  5301. //---------------------------------------------------------------------------
  5302. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5303. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5304. {
  5305. // terminal can be already closed (e.g. dropped connection)
  5306. if (Terminal != NULL)
  5307. {
  5308. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5309. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5310. }
  5311. }
  5312. //---------------------------------------------------------------------------
  5313. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5314. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5315. {
  5316. DebugAssert(!IsLocalBrowserMode());
  5317. TAutoBatch AutoBatch(this);
  5318. TAutoFlag AutoOperationFlag(FAutoOperation);
  5319. if (Side == osRemote)
  5320. {
  5321. std::unique_ptr<TStrings> FileList(new TStringList());
  5322. FileList->AddObject(FileName, RemoteFile);
  5323. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5324. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5325. RemoteDirView->SaveSelection();
  5326. RemoteDirView->SaveSelectedNames();
  5327. try
  5328. {
  5329. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5330. }
  5331. catch(...)
  5332. {
  5333. RemoteDirView->DiscardSavedSelection();
  5334. throw;
  5335. }
  5336. RemoteDirView->RestoreSelection();
  5337. }
  5338. else if (DebugAlwaysTrue(Side == osLocal))
  5339. {
  5340. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5341. {
  5342. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5343. }
  5344. UnicodeString Directory = ExtractFileDir(FileName);
  5345. ReloadLocalDirectory(Directory);
  5346. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5347. if (!SamePaths(Directory, NewDirectory))
  5348. {
  5349. ReloadLocalDirectory(NewDirectory);
  5350. }
  5351. }
  5352. }
  5353. //---------------------------------------------------------------------------
  5354. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5355. {
  5356. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5357. if (Side == osLocal)
  5358. {
  5359. if (Action == TSynchronizeChecklist::saDownloadNew)
  5360. {
  5361. OpenFolderInExplorer(LocalPath);
  5362. }
  5363. else
  5364. {
  5365. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5366. }
  5367. }
  5368. else if (DebugAlwaysTrue(Side == osRemote))
  5369. {
  5370. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5371. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5372. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5373. if (!LocalPath.IsEmpty())
  5374. {
  5375. SessionData->LocalDirectory = LocalPath;
  5376. }
  5377. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5378. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5379. }
  5380. }
  5381. //---------------------------------------------------------------------------
  5382. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5383. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5384. const TCopyParamType * CopyParams)
  5385. {
  5386. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5387. UnicodeString AdditionalParams =
  5388. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5389. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5390. LocalDirectory, RemoteDirectory, Mode, Params,
  5391. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5392. SerializeCopyParamForCommandLine(CopyParams)));
  5393. ExecuteNewInstance(SessionName, AdditionalParams);
  5394. }
  5395. //---------------------------------------------------------------------------
  5396. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5397. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5398. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5399. {
  5400. int Result;
  5401. bool SaveSettings = false;
  5402. int Options =
  5403. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5404. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5405. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5406. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5407. bool Continue =
  5408. (UseDefaults == 0) ||
  5409. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5410. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5411. if (Continue)
  5412. {
  5413. Configuration->Usage->Inc(L"Synchronizations");
  5414. UpdateCopyParamCounters(CopyParam);
  5415. TSynchronizeOptions SynchronizeOptions;
  5416. GetSynchronizeOptions(Params, SynchronizeOptions);
  5417. if (SaveSettings)
  5418. {
  5419. GUIConfiguration->SynchronizeParams = Params;
  5420. }
  5421. else
  5422. {
  5423. SaveMode = false;
  5424. }
  5425. TDateTime StartTime = Now();
  5426. TSynchronizeChecklist * Checklist = NULL;
  5427. try
  5428. {
  5429. DebugAssert(!FAutoOperation);
  5430. FAutoOperation = true;
  5431. try
  5432. {
  5433. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5434. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5435. int Files = -1;
  5436. if (DataList->Count >= 1)
  5437. {
  5438. Files = StrToIntDef(DataList->Strings[0], Files);
  5439. }
  5440. else
  5441. {
  5442. DataList->Add(UnicodeString());
  5443. }
  5444. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5445. FSynchronizeProgressForm->Start();
  5446. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5447. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5448. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5449. &SynchronizeOptions);
  5450. if (Terminal->SessionData->CacheDirectories)
  5451. {
  5452. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5453. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5454. }
  5455. }
  5456. __finally
  5457. {
  5458. FAutoOperation = false;
  5459. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5460. SAFE_DESTROY(FSynchronizeProgressForm);
  5461. }
  5462. TSynchronizeParams SynchronizeParams;
  5463. SynchronizeParams.LocalDirectory = LocalDirectory;
  5464. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5465. SynchronizeParams.Mode = Mode;
  5466. SynchronizeParams.CopyParam = &CopyParam;
  5467. SynchronizeParams.Params = Params;
  5468. SynchronizeParams.Checklist = Checklist;
  5469. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5470. SynchronizeParams.StartTime = &StartTime;
  5471. SynchronizeParams.OnProcessedItem = NULL;
  5472. Result = Checklist->Count;
  5473. if (Checklist->Count > 0)
  5474. {
  5475. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5476. {
  5477. if (!DoSynchronizeChecklistDialog(
  5478. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5479. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5480. {
  5481. Result = -1;
  5482. }
  5483. }
  5484. else
  5485. {
  5486. FullSynchronize(SynchronizeParams, NULL, NULL);
  5487. }
  5488. }
  5489. else
  5490. {
  5491. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5492. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5493. }
  5494. }
  5495. __finally
  5496. {
  5497. delete Checklist;
  5498. }
  5499. OperationComplete(StartTime);
  5500. }
  5501. else
  5502. {
  5503. Result = -1;
  5504. }
  5505. return Result;
  5506. }
  5507. //---------------------------------------------------------------------------
  5508. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5509. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5510. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5511. {
  5512. if (Collect)
  5513. {
  5514. DebugAssert(FSynchronizeProgressForm != NULL);
  5515. if (!FSynchronizeProgressForm->Started)
  5516. {
  5517. FSynchronizeProgressForm->Start();
  5518. }
  5519. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5520. SetTaskbarListProgressValue(CompareProgress);
  5521. }
  5522. else
  5523. {
  5524. DebugAssert(FSynchronizeProgressForm == NULL);
  5525. DebugAssert(FProgressForm != NULL);
  5526. }
  5527. }
  5528. //---------------------------------------------------------------------------
  5529. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5530. {
  5531. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5532. TRemoteFile * File = NULL;
  5533. Terminal->ReadFile(FullFileName, File);
  5534. if (File != NULL)
  5535. {
  5536. std::unique_ptr<TRemoteFile> FileOwner(File);
  5537. TAutoFlag Flag(FStandaloneEditing);
  5538. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5539. Application->ShowMainForm = false;
  5540. Application->Run();
  5541. }
  5542. }
  5543. //---------------------------------------------------------------------------
  5544. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5545. {
  5546. DebugFail();
  5547. }
  5548. //---------------------------------------------------------------------------
  5549. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5550. {
  5551. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5552. SessionData->Assign(Terminal->SessionData);
  5553. UpdateSessionData(SessionData.get());
  5554. Terminal->UpdateSessionCredentials(SessionData.get());
  5555. if (!Terminal->SessionData->HasSessionName())
  5556. {
  5557. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5558. // once IsWorkspace is cleared
  5559. SessionData->Name = UnicodeString();
  5560. SessionData->IsWorkspace = false;
  5561. }
  5562. return SessionData.release();
  5563. }
  5564. //---------------------------------------------------------------------------
  5565. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5566. {
  5567. TSessionData * SessionData = CloneCurrentSessionData();
  5568. try
  5569. {
  5570. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5571. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5572. }
  5573. __finally
  5574. {
  5575. delete SessionData;
  5576. }
  5577. }
  5578. //---------------------------------------------------------------------------
  5579. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5580. {
  5581. TTerminalManager * Manager = TTerminalManager::Instance();
  5582. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5583. if (DebugAlwaysTrue(Terminal != NULL))
  5584. {
  5585. // Update (Managed)Terminal->StateData
  5586. UpdateTerminal(Terminal);
  5587. }
  5588. Manager->SaveWorkspace(DataList.get());
  5589. return DataList.release();
  5590. }
  5591. //---------------------------------------------------------------------------
  5592. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5593. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5594. {
  5595. WinConfiguration->LastWorkspace = Name;
  5596. if (!SavePasswords)
  5597. {
  5598. for (int Index = 0; Index < DataList->Count; Index++)
  5599. {
  5600. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5601. if (SessionData->Link.IsEmpty())
  5602. {
  5603. SessionData->ClearSessionPasswords();
  5604. }
  5605. }
  5606. }
  5607. StoredSessions->NewWorkspace(Name, DataList);
  5608. // modified only
  5609. StoredSessions->Save(false, Explicit);
  5610. }
  5611. //---------------------------------------------------------------------------
  5612. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5613. {
  5614. return
  5615. DefaultStr(WinConfiguration->LastWorkspace,
  5616. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5617. }
  5618. //---------------------------------------------------------------------------
  5619. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5620. {
  5621. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5622. bool AnyNonStoredSessionWithPassword = false;
  5623. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5624. bool AllNonStoredSessionsAnonymous = true;
  5625. TTerminalManager * Manager = TTerminalManager::Instance();
  5626. for (int Index = 0; Index < DataList->Count; Index++)
  5627. {
  5628. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5629. if (SessionData->Link.IsEmpty())
  5630. {
  5631. if (SessionData->HasAnySessionPassword())
  5632. {
  5633. AnyNonStoredSessionWithPassword = true;
  5634. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5635. {
  5636. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5637. }
  5638. }
  5639. if (!SameText(SessionData->UserName, AnonymousUserName))
  5640. {
  5641. AllNonStoredSessionsAnonymous = false;
  5642. }
  5643. }
  5644. }
  5645. bool SavePasswords;
  5646. bool * PSavePasswords;
  5647. bool NotRecommendedSavingPasswords =
  5648. !CustomWinConfiguration->UseMasterPassword &&
  5649. !AllNonStoredSessionsAnonymous;
  5650. if (Configuration->DisablePasswordStoring ||
  5651. !AnyNonStoredSessionWithPassword)
  5652. {
  5653. PSavePasswords = NULL;
  5654. SavePasswords = false;
  5655. }
  5656. else
  5657. {
  5658. PSavePasswords = &SavePasswords;
  5659. SavePasswords =
  5660. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5661. !NotRecommendedSavingPasswords;
  5662. }
  5663. UnicodeString Name = WorkspaceName();
  5664. bool CreateShortcut = false;
  5665. bool Result =
  5666. DoSaveWorkspaceDialog(
  5667. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5668. EnableAutoSave);
  5669. if (Result)
  5670. {
  5671. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5672. if (CreateShortcut)
  5673. {
  5674. TOperationVisualizer Visualizer;
  5675. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5676. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5677. }
  5678. if (EnableAutoSave)
  5679. {
  5680. WinConfiguration->AutoSaveWorkspace = true;
  5681. WinConfiguration->AutoWorkspace = Name;
  5682. if (PSavePasswords != NULL)
  5683. {
  5684. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5685. }
  5686. }
  5687. }
  5688. return Result;
  5689. }
  5690. //---------------------------------------------------------------------------
  5691. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5692. {
  5693. DebugAssert(!IsLocalBrowserMode());
  5694. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5695. if (WinConfiguration->PreservePanelState)
  5696. {
  5697. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5698. }
  5699. UpdateSessionData(Terminal->StateData);
  5700. }
  5701. //---------------------------------------------------------------------------
  5702. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5703. {
  5704. // Keep in sync with TSessionData::CopyStateData
  5705. DebugAssert(!IsLocalBrowserMode());
  5706. DebugAssert(Data != NULL);
  5707. // This is inconsistent with how color (for example) is handled.
  5708. Data->Name = Terminal->SessionData->Name;
  5709. // cannot use RemoteDirView->Path, because it is empty if connection
  5710. // was already closed
  5711. // also only peek, we may not be connected at all atm
  5712. // so make sure we do not try retrieving current directory from the server
  5713. // (particularly with FTP)
  5714. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5715. if (!ACurrentDirectory.IsEmpty())
  5716. {
  5717. Data->RemoteDirectory = ACurrentDirectory;
  5718. }
  5719. Data->Color = SessionColor;
  5720. }
  5721. //---------------------------------------------------------------------------
  5722. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5723. {
  5724. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5725. DebugAssert(Toolbar != NULL);
  5726. for (int i = 0; i < Toolbar->Items->Count; i++)
  5727. {
  5728. TTBXCustomDropDownItem * DropDownItem;
  5729. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5730. if (DropDownItem != NULL)
  5731. {
  5732. ToolbarItemResize(DropDownItem,
  5733. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5734. Toolbar->NonClientWidth);
  5735. break;
  5736. }
  5737. }
  5738. }
  5739. //---------------------------------------------------------------------------
  5740. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5741. {
  5742. Item->EditWidth = Width;
  5743. }
  5744. //---------------------------------------------------------------------------
  5745. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5746. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5747. {
  5748. for (int i = 0; i < Toolbar->Items->Count; i++)
  5749. {
  5750. TTBXCustomDropDownItem * DropDownItem;
  5751. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5752. if (DropDownItem != NULL)
  5753. {
  5754. ASize.x -= DropDownItem->EditWidth;
  5755. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5756. }
  5757. }
  5758. }
  5759. //---------------------------------------------------------------------------
  5760. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5761. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5762. {
  5763. if (WinConfiguration->DDWarnLackOfTempSpace)
  5764. {
  5765. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5766. if (!ADrive.IsEmpty())
  5767. {
  5768. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5769. DebugAssert(RequiredSpace >= 0);
  5770. __int64 RequiredWithReserve;
  5771. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5772. if (FreeSpace < RequiredWithReserve)
  5773. {
  5774. unsigned int Result;
  5775. TMessageParams Params(mpNeverAskAgainCheck);
  5776. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5777. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5778. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5779. if (Result == qaNeverAskAgain)
  5780. {
  5781. WinConfiguration->DDWarnLackOfTempSpace = false;
  5782. }
  5783. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5784. }
  5785. }
  5786. }
  5787. }
  5788. //---------------------------------------------------------------------------
  5789. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5790. {
  5791. DoOpenDirectoryDialog(odAddBookmark, Side);
  5792. }
  5793. //---------------------------------------------------------------------------
  5794. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5795. {
  5796. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5797. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5798. try
  5799. {
  5800. TCustomDirView * DView = DirView(Side);
  5801. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5802. {
  5803. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5804. }
  5805. }
  5806. catch (...)
  5807. {
  5808. delete VisitedDirectories;
  5809. throw;
  5810. }
  5811. return VisitedDirectories;
  5812. }
  5813. //---------------------------------------------------------------------------
  5814. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5815. TOpenDirectoryMode Mode, TOperationSide Side)
  5816. {
  5817. bool Continue = true;
  5818. if (Mode == odAddBookmark)
  5819. {
  5820. TMessageParams Params(mpNeverAskAgainCheck);
  5821. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5822. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5823. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5824. unsigned int Answer =
  5825. MessageDialog(Message,
  5826. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5827. if (Answer == qaNeverAskAgain)
  5828. {
  5829. Continue = true;
  5830. WinConfiguration->UseSharedBookmarks = true;
  5831. }
  5832. else if (Answer == qaOK)
  5833. {
  5834. Continue = true;
  5835. WinConfiguration->UseSharedBookmarks = false;
  5836. }
  5837. else
  5838. {
  5839. Continue = false;
  5840. }
  5841. }
  5842. if (Continue)
  5843. {
  5844. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5845. try
  5846. {
  5847. UnicodeString Name = DirView(Side)->PathName;
  5848. bool Repeat;
  5849. do
  5850. {
  5851. Repeat = false;
  5852. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5853. // do not allow switching to location profiles,
  5854. // if we are not connected
  5855. HasDirView[osLocal] && (Terminal != NULL)))
  5856. {
  5857. TWindowLock Lock(this);
  5858. if (!TryOpenDirectory(Side, Name))
  5859. {
  5860. Repeat = true;
  5861. Mode = odBrowse;
  5862. }
  5863. }
  5864. }
  5865. while (Repeat);
  5866. }
  5867. __finally
  5868. {
  5869. delete VisitedDirectories;
  5870. }
  5871. }
  5872. }
  5873. //---------------------------------------------------------------------------
  5874. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5875. {
  5876. DebugAssert(!FTerminal->CommandSessionOpened);
  5877. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5878. }
  5879. //---------------------------------------------------------------------------
  5880. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5881. {
  5882. bool Result = FTerminal->IsCapable[Capability] ||
  5883. FTerminal->CommandSessionOpened;
  5884. if (!Result)
  5885. {
  5886. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5887. if (!GUIConfiguration->ConfirmCommandSession)
  5888. {
  5889. Result = true;
  5890. }
  5891. else
  5892. {
  5893. TMessageParams Params(mpNeverAskAgainCheck);
  5894. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5895. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5896. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5897. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5898. if (Answer == qaNeverAskAgain)
  5899. {
  5900. GUIConfiguration->ConfirmCommandSession = false;
  5901. Result = true;
  5902. }
  5903. else if (Answer == qaOK)
  5904. {
  5905. Result = true;
  5906. }
  5907. }
  5908. if (Result)
  5909. {
  5910. Result = CommandSessionFallback();
  5911. }
  5912. }
  5913. return Result;
  5914. }
  5915. //---------------------------------------------------------------------------
  5916. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5917. {
  5918. if (EnsureCommandSessionFallback(fcAnyCommand))
  5919. {
  5920. DoConsoleDialog(Terminal, Command);
  5921. }
  5922. }
  5923. //---------------------------------------------------------------------------
  5924. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5925. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5926. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5927. {
  5928. if (!IsSideLocalBrowser(osRemote) &&
  5929. IsFileControl(DropSourceControl, osRemote) &&
  5930. (FDDExtMapFile != NULL))
  5931. {
  5932. Accept = true;
  5933. }
  5934. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5935. DebugAssert(FDDTargetControl != NULL);
  5936. }
  5937. //---------------------------------------------------------------------------
  5938. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5939. _di_IDataObject DataObj, int KeyState,
  5940. const TPoint & Point, int & Effect, bool & Accept)
  5941. {
  5942. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5943. }
  5944. //---------------------------------------------------------------------------
  5945. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5946. _di_IDataObject DataObj, int KeyState,
  5947. const TPoint & Point, int & Effect, bool & Accept)
  5948. {
  5949. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5950. }
  5951. //---------------------------------------------------------------------------
  5952. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5953. TObject * Sender)
  5954. {
  5955. DebugUsedParam(Sender);
  5956. DebugAssert(FDDTargetControl == Sender);
  5957. FDDTargetControl = NULL;
  5958. }
  5959. //---------------------------------------------------------------------------
  5960. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5961. {
  5962. FileControlDDDragLeave(SessionsPageControl);
  5963. }
  5964. //---------------------------------------------------------------------------
  5965. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5966. {
  5967. FileControlDDDragLeave(QueueView3);
  5968. }
  5969. //---------------------------------------------------------------------------
  5970. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5971. {
  5972. DebugAssert(!IsSideLocalBrowser(Side));
  5973. DebugUsedParam(Side);
  5974. bool Edit = false;
  5975. TRemoteFile * File = NULL;
  5976. UnicodeString FileName;
  5977. UnicodeString PointTo;
  5978. bool SymbolicLink = true;
  5979. if (RemoteDirView->ItemFocused)
  5980. {
  5981. DebugAssert(RemoteDirView->ItemFocused->Data);
  5982. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5983. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5984. if (Edit)
  5985. {
  5986. FileName = File->FileName;
  5987. PointTo = File->LinkTo;
  5988. }
  5989. else
  5990. {
  5991. PointTo = File->FileName;
  5992. }
  5993. }
  5994. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5995. Terminal->IsCapable[fcHardLink]))
  5996. {
  5997. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5998. if (Edit)
  5999. {
  6000. DebugAssert(File->FileName == FileName);
  6001. int Params = dfNoRecursive;
  6002. Terminal->ExceptionOnFail = true;
  6003. try
  6004. {
  6005. Terminal->DeleteFile(L"", File, &Params);
  6006. }
  6007. __finally
  6008. {
  6009. Terminal->ExceptionOnFail = false;
  6010. }
  6011. }
  6012. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6013. }
  6014. }
  6015. //---------------------------------------------------------------------------
  6016. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6017. {
  6018. return
  6019. (IsSideLocalBrowser(Side) ||
  6020. ((Terminal != NULL) &&
  6021. Terminal->ResolvingSymlinks &&
  6022. Terminal->IsCapable[fcSymbolicLink]));
  6023. }
  6024. //---------------------------------------------------------------------------
  6025. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6026. {
  6027. return
  6028. !IsSideLocalBrowser(FCurrentSide) &&
  6029. (RemoteDirView->ItemFocused != NULL) &&
  6030. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6031. Terminal->SessionData->ResolveSymlinks;
  6032. }
  6033. //---------------------------------------------------------------------------
  6034. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6035. {
  6036. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6037. FForceExecution = true;
  6038. try
  6039. {
  6040. DirView(osCurrent)->ExecuteCurrentFile();
  6041. }
  6042. __finally
  6043. {
  6044. FForceExecution = false;
  6045. }
  6046. }
  6047. //---------------------------------------------------------------------------
  6048. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6049. {
  6050. TEditorData ExternalEditor;
  6051. ExternalEditor.Editor = edExternal;
  6052. bool Remember = false;
  6053. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6054. {
  6055. if (Remember)
  6056. {
  6057. TEditorList * EditorList = new TEditorList;
  6058. try
  6059. {
  6060. *EditorList = *WinConfiguration->EditorList;
  6061. bool Found = false;
  6062. int i = 0;
  6063. while (!Found && (i < EditorList->Count))
  6064. {
  6065. const TEditorPreferences * Editor = EditorList->Editors[i];
  6066. if ((Editor->Data->Editor == edExternal) &&
  6067. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6068. {
  6069. Found = true;
  6070. }
  6071. i++;
  6072. }
  6073. if (!Found)
  6074. {
  6075. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6076. WinConfiguration->EditorList = EditorList;
  6077. }
  6078. }
  6079. __finally
  6080. {
  6081. delete EditorList;
  6082. }
  6083. }
  6084. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6085. }
  6086. }
  6087. //---------------------------------------------------------------------------
  6088. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6089. {
  6090. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6091. if (FFileFindTerminal == Sender)
  6092. {
  6093. FFileFindTerminal = NULL;
  6094. HideFileFindDialog();
  6095. }
  6096. if (FClipboardTerminal == Sender)
  6097. {
  6098. ClipboardClear(); // implies ClipboardStop
  6099. }
  6100. }
  6101. //---------------------------------------------------------------------------
  6102. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6103. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6104. {
  6105. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6106. DebugAssert(Terminal != NULL);
  6107. if (Data->Terminal == Terminal)
  6108. {
  6109. Data->Terminal = NULL;
  6110. }
  6111. }
  6112. //---------------------------------------------------------------------------
  6113. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6114. {
  6115. UpdateControls();
  6116. SessionColor = TColor(0);
  6117. UpdateRemotePathComboBox(false);
  6118. NeedSession(false);
  6119. }
  6120. //---------------------------------------------------------------------------
  6121. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6122. {
  6123. try
  6124. {
  6125. // Cache, as the login dialog can change its value
  6126. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6127. try
  6128. {
  6129. if (ShowLogin)
  6130. {
  6131. bool ReloadSessions = !Startup;
  6132. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6133. }
  6134. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6135. // This detects if workspace was saved the last time the main widow was closed
  6136. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6137. {
  6138. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6139. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6140. }
  6141. }
  6142. __finally
  6143. {
  6144. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6145. // (so there was no chance for the user to open any session yet)
  6146. if ((ShowLogin || !Startup) &&
  6147. !WinConfiguration->KeepOpenWhenNoSession &&
  6148. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6149. {
  6150. TerminateApplication();
  6151. }
  6152. }
  6153. }
  6154. catch (EAbort &)
  6155. {
  6156. // swallow
  6157. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6158. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6159. }
  6160. }
  6161. //---------------------------------------------------------------------------
  6162. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6163. {
  6164. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6165. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6166. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6167. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6168. try
  6169. {
  6170. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6171. // Clear the design time unthemed tab
  6172. ((SessionsPageControl->PageCount > 0) &&
  6173. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6174. {
  6175. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6176. }
  6177. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6178. {
  6179. TThemeTabSheet * TabSheet;
  6180. if (Index >= SessionsPageControl->PageCount)
  6181. {
  6182. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6183. TabSheet->PageControl = SessionsPageControl;
  6184. }
  6185. else
  6186. {
  6187. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6188. }
  6189. bool IsSessionTab = (Index < TerminalList->Count);
  6190. if (IsSessionTab)
  6191. {
  6192. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6193. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6194. UpdateSessionTab(TabSheet);
  6195. }
  6196. else
  6197. {
  6198. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6199. TabSheet->Tag = 0; // not really needed
  6200. TabSheet->Shadowed = false;
  6201. // 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)
  6202. UpdateNewSessionTab();
  6203. }
  6204. TabSheet->ShowCloseButton = IsSessionTab;
  6205. }
  6206. }
  6207. __finally
  6208. {
  6209. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6210. }
  6211. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6212. }
  6213. //---------------------------------------------------------------------------
  6214. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6215. {
  6216. DoTerminalListChanged();
  6217. }
  6218. //---------------------------------------------------------------------------
  6219. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6220. {
  6221. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6222. DebugAssert(TabSheet->ImageIndex == 0);
  6223. TabSheet->Caption =
  6224. WinConfiguration->SelectiveToolbarText ?
  6225. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6226. UnicodeString();
  6227. }
  6228. //---------------------------------------------------------------------------
  6229. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6230. {
  6231. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6232. }
  6233. //---------------------------------------------------------------------------
  6234. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6235. {
  6236. if (DebugAlwaysTrue(TabSheet != NULL))
  6237. {
  6238. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6239. if (DebugAlwaysTrue(ATerminal != NULL))
  6240. {
  6241. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6242. TabSheet->ImageIndex = AddSessionColor(Color);
  6243. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6244. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6245. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6246. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6247. {
  6248. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6249. }
  6250. }
  6251. }
  6252. }
  6253. //---------------------------------------------------------------------------
  6254. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6255. {
  6256. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6257. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6258. bool Result = (Terminal != NULL);
  6259. if (Result)
  6260. {
  6261. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6262. }
  6263. else
  6264. {
  6265. try
  6266. {
  6267. NewSession(false);
  6268. }
  6269. __finally
  6270. {
  6271. DoTerminalListChanged();
  6272. }
  6273. FSessionsPageControlNewSessionTime = Now();
  6274. }
  6275. return Result;
  6276. }
  6277. //---------------------------------------------------------------------------
  6278. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6279. {
  6280. SessionTabSwitched();
  6281. }
  6282. //---------------------------------------------------------------------------
  6283. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6284. {
  6285. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6286. DebugAssert(TransferList != NULL);
  6287. UnicodeString Name;
  6288. if (TransferList->ItemIndex <= 0)
  6289. {
  6290. Name = L"";
  6291. }
  6292. else
  6293. {
  6294. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6295. }
  6296. if (FCopyParamAutoSelected.IsEmpty())
  6297. {
  6298. // if previous preset was not autoselected, make new preset the "default"
  6299. FCopyParamDefault = Name;
  6300. }
  6301. GUIConfiguration->CopyParamCurrent = Name;
  6302. }
  6303. //---------------------------------------------------------------------------
  6304. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6305. {
  6306. // sanity check
  6307. bool ExistingPreset =
  6308. (FTransferListHoverIndex >= 0) &&
  6309. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6310. DebugAssert(ExistingPreset);
  6311. if (ExistingPreset)
  6312. {
  6313. HDC DC = GetDC(0);
  6314. TCanvas * Canvas = new TCanvas();
  6315. try
  6316. {
  6317. Canvas->Handle = DC;
  6318. Canvas->Font = GetToolbarFont(this);
  6319. UnicodeString Name;
  6320. if (FTransferListHoverIndex == 0)
  6321. {
  6322. Name = L"";
  6323. }
  6324. else
  6325. {
  6326. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6327. }
  6328. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6329. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6330. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6331. static_cast<TObject*>(GetComponent(fcTransferList)));
  6332. UnicodeString InfoStr =
  6333. GUIConfiguration->CopyParamPreset[Name].
  6334. GetInfoStr(L"; ",
  6335. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6336. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6337. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6338. {
  6339. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6340. {
  6341. InfoStr.SetLength(InfoStr.Length() - 1);
  6342. }
  6343. InfoStr += Ellipsis;
  6344. }
  6345. // UpdateCaption does not cause invalidation of whole submenu, while
  6346. // setting Caption property does.
  6347. // also it probably does not resize the label, even if necessary
  6348. // (we do not want that anyway)
  6349. TransferLabel->UpdateCaption(InfoStr);
  6350. }
  6351. __finally
  6352. {
  6353. Canvas->Handle = NULL;
  6354. ReleaseDC(0, DC);
  6355. delete Canvas;
  6356. }
  6357. }
  6358. }
  6359. //---------------------------------------------------------------------------
  6360. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6361. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6362. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6363. {
  6364. if (FTransferListHoverIndex != AHoverIndex)
  6365. {
  6366. FTransferListHoverIndex = AHoverIndex;
  6367. UpdateTransferLabel();
  6368. }
  6369. }
  6370. //---------------------------------------------------------------------------
  6371. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6372. {
  6373. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6374. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6375. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6376. {
  6377. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6378. {
  6379. OpenDirectory(GetSide(osCurrent));
  6380. Message.Result = 1;
  6381. }
  6382. else
  6383. {
  6384. TForm::Dispatch(&Message);
  6385. }
  6386. }
  6387. else
  6388. {
  6389. TForm::Dispatch(&Message);
  6390. }
  6391. }
  6392. //---------------------------------------------------------------------------
  6393. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6394. {
  6395. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6396. if ((FIgnoreNextDialogChar != 0) &&
  6397. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6398. {
  6399. Message.Result = 1;
  6400. }
  6401. else
  6402. {
  6403. TForm::Dispatch(&Message);
  6404. }
  6405. FIgnoreNextDialogChar = 0;
  6406. }
  6407. //---------------------------------------------------------------------------
  6408. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6409. {
  6410. // The four low-order bits are used internally by Windows
  6411. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6412. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6413. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6414. // 61730 - restore thru double click - undocumented
  6415. // 61490 - maximize thru double click - undocumented
  6416. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6417. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6418. {
  6419. SysResizing(Cmd);
  6420. }
  6421. TForm::Dispatch(&Message);
  6422. }
  6423. //---------------------------------------------------------------------------
  6424. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6425. {
  6426. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6427. // Also there no point returning TRUE as we are not able to
  6428. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6429. // Hence the process termination might be safer :)
  6430. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6431. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6432. {
  6433. Message.Result = FALSE;
  6434. }
  6435. else
  6436. {
  6437. Message.Result = TRUE;
  6438. }
  6439. // Do not call default handling as that triggers OnCloseQuery,
  6440. // where our implementation will popup confirmation dialogs, what we do not want,
  6441. // as per Vista guidelines:
  6442. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6443. }
  6444. //---------------------------------------------------------------------------
  6445. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6446. {
  6447. if (Message.EndSession && IsApplicationMinimized())
  6448. {
  6449. // WORKAROUND
  6450. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6451. // when it receives WM_ENDSESSION,
  6452. // but that sometimes (particularly when application is minimized) cause crashes.
  6453. // Crash popups message that blocks log off.
  6454. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6455. // so we call ExitProcess() immediately, not even trying to cleanup.
  6456. // It still causes beep, so there's likely some popup, but it does not block
  6457. // log off.
  6458. ExitProcess(0);
  6459. }
  6460. TForm::Dispatch(&Message);
  6461. }
  6462. #ifdef _DEBUG
  6463. //---------------------------------------------------------------------------
  6464. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6465. {
  6466. TForm::Dispatch(&Message);
  6467. }
  6468. #endif
  6469. //---------------------------------------------------------------------------
  6470. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6471. {
  6472. }
  6473. //---------------------------------------------------------------------------
  6474. void __fastcall TCustomScpExplorerForm::DoShow()
  6475. {
  6476. // only now are the controls resized finally, so the size constraints
  6477. // will not conflict with possibly very small window size
  6478. RestoreFormParams();
  6479. // This indicates that the form was rescaled after parameters were loaded and
  6480. // as most probably the parameters were actually saved with the current form scaling
  6481. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6482. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6483. // and no rounding problems will occur.
  6484. // See also RestoreForm.
  6485. // (we should be safe to call RestoreParams unconditionally)
  6486. if (PixelsPerInch != Screen->PixelsPerInch)
  6487. {
  6488. RestoreParams();
  6489. }
  6490. FixControlsPlacement();
  6491. if (Position == poDefaultPosOnly)
  6492. {
  6493. CutFormToDesktop(this);
  6494. }
  6495. TForm::DoShow();
  6496. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6497. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6498. if (Terminal == NULL)
  6499. {
  6500. StartingDisconnected();
  6501. }
  6502. UpdatePixelsPerInchMainWindowCounter();
  6503. FShowing = true;
  6504. }
  6505. //---------------------------------------------------------------------------
  6506. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6507. {
  6508. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6509. }
  6510. //---------------------------------------------------------------------------
  6511. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6512. {
  6513. DoTerminalListChanged();
  6514. }
  6515. //---------------------------------------------------------------------------
  6516. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6517. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6518. TNotifyEvent OnBalloonClick, TObject * UserData)
  6519. {
  6520. bool Do;
  6521. UnicodeString Message;
  6522. if (E == NULL)
  6523. {
  6524. Message = Str;
  6525. Do = true;
  6526. }
  6527. else
  6528. {
  6529. Do = ExceptionMessage(E, Message);
  6530. }
  6531. if (Do && WinConfiguration->BalloonNotifications)
  6532. {
  6533. UnicodeString Title;
  6534. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6535. {
  6536. Message = TrimLeft(Message);
  6537. }
  6538. else
  6539. {
  6540. Message = UnformatMessage(Message);
  6541. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6542. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6543. Title = LoadResourceString(Captions[Type]);
  6544. if (Terminal != NULL)
  6545. {
  6546. Title = FORMAT(L"%s - %s",
  6547. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6548. }
  6549. }
  6550. if (Seconds == 0)
  6551. {
  6552. Seconds = WinConfiguration->NotificationsTimeout;
  6553. }
  6554. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6555. }
  6556. }
  6557. //---------------------------------------------------------------------------
  6558. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6559. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6560. UnicodeString HelpKeyword, const TMessageParams * Params,
  6561. TTerminal * Terminal)
  6562. {
  6563. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6564. (FOnFeedSynchronizeError != NULL)) &&
  6565. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6566. {
  6567. if (FOnFeedSynchronizeError != NULL)
  6568. {
  6569. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6570. }
  6571. else
  6572. {
  6573. DebugAssert(FErrorList != NULL);
  6574. TStringList * MoreMessagesCopy = NULL;
  6575. if (MoreMessages)
  6576. {
  6577. MoreMessagesCopy = new TStringList();
  6578. MoreMessagesCopy->Assign(MoreMessages);
  6579. }
  6580. FErrorList->AddObject(Message, MoreMessagesCopy);
  6581. }
  6582. PopupTrayBalloon(Terminal, Message, Type);
  6583. return ContinueAnswer(Answers);
  6584. }
  6585. else
  6586. {
  6587. bool UseBalloon = FTrayIcon->Visible;
  6588. if (UseBalloon)
  6589. {
  6590. PopupTrayBalloon(Terminal, Message, Type);
  6591. }
  6592. unsigned int Result;
  6593. try
  6594. {
  6595. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6596. }
  6597. __finally
  6598. {
  6599. // cancel only balloon we popped up, otherwise we may cancel notification
  6600. // balloon that was there before the message dialog
  6601. // (such as "dd confirmation opt in balloon)
  6602. if (UseBalloon)
  6603. {
  6604. FTrayIcon->CancelBalloon();
  6605. }
  6606. }
  6607. return Result;
  6608. }
  6609. }
  6610. //---------------------------------------------------------------------------
  6611. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6612. TTerminal * Terminal, Exception * E)
  6613. {
  6614. if (FTrayIcon->Visible)
  6615. {
  6616. PopupTrayBalloon(Terminal, L"", qtError, E);
  6617. }
  6618. // particularly prevent opening new session from jump list,
  6619. // while exception is shown
  6620. NonVisualDataModule->StartBusy();
  6621. try
  6622. {
  6623. ShowExtendedExceptionEx(Terminal, E);
  6624. }
  6625. __finally
  6626. {
  6627. NonVisualDataModule->EndBusy();
  6628. FTrayIcon->CancelBalloon();
  6629. }
  6630. }
  6631. //---------------------------------------------------------------------------
  6632. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6633. {
  6634. if (Terminal->Active)
  6635. {
  6636. InitStatusBar();
  6637. }
  6638. // cannot rely on active page being page for active terminal,
  6639. // as it can happen that active page is the "new session" page
  6640. // (e.g. when reconnecting active terminal, while login dialog
  6641. // invoked from "new session" page is modal)
  6642. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6643. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6644. }
  6645. //---------------------------------------------------------------------------
  6646. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6647. TTerminal * Terminal, Exception * E)
  6648. {
  6649. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6650. if (!Terminal->Active)
  6651. {
  6652. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6653. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6654. {
  6655. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6656. }
  6657. }
  6658. }
  6659. //---------------------------------------------------------------------------
  6660. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6661. UnicodeString Message, TQueryType Type,
  6662. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6663. {
  6664. if ((E == NULL) ||
  6665. ExceptionMessage(E, Message))
  6666. {
  6667. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6668. if (Important)
  6669. {
  6670. Seconds *= 5;
  6671. }
  6672. UnicodeString NoteMessage(UnformatMessage(Message));
  6673. if (Terminal != NULL)
  6674. {
  6675. NoteMessage = FORMAT(L"%s: %s",
  6676. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6677. }
  6678. if (WinConfiguration->BalloonNotifications)
  6679. {
  6680. AddNote(NoteMessage);
  6681. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6682. }
  6683. else
  6684. {
  6685. FlashOnBackground();
  6686. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6687. }
  6688. }
  6689. }
  6690. //---------------------------------------------------------------------------
  6691. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6692. {
  6693. RestoreApp();
  6694. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6695. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6696. !NonVisualDataModule->Busy)
  6697. {
  6698. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6699. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6700. if (!ComponentVisible[fcQueueView])
  6701. {
  6702. ToggleQueueVisibility();
  6703. GoToQueue();
  6704. }
  6705. }
  6706. }
  6707. //---------------------------------------------------------------------------
  6708. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6709. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6710. {
  6711. UnicodeString Message;
  6712. TNotifyEvent OnClick = NULL;
  6713. TObject * UserData = NULL;
  6714. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6715. switch (Event)
  6716. {
  6717. case qeEmptyButMonitored:
  6718. if ((ATerminal != Terminal) || QueueInvisible)
  6719. {
  6720. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6721. OnClick = QueueEmptyNoteClicked;
  6722. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6723. TerminalNoteData->Terminal = ATerminal;
  6724. UserData = TerminalNoteData;
  6725. }
  6726. break;
  6727. case qeEmpty:
  6728. OperationComplete(ATerminal->QueueOperationStart);
  6729. break;
  6730. case qePendingUserAction:
  6731. if ((ATerminal != Terminal) ||
  6732. (QueueInvisible && !IsQueueAutoPopup()))
  6733. {
  6734. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6735. }
  6736. break;
  6737. default:
  6738. DebugFail();
  6739. }
  6740. if (!Message.IsEmpty())
  6741. {
  6742. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6743. }
  6744. }
  6745. //---------------------------------------------------------------------------
  6746. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6747. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6748. {
  6749. if (FDDExtMapFile != NULL)
  6750. {
  6751. CloseHandle(FDDExtMapFile);
  6752. FDDExtMapFile = NULL;
  6753. }
  6754. if (WinConfiguration->DDFakeFile)
  6755. {
  6756. DDFakeFileInitDrag(FileList, Created);
  6757. }
  6758. }
  6759. //---------------------------------------------------------------------------
  6760. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6761. {
  6762. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6763. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6764. {
  6765. FFakeFileDropTarget = FileName;
  6766. }
  6767. }
  6768. //---------------------------------------------------------------------------
  6769. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6770. {
  6771. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6772. if (!ForceDirectories(ApiPath(Result)))
  6773. {
  6774. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6775. }
  6776. FileSetAttr(ApiPath(Result), faHidden);
  6777. return Result;
  6778. }
  6779. //---------------------------------------------------------------------------
  6780. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6781. bool & Created)
  6782. {
  6783. FFakeFileDropTarget = UnicodeString();
  6784. FDragFakeDirectory = CreateFakeTransferDirectory();
  6785. FileList->AddItem(NULL, FDragFakeDirectory);
  6786. Created = true;
  6787. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6788. {
  6789. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6790. }
  6791. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6792. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6793. {
  6794. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6795. TDragExtCommStruct* CommStruct;
  6796. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6797. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6798. DebugAssert(CommStruct != NULL);
  6799. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6800. CommStruct->Dragging = true;
  6801. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6802. LENOF(CommStruct->DropDest));
  6803. NULL_TERMINATE(CommStruct->DropDest);
  6804. UnmapViewOfFile(CommStruct);
  6805. }
  6806. }
  6807. //---------------------------------------------------------------------------
  6808. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6809. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6810. {
  6811. bool Result;
  6812. DebugAssert(!IsLocalBrowserMode());
  6813. if (Sender == RemoteDirView)
  6814. {
  6815. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6816. }
  6817. else
  6818. {
  6819. DebugAssert(Sender == RemoteDriveView);
  6820. TStrings * FileList = RemoteDriveView->DragFileList();
  6821. try
  6822. {
  6823. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6824. }
  6825. __finally
  6826. {
  6827. delete FileList;
  6828. }
  6829. }
  6830. if (FDDTargetControl == RemoteDriveView)
  6831. {
  6832. RemoteDriveView->UpdateDropTarget();
  6833. }
  6834. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6835. // or from tree view to dir view.
  6836. // foMove happens when file/dir is dragged from remote tree view outside of
  6837. // application via dragex
  6838. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6839. (DropSourceControl == RemoteDriveView))
  6840. {
  6841. RemoteDriveView->UpdateDropSource();
  6842. }
  6843. return Result;
  6844. }
  6845. //---------------------------------------------------------------------------
  6846. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6847. {
  6848. DebugAssert(!IsLocalBrowserMode());
  6849. // This also handles drops of remote files to queue.
  6850. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6851. SAFE_DESTROY(FDDFileList);
  6852. if (!FFakeFileDropTarget.IsEmpty())
  6853. {
  6854. RemoveDir(ApiPath(FFakeFileDropTarget));
  6855. }
  6856. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6857. {
  6858. try
  6859. {
  6860. TDragResult DDResult = (Sender == RemoteDirView) ?
  6861. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6862. // Focus is moved to the target application,
  6863. // but as we are going to present the UI, we need to steal the focus back.
  6864. // This most likely won't work though (windows does not allow application
  6865. // to steal focus most of the time)
  6866. Application->BringToFront();
  6867. // On older version of Windows we never got drMove here, see also comment below.
  6868. // On Windows 10, we get the "move".
  6869. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6870. {
  6871. UnicodeString TargetDirectory;
  6872. TFileOperation Operation;
  6873. // drInvalid may mean drMove, see comment below
  6874. switch (DDResult)
  6875. {
  6876. case drCopy:
  6877. Operation = foCopy;
  6878. break;
  6879. case drMove:
  6880. Operation = foMove;
  6881. break;
  6882. default:
  6883. DebugFail();
  6884. case drInvalid:
  6885. // prefer "copy" for safety
  6886. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6887. break;
  6888. }
  6889. TTransferOperationParam Param;
  6890. UnicodeString CounterName;
  6891. bool ForceQueue;
  6892. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6893. {
  6894. // we get drInvalid both if d&d was intercepted by ddext,
  6895. // and when users drops on no-drop location.
  6896. // we tell the difference by existence of response from ddext,
  6897. // so we ignore absence of response in this case
  6898. if (DDResult != drInvalid)
  6899. {
  6900. // here we know that the extension is installed,
  6901. // as it is checked as soon as drag&drop starts from
  6902. // RemoteFileControlDDCreateDragFileList
  6903. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6904. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6905. {
  6906. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6907. }
  6908. else
  6909. {
  6910. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6911. }
  6912. }
  6913. }
  6914. else
  6915. {
  6916. // download using fake file
  6917. Param.Temp = false;
  6918. Param.DragDrop = true;
  6919. if (ForceQueue)
  6920. {
  6921. Param.Queue = asOn;
  6922. }
  6923. if (Sender == RemoteDirView)
  6924. {
  6925. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6926. }
  6927. if (RemoteFileControlFileOperation(Sender, Operation,
  6928. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6929. {
  6930. Configuration->Usage->Inc(CounterName);
  6931. }
  6932. }
  6933. }
  6934. }
  6935. __finally
  6936. {
  6937. CloseHandle(FDDExtMapFile);
  6938. FDDExtMapFile = NULL;
  6939. delete FDragFakeMonitors;
  6940. FDragFakeMonitors = NULL;
  6941. RemoveDir(ApiPath(FDragFakeDirectory));
  6942. FDragFakeDirectory = L"";
  6943. FFakeFileDropTarget = UnicodeString();
  6944. }
  6945. }
  6946. if (FDragDropSshTerminate.get() != NULL)
  6947. {
  6948. FDragDropSshTerminate->Rethrow();
  6949. }
  6950. }
  6951. //---------------------------------------------------------------------------
  6952. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6953. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6954. {
  6955. // Remember drop effect so we know (when user drops files), if we copy or move
  6956. FLastDropEffect = dwEffect;
  6957. }
  6958. //---------------------------------------------------------------------------
  6959. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6960. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6961. {
  6962. bool Result;
  6963. if (FDDTargetControl == QueueView3)
  6964. {
  6965. Directory = DefaultDownloadTargetDirectory();
  6966. Result = true;
  6967. CounterName = L"DownloadsDragDropQueue";
  6968. ForceQueue = true;
  6969. }
  6970. else if (!FFakeFileDropTarget.IsEmpty())
  6971. {
  6972. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6973. Result = true;
  6974. ForceQueue = false;
  6975. CounterName = L"DownloadsDragDropFakeFile";
  6976. }
  6977. else
  6978. {
  6979. ForceQueue = false;
  6980. Enabled = false;
  6981. try
  6982. {
  6983. int Timer = 0;
  6984. Result = false;
  6985. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6986. {
  6987. {
  6988. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6989. TDragExtCommStruct* CommStruct;
  6990. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6991. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6992. DebugAssert(CommStruct != NULL);
  6993. Result = !CommStruct->Dragging;
  6994. if (Result)
  6995. {
  6996. Directory = ExtractFilePath(CommStruct->DropDest);
  6997. CounterName = L"DownloadsDragDropExternalExt";
  6998. }
  6999. UnmapViewOfFile(CommStruct);
  7000. }
  7001. if (!Result)
  7002. {
  7003. Sleep(50);
  7004. Timer += 50;
  7005. Application->ProcessMessages();
  7006. }
  7007. }
  7008. }
  7009. __finally
  7010. {
  7011. Enabled = true;
  7012. }
  7013. }
  7014. return Result;
  7015. }
  7016. //---------------------------------------------------------------------------
  7017. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7018. const UnicodeString TempDir, int SecDelay)
  7019. {
  7020. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  7021. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7022. if (FDelayedDeletionTimer == NULL)
  7023. {
  7024. DebugAssert(HandleAllocated());
  7025. FDelayedDeletionTimer = new TTimer(this);
  7026. FDelayedDeletionTimer->Interval = 10000;
  7027. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7028. }
  7029. else
  7030. {
  7031. FDelayedDeletionTimer->Enabled = true;
  7032. }
  7033. }
  7034. //---------------------------------------------------------------------------
  7035. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7036. {
  7037. DebugAssert(FDelayedDeletionList != NULL);
  7038. TDateTime N = Now();
  7039. TDateTime Alert;
  7040. UnicodeString Directory;
  7041. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7042. {
  7043. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7044. if ((N >= Alert) || (Sender == NULL))
  7045. {
  7046. Directory = FDelayedDeletionList->Strings[Index];
  7047. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7048. {
  7049. FDelayedDeletionList->Delete(Index);
  7050. }
  7051. }
  7052. }
  7053. if (FDelayedDeletionList->Count == 0)
  7054. {
  7055. FDelayedDeletionTimer->Enabled = false;
  7056. }
  7057. }
  7058. //---------------------------------------------------------------------------
  7059. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7060. {
  7061. DebugAssert(!IsLocalBrowserMode());
  7062. if (IsFileControl(FDDTargetControl, osRemote) ||
  7063. (FDDTargetControl == SessionsPageControl))
  7064. {
  7065. TTerminal * TargetTerminal = NULL;
  7066. TFileOperation Operation = foNone;
  7067. if (FDDTargetControl == SessionsPageControl)
  7068. {
  7069. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7070. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7071. // do not allow dropping on the "+" tab
  7072. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7073. if (TargetTerminal != NULL)
  7074. {
  7075. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7076. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7077. {
  7078. Operation = foRemoteMove;
  7079. }
  7080. else
  7081. {
  7082. Operation = foRemoteCopy;
  7083. }
  7084. }
  7085. }
  7086. else
  7087. {
  7088. if (FLastDropEffect == DROPEFFECT_MOVE)
  7089. {
  7090. Operation = foRemoteMove;
  7091. }
  7092. else if (FLastDropEffect == DROPEFFECT_COPY)
  7093. {
  7094. Operation = foRemoteCopy;
  7095. }
  7096. }
  7097. if (Operation != foNone)
  7098. {
  7099. RemoteFileControlFileOperation(DropSourceControl,
  7100. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7101. }
  7102. // abort drag&drop
  7103. Abort();
  7104. }
  7105. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7106. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7107. (FDDTargetControl != QueueView3))
  7108. {
  7109. DebugAssert(!FDragTempDir.IsEmpty());
  7110. TTransferType Type;
  7111. UnicodeString TempDir = FDragTempDir;
  7112. // We clear FDragTempDir before calling
  7113. // just in case it fail (raises exception)
  7114. FDragTempDir = L"";
  7115. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7116. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7117. // empty directory parameter means temp directory -> don't display it!
  7118. UnicodeString TargetDir = L"";
  7119. int Options = 0;
  7120. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7121. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7122. {
  7123. Abort();
  7124. }
  7125. // TargetDir is set when dropped on local file control
  7126. // (this was workaround for legacy dirview event handling, now it should be
  7127. // made prettier)
  7128. if (TargetDir.IsEmpty())
  7129. {
  7130. TargetDir = TempDir;
  7131. if (ForceDirectories(ApiPath(TargetDir)))
  7132. {
  7133. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7134. FPendingTempSpaceWarn = true;
  7135. try
  7136. {
  7137. FDragDropOperation = true;
  7138. int Params = cpTemporary |
  7139. (Type == ttMove ? cpDelete : 0);
  7140. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7141. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7142. }
  7143. __finally
  7144. {
  7145. FDragDropOperation = false;
  7146. FPendingTempSpaceWarn = false;
  7147. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7148. }
  7149. }
  7150. else
  7151. {
  7152. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7153. }
  7154. }
  7155. }
  7156. }
  7157. //---------------------------------------------------------------------------
  7158. void __fastcall TCustomScpExplorerForm::DDDownload(
  7159. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7160. {
  7161. DebugAssert(!IsLocalBrowserMode());
  7162. TAutoBatch AutoBatch(this);
  7163. UpdateCopyParamCounters(*CopyParam);
  7164. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7165. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7166. {
  7167. RemoteDriveView->UpdateDropSource();
  7168. }
  7169. }
  7170. //---------------------------------------------------------------------------
  7171. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7172. {
  7173. public:
  7174. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7175. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7176. {
  7177. }
  7178. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7179. {
  7180. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7181. TDataObjectFilesEx::AllowData(FormatEtc);
  7182. }
  7183. };
  7184. //---------------------------------------------------------------------------
  7185. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7186. TObject * Sender, TDataObject *& DataObject)
  7187. {
  7188. if (FDDExtMapFile != NULL)
  7189. {
  7190. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7191. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7192. {
  7193. Abort();
  7194. }
  7195. if (FileList->Count > 0)
  7196. {
  7197. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7198. if (!FilesObject->IsValid(true, true))
  7199. {
  7200. FilesObject->_Release();
  7201. }
  7202. else
  7203. {
  7204. DataObject = FilesObject;
  7205. }
  7206. }
  7207. }
  7208. }
  7209. //---------------------------------------------------------------------------
  7210. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7211. {
  7212. DebugFail();
  7213. }
  7214. //---------------------------------------------------------------------------
  7215. void __fastcall TCustomScpExplorerForm::GoToTree()
  7216. {
  7217. ComponentVisible[fcRemoteTree] = true;
  7218. DriveView(osRemote)->SetFocus();
  7219. }
  7220. //---------------------------------------------------------------------------
  7221. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7222. TPanelExport Export)
  7223. {
  7224. TCustomDirView * DirView = this->DirView(Side);
  7225. std::unique_ptr<TStrings> ExportData(new TStringList());
  7226. switch (Export)
  7227. {
  7228. case pePath:
  7229. ExportData->Add(DirView->PathName);
  7230. break;
  7231. case peFileList:
  7232. case peFullFileList:
  7233. {
  7234. bool FullPath = (Export == peFullFileList);
  7235. DirView->CreateFileList(false, FullPath, ExportData.get());
  7236. for (int Index = 0; Index < ExportData->Count; Index++)
  7237. {
  7238. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7239. {
  7240. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7241. }
  7242. }
  7243. }
  7244. break;
  7245. default:
  7246. DebugFail();
  7247. }
  7248. return ExportData.release();
  7249. }
  7250. //---------------------------------------------------------------------------
  7251. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7252. TPanelExport Export, TPanelExportDestination Destination)
  7253. {
  7254. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7255. PanelExportStore(Side, Export, Destination, ExportData.get());
  7256. }
  7257. //---------------------------------------------------------------------------
  7258. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7259. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7260. TStrings * ExportData)
  7261. {
  7262. if (Destination == pedClipboard)
  7263. {
  7264. TInstantOperationVisualizer Visualizer;
  7265. CopyToClipboard(ExportData);
  7266. }
  7267. else
  7268. {
  7269. DebugFail();
  7270. }
  7271. }
  7272. //---------------------------------------------------------------------------
  7273. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7274. {
  7275. TCustomDirView * DirView = this->DirView(Side);
  7276. UnicodeString Mask = DirView->Mask;
  7277. if (DoFilterMaskDialog(DirView, Mask))
  7278. {
  7279. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7280. Configuration->Usage->Inc(L"Filters");
  7281. }
  7282. }
  7283. //---------------------------------------------------------------------------
  7284. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7285. {
  7286. return FQueueController->DefaultOperation();
  7287. }
  7288. //---------------------------------------------------------------------------
  7289. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7290. TQueueOperation Operation, void ** Param)
  7291. {
  7292. switch (Operation)
  7293. {
  7294. case qoPreferences:
  7295. return true;
  7296. case qoGoTo:
  7297. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7298. case qoOnceEmpty:
  7299. return !FQueueController->Empty;
  7300. default:
  7301. return FQueueController->AllowOperation(Operation, Param);
  7302. }
  7303. }
  7304. //---------------------------------------------------------------------------
  7305. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7306. {
  7307. if (DebugAlwaysTrue(QueueView3->Visible))
  7308. {
  7309. QueueView3->SetFocus();
  7310. }
  7311. }
  7312. //---------------------------------------------------------------------------
  7313. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7314. TQueueOperation Operation, void * Param)
  7315. {
  7316. if (Operation == qoGoTo)
  7317. {
  7318. GoToQueue();
  7319. }
  7320. else if (Operation == qoPreferences)
  7321. {
  7322. PreferencesDialog(pmQueue);
  7323. }
  7324. else
  7325. {
  7326. FQueueController->ExecuteOperation(Operation, Param);
  7327. }
  7328. }
  7329. //---------------------------------------------------------------------------
  7330. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7331. {
  7332. return (Queue != NULL) && Queue->Enabled;
  7333. }
  7334. //---------------------------------------------------------------------------
  7335. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7336. {
  7337. DebugAssert(Queue != NULL);
  7338. if (Queue != NULL)
  7339. {
  7340. Queue->Enabled = !Queue->Enabled;
  7341. }
  7342. }
  7343. //---------------------------------------------------------------------------
  7344. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7345. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7346. {
  7347. FQueueActedItem = QueueView3->ItemFocused;
  7348. }
  7349. //---------------------------------------------------------------------------
  7350. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7351. {
  7352. return TopDock->Height + QueueSplitter->Height;
  7353. }
  7354. //---------------------------------------------------------------------------
  7355. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7356. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7357. {
  7358. // when queue is hidden by double-clicking splitter, stray attempt to
  7359. // resize the panel with strange value arrives, make sure it is ignored
  7360. if (ComponentVisible[fcQueueView])
  7361. {
  7362. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7363. RemotePanel->Constraints->MinHeight;
  7364. if (NewSize > HeightLimit)
  7365. {
  7366. NewSize = HeightLimit;
  7367. }
  7368. }
  7369. else
  7370. {
  7371. Accept = false;
  7372. }
  7373. }
  7374. //---------------------------------------------------------------------------
  7375. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7376. TDragObject *& /*DragObject*/)
  7377. {
  7378. FQueueActedItem = QueueView3->ItemFocused;
  7379. }
  7380. //---------------------------------------------------------------------------
  7381. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7382. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7383. {
  7384. Accept = true;
  7385. if (Source == QueueView3)
  7386. {
  7387. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7388. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7389. if (Accept)
  7390. {
  7391. TQueueItemProxy * QueueItem;
  7392. TQueueItemProxy * DestQueueItem;
  7393. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7394. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7395. Accept = (QueueItem != DestQueueItem) &&
  7396. (QueueItem->Status == TQueueItem::qsPending) &&
  7397. (DestQueueItem->Status == TQueueItem::qsPending);
  7398. }
  7399. }
  7400. }
  7401. //---------------------------------------------------------------------------
  7402. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7403. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7404. {
  7405. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7406. {
  7407. TQueueItemProxy * QueueItem;
  7408. TQueueItemProxy * DestQueueItem;
  7409. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7410. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7411. QueueItem->Move(DestQueueItem);
  7412. }
  7413. }
  7414. //---------------------------------------------------------------------------
  7415. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7416. {
  7417. if ((QueueView3->ItemFocused == NULL) &&
  7418. (QueueView3->Items->Count > 0))
  7419. {
  7420. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7421. }
  7422. QueueLabelUpdateStatus();
  7423. }
  7424. //---------------------------------------------------------------------------
  7425. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7426. {
  7427. QueueLabelUpdateStatus();
  7428. }
  7429. //---------------------------------------------------------------------------
  7430. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7431. {
  7432. QueueLabel->UpdateStatus();
  7433. }
  7434. //---------------------------------------------------------------------------
  7435. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7436. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7437. {
  7438. if (Selected)
  7439. {
  7440. NonVisualDataModule->UpdateNonVisibleActions();
  7441. }
  7442. }
  7443. //---------------------------------------------------------------------------
  7444. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7445. {
  7446. TDragDropFilesEx * Result = NULL;
  7447. if (Sender == SessionsPageControl)
  7448. {
  7449. Result = FSessionsDragDropFilesEx;
  7450. }
  7451. else if (Sender == QueueView3)
  7452. {
  7453. Result = FQueueDragDropFilesEx;
  7454. }
  7455. else
  7456. {
  7457. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7458. if (DirView != NULL)
  7459. {
  7460. Result = DirView->DragDropFilesEx;
  7461. }
  7462. else
  7463. {
  7464. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7465. if (DriveView != NULL)
  7466. {
  7467. Result = DriveView->DragDropFilesEx;
  7468. }
  7469. }
  7470. }
  7471. DebugAssert(Result != NULL);
  7472. return Result;
  7473. }
  7474. //---------------------------------------------------------------------------
  7475. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7476. {
  7477. return (DView->SelCount == DView->FilesCount);
  7478. }
  7479. //---------------------------------------------------------------------------
  7480. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7481. {
  7482. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7483. }
  7484. //---------------------------------------------------------------------------
  7485. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7486. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7487. {
  7488. DebugAssert(!IsLocalBrowserMode());
  7489. TFileOperation Operation;
  7490. switch (Effect)
  7491. {
  7492. case DROPEFFECT_MOVE:
  7493. Operation = foMove;
  7494. break;
  7495. case DROPEFFECT_COPY:
  7496. // occures on WinXP (reported by user)
  7497. default:
  7498. Operation = foCopy;
  7499. break;
  7500. };
  7501. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7502. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7503. if (DragDropFilesEx->FileList->Count > 0)
  7504. {
  7505. TStrings * FileList = new TStringList();
  7506. try
  7507. {
  7508. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7509. {
  7510. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7511. }
  7512. FDragDropOperation = true;
  7513. TTransferOperationParam Param;
  7514. Param.TargetDirectory = TargetPath;
  7515. // upload, no temp dirs
  7516. Param.Temp = false;
  7517. Param.DragDrop = DragDrop;
  7518. Param.Options =
  7519. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7520. if (ForceQueue)
  7521. {
  7522. Param.Queue = asOn;
  7523. }
  7524. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7525. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7526. {
  7527. if (IsFileControl(DropSourceControl, osLocal))
  7528. {
  7529. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7530. }
  7531. else
  7532. {
  7533. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7534. }
  7535. }
  7536. }
  7537. __finally
  7538. {
  7539. FDragDropOperation = false;
  7540. delete FileList;
  7541. }
  7542. }
  7543. }
  7544. //---------------------------------------------------------------------------
  7545. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7546. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7547. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7548. {
  7549. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7550. }
  7551. //---------------------------------------------------------------------------
  7552. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7553. TObject * Sender, int grfKeyState, int & dwEffect)
  7554. {
  7555. DebugAssert(!IsLocalBrowserMode());
  7556. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7557. if ((dwEffect != DROPEFFECT_NONE) &&
  7558. IsFileControl(DropSourceControl, osRemote))
  7559. {
  7560. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7561. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7562. (RemoteDirView->DropTarget == NULL))
  7563. {
  7564. dwEffect = DROPEFFECT_NONE;
  7565. }
  7566. else
  7567. {
  7568. if (dwEffect == DROPEFFECT_COPY)
  7569. {
  7570. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7571. // currently we support copying always (at least via temporary directory);
  7572. // remove associated checks once this all proves stable and working
  7573. bool CopyCapable = true;
  7574. // if we do not support neither of operations, there's no discussion
  7575. if (!MoveCapable && !CopyCapable)
  7576. {
  7577. dwEffect = DROPEFFECT_NONE;
  7578. }
  7579. else
  7580. {
  7581. // The default effect inside remote panel is move,
  7582. // unless we do not support it, but support copy
  7583. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7584. {
  7585. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7586. }
  7587. else
  7588. {
  7589. // with ctrl-down, we want copy unless it is not supported
  7590. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7591. }
  7592. }
  7593. }
  7594. }
  7595. }
  7596. }
  7597. //---------------------------------------------------------------------------
  7598. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7599. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7600. {
  7601. DebugAssert(!IsLocalBrowserMode());
  7602. if (FDDTotalSize >= 0)
  7603. {
  7604. if (File->IsDirectory)
  7605. {
  7606. FDDTotalSize = -1;
  7607. }
  7608. else
  7609. {
  7610. FDDTotalSize += File->Size;
  7611. }
  7612. }
  7613. DebugAssert(!FDragTempDir.IsEmpty());
  7614. // TODO: this is quite ineffective
  7615. // TODO: what if invalid character replacement is disabled?
  7616. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7617. UnicodeString TransferFileName;
  7618. if (Sender == RemoteDriveView)
  7619. {
  7620. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7621. }
  7622. else
  7623. {
  7624. TransferFileName = File->FileName;
  7625. }
  7626. FDDFileList->AddObject(TransferFileName, File);
  7627. }
  7628. //---------------------------------------------------------------------------
  7629. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7630. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7631. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7632. {
  7633. // sometimes we do not get DDEnd so the list is not released
  7634. SAFE_DESTROY(FDDFileList);
  7635. FDDFileList = new TStringList();
  7636. FDragTempDir = WinConfiguration->TemporaryDir();
  7637. FDDTotalSize = 0;
  7638. FDragDropSshTerminate.reset(NULL);
  7639. }
  7640. //---------------------------------------------------------------------------
  7641. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7642. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7643. HRESULT & Result)
  7644. {
  7645. if (Result == DRAGDROP_S_DROP)
  7646. {
  7647. try
  7648. {
  7649. GlobalDragImageList->HideDragImage();
  7650. try
  7651. {
  7652. RemoteFileControlDDTargetDrop();
  7653. }
  7654. catch(ESshTerminate & E)
  7655. {
  7656. FDragDropSshTerminate.reset(E.Clone());
  7657. }
  7658. }
  7659. catch (Exception &E)
  7660. {
  7661. // If downloading fails we need to cancel drag&drop, otherwise
  7662. // Explorer shows error
  7663. // But by the way exception probably never reach this point as
  7664. // it's catched on way.
  7665. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7666. Result = DRAGDROP_S_CANCEL;
  7667. ShowExtendedException(Terminal, &E);
  7668. }
  7669. }
  7670. }
  7671. //---------------------------------------------------------------------------
  7672. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7673. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7674. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7675. {
  7676. TFileMasks::TParams MaskParams;
  7677. MaskParams.Size = Size;
  7678. MaskParams.Modification = Modification;
  7679. // this does not re-parse the mask if it is the same as the last time
  7680. FDirViewMatchMask = Masks;
  7681. bool ImplicitMatch;
  7682. Matches =
  7683. // RecurseInclude parameter has no effect as the Path is empty
  7684. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7685. (AllowImplicitMatches || !ImplicitMatch);
  7686. }
  7687. //---------------------------------------------------------------------------
  7688. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7689. TObject * Sender, TListItem * Item, WORD & Indexes)
  7690. {
  7691. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7692. UnicodeString Ext;
  7693. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7694. {
  7695. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7696. }
  7697. else
  7698. {
  7699. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7700. }
  7701. if (SameText(Ext, Configuration->PartialExt))
  7702. {
  7703. Indexes |= oiPartial;
  7704. }
  7705. }
  7706. //---------------------------------------------------------------------------
  7707. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7708. {
  7709. return
  7710. DirViewEnabled(osCurrent) &&
  7711. DirView(osCurrent)->CanPasteFromClipBoard();
  7712. }
  7713. //---------------------------------------------------------------------------
  7714. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7715. {
  7716. bool Result = false;
  7717. if (CanPasteToDirViewFromClipBoard())
  7718. {
  7719. Result = true;
  7720. }
  7721. else
  7722. {
  7723. UnicodeString ClipboardText;
  7724. if (NonEmptyTextFromClipboard(ClipboardText))
  7725. {
  7726. if (StoredSessions->IsUrl(ClipboardText))
  7727. {
  7728. Result = true;
  7729. }
  7730. else
  7731. {
  7732. Result = DirViewEnabled(osCurrent);
  7733. }
  7734. }
  7735. }
  7736. return Result;
  7737. }
  7738. //---------------------------------------------------------------------------
  7739. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7740. {
  7741. if (DoesClipboardContainOurFiles())
  7742. {
  7743. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7744. {
  7745. DebugAssert(!IsLocalBrowserMode());
  7746. TTerminalManager * Manager = TTerminalManager::Instance();
  7747. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7748. Manager->ActiveTerminal = FClipboardTerminal;
  7749. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7750. }
  7751. }
  7752. else if (CanPasteToDirViewFromClipBoard())
  7753. {
  7754. DirView(osCurrent)->PasteFromClipBoard();
  7755. }
  7756. else
  7757. {
  7758. UnicodeString ClipboardText;
  7759. if (NonEmptyTextFromClipboard(ClipboardText))
  7760. {
  7761. if (StoredSessions->IsUrl(ClipboardText))
  7762. {
  7763. NewSession(false, ClipboardText);
  7764. }
  7765. else
  7766. {
  7767. DirView(osCurrent)->Path = ClipboardText;
  7768. }
  7769. }
  7770. }
  7771. }
  7772. //---------------------------------------------------------------------------
  7773. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7774. {
  7775. // TBD
  7776. }
  7777. //---------------------------------------------------------------------------
  7778. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7779. {
  7780. TCustomDirView * ADirView = DirView(Side);
  7781. ADirView->SelectAll(Mode);
  7782. ADirView->SetFocus();
  7783. }
  7784. //---------------------------------------------------------------------------
  7785. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7786. {
  7787. TCustomDirView * ADirView = DirView(Side);
  7788. TFileFilter Filter;
  7789. DefaultFileFilter(Filter);
  7790. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7791. {
  7792. Configuration->Usage->Inc(L"MaskSelections");
  7793. ADirView->SelectFiles(Filter, Select);
  7794. ADirView->SetFocus();
  7795. }
  7796. }
  7797. //---------------------------------------------------------------------------
  7798. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7799. {
  7800. TCustomDirView * ADirView = DirView(Side);
  7801. ADirView->RestoreSelectedNames();
  7802. ADirView->SetFocus();
  7803. }
  7804. //---------------------------------------------------------------------------
  7805. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7806. {
  7807. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7808. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7809. {
  7810. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7811. UnicodeString Ext;
  7812. if (!IsSideLocalBrowser(osCurrent))
  7813. {
  7814. Ext = UnixExtractFileExt(FileName);
  7815. }
  7816. else
  7817. {
  7818. Ext = ExtractFileExt(FileName);
  7819. }
  7820. if (Ext.IsEmpty())
  7821. {
  7822. Ext = L".";
  7823. }
  7824. TFileFilter Filter;
  7825. Filter.Masks = FORMAT(L"*%s", (Ext));
  7826. Filter.Directories = false;
  7827. CurrentDirView->SelectFiles(Filter, Select);
  7828. }
  7829. }
  7830. //---------------------------------------------------------------------------
  7831. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7832. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7833. {
  7834. UnicodeString Result;
  7835. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7836. {
  7837. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7838. }
  7839. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7840. {
  7841. // noop
  7842. }
  7843. else
  7844. {
  7845. Result =
  7846. FMTLOAD(FILE_INFO_FORMAT,
  7847. (FormatBytes(FileInfo.SelectedSize),
  7848. FormatBytes(FileInfo.FilesSize),
  7849. FormatNumber(FileInfo.SelectedCount),
  7850. FormatNumber(FileInfo.FilesCount)));
  7851. }
  7852. return Result;
  7853. }
  7854. //---------------------------------------------------------------------------
  7855. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7856. TTBXStatusPanel * Panel, TOperationSide Side)
  7857. {
  7858. if (Panel->Index == 1)
  7859. {
  7860. ToggleShowHiddenFiles();
  7861. }
  7862. else if (Panel->Index == 2)
  7863. {
  7864. Filter(Side);
  7865. }
  7866. }
  7867. //---------------------------------------------------------------------------
  7868. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7869. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7870. {
  7871. DebugAssert(!StatusBar->SimplePanel);
  7872. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7873. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7877. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7878. {
  7879. DebugAssert(StatusBar->Panels->Count >= 3);
  7880. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7881. if (FileInfo.HiddenCount > 0)
  7882. {
  7883. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7884. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7885. }
  7886. else
  7887. {
  7888. HiddenFilesPanel->ViewPriority = 0;
  7889. // not really necessary, just to cleanup no-longer-valid data
  7890. HiddenFilesPanel->Caption = L"";
  7891. }
  7892. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7893. if (FileInfo.FilteredCount > 0)
  7894. {
  7895. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7896. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7897. }
  7898. else
  7899. {
  7900. FilteredFilesPanel->ViewPriority = 0;
  7901. // not really necessary, just to cleanup no-longer-valid data
  7902. FilteredFilesPanel->Caption = L"";
  7903. }
  7904. }
  7905. //---------------------------------------------------------------------------
  7906. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7907. TObject * /*Sender*/)
  7908. {
  7909. if (DirView(osRemote)->Enabled)
  7910. {
  7911. DirView(osRemote)->SetFocus();
  7912. }
  7913. }
  7914. //---------------------------------------------------------------------------
  7915. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7916. {
  7917. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7918. switch (Config.Show)
  7919. {
  7920. case qvShow:
  7921. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7922. {
  7923. Config.Show = qvHide;
  7924. }
  7925. else
  7926. {
  7927. Config.Show = Config.LastHideShow;
  7928. }
  7929. break;
  7930. case qvHideWhenEmpty:
  7931. if (ComponentVisible[fcQueueView])
  7932. {
  7933. Config.Show = qvHide;
  7934. }
  7935. else
  7936. {
  7937. Config.LastHideShow = Config.Show;
  7938. Config.Show = qvShow;
  7939. }
  7940. break;
  7941. case qvHide:
  7942. Config.LastHideShow = Config.Show;
  7943. Config.Show = qvShow;
  7944. break;
  7945. }
  7946. WinConfiguration->QueueView = Config;
  7947. }
  7948. //---------------------------------------------------------------------------
  7949. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7950. {
  7951. UnicodeString Result;
  7952. if (FTerminal != NULL)
  7953. {
  7954. switch (WinConfiguration->PathInCaption)
  7955. {
  7956. case picShort:
  7957. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7958. break;
  7959. case picFull:
  7960. Result = FTerminal->CurrentDirectory;
  7961. break;
  7962. }
  7963. }
  7964. return Result;
  7965. }
  7966. //---------------------------------------------------------------------------
  7967. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7968. {
  7969. TColor Result = GetWindowColor(FSessionColor);
  7970. return Result;
  7971. }
  7972. //---------------------------------------------------------------------------
  7973. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7974. {
  7975. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7976. return Result;
  7977. }
  7978. //---------------------------------------------------------------------------
  7979. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7980. {
  7981. TTerminalManager::Instance()->UpdateAppTitle();
  7982. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7983. // background to gray on Windows 7 (works on Windows 10).
  7984. // See also EnableControl
  7985. if (Showing)
  7986. {
  7987. if (Terminal != NULL)
  7988. {
  7989. // Update path when it changes
  7990. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7991. {
  7992. UpdateSessionTab(SessionsPageControl->ActivePage);
  7993. }
  7994. }
  7995. NonVisualDataModule->ReconnectSessionAction->Update();
  7996. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  7997. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  7998. CenterReconnectToolbar();
  7999. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  8000. if (HasTerminal)
  8001. {
  8002. // TODO needed yet with second local browser?
  8003. if (!RemoteDirView->Enabled)
  8004. {
  8005. RemoteDirView->Enabled = true;
  8006. if (FRemoteDirViewWasFocused)
  8007. {
  8008. ActiveControl = RemoteDirView;
  8009. }
  8010. }
  8011. RemoteDirView->Color = PanelColor();
  8012. }
  8013. else
  8014. {
  8015. if (RemoteDirView->Enabled)
  8016. {
  8017. // This is first called when the form is being constructed
  8018. // (not anymore due to Showing test above)
  8019. // but the false is overriden in the constructor later.
  8020. // An even later in TScpCommanderForm::DoShow()
  8021. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8022. RemoteDirView->Enabled = false;
  8023. RemoteDirView->Color = DisabledPanelColor();
  8024. }
  8025. }
  8026. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8027. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8028. RemoteDriveView->Color = RemoteDirView->Color;
  8029. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8030. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  8031. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8032. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8033. QueueLabelUpdateStatus();
  8034. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8035. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8036. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8037. }
  8038. }
  8039. //---------------------------------------------------------------------------
  8040. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8041. {
  8042. UpdateControls();
  8043. }
  8044. //---------------------------------------------------------------------------
  8045. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8046. TObject * Sender)
  8047. {
  8048. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8049. DebugAssert(DirView != NULL);
  8050. DoDirViewLoaded(DirView);
  8051. TransferPresetAutoSelect();
  8052. }
  8053. //---------------------------------------------------------------------------
  8054. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8055. {
  8056. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8057. // first run after installation
  8058. if (double(Updates.LastCheck) == 0)
  8059. {
  8060. // make sure next time there will be an update (if enabled)
  8061. Updates.LastCheck = TDateTime(1);
  8062. WinConfiguration->Updates = Updates;
  8063. }
  8064. else if ((double(Updates.Period) > 0) &&
  8065. (Now() - Updates.LastCheck >= Updates.Period))
  8066. {
  8067. TThreadMethod OnUpdatesChecked = NULL;
  8068. if (!IsUWP())
  8069. {
  8070. OnUpdatesChecked = UpdatesChecked;
  8071. }
  8072. StartUpdateThread(OnUpdatesChecked);
  8073. }
  8074. }
  8075. //---------------------------------------------------------------------------
  8076. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8077. {
  8078. UnicodeString Message;
  8079. bool New;
  8080. TQueryType Type;
  8081. GetUpdatesMessage(Message, New, Type, false);
  8082. if (!Message.IsEmpty())
  8083. {
  8084. if (New)
  8085. {
  8086. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8087. }
  8088. if (!New && (Type != qtWarning))
  8089. {
  8090. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8091. }
  8092. else
  8093. {
  8094. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8095. }
  8096. if (New)
  8097. {
  8098. Configuration->Usage->Inc(L"UpdateNotifications");
  8099. }
  8100. }
  8101. }
  8102. //---------------------------------------------------------------------------
  8103. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8104. {
  8105. RestoreApp();
  8106. bool CanDisplay = !NonVisualDataModule->Busy;
  8107. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8108. {
  8109. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8110. }
  8111. if (CanDisplay)
  8112. {
  8113. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8114. CheckForUpdates(true);
  8115. }
  8116. else
  8117. {
  8118. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8119. }
  8120. }
  8121. //---------------------------------------------------------------------------
  8122. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8123. TCopyParamRuleData & Data)
  8124. {
  8125. DebugAssert(Terminal != NULL);
  8126. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8127. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8128. Data.RemoteDirectory = RemoteDirView->PathName;
  8129. }
  8130. //---------------------------------------------------------------------------
  8131. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8132. {
  8133. // Terminal can be null when we are changing local directory implicitly,
  8134. // because it has been deleted, while no session is connected
  8135. // (Login dialog is open)
  8136. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8137. {
  8138. DebugAssert(!IsLocalBrowserMode());
  8139. TCopyParamRuleData Data;
  8140. GetTransferPresetAutoSelectData(Data);
  8141. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8142. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8143. if (CopyParamIndex < 0)
  8144. {
  8145. // there is no preset that matches autoselection
  8146. // set preset that we consider "default"
  8147. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8148. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8149. }
  8150. else
  8151. {
  8152. // there is preset matching autoselection
  8153. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8154. if (CopyParamName == FCopyParamAutoSelected)
  8155. {
  8156. // autoselected the same preset as the last time
  8157. // make no change (i.e. preserve custom user preset, if any)
  8158. }
  8159. else
  8160. {
  8161. // autoselected the different preset then the last time (or there
  8162. // was default preset set)
  8163. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8164. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8165. }
  8166. }
  8167. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8168. {
  8169. if (CopyParamIndex >= 0)
  8170. {
  8171. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8172. }
  8173. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8174. try
  8175. {
  8176. int Fmt =
  8177. (CopyParamIndex < 0) ?
  8178. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8179. COPY_PARAM_AUTOSELECTED;
  8180. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8181. Data->Message = MainInstructions(Message);
  8182. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8183. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8184. CopyParamAttrs);
  8185. if (CopyParamIndex >= 0)
  8186. {
  8187. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8188. Info = FORMAT(L"%s\n \n%s", (Info,
  8189. FMTLOAD(COPY_PARAM_RULE,
  8190. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8191. }
  8192. Data->Message += L"\n\n" + Info;
  8193. if (WinConfiguration->CopyParamAutoSelectNotice)
  8194. {
  8195. TransferPresetNoteMessage(Data, true);
  8196. }
  8197. else
  8198. {
  8199. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8200. Data = NULL; // ownership passed
  8201. }
  8202. }
  8203. __finally
  8204. {
  8205. delete Data;
  8206. }
  8207. }
  8208. }
  8209. }
  8210. //---------------------------------------------------------------------------
  8211. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8212. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8213. {
  8214. DebugAssert(NoteData != NULL);
  8215. DebugAssert(!IsLocalBrowserMode());
  8216. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8217. TQueryButtonAlias Aliases[1];
  8218. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8219. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8220. Params.Aliases = Aliases;
  8221. Params.AliasesCount = LENOF(Aliases);
  8222. unsigned int Result =
  8223. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8224. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8225. switch (Result)
  8226. {
  8227. case qaNeverAskAgain:
  8228. DebugAssert(AllowNeverAskAgain);
  8229. WinConfiguration->CopyParamAutoSelectNotice = false;
  8230. break;
  8231. case qaIgnore:
  8232. PreferencesDialog(pmPresets);
  8233. break;
  8234. }
  8235. }
  8236. //---------------------------------------------------------------------------
  8237. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8238. {
  8239. // as of now useless, as this is used for notes only, never for balloons, ...
  8240. RestoreApp();
  8241. // .. and we should never be busy here
  8242. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8243. {
  8244. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8245. }
  8246. }
  8247. //---------------------------------------------------------------------------
  8248. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8249. TPreferencesMode APreferencesMode)
  8250. {
  8251. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8252. TCopyParamRuleData Data;
  8253. if (Terminal != NULL)
  8254. {
  8255. PreferencesData.reset(new TPreferencesDialogData());
  8256. GetTransferPresetAutoSelectData(Data);
  8257. PreferencesData->CopyParamRuleData = &Data;
  8258. }
  8259. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8260. }
  8261. //---------------------------------------------------------------------------
  8262. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8263. const TCustomCommandType & Command)
  8264. {
  8265. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8266. {
  8267. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8268. }
  8269. }
  8270. //---------------------------------------------------------------------------
  8271. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8272. {
  8273. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8274. TCustomCommandType Command;
  8275. // make sure we use local custom command when remote are not supported
  8276. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8277. {
  8278. Command = FLastCustomCommand;
  8279. }
  8280. else
  8281. {
  8282. Command.Params = Command.Params | ccLocal;
  8283. }
  8284. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8285. FEditingFocusedAdHocCommand = OnFocused;
  8286. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8287. int Options =
  8288. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8289. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8290. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8291. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8292. {
  8293. FLastCustomCommand = Command;
  8294. UpdateCustomCommandsToolbar();
  8295. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8296. }
  8297. }
  8298. //---------------------------------------------------------------------------
  8299. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8300. {
  8301. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8302. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8303. DebugAssert(State > 0);
  8304. if (State <= 0)
  8305. {
  8306. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8307. }
  8308. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8309. }
  8310. //---------------------------------------------------------------------------
  8311. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8312. TCustomCommandType & Command, int & State)
  8313. {
  8314. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8315. if (Result)
  8316. {
  8317. Command = FLastCustomCommand;
  8318. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8319. }
  8320. return Result;
  8321. }
  8322. //---------------------------------------------------------------------------
  8323. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8324. {
  8325. ResetIncrementalSearch();
  8326. }
  8327. //---------------------------------------------------------------------------
  8328. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8329. {
  8330. DebugAssert(ComponentVisible[Component]);
  8331. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8332. }
  8333. //---------------------------------------------------------------------------
  8334. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8335. {
  8336. // when queue panel is resized here directly, the status bar is stretched
  8337. // over whole space the panel occupied
  8338. PostComponentHide(fcQueueView);
  8339. }
  8340. //---------------------------------------------------------------------------
  8341. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8342. {
  8343. WinConfiguration->ResetSysDarkTheme();
  8344. ConfigurationChanged();
  8345. ConfigureInterface();
  8346. TForm::Dispatch(&Message);
  8347. }
  8348. //---------------------------------------------------------------------------
  8349. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8350. {
  8351. TMessage * M = static_cast<TMessage*>(Message);
  8352. switch (M->Msg)
  8353. {
  8354. case CM_DIALOGCHAR:
  8355. CMDialogChar(*M);
  8356. break;
  8357. case WM_APPCOMMAND:
  8358. WMAppCommand(*M);
  8359. break;
  8360. case WM_SYSCOMMAND:
  8361. WMSysCommand(*M);
  8362. break;
  8363. case WM_QUERYENDSESSION:
  8364. WMQueryEndSession(*M);
  8365. break;
  8366. case WM_ENDSESSION:
  8367. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8368. break;
  8369. #ifdef _DEBUG
  8370. case WM_WINDOWPOSCHANGED:
  8371. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8372. break;
  8373. case CM_CUSTOMSTYLECHANGED:
  8374. TForm::Dispatch(Message);
  8375. break;
  8376. #endif
  8377. case WM_COMPONENT_HIDE:
  8378. {
  8379. Byte Component = static_cast<Byte>(M->WParam);
  8380. // sanity check
  8381. if (ComponentVisible[Component])
  8382. {
  8383. // special treatment
  8384. if (Component == fcQueueView)
  8385. {
  8386. ToggleQueueVisibility();
  8387. DebugAssert(!ComponentVisible[fcQueueView]);
  8388. }
  8389. else
  8390. {
  8391. ComponentVisible[Component] = false;
  8392. }
  8393. }
  8394. }
  8395. break;
  8396. case WM_COPYDATA:
  8397. WMCopyData(*M);
  8398. break;
  8399. case WM_MANAGES_CAPTION:
  8400. // caption managed in TTerminalManager::UpdateAppTitle()
  8401. M->Result = 1;
  8402. break;
  8403. case WM_WANTS_MOUSEWHEEL:
  8404. M->Result = 1;
  8405. break;
  8406. case CM_SHOWINGCHANGED:
  8407. CMShowingChanged(*M);
  8408. break;
  8409. case WM_CLOSE:
  8410. WMClose(*M);
  8411. break;
  8412. case WM_DPICHANGED:
  8413. WMDpiChanged(*M);
  8414. break;
  8415. case CM_DPICHANGED:
  8416. CMDpiChanged(*M);
  8417. break;
  8418. case WM_WININICHANGE:
  8419. WMWinIniChange(*M);
  8420. break;
  8421. case CM_DIALOGKEY:
  8422. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8423. break;
  8424. default:
  8425. TForm::Dispatch(Message);
  8426. break;
  8427. }
  8428. }
  8429. //---------------------------------------------------------------------------
  8430. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8431. {
  8432. // noop
  8433. }
  8434. //---------------------------------------------------------------------------
  8435. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8436. {
  8437. TForm::Dispatch(&Message);
  8438. FSessionColors->Clear();
  8439. AddFixedSessionImages();
  8440. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8441. UpdateImages();
  8442. }
  8443. //---------------------------------------------------------------------------
  8444. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8445. {
  8446. TForm::Dispatch(&Message);
  8447. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8448. UpdatePixelsPerInchMainWindowCounter();
  8449. }
  8450. //---------------------------------------------------------------------------
  8451. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8452. {
  8453. // Cannot close window while we are busy.
  8454. // We cannot test this in FormCloseQuery as that is called also from
  8455. // Close(), which is called by CloseApplicationAction. So we can be busy
  8456. // there already even, when it is legitimate to close the application.
  8457. // Possibly a better place to handle this would be WMSysCommand.
  8458. if (NonVisualDataModule->Busy)
  8459. {
  8460. Message.Result = 1;
  8461. }
  8462. else
  8463. {
  8464. TForm::Dispatch(&Message);
  8465. }
  8466. }
  8467. //---------------------------------------------------------------------------
  8468. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8469. {
  8470. TForm::Dispatch(&Message);
  8471. if (Showing && (Terminal == NULL))
  8472. {
  8473. // When we are starting minimized (i.e. from an installer),
  8474. // postpone showing Login dialog until we get restored.
  8475. // Otherwise the Login dialog (and Authentication window) show restored
  8476. // over invisible (minimized) main window.
  8477. if (WindowState == wsMinimized)
  8478. {
  8479. FNeedSession = true;
  8480. }
  8481. else
  8482. {
  8483. // This happens before application ever goes idle, so the toolbars would
  8484. // stay enabled (initial state) until the Login dialog is dismissed.
  8485. UpdateActions();
  8486. NonVisualDataModule->StartBusy();
  8487. try
  8488. {
  8489. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8490. // otherwise the dialog does not receive focus.
  8491. // With Commander interface the ProcessMessages is called already
  8492. // by TDriveView, but with Explorer interface, we need to call it explicily
  8493. Application->ProcessMessages();
  8494. // do not reload sessions, they have been loaded just now (optimization)
  8495. NeedSession(true);
  8496. }
  8497. __finally
  8498. {
  8499. NonVisualDataModule->EndBusy();
  8500. }
  8501. }
  8502. }
  8503. }
  8504. //---------------------------------------------------------------------------
  8505. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8506. {
  8507. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8508. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8509. }
  8510. //---------------------------------------------------------------------------
  8511. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8512. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8513. int & MaxHeight)
  8514. {
  8515. // workaround for bug in TWinControl.CalcConstraints
  8516. // Check for empty rect (restore from iconic state) is done there only after
  8517. // call to AdjustClientRect, which enlarges the rect (for forms).
  8518. TRect R = GetClientRect();
  8519. // when restoring from iconic state, place no restrictions
  8520. if (IsRectEmpty(R))
  8521. {
  8522. MinWidth = 0;
  8523. MinHeight = 0;
  8524. MaxWidth = 32000;
  8525. MaxHeight = 32000;
  8526. }
  8527. }
  8528. //---------------------------------------------------------------------------
  8529. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8530. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8531. {
  8532. // terminal can be already closed (e.g. dropped connection)
  8533. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8534. {
  8535. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8536. try
  8537. {
  8538. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8539. }
  8540. catch(...)
  8541. {
  8542. if (!Terminal->Active)
  8543. {
  8544. Close = true;
  8545. }
  8546. throw;
  8547. }
  8548. }
  8549. }
  8550. //---------------------------------------------------------------------------
  8551. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8552. {
  8553. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8554. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8555. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8556. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8557. {
  8558. OnGetSpaceAvailable = GetSpaceAvailable;
  8559. }
  8560. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8561. OnGetSpaceAvailable);
  8562. }
  8563. //---------------------------------------------------------------------------
  8564. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8565. {
  8566. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8567. Terminal->FillSessionDataForCode(Data.get());
  8568. return Data.release();
  8569. }
  8570. //---------------------------------------------------------------------------
  8571. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8572. {
  8573. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8574. DoGenerateUrlDialog(Data.get(), Paths);
  8575. }
  8576. //---------------------------------------------------------------------------
  8577. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8578. {
  8579. GenerateUrl(NULL);
  8580. }
  8581. //---------------------------------------------------------------------------
  8582. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8583. {
  8584. std::unique_ptr<TStrings> Paths(new TStringList());
  8585. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8586. GenerateUrl(Paths.get());
  8587. }
  8588. //---------------------------------------------------------------------------
  8589. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8590. {
  8591. FSessionColor = value;
  8592. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8593. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8594. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8595. DebugAssert(ColorItem != NULL);
  8596. ColorItem->Color = C;
  8597. NonVisualDataModule->ColorMenuItem->Color = C;
  8598. UpdateControls();
  8599. }
  8600. //---------------------------------------------------------------------------
  8601. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8602. {
  8603. if (value != FSessionColor)
  8604. {
  8605. UpdateSessionColor(value);
  8606. // Is null when called from LastTerminalClosed
  8607. if (Terminal != NULL)
  8608. {
  8609. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8610. }
  8611. }
  8612. }
  8613. //---------------------------------------------------------------------------
  8614. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8615. {
  8616. bool Result = FNoteTimer->Enabled;
  8617. if (Result)
  8618. {
  8619. // cannot cancel note too early
  8620. bool NotEarly =
  8621. (Now() - FNoteShown >
  8622. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8623. if (Force || NotEarly)
  8624. {
  8625. FNoteTimer->Enabled = false;
  8626. FNote = L"";
  8627. SAFE_DESTROY(FNoteData);
  8628. FOnNoteClick = NULL;
  8629. FNoteHints = FNotes->Text;
  8630. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8631. UpdateStatusBar();
  8632. }
  8633. }
  8634. return Result;
  8635. }
  8636. //---------------------------------------------------------------------------
  8637. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8638. {
  8639. DebugAssert(FNoteTimer->Enabled);
  8640. CancelNote(true);
  8641. }
  8642. //---------------------------------------------------------------------------
  8643. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8644. {
  8645. int P = Note.Pos(L"\n");
  8646. if (P > 0)
  8647. {
  8648. Note.SetLength(P - 1);
  8649. }
  8650. FNotes->Add(FORMAT(L"[%s] %s",
  8651. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8652. while (FNotes->Count > 10)
  8653. {
  8654. FNotes->Delete(0);
  8655. }
  8656. if (UpdateNow)
  8657. {
  8658. FNoteHints = FNotes->Text;
  8659. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8660. UpdateStatusBar();
  8661. }
  8662. }
  8663. //---------------------------------------------------------------------------
  8664. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8665. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8666. {
  8667. int P = Note.Pos(L"\n");
  8668. if (P > 0)
  8669. {
  8670. Note.SetLength(P - 1);
  8671. }
  8672. FNoteHints = FNotes->Text;
  8673. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8674. FNote = Note;
  8675. SAFE_DESTROY(FNoteData);
  8676. FNoteData = NoteData;
  8677. FOnNoteClick = OnNoteClick;
  8678. AddNote(Note, false);
  8679. UpdateStatusBar();
  8680. FNoteShown = Now();
  8681. FNoteTimer->Enabled = false;
  8682. if (Seconds == 0)
  8683. {
  8684. Seconds = WinConfiguration->NotificationsTimeout;
  8685. }
  8686. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8687. FNoteTimer->Enabled = true;
  8688. }
  8689. //---------------------------------------------------------------------------
  8690. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8691. {
  8692. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8693. }
  8694. //---------------------------------------------------------------------------
  8695. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8696. {
  8697. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8698. {
  8699. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8700. }
  8701. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8702. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8703. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8704. 0, NULL, NULL);
  8705. }
  8706. //---------------------------------------------------------------------------
  8707. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8708. {
  8709. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8710. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8711. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8712. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8713. }
  8714. //---------------------------------------------------------------------------
  8715. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8716. {
  8717. WinConfiguration->FormatSizeBytes = Style;
  8718. }
  8719. //---------------------------------------------------------------------------
  8720. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8721. {
  8722. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8723. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8724. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8725. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8726. }
  8727. //---------------------------------------------------------------------------
  8728. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8729. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8730. {
  8731. if (Panel->Index == 0)
  8732. {
  8733. if (FOnNoteClick != NULL)
  8734. {
  8735. // prevent the user data from being freed by possible call
  8736. // to CancelNote or PostNote during call to OnNoteClick
  8737. std::unique_ptr<TObject> NoteData(FNoteData);
  8738. TNotifyEvent OnNoteClick = FOnNoteClick;
  8739. FNoteData = NULL;
  8740. // need to cancel the note as we are going to delete its user data
  8741. CancelNote(true);
  8742. OnNoteClick(NoteData.get());
  8743. }
  8744. }
  8745. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8746. {
  8747. FileSystemInfo();
  8748. }
  8749. }
  8750. //---------------------------------------------------------------------------
  8751. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8752. {
  8753. // workaround:
  8754. // 1) for unknown reason, disabling window, while minimized,
  8755. // prevents it from restoring, even if it was enabled again meanwhile
  8756. // 2) when disabling the main window, while another has focus
  8757. // minimize is no longer possible
  8758. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8759. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8760. // Shouldn't we use IsApplicationMinimized() here?
  8761. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8762. {
  8763. Enabled = false;
  8764. }
  8765. FLockLevel++;
  8766. }
  8767. //---------------------------------------------------------------------------
  8768. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8769. {
  8770. DebugAssert(FLockLevel > 0);
  8771. FLockLevel--;
  8772. if (FLockLevel == 0)
  8773. {
  8774. DebugAssert(FLockSuspendLevel == 0);
  8775. Enabled = true;
  8776. }
  8777. }
  8778. //---------------------------------------------------------------------------
  8779. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8780. {
  8781. // We need to make sure that window is enabled when the last modal window closes
  8782. // otherwise focus is not restored correctly.
  8783. // So we re-enable the window when modal window opens and
  8784. // disable it back after is closes
  8785. if (FLockLevel > 0)
  8786. {
  8787. // while we have nesting counter, we know that we never be called
  8788. // recursivelly as Application->OnModalBegin is called only
  8789. // for the top-level modal window
  8790. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8791. {
  8792. // won't be disabled when conditions in LockWindow() were not satisfied
  8793. FDisabledOnLockSuspend = !Enabled;
  8794. // When minimized to tray (or actually when set to SW_HIDE),
  8795. // setting Enabled makes the window focusable even when there's
  8796. // modal window over it
  8797. if (!FTrayIcon->Visible)
  8798. {
  8799. Enabled = true;
  8800. }
  8801. }
  8802. FLockSuspendLevel++;
  8803. }
  8804. }
  8805. //---------------------------------------------------------------------------
  8806. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8807. {
  8808. if (FLockSuspendLevel > 0)
  8809. {
  8810. DebugAssert(FLockLevel > 0);
  8811. FLockSuspendLevel--;
  8812. // see comment in SuspendWindowLock
  8813. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8814. {
  8815. // Note that window can be enabled here, when we were minized to tray when
  8816. // was SuspendWindowLock() called.
  8817. // We should possibly do the same check as in LockWindow(),
  8818. // if it is ever possible that the consitions change between
  8819. // SuspendWindowLock() and ResumeWindowLock()
  8820. Enabled = !FDisabledOnLockSuspend;
  8821. }
  8822. }
  8823. }
  8824. //---------------------------------------------------------------------------
  8825. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8826. {
  8827. if (!TextOnly)
  8828. {
  8829. TTBXComboBoxItem * RemotePathComboBox =
  8830. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8831. TStrings * Items = RemotePathComboBox->Strings;
  8832. Items->BeginUpdate();
  8833. try
  8834. {
  8835. Items->Clear();
  8836. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8837. {
  8838. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8839. while (!IsUnixRootPath(APath))
  8840. {
  8841. int P = APath.LastDelimiter(L'/');
  8842. DebugAssert(P >= 0);
  8843. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8844. APath.SetLength(P - 1);
  8845. }
  8846. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8847. }
  8848. }
  8849. __finally
  8850. {
  8851. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8852. // Setting ItemIndex to -1 does not reset its text
  8853. if (Items->Count == 0)
  8854. {
  8855. RemotePathComboBox->Text = L"";
  8856. }
  8857. Items->EndUpdate();
  8858. }
  8859. }
  8860. }
  8861. //---------------------------------------------------------------------------
  8862. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8863. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8864. int & ImageIndex)
  8865. {
  8866. if (AIndex < 0)
  8867. {
  8868. AIndex = Sender->ItemIndex;
  8869. }
  8870. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8871. }
  8872. //---------------------------------------------------------------------------
  8873. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8874. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8875. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8876. {
  8877. ARect.Left += (10 * AIndex);
  8878. }
  8879. //---------------------------------------------------------------------------
  8880. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8881. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8882. {
  8883. AWidth += (10 * AIndex);
  8884. }
  8885. //---------------------------------------------------------------------------
  8886. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8887. TObject * Sender)
  8888. {
  8889. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8890. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8891. int Index = RemotePathComboBox->ItemIndex;
  8892. while (Index < RemotePathComboBox->Strings->Count - 1)
  8893. {
  8894. APath = UnixExtractFileDir(APath);
  8895. Index++;
  8896. }
  8897. // VanDyke style paths
  8898. if (APath.IsEmpty())
  8899. {
  8900. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8901. APath = ROOTDIRECTORY;
  8902. }
  8903. if (RemoteDirView->Path != APath)
  8904. {
  8905. RemoteDirView->Path = APath;
  8906. }
  8907. }
  8908. //---------------------------------------------------------------------------
  8909. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8910. {
  8911. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8912. DebugUsedParam(Sender);
  8913. UpdateRemotePathComboBox(true);
  8914. }
  8915. //---------------------------------------------------------------------------
  8916. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8917. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8918. {
  8919. ResetIncrementalSearch();
  8920. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8921. DebugAssert(DirView != NULL);
  8922. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8923. {
  8924. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8925. // OnEditing is called also from TCustomListView::CanEdit
  8926. if (Edit != NULL)
  8927. {
  8928. EditSelectBaseName(Edit);
  8929. }
  8930. }
  8931. }
  8932. //---------------------------------------------------------------------------
  8933. //---------------------------------------------------------------------------
  8934. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8935. {
  8936. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8937. Result->AutoDetectDnD = false;
  8938. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8939. Result->RenderDataOn = rdoEnterAndDropSync;
  8940. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8941. return Result;
  8942. }
  8943. //---------------------------------------------------------------------------
  8944. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8945. {
  8946. TForm::CreateWnd();
  8947. if (FSessionsDragDropFilesEx == NULL)
  8948. {
  8949. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8950. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8951. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8952. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8953. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8954. }
  8955. if (FQueueDragDropFilesEx == NULL)
  8956. {
  8957. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8958. // No need to set OnDragOver as we do not have any restrictions
  8959. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8960. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8961. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8962. }
  8963. }
  8964. //---------------------------------------------------------------------------
  8965. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8966. {
  8967. TForm::DestroyWnd();
  8968. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8969. FQueueDragDropFilesEx->DragDropControl = NULL;
  8970. }
  8971. //---------------------------------------------------------------------------
  8972. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8973. {
  8974. SideEnter(FCurrentSide);
  8975. FEverShown = true;
  8976. }
  8977. //---------------------------------------------------------------------------
  8978. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8979. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8980. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8981. {
  8982. if (!NonVisualDataModule->Busy)
  8983. {
  8984. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8985. Configuration->Usage->Inc(L"FileFinds");
  8986. LockWindow(true);
  8987. NonVisualDataModule->StartBusy();
  8988. WinConfiguration->LockedInterface = true;
  8989. try
  8990. {
  8991. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8992. }
  8993. __finally
  8994. {
  8995. WinConfiguration->LockedInterface = false;
  8996. NonVisualDataModule->EndBusy();
  8997. UnlockWindow();
  8998. }
  8999. }
  9000. }
  9001. //---------------------------------------------------------------------------
  9002. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9003. {
  9004. if (!NonVisualDataModule->Busy)
  9005. {
  9006. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9007. SetFocus();
  9008. RemoteDirView->Path = UnixExtractFilePath(Path);
  9009. UnicodeString FileName = UnixExtractFileName(Path);
  9010. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9011. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9012. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9013. if (Item == NULL)
  9014. {
  9015. ReloadDirectory(osRemote);
  9016. Item = RemoteDirView->FindFileItem(FileName);
  9017. }
  9018. if (Item != NULL)
  9019. {
  9020. RemoteDirView->ItemFocused = Item;
  9021. RemoteDirView->ItemFocused->MakeVisible(false);
  9022. RemoteDirView->SetFocus();
  9023. }
  9024. }
  9025. }
  9026. //---------------------------------------------------------------------------
  9027. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9028. {
  9029. bool Result = !NonVisualDataModule->Busy;
  9030. if (Result)
  9031. {
  9032. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9033. }
  9034. return Result;
  9035. }
  9036. //---------------------------------------------------------------------------
  9037. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9038. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9039. {
  9040. if (CanOperateOnFoundFiles(ATerminal))
  9041. {
  9042. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9043. FOnFileOperationFinished = OnFileOperationFinished;
  9044. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9045. }
  9046. }
  9047. //---------------------------------------------------------------------------
  9048. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9049. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9050. {
  9051. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9052. }
  9053. //---------------------------------------------------------------------------
  9054. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9055. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9056. {
  9057. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9058. }
  9059. //---------------------------------------------------------------------------
  9060. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9061. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9062. {
  9063. if (CanOperateOnFoundFiles(ATerminal))
  9064. {
  9065. for (int Index = 0; Index < FileList->Count; Index++)
  9066. {
  9067. UnicodeString FileName = FileList->Strings[Index];
  9068. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9069. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9070. OnFileOperationFinished(FileName, true);
  9071. }
  9072. }
  9073. }
  9074. //---------------------------------------------------------------------------
  9075. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9076. {
  9077. FFileFindTerminal = Terminal;
  9078. ShowFileFindDialog(
  9079. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9080. DoDownloadFoundFiles, DoEditFoundFiles);
  9081. }
  9082. //---------------------------------------------------------------------------
  9083. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9084. {
  9085. FTaskbarList = TaskbarList;
  9086. }
  9087. //---------------------------------------------------------------------------
  9088. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9089. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9090. {
  9091. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9092. if (Index >= 0)
  9093. {
  9094. if (Button == mbLeft)
  9095. {
  9096. // "Mouse down" is raised only after tab is switched.
  9097. // If switching tab (switching session) takes long enough for user
  9098. // to actually release the button, "mouse down" is still raised,
  9099. // but we do not get "mouse up" event, so dragging is not cancelled,
  9100. // prevent that by not beginning dragging in the first place.
  9101. if (IsKeyPressed(VK_LBUTTON))
  9102. {
  9103. // when user clicks the "+", we get mouse down only after the session
  9104. // is closed, when new session tab is already on X:Y, so dragging
  9105. // starts, prevent that
  9106. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9107. {
  9108. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9109. if (Terminal != NULL)
  9110. {
  9111. SessionsPageControl->BeginDrag(false);
  9112. }
  9113. }
  9114. }
  9115. }
  9116. else if (Button == mbMiddle)
  9117. {
  9118. // ignore middle-click for "New session tab"
  9119. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9120. if (Terminal != NULL)
  9121. {
  9122. CloseSessionTab(Index);
  9123. }
  9124. }
  9125. }
  9126. }
  9127. //---------------------------------------------------------------------------
  9128. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9129. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9130. {
  9131. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9132. // do not allow dropping on the "+" tab
  9133. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9134. if ((TargetTerminal != NULL) &&
  9135. (SessionsPageControl->ActivePage->PageIndex != Index))
  9136. {
  9137. Configuration->Usage->Inc(L"SessionTabMoves");
  9138. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9139. // but we want to actually prevent that to avoid flicker
  9140. SessionsPageControl->ActivePage->PageIndex = Index;
  9141. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9142. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9143. }
  9144. }
  9145. //---------------------------------------------------------------------------
  9146. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9147. TObject * Sender, TObject * Source, int X, int Y,
  9148. TDragState /*State*/, bool & Accept)
  9149. {
  9150. Accept = (Sender == Source);
  9151. if (Accept)
  9152. {
  9153. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9154. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9155. // do not allow dragging to the "+" tab
  9156. Accept = (Terminal != NULL);
  9157. }
  9158. }
  9159. //---------------------------------------------------------------------------
  9160. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9161. const TPoint & Point, int & Effect)
  9162. {
  9163. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9164. if (Index < 0)
  9165. {
  9166. Effect = DROPEFFECT_NONE;
  9167. }
  9168. else
  9169. {
  9170. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9171. // do not allow dropping on the "+" tab
  9172. if (TargetTerminal == NULL)
  9173. {
  9174. Effect = DROPEFFECT_NONE;
  9175. }
  9176. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9177. {
  9178. Effect = DROPEFFECT_COPY;
  9179. }
  9180. }
  9181. }
  9182. //---------------------------------------------------------------------------
  9183. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9184. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9185. {
  9186. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9187. // do not allow dropping on the "+" tab
  9188. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9189. if (TargetTerminal != NULL)
  9190. {
  9191. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9192. if (!IsFileControl(DropSourceControl, osRemote))
  9193. {
  9194. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9195. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9196. // Why don't we use Terminal->CurrentDirectory directly?
  9197. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9198. // do not force queue, drag drop
  9199. false, true);
  9200. }
  9201. }
  9202. }
  9203. //---------------------------------------------------------------------------
  9204. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9205. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9206. {
  9207. // Downloads are handled in RemoteFileControlDDEnd
  9208. if (!IsFileControl(DropSourceControl, osRemote))
  9209. {
  9210. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9211. Terminal->CurrentDirectory,
  9212. // force queue, drag drop
  9213. true, true);
  9214. }
  9215. }
  9216. //---------------------------------------------------------------------------
  9217. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9218. {
  9219. FShowing = false;
  9220. // Do not save empty workspace
  9221. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9222. {
  9223. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9224. UnicodeString Name = WorkspaceName();
  9225. DoSaveWorkspace(
  9226. Name, DataList.get(),
  9227. !Configuration->DisablePasswordStoring &&
  9228. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9229. WinConfiguration->LastStoredSession = Name;
  9230. }
  9231. }
  9232. //---------------------------------------------------------------------------
  9233. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9234. {
  9235. TManagedTerminal * ManagedTerminal =
  9236. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9237. if (ManagedTerminal != NULL)
  9238. {
  9239. ManagedTerminal->DirectoryLoaded = Now();
  9240. }
  9241. }
  9242. //---------------------------------------------------------------------------
  9243. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9244. TListItem * /*Item*/, bool /*Selected*/)
  9245. {
  9246. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9247. switch (DirView->LastSelectMethod)
  9248. {
  9249. case smKeyboard:
  9250. Configuration->Usage->Inc(L"KeyboardSelections");
  9251. break;
  9252. case smMouse:
  9253. Configuration->Usage->Inc(L"MouseSelections");
  9254. break;
  9255. }
  9256. }
  9257. //---------------------------------------------------------------------------
  9258. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9259. {
  9260. if (Color != 0)
  9261. {
  9262. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9263. }
  9264. else
  9265. {
  9266. return FSessionTabImageIndex;
  9267. }
  9268. }
  9269. //---------------------------------------------------------------------------
  9270. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9271. {
  9272. TPngImageCollectionItem * Item =
  9273. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9274. FSessionColors->AddPng(Item->PngImage);
  9275. return FSessionColors->Count - 1;
  9276. }
  9277. //---------------------------------------------------------------------------
  9278. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9279. {
  9280. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9281. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9282. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9283. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9284. }
  9285. //---------------------------------------------------------------------------
  9286. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9287. {
  9288. for (int Index = 0; Index < Control->ControlCount; Index++)
  9289. {
  9290. TControl * AControl = Control->Controls[Index];
  9291. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9292. if (WinControl != NULL)
  9293. {
  9294. CollectItemsWithTextDisplayMode(WinControl);
  9295. }
  9296. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9297. if (Toolbar != NULL)
  9298. {
  9299. // we care for top-level items only
  9300. TTBCustomItem * Items = Toolbar->Items;
  9301. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9302. {
  9303. TTBCustomItem * Item = Items->Items[ItemIndex];
  9304. if (((Item->DisplayMode == nbdmImageAndText) ||
  9305. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9306. EligibleForImageDisplayMode(Item))
  9307. {
  9308. FItemsWithTextDisplayMode.insert(Item);
  9309. }
  9310. }
  9311. }
  9312. }
  9313. }
  9314. //---------------------------------------------------------------------------
  9315. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9316. {
  9317. return true;
  9318. }
  9319. //---------------------------------------------------------------------------
  9320. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9321. {
  9322. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9323. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9324. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9325. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9326. bool Result = true;
  9327. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9328. {
  9329. TTBCustomItem * Item = *i;
  9330. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9331. if (Label != NULL)
  9332. {
  9333. // optimization
  9334. if (Label->Visible == SelectiveToolbarText)
  9335. {
  9336. Result = false;
  9337. }
  9338. else
  9339. {
  9340. Label->Visible = SelectiveToolbarText;
  9341. }
  9342. }
  9343. else
  9344. {
  9345. // optimization
  9346. if (Item->DisplayMode == DisplayMode)
  9347. {
  9348. Result = false;
  9349. }
  9350. else
  9351. {
  9352. Item->DisplayMode = DisplayMode;
  9353. }
  9354. }
  9355. ++i;
  9356. }
  9357. if (Result)
  9358. {
  9359. UpdateNewSessionTab();
  9360. }
  9361. return Result;
  9362. }
  9363. //---------------------------------------------------------------------------
  9364. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9365. {
  9366. DebugFail();
  9367. }
  9368. //---------------------------------------------------------------------------
  9369. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9370. {
  9371. // Among other, a lock level is non zero, while directory is being loaded,
  9372. // even when it happens as a result of dirview navigation,
  9373. // i.e. when TNonVisualDataModule is NOT FBusy.
  9374. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9375. // Among other this prevents a panel auto update to occur while
  9376. // directory is changing.
  9377. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9378. }
  9379. //---------------------------------------------------------------------------
  9380. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9381. {
  9382. // While the window is disabled, we still seem to process menu shortcuts at least,
  9383. // so stop it at least here.
  9384. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9385. return
  9386. (Allowed == aaUpdate) ||
  9387. !NonVisualDataModule->Busy;
  9388. }
  9389. //---------------------------------------------------------------------------
  9390. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9391. {
  9392. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9393. }
  9394. //---------------------------------------------------------------------------
  9395. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9396. {
  9397. // This is somewhat redundant to LockWindow() call from
  9398. // TTerminalManager::TerminalReadDirectoryProgress.
  9399. // But disabling window is known not to block keyboard shorcuts
  9400. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9401. if (Busy > 0)
  9402. {
  9403. if (NonVisualDataModule->Busy)
  9404. {
  9405. State = false;
  9406. }
  9407. else
  9408. {
  9409. NonVisualDataModule->StartBusy();
  9410. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9411. LockWindow();
  9412. }
  9413. }
  9414. else if (Busy < 0)
  9415. {
  9416. UnlockWindow();
  9417. NonVisualDataModule->EndBusy();
  9418. }
  9419. else
  9420. {
  9421. State = NonVisualDataModule->Busy;
  9422. }
  9423. }
  9424. //---------------------------------------------------------------------------
  9425. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9426. {
  9427. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9428. // no context menu for "New session tab"
  9429. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9430. {
  9431. SessionsPageControl->ActivePageIndex = Index;
  9432. if (DebugAlwaysTrue(SessionTabSwitched()))
  9433. {
  9434. // copied from TControl.WMContextMenu
  9435. SendCancelMode(SessionsPageControl);
  9436. // explicit popup instead of using PopupMenu property
  9437. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9438. // while connecting yet not-connected session and hence
  9439. // allowing an access to commands over not-completelly connected session
  9440. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9441. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9442. PopupMenu->PopupComponent = SessionsPageControl;
  9443. PopupMenu->Popup(Point.x, Point.y);
  9444. }
  9445. }
  9446. Handled = true;
  9447. }
  9448. //---------------------------------------------------------------------------
  9449. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9450. {
  9451. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9452. }
  9453. //---------------------------------------------------------------------------
  9454. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9455. {
  9456. Configuration->Usage->Inc(L"PasswordChanges");
  9457. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9458. ChangePasswordData->DisableAuthentationsExceptPassword();
  9459. ChangePasswordData->ChangePassword = true;
  9460. std::unique_ptr<TTerminal> ChangePasswordSession(
  9461. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9462. ChangePasswordSession->AutoReadDirectory = false;
  9463. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9464. {
  9465. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9466. }
  9467. else
  9468. {
  9469. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9470. }
  9471. }
  9472. //---------------------------------------------------------------------------
  9473. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9474. {
  9475. // No nice way to assert SSH2
  9476. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9477. }
  9478. //---------------------------------------------------------------------------
  9479. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9480. {
  9481. TOperationVisualizer OperationVisualizer;
  9482. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9483. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9484. }
  9485. //---------------------------------------------------------------------------
  9486. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9487. {
  9488. TForm::ChangeScale(M, D);
  9489. int APixelsPerInch = GetControlPixelsPerInch(this);
  9490. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9491. try
  9492. {
  9493. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9494. }
  9495. __finally
  9496. {
  9497. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9498. }
  9499. }
  9500. //---------------------------------------------------------------------------
  9501. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9502. {
  9503. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9504. }
  9505. //---------------------------------------------------------------------
  9506. class TPublicControl : public TControl
  9507. {
  9508. friend class TCustomScpExplorerForm;
  9509. };
  9510. //---------------------------------------------------------------------------
  9511. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9512. {
  9513. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9514. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9515. }
  9516. //---------------------------------------------------------------------------
  9517. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9518. {
  9519. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9520. OpenBookmark(Side, Bookmark);
  9521. }
  9522. //---------------------------------------------------------------------------
  9523. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9524. {
  9525. DoBookmarkClick(osLocal, Sender);
  9526. }
  9527. //---------------------------------------------------------------------------
  9528. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9529. {
  9530. DoBookmarkClick(osRemote, Sender);
  9531. }
  9532. //---------------------------------------------------------------------------
  9533. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9534. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9535. {
  9536. // TODO
  9537. if (BookmarkList != NULL)
  9538. {
  9539. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9540. if (!WinConfiguration->UseLocationProfiles)
  9541. {
  9542. std::unique_ptr<TStringList> Directories(new TStringList());
  9543. Directories->CaseSensitive = (Side == osRemote);
  9544. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9545. {
  9546. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9547. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9548. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9549. {
  9550. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9551. Item->Caption = EscapeHotkey(Directory);
  9552. Item->ShortCut = Bookmark->ShortCut;
  9553. Item->OnClick = OnBookmarkClick;
  9554. Item->Tag = reinterpret_cast<int>(Bookmark);
  9555. Directories->Add(Directory);
  9556. Menu->Add(Item.release());
  9557. }
  9558. }
  9559. }
  9560. else
  9561. {
  9562. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9563. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9564. {
  9565. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9566. if (!Bookmark->Node.IsEmpty())
  9567. {
  9568. Folders->Add(Bookmark->Node);
  9569. }
  9570. }
  9571. for (int Index = 0; Index < Folders->Count; Index++)
  9572. {
  9573. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9574. Item->Caption = Folders->Strings[Index];
  9575. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9576. Folders->Objects[Index] = Item.get();
  9577. Menu->Add(Item.release());
  9578. }
  9579. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9580. {
  9581. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9582. TTBCustomItem * Parent;
  9583. if (!Bookmark->Node.IsEmpty())
  9584. {
  9585. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9586. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9587. }
  9588. else
  9589. {
  9590. Parent = Menu;
  9591. }
  9592. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9593. Item->Caption = Bookmark->Name;
  9594. Item->ShortCut = Bookmark->ShortCut;
  9595. Item->OnClick = OnBookmarkClick;
  9596. Item->Tag = reinterpret_cast<int>(Bookmark);
  9597. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9598. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9599. {
  9600. // No hint for location profiles that are actually mere "bookmarks"
  9601. }
  9602. else
  9603. {
  9604. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9605. UnicodeString LongHint;
  9606. if (!Bookmark->Local.IsEmpty())
  9607. {
  9608. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9609. }
  9610. if (!Bookmark->Remote.IsEmpty())
  9611. {
  9612. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9613. }
  9614. AddToList(Hint, LongHint, L"|");
  9615. Item->Hint = Hint;
  9616. }
  9617. Parent->Add(Item.release());
  9618. }
  9619. }
  9620. AddMenuSeparator(Menu);
  9621. }
  9622. }
  9623. //---------------------------------------------------------------------------
  9624. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9625. {
  9626. Menu->Clear();
  9627. std::unique_ptr<TTBCustomItem> Item;
  9628. Item.reset(new TTBXItem(Owner));
  9629. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9630. Item->Options = Item->Options << tboDefault;
  9631. Menu->Add(Item.release());
  9632. AddMenuSeparator(Menu);
  9633. if (Terminal != NULL)
  9634. {
  9635. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9636. }
  9637. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9638. Item.reset(new TTBXItem(Owner));
  9639. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9640. Menu->Add(Item.release());
  9641. }
  9642. //---------------------------------------------------------------------------
  9643. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9644. {
  9645. bool Result = true;
  9646. try
  9647. {
  9648. if (Side == osRemote)
  9649. {
  9650. Terminal->ExceptionOnFail = true;
  9651. }
  9652. try
  9653. {
  9654. DirView(Side)->Path = Path;
  9655. }
  9656. __finally
  9657. {
  9658. if (Side == osRemote)
  9659. {
  9660. Terminal->ExceptionOnFail = false;
  9661. }
  9662. }
  9663. }
  9664. catch (Exception & E)
  9665. {
  9666. if ((Side != osRemote) || Terminal->Active)
  9667. {
  9668. ShowExtendedExceptionEx(Terminal, &E);
  9669. Result = false;
  9670. }
  9671. else
  9672. {
  9673. throw;
  9674. }
  9675. }
  9676. return Result;
  9677. }
  9678. //---------------------------------------------------------------------------
  9679. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9680. {
  9681. // Make sure there some feedback even when loading tiny local folders
  9682. TInstantOperationVisualizer Visualizer;
  9683. DirView(Side)->ReloadDirectory();
  9684. }
  9685. //---------------------------------------------------------------------------
  9686. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9687. {
  9688. if (Index == SessionsPageControl->TabIndex)
  9689. {
  9690. CloseSession();
  9691. }
  9692. else
  9693. {
  9694. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9695. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9696. TTerminalManager * Manager = TTerminalManager::Instance();
  9697. if (!Terminal->Active)
  9698. {
  9699. Manager->FreeTerminal(Terminal);
  9700. }
  9701. else
  9702. {
  9703. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9704. if (CanCloseQueue(Queue))
  9705. {
  9706. Manager->FreeTerminal(Terminal);
  9707. }
  9708. }
  9709. }
  9710. }
  9711. //---------------------------------------------------------------------------
  9712. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9713. {
  9714. CloseSessionTab(Index);
  9715. }
  9716. //---------------------------------------------------------------------------
  9717. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9718. {
  9719. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9720. {
  9721. TInstantOperationVisualizer Visualizer;
  9722. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9723. // before we create a new data object
  9724. ClipboardClear();
  9725. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9726. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9727. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9728. // Intermediate variable to WORKAROUND a crash in compiler
  9729. TObjectList * ClipboardFakeMonitors =
  9730. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9731. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9732. if (FClipboardDragDropFilesEx.get() == NULL)
  9733. {
  9734. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9735. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9736. }
  9737. FClipboardDragDropFilesEx->FileList->Clear();
  9738. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9739. FClipboardDragDropFilesEx->CopyToClipboard();
  9740. FClipboardTerminal = FTerminal;
  9741. // Need full paths, as cwd can be different once files are pasted
  9742. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9743. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9744. {
  9745. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9746. }
  9747. }
  9748. }
  9749. //---------------------------------------------------------------------------
  9750. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9751. {
  9752. if (OpenClipboard(0))
  9753. {
  9754. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9755. CloseClipboard();
  9756. }
  9757. }
  9758. //---------------------------------------------------------------------------
  9759. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9760. {
  9761. return !FClipboardFakeDirectory.IsEmpty();
  9762. }
  9763. //---------------------------------------------------------------------------
  9764. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9765. {
  9766. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9767. FClipboardFakeDirectory = UnicodeString();
  9768. FClipboardTerminal = NULL;
  9769. // Also called in Idle()
  9770. FClipboardFakeMonitors.reset(NULL);
  9771. }
  9772. //---------------------------------------------------------------------------
  9773. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9774. {
  9775. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9776. {
  9777. ClipboardStop();
  9778. }
  9779. }
  9780. //---------------------------------------------------------------------------
  9781. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9782. {
  9783. TTerminalManager * Manager = TTerminalManager::Instance();
  9784. Manager->ActiveTerminal = FClipboardTerminal;
  9785. TTransferOperationParam Params;
  9786. Params.TargetDirectory = TargetDirectory;
  9787. Params.DragDrop = DragDrop;
  9788. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9789. }
  9790. //---------------------------------------------------------------------------
  9791. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9792. {
  9793. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9794. // (as it pumps a Windows message queue while being shutted down)
  9795. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9796. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9797. {
  9798. // Can fail as it can be e.g. locked by AV, so we retry that later
  9799. bool Removed = RemoveDir(ApiPath(FileName));
  9800. try
  9801. {
  9802. if (!NonVisualDataModule->Busy)
  9803. {
  9804. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9805. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9806. }
  9807. }
  9808. __finally
  9809. {
  9810. if (!Removed)
  9811. {
  9812. RemoveDir(ApiPath(FileName));
  9813. }
  9814. }
  9815. }
  9816. }
  9817. //---------------------------------------------------------------------------
  9818. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9819. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9820. {
  9821. TFileMasks::TParams MaskParams;
  9822. MaskParams.Size = Size;
  9823. MaskParams.Modification = Modification;
  9824. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9825. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9826. {
  9827. bool ImplicitMatch;
  9828. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9829. !ImplicitMatch)
  9830. {
  9831. Color = Iter->Color;
  9832. break;
  9833. }
  9834. }
  9835. }
  9836. //---------------------------------------------------------------------------
  9837. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9838. {
  9839. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9840. if (!DirView->IsEditing())
  9841. {
  9842. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9843. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9844. // while searching
  9845. if (Key == VK_BACK)
  9846. {
  9847. if (!FIncrementalSearch.IsEmpty())
  9848. {
  9849. if (FIncrementalSearch.Length() == 1)
  9850. {
  9851. ResetIncrementalSearch();
  9852. }
  9853. else
  9854. {
  9855. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9856. IncrementalSearch(NewText, false, false);
  9857. }
  9858. Key = 0;
  9859. }
  9860. }
  9861. }
  9862. }
  9863. //---------------------------------------------------------------------------
  9864. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9865. {
  9866. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9867. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9868. {
  9869. // Filter control sequences.
  9870. // When not searching yet, prefer use of the space for toggling file selection
  9871. // (so we cannot incrementally search for a string starting with a space).
  9872. if ((Key > VK_SPACE) ||
  9873. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9874. {
  9875. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9876. Key = 0;
  9877. }
  9878. }
  9879. }
  9880. //---------------------------------------------------------------------------
  9881. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9882. {
  9883. if (!FIncrementalSearch.IsEmpty())
  9884. {
  9885. FIncrementalSearch = L"";
  9886. DirView(osCurrent)->UpdateStatusBar();
  9887. }
  9888. }
  9889. //---------------------------------------------------------------------------
  9890. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9891. {
  9892. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9893. if (Item == NULL)
  9894. {
  9895. MessageBeep(MB_ICONHAND);
  9896. }
  9897. else
  9898. {
  9899. TCustomDirView * ADirView = DirView(osCurrent);
  9900. {
  9901. TAutoNestingCounter Guard(FIncrementalSearching);
  9902. ADirView->FocusItem(Item);
  9903. if (ADirView->NortonLike == nlOff)
  9904. {
  9905. if ((ADirView->Selected != Item) ||
  9906. (ADirView->SelCount > 1))
  9907. {
  9908. ADirView->ClearSelection();
  9909. ADirView->Selected = Item;
  9910. }
  9911. }
  9912. }
  9913. FIncrementalSearch = Text;
  9914. Item->MakeVisible(false);
  9915. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9916. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9917. ADirView->UpdateStatusBar();
  9918. }
  9919. }
  9920. //---------------------------------------------------------------------------
  9921. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9922. {
  9923. int Index = Item->Index;
  9924. if (!Reverse)
  9925. {
  9926. Index++;
  9927. if (Index >= Item->Owner->Count)
  9928. {
  9929. Index = 0;
  9930. }
  9931. }
  9932. else
  9933. {
  9934. Index--;
  9935. if (Index < 0)
  9936. {
  9937. Index = Item->Owner->Count - 1;
  9938. }
  9939. }
  9940. return Item->Owner->Item[Index];
  9941. }
  9942. //---------------------------------------------------------------------------
  9943. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9944. {
  9945. TCustomDirView * ADirView = DirView(osCurrent);
  9946. if (ADirView->Items->Count == 0)
  9947. {
  9948. return NULL;
  9949. }
  9950. else
  9951. {
  9952. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9953. TListItem * Item = CurrentItem;
  9954. if (SkipCurrent)
  9955. {
  9956. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9957. }
  9958. while (true)
  9959. {
  9960. bool Matches = false;
  9961. switch (WinConfiguration->PanelSearch)
  9962. {
  9963. case isNameStartOnly:
  9964. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9965. break;
  9966. case isName:
  9967. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9968. break;
  9969. case isAll:
  9970. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9971. int Index = 0;
  9972. while ((Index < ColCount) && !Matches)
  9973. {
  9974. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9975. if (Visible)
  9976. {
  9977. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9978. }
  9979. Index++;
  9980. }
  9981. break;
  9982. }
  9983. if (Matches)
  9984. {
  9985. return Item;
  9986. }
  9987. Item = GetNextFile(Item, Reverse);
  9988. if (Item == CurrentItem)
  9989. {
  9990. return NULL;
  9991. }
  9992. }
  9993. }
  9994. }
  9995. //---------------------------------------------------------------------------
  9996. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9997. {
  9998. ResetIncrementalSearch();
  9999. }
  10000. //---------------------------------------------------------------------------
  10001. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10002. {
  10003. if (Message.CharCode == VK_TAB)
  10004. {
  10005. if (!FIncrementalSearch.IsEmpty())
  10006. {
  10007. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10008. bool Reverse = Shift.Contains(ssShift);
  10009. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10010. Message.Result = 1;
  10011. return;
  10012. }
  10013. }
  10014. else if (Message.CharCode == VK_ESCAPE)
  10015. {
  10016. if (!FIncrementalSearch.IsEmpty())
  10017. {
  10018. ResetIncrementalSearch();
  10019. Message.Result = 1;
  10020. return;
  10021. }
  10022. }
  10023. TForm::Dispatch(&Message);
  10024. }
  10025. //---------------------------------------------------------------------------
  10026. void __fastcall TCustomScpExplorerForm::Deactivate()
  10027. {
  10028. TForm::Deactivate();
  10029. // E.g. when switching to an internal editor window
  10030. ResetIncrementalSearch();
  10031. }
  10032. //---------------------------------------------------------------------------
  10033. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10034. {
  10035. // When switching to another application
  10036. ResetIncrementalSearch();
  10037. }
  10038. //---------------------------------------------------------------------------
  10039. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10040. {
  10041. ResetIncrementalSearch();
  10042. }
  10043. //---------------------------------------------------------------------------
  10044. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10045. {
  10046. if (FIncrementalSearching <= 0)
  10047. {
  10048. ResetIncrementalSearch();
  10049. }
  10050. }
  10051. //---------------------------------------------------------------------------
  10052. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10053. {
  10054. CountClicksForWindowPrint(this);
  10055. }
  10056. //---------------------------------------------------------------------------
  10057. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10058. {
  10059. CenterReconnectToolbar();
  10060. }
  10061. //---------------------------------------------------------------------------
  10062. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10063. {
  10064. if (RemoteDirView->ItemFocused != NULL)
  10065. {
  10066. RemoteDirView->ItemFocused->Selected = true;
  10067. }
  10068. }
  10069. //---------------------------------------------------------------------------