CustomScpExplorer.cpp 343 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514
  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. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. class TAutoBatch
  119. {
  120. public:
  121. TAutoBatch(TCustomScpExplorerForm * Form) :
  122. FForm(Form)
  123. {
  124. FForm->BatchStart(FBatchStorage);
  125. }
  126. ~TAutoBatch()
  127. {
  128. FForm->BatchEnd(FBatchStorage);
  129. }
  130. private:
  131. TCustomScpExplorerForm * FForm;
  132. void * FBatchStorage;
  133. };
  134. //---------------------------------------------------------------------------
  135. struct TTransferOperationParam
  136. {
  137. TTransferOperationParam();
  138. UnicodeString TargetDirectory;
  139. bool Temp;
  140. bool DragDrop;
  141. int Options;
  142. TAutoSwitch Queue;
  143. };
  144. //---------------------------------------------------------------------------
  145. TTransferOperationParam::TTransferOperationParam()
  146. {
  147. Temp = false;
  148. DragDrop = false;
  149. Options = 0;
  150. Queue = asAuto;
  151. }
  152. //---------------------------------------------------------------------------
  153. class TTransferPresetNoteData : public TObject
  154. {
  155. public:
  156. UnicodeString Message;
  157. };
  158. //---------------------------------------------------------------------------
  159. class TTerminalNoteData : public TObject
  160. {
  161. public:
  162. TTerminal * Terminal;
  163. };
  164. //---------------------------------------------------------------------------
  165. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  166. FFormRestored(false),
  167. TForm(Owner)
  168. {
  169. FCurrentSide = osRemote;
  170. FEverShown = false;
  171. FDocks = new TList();
  172. InitControls();
  173. RestoreParams();
  174. ConfigurationChanged();
  175. RemoteDirView->Invalidate();
  176. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  177. NonVisualDataModule->ScpExplorer = this;
  178. FAutoOperation = false;
  179. FOnFileOperationFinished = NULL;
  180. FForceExecution = false;
  181. FIgnoreNextDialogChar = 0;
  182. FErrorList = NULL;
  183. FSynchronizeProgressForm = NULL;
  184. FProgressForm = NULL;
  185. FRefreshLocalDirectory = false;
  186. FRefreshRemoteDirectory = false;
  187. FDDExtMapFile = NULL;
  188. // CreateMutexW keeps failing with ERROR_NOACCESS
  189. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  190. DebugAssert(FDDExtMutex != NULL);
  191. FDDTargetControl = NULL;
  192. FDelayedDeletionTimer = NULL;
  193. FDelayedDeletionList = new TStringList();
  194. FDDFileList = NULL;
  195. FPendingTempSpaceWarn = false;
  196. FCapturedLog = NULL;
  197. FDragDropOperation = false;
  198. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  199. FAllowTransferPresetAutoSelect = true;
  200. FCopyParamDefault = L"";
  201. FSynchronizeController = NULL;
  202. FPendingQueueActionItem = NULL;
  203. FLockLevel = 0;
  204. FLockSuspendLevel = 0;
  205. FDisabledOnLockSuspend = false;
  206. FAlternativeDelete = false;
  207. FTrayIcon = new ::TTrayIcon(0);
  208. FTrayIcon->OnClick = TrayIconClick;
  209. FMaxQueueLength = 0;
  210. FLastContextPopupScreenPoint = TPoint(-1, -1);
  211. FTransferResumeList = NULL;
  212. FMoveToQueue = false;
  213. FStandaloneEditing = false;
  214. FOnFeedSynchronizeError = NULL;
  215. FNeedSession = false;
  216. FDoNotIdleCurrentTerminal = 0;
  217. FIncrementalSearching = 0;
  218. FIncrementalSearchHaveNext = false;
  219. FEditorManager = new TEditorManager();
  220. FEditorManager->OnFileChange = ExecutedFileChanged;
  221. FEditorManager->OnFileReload = ExecutedFileReload;
  222. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  223. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  224. FLocalEditors = new TList();
  225. FQueueStatus = NULL;
  226. FQueueStatusSection = new TCriticalSection();
  227. FQueueStatusInvalidated = false;
  228. FQueueItemInvalidated = false;
  229. FQueueActedItem = NULL;
  230. FQueueController = new TQueueController(QueueView3);
  231. FUserActionTimer = new TTimer(this);
  232. FUserActionTimer->Enabled = false;
  233. FUserActionTimer->Interval = 10;
  234. FUserActionTimer->OnTimer = UserActionTimer;
  235. FNotes = new TStringList();
  236. FNoteTimer = new TTimer(this);
  237. FNoteTimer->Enabled = false;
  238. FNoteTimer->OnTimer = NoteTimer;
  239. FOnNoteClick = NULL;
  240. FOle32Library = LoadLibrary(L"Ole32.dll");
  241. FDragMoveCursor = FOle32Library != NULL ?
  242. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  243. UseSystemSettings(this);
  244. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  245. static_cast<TObject*>(GetComponent(fcTransferList)));
  246. DebugAssert(TransferList != NULL);
  247. FTransferListHoverIndex = -1;
  248. TransferList->OnChange = TransferListChange;
  249. TransferList->OnDrawItem = TransferListDrawItem;
  250. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  251. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  252. UseDesktopFont(SessionsPageControl);
  253. UpdateSessionsPageControlHeight();
  254. UseDesktopFont(RemoteDirView);
  255. UseDesktopFont(RemoteDriveView);
  256. UseDesktopFont(QueueView3);
  257. UseDesktopFont(QueueLabel);
  258. UseDesktopFont(RemoteStatusBar);
  259. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  260. QueueSplitter->ShowHint = true;
  261. RemotePanelSplitter->ShowHint = true;
  262. UpdateImages();
  263. FCustomCommandMenu = CreateTBXPopupMenu(this);
  264. FCustomCommandLocalFileList = NULL;
  265. FCustomCommandRemoteFileList = NULL;
  266. FSessionColors = new TPngImageList(this);
  267. FSessionColors->ColorDepth = cd32Bit;
  268. AddFixedSessionImages();
  269. SessionsPageControl->Images = FSessionColors;
  270. QueueLabel->FocusControl = QueueView3;
  271. UpdateQueueLabel();
  272. FRemoteDirViewWasFocused = true;
  273. CreateHiddenWindow();
  274. StartUpdates();
  275. }
  276. //---------------------------------------------------------------------------
  277. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  278. {
  279. // this has to be one of the very first things to do
  280. StopUpdateThread();
  281. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  282. TTerminalManager::Instance()->ScpExplorer = NULL;
  283. delete FCustomCommandLocalFileList;
  284. delete FCustomCommandRemoteFileList;
  285. delete FCustomCommandMenu;
  286. delete FTrayIcon;
  287. FTrayIcon = NULL;
  288. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  289. delete FEditorManager;
  290. ForceCloseLocalEditors();
  291. delete FLocalEditors;
  292. HideFileFindDialog();
  293. if (FDelayedDeletionTimer)
  294. {
  295. DoDelayedDeletion(NULL);
  296. SAFE_DESTROY(FDelayedDeletionTimer);
  297. }
  298. SAFE_DESTROY(FDelayedDeletionList);
  299. // sometimes we do not get DDEnd so the list is not released
  300. SAFE_DESTROY(FDDFileList);
  301. DebugAssert(FSynchronizeController == NULL);
  302. CloseHandle(FDDExtMutex);
  303. FDDExtMutex = NULL;
  304. FreeLibrary(FOle32Library);
  305. FOle32Library = NULL;
  306. FDragMoveCursor = NULL;
  307. DebugAssert(!FErrorList);
  308. if (FEverShown)
  309. {
  310. // when window is never shown (like when running command-line operation),
  311. // particularly window site is not restored correctly (BoundsRect value set
  312. // in RestoreForm gets lost during handle allocation), so we do not want
  313. // it to be stored
  314. StoreParams();
  315. }
  316. Terminal = NULL;
  317. Queue = NULL;
  318. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  319. NonVisualDataModule->ScpExplorer = NULL;
  320. delete FQueueController;
  321. FQueueController = NULL;
  322. delete FQueueStatusSection;
  323. FQueueStatusSection = NULL;
  324. delete FQueueStatus;
  325. FQueueStatus = NULL;
  326. delete FUserActionTimer;
  327. FUserActionTimer = NULL;
  328. delete FNoteTimer;
  329. delete FNotes;
  330. delete FNoteData;
  331. SAFE_DESTROY(FDocks);
  332. SessionsPageControl->Images = NULL;
  333. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  334. SAFE_DESTROY(FSessionColors);
  335. SAFE_DESTROY(FSessionsDragDropFilesEx);
  336. SAFE_DESTROY(FHistoryMenu[0][0]);
  337. SAFE_DESTROY(FHistoryMenu[0][1]);
  338. SAFE_DESTROY(FHistoryMenu[1][0]);
  339. SAFE_DESTROY(FHistoryMenu[1][1]);
  340. if (FHiddenWindow != NULL)
  341. {
  342. DestroyWindow(FHiddenWindow);
  343. FHiddenWindow = NULL;
  344. }
  345. }
  346. //---------------------------------------------------------------------------
  347. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  348. {
  349. std::unique_ptr<TSessionData> Data;
  350. if (!Session.IsEmpty())
  351. {
  352. bool DefaultsOnly;
  353. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  354. }
  355. TTerminalManager * Manager = TTerminalManager::Instance();
  356. for (int Index = 0; Index < Manager->Count; Index++)
  357. {
  358. TTerminal * Terminal = Manager->Terminals[Index];
  359. if (Session.IsEmpty() ||
  360. Terminal->SessionData->IsSameSite(Data.get()))
  361. {
  362. if (Path.IsEmpty())
  363. {
  364. Terminal->ClearCaches();
  365. }
  366. else
  367. {
  368. Terminal->DirectoryModified(Path, true);
  369. }
  370. }
  371. }
  372. // We should flag a pending refresh for the background terminals or busy foreground terminals
  373. if ((Terminal != NULL) && Terminal->Active &&
  374. CanCommandLineFromAnotherInstance() &&
  375. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  376. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  377. {
  378. Terminal->ReloadDirectory();
  379. }
  380. }
  381. //---------------------------------------------------------------------------
  382. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  383. {
  384. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  385. size_t MessageSize = sizeof(TCopyDataMessage);
  386. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  387. if (Result)
  388. {
  389. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  390. Result = (Message.Version == TCopyDataMessage::Version1);
  391. if (Result)
  392. {
  393. switch (Message.Command)
  394. {
  395. case TCopyDataMessage::CommandCanCommandLine:
  396. Result = CanCommandLineFromAnotherInstance();
  397. break;
  398. case TCopyDataMessage::CommandCommandLine:
  399. {
  400. UnicodeString CommandLine(Message.CommandLine);
  401. Result = CommandLineFromAnotherInstance(CommandLine);
  402. }
  403. break;
  404. case TCopyDataMessage::RefreshPanel:
  405. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  406. break;
  407. case TCopyDataMessage::MainWindowCheck:
  408. Result = true;
  409. break;
  410. default:
  411. Result = false;
  412. break;
  413. }
  414. }
  415. }
  416. Message.Result = Result ? 1 : 0;
  417. }
  418. //---------------------------------------------------------------------------
  419. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  420. {
  421. WNDCLASS WindowClass = {0};
  422. WindowClass.lpfnWndProc = DefWindowProc;
  423. WindowClass.hInstance = HInstance;
  424. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  425. FHiddenWindow = NULL;
  426. if (RegisterClass(&WindowClass))
  427. {
  428. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  429. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  430. }
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanConsole()
  434. {
  435. return
  436. (Terminal != NULL) &&
  437. Terminal->Active &&
  438. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  439. }
  440. //---------------------------------------------------------------------------
  441. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  442. {
  443. bool Result = Visible && !NonVisualDataModule->Busy;
  444. return Result;
  445. }
  446. //---------------------------------------------------------------------------
  447. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  448. const UnicodeString & CommandLine)
  449. {
  450. TProgramParams Params(CommandLine);
  451. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  452. if (Result)
  453. {
  454. NonVisualDataModule->StartBusy();
  455. try
  456. {
  457. // this action is initiated from another process,
  458. // so it's likely that our window is not visible,
  459. // and user won't see what is going on
  460. Application->BringToFront();
  461. // reload sessions as we may be asked to open a session
  462. // just stored by another instance
  463. StoredSessions->Reload();
  464. UnicodeString SessionName = Params.Param[1];
  465. std::unique_ptr<TObjectList> DataList(new TObjectList());
  466. UnicodeString DownloadFile; // unused
  467. try
  468. {
  469. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  470. }
  471. catch (EAbort &)
  472. {
  473. // Happens when opening session in PuTTY and in other situations,
  474. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  475. // to process the command-line on its own (or to pass it to yet another instance)
  476. }
  477. if (DataList->Count > 0)
  478. {
  479. TTerminalManager * Manager = TTerminalManager::Instance();
  480. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  481. }
  482. }
  483. __finally
  484. {
  485. NonVisualDataModule->EndBusy();
  486. }
  487. }
  488. return Result;
  489. }
  490. //---------------------------------------------------------------------------
  491. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  492. {
  493. if (FTerminal != value)
  494. {
  495. TerminalChanging();
  496. DoSetTerminal(value, false);
  497. }
  498. }
  499. //---------------------------------------------------------------------------
  500. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TTerminal * value, bool Replace)
  501. {
  502. FTerminal = value;
  503. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  504. FAllowTransferPresetAutoSelect = false;
  505. try
  506. {
  507. TerminalChanged(Replace);
  508. }
  509. __finally
  510. {
  511. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  512. }
  513. if (Terminal != NULL)
  514. {
  515. TransferPresetAutoSelect();
  516. }
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TTerminal * value)
  520. {
  521. DoSetTerminal(value, true);
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  525. {
  526. if (FTerminal != NULL)
  527. {
  528. UpdateTerminal(Terminal);
  529. }
  530. }
  531. //---------------------------------------------------------------------------
  532. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  533. {
  534. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  535. if (ManagedTerminal != NULL)
  536. {
  537. UpdateSessionColor((TColor)ManagedTerminal->StateData->Color);
  538. }
  539. DoTerminalListChanged();
  540. if (Replaced)
  541. {
  542. RemoteDirView->ReplaceTerminal(Terminal);
  543. }
  544. else
  545. {
  546. RemoteDirView->Terminal = Terminal;
  547. }
  548. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  549. if (Terminal != NULL)
  550. {
  551. if (Terminal->Active)
  552. {
  553. Terminal->RefreshDirectory();
  554. }
  555. DebugAssert(ManagedTerminal == Terminal);
  556. if (WinConfiguration->PreservePanelState)
  557. {
  558. if (ManagedTerminal->RemoteExplorerState != NULL)
  559. {
  560. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  561. }
  562. else
  563. {
  564. RemoteDirView->ClearState();
  565. }
  566. }
  567. if (!Replaced)
  568. {
  569. InitStatusBar();
  570. }
  571. }
  572. UpdateTransferList();
  573. // Update panels Enable state before refreshing the labels
  574. UpdateControls();
  575. }
  576. //---------------------------------------------------------------------------
  577. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  578. {
  579. if (Queue != value)
  580. {
  581. if (FQueue != NULL)
  582. {
  583. FQueue->OnListUpdate = NULL;
  584. FQueue->OnQueueItemUpdate = NULL;
  585. }
  586. FQueue = value;
  587. if (FQueue != NULL)
  588. {
  589. DebugAssert(FQueue->OnListUpdate == NULL);
  590. FQueue->OnListUpdate = QueueListUpdate;
  591. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  592. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  593. }
  594. QueueChanged();
  595. }
  596. }
  597. //---------------------------------------------------------------------------
  598. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  599. TListItem * Item)
  600. {
  601. if (FQueueActedItem == Item)
  602. {
  603. FQueueActedItem = NULL;
  604. if ((QueueView3->PopupMenu != NULL) &&
  605. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  606. {
  607. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  608. ReleaseCapture();
  609. }
  610. }
  611. if (Item->Data == FPendingQueueActionItem)
  612. {
  613. FPendingQueueActionItem = NULL;
  614. }
  615. }
  616. //---------------------------------------------------------------------------
  617. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  618. {
  619. {
  620. TGuard Guard(FQueueStatusSection);
  621. FQueueStatusInvalidated = false;
  622. if (FQueue != NULL)
  623. {
  624. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  625. }
  626. }
  627. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  628. {
  629. FMaxQueueLength = FQueueStatus->Count;
  630. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  631. }
  632. FQueueController->UpdateQueueStatus(FQueueStatus);
  633. SetQueueProgress();
  634. UpdateQueueView();
  635. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  636. if (IsEmpty && (Terminal != NULL))
  637. {
  638. TOnceDoneOperation OnceDoneOperation =
  639. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  640. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  641. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  642. {
  643. FQueue->Enabled = false;
  644. }
  645. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  646. {
  647. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  648. }
  649. }
  650. }
  651. //---------------------------------------------------------------------------
  652. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  653. {
  654. UnicodeString Result;
  655. if (FQueueStatus != NULL)
  656. {
  657. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  658. {
  659. TFileOperationProgressType * ProgressData =
  660. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  661. if ((ProgressData != NULL) && ProgressData->InProgress)
  662. {
  663. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  664. }
  665. }
  666. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  667. {
  668. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  669. }
  670. }
  671. return Result;
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  675. {
  676. ComponentVisible[fcQueueView] =
  677. (WinConfiguration->QueueView.Show == qvShow) ||
  678. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  679. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  680. }
  681. //---------------------------------------------------------------------------
  682. void __fastcall TCustomScpExplorerForm::QueueChanged()
  683. {
  684. if (FQueueStatus != NULL)
  685. {
  686. delete FQueueStatus;
  687. FQueueStatus = NULL;
  688. }
  689. UpdateQueueStatus(true);
  690. }
  691. //---------------------------------------------------------------------------
  692. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  693. {
  694. if (FQueue == Queue)
  695. {
  696. FQueueStatusInvalidated = true;
  697. }
  698. }
  699. //---------------------------------------------------------------------------
  700. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  701. TQueueItem * Item)
  702. {
  703. if (FQueue == Queue)
  704. {
  705. TGuard Guard(FQueueStatusSection);
  706. // this may be running in parallel with QueueChanged
  707. if (FQueueStatus != NULL)
  708. {
  709. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  710. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  711. {
  712. FRefreshLocalDirectory = (QueueItem == NULL) ||
  713. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  714. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  715. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  716. }
  717. if (QueueItem != NULL)
  718. {
  719. QueueItem->UserData = (void*)true;
  720. FQueueItemInvalidated = true;
  721. }
  722. }
  723. }
  724. }
  725. //---------------------------------------------------------------------------
  726. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  727. {
  728. // during standalone editing, we have no way to see/control queue,
  729. // so we have to always popup prompts automatically
  730. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  734. {
  735. if (FQueueStatus != NULL)
  736. {
  737. bool QueueAutoPopup = IsQueueAutoPopup();
  738. bool NeedRefresh = FQueueController->NeedRefresh();
  739. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  740. FQueueItemInvalidated = false;
  741. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  742. bool Updated = false;
  743. TQueueItemProxy * QueueItem;
  744. bool WasUserAction;
  745. for (int Index = 0; Index < Limit; Index++)
  746. {
  747. bool Update = false;
  748. QueueItem = FQueueStatus->Items[Index];
  749. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  750. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  751. (FPendingQueueActionItem == NULL))
  752. {
  753. FPendingQueueActionItem = QueueItem;
  754. FUserActionTimer->Enabled = true;
  755. }
  756. if ((bool)QueueItem->UserData)
  757. {
  758. QueueItem->UserData = (void*)false;
  759. QueueItem->Update();
  760. Updated = true;
  761. Update = true;
  762. }
  763. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  764. {
  765. Update = true;
  766. }
  767. if (Update)
  768. {
  769. FQueueController->RefreshQueueItem(QueueItem);
  770. }
  771. }
  772. if (Updated)
  773. {
  774. NonVisualDataModule->UpdateNonVisibleActions();
  775. SetQueueProgress();
  776. }
  777. }
  778. }
  779. //---------------------------------------------------------------------------
  780. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  781. {
  782. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  783. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  784. }
  785. //---------------------------------------------------------------------------
  786. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  787. {
  788. if (ProgressData->Operation != foCalculateSize)
  789. {
  790. int OverallProgress;
  791. // FProgressForm is null when this is called from SetQueueProgress
  792. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  793. {
  794. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  795. }
  796. else
  797. {
  798. OverallProgress = ProgressData->OverallProgress();
  799. }
  800. // implies TBPF_NORMAL
  801. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  802. }
  803. else
  804. {
  805. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  806. }
  807. }
  808. //---------------------------------------------------------------------------
  809. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  810. {
  811. TTerminalManager::Instance()->QueueStatusUpdated();
  812. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  813. {
  814. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  815. {
  816. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  817. {
  818. TFileOperationProgressType * ProgressData = NULL;
  819. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  820. {
  821. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  822. }
  823. if ((ProgressData != NULL) &&
  824. ProgressData->InProgress)
  825. {
  826. SetTaskbarListProgressValue(ProgressData);
  827. }
  828. else
  829. {
  830. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  831. }
  832. }
  833. else
  834. {
  835. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  836. }
  837. }
  838. else
  839. {
  840. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  841. }
  842. }
  843. UpdateQueueLabel();
  844. }
  845. //---------------------------------------------------------------------------
  846. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  847. {
  848. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  849. if (FQueueStatus != NULL)
  850. {
  851. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  852. if (ActiveAndPendingPrimaryCount > 0)
  853. {
  854. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  855. }
  856. }
  857. QueueLabel->Caption = Caption;
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  861. {
  862. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  863. static_cast<TComponent*>(GetComponent(fcTransferList)));
  864. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  865. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  866. TransferList->Strings->BeginUpdate();
  867. try
  868. {
  869. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  870. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  871. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  872. if (FTransferDropDownHint.IsEmpty())
  873. {
  874. FTransferDropDownHint = TransferDropDown->Hint;
  875. }
  876. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  877. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  878. TransferDropDown->Text = StripHotkey(Name);
  879. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  880. (FTransferDropDownHint, StripHotkey(Name),
  881. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  882. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  883. // update the label, otherwise when it is updated only on the first draw
  884. // of the list, it is drawn "bold" for some reason
  885. FTransferListHoverIndex = TransferList->ItemIndex;
  886. UpdateTransferLabel();
  887. }
  888. __finally
  889. {
  890. TransferList->Strings->EndUpdate();
  891. }
  892. }
  893. //---------------------------------------------------------------------------
  894. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  895. {
  896. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  897. DebugAssert(Toolbar != NULL);
  898. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  899. }
  900. //---------------------------------------------------------------------------
  901. void __fastcall TCustomScpExplorerForm::UpdateActions()
  902. {
  903. TForm::UpdateActions();
  904. if (ComponentVisible[fcCustomCommandsBand])
  905. {
  906. UpdateCustomCommandsToolbar();
  907. }
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  911. {
  912. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  913. }
  914. //---------------------------------------------------------------------------
  915. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  916. {
  917. Color = GetBtnFaceColor();
  918. DebugAssert(Configuration && RemoteDirView);
  919. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  920. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  921. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  922. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  923. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  924. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  925. {
  926. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  927. // selection is not redrawn automatically when RowSelect changes
  928. RemoteDirView->Invalidate();
  929. }
  930. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  931. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  932. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  933. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  934. UpdateSessionsPageControlHeight();
  935. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  936. UpdateToolbarDisplayMode();
  937. UpdateTransferList();
  938. if (Terminal != NULL)
  939. {
  940. TransferPresetAutoSelect();
  941. }
  942. UpdateQueueView();
  943. UpdateControls();
  944. // this can be called even before constuctor finishes.
  945. if (FEditorManager != NULL)
  946. {
  947. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  948. }
  949. // this can be called even before constuctor finishes.
  950. if (FLocalEditors != NULL)
  951. {
  952. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  953. {
  954. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  955. }
  956. }
  957. if (ComponentVisible[fcCustomCommandsBand])
  958. {
  959. UpdateCustomCommandsToolbar();
  960. }
  961. // show only when keeping queue items forever,
  962. // otherwise, its enough to have in in the context menu
  963. QueueDeleteAllDoneQueueToolbarItem->Visible =
  964. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  965. if (FFileColorsCurrent != WinConfiguration->FileColors)
  966. {
  967. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  968. FFileColorsCurrent = WinConfiguration->FileColors;
  969. FileColorsChanged();
  970. }
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  974. {
  975. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  976. DirView->Invalidate();
  977. }
  978. //---------------------------------------------------------------------------
  979. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  980. {
  981. DoFileColorsChanged(RemoteDirView);
  982. }
  983. //---------------------------------------------------------------------------
  984. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  985. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  986. void * /*Arg*/)
  987. {
  988. if (Token != NULL)
  989. {
  990. TForm * Editor = dynamic_cast<TForm*>(Token);
  991. DebugAssert(Editor != NULL);
  992. ReconfigureEditorForm(Editor);
  993. }
  994. }
  995. //---------------------------------------------------------------------------
  996. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  997. {
  998. WinConfiguration->DDTransferConfirmation = asOn;
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1002. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1003. {
  1004. // noop
  1005. }
  1006. //---------------------------------------------------------------------------
  1007. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1008. TTransferDirection Direction, TTransferType Type, bool Temp,
  1009. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1010. bool Confirm, bool DragDrop, int Options)
  1011. {
  1012. bool Result = true;
  1013. DebugAssert(Terminal && Terminal->Active);
  1014. // these parameters are known in advance
  1015. int Params =
  1016. FLAGMASK(Type == ttMove, cpDelete);
  1017. bool ToTemp = (Temp && (Direction == tdToLocal));
  1018. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1019. {
  1020. bool DisableNewerOnly =
  1021. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1022. ToTemp;
  1023. Options |=
  1024. FLAGMASK(ToTemp, coTemp) |
  1025. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1026. coDoNotShowAgain;
  1027. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1028. int CopyParamAttrs = (Direction == tdToRemote ?
  1029. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1030. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1031. int OutputOptions =
  1032. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1033. cooDoNotShowAgain);
  1034. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1035. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1036. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1037. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1038. if (Result)
  1039. {
  1040. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1041. {
  1042. if (DragDrop)
  1043. {
  1044. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1045. {
  1046. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1047. NULL, 0, EnableDDTransferConfirmation, NULL);
  1048. }
  1049. WinConfiguration->DDTransferConfirmation = asOff;
  1050. }
  1051. else
  1052. {
  1053. WinConfiguration->ConfirmTransferring = false;
  1054. }
  1055. }
  1056. else
  1057. {
  1058. // User explicitly unchecked "do not show again",
  1059. // so show him the dialog the next time
  1060. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1061. {
  1062. WinConfiguration->DDTransferConfirmation = asOn;
  1063. }
  1064. }
  1065. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1066. }
  1067. }
  1068. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1069. {
  1070. Configuration->Usage->Inc(L"TransfersOnBackground");
  1071. // these parameter are known only after transfer dialog
  1072. Params |=
  1073. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1074. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1075. Result = false;
  1076. ClearTransferSourceSelection(Direction);
  1077. }
  1078. return Result;
  1079. }
  1080. //---------------------------------------------------------------------------
  1081. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1082. {
  1083. if (FOnFileOperationFinished != NULL)
  1084. {
  1085. FOnFileOperationFinished(UnicodeString(), true);
  1086. }
  1087. else
  1088. {
  1089. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1090. if (HasDirView[Side])
  1091. {
  1092. DirView(Side)->SelectAll(smNone);
  1093. }
  1094. }
  1095. }
  1096. //---------------------------------------------------------------------------
  1097. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1098. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1099. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1100. int Params)
  1101. {
  1102. DebugAssert(Queue != NULL);
  1103. bool SingleFile = false;
  1104. if (FileList->Count == 1)
  1105. {
  1106. if (Direction == tdToRemote)
  1107. {
  1108. UnicodeString FileName = FileList->Strings[0];
  1109. SingleFile = FileExists(ApiPath(FileName));
  1110. }
  1111. else
  1112. {
  1113. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1114. SingleFile = !File->IsDirectory;
  1115. }
  1116. }
  1117. TQueueItem * QueueItem;
  1118. if (Direction == tdToRemote)
  1119. {
  1120. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1121. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1122. }
  1123. else
  1124. {
  1125. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1126. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1127. }
  1128. AddQueueItem(Queue, QueueItem, Terminal);
  1129. }
  1130. //---------------------------------------------------------------------------
  1131. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1132. {
  1133. if (Queue->IsEmpty)
  1134. {
  1135. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1136. ManagedTerminal->QueueOperationStart = Now();
  1137. }
  1138. Queue->AddItem(QueueItem);
  1139. }
  1140. //---------------------------------------------------------------------------
  1141. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1142. {
  1143. }
  1144. //---------------------------------------------------------------------------
  1145. void __fastcall TCustomScpExplorerForm::InitControls()
  1146. {
  1147. DebugAssert(FDocks != NULL);
  1148. for (int Index = 0; Index < ComponentCount; Index++)
  1149. {
  1150. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1151. if ((Dock != NULL) && (Dock->Tag == 0))
  1152. {
  1153. FDocks->Add(Dock);
  1154. }
  1155. }
  1156. CollectItemsWithTextDisplayMode(this);
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1160. {
  1161. ConfigurationChanged();
  1162. QueuePanel->Height =
  1163. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1164. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1165. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1166. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. void __fastcall TCustomScpExplorerForm::StoreParams()
  1170. {
  1171. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1172. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1173. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1174. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1175. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1176. }
  1177. //---------------------------------------------------------------------------
  1178. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1179. {
  1180. if (!FFormRestored)
  1181. {
  1182. FFormRestored = true;
  1183. RestoreFormParams();
  1184. }
  1185. TForm::CreateParams(Params);
  1186. }
  1187. //---------------------------------------------------------------------------
  1188. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1189. {
  1190. DebugAssert(FDocks != NULL);
  1191. for (int Index = 0; Index < FDocks->Count; Index++)
  1192. {
  1193. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1194. }
  1195. }
  1196. //---------------------------------------------------------------------------
  1197. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1198. {
  1199. SetDockAllowDrag(true);
  1200. while (!ButtonsStr.IsEmpty())
  1201. {
  1202. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1203. UnicodeString Name = CutToChar(S, L'=', true);
  1204. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1205. {
  1206. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1207. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1208. {
  1209. while (!S.IsEmpty())
  1210. {
  1211. UnicodeString S2 = CutToChar(S, L',', true);
  1212. UnicodeString Name = CutToChar(S2, L':', true);
  1213. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1214. {
  1215. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1216. if (SameText(GetToolbarItemName(Item), Name))
  1217. {
  1218. // forward compatibility
  1219. UnicodeString S3 = CutToChar(S2, L':', true);
  1220. bool Visible = StrToIntDef(S3, 0);
  1221. Item->Visible = Visible;
  1222. }
  1223. }
  1224. }
  1225. }
  1226. }
  1227. }
  1228. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1229. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1230. }
  1231. //---------------------------------------------------------------------------
  1232. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1233. {
  1234. UnicodeString Result;
  1235. SetDockAllowDrag(true);
  1236. Result = ::GetToolbarsLayoutStr(this);
  1237. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1238. return Result;
  1239. }
  1240. //---------------------------------------------------------------------------
  1241. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1242. {
  1243. UnicodeString Result;
  1244. if (Item->Action != NULL)
  1245. {
  1246. Result = Item->Action->Name;
  1247. Result = RemoveSuffix(Result, L"Action");
  1248. }
  1249. else
  1250. {
  1251. Result = Item->Name;
  1252. Result = RemoveSuffix(Result, L"SubmenuItem");
  1253. Result = RemoveSuffix(Result, L"Item");
  1254. }
  1255. return Result;
  1256. }
  1257. //---------------------------------------------------------------------------
  1258. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1259. {
  1260. UnicodeString Result;
  1261. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1262. {
  1263. UnicodeString ToolbarStr;
  1264. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1265. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1266. {
  1267. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1268. {
  1269. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1270. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1271. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1272. // and save the changes here only.
  1273. if (!Item->Visible)
  1274. {
  1275. UnicodeString Name = GetToolbarItemName(Item);
  1276. DebugAssert(Name.Pos(L"TBX") == 0);
  1277. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1278. }
  1279. }
  1280. }
  1281. if (!ToolbarStr.IsEmpty())
  1282. {
  1283. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1284. }
  1285. }
  1286. return Result;
  1287. }
  1288. //---------------------------------------------------------------------------
  1289. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1290. {
  1291. DebugAssert(FProgressForm == NULL);
  1292. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1293. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1294. FProgressForm->DeleteLocalToRecycleBin =
  1295. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1296. FProgressForm->DeleteRemoteToRecycleBin =
  1297. (Terminal != NULL) &&
  1298. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1299. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1300. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1301. LockWindow();
  1302. if (FTaskbarList != NULL)
  1303. {
  1304. // Actually, do not know what hides the progress once the operation finishes
  1305. SetTaskbarListProgressState(TBPF_NORMAL);
  1306. }
  1307. }
  1308. //---------------------------------------------------------------------------
  1309. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1310. {
  1311. UnlockWindow();
  1312. SAFE_DESTROY(FProgressForm);
  1313. SetQueueProgress();
  1314. }
  1315. //---------------------------------------------------------------------------
  1316. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1317. TFileOperationProgressType & ProgressData)
  1318. {
  1319. // Download to temporary local directory
  1320. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1321. {
  1322. bool Continue = true;
  1323. FPendingTempSpaceWarn = false;
  1324. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1325. if (!Continue)
  1326. {
  1327. Abort();
  1328. }
  1329. }
  1330. // operation is being executed and we still didn't show up progress form
  1331. if (ProgressData.InProgress && !FProgressForm)
  1332. {
  1333. CreateProgressForm(NULL);
  1334. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1335. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1336. }
  1337. // operation is finished (or terminated), so we hide progress form
  1338. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1339. {
  1340. DestroyProgressForm();
  1341. if (ProgressData.Operation == foCalculateSize)
  1342. {
  1343. // When calculating size before transfer, the abort caused by
  1344. // cancel flag set due to "MoveToQueue" below,
  1345. // is not propagated back to us in ExecuteFileOperation,
  1346. // as it is silently swallowed.
  1347. // So we explicitly (re)throw it here.
  1348. if (FMoveToQueue)
  1349. {
  1350. Abort();
  1351. }
  1352. }
  1353. else
  1354. {
  1355. if ((ProgressData.Cancel == csContinue) &&
  1356. !FAutoOperation)
  1357. {
  1358. OperationComplete(ProgressData.StartTime);
  1359. }
  1360. }
  1361. }
  1362. if (FProgressForm &&
  1363. // Can happen only during synchronization, when ending one of the operations
  1364. ProgressData.InProgress)
  1365. {
  1366. FProgressForm->SetProgressData(ProgressData);
  1367. if (FTaskbarList != NULL)
  1368. {
  1369. DebugAssert(ProgressData.InProgress);
  1370. SetTaskbarListProgressValue(&ProgressData);
  1371. }
  1372. if (FProgressForm->Cancel > csContinue)
  1373. {
  1374. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1375. if (FProgressForm->Cancel == csCancelFile)
  1376. {
  1377. FProgressForm->ClearCancel();
  1378. }
  1379. // cancel cancels even the move
  1380. FMoveToQueue = false;
  1381. }
  1382. else if (FProgressForm->MoveToQueue)
  1383. {
  1384. FMoveToQueue = true;
  1385. ProgressData.SetCancelAtLeast(csCancel);
  1386. }
  1387. if ((FTransferResumeList != NULL) &&
  1388. ProgressData.InProgress &&
  1389. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1390. !ProgressData.FullFileName.IsEmpty())
  1391. {
  1392. if ((FTransferResumeList->Count == 0) ||
  1393. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1394. {
  1395. // note that we do not recognize directories from files here
  1396. FTransferResumeList->Add(ProgressData.FullFileName);
  1397. }
  1398. }
  1399. }
  1400. }
  1401. //---------------------------------------------------------------------------
  1402. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1403. const TDateTime & StartTime)
  1404. {
  1405. if (GUIConfiguration->BeepOnFinish &&
  1406. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1407. {
  1408. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1409. DWORD Sound;
  1410. if (!ExtractFileExt(BeepSound).IsEmpty())
  1411. {
  1412. Sound = SND_FILENAME;
  1413. }
  1414. else
  1415. {
  1416. Sound = SND_ALIAS;
  1417. }
  1418. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1419. }
  1420. }
  1421. //---------------------------------------------------------------------------
  1422. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1423. TFileOperationProgressType & ProgressData)
  1424. {
  1425. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1426. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1427. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1428. FileOperationProgress(ProgressData);
  1429. }
  1430. //---------------------------------------------------------------------------
  1431. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1432. {
  1433. UnicodeString Result;
  1434. if (FProgressForm != NULL)
  1435. {
  1436. Result = FProgressForm->ProgressStr();
  1437. }
  1438. return Result;
  1439. }
  1440. //---------------------------------------------------------------------------
  1441. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1442. bool DragDrop)
  1443. {
  1444. return (!DragDrop && (DropSourceControl == NULL)) ||
  1445. (DropSourceControl == RemoteDirView);
  1446. }
  1447. //---------------------------------------------------------------------------
  1448. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1449. TFileOperation Operation, TOperationSide Side,
  1450. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1451. TOnceDoneOperation & OnceDoneOperation)
  1452. {
  1453. if (!FAutoOperation)
  1454. {
  1455. // no selection on "/upload", form servers only as event handler
  1456. // (it is not displayed)
  1457. if (PanelOperation(Side, FDragDropOperation) &&
  1458. Visible && (Operation != foCalculateSize) &&
  1459. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1460. {
  1461. if (FOnFileOperationFinished != NULL)
  1462. {
  1463. FOnFileOperationFinished(FileName, Success);
  1464. }
  1465. else
  1466. {
  1467. TCustomDirView * DView = DirView(Side);
  1468. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1469. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1470. // this can happen when local drive is unplugged in the middle of the operation
  1471. if (Item != NULL)
  1472. {
  1473. if (Success) Item->Selected = false;
  1474. DView->MakeProgressVisible(Item);
  1475. }
  1476. }
  1477. }
  1478. if ((Operation == foCopy) || (Operation == foMove))
  1479. {
  1480. if (Side == osLocal)
  1481. {
  1482. Configuration->Usage->Inc(L"UploadedFiles");
  1483. }
  1484. else
  1485. {
  1486. Configuration->Usage->Inc(L"DownloadedFiles");
  1487. }
  1488. }
  1489. }
  1490. if (Success && (FSynchronizeController != NULL))
  1491. {
  1492. if (Operation == foCopy)
  1493. {
  1494. DebugAssert(Side == osLocal);
  1495. FSynchronizeController->LogOperation(soUpload, FileName);
  1496. }
  1497. else if (Operation == foDelete)
  1498. {
  1499. DebugAssert(Side == osRemote);
  1500. FSynchronizeController->LogOperation(soDelete, FileName);
  1501. }
  1502. }
  1503. if (FProgressForm)
  1504. {
  1505. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1506. }
  1507. }
  1508. //---------------------------------------------------------------------------
  1509. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1510. TFileOperation Operation, TOperationSide Side,
  1511. bool Temp, const UnicodeString & FileName, Boolean Success,
  1512. TOnceDoneOperation & OnceDoneOperation)
  1513. {
  1514. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1515. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1516. OnceDoneOperation);
  1517. }
  1518. //---------------------------------------------------------------------------
  1519. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1520. {
  1521. DebugAssert(GetSide(Side) == osRemote);
  1522. DebugUsedParam(Side);
  1523. return RemoteDirView;
  1524. }
  1525. //---------------------------------------------------------------------------
  1526. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1527. {
  1528. DebugAssert(GetSide(Side) == osRemote);
  1529. DebugUsedParam(Side);
  1530. return (Terminal != NULL);
  1531. }
  1532. //---------------------------------------------------------------------------
  1533. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1534. TOperationSide Side, int FilesOnly)
  1535. {
  1536. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1537. }
  1538. //---------------------------------------------------------------------------
  1539. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1540. TOperationSide Side, int FilesOnly)
  1541. {
  1542. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1543. }
  1544. //---------------------------------------------------------------------------
  1545. struct THistoryItemData
  1546. {
  1547. short int Side;
  1548. short int Index;
  1549. };
  1550. //---------------------------------------------------------------------------
  1551. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1552. {
  1553. DirView(Side)->HistoryGo(Index);
  1554. }
  1555. //---------------------------------------------------------------------------
  1556. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1557. {
  1558. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1559. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1560. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1561. }
  1562. //---------------------------------------------------------------------------
  1563. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1564. bool Back)
  1565. {
  1566. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1567. {
  1568. TCustomDirView * DView = DirView(Side);
  1569. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1570. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1571. if (ICount > 10)
  1572. {
  1573. ICount = 10;
  1574. }
  1575. Menu->Items->Clear();
  1576. THistoryItemData Data;
  1577. Data.Side = Side;
  1578. for (short int i = 1; i <= ICount; i++)
  1579. {
  1580. TTBCustomItem * Item = new TTBXItem(Menu);
  1581. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1582. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1583. Item->Hint = DView->HistoryPath[Data.Index];
  1584. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1585. Item->Tag = *reinterpret_cast<int*>(&Data);
  1586. Item->OnClick = HistoryItemClick;
  1587. Menu->Items->Add(Item);
  1588. }
  1589. }
  1590. }
  1591. //---------------------------------------------------------------------------
  1592. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1593. TOperationSide Side, bool Back)
  1594. {
  1595. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1596. {
  1597. // workaround
  1598. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1599. // operator new allocates memory in Pascal code, but calls inline
  1600. // contructor in C++, leading in problems, the function does
  1601. // both in Pascal code
  1602. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1603. UpdateHistoryMenu(Side, Back);
  1604. }
  1605. return FHistoryMenu[Side == osLocal][Back];
  1606. }
  1607. //---------------------------------------------------------------------------
  1608. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1609. TCustomDirView *Sender)
  1610. {
  1611. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1612. UpdateHistoryMenu(Side, true);
  1613. UpdateHistoryMenu(Side, false);
  1614. }
  1615. //---------------------------------------------------------------------------
  1616. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1617. {
  1618. // remote custom commands can be executed only if the server supports shell commands
  1619. // or have secondary shell
  1620. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1621. }
  1622. //---------------------------------------------------------------------------
  1623. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1624. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1625. {
  1626. int Result;
  1627. TFileCustomCommand RemoteCustomCommand;
  1628. TLocalCustomCommand LocalCustomCommand;
  1629. TFileCustomCommand * NonInteractiveCustomCommand =
  1630. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1631. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1632. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1633. if (FLAGCLEAR(Command.Params, ccLocal))
  1634. {
  1635. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1636. // custom command that does not operate with files can be executed anytime ...
  1637. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1638. {
  1639. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1640. {
  1641. // ... but do not show such command in remote file menu (TODO)
  1642. Result = AllowedState;
  1643. }
  1644. else
  1645. {
  1646. Result = -1;
  1647. }
  1648. }
  1649. else
  1650. {
  1651. if ((ListType == ccltAll) || (ListType == ccltFile))
  1652. {
  1653. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1654. }
  1655. else
  1656. {
  1657. Result = -1;
  1658. }
  1659. }
  1660. }
  1661. else
  1662. {
  1663. // custom command that does not operate with files can be executed anytime
  1664. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1665. {
  1666. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1667. }
  1668. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1669. {
  1670. if ((ListType == ccltAll) || (ListType == ccltFile))
  1671. {
  1672. // special case is "diff"-style command that can be executed over any side,
  1673. // if we have both sides
  1674. Result =
  1675. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1676. // directly (instead of EnableSelectedOperation) to pass
  1677. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1678. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1679. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1680. }
  1681. else if (ListType == ccltBoth)
  1682. {
  1683. Result = 1;
  1684. }
  1685. else
  1686. {
  1687. Result = -1;
  1688. }
  1689. }
  1690. else
  1691. {
  1692. if ((ListType == ccltAll) || (ListType == ccltFile))
  1693. {
  1694. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1695. }
  1696. else
  1697. {
  1698. Result = -1;
  1699. }
  1700. }
  1701. }
  1702. return Result;
  1703. }
  1704. //---------------------------------------------------------------------------
  1705. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1706. TStrings * FileList, const TCustomCommandType & ACommand,
  1707. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1708. {
  1709. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1710. {
  1711. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1712. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1713. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1714. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1715. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1716. bool Capture =
  1717. FLAGSET(ACommand.Params, ccShowResults) ||
  1718. FLAGSET(ACommand.Params, ccCopyResults) ||
  1719. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1720. TCaptureOutputEvent OutputEvent = NULL;
  1721. DebugAssert(FCapturedLog == NULL);
  1722. if (Capture)
  1723. {
  1724. FCapturedLog = new TStringList();
  1725. OutputEvent = TerminalCaptureLog;
  1726. }
  1727. try
  1728. {
  1729. if (!RemoteCustomCommand.IsFileCommand(Command))
  1730. {
  1731. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1732. OutputEvent);
  1733. }
  1734. else
  1735. {
  1736. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1737. }
  1738. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1739. {
  1740. if (FLAGSET(ACommand.Params, ccCopyResults))
  1741. {
  1742. CopyToClipboard(FCapturedLog);
  1743. }
  1744. if (FLAGSET(ACommand.Params, ccShowResults))
  1745. {
  1746. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1747. }
  1748. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1749. {
  1750. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1751. }
  1752. }
  1753. }
  1754. __finally
  1755. {
  1756. SAFE_DESTROY(FCapturedLog);
  1757. }
  1758. }
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1762. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1763. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1764. {
  1765. TStrings * LocalFileList = NULL;
  1766. TStrings * RemoteFileList = NULL;
  1767. TStrings * RemoteFileListFull = NULL;
  1768. try
  1769. {
  1770. if (LocalFileCommand)
  1771. {
  1772. if (ALocalFileList == NULL)
  1773. {
  1774. DebugAssert(HasDirView[osLocal]);
  1775. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1776. // directly (instead of EnableSelectedOperation) to pass
  1777. // false to FocusedFileOnlyWhenFocused
  1778. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1779. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1780. }
  1781. else
  1782. {
  1783. LocalFileList = ALocalFileList;
  1784. }
  1785. if (FileListCommand)
  1786. {
  1787. if (LocalFileList->Count != 1)
  1788. {
  1789. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1790. }
  1791. }
  1792. else
  1793. {
  1794. if ((LocalFileList->Count != 1) &&
  1795. (FileList->Count != 1) &&
  1796. (LocalFileList->Count != FileList->Count))
  1797. {
  1798. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1799. }
  1800. }
  1801. }
  1802. UnicodeString RootTempDir;
  1803. UnicodeString TempDir;
  1804. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1805. if (!RemoteFiles)
  1806. {
  1807. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1808. }
  1809. try
  1810. {
  1811. TDateTimes RemoteFileTimes;
  1812. if (RemoteFiles)
  1813. {
  1814. RemoteFileList = FileList;
  1815. }
  1816. else
  1817. {
  1818. RemoteFileList = new TStringList();
  1819. TMakeLocalFileListParams MakeFileListParam;
  1820. MakeFileListParam.FileList = RemoteFileList;
  1821. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1822. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1823. MakeFileListParam.Recursive =
  1824. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1825. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1826. if (!MakeFileListParam.Recursive)
  1827. {
  1828. RemoteFileTimes.clear();
  1829. RemoteFileListFull = new TStringList();
  1830. MakeFileListParam.FileList = RemoteFileListFull;
  1831. MakeFileListParam.Recursive = true;
  1832. MakeFileListParam.IncludeDirs = false;
  1833. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1834. }
  1835. else
  1836. {
  1837. RemoteFileListFull = RemoteFileList;
  1838. }
  1839. }
  1840. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1841. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1842. if (!NonBlocking)
  1843. {
  1844. int Count;
  1845. if (FileListCommand)
  1846. {
  1847. Count = 1;
  1848. }
  1849. else if (LocalFileCommand)
  1850. {
  1851. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1852. }
  1853. else
  1854. {
  1855. Count = RemoteFileList->Count;
  1856. }
  1857. Progress.Start(foCustomCommand, osRemote, Count);
  1858. DebugAssert(FProgressForm != NULL);
  1859. FProgressForm->ReadOnly = true;
  1860. }
  1861. try
  1862. {
  1863. if (FileListCommand)
  1864. {
  1865. UnicodeString LocalFile;
  1866. // MakeFileList does not delimit filenames
  1867. UnicodeString FileList = MakeFileList(RemoteFileList);
  1868. if (LocalFileCommand)
  1869. {
  1870. DebugAssert(LocalFileList->Count == 1);
  1871. LocalFile = LocalFileList->Strings[0];
  1872. }
  1873. TLocalCustomCommand CustomCommand(Data,
  1874. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1875. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1876. if (NonBlocking)
  1877. {
  1878. DebugAssert(!POutput);
  1879. ExecuteShellChecked(ShellCommand);
  1880. }
  1881. else
  1882. {
  1883. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1884. }
  1885. }
  1886. else if (LocalFileCommand)
  1887. {
  1888. if (LocalFileList->Count == 1)
  1889. {
  1890. UnicodeString LocalFile = LocalFileList->Strings[0];
  1891. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1892. {
  1893. UnicodeString FileName = RemoteFileList->Strings[Index];
  1894. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1895. Progress.SetFile(FileNameForProgress);
  1896. TLocalCustomCommand CustomCommand(Data,
  1897. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1898. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1899. TOnceDoneOperation OnceDoneOperation;
  1900. // Do not unselect anything, as it won't work with two panels anyway
  1901. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1902. }
  1903. }
  1904. else if (RemoteFileList->Count == 1)
  1905. {
  1906. UnicodeString FileName = RemoteFileList->Strings[0];
  1907. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1908. {
  1909. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1910. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1911. Progress.SetFile(LocalFileNameOnly);
  1912. TLocalCustomCommand CustomCommand(
  1913. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1914. FileName, LocalFileName, L"");
  1915. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1916. TOnceDoneOperation OnceDoneOperation;
  1917. // Do not unselect anything, as it won't work with two panels anyway
  1918. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1919. }
  1920. }
  1921. else
  1922. {
  1923. if (LocalFileList->Count != RemoteFileList->Count)
  1924. {
  1925. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1926. }
  1927. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1928. {
  1929. UnicodeString FileName = RemoteFileList->Strings[Index];
  1930. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1931. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1932. Progress.SetFile(LocalFileNameOnly); // sic
  1933. TLocalCustomCommand CustomCommand(
  1934. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1935. FileName, LocalFileName, L"");
  1936. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1937. TOnceDoneOperation OnceDoneOperation;
  1938. // Do not unselect anything, as it won't work with two panels anyway
  1939. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1940. }
  1941. }
  1942. }
  1943. else
  1944. {
  1945. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1946. {
  1947. UnicodeString FileName = RemoteFileList->Strings[Index];
  1948. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1949. Progress.SetFile(FileNameForProgress);
  1950. TLocalCustomCommand CustomCommand(Data,
  1951. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1952. FileName, L"", L"");
  1953. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1954. TOnceDoneOperation OnceDoneOperation;
  1955. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1956. }
  1957. }
  1958. }
  1959. __finally
  1960. {
  1961. if (!NonBlocking)
  1962. {
  1963. Progress.Stop();
  1964. }
  1965. }
  1966. DebugAssert(!FAutoOperation);
  1967. if (!RemoteFiles)
  1968. {
  1969. TempDir = IncludeTrailingBackslash(TempDir);
  1970. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1971. {
  1972. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1973. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1974. // Skip directories, process only nested files.
  1975. // The check is redundant as FileAge fails for directories anyway.
  1976. !DirectoryExists(FileName))
  1977. {
  1978. UnicodeString RemoteDir =
  1979. UnixExtractFileDir(
  1980. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1981. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1982. TDateTime NewTime;
  1983. if (FileAge(FileName, NewTime) &&
  1984. (NewTime != RemoteFileTimes[Index]))
  1985. {
  1986. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1987. TemporaryFileCopyParam(CopyParam);
  1988. CopyParam.FileMask = L"";
  1989. FAutoOperation = true;
  1990. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1991. TemporaryFilesList->Add(FileName);
  1992. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1993. }
  1994. }
  1995. }
  1996. }
  1997. }
  1998. __finally
  1999. {
  2000. FAutoOperation = false;
  2001. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2002. {
  2003. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2004. }
  2005. }
  2006. }
  2007. __finally
  2008. {
  2009. if (RemoteFileList != FileList)
  2010. {
  2011. delete RemoteFileList;
  2012. }
  2013. if (LocalFileList != ALocalFileList)
  2014. {
  2015. delete LocalFileList;
  2016. }
  2017. if (RemoteFileListFull != RemoteFileList)
  2018. {
  2019. delete RemoteFileListFull;
  2020. }
  2021. }
  2022. }
  2023. //---------------------------------------------------------------------------
  2024. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2025. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2026. bool FileListCommand, UnicodeString * POutput)
  2027. {
  2028. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2029. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2030. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2031. {
  2032. UnicodeString FileName = SelectedFileList->Strings[Index];
  2033. if (DirectoryExists(FileName))
  2034. {
  2035. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2036. {
  2037. LocalFileList->Add(FileName);
  2038. }
  2039. if (FLAGSET(ACommand.Params, ccRecursive))
  2040. {
  2041. TMakeLocalFileListParams MakeFileListParam;
  2042. MakeFileListParam.FileList = LocalFileList.get();
  2043. MakeFileListParam.FileTimes = NULL;
  2044. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2045. MakeFileListParam.Recursive = true;
  2046. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2047. }
  2048. }
  2049. else
  2050. {
  2051. LocalFileList->Add(FileName);
  2052. }
  2053. }
  2054. if (FileListCommand)
  2055. {
  2056. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2057. TLocalCustomCommand CustomCommand(
  2058. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2059. L"", L"", FileList);
  2060. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2061. }
  2062. else
  2063. {
  2064. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2065. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2066. DebugAssert(FProgressForm != NULL);
  2067. FProgressForm->ReadOnly = true;
  2068. try
  2069. {
  2070. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2071. {
  2072. UnicodeString FileName = LocalFileList->Strings[Index];
  2073. Progress.SetFile(FileName);
  2074. TLocalCustomCommand CustomCommand(
  2075. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2076. FileName, L"", L"");
  2077. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2078. TOnceDoneOperation OnceDoneOperation;
  2079. Progress.Finish(FileName, true, OnceDoneOperation);
  2080. }
  2081. }
  2082. __finally
  2083. {
  2084. Progress.Stop();
  2085. }
  2086. }
  2087. }
  2088. //---------------------------------------------------------------------------
  2089. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2090. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2091. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2092. {
  2093. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2094. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2095. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2096. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2097. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2098. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2099. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2100. std::unique_ptr<UnicodeString> POutput;
  2101. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2102. FLAGSET(ACommand.Params, ccCopyResults))
  2103. {
  2104. POutput.reset(new UnicodeString());
  2105. }
  2106. if (!LocalCustomCommand.IsFileCommand(Command))
  2107. {
  2108. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2109. }
  2110. // remote files?
  2111. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2112. {
  2113. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2114. }
  2115. // local files
  2116. else
  2117. {
  2118. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2119. }
  2120. if (POutput.get() != NULL)
  2121. {
  2122. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2123. int P = POutput->Pos(sLineBreak);
  2124. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2125. {
  2126. POutput->SetLength(P - 1);
  2127. }
  2128. // Copy even empty output to clipboard
  2129. if (FLAGSET(ACommand.Params, ccCopyResults))
  2130. {
  2131. CopyToClipboard(*POutput);
  2132. }
  2133. // But do not show an empty message box.
  2134. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2135. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2136. !POutput->IsEmpty())
  2137. {
  2138. TClipboardHandler ClipboardHandler;
  2139. ClipboardHandler.Text = *POutput;
  2140. TMessageParams Params;
  2141. TQueryButtonAlias Aliases[1];
  2142. Aliases[0].Button = qaRetry;
  2143. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2144. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2145. Params.Aliases = Aliases;
  2146. Params.AliasesCount = LENOF(Aliases);
  2147. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2148. }
  2149. }
  2150. }
  2151. //---------------------------------------------------------------------------
  2152. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2153. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2154. {
  2155. TCustomCommandData Data(Terminal);
  2156. UnicodeString Site = Terminal->SessionData->SessionKey;
  2157. UnicodeString HelpKeyword = ACommand.HomePage;
  2158. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2159. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2160. {
  2161. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2162. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2163. {
  2164. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2165. }
  2166. else
  2167. {
  2168. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2169. }
  2170. if (!DoCustomCommandOptionsDialog(
  2171. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2172. {
  2173. Abort();
  2174. }
  2175. }
  2176. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2177. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2178. {
  2179. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2180. }
  2181. else
  2182. {
  2183. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2184. }
  2185. }
  2186. //---------------------------------------------------------------------------
  2187. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2188. const TCustomCommandType & Command)
  2189. {
  2190. DebugAssert(FCustomCommandLocalFileList != NULL);
  2191. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2192. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2193. TStrings * LocalFileList = new TStringList();
  2194. TStrings * RemoteFileList = new TStringList();
  2195. try
  2196. {
  2197. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2198. {
  2199. LocalFileList->Clear();
  2200. LocalFileList->AddObject(
  2201. FCustomCommandLocalFileList->Strings[Index],
  2202. FCustomCommandLocalFileList->Objects[Index]);
  2203. RemoteFileList->Clear();
  2204. RemoteFileList->AddObject(
  2205. FCustomCommandRemoteFileList->Strings[Index],
  2206. FCustomCommandRemoteFileList->Objects[Index]);
  2207. CustomCommand(RemoteFileList, Command, LocalFileList);
  2208. }
  2209. }
  2210. __finally
  2211. {
  2212. delete LocalFileList;
  2213. delete RemoteFileList;
  2214. }
  2215. }
  2216. //---------------------------------------------------------------------------
  2217. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2218. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2219. {
  2220. delete FCustomCommandLocalFileList;
  2221. delete FCustomCommandRemoteFileList;
  2222. // takeover ownership,
  2223. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2224. FCustomCommandLocalFileList = LocalFileList;
  2225. FCustomCommandRemoteFileList = RemoteFileList;
  2226. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2227. if (Button != NULL)
  2228. {
  2229. FCustomCommandMenu->Items->Clear();
  2230. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2231. MenuPopup(FCustomCommandMenu, Button);
  2232. }
  2233. else
  2234. {
  2235. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2236. }
  2237. }
  2238. //---------------------------------------------------------------------------
  2239. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2240. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2241. {
  2242. DebugAssert(FCapturedLog != NULL);
  2243. if ((OutputType == cotOutput) || (OutputType == cotError))
  2244. {
  2245. FCapturedLog->Add(AddedLine);
  2246. }
  2247. }
  2248. //---------------------------------------------------------------------------
  2249. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2250. TOperationSide Side)
  2251. {
  2252. return (Side == osRemote) &&
  2253. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2254. }
  2255. //---------------------------------------------------------------------------
  2256. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2257. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2258. {
  2259. TTBItemOptions O;
  2260. O = Item->Options;
  2261. TCustomDirView * DView = DirView(Side);
  2262. if ((DView->ItemFocused != NULL) &&
  2263. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2264. // when resolving links is disabled, default action is to enter the directory,
  2265. // no matter what DoubleClickAction is configured to
  2266. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2267. // Can only Edit files, but can Open/Copy even directories
  2268. ((DoubleClickAction != dcaEdit) ||
  2269. !DView->ItemIsDirectory(DView->ItemFocused)))
  2270. {
  2271. Item->Options = O << tboDefault;
  2272. }
  2273. else
  2274. {
  2275. Item->Options = O >> tboDefault;
  2276. }
  2277. }
  2278. //---------------------------------------------------------------------------
  2279. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2280. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2281. {
  2282. TCustomDirView * DView = DirView(Side);
  2283. TListItem * Item = DView->ItemFocused;
  2284. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2285. EnableFocusedOperation[Side])
  2286. {
  2287. TPoint ClientPoint;
  2288. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2289. TPoint(0, 0) : MousePos;
  2290. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2291. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2292. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2293. }
  2294. }
  2295. //---------------------------------------------------------------------------
  2296. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2297. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2298. {
  2299. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2300. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2301. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2302. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2303. Handled = true;
  2304. }
  2305. //---------------------------------------------------------------------------
  2306. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2307. {
  2308. }
  2309. //---------------------------------------------------------------------------
  2310. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2311. {
  2312. DebugAssert(FErrorList == NULL);
  2313. if (WinConfiguration->ContinueOnError)
  2314. {
  2315. FErrorList = new TStringList();
  2316. Configuration->Usage->Inc(L"ContinuationsOnError");
  2317. }
  2318. NonVisualDataModule->StartBusy();
  2319. }
  2320. //---------------------------------------------------------------------------
  2321. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2322. {
  2323. NonVisualDataModule->EndBusy();
  2324. if (FErrorList)
  2325. {
  2326. HandleErrorList(FErrorList);
  2327. }
  2328. }
  2329. //---------------------------------------------------------------------------
  2330. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2331. const TCopyParamType & CopyParam)
  2332. {
  2333. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2334. {
  2335. Configuration->Usage->Inc(L"FileMaskUses");
  2336. }
  2337. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2338. {
  2339. Configuration->Usage->Inc(L"OperationMaskUses");
  2340. }
  2341. }
  2342. //---------------------------------------------------------------------------
  2343. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2344. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2345. {
  2346. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2347. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2348. TTransferOperationParam DefaultParam;
  2349. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2350. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2351. switch (Param.Queue)
  2352. {
  2353. case asOn:
  2354. CopyParam.Queue = true;
  2355. break;
  2356. case asOff:
  2357. CopyParam.Queue = false;
  2358. break;
  2359. case asAuto:
  2360. default:
  2361. // keep default
  2362. break;
  2363. }
  2364. bool Result =
  2365. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2366. if (Result)
  2367. {
  2368. DebugAssert(Terminal);
  2369. bool SelectionRestored = false;
  2370. TCustomDirView * DView = NULL;
  2371. if (HasDirView[Side])
  2372. {
  2373. DView = DirView(Side);
  2374. DView->SaveSelection();
  2375. DView->SaveSelectedNames();
  2376. }
  2377. UpdateCopyParamCounters(CopyParam);
  2378. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2379. DebugAssert(FTransferResumeList == NULL);
  2380. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2381. FMoveToQueue = false;
  2382. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2383. try
  2384. {
  2385. TStrings * PermanentFileList;
  2386. std::unique_ptr<TStrings> PermanentFileListOwner;
  2387. try
  2388. {
  2389. if (Side == osLocal)
  2390. {
  2391. PermanentFileList = FileList;
  2392. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2393. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2394. if (Operation == foMove)
  2395. {
  2396. ReloadLocalDirectory();
  2397. if (DView != NULL)
  2398. {
  2399. DView->RestoreSelection();
  2400. }
  2401. SelectionRestored = true;
  2402. }
  2403. }
  2404. else
  2405. {
  2406. // Clone the file list as it may refer to current directory files,
  2407. // which get destroyed, when the source directory is reloaded after foMove operation.
  2408. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2409. // But for a hotfix, we are not going to do such a big change.
  2410. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2411. PermanentFileList = PermanentFileListOwner.get();
  2412. try
  2413. {
  2414. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2415. }
  2416. __finally
  2417. {
  2418. if (Operation == foMove)
  2419. {
  2420. if (DView != NULL)
  2421. {
  2422. DView->RestoreSelection();
  2423. }
  2424. SelectionRestored = true;
  2425. }
  2426. ReloadLocalDirectory(Param.TargetDirectory);
  2427. }
  2428. }
  2429. }
  2430. catch (EAbort &)
  2431. {
  2432. if (FMoveToQueue)
  2433. {
  2434. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2435. DebugAssert(CopyParam.TransferSkipList == NULL);
  2436. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2437. if (TransferResumeList->Count > 0)
  2438. {
  2439. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2440. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2441. }
  2442. CopyParam.TransferSkipList = TransferResumeList.release();
  2443. // not really needed, just to keep it consistent with TransferResumeList
  2444. FTransferResumeList = NULL;
  2445. FMoveToQueue = false;
  2446. Configuration->Usage->Inc("MovesToBackground");
  2447. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2448. ClearTransferSourceSelection(Direction);
  2449. }
  2450. throw;
  2451. }
  2452. }
  2453. __finally
  2454. {
  2455. if (!SelectionRestored && (DView != NULL))
  2456. {
  2457. DView->DiscardSavedSelection();
  2458. }
  2459. FTransferResumeList = NULL;
  2460. }
  2461. }
  2462. return Result;
  2463. }
  2464. //---------------------------------------------------------------------------
  2465. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2466. TOperationSide Side, TStrings * FileList, void * Param)
  2467. {
  2468. DebugAssert(FileList->Count);
  2469. // We deliberately do not toggle alternative flag (Param), but use OR,
  2470. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2471. // shortcut of CurrentDeleteAlternativeAction
  2472. bool Alternative =
  2473. bool(Param) || UseAlternativeFunction();
  2474. bool Recycle;
  2475. if (Side == osLocal)
  2476. {
  2477. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2478. }
  2479. else
  2480. {
  2481. Recycle =
  2482. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2483. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2484. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2485. }
  2486. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2487. if (!Result)
  2488. {
  2489. UnicodeString Query;
  2490. if (FileList->Count == 1)
  2491. {
  2492. if (Side == osLocal)
  2493. {
  2494. Query = ExtractFileName(FileList->Strings[0]);
  2495. }
  2496. else
  2497. {
  2498. Query = UnixExtractFileName(FileList->Strings[0]);
  2499. }
  2500. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2501. }
  2502. else
  2503. {
  2504. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2505. }
  2506. TMessageParams Params(mpNeverAskAgainCheck);
  2507. Params.ImageName = L"Delete file";
  2508. unsigned int Answer =
  2509. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2510. if (Answer == qaNeverAskAgain)
  2511. {
  2512. Result = true;
  2513. if (Recycle)
  2514. {
  2515. WinConfiguration->ConfirmRecycling = false;
  2516. }
  2517. else
  2518. {
  2519. WinConfiguration->ConfirmDeleting = false;
  2520. }
  2521. }
  2522. else
  2523. {
  2524. Result = (Answer == qaOK);
  2525. }
  2526. }
  2527. if (Result)
  2528. {
  2529. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2530. }
  2531. return Result;
  2532. }
  2533. //---------------------------------------------------------------------------
  2534. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2535. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2536. {
  2537. TAutoBatch AutoBatch(this);
  2538. bool Result;
  2539. if ((Operation == foCopy) || (Operation == foMove))
  2540. {
  2541. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2542. }
  2543. else if (Operation == foRename)
  2544. {
  2545. DebugAssert(DirView(Side)->ItemFocused);
  2546. DirView(Side)->ItemFocused->EditCaption();
  2547. Result = true;
  2548. }
  2549. else if (Operation == foDelete)
  2550. {
  2551. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2552. }
  2553. else if (Operation == foSetProperties)
  2554. {
  2555. RemoteDirView->SaveSelectedNames();
  2556. Result = SetProperties(Side, FileList);
  2557. }
  2558. else if (Operation == foCustomCommand)
  2559. {
  2560. DebugAssert(Param);
  2561. DebugAssert(Side == osRemote);
  2562. RemoteDirView->SaveSelectedNames();
  2563. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2564. CustomCommand(FileList, *Command, NULL);
  2565. Result = true;
  2566. }
  2567. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2568. {
  2569. DebugAssert(Side == osRemote);
  2570. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2571. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2572. }
  2573. else if (Operation == foLock)
  2574. {
  2575. DebugAssert(Side == osRemote);
  2576. LockFiles(FileList, true);
  2577. Result = true;
  2578. }
  2579. else if (Operation == foUnlock)
  2580. {
  2581. DebugAssert(Side == osRemote);
  2582. LockFiles(FileList, false);
  2583. Result = true;
  2584. }
  2585. else
  2586. {
  2587. DebugFail();
  2588. }
  2589. return Result;
  2590. }
  2591. //---------------------------------------------------------------------------
  2592. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2593. {
  2594. if (Side == osCurrent)
  2595. {
  2596. Side = FCurrentSide;
  2597. }
  2598. return Side;
  2599. }
  2600. //---------------------------------------------------------------------------
  2601. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2602. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2603. {
  2604. Side = GetSide(Side);
  2605. bool Result;
  2606. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2607. try
  2608. {
  2609. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2610. }
  2611. __finally
  2612. {
  2613. delete FileList;
  2614. }
  2615. return Result;
  2616. }
  2617. //---------------------------------------------------------------------------
  2618. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2619. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2620. bool NoConfirmation, void * Param)
  2621. {
  2622. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2623. {
  2624. if ((Operation == foCopy) || (Operation == foMove))
  2625. {
  2626. if (GetSide(Side) == osLocal)
  2627. {
  2628. Configuration->Usage->Inc(L"UploadsCommand");
  2629. }
  2630. else
  2631. {
  2632. Configuration->Usage->Inc(L"DownloadsCommand");
  2633. }
  2634. }
  2635. }
  2636. }
  2637. //---------------------------------------------------------------------------
  2638. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2639. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2640. {
  2641. TTransferOperationParam Param;
  2642. if ((WinConfiguration->Interface != ifCommander) ||
  2643. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2644. {
  2645. Flags &= ~cocShortCutHint;
  2646. }
  2647. TCustomDirView * DView = DirView(Side);
  2648. Param.Options =
  2649. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2650. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2651. if (FLAGSET(Flags, cocQueue))
  2652. {
  2653. Param.Queue = asOn;
  2654. }
  2655. else if (FLAGSET(Flags, cocNonQueue))
  2656. {
  2657. Param.Queue = asOff;
  2658. }
  2659. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2660. }
  2661. //---------------------------------------------------------------------------
  2662. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2663. {
  2664. try
  2665. {
  2666. if (ErrorList->Count)
  2667. {
  2668. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2669. if (MessageDialog(Message, qtError,
  2670. qaOK | qaCancel, HELP_NONE) == qaOK)
  2671. {
  2672. unsigned int Answer;
  2673. int Index = 0;
  2674. do
  2675. {
  2676. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2677. TQueryButtonAlias Aliases[2];
  2678. Aliases[0].Button = qaYes;
  2679. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2680. Aliases[1].Button = qaNo;
  2681. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2682. TMessageParams Params;
  2683. Params.Aliases = Aliases;
  2684. Params.AliasesCount = LENOF(Aliases);
  2685. Answer = MoreMessageDialog(
  2686. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2687. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2688. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2689. qaOK, HELP_NONE, &Params);
  2690. if (Answer == qaNo)
  2691. {
  2692. Index++;
  2693. }
  2694. if (Answer == qaYes)
  2695. {
  2696. Index--;
  2697. }
  2698. }
  2699. while (Answer != qaOK);
  2700. }
  2701. }
  2702. }
  2703. __finally
  2704. {
  2705. TStrings * List = ErrorList;
  2706. ErrorList = NULL;
  2707. for (int i = 0; i < List->Count; i++)
  2708. {
  2709. delete List->Objects[i];
  2710. }
  2711. delete List;
  2712. }
  2713. }
  2714. //---------------------------------------------------------------------------
  2715. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2716. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2717. {
  2718. // needed for checking filemasks, as there's no directory object
  2719. // associated with the file object
  2720. File->FullFileName = FullFileName;
  2721. TFileMasks::TParams MaskParams;
  2722. MaskParams.Size = File->Size;
  2723. MaskParams.Modification = File->Modification;
  2724. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2725. }
  2726. //---------------------------------------------------------------------------
  2727. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2728. {
  2729. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2730. Side = GetSide(Side);
  2731. TCustomDirView * DView = DirView(Side);
  2732. TListItem * FocusedItem = DView->ItemFocused;
  2733. UnicodeString Name;
  2734. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2735. {
  2736. Name = DView->ItemFileName(FocusedItem);
  2737. }
  2738. else
  2739. {
  2740. Name = LoadStr(NEW_FILE);
  2741. }
  2742. UnicodeString Names = Name;
  2743. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2744. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2745. HELP_EDIT_NEW, History.get(), true))
  2746. {
  2747. while (!Names.IsEmpty())
  2748. {
  2749. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2750. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2751. UnicodeString TargetFileName;
  2752. UnicodeString LocalFileName;
  2753. UnicodeString RootTempDir;
  2754. UnicodeString TempDir;
  2755. UnicodeString RemoteDirectory;
  2756. bool ExistingFile = false;
  2757. if (Side == osRemote)
  2758. {
  2759. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2760. TRemoteFile * File = NULL;
  2761. if (FTerminal->FileExists(Name, &File) &&
  2762. !File->IsDirectory)
  2763. {
  2764. try
  2765. {
  2766. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2767. ExistingFile = true;
  2768. }
  2769. __finally
  2770. {
  2771. delete File;
  2772. }
  2773. }
  2774. if (!ExistingFile)
  2775. {
  2776. RemoteDirectory = UnixExtractFilePath(Name);
  2777. TemporaryDirectoryForRemoteFiles(
  2778. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2779. TargetFileName = UnixExtractFileName(Name);
  2780. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2781. LocalFileName = TempDir +
  2782. // We probably do not want to trim the VMS version here
  2783. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2784. }
  2785. }
  2786. else
  2787. {
  2788. if (ExtractFilePath(Name).IsEmpty())
  2789. {
  2790. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2791. }
  2792. else
  2793. {
  2794. LocalFileName = ExpandFileName(Name);
  2795. }
  2796. TargetFileName = ExtractFileName(Name);
  2797. }
  2798. if (!ExistingFile)
  2799. {
  2800. if (!FileExists(ApiPath(LocalFileName)))
  2801. {
  2802. int File = FileCreate(ApiPath(LocalFileName));
  2803. if (File < 0)
  2804. {
  2805. if (!RootTempDir.IsEmpty())
  2806. {
  2807. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2808. }
  2809. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2810. }
  2811. else
  2812. {
  2813. FileClose(File);
  2814. }
  2815. }
  2816. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2817. const TEditorData * ExternalEditor = NULL;
  2818. TFileMasks::TParams MaskParams; // size not known
  2819. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2820. false, MaskParams);
  2821. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2822. ExternalEditor, RootTempDir, RemoteDirectory);
  2823. }
  2824. }
  2825. }
  2826. }
  2827. //---------------------------------------------------------------------------
  2828. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2829. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2830. {
  2831. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2832. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2833. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2834. return
  2835. ((ExecuteFileBy == efInternalEditor)) ||
  2836. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2837. }
  2838. //---------------------------------------------------------------------------
  2839. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2840. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2841. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2842. UnicodeString RemoteDirectory)
  2843. {
  2844. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2845. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2846. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2847. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2848. Side = GetSide(Side);
  2849. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2850. if (Side == osRemote)
  2851. {
  2852. Data->Terminal = Terminal;
  2853. Data->Queue = Queue;
  2854. Data->SessionData = CloneCurrentSessionData();
  2855. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2856. Data->RemoteDirectory = RemoteDirectory;
  2857. Data->SessionName = Terminal->SessionData->SessionName;
  2858. Data->LocalRootDirectory = LocalRootDirectory;
  2859. Data->OriginalFileName = OriginalFileName;
  2860. Data->Command = L""; // will be changed later for external editor
  2861. }
  2862. if (ExecuteFileBy == efInternalEditor)
  2863. {
  2864. if (Side == osRemote)
  2865. {
  2866. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2867. L" - " + Terminal->SessionData->SessionName;
  2868. TForm * Editor;
  2869. try
  2870. {
  2871. TNotifyEvent OnFileChanged = NULL;
  2872. TNotifyEvent OnSaveAll = NULL;
  2873. TAnyModifiedEvent OnAnyModified = NULL;
  2874. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2875. if (Terminal->IsCapable[fsBackgroundTransfers])
  2876. {
  2877. OnFileChanged = FEditorManager->FileChanged;
  2878. OnSaveAll = SaveAllInternalEditors;
  2879. OnAnyModified = AnyInternalEditorModified;
  2880. }
  2881. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2882. FEditorManager->FileReload, FEditorManager->FileClosed,
  2883. OnSaveAll, OnAnyModified,
  2884. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2885. }
  2886. catch(...)
  2887. {
  2888. if (!LocalRootDirectory.IsEmpty())
  2889. {
  2890. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2891. }
  2892. throw;
  2893. }
  2894. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2895. }
  2896. else
  2897. {
  2898. DebugAssert(!FStandaloneEditing);
  2899. TForm * Editor =
  2900. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2901. SaveAllInternalEditors, AnyInternalEditorModified,
  2902. L"", false, SessionColor, -1);
  2903. FLocalEditors->Add(Editor);
  2904. }
  2905. }
  2906. else
  2907. {
  2908. HANDLE Process;
  2909. if (ExecuteFileBy == efExternalEditor)
  2910. {
  2911. UnicodeString Program, Params, Dir;
  2912. Data->Command = ExternalEditor->ExternalEditor;
  2913. ReformatFileNameCommand(Data->Command);
  2914. SplitCommand(Data->Command, Program, Params, Dir);
  2915. Params = ExpandFileNameCommand(Params, FileName);
  2916. Program = ExpandEnvironmentVariables(Program);
  2917. if (!ExecuteShell(Program, Params, Process))
  2918. {
  2919. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2920. }
  2921. }
  2922. else
  2923. {
  2924. DebugAssert(Side == osRemote);
  2925. if (!ExecuteShell(FileName, L"", Process))
  2926. {
  2927. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2928. }
  2929. }
  2930. if ((Side == osLocal) ||
  2931. ((ExecuteFileBy == efShell) &&
  2932. !WinConfiguration->Editor.SDIShellEditor) ||
  2933. ((ExecuteFileBy == efExternalEditor) &&
  2934. !ExternalEditor->SDIExternalEditor))
  2935. {
  2936. // no need for handle
  2937. if (Process != NULL)
  2938. {
  2939. DebugCheck(CloseHandle(Process));
  2940. }
  2941. Process = INVALID_HANDLE_VALUE;
  2942. }
  2943. else
  2944. {
  2945. if (Process == NULL)
  2946. {
  2947. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2948. }
  2949. }
  2950. if (Side == osRemote)
  2951. {
  2952. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2953. }
  2954. }
  2955. }
  2956. //---------------------------------------------------------------------------
  2957. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2958. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2959. void * /*Arg*/)
  2960. {
  2961. if (Token != NULL)
  2962. {
  2963. EditorFormFileSave(static_cast<TForm *>(Token));
  2964. }
  2965. }
  2966. //---------------------------------------------------------------------------
  2967. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2968. {
  2969. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2970. {
  2971. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2972. }
  2973. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2974. }
  2975. //---------------------------------------------------------------------------
  2976. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2977. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2978. void * Arg)
  2979. {
  2980. if ((Token != NULL) &&
  2981. IsEditorFormModified(static_cast<TForm *>(Token)))
  2982. {
  2983. *static_cast<bool *>(Arg) = true;
  2984. }
  2985. }
  2986. //---------------------------------------------------------------------------
  2987. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2988. TObject * /*Sender*/, bool & Modified)
  2989. {
  2990. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2991. {
  2992. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2993. {
  2994. Modified = true;
  2995. }
  2996. }
  2997. if (!Modified)
  2998. {
  2999. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3000. }
  3001. }
  3002. //---------------------------------------------------------------------------
  3003. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3004. {
  3005. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3006. }
  3007. //---------------------------------------------------------------------------
  3008. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3009. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3010. UnicodeString & Result, UnicodeString & RootDirectory)
  3011. {
  3012. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3013. {
  3014. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3015. Result = RootDirectory;
  3016. }
  3017. else
  3018. {
  3019. RootDirectory = L"";
  3020. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3021. Result = IncludeTrailingBackslash(Result);
  3022. }
  3023. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3024. {
  3025. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3026. }
  3027. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3028. {
  3029. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3030. {
  3031. RemoteDirectory.Delete(1, 1);
  3032. }
  3033. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3034. }
  3035. if (!ForceDirectories(ApiPath(Result)))
  3036. {
  3037. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3038. }
  3039. }
  3040. //---------------------------------------------------------------------------
  3041. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3042. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3043. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3044. {
  3045. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3046. if (ForceText)
  3047. {
  3048. CopyParam.TransferMode = tmAscii;
  3049. }
  3050. TemporaryFileCopyParam(CopyParam);
  3051. if (AllFiles)
  3052. {
  3053. CopyParam.IncludeFileMask = TFileMasks();
  3054. }
  3055. if (TempDir.IsEmpty())
  3056. {
  3057. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3058. }
  3059. DebugAssert(!FAutoOperation);
  3060. FAutoOperation = AutoOperation;
  3061. Terminal->ExceptionOnFail = true;
  3062. try
  3063. {
  3064. try
  3065. {
  3066. // turn off confirmations, as for MDI editors we may possibly download
  3067. // the same file over
  3068. Terminal->CopyToLocal(
  3069. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3070. if (GetTargetNames)
  3071. {
  3072. for (int i = 0; i < FileList->Count; i++)
  3073. {
  3074. FileList->Strings[i] =
  3075. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3076. }
  3077. }
  3078. }
  3079. catch(...)
  3080. {
  3081. if (!RootTempDir.IsEmpty())
  3082. {
  3083. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3084. }
  3085. throw;
  3086. }
  3087. }
  3088. __finally
  3089. {
  3090. FAutoOperation = false;
  3091. Terminal->ExceptionOnFail = false;
  3092. }
  3093. }
  3094. //---------------------------------------------------------------------------
  3095. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3096. {
  3097. // Do not waste time checking for default editor list the next time,
  3098. // or testing if default editor is not notepad.
  3099. bool TryNextTime = false;
  3100. UnicodeString UsageState;
  3101. if (!WinConfiguration->EditorList->IsDefaultList())
  3102. {
  3103. UsageState = "H";
  3104. }
  3105. else
  3106. {
  3107. UnicodeString Executable;
  3108. UnicodeString ExecutableDescription;
  3109. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3110. {
  3111. UnicodeString Message =
  3112. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3113. unsigned int Answer =
  3114. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3115. if (Answer != qaOK)
  3116. {
  3117. UsageState = "R";
  3118. }
  3119. else
  3120. {
  3121. UsageState = "A";
  3122. TEditorData EditorData;
  3123. EditorData.Editor = edExternal;
  3124. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3125. EditorData.DecideExternalEditorText();
  3126. TEditorList EditorList;
  3127. EditorList = *WinConfiguration->EditorList;
  3128. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3129. WinConfiguration->EditorList = &EditorList;
  3130. }
  3131. }
  3132. }
  3133. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3134. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3135. }
  3136. //---------------------------------------------------------------------------
  3137. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3138. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3139. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3140. {
  3141. if (ExecuteFileBy == efDefaultEditor)
  3142. {
  3143. if (!WinConfiguration->OfferedEditorAutoConfig)
  3144. {
  3145. EditorAutoConfig();
  3146. }
  3147. const TEditorPreferences * Editor =
  3148. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3149. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3150. {
  3151. ExecuteFileBy = efInternalEditor;
  3152. ExternalEditor = NULL;
  3153. }
  3154. else if (Editor->Data->Editor == edOpen)
  3155. {
  3156. ExecuteFileBy = efShell;
  3157. ExternalEditor = NULL;
  3158. }
  3159. else
  3160. {
  3161. ExecuteFileBy = efExternalEditor;
  3162. ExternalEditor = Editor->Data;
  3163. }
  3164. }
  3165. }
  3166. //---------------------------------------------------------------------------
  3167. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3168. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3169. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3170. {
  3171. UnicodeString OriginalFileName;
  3172. UnicodeString LocalRootDirectory;
  3173. UnicodeString RemoteDirectory;
  3174. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3175. (Side == osLocal), MaskParams);
  3176. UnicodeString Counter;
  3177. UnicodeString LocalFileName;
  3178. bool Handled = false;
  3179. if (Side == osRemote)
  3180. {
  3181. // We need to trim VMS version here, so that we use name without version
  3182. // when uploading back to create a new version of the file
  3183. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3184. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3185. TObject * Token = NULL;
  3186. UnicodeString LocalDirectory;
  3187. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3188. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3189. LocalDirectory))
  3190. {
  3191. if (Token != NULL)
  3192. {
  3193. TForm * Form = dynamic_cast<TForm *>(Token);
  3194. if (Form->WindowState == wsMinimized)
  3195. {
  3196. ShowWindow(Form->Handle, SW_RESTORE);
  3197. }
  3198. else
  3199. {
  3200. Form->SetFocus();
  3201. }
  3202. Handled = true;
  3203. }
  3204. else
  3205. {
  3206. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3207. }
  3208. }
  3209. if (!Handled)
  3210. {
  3211. TStringList * FileList1 = new TStringList();
  3212. try
  3213. {
  3214. FileList1->AddObject(FullFileName, Object);
  3215. TemporarilyDownloadFiles(FileList1,
  3216. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3217. LocalRootDirectory, LocalDirectory, true, true, true);
  3218. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3219. }
  3220. __finally
  3221. {
  3222. delete FileList1;
  3223. }
  3224. switch (ExecuteFileBy)
  3225. {
  3226. case efShell:
  3227. Counter = "RemoteFilesExecuted";
  3228. break;
  3229. case efInternalEditor:
  3230. Counter = "RemoteFilesOpenedInInternalEditor";
  3231. break;
  3232. case efExternalEditor:
  3233. Counter = "RemoteFilesOpenedInExternalEditor";
  3234. break;
  3235. default:
  3236. DebugFail();
  3237. }
  3238. }
  3239. }
  3240. else
  3241. {
  3242. LocalFileName = FullFileName;
  3243. OriginalFileName = ExtractFileName(FullFileName);
  3244. switch (ExecuteFileBy)
  3245. {
  3246. case efShell:
  3247. Counter = "LocalFilesExecuted";
  3248. break;
  3249. case efInternalEditor:
  3250. Counter = "LocalFilesOpenedInInternalEditor";
  3251. break;
  3252. case efExternalEditor:
  3253. Counter = "LocalFilesOpenedInExternalEditor";
  3254. break;
  3255. default:
  3256. DebugFail();
  3257. }
  3258. }
  3259. if (!Handled)
  3260. {
  3261. Configuration->Usage->Inc(Counter);
  3262. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3263. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3264. }
  3265. }
  3266. //---------------------------------------------------------------------------
  3267. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3268. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3269. bool AllSelected, bool OnFocused)
  3270. {
  3271. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3272. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3273. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3274. Side = GetSide(Side);
  3275. TCustomDirView * DView = DirView(Side);
  3276. TStrings * FileList = AllSelected ?
  3277. DView->CreateFileList(OnFocused, Side == osLocal) :
  3278. DView->CreateFocusedFileList(Side == osLocal);
  3279. try
  3280. {
  3281. DebugAssert(AllSelected || (FileList->Count == 1));
  3282. for (int i = 0; i < FileList->Count; i++)
  3283. {
  3284. UnicodeString ListFileName = FileList->Strings[i];
  3285. UnicodeString FileNameOnly = (Side == osRemote) ?
  3286. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3287. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3288. if (!DView->ItemIsDirectory(Item))
  3289. {
  3290. UnicodeString FullFileName;
  3291. if (Side == osRemote)
  3292. {
  3293. FullFileName = RemoteDirView->Path + ListFileName;
  3294. }
  3295. else
  3296. {
  3297. FullFileName = ListFileName;
  3298. }
  3299. TFileMasks::TParams MaskParams;
  3300. MaskParams.Size = DView->ItemFileSize(Item);
  3301. TDateTimePrecision Precision;
  3302. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3303. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3304. FileList->Objects[i], MaskParams);
  3305. }
  3306. }
  3307. }
  3308. __finally
  3309. {
  3310. delete FileList;
  3311. }
  3312. }
  3313. //---------------------------------------------------------------------------
  3314. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3315. {
  3316. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3317. CopyParam.FileNameCase = ncNoChange;
  3318. CopyParam.PreserveRights = false;
  3319. CopyParam.PreserveReadOnly = false;
  3320. CopyParam.ReplaceInvalidChars = true;
  3321. CopyParam.IncludeFileMask = TFileMasks();
  3322. CopyParam.NewerOnly = false;
  3323. CopyParam.FileMask = L"";
  3324. }
  3325. //---------------------------------------------------------------------------
  3326. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3327. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3328. {
  3329. TTerminalManager * Manager = TTerminalManager::Instance();
  3330. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3331. {
  3332. if (!NonVisualDataModule->Busy)
  3333. {
  3334. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3335. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3336. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3337. if (Data->Terminal == NULL)
  3338. {
  3339. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3340. if (SameSiteTerminal != NULL)
  3341. {
  3342. UnicodeString Message =
  3343. FMTLOAD(EDIT_SESSION_REATTACH,
  3344. (FileNameOnly, Data->SessionName, FileNameOnly));
  3345. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3346. {
  3347. Data->Terminal = Terminal;
  3348. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3349. Data->SessionName = Terminal->SessionData->SessionName;
  3350. // We might also overwrite session data
  3351. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3352. }
  3353. else
  3354. {
  3355. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3356. Abort();
  3357. }
  3358. }
  3359. }
  3360. // foreground session should reconnect itself
  3361. else if (Terminal != Data->Terminal)
  3362. {
  3363. UnicodeString Message =
  3364. MainInstructions(
  3365. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3366. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3367. {
  3368. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3369. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3370. }
  3371. else
  3372. {
  3373. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3374. Abort();
  3375. }
  3376. }
  3377. }
  3378. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3379. {
  3380. Configuration->Usage->Inc(L"EditInactiveSession");
  3381. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3382. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3383. (ExtractFileName(FileName), Data->SessionName)));
  3384. }
  3385. }
  3386. TStrings * FileList = new TStringList();
  3387. try
  3388. {
  3389. FileList->Add(FileName);
  3390. // consider using the same settings (preset) as when the file was downloaded
  3391. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3392. TemporaryFileCopyParam(CopyParam);
  3393. if (Data->ForceText)
  3394. {
  3395. CopyParam.TransferMode = tmAscii;
  3396. }
  3397. // so i do not need to worry if masking algorithm works in all cases
  3398. // ("" means "copy file name", no masking is actually done)
  3399. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3400. {
  3401. CopyParam.FileMask = L"";
  3402. }
  3403. else
  3404. {
  3405. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3406. }
  3407. DebugAssert(Data->Queue != NULL);
  3408. int Params = cpNoConfirmation | cpTemporary;
  3409. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3410. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3411. QueueItem->CompleteEvent = UploadCompleteEvent;
  3412. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3413. }
  3414. __finally
  3415. {
  3416. delete FileList;
  3417. }
  3418. }
  3419. //---------------------------------------------------------------------------
  3420. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3421. const UnicodeString FileName, const TEditedFileData * Data)
  3422. {
  3423. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3424. DebugAssert(!NonVisualDataModule->Busy);
  3425. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3426. {
  3427. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3428. (ExtractFileName(FileName), Data->SessionName)));
  3429. }
  3430. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3431. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3432. NonVisualDataModule->StartBusy();
  3433. try
  3434. {
  3435. std::unique_ptr<TRemoteFile> File;
  3436. UnicodeString RemoteFileName =
  3437. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3438. FTerminal->ExceptionOnFail = true;
  3439. try
  3440. {
  3441. TRemoteFile * AFile = NULL;
  3442. FTerminal->ReadFile(RemoteFileName, AFile);
  3443. File.reset(AFile);
  3444. if (!File->HaveFullFileName)
  3445. {
  3446. File->FullFileName = RemoteFileName;
  3447. }
  3448. }
  3449. __finally
  3450. {
  3451. FTerminal->ExceptionOnFail = false;
  3452. }
  3453. std::unique_ptr<TStrings> FileList(new TStringList());
  3454. FileList->AddObject(RemoteFileName, File.get());
  3455. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3456. UnicodeString TempDir = ExtractFilePath(FileName);
  3457. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3458. TempDir, true, true, true);
  3459. // sanity check, the target file name should be still the same
  3460. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3461. }
  3462. __finally
  3463. {
  3464. NonVisualDataModule->EndBusy();
  3465. // it actually may not exist anymore...
  3466. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3467. }
  3468. }
  3469. //---------------------------------------------------------------------------
  3470. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3471. const TEditedFileData * Data, bool & KeepOpen)
  3472. {
  3473. // Command is set for external editors only (not for "shell" open).
  3474. if (!Data->Command.IsEmpty())
  3475. {
  3476. bool AnyFound = false;
  3477. bool AnyMDI = false;
  3478. bool AnyNonMDI = false;
  3479. bool AnyDetect = false;
  3480. TEditorList * EditorList = new TEditorList();
  3481. try
  3482. {
  3483. *EditorList = *WinConfiguration->EditorList;
  3484. for (int i = 0; i < EditorList->Count; i++)
  3485. {
  3486. const TEditorPreferences * Editor = EditorList->Editors[i];
  3487. if ((Editor->Data->Editor == edExternal) &&
  3488. (Editor->Data->ExternalEditor == Data->Command))
  3489. {
  3490. AnyFound = true;
  3491. if (Editor->Data->SDIExternalEditor)
  3492. {
  3493. AnyNonMDI = true;
  3494. if (Editor->Data->DetectMDIExternalEditor)
  3495. {
  3496. AnyDetect = true;
  3497. }
  3498. }
  3499. else
  3500. {
  3501. AnyMDI = true;
  3502. }
  3503. }
  3504. }
  3505. bool EnableMDI = false;
  3506. bool DisableDetect = false;
  3507. if (AnyMDI)
  3508. {
  3509. KeepOpen = true;
  3510. if (AnyNonMDI)
  3511. {
  3512. // there is at least one instance of the editor with MDI support enabled,
  3513. // and one with disabled, enable it for all instances
  3514. EnableMDI = true;
  3515. }
  3516. }
  3517. else if (AnyFound && !AnyDetect)
  3518. {
  3519. // at least once instance found but all have MDI autodetection disabled
  3520. // => close the file (default action)
  3521. }
  3522. else
  3523. {
  3524. // no instance of the editor has MDI support enabled
  3525. TMessageParams Params;
  3526. if (AnyFound)
  3527. {
  3528. // there is at least one instance of the editor
  3529. Params.Params |= mpNeverAskAgainCheck;
  3530. }
  3531. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3532. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3533. switch (Answer)
  3534. {
  3535. case qaNeverAskAgain:
  3536. DisableDetect = true;
  3537. break;
  3538. case qaNo:
  3539. EnableMDI = true;
  3540. KeepOpen = true;
  3541. break;
  3542. }
  3543. }
  3544. if (AnyFound && (EnableMDI || DisableDetect))
  3545. {
  3546. bool Changed = false;
  3547. for (int i = 0; i < EditorList->Count; i++)
  3548. {
  3549. const TEditorPreferences * Editor = EditorList->Editors[i];
  3550. if ((Editor->Data->Editor == edExternal) &&
  3551. (Editor->Data->ExternalEditor == Data->Command) &&
  3552. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3553. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3554. {
  3555. Changed = true;
  3556. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3557. if (EnableMDI)
  3558. {
  3559. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3560. }
  3561. if (DisableDetect)
  3562. {
  3563. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3564. }
  3565. EditorList->Change(i, UpdatedEditor);
  3566. }
  3567. }
  3568. if (Changed)
  3569. {
  3570. WinConfiguration->EditorList = EditorList;
  3571. }
  3572. }
  3573. }
  3574. __finally
  3575. {
  3576. delete EditorList;
  3577. }
  3578. }
  3579. else
  3580. {
  3581. // "open" case
  3582. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3583. qaOK, HELP_APP_EARLY_CLOSED);
  3584. }
  3585. }
  3586. //---------------------------------------------------------------------------
  3587. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3588. {
  3589. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3590. }
  3591. //---------------------------------------------------------------------------
  3592. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3593. {
  3594. SideEnter(osRemote);
  3595. }
  3596. //---------------------------------------------------------------------------
  3597. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3598. {
  3599. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3600. SideEnter(osRemote);
  3601. }
  3602. //---------------------------------------------------------------------------
  3603. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3604. {
  3605. FCurrentSide = Side;
  3606. if (Visible)
  3607. {
  3608. UpdateControls();
  3609. }
  3610. }
  3611. //---------------------------------------------------------------------------
  3612. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3613. TStrings * FileList, bool Alternative)
  3614. {
  3615. TCustomDirView * DView = DirView(Side);
  3616. DView->SaveSelection();
  3617. DView->SaveSelectedNames();
  3618. DebugAssert(!FAlternativeDelete);
  3619. FAlternativeDelete = Alternative;
  3620. try
  3621. {
  3622. if (Side == osRemote)
  3623. {
  3624. DebugAssert(Terminal != NULL);
  3625. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3626. }
  3627. else
  3628. {
  3629. try
  3630. {
  3631. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3632. }
  3633. __finally
  3634. {
  3635. ReloadLocalDirectory();
  3636. }
  3637. }
  3638. FAlternativeDelete = false;
  3639. }
  3640. catch(...)
  3641. {
  3642. FAlternativeDelete = false;
  3643. DView->DiscardSavedSelection();
  3644. throw;
  3645. }
  3646. DView->RestoreSelection();
  3647. }
  3648. //---------------------------------------------------------------------------
  3649. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3650. {
  3651. DebugAssert(Terminal);
  3652. RemoteDirView->SaveSelection();
  3653. RemoteDirView->SaveSelectedNames();
  3654. try
  3655. {
  3656. if (Lock)
  3657. {
  3658. Terminal->LockFiles(FileList);
  3659. }
  3660. else
  3661. {
  3662. Terminal->UnlockFiles(FileList);
  3663. }
  3664. }
  3665. catch(...)
  3666. {
  3667. RemoteDirView->DiscardSavedSelection();
  3668. throw;
  3669. }
  3670. RemoteDirView->RestoreSelection();
  3671. }
  3672. //---------------------------------------------------------------------------
  3673. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3674. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3675. bool NoConfirmation, bool Move)
  3676. {
  3677. DebugAssert(Terminal != NULL);
  3678. // update Terminal->StateData->RemoteDirectory
  3679. UpdateTerminal(Terminal);
  3680. if (Session == NULL)
  3681. {
  3682. Session = TTerminalManager::Instance()->ActiveTerminal;
  3683. }
  3684. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3685. {
  3686. if (RemoteDriveView->DropTarget != NULL)
  3687. {
  3688. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3689. }
  3690. else if (RemoteDirView->DropTarget != NULL)
  3691. {
  3692. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3693. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3694. }
  3695. else
  3696. {
  3697. Target = RemoteDirView->Path;
  3698. }
  3699. }
  3700. else
  3701. {
  3702. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3703. Target = ManagedTerminal->StateData->RemoteDirectory;
  3704. }
  3705. Target = UnixIncludeTrailingBackslash(Target);
  3706. if (FileList->Count == 1)
  3707. {
  3708. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3709. }
  3710. else
  3711. {
  3712. FileMask = AnyMask;
  3713. }
  3714. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3715. bool Result = true;
  3716. if (!NoConfirmation)
  3717. {
  3718. bool Multi = (FileList->Count > 1);
  3719. if (Move)
  3720. {
  3721. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3722. }
  3723. else
  3724. {
  3725. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3726. TStrings * Directories = new TStringList;
  3727. try
  3728. {
  3729. for (int Index = 0; Index < Sessions->Count; Index++)
  3730. {
  3731. TManagedTerminal * Terminal =
  3732. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3733. Directories->Add(Terminal->StateData->RemoteDirectory);
  3734. }
  3735. TDirectRemoteCopy AllowDirectCopy;
  3736. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3737. {
  3738. DebugAssert(DirectCopy);
  3739. AllowDirectCopy = drcAllow;
  3740. }
  3741. else if (FTerminal->IsCapable[fcSecondaryShell])
  3742. {
  3743. DebugAssert(DirectCopy);
  3744. AllowDirectCopy = drcConfirmCommandSession;
  3745. }
  3746. else
  3747. {
  3748. DebugAssert(!DirectCopy);
  3749. AllowDirectCopy = drcDisallow;
  3750. }
  3751. void * ASession = Session;
  3752. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3753. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3754. Session = static_cast<TTerminal *>(ASession);
  3755. }
  3756. __finally
  3757. {
  3758. delete Directories;
  3759. }
  3760. }
  3761. }
  3762. return Result;
  3763. }
  3764. //---------------------------------------------------------------------------
  3765. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3766. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3767. {
  3768. bool DirectCopy;
  3769. UnicodeString Target, FileMask;
  3770. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3771. if (Result)
  3772. {
  3773. if (!Move && !DirectCopy)
  3774. {
  3775. Configuration->Usage->Inc("RemoteCopyTemp");
  3776. UnicodeString RootTempDir;
  3777. UnicodeString TempDir;
  3778. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3779. TStrings * TemporaryFilesList = new TStringList();
  3780. try
  3781. {
  3782. TMakeLocalFileListParams MakeFileListParam;
  3783. MakeFileListParam.FileList = TemporaryFilesList;
  3784. MakeFileListParam.FileTimes = NULL;
  3785. MakeFileListParam.IncludeDirs = true;
  3786. MakeFileListParam.Recursive = false;
  3787. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3788. TTerminalManager::Instance()->ActiveTerminal = Session;
  3789. if (TemporaryFilesList->Count > 0)
  3790. {
  3791. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3792. CopyParam.FileMask = FileMask;
  3793. DebugAssert(!FAutoOperation);
  3794. FAutoOperation = true;
  3795. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3796. }
  3797. }
  3798. __finally
  3799. {
  3800. delete TemporaryFilesList;
  3801. FAutoOperation = false;
  3802. if (!RootTempDir.IsEmpty())
  3803. {
  3804. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3805. }
  3806. }
  3807. }
  3808. else
  3809. {
  3810. RemoteDirView->SaveSelection();
  3811. RemoteDirView->SaveSelectedNames();
  3812. try
  3813. {
  3814. if (Move)
  3815. {
  3816. Configuration->Usage->Inc("RemoteMove");
  3817. Terminal->MoveFiles(FileList, Target, FileMask);
  3818. }
  3819. else
  3820. {
  3821. Configuration->Usage->Inc("RemoteCopyDirect");
  3822. DebugAssert(DirectCopy);
  3823. DebugAssert(Session == FTerminal);
  3824. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3825. FTerminal->CommandSessionOpened ||
  3826. CommandSessionFallback())
  3827. {
  3828. Terminal->CopyFiles(FileList, Target, FileMask);
  3829. }
  3830. }
  3831. }
  3832. catch(...)
  3833. {
  3834. RemoteDirView->DiscardSavedSelection();
  3835. throw;
  3836. }
  3837. RemoteDirView->RestoreSelection();
  3838. }
  3839. }
  3840. return Result;
  3841. }
  3842. //---------------------------------------------------------------------------
  3843. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3844. const UnicodeString & Path, TRemoteProperties & Properties)
  3845. {
  3846. Properties.Valid = Properties.Valid << vpEncrypt;
  3847. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3848. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3849. }
  3850. //---------------------------------------------------------------------------
  3851. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3852. {
  3853. Side = GetSide(Side);
  3854. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3855. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3856. UnicodeString Name = LoadStr(NEW_FOLDER);
  3857. int AllowedChanges =
  3858. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3859. bool SaveSettings = false;
  3860. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3861. {
  3862. TWindowLock Lock(this);
  3863. if (Side == osRemote)
  3864. {
  3865. if (SaveSettings)
  3866. {
  3867. GUIConfiguration->NewDirectoryProperties = Properties;
  3868. }
  3869. CreateRemoteDirectory(Name, Properties);
  3870. }
  3871. else
  3872. {
  3873. DirView(Side)->CreateDirectory(Name);
  3874. }
  3875. }
  3876. }
  3877. //---------------------------------------------------------------------------
  3878. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3879. {
  3880. TWindowLock Lock(this);
  3881. DirView(Side)->ExecuteHomeDirectory();
  3882. }
  3883. //---------------------------------------------------------------------------
  3884. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3885. {
  3886. DoOpenDirectoryDialog(odBrowse, Side);
  3887. }
  3888. //---------------------------------------------------------------------------
  3889. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3890. {
  3891. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3892. bool Result = !Path.IsEmpty();
  3893. if (Result)
  3894. {
  3895. // While we might get here when the session is closed (from location profiles),
  3896. // it's not a problem as the Path setter is noop then
  3897. DirView(Side)->Path = Path;
  3898. }
  3899. return Result;
  3900. }
  3901. //---------------------------------------------------------------------------
  3902. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3903. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3904. bool & Close)
  3905. {
  3906. // terminal can be already closed (e.g. dropped connection)
  3907. if (Terminal != NULL)
  3908. {
  3909. try
  3910. {
  3911. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3912. }
  3913. catch(...)
  3914. {
  3915. if (!Terminal->Active)
  3916. {
  3917. Close = true;
  3918. }
  3919. throw;
  3920. }
  3921. }
  3922. }
  3923. //---------------------------------------------------------------------------
  3924. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3925. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3926. bool & Close)
  3927. {
  3928. // terminal can be already closed (e.g. dropped connection)
  3929. if (Terminal != NULL)
  3930. {
  3931. Configuration->Usage->Inc(L"ChecksumCalculated");
  3932. try
  3933. {
  3934. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3935. }
  3936. catch(...)
  3937. {
  3938. if (!Terminal->Active)
  3939. {
  3940. Close = true;
  3941. }
  3942. throw;
  3943. }
  3944. }
  3945. }
  3946. //---------------------------------------------------------------------------
  3947. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3948. {
  3949. bool Result;
  3950. if (Side == osRemote)
  3951. {
  3952. TRemoteTokenList * GroupList = NULL;
  3953. TRemoteTokenList * UserList = NULL;
  3954. try
  3955. {
  3956. TRemoteProperties CurrentProperties;
  3957. if (Terminal->LoadFilesProperties(FileList))
  3958. {
  3959. RemoteDirView->Invalidate();
  3960. }
  3961. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3962. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3963. if (CapableGroupChanging || CapableOwnerChanging)
  3964. {
  3965. if (CapableGroupChanging)
  3966. {
  3967. GroupList = Terminal->Groups->Duplicate();
  3968. }
  3969. if (CapableOwnerChanging)
  3970. {
  3971. UserList = Terminal->Users->Duplicate();
  3972. }
  3973. TRemoteDirectory * Files = Terminal->Files;
  3974. int Count = Files->Count;
  3975. if (Count > 100)
  3976. {
  3977. Count = 100;
  3978. }
  3979. for (int Index = 0; Index < Count; Index++)
  3980. {
  3981. TRemoteFile * File = Files->Files[Index];
  3982. if (CapableGroupChanging)
  3983. {
  3984. GroupList->AddUnique(File->Group);
  3985. }
  3986. if (CapableOwnerChanging)
  3987. {
  3988. UserList->AddUnique(File->Owner);
  3989. }
  3990. }
  3991. // if we haven't collected tokens for all files in current directory,
  3992. // make sure we collect them at least for all selected files.
  3993. // (note that so far the files in FileList has to be from current direcotry)
  3994. if (Count < Files->Count)
  3995. {
  3996. for (int Index = 0; Index < FileList->Count; Index++)
  3997. {
  3998. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3999. if (CapableGroupChanging)
  4000. {
  4001. GroupList->AddUnique(File->Group);
  4002. }
  4003. if (CapableOwnerChanging)
  4004. {
  4005. UserList->AddUnique(File->Owner);
  4006. }
  4007. }
  4008. }
  4009. }
  4010. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4011. int Flags = 0;
  4012. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4013. if (CapableOwnerChanging) Flags |= cpOwner;
  4014. if (CapableGroupChanging) Flags |= cpGroup;
  4015. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4016. if (Terminal->IsCapable[fcCalculatingChecksum])
  4017. {
  4018. CalculateChecksumEvent = CalculateChecksum;
  4019. }
  4020. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4021. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4022. TRemoteProperties NewProperties = CurrentProperties;
  4023. Result =
  4024. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4025. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4026. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4027. CalculateSize, CalculateChecksumEvent);
  4028. if (Result)
  4029. {
  4030. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4031. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4032. }
  4033. }
  4034. __finally
  4035. {
  4036. delete GroupList;
  4037. delete UserList;
  4038. }
  4039. }
  4040. else
  4041. {
  4042. DirView(Side)->DisplayPropertiesMenu();
  4043. Result = true;
  4044. }
  4045. return Result;
  4046. }
  4047. //---------------------------------------------------------------------------
  4048. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4049. {
  4050. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4051. {
  4052. FIgnoreNextDialogChar = Key;
  4053. }
  4054. Key = 0;
  4055. }
  4056. //---------------------------------------------------------------------------
  4057. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4058. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4059. {
  4060. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4061. if (Command != NULL)
  4062. {
  4063. KeyProcessed(Key, Shift);
  4064. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4065. {
  4066. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4067. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4068. false, false, const_cast<TCustomCommandType *>(Command));
  4069. }
  4070. }
  4071. }
  4072. //---------------------------------------------------------------------------
  4073. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4074. {
  4075. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4076. {
  4077. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4078. }
  4079. if (!DirView(osCurrent)->IsEditing())
  4080. {
  4081. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4082. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4083. {
  4084. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4085. if (((Action->ShortCut == KeyShortCut) ||
  4086. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4087. AllowedAction(Action, aaShortCut))
  4088. {
  4089. // Has to be called before the action as the dialog char is already in queue.
  4090. // So when the action consumes message queue, we already need to have the
  4091. // FIgnoreNextDialogChar set
  4092. KeyProcessed(Key, Shift);
  4093. // Reset reference to previous component (when menu/toolbar was clicked).
  4094. // Needed to detect that action was invoked by keyboard shortcut
  4095. // in TNonVisualDataModule::ExplorerActionsExecute
  4096. Action->ActionComponent = NULL;
  4097. Action->Execute();
  4098. return;
  4099. }
  4100. }
  4101. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4102. {
  4103. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4104. {
  4105. KeyProcessed(Key, Shift);
  4106. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4107. return;
  4108. }
  4109. }
  4110. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4111. {
  4112. KeyProcessed(Key, Shift);
  4113. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4114. }
  4115. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4116. if (IsCustomShortCut(CustomShortCut))
  4117. {
  4118. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4119. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4120. if (WinConfiguration->SharedBookmarks != NULL)
  4121. {
  4122. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4123. if ((Bookmark != NULL) &&
  4124. OpenBookmark(FCurrentSide, Bookmark))
  4125. {
  4126. KeyProcessed(Key, Shift);
  4127. }
  4128. }
  4129. }
  4130. }
  4131. TForm::KeyDown(Key, Shift);
  4132. }
  4133. //---------------------------------------------------------------------------
  4134. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4135. {
  4136. DebugAssert(Terminal != NULL);
  4137. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4138. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4139. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4140. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4141. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4142. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4143. // expanded from ?: to avoid memory leaks
  4144. if (SecurityEnabled)
  4145. {
  4146. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4147. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4148. }
  4149. else
  4150. {
  4151. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4152. }
  4153. if (FileSystemInfo.ProtocolName.IsEmpty())
  4154. {
  4155. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4156. }
  4157. else
  4158. {
  4159. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4160. }
  4161. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4162. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4163. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4164. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4165. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4166. UnicodeString CompressionStr = CSCompressionStr;
  4167. if (CSCompressionStr != SCCompressionStr)
  4168. {
  4169. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4170. }
  4171. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4172. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4173. UpdateStatusBar();
  4174. }
  4175. //---------------------------------------------------------------------------
  4176. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4177. {
  4178. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4179. DebugAssert(SessionStatusBar != NULL);
  4180. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4181. {
  4182. // note: (Terminal->Status < sshReady) currently never happens here,
  4183. // so STATUS_CONNECTING is never used
  4184. SessionStatusBar->SimplePanel = true;
  4185. SessionStatusBar->SimpleText = LoadStr(
  4186. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4187. }
  4188. else
  4189. {
  4190. DebugAssert(Terminal);
  4191. SessionStatusBar->SimplePanel = false;
  4192. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4193. if (!FNote.IsEmpty())
  4194. {
  4195. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4196. }
  4197. else
  4198. {
  4199. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4200. }
  4201. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4202. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4203. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4204. }
  4205. }
  4206. //---------------------------------------------------------------------------
  4207. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4208. {
  4209. Panel->Caption = L"";
  4210. }
  4211. //---------------------------------------------------------------------------
  4212. void __fastcall TCustomScpExplorerForm::Idle()
  4213. {
  4214. if (FShowing || FStandaloneEditing)
  4215. {
  4216. FEditorManager->Check();
  4217. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4218. // that needs to know if queue view is visible (and it may be closed after queue update)
  4219. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4220. }
  4221. if (FShowing)
  4222. {
  4223. if (!NonVisualDataModule->Busy &&
  4224. // Menu is opened or drag&drop is going on
  4225. (Mouse->Capture == NULL))
  4226. {
  4227. if (FRefreshRemoteDirectory)
  4228. {
  4229. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4230. {
  4231. Terminal->RefreshDirectory();
  4232. }
  4233. FRefreshRemoteDirectory = false;
  4234. }
  4235. if (FRefreshLocalDirectory)
  4236. {
  4237. ReloadLocalDirectory();
  4238. FRefreshLocalDirectory = false;
  4239. }
  4240. if (WinConfiguration->RefreshRemotePanel)
  4241. {
  4242. TManagedTerminal * ManagedTerminal =
  4243. dynamic_cast<TManagedTerminal *>(Terminal);
  4244. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4245. (Now() - ManagedTerminal->DirectoryLoaded >
  4246. WinConfiguration->RefreshRemotePanelInterval))
  4247. {
  4248. RemoteDirView->ReloadDirectory();
  4249. }
  4250. }
  4251. }
  4252. }
  4253. if (FShowing || FStandaloneEditing)
  4254. {
  4255. if (FQueueStatusInvalidated)
  4256. {
  4257. UpdateQueueStatus(false);
  4258. }
  4259. RefreshQueueItems();
  4260. }
  4261. if (FShowing)
  4262. {
  4263. UpdateStatusBar();
  4264. }
  4265. FIgnoreNextDialogChar = 0;
  4266. }
  4267. //---------------------------------------------------------------------------
  4268. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4269. {
  4270. try
  4271. {
  4272. FUserActionTimer->Enabled = false;
  4273. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4274. {
  4275. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4276. {
  4277. FPendingQueueActionItem->ProcessUserAction();
  4278. }
  4279. }
  4280. }
  4281. __finally
  4282. {
  4283. FPendingQueueActionItem = NULL;
  4284. }
  4285. }
  4286. //---------------------------------------------------------------------------
  4287. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4288. {
  4289. if (WinConfiguration->MinimizeToTray)
  4290. {
  4291. UpdateTrayIcon();
  4292. FTrayIcon->Visible = true;
  4293. if (Visible)
  4294. {
  4295. ShowWindow(Handle, SW_HIDE);
  4296. }
  4297. }
  4298. }
  4299. //---------------------------------------------------------------------------
  4300. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4301. {
  4302. // WORKAROUND
  4303. // When restoring maximized window from minimization,
  4304. // rarely some controls do not align properly.
  4305. // Two instances seen (both for Commander):
  4306. // - When restoring, window is temporarily narrower (not maximized),
  4307. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4308. // Once maximized already, top dock shinks back, but the session PageControl,
  4309. // do not align up, leaving space between TopDock and PageControl.
  4310. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4311. for (int Index = 0; Index < ControlCount; Index++)
  4312. {
  4313. RealignControl(Controls[Index]);
  4314. }
  4315. if (FTrayIcon->Visible)
  4316. {
  4317. FTrayIcon->Visible = false;
  4318. if (Visible)
  4319. {
  4320. ShowWindow(Handle, SW_SHOW);
  4321. }
  4322. }
  4323. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4324. {
  4325. FNeedSession = false;
  4326. NonVisualDataModule->StartBusy();
  4327. try
  4328. {
  4329. NeedSession(false);
  4330. }
  4331. __finally
  4332. {
  4333. NonVisualDataModule->EndBusy();
  4334. }
  4335. }
  4336. }
  4337. //---------------------------------------------------------------------------
  4338. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4339. {
  4340. FTrayIcon->Hint = GetMainForm()->Caption;
  4341. }
  4342. //---------------------------------------------------------------------------
  4343. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4344. {
  4345. UpdateTrayIcon();
  4346. }
  4347. //---------------------------------------------------------------------------
  4348. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4349. {
  4350. // workaround
  4351. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4352. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4353. // But WM_ACTIVATEAPP message can be received even when
  4354. // the main window is minimized to the tray and internal editor window is focused
  4355. // (after another application was previously active)
  4356. if (::IsIconic(Handle))
  4357. {
  4358. if (!IsAppIconic())
  4359. {
  4360. SetAppIconic(true);
  4361. }
  4362. }
  4363. ::ApplicationRestore();
  4364. Application->BringToFront();
  4365. }
  4366. //---------------------------------------------------------------------------
  4367. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4368. {
  4369. RestoreApp();
  4370. }
  4371. //---------------------------------------------------------------------------
  4372. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4373. {
  4374. if (OpenInNewWindow())
  4375. {
  4376. // todo: Pass FromSite
  4377. ExecuteNewInstance(SessionUrl);
  4378. }
  4379. else
  4380. {
  4381. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4382. }
  4383. }
  4384. //---------------------------------------------------------------------------
  4385. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4386. {
  4387. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4388. // current working directories become defaults here, what is not right
  4389. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4390. StoredSessions->NewSession(SessionName, SessionData.get());
  4391. // modified only, explicit
  4392. StoredSessions->Save(false, true);
  4393. // encode session name because of slashes in hierarchical sessions
  4394. return EncodeUrlString(SessionName);
  4395. }
  4396. //---------------------------------------------------------------------------
  4397. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4398. {
  4399. if (OpenInNewWindow())
  4400. {
  4401. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4402. }
  4403. else
  4404. {
  4405. // current working directories become defaults here, what is not right
  4406. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4407. TTerminalManager * Manager = TTerminalManager::Instance();
  4408. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4409. Manager->ActiveTerminal = Terminal;
  4410. }
  4411. }
  4412. //---------------------------------------------------------------------------
  4413. void __fastcall TCustomScpExplorerForm::RenameSession()
  4414. {
  4415. UnicodeString Name = Terminal->SessionData->SessionName;
  4416. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4417. {
  4418. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4419. TSessionData::ValidateName(Name);
  4420. // This is inconsistent with how color (for example) is handled.
  4421. Terminal->SessionData->Name = Name;
  4422. UpdateControls();
  4423. // Add/Remove distinguishing paths from sessions of the same name.
  4424. DoTerminalListChanged();
  4425. }
  4426. }
  4427. //---------------------------------------------------------------------------
  4428. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4429. {
  4430. DebugAssert(Queue != NULL);
  4431. bool Result = Queue->IsEmpty;
  4432. if (!Result)
  4433. {
  4434. SetFocus();
  4435. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4436. }
  4437. return Result;
  4438. }
  4439. //---------------------------------------------------------------------------
  4440. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4441. {
  4442. return CanCloseQueue(FQueue);
  4443. }
  4444. //---------------------------------------------------------------------------
  4445. void __fastcall TCustomScpExplorerForm::CloseSession()
  4446. {
  4447. if (CanCloseQueue())
  4448. {
  4449. TTerminalManager::Instance()->FreeActiveTerminal();
  4450. }
  4451. }
  4452. //---------------------------------------------------------------------------
  4453. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4454. {
  4455. if (OpenInNewWindow())
  4456. {
  4457. // encode session name because of slashes in hierarchical sessions
  4458. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4459. }
  4460. else
  4461. {
  4462. TTerminalManager * Manager = TTerminalManager::Instance();
  4463. TTerminal * Terminal = Manager->NewTerminal(Data);
  4464. Manager->ActiveTerminal = Terminal;
  4465. }
  4466. }
  4467. //---------------------------------------------------------------------------
  4468. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4469. {
  4470. if (OpenInNewWindow())
  4471. {
  4472. ExecuteNewInstance(Name);
  4473. }
  4474. else
  4475. {
  4476. TTerminalManager * Manager = TTerminalManager::Instance();
  4477. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4478. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4479. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4480. }
  4481. }
  4482. //---------------------------------------------------------------------------
  4483. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4484. bool &CanClose)
  4485. {
  4486. if (Terminal != NULL)
  4487. {
  4488. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4489. {
  4490. unsigned int Result;
  4491. TMessageParams Params(mpNeverAskAgainCheck);
  4492. UnicodeString Message;
  4493. int Answers = qaOK | qaCancel;
  4494. if (TTerminalManager::Instance()->Count > 1)
  4495. {
  4496. if (!WinConfiguration->AutoSaveWorkspace)
  4497. {
  4498. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4499. Answers = qaYes | qaNo | qaCancel;
  4500. }
  4501. else
  4502. {
  4503. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4504. }
  4505. }
  4506. else
  4507. {
  4508. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4509. }
  4510. if (WinConfiguration->AutoSaveWorkspace)
  4511. {
  4512. Message =
  4513. FORMAT("%s\n\n%s", (Message,
  4514. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4515. }
  4516. SetFocus();
  4517. Result = MessageDialog(Message, qtConfirmation,
  4518. Answers, HELP_NONE, &Params);
  4519. if (Result == qaNeverAskAgain)
  4520. {
  4521. WinConfiguration->ConfirmClosingSession = false;
  4522. }
  4523. if (Result == qaNo)
  4524. {
  4525. CanClose = SaveWorkspace(true);
  4526. // note that the workspace will be saved redundatly again from FormClose
  4527. }
  4528. else
  4529. {
  4530. CanClose =
  4531. (Result == qaOK) ||
  4532. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4533. (Result == qaNeverAskAgain);
  4534. }
  4535. }
  4536. if (CanClose)
  4537. {
  4538. CanClose = CanCloseQueue();
  4539. }
  4540. }
  4541. if (CanClose)
  4542. {
  4543. CanClose =
  4544. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4545. FEditorManager->CloseExternalFilesWithoutProcess();
  4546. if (CanClose)
  4547. {
  4548. while (CanClose && (FLocalEditors->Count > 0))
  4549. {
  4550. int PrevCount = FLocalEditors->Count;
  4551. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4552. CanClose = (FLocalEditors->Count < PrevCount);
  4553. }
  4554. if (CanClose)
  4555. {
  4556. CanClose = FEditorManager->Empty(true);
  4557. if (!CanClose)
  4558. {
  4559. SetFocus();
  4560. CanClose =
  4561. (MessageDialog(
  4562. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4563. }
  4564. }
  4565. }
  4566. }
  4567. }
  4568. //---------------------------------------------------------------------------
  4569. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4570. {
  4571. TForm * Form = dynamic_cast<TForm *>(Sender);
  4572. DebugAssert(Form != NULL);
  4573. Form->Close();
  4574. }
  4575. //---------------------------------------------------------------------------
  4576. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4577. {
  4578. TForm * Form = dynamic_cast<TForm *>(Sender);
  4579. delete Form;
  4580. }
  4581. //---------------------------------------------------------------------------
  4582. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4583. {
  4584. while (FLocalEditors->Count > 0)
  4585. {
  4586. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4587. }
  4588. }
  4589. //---------------------------------------------------------------------------
  4590. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4591. TObject *Sender)
  4592. {
  4593. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4594. try
  4595. {
  4596. SetProperties(osRemote, FileList);
  4597. }
  4598. __finally
  4599. {
  4600. delete FileList;
  4601. }
  4602. }
  4603. //---------------------------------------------------------------------------
  4604. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4605. {
  4606. if (value)
  4607. {
  4608. if (Component == fcCustomCommandsBand)
  4609. {
  4610. UpdateCustomCommandsToolbar();
  4611. }
  4612. }
  4613. }
  4614. //---------------------------------------------------------------------------
  4615. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4616. {
  4617. TControl * Control = GetComponent(Component);
  4618. DebugAssert(Control);
  4619. bool Changed = (Control->Visible != value);
  4620. if (Changed)
  4621. {
  4622. ComponentShowing(Component, value);
  4623. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4624. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4625. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4626. if (value)
  4627. {
  4628. int RemainingHeight = Control->Parent->ClientHeight;
  4629. int RemainingWidth = Control->Parent->ClientWidth;
  4630. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4631. {
  4632. TControl * ChildControl = Control->Parent->Controls[i];
  4633. if (ChildControl->Visible)
  4634. {
  4635. switch (ChildControl->Align)
  4636. {
  4637. case alTop:
  4638. case alBottom:
  4639. RemainingHeight -= ChildControl->Height;
  4640. break;
  4641. case alLeft:
  4642. case alRight:
  4643. RemainingWidth -= ChildControl->Width;
  4644. break;
  4645. }
  4646. }
  4647. }
  4648. int Reserve = ScaleByTextHeight(this, 32);
  4649. // queue in explorer, trees in commander
  4650. if (Control->Height > RemainingHeight - Reserve)
  4651. {
  4652. Control->Height = RemainingHeight / 2;
  4653. }
  4654. if (Control->Width > RemainingWidth - Reserve)
  4655. {
  4656. Control->Width = RemainingWidth / 2;
  4657. }
  4658. }
  4659. Control->Visible = value;
  4660. if (WasFocused && Visible)
  4661. {
  4662. DirView(osCurrent)->SetFocus();
  4663. }
  4664. FixControlsPlacement();
  4665. }
  4666. }
  4667. //---------------------------------------------------------------------------
  4668. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4669. {
  4670. TControl * Control = GetComponent(Component);
  4671. if (Control == NULL)
  4672. {
  4673. return false;
  4674. }
  4675. else
  4676. {
  4677. return Control->Visible;
  4678. }
  4679. }
  4680. //---------------------------------------------------------------------------
  4681. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4682. {
  4683. bool Result;
  4684. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4685. {
  4686. Result = !IsUWP();
  4687. }
  4688. else
  4689. {
  4690. Result = true;
  4691. }
  4692. return Result;
  4693. }
  4694. //---------------------------------------------------------------------------
  4695. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4696. {
  4697. if (RemoteDirView->ItemFocused != NULL)
  4698. {
  4699. RemoteDirView->ItemFocused->MakeVisible(false);
  4700. }
  4701. QueueSplitter->Visible = QueuePanel->Visible;
  4702. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4703. }
  4704. //---------------------------------------------------------------------------
  4705. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4706. {
  4707. switch (Component) {
  4708. case fcStatusBar: return RemoteStatusBar;
  4709. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4710. case fcQueueView: return QueuePanel;
  4711. case fcQueueToolbar: return QueueDock;
  4712. case fcRemoteTree: return RemoteDriveView;
  4713. case fcSessionsTabs: return SessionsPageControl;
  4714. default: return NULL;
  4715. }
  4716. }
  4717. //---------------------------------------------------------------------------
  4718. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4719. TObject *Sender, TListColumn *Column, TPoint &Point)
  4720. {
  4721. DebugAssert(NonVisualDataModule && Column && Sender);
  4722. NonVisualDataModule->ListColumn = Column;
  4723. TPopupMenu * DirViewColumnMenu;
  4724. if (Sender == RemoteDirView)
  4725. {
  4726. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4727. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4728. (Column->Index == uvName);
  4729. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4730. (Column->Index == uvSize);
  4731. }
  4732. else
  4733. {
  4734. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4735. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4736. (Column->Index == dvName);
  4737. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4738. (Column->Index == dvSize);
  4739. }
  4740. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4741. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4742. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4743. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4744. }
  4745. //---------------------------------------------------------------------------
  4746. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4747. TObject *Sender, TListItem *Item, bool &AllowExec)
  4748. {
  4749. DoDirViewExecFile(Sender, Item, AllowExec);
  4750. }
  4751. //---------------------------------------------------------------------------
  4752. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4753. TListItem * Item, bool & AllowExec)
  4754. {
  4755. DebugAssert(Sender && Item && Configuration);
  4756. DebugAssert(AllowExec);
  4757. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4758. bool Remote = (ADirView == DirView(osRemote));
  4759. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4760. TOperationSide Side = (Remote ? osRemote : osLocal);
  4761. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4762. // on files only (not directories)
  4763. // and only when symlinks are resolved (apply to remote panel only)
  4764. if (!ADirView->ItemIsDirectory(Item) &&
  4765. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4766. {
  4767. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4768. !FForceExecution &&
  4769. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4770. {
  4771. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4772. {
  4773. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4774. UnlockWindow();
  4775. try
  4776. {
  4777. ExecuteFileOperation(
  4778. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4779. }
  4780. __finally
  4781. {
  4782. LockWindow();
  4783. }
  4784. AllowExec = false;
  4785. }
  4786. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4787. {
  4788. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4789. {
  4790. ExecuteFile(Side, efDefaultEditor);
  4791. AllowExec = false;
  4792. }
  4793. }
  4794. else
  4795. {
  4796. DebugFail();
  4797. }
  4798. }
  4799. // if we have not done anything special, fall back to default behavior
  4800. if (AllowExec)
  4801. {
  4802. if (Remote && !WinConfiguration->DisableOpenEdit)
  4803. {
  4804. ExecuteFile(osRemote, efShell);
  4805. AllowExec = false;
  4806. }
  4807. }
  4808. }
  4809. }
  4810. //---------------------------------------------------------------------------
  4811. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4812. {
  4813. return ((Side == osRemote) || (Side == osCurrent));
  4814. }
  4815. //---------------------------------------------------------------------------
  4816. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4817. {
  4818. DebugFail();
  4819. }
  4820. //---------------------------------------------------------------------------
  4821. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4822. {
  4823. DebugFail();
  4824. }
  4825. //---------------------------------------------------------------------------
  4826. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4827. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4828. {
  4829. TSynchronizeParamType Params;
  4830. Params.LocalDirectory = LocalDirectory;
  4831. Params.RemoteDirectory = RemoteDirectory;
  4832. int UnusedParams =
  4833. (GUIConfiguration->SynchronizeParams &
  4834. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4835. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4836. Params.Options = GUIConfiguration->SynchronizeOptions;
  4837. bool SaveSettings = false;
  4838. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4839. &DoSynchronizeTooManyDirectories);
  4840. DebugAssert(FSynchronizeController == NULL);
  4841. FSynchronizeController = &Controller;
  4842. bool Result;
  4843. try
  4844. {
  4845. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4846. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4847. int Options =
  4848. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4849. DebugAssert(FOnFeedSynchronizeError == NULL);
  4850. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4851. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4852. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4853. if (Result)
  4854. {
  4855. if (SaveSettings)
  4856. {
  4857. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4858. GUIConfiguration->SynchronizeOptions = Params.Options;
  4859. }
  4860. else
  4861. {
  4862. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4863. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4864. FLAGSET(Params.Options, soSynchronize))
  4865. {
  4866. GUIConfiguration->SynchronizeOptions =
  4867. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4868. soSynchronize;
  4869. }
  4870. }
  4871. LocalDirectory = Params.LocalDirectory;
  4872. RemoteDirectory = Params.RemoteDirectory;
  4873. }
  4874. }
  4875. __finally
  4876. {
  4877. FSynchronizeController = NULL;
  4878. DebugAssert(FOnFeedSynchronizeError == NULL);
  4879. FOnFeedSynchronizeError = NULL;
  4880. }
  4881. return Result;
  4882. }
  4883. //---------------------------------------------------------------------------
  4884. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4885. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4886. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4887. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4888. TSynchronizeOptions * Options, bool Full)
  4889. {
  4890. // Keep-up-to-date mode
  4891. if (Terminal->Status == ssOpened)
  4892. {
  4893. try
  4894. {
  4895. int PParams = Params.Params;
  4896. if (!Full)
  4897. {
  4898. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4899. if (FLAGSET(Params.Options, soRecurse))
  4900. {
  4901. PParams |= TTerminal::spSubDirs;
  4902. }
  4903. }
  4904. else
  4905. {
  4906. // if keepuptodate is non-recursive,
  4907. // full sync before has to be non-recursive as well
  4908. if (FLAGCLEAR(Params.Options, soRecurse))
  4909. {
  4910. PParams |= TTerminal::spNoRecurse;
  4911. }
  4912. }
  4913. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4914. PParams, Checklist, Options);
  4915. }
  4916. catch (Exception & E)
  4917. {
  4918. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4919. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4920. {
  4921. // noop, already logged in MoreMessageDialog
  4922. }
  4923. else
  4924. {
  4925. ShowExtendedExceptionEx(Terminal, &E);
  4926. throw;
  4927. }
  4928. }
  4929. }
  4930. }
  4931. //---------------------------------------------------------------------------
  4932. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4933. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4934. const UnicodeString ErrorStr)
  4935. {
  4936. if (!Directory.IsEmpty())
  4937. {
  4938. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4939. }
  4940. else
  4941. {
  4942. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4943. }
  4944. }
  4945. //---------------------------------------------------------------------------
  4946. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4947. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4948. {
  4949. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4950. {
  4951. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4952. }
  4953. else
  4954. {
  4955. TMessageParams Params(mpNeverAskAgainCheck);
  4956. unsigned int Result = MessageDialog(
  4957. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4958. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4959. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4960. {
  4961. MaxDirectories *= 2;
  4962. if (Result == qaNeverAskAgain)
  4963. {
  4964. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4965. }
  4966. }
  4967. else
  4968. {
  4969. Abort();
  4970. }
  4971. }
  4972. }
  4973. //---------------------------------------------------------------------------
  4974. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4975. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4976. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4977. TSynchronizeOptions * Options)
  4978. {
  4979. // Keep-up-to-date mode
  4980. DebugAssert(!FAutoOperation);
  4981. void * BatchStorage;
  4982. BatchStart(BatchStorage);
  4983. FAutoOperation = true;
  4984. bool AnyOperation = false;
  4985. TDateTime StartTime = Now();
  4986. TSynchronizeChecklist * AChecklist = NULL;
  4987. try
  4988. {
  4989. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4990. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4991. {
  4992. FSynchronizeProgressForm->Start();
  4993. }
  4994. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4995. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4996. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4997. Options);
  4998. SAFE_DESTROY(FSynchronizeProgressForm);
  4999. AnyOperation = (AChecklist->CheckedCount > 0);
  5000. if (AnyOperation)
  5001. {
  5002. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5003. CreateProgressForm(&SynchronizeProgress);
  5004. try
  5005. {
  5006. Terminal->SynchronizeApply(
  5007. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5008. SynchronizeProcessedItem, NULL, NULL, NULL);
  5009. }
  5010. __finally
  5011. {
  5012. DestroyProgressForm();
  5013. }
  5014. }
  5015. }
  5016. __finally
  5017. {
  5018. if (Checklist == NULL)
  5019. {
  5020. delete AChecklist;
  5021. }
  5022. else
  5023. {
  5024. *Checklist = AChecklist;
  5025. }
  5026. FAutoOperation = false;
  5027. SAFE_DESTROY(FSynchronizeProgressForm);
  5028. BatchEnd(BatchStorage);
  5029. ReloadLocalDirectory();
  5030. if (AnyOperation)
  5031. {
  5032. OperationComplete(StartTime);
  5033. }
  5034. }
  5035. }
  5036. //---------------------------------------------------------------------------
  5037. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5038. {
  5039. // can be called from command line
  5040. return Visible &&
  5041. ((DirView(osRemote)->SelCount > 0) ||
  5042. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5043. }
  5044. //---------------------------------------------------------------------------
  5045. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5046. {
  5047. LogSynchronizeEvent(Terminal, Message);
  5048. }
  5049. //---------------------------------------------------------------------------
  5050. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5051. int Params, TSynchronizeOptions & Options)
  5052. {
  5053. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5054. {
  5055. Options.Filter = new TStringList();
  5056. Options.Filter->CaseSensitive = false;
  5057. Options.Filter->Duplicates = Types::dupAccept;
  5058. if (DirView(osRemote)->SelCount > 0)
  5059. {
  5060. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5061. }
  5062. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5063. {
  5064. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5065. }
  5066. Options.Filter->Sort();
  5067. }
  5068. }
  5069. //---------------------------------------------------------------------------
  5070. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5071. {
  5072. TCopyParamType Defaults;
  5073. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5074. ConfigStorage->AccessMode = smRead;
  5075. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5076. {
  5077. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5078. }
  5079. ConfigStorage.reset(NULL);
  5080. std::unique_ptr<TStringList> Options(new TStringList());
  5081. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5082. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5083. UnicodeString Result;
  5084. if (Options->Count > 0)
  5085. {
  5086. Result =
  5087. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5088. }
  5089. return Result;
  5090. }
  5091. //---------------------------------------------------------------------------
  5092. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5093. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5094. {
  5095. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5096. UnicodeString AdditionalParams =
  5097. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5098. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5099. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5100. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5101. SerializeCopyParamForCommandLine(CopyParams)));
  5102. ExecuteNewInstance(SessionName, AdditionalParams);
  5103. }
  5104. //---------------------------------------------------------------------------
  5105. struct TSynchronizeParams
  5106. {
  5107. UnicodeString LocalDirectory;
  5108. UnicodeString RemoteDirectory;
  5109. TSynchronizeMode Mode;
  5110. TSynchronizeChecklist * Checklist;
  5111. int Params;
  5112. TCopyParamType * CopyParam;
  5113. TDateTime CollectElapsed;
  5114. TDateTime * StartTime;
  5115. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5116. };
  5117. //---------------------------------------------------------------------------
  5118. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5119. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5120. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5121. {
  5122. DebugAssert(!FAutoOperation);
  5123. void * BatchStorage;
  5124. BatchStart(BatchStorage);
  5125. FAutoOperation = true;
  5126. TFileOperationStatistics Statistics;
  5127. TDateTime Start = Now();
  5128. try
  5129. {
  5130. Params.OnProcessedItem = OnProcessedItem;
  5131. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5132. CreateProgressForm(&SynchronizeProgress);
  5133. Terminal->SynchronizeApply(
  5134. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5135. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5136. &Statistics);
  5137. }
  5138. __finally
  5139. {
  5140. Params.OnProcessedItem = NULL;
  5141. FAutoOperation = false;
  5142. DestroyProgressForm();
  5143. BatchEnd(BatchStorage);
  5144. ReloadLocalDirectory();
  5145. }
  5146. if (WinConfiguration->SynchronizeSummary)
  5147. {
  5148. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5149. // The statistics should be 0 anyway in this case
  5150. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5151. {
  5152. Message += L"\n";
  5153. if (Statistics.FilesUploaded > 0)
  5154. {
  5155. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5156. }
  5157. if (Statistics.FilesDownloaded > 0)
  5158. {
  5159. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5160. }
  5161. if (Statistics.FilesDeletedLocal > 0)
  5162. {
  5163. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5164. }
  5165. if (Statistics.FilesDeletedRemote > 0)
  5166. {
  5167. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5168. }
  5169. }
  5170. TDateTime Elapsed = (Now() - Start);
  5171. Message +=
  5172. L"\n" +
  5173. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5174. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5175. TMessageParams Params(mpNeverAskAgainCheck);
  5176. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5177. if (Result == qaNeverAskAgain)
  5178. {
  5179. WinConfiguration->SynchronizeSummary = false;
  5180. }
  5181. }
  5182. }
  5183. //---------------------------------------------------------------------------
  5184. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5185. {
  5186. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5187. {
  5188. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5189. }
  5190. // Not set in keep-up-to-date mode - Synchronize() method
  5191. if (Token != NULL)
  5192. {
  5193. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5194. if (Params.OnProcessedItem != NULL)
  5195. {
  5196. Params.OnProcessedItem(NULL, ChecklistItem);
  5197. }
  5198. }
  5199. }
  5200. //---------------------------------------------------------------------------
  5201. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5202. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5203. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5204. {
  5205. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5206. *Params.StartTime = Now();
  5207. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5208. }
  5209. //---------------------------------------------------------------------------
  5210. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5211. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5212. {
  5213. // terminal can be already closed (e.g. dropped connection)
  5214. if (Terminal != NULL)
  5215. {
  5216. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5217. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5218. }
  5219. }
  5220. //---------------------------------------------------------------------------
  5221. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5222. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5223. {
  5224. TAutoBatch AutoBatch(this);
  5225. TAutoFlag AutoOperationFlag(FAutoOperation);
  5226. if (Side == osRemote)
  5227. {
  5228. std::unique_ptr<TStrings> FileList(new TStringList());
  5229. FileList->AddObject(FileName, RemoteFile);
  5230. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5231. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5232. RemoteDirView->SaveSelection();
  5233. RemoteDirView->SaveSelectedNames();
  5234. try
  5235. {
  5236. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5237. }
  5238. catch(...)
  5239. {
  5240. RemoteDirView->DiscardSavedSelection();
  5241. throw;
  5242. }
  5243. RemoteDirView->RestoreSelection();
  5244. }
  5245. else if (DebugAlwaysTrue(Side == osLocal))
  5246. {
  5247. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5248. {
  5249. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5250. }
  5251. UnicodeString Directory = ExtractFileDir(FileName);
  5252. ReloadLocalDirectory(Directory);
  5253. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5254. if (!SamePaths(Directory, NewDirectory))
  5255. {
  5256. ReloadLocalDirectory(NewDirectory);
  5257. }
  5258. }
  5259. }
  5260. //---------------------------------------------------------------------------
  5261. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5262. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5263. const TCopyParamType * CopyParams)
  5264. {
  5265. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5266. UnicodeString AdditionalParams =
  5267. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5268. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5269. LocalDirectory, RemoteDirectory, Mode, Params,
  5270. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5271. SerializeCopyParamForCommandLine(CopyParams)));
  5272. ExecuteNewInstance(SessionName, AdditionalParams);
  5273. }
  5274. //---------------------------------------------------------------------------
  5275. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5276. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5277. TSynchronizeMode & Mode, int Params, bool & SaveMode, bool UseDefaults)
  5278. {
  5279. int Result;
  5280. bool SaveSettings = false;
  5281. int Options =
  5282. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5283. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5284. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5285. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5286. bool Continue =
  5287. UseDefaults ||
  5288. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5289. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow);
  5290. if (Continue)
  5291. {
  5292. Configuration->Usage->Inc(L"Synchronizations");
  5293. UpdateCopyParamCounters(CopyParam);
  5294. TSynchronizeOptions SynchronizeOptions;
  5295. GetSynchronizeOptions(Params, SynchronizeOptions);
  5296. if (SaveSettings)
  5297. {
  5298. GUIConfiguration->SynchronizeParams = Params;
  5299. }
  5300. else
  5301. {
  5302. SaveMode = false;
  5303. }
  5304. TDateTime StartTime = Now();
  5305. TSynchronizeChecklist * Checklist = NULL;
  5306. try
  5307. {
  5308. DebugAssert(!FAutoOperation);
  5309. FAutoOperation = true;
  5310. try
  5311. {
  5312. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5313. FSynchronizeProgressForm->Start();
  5314. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5315. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5316. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5317. &SynchronizeOptions);
  5318. }
  5319. __finally
  5320. {
  5321. FAutoOperation = false;
  5322. SAFE_DESTROY(FSynchronizeProgressForm);
  5323. }
  5324. TSynchronizeParams SynchronizeParams;
  5325. SynchronizeParams.LocalDirectory = LocalDirectory;
  5326. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5327. SynchronizeParams.Mode = Mode;
  5328. SynchronizeParams.CopyParam = &CopyParam;
  5329. SynchronizeParams.Params = Params;
  5330. SynchronizeParams.Checklist = Checklist;
  5331. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5332. SynchronizeParams.StartTime = &StartTime;
  5333. SynchronizeParams.OnProcessedItem = NULL;
  5334. Result = Checklist->Count;
  5335. if (Checklist->Count > 0)
  5336. {
  5337. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5338. {
  5339. if (!DoSynchronizeChecklistDialog(
  5340. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5341. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5342. {
  5343. Result = -1;
  5344. }
  5345. }
  5346. else
  5347. {
  5348. FullSynchronize(SynchronizeParams, NULL, NULL);
  5349. }
  5350. }
  5351. else
  5352. {
  5353. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5354. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5355. }
  5356. }
  5357. __finally
  5358. {
  5359. delete Checklist;
  5360. }
  5361. OperationComplete(StartTime);
  5362. }
  5363. else
  5364. {
  5365. Result = -1;
  5366. }
  5367. return Result;
  5368. }
  5369. //---------------------------------------------------------------------------
  5370. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5371. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5372. bool & Continue, bool Collect)
  5373. {
  5374. if (Collect)
  5375. {
  5376. DebugAssert(FSynchronizeProgressForm != NULL);
  5377. if (!FSynchronizeProgressForm->Started)
  5378. {
  5379. FSynchronizeProgressForm->Start();
  5380. }
  5381. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5382. }
  5383. else
  5384. {
  5385. DebugAssert(FSynchronizeProgressForm == NULL);
  5386. DebugAssert(FProgressForm != NULL);
  5387. }
  5388. }
  5389. //---------------------------------------------------------------------------
  5390. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5391. {
  5392. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5393. TRemoteFile * File = NULL;
  5394. Terminal->ReadFile(FullFileName, File);
  5395. if (File != NULL)
  5396. {
  5397. std::unique_ptr<TRemoteFile> FileOwner(File);
  5398. TAutoFlag Flag(FStandaloneEditing);
  5399. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5400. Application->ShowMainForm = false;
  5401. Application->Run();
  5402. }
  5403. }
  5404. //---------------------------------------------------------------------------
  5405. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5406. {
  5407. DebugFail();
  5408. }
  5409. //---------------------------------------------------------------------------
  5410. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5411. {
  5412. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5413. SessionData->Assign(Terminal->SessionData);
  5414. UpdateSessionData(SessionData.get());
  5415. Terminal->UpdateSessionCredentials(SessionData.get());
  5416. if (!Terminal->SessionData->HasSessionName())
  5417. {
  5418. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5419. // once IsWorkspace is cleared
  5420. SessionData->Name = UnicodeString();
  5421. SessionData->IsWorkspace = false;
  5422. }
  5423. return SessionData.release();
  5424. }
  5425. //---------------------------------------------------------------------------
  5426. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5427. {
  5428. TSessionData * SessionData = CloneCurrentSessionData();
  5429. try
  5430. {
  5431. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5432. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5433. }
  5434. __finally
  5435. {
  5436. delete SessionData;
  5437. }
  5438. }
  5439. //---------------------------------------------------------------------------
  5440. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5441. {
  5442. TTerminalManager * Manager = TTerminalManager::Instance();
  5443. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5444. if (DebugAlwaysTrue(Terminal != NULL))
  5445. {
  5446. // Update (Managed)Terminal->StateData
  5447. UpdateTerminal(Terminal);
  5448. }
  5449. Manager->SaveWorkspace(DataList.get());
  5450. return DataList.release();
  5451. }
  5452. //---------------------------------------------------------------------------
  5453. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5454. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5455. {
  5456. WinConfiguration->LastWorkspace = Name;
  5457. if (!SavePasswords)
  5458. {
  5459. for (int Index = 0; Index < DataList->Count; Index++)
  5460. {
  5461. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5462. if (SessionData->Link.IsEmpty())
  5463. {
  5464. SessionData->ClearSessionPasswords();
  5465. }
  5466. }
  5467. }
  5468. StoredSessions->NewWorkspace(Name, DataList);
  5469. // modified only
  5470. StoredSessions->Save(false, Explicit);
  5471. }
  5472. //---------------------------------------------------------------------------
  5473. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5474. {
  5475. return
  5476. DefaultStr(WinConfiguration->LastWorkspace,
  5477. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5478. }
  5479. //---------------------------------------------------------------------------
  5480. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5481. {
  5482. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5483. bool AnyNonStoredSessionWithPassword = false;
  5484. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5485. bool AllNonStoredSessionsAnonymous = true;
  5486. TTerminalManager * Manager = TTerminalManager::Instance();
  5487. for (int Index = 0; Index < DataList->Count; Index++)
  5488. {
  5489. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5490. if (SessionData->Link.IsEmpty())
  5491. {
  5492. if (SessionData->HasAnySessionPassword())
  5493. {
  5494. AnyNonStoredSessionWithPassword = true;
  5495. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5496. {
  5497. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5498. }
  5499. }
  5500. if (!SameText(SessionData->UserName, AnonymousUserName))
  5501. {
  5502. AllNonStoredSessionsAnonymous = false;
  5503. }
  5504. }
  5505. }
  5506. bool SavePasswords;
  5507. bool * PSavePasswords;
  5508. bool NotRecommendedSavingPasswords =
  5509. !CustomWinConfiguration->UseMasterPassword &&
  5510. !AllNonStoredSessionsAnonymous;
  5511. if (Configuration->DisablePasswordStoring ||
  5512. !AnyNonStoredSessionWithPassword)
  5513. {
  5514. PSavePasswords = NULL;
  5515. SavePasswords = false;
  5516. }
  5517. else
  5518. {
  5519. PSavePasswords = &SavePasswords;
  5520. SavePasswords =
  5521. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5522. !NotRecommendedSavingPasswords;
  5523. }
  5524. UnicodeString Name = WorkspaceName();
  5525. bool CreateShortcut = false;
  5526. bool Result =
  5527. DoSaveWorkspaceDialog(
  5528. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5529. EnableAutoSave);
  5530. if (Result)
  5531. {
  5532. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5533. if (CreateShortcut)
  5534. {
  5535. TOperationVisualizer Visualizer;
  5536. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5537. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5538. }
  5539. if (EnableAutoSave)
  5540. {
  5541. WinConfiguration->AutoSaveWorkspace = true;
  5542. WinConfiguration->AutoWorkspace = Name;
  5543. if (PSavePasswords != NULL)
  5544. {
  5545. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5546. }
  5547. }
  5548. }
  5549. return Result;
  5550. }
  5551. //---------------------------------------------------------------------------
  5552. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5553. {
  5554. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5555. DebugAssert(ManagedTerminal != NULL);
  5556. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5557. if (WinConfiguration->PreservePanelState)
  5558. {
  5559. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5560. }
  5561. UpdateSessionData(ManagedTerminal->StateData);
  5562. }
  5563. //---------------------------------------------------------------------------
  5564. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5565. {
  5566. // Keep in sync with TSessionData::CopyStateData
  5567. DebugAssert(Data != NULL);
  5568. // This is inconsistent with how color (for example) is handled.
  5569. Data->Name = Terminal->SessionData->Name;
  5570. // cannot use RemoteDirView->Path, because it is empty if connection
  5571. // was already closed
  5572. // also only peek, we may not be connected at all atm
  5573. // so make sure we do not try retrieving current directory from the server
  5574. // (particularly with FTP)
  5575. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5576. if (!ACurrentDirectory.IsEmpty())
  5577. {
  5578. Data->RemoteDirectory = ACurrentDirectory;
  5579. }
  5580. Data->Color = SessionColor;
  5581. }
  5582. //---------------------------------------------------------------------------
  5583. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5584. {
  5585. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5586. DebugAssert(Toolbar != NULL);
  5587. for (int i = 0; i < Toolbar->Items->Count; i++)
  5588. {
  5589. TTBXCustomDropDownItem * DropDownItem;
  5590. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5591. if (DropDownItem != NULL)
  5592. {
  5593. ToolbarItemResize(DropDownItem,
  5594. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5595. Toolbar->NonClientWidth);
  5596. break;
  5597. }
  5598. }
  5599. }
  5600. //---------------------------------------------------------------------------
  5601. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5602. {
  5603. Item->EditWidth = Width;
  5604. }
  5605. //---------------------------------------------------------------------------
  5606. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5607. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5608. {
  5609. for (int i = 0; i < Toolbar->Items->Count; i++)
  5610. {
  5611. TTBXCustomDropDownItem * DropDownItem;
  5612. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5613. if (DropDownItem != NULL)
  5614. {
  5615. ASize.x -= DropDownItem->EditWidth;
  5616. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5617. }
  5618. }
  5619. }
  5620. //---------------------------------------------------------------------------
  5621. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5622. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5623. {
  5624. if (WinConfiguration->DDWarnLackOfTempSpace)
  5625. {
  5626. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5627. if (!ADrive.IsEmpty())
  5628. {
  5629. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5630. DebugAssert(RequiredSpace >= 0);
  5631. __int64 RequiredWithReserve;
  5632. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5633. if (FreeSpace < RequiredWithReserve)
  5634. {
  5635. unsigned int Result;
  5636. TMessageParams Params(mpNeverAskAgainCheck);
  5637. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5638. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5639. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5640. if (Result == qaNeverAskAgain)
  5641. {
  5642. WinConfiguration->DDWarnLackOfTempSpace = false;
  5643. }
  5644. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5645. }
  5646. }
  5647. }
  5648. }
  5649. //---------------------------------------------------------------------------
  5650. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5651. {
  5652. DoOpenDirectoryDialog(odAddBookmark, Side);
  5653. }
  5654. //---------------------------------------------------------------------------
  5655. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5656. {
  5657. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5658. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5659. try
  5660. {
  5661. TCustomDirView * DView = DirView(Side);
  5662. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5663. {
  5664. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5665. }
  5666. }
  5667. catch (...)
  5668. {
  5669. delete VisitedDirectories;
  5670. throw;
  5671. }
  5672. return VisitedDirectories;
  5673. }
  5674. //---------------------------------------------------------------------------
  5675. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5676. TOpenDirectoryMode Mode, TOperationSide Side)
  5677. {
  5678. bool Continue = true;
  5679. if (Mode == odAddBookmark)
  5680. {
  5681. TMessageParams Params(mpNeverAskAgainCheck);
  5682. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5683. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5684. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5685. unsigned int Answer =
  5686. MessageDialog(Message,
  5687. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5688. if (Answer == qaNeverAskAgain)
  5689. {
  5690. Continue = true;
  5691. WinConfiguration->UseSharedBookmarks = true;
  5692. }
  5693. else if (Answer == qaOK)
  5694. {
  5695. Continue = true;
  5696. WinConfiguration->UseSharedBookmarks = false;
  5697. }
  5698. else
  5699. {
  5700. Continue = false;
  5701. }
  5702. }
  5703. if (Continue)
  5704. {
  5705. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5706. try
  5707. {
  5708. UnicodeString Name = DirView(Side)->PathName;
  5709. bool Repeat;
  5710. do
  5711. {
  5712. Repeat = false;
  5713. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5714. // do not allow switching to location profiles,
  5715. // if we are not connected
  5716. HasDirView[osLocal] && (Terminal != NULL)))
  5717. {
  5718. TWindowLock Lock(this);
  5719. if (!TryOpenDirectory(Side, Name))
  5720. {
  5721. Repeat = true;
  5722. Mode = odBrowse;
  5723. }
  5724. }
  5725. }
  5726. while (Repeat);
  5727. }
  5728. __finally
  5729. {
  5730. delete VisitedDirectories;
  5731. }
  5732. }
  5733. }
  5734. //---------------------------------------------------------------------------
  5735. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5736. {
  5737. DebugAssert(!FTerminal->CommandSessionOpened);
  5738. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5739. }
  5740. //---------------------------------------------------------------------------
  5741. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5742. {
  5743. bool Result = FTerminal->IsCapable[Capability] ||
  5744. FTerminal->CommandSessionOpened;
  5745. if (!Result)
  5746. {
  5747. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5748. if (!GUIConfiguration->ConfirmCommandSession)
  5749. {
  5750. Result = true;
  5751. }
  5752. else
  5753. {
  5754. TMessageParams Params(mpNeverAskAgainCheck);
  5755. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5756. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5757. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5758. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5759. if (Answer == qaNeverAskAgain)
  5760. {
  5761. GUIConfiguration->ConfirmCommandSession = false;
  5762. Result = true;
  5763. }
  5764. else if (Answer == qaOK)
  5765. {
  5766. Result = true;
  5767. }
  5768. }
  5769. if (Result)
  5770. {
  5771. Result = CommandSessionFallback();
  5772. }
  5773. }
  5774. return Result;
  5775. }
  5776. //---------------------------------------------------------------------------
  5777. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5778. {
  5779. if (EnsureCommandSessionFallback(fcAnyCommand))
  5780. {
  5781. DoConsoleDialog(Terminal, Command);
  5782. }
  5783. }
  5784. //---------------------------------------------------------------------------
  5785. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5786. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5787. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5788. {
  5789. if (IsFileControl(DropSourceControl, osRemote) &&
  5790. (FDDExtMapFile != NULL))
  5791. {
  5792. Accept = true;
  5793. }
  5794. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5795. DebugAssert(FDDTargetControl != NULL);
  5796. }
  5797. //---------------------------------------------------------------------------
  5798. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5799. _di_IDataObject DataObj, int KeyState,
  5800. const TPoint & Point, int & Effect, bool & Accept)
  5801. {
  5802. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5803. }
  5804. //---------------------------------------------------------------------------
  5805. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5806. _di_IDataObject DataObj, int KeyState,
  5807. const TPoint & Point, int & Effect, bool & Accept)
  5808. {
  5809. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5810. }
  5811. //---------------------------------------------------------------------------
  5812. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5813. TObject * Sender)
  5814. {
  5815. DebugUsedParam(Sender);
  5816. DebugAssert(FDDTargetControl == Sender);
  5817. FDDTargetControl = NULL;
  5818. }
  5819. //---------------------------------------------------------------------------
  5820. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5821. {
  5822. FileControlDDDragLeave(SessionsPageControl);
  5823. }
  5824. //---------------------------------------------------------------------------
  5825. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5826. {
  5827. FileControlDDDragLeave(QueueView3);
  5828. }
  5829. //---------------------------------------------------------------------------
  5830. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5831. {
  5832. DebugAssert(GetSide(Side) == osRemote);
  5833. DebugUsedParam(Side);
  5834. bool Edit = false;
  5835. TRemoteFile * File = NULL;
  5836. UnicodeString FileName;
  5837. UnicodeString PointTo;
  5838. bool SymbolicLink = true;
  5839. if (RemoteDirView->ItemFocused)
  5840. {
  5841. DebugAssert(RemoteDirView->ItemFocused->Data);
  5842. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5843. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5844. if (Edit)
  5845. {
  5846. FileName = File->FileName;
  5847. PointTo = File->LinkTo;
  5848. }
  5849. else
  5850. {
  5851. PointTo = File->FileName;
  5852. }
  5853. }
  5854. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5855. Terminal->IsCapable[fcHardLink]))
  5856. {
  5857. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5858. if (Edit)
  5859. {
  5860. DebugAssert(File->FileName == FileName);
  5861. int Params = dfNoRecursive;
  5862. Terminal->ExceptionOnFail = true;
  5863. try
  5864. {
  5865. Terminal->DeleteFile(L"", File, &Params);
  5866. }
  5867. __finally
  5868. {
  5869. Terminal->ExceptionOnFail = false;
  5870. }
  5871. }
  5872. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5873. }
  5874. }
  5875. //---------------------------------------------------------------------------
  5876. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5877. {
  5878. return
  5879. (Terminal != NULL) &&
  5880. ((GetSide(Side) != osRemote) ||
  5881. (Terminal->ResolvingSymlinks &&
  5882. Terminal->IsCapable[fcSymbolicLink]));
  5883. }
  5884. //---------------------------------------------------------------------------
  5885. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5886. {
  5887. return
  5888. (FCurrentSide == osRemote) &&
  5889. (RemoteDirView->ItemFocused != NULL) &&
  5890. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5891. Terminal->SessionData->ResolveSymlinks;
  5892. }
  5893. //---------------------------------------------------------------------------
  5894. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5895. {
  5896. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5897. FForceExecution = true;
  5898. try
  5899. {
  5900. DirView(osCurrent)->ExecuteCurrentFile();
  5901. }
  5902. __finally
  5903. {
  5904. FForceExecution = false;
  5905. }
  5906. }
  5907. //---------------------------------------------------------------------------
  5908. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5909. {
  5910. TEditorData ExternalEditor;
  5911. ExternalEditor.Editor = edExternal;
  5912. bool Remember = false;
  5913. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5914. (GetSide(osCurrent) == osRemote)))
  5915. {
  5916. if (Remember)
  5917. {
  5918. TEditorList * EditorList = new TEditorList;
  5919. try
  5920. {
  5921. *EditorList = *WinConfiguration->EditorList;
  5922. bool Found = false;
  5923. int i = 0;
  5924. while (!Found && (i < EditorList->Count))
  5925. {
  5926. const TEditorPreferences * Editor = EditorList->Editors[i];
  5927. if ((Editor->Data->Editor == edExternal) &&
  5928. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5929. {
  5930. Found = true;
  5931. }
  5932. i++;
  5933. }
  5934. if (!Found)
  5935. {
  5936. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5937. WinConfiguration->EditorList = EditorList;
  5938. }
  5939. }
  5940. __finally
  5941. {
  5942. delete EditorList;
  5943. }
  5944. }
  5945. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5946. }
  5947. }
  5948. //---------------------------------------------------------------------------
  5949. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5950. {
  5951. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5952. if (FFileFindTerminal == Sender)
  5953. {
  5954. FFileFindTerminal = NULL;
  5955. HideFileFindDialog();
  5956. }
  5957. if (FClipboardTerminal == Sender)
  5958. {
  5959. ClipboardClear(); // implies ClipboardStop
  5960. }
  5961. }
  5962. //---------------------------------------------------------------------------
  5963. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5964. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5965. {
  5966. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5967. DebugAssert(Terminal != NULL);
  5968. if (Data->Terminal == Terminal)
  5969. {
  5970. Data->Terminal = NULL;
  5971. }
  5972. }
  5973. //---------------------------------------------------------------------------
  5974. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5975. {
  5976. UpdateControls();
  5977. SessionColor = TColor(0);
  5978. UpdateRemotePathComboBox(false);
  5979. NeedSession(true);
  5980. }
  5981. //---------------------------------------------------------------------------
  5982. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5983. {
  5984. try
  5985. {
  5986. try
  5987. {
  5988. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5989. }
  5990. __finally
  5991. {
  5992. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5993. (!Terminal || !Terminal->Active))
  5994. {
  5995. TerminateApplication();
  5996. }
  5997. }
  5998. }
  5999. catch (EAbort &)
  6000. {
  6001. // swallow
  6002. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6003. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6004. }
  6005. }
  6006. //---------------------------------------------------------------------------
  6007. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6008. {
  6009. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6010. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6011. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6012. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6013. try
  6014. {
  6015. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6016. // Clear the design time unthemed tab
  6017. ((SessionsPageControl->PageCount > 0) &&
  6018. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6019. {
  6020. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6021. }
  6022. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6023. {
  6024. TThemeTabSheet * TabSheet;
  6025. if (Index >= SessionsPageControl->PageCount)
  6026. {
  6027. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6028. TabSheet->PageControl = SessionsPageControl;
  6029. }
  6030. else
  6031. {
  6032. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6033. }
  6034. bool IsSessionTab = (Index < TerminalList->Count);
  6035. if (IsSessionTab)
  6036. {
  6037. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6038. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6039. UpdateSessionTab(TabSheet);
  6040. }
  6041. else
  6042. {
  6043. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6044. TabSheet->Tag = 0; // not really needed
  6045. // We know that we are at the last page, sotherwise we could not call this (it assumes that new session tab is the last one)
  6046. UpdateNewSessionTab();
  6047. }
  6048. TabSheet->ShowCloseButton = IsSessionTab;
  6049. }
  6050. }
  6051. __finally
  6052. {
  6053. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6054. }
  6055. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6056. }
  6057. //---------------------------------------------------------------------------
  6058. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6059. {
  6060. DoTerminalListChanged();
  6061. }
  6062. //---------------------------------------------------------------------------
  6063. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6064. {
  6065. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6066. DebugAssert(TabSheet->ImageIndex == 0);
  6067. TabSheet->Caption =
  6068. WinConfiguration->SelectiveToolbarText ?
  6069. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6070. UnicodeString();
  6071. }
  6072. //---------------------------------------------------------------------------
  6073. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6074. {
  6075. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  6076. }
  6077. //---------------------------------------------------------------------------
  6078. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6079. {
  6080. if (DebugAlwaysTrue(TabSheet != NULL))
  6081. {
  6082. TManagedTerminal * ManagedTerminal =
  6083. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6084. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6085. {
  6086. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6087. TabSheet->ImageIndex = AddSessionColor(Color);
  6088. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6089. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6090. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6091. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6092. {
  6093. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6094. }
  6095. }
  6096. }
  6097. }
  6098. //---------------------------------------------------------------------------
  6099. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6100. {
  6101. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6102. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6103. bool Result = (Terminal != NULL);
  6104. if (Result)
  6105. {
  6106. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6107. }
  6108. else
  6109. {
  6110. try
  6111. {
  6112. NewSession(false);
  6113. }
  6114. __finally
  6115. {
  6116. DoTerminalListChanged();
  6117. }
  6118. FSessionsPageControlNewSessionTime = Now();
  6119. }
  6120. return Result;
  6121. }
  6122. //---------------------------------------------------------------------------
  6123. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6124. {
  6125. SessionTabSwitched();
  6126. }
  6127. //---------------------------------------------------------------------------
  6128. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6129. {
  6130. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6131. DebugAssert(TransferList != NULL);
  6132. UnicodeString Name;
  6133. if (TransferList->ItemIndex <= 0)
  6134. {
  6135. Name = L"";
  6136. }
  6137. else
  6138. {
  6139. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6140. }
  6141. if (FCopyParamAutoSelected.IsEmpty())
  6142. {
  6143. // if previous preset was not autoselected, make new preset the "default"
  6144. FCopyParamDefault = Name;
  6145. }
  6146. GUIConfiguration->CopyParamCurrent = Name;
  6147. }
  6148. //---------------------------------------------------------------------------
  6149. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6150. {
  6151. // sanity check
  6152. bool ExistingPreset =
  6153. (FTransferListHoverIndex >= 0) &&
  6154. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6155. DebugAssert(ExistingPreset);
  6156. if (ExistingPreset)
  6157. {
  6158. HDC DC = GetDC(0);
  6159. TCanvas * Canvas = new TCanvas();
  6160. try
  6161. {
  6162. Canvas->Handle = DC;
  6163. Canvas->Font = GetToolbarFont(this);
  6164. UnicodeString Name;
  6165. if (FTransferListHoverIndex == 0)
  6166. {
  6167. Name = L"";
  6168. }
  6169. else
  6170. {
  6171. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6172. }
  6173. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6174. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6175. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6176. static_cast<TObject*>(GetComponent(fcTransferList)));
  6177. UnicodeString InfoStr =
  6178. GUIConfiguration->CopyParamPreset[Name].
  6179. GetInfoStr(L"; ",
  6180. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6181. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6182. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6183. {
  6184. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6185. {
  6186. InfoStr.SetLength(InfoStr.Length() - 1);
  6187. }
  6188. InfoStr += Ellipsis;
  6189. }
  6190. // UpdateCaption does not cause invalidation of whole submenu, while
  6191. // setting Caption property does.
  6192. // also it probably does not resize the label, even if necessary
  6193. // (we do not want that anyway)
  6194. TransferLabel->UpdateCaption(InfoStr);
  6195. }
  6196. __finally
  6197. {
  6198. Canvas->Handle = NULL;
  6199. ReleaseDC(0, DC);
  6200. delete Canvas;
  6201. }
  6202. }
  6203. }
  6204. //---------------------------------------------------------------------------
  6205. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6206. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6207. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6208. {
  6209. if (FTransferListHoverIndex != AHoverIndex)
  6210. {
  6211. FTransferListHoverIndex = AHoverIndex;
  6212. UpdateTransferLabel();
  6213. }
  6214. }
  6215. //---------------------------------------------------------------------------
  6216. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6217. {
  6218. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6219. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6220. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6221. {
  6222. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6223. {
  6224. OpenDirectory(GetSide(osCurrent));
  6225. Message.Result = 1;
  6226. }
  6227. else
  6228. {
  6229. TForm::Dispatch(&Message);
  6230. }
  6231. }
  6232. else
  6233. {
  6234. TForm::Dispatch(&Message);
  6235. }
  6236. }
  6237. //---------------------------------------------------------------------------
  6238. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6239. {
  6240. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6241. if ((FIgnoreNextDialogChar != 0) &&
  6242. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6243. {
  6244. Message.Result = 1;
  6245. }
  6246. else
  6247. {
  6248. TForm::Dispatch(&Message);
  6249. }
  6250. FIgnoreNextDialogChar = 0;
  6251. }
  6252. //---------------------------------------------------------------------------
  6253. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6254. {
  6255. // The four low-order bits are used internally by Windows
  6256. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6257. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6258. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6259. // 61730 - restore thru double click - undocumented
  6260. // 61490 - maximize thru double click - undocumented
  6261. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6262. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6263. {
  6264. SysResizing(Cmd);
  6265. }
  6266. TForm::Dispatch(&Message);
  6267. }
  6268. //---------------------------------------------------------------------------
  6269. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6270. {
  6271. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6272. // Also there no point returning TRUE as we are not able to
  6273. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6274. // Hence the process termination might be safer :)
  6275. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6276. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6277. {
  6278. Message.Result = FALSE;
  6279. }
  6280. else
  6281. {
  6282. Message.Result = TRUE;
  6283. }
  6284. // Do not call default handling as that triggers OnCloseQuery,
  6285. // where our implementation will popup confirmation dialogs, what we do not want,
  6286. // as per Vista guidelines:
  6287. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6288. }
  6289. //---------------------------------------------------------------------------
  6290. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6291. {
  6292. if (Message.EndSession && IsApplicationMinimized())
  6293. {
  6294. // WORKAROUND
  6295. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6296. // when it receives WM_ENDSESSION,
  6297. // but that sometimes (particularly when application is minimized) cause crashes.
  6298. // Crash popups message that blocks log off.
  6299. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6300. // so we call ExitProcess() immediately, not even trying to cleanup.
  6301. // It still causes beep, so there's likely some popup, but it does not block
  6302. // log off.
  6303. ExitProcess(0);
  6304. }
  6305. TForm::Dispatch(&Message);
  6306. }
  6307. #ifdef _DEBUG
  6308. //---------------------------------------------------------------------------
  6309. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6310. {
  6311. TForm::Dispatch(&Message);
  6312. }
  6313. #endif
  6314. //---------------------------------------------------------------------------
  6315. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6316. {
  6317. }
  6318. //---------------------------------------------------------------------------
  6319. void __fastcall TCustomScpExplorerForm::DoShow()
  6320. {
  6321. // only now are the controls resized finally, so the size constraints
  6322. // will not conflict with possibly very small window size
  6323. RestoreFormParams();
  6324. // This indicates that the form was rescaled after parameters were loaded and
  6325. // as most probably the parameters were actually saved with the current form scaling
  6326. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6327. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6328. // and no rounding problems will occur.
  6329. // See also RestoreForm.
  6330. // (we should be safe to call RestoreParams unconditionally)
  6331. if (PixelsPerInch != Screen->PixelsPerInch)
  6332. {
  6333. RestoreParams();
  6334. }
  6335. FixControlsPlacement();
  6336. if (Position == poDefaultPosOnly)
  6337. {
  6338. CutFormToDesktop(this);
  6339. }
  6340. TForm::DoShow();
  6341. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6342. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6343. if (Terminal == NULL)
  6344. {
  6345. StartingDisconnected();
  6346. }
  6347. UpdatePixelsPerInchMainWindowCounter();
  6348. FShowing = true;
  6349. }
  6350. //---------------------------------------------------------------------------
  6351. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6352. {
  6353. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6354. }
  6355. //---------------------------------------------------------------------------
  6356. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6357. {
  6358. DoTerminalListChanged();
  6359. }
  6360. //---------------------------------------------------------------------------
  6361. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6362. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6363. TNotifyEvent OnBalloonClick, TObject * UserData)
  6364. {
  6365. bool Do;
  6366. UnicodeString Message;
  6367. if (E == NULL)
  6368. {
  6369. Message = Str;
  6370. Do = true;
  6371. }
  6372. else
  6373. {
  6374. Do = ExceptionMessage(E, Message);
  6375. }
  6376. if (Do && WinConfiguration->BalloonNotifications)
  6377. {
  6378. UnicodeString Title;
  6379. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6380. {
  6381. Message = TrimLeft(Message);
  6382. }
  6383. else
  6384. {
  6385. Message = UnformatMessage(Message);
  6386. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6387. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6388. Title = LoadResourceString(Captions[Type]);
  6389. if (Terminal != NULL)
  6390. {
  6391. Title = FORMAT(L"%s - %s",
  6392. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6393. }
  6394. }
  6395. if (Seconds == 0)
  6396. {
  6397. Seconds = WinConfiguration->NotificationsTimeout;
  6398. }
  6399. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6400. }
  6401. }
  6402. //---------------------------------------------------------------------------
  6403. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6404. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6405. UnicodeString HelpKeyword, const TMessageParams * Params,
  6406. TTerminal * Terminal)
  6407. {
  6408. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6409. (FOnFeedSynchronizeError != NULL)) &&
  6410. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6411. {
  6412. if (FOnFeedSynchronizeError != NULL)
  6413. {
  6414. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6415. }
  6416. else
  6417. {
  6418. DebugAssert(FErrorList != NULL);
  6419. TStringList * MoreMessagesCopy = NULL;
  6420. if (MoreMessages)
  6421. {
  6422. MoreMessagesCopy = new TStringList();
  6423. MoreMessagesCopy->Assign(MoreMessages);
  6424. }
  6425. FErrorList->AddObject(Message, MoreMessagesCopy);
  6426. }
  6427. PopupTrayBalloon(Terminal, Message, Type);
  6428. return ContinueAnswer(Answers);
  6429. }
  6430. else
  6431. {
  6432. bool UseBalloon = FTrayIcon->Visible;
  6433. if (UseBalloon)
  6434. {
  6435. PopupTrayBalloon(Terminal, Message, Type);
  6436. }
  6437. unsigned int Result;
  6438. try
  6439. {
  6440. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6441. }
  6442. __finally
  6443. {
  6444. // cancel only balloon we popped up, otherwise we may cancel notification
  6445. // balloon that was there before the message dialog
  6446. // (such as "dd confirmation opt in balloon)
  6447. if (UseBalloon)
  6448. {
  6449. FTrayIcon->CancelBalloon();
  6450. }
  6451. }
  6452. return Result;
  6453. }
  6454. }
  6455. //---------------------------------------------------------------------------
  6456. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6457. TTerminal * Terminal, Exception * E)
  6458. {
  6459. if (FTrayIcon->Visible)
  6460. {
  6461. PopupTrayBalloon(Terminal, L"", qtError, E);
  6462. }
  6463. // particularly prevent opening new session from jump list,
  6464. // while exception is shown
  6465. NonVisualDataModule->StartBusy();
  6466. try
  6467. {
  6468. ShowExtendedExceptionEx(Terminal, E);
  6469. }
  6470. __finally
  6471. {
  6472. NonVisualDataModule->EndBusy();
  6473. FTrayIcon->CancelBalloon();
  6474. }
  6475. }
  6476. //---------------------------------------------------------------------------
  6477. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6478. {
  6479. // cannot rely on active page being page for active terminal,
  6480. // as it can happen that active page is the "new session" page
  6481. // (e.g. when reconnecting active terminal, while login dialog
  6482. // invoked from "new session" page is modal)
  6483. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6484. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6485. }
  6486. //---------------------------------------------------------------------------
  6487. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6488. TTerminal * Terminal, Exception * E)
  6489. {
  6490. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6491. if (!Terminal->Active)
  6492. {
  6493. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6494. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6495. {
  6496. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6497. }
  6498. }
  6499. }
  6500. //---------------------------------------------------------------------------
  6501. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6502. UnicodeString Message, TQueryType Type,
  6503. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6504. {
  6505. if ((E == NULL) ||
  6506. ExceptionMessage(E, Message))
  6507. {
  6508. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6509. if (Important)
  6510. {
  6511. Seconds *= 5;
  6512. }
  6513. UnicodeString NoteMessage(UnformatMessage(Message));
  6514. if (Terminal != NULL)
  6515. {
  6516. NoteMessage = FORMAT(L"%s: %s",
  6517. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6518. }
  6519. if (WinConfiguration->BalloonNotifications)
  6520. {
  6521. AddNote(NoteMessage);
  6522. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6523. }
  6524. else
  6525. {
  6526. FlashOnBackground();
  6527. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6528. }
  6529. }
  6530. }
  6531. //---------------------------------------------------------------------------
  6532. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6533. {
  6534. RestoreApp();
  6535. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6536. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6537. !NonVisualDataModule->Busy)
  6538. {
  6539. TTerminal * Terminal = TerminalNoteData->Terminal;
  6540. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6541. if (!ComponentVisible[fcQueueView])
  6542. {
  6543. ToggleQueueVisibility();
  6544. GoToQueue();
  6545. }
  6546. }
  6547. }
  6548. //---------------------------------------------------------------------------
  6549. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6550. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6551. {
  6552. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6553. UnicodeString Message;
  6554. TNotifyEvent OnClick = NULL;
  6555. TObject * UserData = NULL;
  6556. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6557. switch (Event)
  6558. {
  6559. case qeEmptyButMonitored:
  6560. if ((ATerminal != Terminal) || QueueInvisible)
  6561. {
  6562. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6563. OnClick = QueueEmptyNoteClicked;
  6564. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6565. TerminalNoteData->Terminal = ATerminal;
  6566. UserData = TerminalNoteData;
  6567. }
  6568. break;
  6569. case qeEmpty:
  6570. OperationComplete(ManagedTerminal->QueueOperationStart);
  6571. break;
  6572. case qePendingUserAction:
  6573. if ((ATerminal != Terminal) ||
  6574. (QueueInvisible && !IsQueueAutoPopup()))
  6575. {
  6576. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6577. }
  6578. break;
  6579. default:
  6580. DebugFail();
  6581. }
  6582. if (!Message.IsEmpty())
  6583. {
  6584. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6585. }
  6586. }
  6587. //---------------------------------------------------------------------------
  6588. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6589. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6590. {
  6591. if (FDDExtMapFile != NULL)
  6592. {
  6593. CloseHandle(FDDExtMapFile);
  6594. FDDExtMapFile = NULL;
  6595. }
  6596. if (WinConfiguration->DDFakeFile)
  6597. {
  6598. DDFakeFileInitDrag(FileList, Created);
  6599. }
  6600. }
  6601. //---------------------------------------------------------------------------
  6602. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6603. {
  6604. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6605. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6606. {
  6607. FFakeFileDropTarget = FileName;
  6608. }
  6609. }
  6610. //---------------------------------------------------------------------------
  6611. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6612. {
  6613. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6614. if (!ForceDirectories(ApiPath(Result)))
  6615. {
  6616. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6617. }
  6618. FileSetAttr(ApiPath(Result), faHidden);
  6619. return Result;
  6620. }
  6621. //---------------------------------------------------------------------------
  6622. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6623. bool & Created)
  6624. {
  6625. FFakeFileDropTarget = UnicodeString();
  6626. FDragFakeDirectory = CreateFakeTransferDirectory();
  6627. FileList->AddItem(NULL, FDragFakeDirectory);
  6628. Created = true;
  6629. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6630. {
  6631. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6632. }
  6633. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6634. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6635. {
  6636. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6637. TDragExtCommStruct* CommStruct;
  6638. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6639. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6640. DebugAssert(CommStruct != NULL);
  6641. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6642. CommStruct->Dragging = true;
  6643. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6644. LENOF(CommStruct->DropDest));
  6645. NULL_TERMINATE(CommStruct->DropDest);
  6646. UnmapViewOfFile(CommStruct);
  6647. }
  6648. }
  6649. //---------------------------------------------------------------------------
  6650. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6651. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6652. {
  6653. bool Result;
  6654. if (Sender == RemoteDirView)
  6655. {
  6656. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6657. }
  6658. else
  6659. {
  6660. DebugAssert(Sender == RemoteDriveView);
  6661. TStrings * FileList = RemoteDriveView->DragFileList();
  6662. try
  6663. {
  6664. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6665. }
  6666. __finally
  6667. {
  6668. delete FileList;
  6669. }
  6670. }
  6671. if (FDDTargetControl == RemoteDriveView)
  6672. {
  6673. RemoteDriveView->UpdateDropTarget();
  6674. }
  6675. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6676. // or from tree view to dir view.
  6677. // foMove happens when file/dir is dragged from remote tree view outside of
  6678. // application via dragex
  6679. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6680. (DropSourceControl == RemoteDriveView))
  6681. {
  6682. RemoteDriveView->UpdateDropSource();
  6683. }
  6684. return Result;
  6685. }
  6686. //---------------------------------------------------------------------------
  6687. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6688. {
  6689. // This also handles drops of remote files to queue.
  6690. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6691. SAFE_DESTROY(FDDFileList);
  6692. if (!FFakeFileDropTarget.IsEmpty())
  6693. {
  6694. RemoveDir(ApiPath(FFakeFileDropTarget));
  6695. }
  6696. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6697. {
  6698. try
  6699. {
  6700. TDragResult DDResult = (Sender == RemoteDirView) ?
  6701. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6702. // Focus is moved to the target application,
  6703. // but as we are going to present the UI, we need to steal the focus back.
  6704. // This most likely won't work though (windows does not allow application
  6705. // to steal focus most of the time)
  6706. Application->BringToFront();
  6707. // On older version of Windows we never got drMove here, see also comment below.
  6708. // On Windows 10, we get the "move".
  6709. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6710. {
  6711. UnicodeString TargetDirectory;
  6712. TFileOperation Operation;
  6713. // drInvalid may mean drMove, see comment below
  6714. switch (DDResult)
  6715. {
  6716. case drCopy:
  6717. Operation = foCopy;
  6718. break;
  6719. case drMove:
  6720. Operation = foMove;
  6721. break;
  6722. default:
  6723. DebugFail();
  6724. case drInvalid:
  6725. // prefer "copy" for safety
  6726. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6727. break;
  6728. }
  6729. TTransferOperationParam Param;
  6730. UnicodeString CounterName;
  6731. bool ForceQueue;
  6732. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6733. {
  6734. // we get drInvalid both if d&d was intercepted by ddext,
  6735. // and when users drops on no-drop location.
  6736. // we tell the difference by existence of response from ddext,
  6737. // so we ignore absence of response in this case
  6738. if (DDResult != drInvalid)
  6739. {
  6740. // here we know that the extension is installed,
  6741. // as it is checked as soon as drag&drop starts from
  6742. // RemoteFileControlDDCreateDragFileList
  6743. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6744. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6745. {
  6746. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6747. }
  6748. else
  6749. {
  6750. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6751. }
  6752. }
  6753. }
  6754. else
  6755. {
  6756. // download using fake file
  6757. Param.Temp = false;
  6758. Param.DragDrop = true;
  6759. if (ForceQueue)
  6760. {
  6761. Param.Queue = asOn;
  6762. }
  6763. if (Sender == RemoteDirView)
  6764. {
  6765. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6766. }
  6767. if (RemoteFileControlFileOperation(Sender, Operation,
  6768. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6769. {
  6770. Configuration->Usage->Inc(CounterName);
  6771. }
  6772. }
  6773. }
  6774. }
  6775. __finally
  6776. {
  6777. CloseHandle(FDDExtMapFile);
  6778. FDDExtMapFile = NULL;
  6779. delete FDragFakeMonitors;
  6780. FDragFakeMonitors = NULL;
  6781. RemoveDir(ApiPath(FDragFakeDirectory));
  6782. FDragFakeDirectory = L"";
  6783. FFakeFileDropTarget = UnicodeString();
  6784. }
  6785. }
  6786. if (FDragDropSshTerminate.get() != NULL)
  6787. {
  6788. FDragDropSshTerminate->Rethrow();
  6789. }
  6790. }
  6791. //---------------------------------------------------------------------------
  6792. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6793. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6794. {
  6795. // Remember drop effect so we know (when user drops files), if we copy or move
  6796. FLastDropEffect = dwEffect;
  6797. }
  6798. //---------------------------------------------------------------------------
  6799. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6800. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6801. {
  6802. bool Result;
  6803. if (FDDTargetControl == QueueView3)
  6804. {
  6805. Directory = DefaultDownloadTargetDirectory();
  6806. Result = true;
  6807. CounterName = L"DownloadsDragDropQueue";
  6808. ForceQueue = true;
  6809. }
  6810. else if (!FFakeFileDropTarget.IsEmpty())
  6811. {
  6812. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6813. Result = true;
  6814. ForceQueue = false;
  6815. CounterName = L"DownloadsDragDropFakeFile";
  6816. }
  6817. else
  6818. {
  6819. ForceQueue = false;
  6820. Enabled = false;
  6821. try
  6822. {
  6823. int Timer = 0;
  6824. Result = false;
  6825. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6826. {
  6827. {
  6828. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6829. TDragExtCommStruct* CommStruct;
  6830. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6831. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6832. DebugAssert(CommStruct != NULL);
  6833. Result = !CommStruct->Dragging;
  6834. if (Result)
  6835. {
  6836. Directory = ExtractFilePath(CommStruct->DropDest);
  6837. CounterName = L"DownloadsDragDropExternalExt";
  6838. }
  6839. UnmapViewOfFile(CommStruct);
  6840. }
  6841. if (!Result)
  6842. {
  6843. Sleep(50);
  6844. Timer += 50;
  6845. Application->ProcessMessages();
  6846. }
  6847. }
  6848. }
  6849. __finally
  6850. {
  6851. Enabled = true;
  6852. }
  6853. }
  6854. return Result;
  6855. }
  6856. //---------------------------------------------------------------------------
  6857. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6858. const UnicodeString TempDir, int SecDelay)
  6859. {
  6860. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6861. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6862. if (FDelayedDeletionTimer == NULL)
  6863. {
  6864. DebugAssert(HandleAllocated());
  6865. FDelayedDeletionTimer = new TTimer(this);
  6866. FDelayedDeletionTimer->Interval = 10000;
  6867. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6868. }
  6869. else
  6870. {
  6871. FDelayedDeletionTimer->Enabled = true;
  6872. }
  6873. }
  6874. //---------------------------------------------------------------------------
  6875. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6876. {
  6877. DebugAssert(FDelayedDeletionList != NULL);
  6878. TDateTime N = Now();
  6879. TDateTime Alert;
  6880. UnicodeString Directory;
  6881. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6882. {
  6883. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6884. if ((N >= Alert) || (Sender == NULL))
  6885. {
  6886. Directory = FDelayedDeletionList->Strings[Index];
  6887. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6888. {
  6889. FDelayedDeletionList->Delete(Index);
  6890. }
  6891. }
  6892. }
  6893. if (FDelayedDeletionList->Count == 0)
  6894. {
  6895. FDelayedDeletionTimer->Enabled = false;
  6896. }
  6897. }
  6898. //---------------------------------------------------------------------------
  6899. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6900. {
  6901. if (IsFileControl(FDDTargetControl, osRemote) ||
  6902. (FDDTargetControl == SessionsPageControl))
  6903. {
  6904. TTerminal * TargetTerminal = NULL;
  6905. TFileOperation Operation = foNone;
  6906. if (FDDTargetControl == SessionsPageControl)
  6907. {
  6908. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6909. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6910. // do not allow dropping on the "+" tab
  6911. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6912. if (TargetTerminal != NULL)
  6913. {
  6914. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6915. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6916. {
  6917. Operation = foRemoteMove;
  6918. }
  6919. else
  6920. {
  6921. Operation = foRemoteCopy;
  6922. }
  6923. }
  6924. }
  6925. else
  6926. {
  6927. if (FLastDropEffect == DROPEFFECT_MOVE)
  6928. {
  6929. Operation = foRemoteMove;
  6930. }
  6931. else if (FLastDropEffect == DROPEFFECT_COPY)
  6932. {
  6933. Operation = foRemoteCopy;
  6934. }
  6935. }
  6936. if (Operation != foNone)
  6937. {
  6938. RemoteFileControlFileOperation(DropSourceControl,
  6939. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6940. }
  6941. // abort drag&drop
  6942. Abort();
  6943. }
  6944. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6945. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6946. (FDDTargetControl != QueueView3))
  6947. {
  6948. DebugAssert(!FDragTempDir.IsEmpty());
  6949. TTransferType Type;
  6950. UnicodeString TempDir = FDragTempDir;
  6951. // We clear FDragTempDir before calling
  6952. // just in case it fail (raises exception)
  6953. FDragTempDir = L"";
  6954. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6955. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6956. // empty directory parameter means temp directory -> don't display it!
  6957. UnicodeString TargetDir = L"";
  6958. int Options = 0;
  6959. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6960. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6961. {
  6962. Abort();
  6963. }
  6964. // TargetDir is set when dropped on local file control
  6965. // (this was workaround for legacy dirview event handling, now it should be
  6966. // made prettier)
  6967. if (TargetDir.IsEmpty())
  6968. {
  6969. TargetDir = TempDir;
  6970. if (ForceDirectories(ApiPath(TargetDir)))
  6971. {
  6972. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6973. FPendingTempSpaceWarn = true;
  6974. try
  6975. {
  6976. FDragDropOperation = true;
  6977. int Params = cpTemporary |
  6978. (Type == ttMove ? cpDelete : 0);
  6979. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6980. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6981. }
  6982. __finally
  6983. {
  6984. FDragDropOperation = false;
  6985. FPendingTempSpaceWarn = false;
  6986. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6987. }
  6988. }
  6989. else
  6990. {
  6991. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6992. }
  6993. }
  6994. }
  6995. }
  6996. //---------------------------------------------------------------------------
  6997. void __fastcall TCustomScpExplorerForm::DDDownload(
  6998. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6999. {
  7000. TAutoBatch AutoBatch(this);
  7001. UpdateCopyParamCounters(*CopyParam);
  7002. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7003. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7004. {
  7005. RemoteDriveView->UpdateDropSource();
  7006. }
  7007. }
  7008. //---------------------------------------------------------------------------
  7009. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7010. {
  7011. public:
  7012. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7013. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7014. {
  7015. }
  7016. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7017. {
  7018. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7019. TDataObjectFilesEx::AllowData(FormatEtc);
  7020. }
  7021. };
  7022. //---------------------------------------------------------------------------
  7023. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7024. TObject * Sender, TDataObject *& DataObject)
  7025. {
  7026. if (FDDExtMapFile != NULL)
  7027. {
  7028. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7029. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7030. {
  7031. Abort();
  7032. }
  7033. if (FileList->Count > 0)
  7034. {
  7035. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7036. if (!FilesObject->IsValid(true, true))
  7037. {
  7038. FilesObject->_Release();
  7039. }
  7040. else
  7041. {
  7042. DataObject = FilesObject;
  7043. }
  7044. }
  7045. }
  7046. }
  7047. //---------------------------------------------------------------------------
  7048. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7049. {
  7050. DebugFail();
  7051. }
  7052. //---------------------------------------------------------------------------
  7053. void __fastcall TCustomScpExplorerForm::GoToTree()
  7054. {
  7055. ComponentVisible[fcRemoteTree] = true;
  7056. RemoteDriveView->SetFocus();
  7057. }
  7058. //---------------------------------------------------------------------------
  7059. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7060. TPanelExport Export)
  7061. {
  7062. TCustomDirView * DirView = this->DirView(Side);
  7063. std::unique_ptr<TStrings> ExportData(new TStringList());
  7064. switch (Export)
  7065. {
  7066. case pePath:
  7067. ExportData->Add(DirView->PathName);
  7068. break;
  7069. case peFileList:
  7070. case peFullFileList:
  7071. {
  7072. bool FullPath = (Export == peFullFileList);
  7073. DirView->CreateFileList(false, FullPath, ExportData.get());
  7074. for (int Index = 0; Index < ExportData->Count; Index++)
  7075. {
  7076. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7077. {
  7078. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7079. }
  7080. }
  7081. }
  7082. break;
  7083. default:
  7084. DebugFail();
  7085. }
  7086. return ExportData.release();
  7087. }
  7088. //---------------------------------------------------------------------------
  7089. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7090. TPanelExport Export, TPanelExportDestination Destination)
  7091. {
  7092. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7093. PanelExportStore(Side, Export, Destination, ExportData.get());
  7094. }
  7095. //---------------------------------------------------------------------------
  7096. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7097. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7098. TStrings * ExportData)
  7099. {
  7100. if (Destination == pedClipboard)
  7101. {
  7102. TInstantOperationVisualizer Visualizer;
  7103. CopyToClipboard(ExportData);
  7104. }
  7105. else
  7106. {
  7107. DebugFail();
  7108. }
  7109. }
  7110. //---------------------------------------------------------------------------
  7111. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7112. {
  7113. TCustomDirView * DirView = this->DirView(Side);
  7114. UnicodeString Mask = DirView->Mask;
  7115. if (DoFilterMaskDialog(DirView, Mask))
  7116. {
  7117. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7118. Configuration->Usage->Inc(L"Filters");
  7119. }
  7120. }
  7121. //---------------------------------------------------------------------------
  7122. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7123. {
  7124. return FQueueController->DefaultOperation();
  7125. }
  7126. //---------------------------------------------------------------------------
  7127. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7128. TQueueOperation Operation, void ** Param)
  7129. {
  7130. switch (Operation)
  7131. {
  7132. case qoPreferences:
  7133. return true;
  7134. case qoGoTo:
  7135. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7136. case qoOnceEmpty:
  7137. return !FQueueController->Empty;
  7138. default:
  7139. return FQueueController->AllowOperation(Operation, Param);
  7140. }
  7141. }
  7142. //---------------------------------------------------------------------------
  7143. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7144. {
  7145. if (DebugAlwaysTrue(QueueView3->Visible))
  7146. {
  7147. QueueView3->SetFocus();
  7148. }
  7149. }
  7150. //---------------------------------------------------------------------------
  7151. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7152. TQueueOperation Operation, void * Param)
  7153. {
  7154. if (Operation == qoGoTo)
  7155. {
  7156. GoToQueue();
  7157. }
  7158. else if (Operation == qoPreferences)
  7159. {
  7160. PreferencesDialog(pmQueue);
  7161. }
  7162. else
  7163. {
  7164. FQueueController->ExecuteOperation(Operation, Param);
  7165. }
  7166. }
  7167. //---------------------------------------------------------------------------
  7168. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7169. {
  7170. return (Queue != NULL) && Queue->Enabled;
  7171. }
  7172. //---------------------------------------------------------------------------
  7173. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7174. {
  7175. DebugAssert(Queue != NULL);
  7176. if (Queue != NULL)
  7177. {
  7178. Queue->Enabled = !Queue->Enabled;
  7179. }
  7180. }
  7181. //---------------------------------------------------------------------------
  7182. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7183. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7184. {
  7185. FQueueActedItem = QueueView3->ItemFocused;
  7186. }
  7187. //---------------------------------------------------------------------------
  7188. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7189. {
  7190. return TopDock->Height + QueueSplitter->Height;
  7191. }
  7192. //---------------------------------------------------------------------------
  7193. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7194. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7195. {
  7196. // when queue is hidden by double-clicking splitter, stray attempt to
  7197. // resize the panel with strange value arrives, make sure it is ignored
  7198. if (ComponentVisible[fcQueueView])
  7199. {
  7200. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7201. RemotePanel->Constraints->MinHeight;
  7202. if (NewSize > HeightLimit)
  7203. {
  7204. NewSize = HeightLimit;
  7205. }
  7206. }
  7207. else
  7208. {
  7209. Accept = false;
  7210. }
  7211. }
  7212. //---------------------------------------------------------------------------
  7213. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7214. TDragObject *& /*DragObject*/)
  7215. {
  7216. FQueueActedItem = QueueView3->ItemFocused;
  7217. }
  7218. //---------------------------------------------------------------------------
  7219. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7220. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7221. {
  7222. Accept = true;
  7223. if (Source == QueueView3)
  7224. {
  7225. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7226. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7227. if (Accept)
  7228. {
  7229. TQueueItemProxy * QueueItem;
  7230. TQueueItemProxy * DestQueueItem;
  7231. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7232. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7233. Accept = (QueueItem != DestQueueItem) &&
  7234. (QueueItem->Status == TQueueItem::qsPending) &&
  7235. (DestQueueItem->Status == TQueueItem::qsPending);
  7236. }
  7237. }
  7238. }
  7239. //---------------------------------------------------------------------------
  7240. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7241. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7242. {
  7243. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7244. {
  7245. TQueueItemProxy * QueueItem;
  7246. TQueueItemProxy * DestQueueItem;
  7247. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7248. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7249. QueueItem->Move(DestQueueItem);
  7250. }
  7251. }
  7252. //---------------------------------------------------------------------------
  7253. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7254. {
  7255. if ((QueueView3->ItemFocused == NULL) &&
  7256. (QueueView3->Items->Count > 0))
  7257. {
  7258. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7259. }
  7260. QueueLabelUpdateStatus();
  7261. }
  7262. //---------------------------------------------------------------------------
  7263. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7264. {
  7265. QueueLabelUpdateStatus();
  7266. }
  7267. //---------------------------------------------------------------------------
  7268. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7269. {
  7270. QueueLabel->UpdateStatus();
  7271. }
  7272. //---------------------------------------------------------------------------
  7273. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7274. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7275. {
  7276. if (Selected)
  7277. {
  7278. NonVisualDataModule->UpdateNonVisibleActions();
  7279. }
  7280. }
  7281. //---------------------------------------------------------------------------
  7282. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7283. {
  7284. TDragDropFilesEx * Result = NULL;
  7285. if (Sender == SessionsPageControl)
  7286. {
  7287. Result = FSessionsDragDropFilesEx;
  7288. }
  7289. else if (Sender == QueueView3)
  7290. {
  7291. Result = FQueueDragDropFilesEx;
  7292. }
  7293. else
  7294. {
  7295. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7296. if (DirView != NULL)
  7297. {
  7298. Result = DirView->DragDropFilesEx;
  7299. }
  7300. else
  7301. {
  7302. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7303. if (DriveView != NULL)
  7304. {
  7305. Result = DriveView->DragDropFilesEx;
  7306. }
  7307. }
  7308. }
  7309. DebugAssert(Result != NULL);
  7310. return Result;
  7311. }
  7312. //---------------------------------------------------------------------------
  7313. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7314. {
  7315. return (DView->SelCount == DView->FilesCount);
  7316. }
  7317. //---------------------------------------------------------------------------
  7318. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7319. {
  7320. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7321. }
  7322. //---------------------------------------------------------------------------
  7323. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7324. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7325. {
  7326. TFileOperation Operation;
  7327. switch (Effect)
  7328. {
  7329. case DROPEFFECT_MOVE:
  7330. Operation = foMove;
  7331. break;
  7332. case DROPEFFECT_COPY:
  7333. // occures on WinXP (reported by user)
  7334. default:
  7335. Operation = foCopy;
  7336. break;
  7337. };
  7338. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7339. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7340. if (DragDropFilesEx->FileList->Count > 0)
  7341. {
  7342. TStrings * FileList = new TStringList();
  7343. try
  7344. {
  7345. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7346. {
  7347. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7348. }
  7349. FDragDropOperation = true;
  7350. TTransferOperationParam Param;
  7351. Param.TargetDirectory = TargetPath;
  7352. // upload, no temp dirs
  7353. Param.Temp = false;
  7354. Param.DragDrop = DragDrop;
  7355. Param.Options =
  7356. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7357. if (ForceQueue)
  7358. {
  7359. Param.Queue = asOn;
  7360. }
  7361. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7362. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7363. {
  7364. if (IsFileControl(DropSourceControl, osLocal))
  7365. {
  7366. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7367. }
  7368. else
  7369. {
  7370. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7371. }
  7372. }
  7373. }
  7374. __finally
  7375. {
  7376. FDragDropOperation = false;
  7377. delete FileList;
  7378. }
  7379. }
  7380. }
  7381. //---------------------------------------------------------------------------
  7382. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7383. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7384. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7385. {
  7386. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7387. }
  7388. //---------------------------------------------------------------------------
  7389. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7390. TObject * Sender, int grfKeyState, int & dwEffect)
  7391. {
  7392. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7393. if ((dwEffect != DROPEFFECT_NONE) &&
  7394. IsFileControl(DropSourceControl, osRemote))
  7395. {
  7396. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7397. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7398. (RemoteDirView->DropTarget == NULL))
  7399. {
  7400. dwEffect = DROPEFFECT_NONE;
  7401. }
  7402. else
  7403. {
  7404. if (dwEffect == DROPEFFECT_COPY)
  7405. {
  7406. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7407. // currently we support copying always (at least via temporary directory);
  7408. // remove associated checks once this all proves stable and working
  7409. bool CopyCapable = true;
  7410. // if we do not support neither of operations, there's no discussion
  7411. if (!MoveCapable && !CopyCapable)
  7412. {
  7413. dwEffect = DROPEFFECT_NONE;
  7414. }
  7415. else
  7416. {
  7417. // The default effect inside remote panel is move,
  7418. // unless we do not support it, but support copy
  7419. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7420. {
  7421. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7422. }
  7423. else
  7424. {
  7425. // with ctrl-down, we want copy unless it is not supported
  7426. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7427. }
  7428. }
  7429. }
  7430. }
  7431. }
  7432. }
  7433. //---------------------------------------------------------------------------
  7434. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7435. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7436. {
  7437. if (FDDTotalSize >= 0)
  7438. {
  7439. if (File->IsDirectory)
  7440. {
  7441. FDDTotalSize = -1;
  7442. }
  7443. else
  7444. {
  7445. FDDTotalSize += File->Size;
  7446. }
  7447. }
  7448. DebugAssert(!FDragTempDir.IsEmpty());
  7449. // TODO: this is quite ineffective
  7450. // TODO: what if invalid character replacement is disabled?
  7451. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7452. UnicodeString TransferFileName;
  7453. if (Sender == RemoteDriveView)
  7454. {
  7455. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7456. }
  7457. else
  7458. {
  7459. TransferFileName = File->FileName;
  7460. }
  7461. FDDFileList->AddObject(TransferFileName, File);
  7462. }
  7463. //---------------------------------------------------------------------------
  7464. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7465. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7466. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7467. {
  7468. // sometimes we do not get DDEnd so the list is not released
  7469. SAFE_DESTROY(FDDFileList);
  7470. FDDFileList = new TStringList();
  7471. FDragTempDir = WinConfiguration->TemporaryDir();
  7472. FDDTotalSize = 0;
  7473. FDragDropSshTerminate.reset(NULL);
  7474. }
  7475. //---------------------------------------------------------------------------
  7476. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7477. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7478. HRESULT & Result)
  7479. {
  7480. if (Result == DRAGDROP_S_DROP)
  7481. {
  7482. try
  7483. {
  7484. GlobalDragImageList->HideDragImage();
  7485. try
  7486. {
  7487. RemoteFileControlDDTargetDrop();
  7488. }
  7489. catch(ESshTerminate & E)
  7490. {
  7491. FDragDropSshTerminate.reset(E.Clone());
  7492. }
  7493. }
  7494. catch (Exception &E)
  7495. {
  7496. // If downloading fails we need to cancel drag&drop, otherwise
  7497. // Explorer shows error
  7498. // But by the way exception probably never reach this point as
  7499. // it's catched on way.
  7500. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7501. Result = DRAGDROP_S_CANCEL;
  7502. ShowExtendedException(Terminal, &E);
  7503. }
  7504. }
  7505. }
  7506. //---------------------------------------------------------------------------
  7507. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7508. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7509. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7510. {
  7511. TFileMasks::TParams MaskParams;
  7512. MaskParams.Size = Size;
  7513. MaskParams.Modification = Modification;
  7514. // this does not re-parse the mask if it is the same as the last time
  7515. FDirViewMatchMask = Masks;
  7516. bool ImplicitMatch;
  7517. Matches =
  7518. // RecurseInclude parameter has no effect as the Path is empty
  7519. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7520. (AllowImplicitMatches || !ImplicitMatch);
  7521. }
  7522. //---------------------------------------------------------------------------
  7523. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7524. TObject * Sender, TListItem * Item, WORD & Indexes)
  7525. {
  7526. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7527. UnicodeString Ext;
  7528. if (DirView == RemoteDirView)
  7529. {
  7530. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7531. }
  7532. else
  7533. {
  7534. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7535. }
  7536. if (SameText(Ext, Configuration->PartialExt))
  7537. {
  7538. Indexes |= oiPartial;
  7539. }
  7540. }
  7541. //---------------------------------------------------------------------------
  7542. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7543. {
  7544. return
  7545. DirViewEnabled(osCurrent) &&
  7546. DirView(osCurrent)->CanPasteFromClipBoard();
  7547. }
  7548. //---------------------------------------------------------------------------
  7549. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7550. {
  7551. bool Result = false;
  7552. if (CanPasteToDirViewFromClipBoard())
  7553. {
  7554. Result = true;
  7555. }
  7556. else
  7557. {
  7558. UnicodeString ClipboardText;
  7559. if (NonEmptyTextFromClipboard(ClipboardText))
  7560. {
  7561. if (StoredSessions->IsUrl(ClipboardText))
  7562. {
  7563. Result = true;
  7564. }
  7565. else
  7566. {
  7567. Result = DirViewEnabled(osCurrent);
  7568. }
  7569. }
  7570. }
  7571. return Result;
  7572. }
  7573. //---------------------------------------------------------------------------
  7574. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7575. {
  7576. if (DoesClipboardContainOurFiles())
  7577. {
  7578. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7579. {
  7580. TTerminalManager * Manager = TTerminalManager::Instance();
  7581. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7582. Manager->ActiveTerminal = FClipboardTerminal;
  7583. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7584. }
  7585. }
  7586. else if (CanPasteToDirViewFromClipBoard())
  7587. {
  7588. DirView(osCurrent)->PasteFromClipBoard();
  7589. }
  7590. else
  7591. {
  7592. UnicodeString ClipboardText;
  7593. if (NonEmptyTextFromClipboard(ClipboardText))
  7594. {
  7595. if (StoredSessions->IsUrl(ClipboardText))
  7596. {
  7597. NewSession(false, ClipboardText);
  7598. }
  7599. else
  7600. {
  7601. DirView(osCurrent)->Path = ClipboardText;
  7602. }
  7603. }
  7604. }
  7605. }
  7606. //---------------------------------------------------------------------------
  7607. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7608. {
  7609. // TBD
  7610. }
  7611. //---------------------------------------------------------------------------
  7612. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7613. {
  7614. TCustomDirView * ADirView = DirView(Side);
  7615. ADirView->SelectAll(Mode);
  7616. ADirView->SetFocus();
  7617. }
  7618. //---------------------------------------------------------------------------
  7619. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7620. {
  7621. TCustomDirView * ADirView = DirView(Side);
  7622. TFileFilter Filter;
  7623. DefaultFileFilter(Filter);
  7624. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7625. {
  7626. Configuration->Usage->Inc(L"MaskSelections");
  7627. ADirView->SelectFiles(Filter, Select);
  7628. ADirView->SetFocus();
  7629. }
  7630. }
  7631. //---------------------------------------------------------------------------
  7632. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7633. {
  7634. TCustomDirView * ADirView = DirView(Side);
  7635. ADirView->RestoreSelectedNames();
  7636. ADirView->SetFocus();
  7637. }
  7638. //---------------------------------------------------------------------------
  7639. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7640. {
  7641. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7642. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7643. {
  7644. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7645. UnicodeString Ext;
  7646. if (GetSide(osCurrent) == osRemote)
  7647. {
  7648. Ext = UnixExtractFileExt(FileName);
  7649. }
  7650. else
  7651. {
  7652. Ext = ExtractFileExt(FileName);
  7653. }
  7654. if (Ext.IsEmpty())
  7655. {
  7656. Ext = L".";
  7657. }
  7658. TFileFilter Filter;
  7659. Filter.Masks = FORMAT(L"*%s", (Ext));
  7660. Filter.Directories = false;
  7661. CurrentDirView->SelectFiles(Filter, Select);
  7662. }
  7663. }
  7664. //---------------------------------------------------------------------------
  7665. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7666. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7667. {
  7668. UnicodeString Result;
  7669. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7670. {
  7671. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7672. }
  7673. else if ((Side == osRemote) && (Terminal == NULL))
  7674. {
  7675. // noop
  7676. }
  7677. else
  7678. {
  7679. Result =
  7680. FMTLOAD(FILE_INFO_FORMAT,
  7681. (FormatBytes(FileInfo.SelectedSize),
  7682. FormatBytes(FileInfo.FilesSize),
  7683. FormatNumber(FileInfo.SelectedCount),
  7684. FormatNumber(FileInfo.FilesCount)));
  7685. }
  7686. return Result;
  7687. }
  7688. //---------------------------------------------------------------------------
  7689. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7690. TTBXStatusPanel * Panel, TOperationSide Side)
  7691. {
  7692. if (Panel->Index == 1)
  7693. {
  7694. ToggleShowHiddenFiles();
  7695. }
  7696. else if (Panel->Index == 2)
  7697. {
  7698. Filter(Side);
  7699. }
  7700. }
  7701. //---------------------------------------------------------------------------
  7702. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7703. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7704. {
  7705. DebugAssert(!StatusBar->SimplePanel);
  7706. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7707. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7708. }
  7709. //---------------------------------------------------------------------------
  7710. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7711. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7712. {
  7713. DebugAssert(StatusBar->Panels->Count >= 3);
  7714. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7715. if (FileInfo.HiddenCount > 0)
  7716. {
  7717. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7718. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7719. }
  7720. else
  7721. {
  7722. HiddenFilesPanel->ViewPriority = 0;
  7723. // not really necessary, just to cleanup no-longer-valid data
  7724. HiddenFilesPanel->Caption = L"";
  7725. }
  7726. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7727. if (FileInfo.FilteredCount > 0)
  7728. {
  7729. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7730. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7731. }
  7732. else
  7733. {
  7734. FilteredFilesPanel->ViewPriority = 0;
  7735. // not really necessary, just to cleanup no-longer-valid data
  7736. FilteredFilesPanel->Caption = L"";
  7737. }
  7738. }
  7739. //---------------------------------------------------------------------------
  7740. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7741. TObject * /*Sender*/)
  7742. {
  7743. if (RemoteDirView->Enabled)
  7744. {
  7745. RemoteDirView->SetFocus();
  7746. }
  7747. }
  7748. //---------------------------------------------------------------------------
  7749. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7750. {
  7751. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7752. switch (Config.Show)
  7753. {
  7754. case qvShow:
  7755. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7756. {
  7757. Config.Show = qvHide;
  7758. }
  7759. else
  7760. {
  7761. Config.Show = Config.LastHideShow;
  7762. }
  7763. break;
  7764. case qvHideWhenEmpty:
  7765. if (ComponentVisible[fcQueueView])
  7766. {
  7767. Config.Show = qvHide;
  7768. }
  7769. else
  7770. {
  7771. Config.LastHideShow = Config.Show;
  7772. Config.Show = qvShow;
  7773. }
  7774. break;
  7775. case qvHide:
  7776. Config.LastHideShow = Config.Show;
  7777. Config.Show = qvShow;
  7778. break;
  7779. }
  7780. WinConfiguration->QueueView = Config;
  7781. }
  7782. //---------------------------------------------------------------------------
  7783. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7784. {
  7785. UnicodeString Result;
  7786. if (FTerminal != NULL)
  7787. {
  7788. switch (WinConfiguration->PathInCaption)
  7789. {
  7790. case picShort:
  7791. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7792. break;
  7793. case picFull:
  7794. Result = FTerminal->CurrentDirectory;
  7795. break;
  7796. }
  7797. }
  7798. return Result;
  7799. }
  7800. //---------------------------------------------------------------------------
  7801. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7802. {
  7803. TColor Result = GetWindowColor(FSessionColor);
  7804. return Result;
  7805. }
  7806. //---------------------------------------------------------------------------
  7807. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7808. {
  7809. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7810. return Result;
  7811. }
  7812. //---------------------------------------------------------------------------
  7813. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7814. {
  7815. TTerminalManager::Instance()->UpdateAppTitle();
  7816. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7817. // background to gray on Windows 7 (works on Windows 10).
  7818. // See also EnableControl
  7819. if (Showing)
  7820. {
  7821. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7822. if (HasTerminal)
  7823. {
  7824. // Update path when it changes
  7825. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7826. {
  7827. UpdateSessionTab(SessionsPageControl->ActivePage);
  7828. }
  7829. if (!RemoteDirView->Enabled)
  7830. {
  7831. RemoteDirView->Enabled = true;
  7832. if (FRemoteDirViewWasFocused)
  7833. {
  7834. ActiveControl = RemoteDirView;
  7835. }
  7836. }
  7837. RemoteDirView->Color = PanelColor();
  7838. }
  7839. else
  7840. {
  7841. if (RemoteDirView->Enabled)
  7842. {
  7843. // This is first called when the form is being constructed
  7844. // (not anymore due to Showing test above)
  7845. // but the false is overriden in the constructor later.
  7846. // An even later in TScpCommanderForm::DoShow()
  7847. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7848. RemoteDirView->Enabled = false;
  7849. RemoteDirView->Color = DisabledPanelColor();
  7850. }
  7851. }
  7852. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  7853. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  7854. RemoteDriveView->Color = RemoteDirView->Color;
  7855. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  7856. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  7857. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  7858. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  7859. QueueLabelUpdateStatus();
  7860. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  7861. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  7862. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7863. }
  7864. }
  7865. //---------------------------------------------------------------------------
  7866. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7867. {
  7868. UpdateControls();
  7869. }
  7870. //---------------------------------------------------------------------------
  7871. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7872. TObject * Sender)
  7873. {
  7874. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7875. DebugAssert(DirView != NULL);
  7876. DoDirViewLoaded(DirView);
  7877. TransferPresetAutoSelect();
  7878. }
  7879. //---------------------------------------------------------------------------
  7880. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7881. {
  7882. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7883. // first run after installation
  7884. if (double(Updates.LastCheck) == 0)
  7885. {
  7886. // make sure next time there will be an update (if enabled)
  7887. Updates.LastCheck = TDateTime(1);
  7888. WinConfiguration->Updates = Updates;
  7889. }
  7890. else if ((double(Updates.Period) > 0) &&
  7891. (Now() - Updates.LastCheck >= Updates.Period))
  7892. {
  7893. TThreadMethod OnUpdatesChecked = NULL;
  7894. if (!IsUWP())
  7895. {
  7896. OnUpdatesChecked = UpdatesChecked;
  7897. }
  7898. StartUpdateThread(OnUpdatesChecked);
  7899. }
  7900. }
  7901. //---------------------------------------------------------------------------
  7902. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7903. {
  7904. UnicodeString Message;
  7905. bool New;
  7906. TQueryType Type;
  7907. GetUpdatesMessage(Message, New, Type, false);
  7908. if (!Message.IsEmpty())
  7909. {
  7910. if (New)
  7911. {
  7912. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7913. }
  7914. if (!New && (Type != qtWarning))
  7915. {
  7916. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7917. }
  7918. else
  7919. {
  7920. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7921. }
  7922. if (New)
  7923. {
  7924. Configuration->Usage->Inc(L"UpdateNotifications");
  7925. }
  7926. }
  7927. }
  7928. //---------------------------------------------------------------------------
  7929. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7930. {
  7931. RestoreApp();
  7932. bool CanDisplay = !NonVisualDataModule->Busy;
  7933. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7934. {
  7935. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7936. }
  7937. if (CanDisplay)
  7938. {
  7939. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7940. CheckForUpdates(true);
  7941. }
  7942. else
  7943. {
  7944. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7945. }
  7946. }
  7947. //---------------------------------------------------------------------------
  7948. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7949. TCopyParamRuleData & Data)
  7950. {
  7951. DebugAssert(Terminal != NULL);
  7952. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7953. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7954. Data.RemoteDirectory = RemoteDirView->PathName;
  7955. }
  7956. //---------------------------------------------------------------------------
  7957. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7958. {
  7959. // Terminal can be null when we are changing local directory implicitly,
  7960. // because it has been deleted, while no session is connected
  7961. // (Login dialog is open)
  7962. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7963. {
  7964. TCopyParamRuleData Data;
  7965. GetTransferPresetAutoSelectData(Data);
  7966. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7967. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7968. if (CopyParamIndex < 0)
  7969. {
  7970. // there is no preset that matches autoselection
  7971. // set preset that we consider "default"
  7972. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7973. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7974. }
  7975. else
  7976. {
  7977. // there is preset matching autoselection
  7978. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7979. if (CopyParamName == FCopyParamAutoSelected)
  7980. {
  7981. // autoselected the same preset as the last time
  7982. // make no change (i.e. preserve custom user preset, if any)
  7983. }
  7984. else
  7985. {
  7986. // autoselected the different preset then the last time (or there
  7987. // was default preset set)
  7988. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7989. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7990. }
  7991. }
  7992. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7993. {
  7994. if (CopyParamIndex >= 0)
  7995. {
  7996. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7997. }
  7998. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7999. try
  8000. {
  8001. int Fmt =
  8002. (CopyParamIndex < 0) ?
  8003. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8004. COPY_PARAM_AUTOSELECTED;
  8005. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8006. Data->Message = MainInstructions(Message);
  8007. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8008. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8009. CopyParamAttrs);
  8010. if (CopyParamIndex >= 0)
  8011. {
  8012. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8013. Info = FORMAT(L"%s\n \n%s", (Info,
  8014. FMTLOAD(COPY_PARAM_RULE,
  8015. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8016. }
  8017. Data->Message += L"\n\n" + Info;
  8018. if (WinConfiguration->CopyParamAutoSelectNotice)
  8019. {
  8020. TransferPresetNoteMessage(Data, true);
  8021. }
  8022. else
  8023. {
  8024. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8025. Data = NULL; // ownership passed
  8026. }
  8027. }
  8028. __finally
  8029. {
  8030. delete Data;
  8031. }
  8032. }
  8033. }
  8034. }
  8035. //---------------------------------------------------------------------------
  8036. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8037. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8038. {
  8039. DebugAssert(NoteData != NULL);
  8040. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8041. TQueryButtonAlias Aliases[1];
  8042. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8043. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8044. Params.Aliases = Aliases;
  8045. Params.AliasesCount = LENOF(Aliases);
  8046. unsigned int Result =
  8047. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8048. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8049. switch (Result)
  8050. {
  8051. case qaNeverAskAgain:
  8052. DebugAssert(AllowNeverAskAgain);
  8053. WinConfiguration->CopyParamAutoSelectNotice = false;
  8054. break;
  8055. case qaIgnore:
  8056. PreferencesDialog(pmPresets);
  8057. break;
  8058. }
  8059. }
  8060. //---------------------------------------------------------------------------
  8061. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8062. {
  8063. // as of now useless, as this is used for notes only, never for balloons, ...
  8064. RestoreApp();
  8065. // .. and we should never be busy here
  8066. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8067. {
  8068. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8069. }
  8070. }
  8071. //---------------------------------------------------------------------------
  8072. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8073. TPreferencesMode APreferencesMode)
  8074. {
  8075. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8076. TCopyParamRuleData Data;
  8077. if (Terminal != NULL)
  8078. {
  8079. PreferencesData.reset(new TPreferencesDialogData());
  8080. GetTransferPresetAutoSelectData(Data);
  8081. PreferencesData->CopyParamRuleData = &Data;
  8082. }
  8083. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8084. }
  8085. //---------------------------------------------------------------------------
  8086. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8087. const TCustomCommandType & Command)
  8088. {
  8089. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8090. {
  8091. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8092. }
  8093. }
  8094. //---------------------------------------------------------------------------
  8095. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8096. {
  8097. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8098. TCustomCommandType Command;
  8099. // make sure we use local custom command when remote are not supported
  8100. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8101. {
  8102. Command = FLastCustomCommand;
  8103. }
  8104. else
  8105. {
  8106. Command.Params = Command.Params | ccLocal;
  8107. }
  8108. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8109. FEditingFocusedAdHocCommand = OnFocused;
  8110. bool LocalSide = (FCurrentSide == osLocal);
  8111. int Options =
  8112. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8113. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8114. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8115. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8116. {
  8117. FLastCustomCommand = Command;
  8118. UpdateCustomCommandsToolbar();
  8119. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8120. }
  8121. }
  8122. //---------------------------------------------------------------------------
  8123. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8124. {
  8125. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8126. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8127. DebugAssert(State > 0);
  8128. if (State <= 0)
  8129. {
  8130. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8131. }
  8132. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8133. }
  8134. //---------------------------------------------------------------------------
  8135. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8136. TCustomCommandType & Command, int & State)
  8137. {
  8138. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8139. if (Result)
  8140. {
  8141. Command = FLastCustomCommand;
  8142. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8143. }
  8144. return Result;
  8145. }
  8146. //---------------------------------------------------------------------------
  8147. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8148. {
  8149. if (RemoteDirView->ItemFocused != NULL)
  8150. {
  8151. RemoteDirView->ItemFocused->CancelEdit();
  8152. }
  8153. ResetIncrementalSearch();
  8154. }
  8155. //---------------------------------------------------------------------------
  8156. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8157. {
  8158. DebugAssert(ComponentVisible[Component]);
  8159. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8160. }
  8161. //---------------------------------------------------------------------------
  8162. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8163. {
  8164. // when queue panel is resized here directly, the status bar is stretched
  8165. // over whole space the panel occupied
  8166. PostComponentHide(fcQueueView);
  8167. }
  8168. //---------------------------------------------------------------------------
  8169. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8170. {
  8171. WinConfiguration->ResetSysDarkTheme();
  8172. ConfigurationChanged();
  8173. ConfigureInterface();
  8174. TForm::Dispatch(&Message);
  8175. }
  8176. //---------------------------------------------------------------------------
  8177. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8178. {
  8179. TMessage * M = static_cast<TMessage*>(Message);
  8180. switch (M->Msg)
  8181. {
  8182. case CM_DIALOGCHAR:
  8183. CMDialogChar(*M);
  8184. break;
  8185. case WM_APPCOMMAND:
  8186. WMAppCommand(*M);
  8187. break;
  8188. case WM_SYSCOMMAND:
  8189. WMSysCommand(*M);
  8190. break;
  8191. case WM_QUERYENDSESSION:
  8192. WMQueryEndSession(*M);
  8193. break;
  8194. case WM_ENDSESSION:
  8195. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8196. break;
  8197. #ifdef _DEBUG
  8198. case WM_WINDOWPOSCHANGED:
  8199. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8200. break;
  8201. case CM_CUSTOMSTYLECHANGED:
  8202. TForm::Dispatch(Message);
  8203. break;
  8204. #endif
  8205. case WM_COMPONENT_HIDE:
  8206. {
  8207. Byte Component = static_cast<Byte>(M->WParam);
  8208. // sanity check
  8209. if (ComponentVisible[Component])
  8210. {
  8211. // special treatment
  8212. if (Component == fcQueueView)
  8213. {
  8214. ToggleQueueVisibility();
  8215. DebugAssert(!ComponentVisible[fcQueueView]);
  8216. }
  8217. else
  8218. {
  8219. ComponentVisible[Component] = false;
  8220. }
  8221. }
  8222. }
  8223. break;
  8224. case WM_COPYDATA:
  8225. WMCopyData(*M);
  8226. break;
  8227. case WM_MANAGES_CAPTION:
  8228. // caption managed in TTerminalManager::UpdateAppTitle()
  8229. M->Result = 1;
  8230. break;
  8231. case WM_WANTS_MOUSEWHEEL:
  8232. M->Result = 1;
  8233. break;
  8234. case CM_SHOWINGCHANGED:
  8235. CMShowingChanged(*M);
  8236. break;
  8237. case WM_CLOSE:
  8238. WMClose(*M);
  8239. break;
  8240. case WM_DPICHANGED:
  8241. WMDpiChanged(*M);
  8242. break;
  8243. case CM_DPICHANGED:
  8244. CMDpiChanged(*M);
  8245. break;
  8246. case WM_WININICHANGE:
  8247. WMWinIniChange(*M);
  8248. break;
  8249. case CM_DIALOGKEY:
  8250. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8251. break;
  8252. default:
  8253. TForm::Dispatch(Message);
  8254. break;
  8255. }
  8256. }
  8257. //---------------------------------------------------------------------------
  8258. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8259. {
  8260. // noop
  8261. }
  8262. //---------------------------------------------------------------------------
  8263. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8264. {
  8265. TForm::Dispatch(&Message);
  8266. FSessionColors->Clear();
  8267. AddFixedSessionImages();
  8268. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8269. UpdateImages();
  8270. }
  8271. //---------------------------------------------------------------------------
  8272. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8273. {
  8274. TForm::Dispatch(&Message);
  8275. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8276. UpdatePixelsPerInchMainWindowCounter();
  8277. }
  8278. //---------------------------------------------------------------------------
  8279. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8280. {
  8281. // Cannot close window while we are busy.
  8282. // We cannot test this in FormCloseQuery as that is called also from
  8283. // Close(), which is called by CloseApplicationAction. So we can be busy
  8284. // there already even, when it is legitimate to close the application.
  8285. // Possibly a better place to handle this would be WMSysCommand.
  8286. if (NonVisualDataModule->Busy)
  8287. {
  8288. Message.Result = 1;
  8289. }
  8290. else
  8291. {
  8292. TForm::Dispatch(&Message);
  8293. }
  8294. }
  8295. //---------------------------------------------------------------------------
  8296. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8297. {
  8298. TForm::Dispatch(&Message);
  8299. if (Showing && (Terminal == NULL))
  8300. {
  8301. // When we are starting minimized (i.e. from an installer),
  8302. // postpone showing Login dialog until we get restored.
  8303. // Otherwise the Login dialog (and Authentication window) show restored
  8304. // over invidible (minimized) main window.
  8305. if (WindowState == wsMinimized)
  8306. {
  8307. FNeedSession = true;
  8308. }
  8309. else
  8310. {
  8311. // This happens before application ever goes idle, so the toolbars would
  8312. // stay enabled (initial state) until the Login dialog is dismissed.
  8313. UpdateActions();
  8314. NonVisualDataModule->StartBusy();
  8315. try
  8316. {
  8317. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8318. // otherwise the dialog does not receive focus.
  8319. // With Commander interface the ProcessMessages is called already
  8320. // by TDriveView, but with Explorer interface, we need to call it explicily
  8321. Application->ProcessMessages();
  8322. // do not reload sessions, they have been loaded just now (optimization)
  8323. NeedSession(false);
  8324. }
  8325. __finally
  8326. {
  8327. NonVisualDataModule->EndBusy();
  8328. }
  8329. }
  8330. }
  8331. }
  8332. //---------------------------------------------------------------------------
  8333. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8334. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8335. int & MaxHeight)
  8336. {
  8337. // workaround for bug in TWinControl.CalcConstraints
  8338. // Check for empty rect (restore from iconic state) is done there only after
  8339. // call to AdjustClientRect, which enlarges the rect (for forms).
  8340. TRect R = GetClientRect();
  8341. // when restoring from iconic state, place no restrictions
  8342. if (IsRectEmpty(R))
  8343. {
  8344. MinWidth = 0;
  8345. MinHeight = 0;
  8346. MaxWidth = 32000;
  8347. MaxHeight = 32000;
  8348. }
  8349. }
  8350. //---------------------------------------------------------------------------
  8351. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8352. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8353. {
  8354. // terminal can be already closed (e.g. dropped connection)
  8355. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8356. {
  8357. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8358. try
  8359. {
  8360. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8361. }
  8362. catch(...)
  8363. {
  8364. if (!Terminal->Active)
  8365. {
  8366. Close = true;
  8367. }
  8368. throw;
  8369. }
  8370. }
  8371. }
  8372. //---------------------------------------------------------------------------
  8373. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8374. {
  8375. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8376. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8377. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8378. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8379. {
  8380. OnGetSpaceAvailable = GetSpaceAvailable;
  8381. }
  8382. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8383. OnGetSpaceAvailable);
  8384. }
  8385. //---------------------------------------------------------------------------
  8386. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8387. {
  8388. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8389. Terminal->FillSessionDataForCode(Data.get());
  8390. return Data.release();
  8391. }
  8392. //---------------------------------------------------------------------------
  8393. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8394. {
  8395. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8396. DoGenerateUrlDialog(Data.get(), Paths);
  8397. }
  8398. //---------------------------------------------------------------------------
  8399. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8400. {
  8401. GenerateUrl(NULL);
  8402. }
  8403. //---------------------------------------------------------------------------
  8404. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8405. {
  8406. std::unique_ptr<TStrings> Paths(new TStringList());
  8407. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8408. GenerateUrl(Paths.get());
  8409. }
  8410. //---------------------------------------------------------------------------
  8411. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8412. {
  8413. FSessionColor = value;
  8414. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8415. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8416. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8417. DebugAssert(ColorItem != NULL);
  8418. ColorItem->Color = C;
  8419. NonVisualDataModule->ColorMenuItem->Color = C;
  8420. UpdateControls();
  8421. }
  8422. //---------------------------------------------------------------------------
  8423. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8424. {
  8425. if (value != FSessionColor)
  8426. {
  8427. UpdateSessionColor(value);
  8428. // Is null when called from LastTerminalClosed
  8429. if (Terminal != NULL)
  8430. {
  8431. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8432. }
  8433. }
  8434. }
  8435. //---------------------------------------------------------------------------
  8436. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8437. {
  8438. bool Result = FNoteTimer->Enabled;
  8439. if (Result)
  8440. {
  8441. // cannot cancel note too early
  8442. bool NotEarly =
  8443. (Now() - FNoteShown >
  8444. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8445. if (Force || NotEarly)
  8446. {
  8447. FNoteTimer->Enabled = false;
  8448. FNote = L"";
  8449. SAFE_DESTROY(FNoteData);
  8450. FOnNoteClick = NULL;
  8451. FNoteHints = FNotes->Text;
  8452. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8453. UpdateStatusBar();
  8454. }
  8455. }
  8456. return Result;
  8457. }
  8458. //---------------------------------------------------------------------------
  8459. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8460. {
  8461. DebugAssert(FNoteTimer->Enabled);
  8462. CancelNote(true);
  8463. }
  8464. //---------------------------------------------------------------------------
  8465. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8466. {
  8467. int P = Note.Pos(L"\n");
  8468. if (P > 0)
  8469. {
  8470. Note.SetLength(P - 1);
  8471. }
  8472. FNotes->Add(FORMAT(L"[%s] %s",
  8473. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8474. while (FNotes->Count > 10)
  8475. {
  8476. FNotes->Delete(0);
  8477. }
  8478. if (UpdateNow)
  8479. {
  8480. FNoteHints = FNotes->Text;
  8481. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8482. UpdateStatusBar();
  8483. }
  8484. }
  8485. //---------------------------------------------------------------------------
  8486. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8487. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8488. {
  8489. int P = Note.Pos(L"\n");
  8490. if (P > 0)
  8491. {
  8492. Note.SetLength(P - 1);
  8493. }
  8494. FNoteHints = FNotes->Text;
  8495. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8496. FNote = Note;
  8497. SAFE_DESTROY(FNoteData);
  8498. FNoteData = NoteData;
  8499. FOnNoteClick = OnNoteClick;
  8500. AddNote(Note, false);
  8501. UpdateStatusBar();
  8502. FNoteShown = Now();
  8503. FNoteTimer->Enabled = false;
  8504. if (Seconds == 0)
  8505. {
  8506. Seconds = WinConfiguration->NotificationsTimeout;
  8507. }
  8508. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8509. FNoteTimer->Enabled = true;
  8510. }
  8511. //---------------------------------------------------------------------------
  8512. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8513. {
  8514. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8515. }
  8516. //---------------------------------------------------------------------------
  8517. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8518. {
  8519. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8520. {
  8521. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8522. }
  8523. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8524. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8525. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8526. 0, NULL, NULL);
  8527. }
  8528. //---------------------------------------------------------------------------
  8529. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8530. {
  8531. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8532. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8533. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8534. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8535. }
  8536. //---------------------------------------------------------------------------
  8537. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8538. {
  8539. WinConfiguration->FormatSizeBytes = Style;
  8540. }
  8541. //---------------------------------------------------------------------------
  8542. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8543. {
  8544. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8545. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8546. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8547. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8548. }
  8549. //---------------------------------------------------------------------------
  8550. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8551. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8552. {
  8553. if (Panel->Index == 0)
  8554. {
  8555. if (FOnNoteClick != NULL)
  8556. {
  8557. // prevent the user data from being freed by possible call
  8558. // to CancelNote or PostNote during call to OnNoteClick
  8559. std::unique_ptr<TObject> NoteData(FNoteData);
  8560. TNotifyEvent OnNoteClick = FOnNoteClick;
  8561. FNoteData = NULL;
  8562. // need to cancel the note as we are going to delete its user data
  8563. CancelNote(true);
  8564. OnNoteClick(NoteData.get());
  8565. }
  8566. }
  8567. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8568. {
  8569. FileSystemInfo();
  8570. }
  8571. }
  8572. //---------------------------------------------------------------------------
  8573. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8574. {
  8575. // workaround:
  8576. // 1) for unknown reason, disabling window, while minimized,
  8577. // prevents it from restoring, even if it was enabled again meanwhile
  8578. // 2) when disabling the main window, while another has focus
  8579. // minimize is no longer possible
  8580. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8581. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8582. // Shouldn't we use IsApplicationMinimized() here?
  8583. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8584. {
  8585. Enabled = false;
  8586. }
  8587. FLockLevel++;
  8588. }
  8589. //---------------------------------------------------------------------------
  8590. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8591. {
  8592. DebugAssert(FLockLevel > 0);
  8593. FLockLevel--;
  8594. if (FLockLevel == 0)
  8595. {
  8596. DebugAssert(FLockSuspendLevel == 0);
  8597. Enabled = true;
  8598. }
  8599. }
  8600. //---------------------------------------------------------------------------
  8601. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8602. {
  8603. // We need to make sure that window is enabled when the last modal window closes
  8604. // otherwise focus is not restored correctly.
  8605. // So we re-enable the window when modal window opens and
  8606. // disable it back after is closes
  8607. if (FLockLevel > 0)
  8608. {
  8609. // while we have nesting counter, we know that we never be called
  8610. // recursivelly as Application->OnModalBegin is called only
  8611. // for the top-level modal window
  8612. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8613. {
  8614. // won't be disabled when conditions in LockWindow() were not satisfied
  8615. FDisabledOnLockSuspend = !Enabled;
  8616. // When minimized to tray (or actually when set to SW_HIDE),
  8617. // setting Enabled makes the window focusable even when there's
  8618. // modal window over it
  8619. if (!FTrayIcon->Visible)
  8620. {
  8621. Enabled = true;
  8622. }
  8623. }
  8624. FLockSuspendLevel++;
  8625. }
  8626. }
  8627. //---------------------------------------------------------------------------
  8628. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8629. {
  8630. if (FLockSuspendLevel > 0)
  8631. {
  8632. DebugAssert(FLockLevel > 0);
  8633. FLockSuspendLevel--;
  8634. // see comment in SuspendWindowLock
  8635. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8636. {
  8637. // Note that window can be enabled here, when we were minized to tray when
  8638. // was SuspendWindowLock() called.
  8639. // We should possibly do the same check as in LockWindow(),
  8640. // if it is ever possible that the consitions change between
  8641. // SuspendWindowLock() and ResumeWindowLock()
  8642. Enabled = !FDisabledOnLockSuspend;
  8643. }
  8644. }
  8645. }
  8646. //---------------------------------------------------------------------------
  8647. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8648. {
  8649. if (!TextOnly)
  8650. {
  8651. TTBXComboBoxItem * RemotePathComboBox =
  8652. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8653. TStrings * Items = RemotePathComboBox->Strings;
  8654. Items->BeginUpdate();
  8655. try
  8656. {
  8657. Items->Clear();
  8658. if (Terminal != NULL)
  8659. {
  8660. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8661. while (!IsUnixRootPath(APath))
  8662. {
  8663. int P = APath.LastDelimiter(L'/');
  8664. DebugAssert(P >= 0);
  8665. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8666. APath.SetLength(P - 1);
  8667. }
  8668. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8669. }
  8670. }
  8671. __finally
  8672. {
  8673. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8674. // Setting ItemIndex to -1 does not reset its text
  8675. if (Items->Count == 0)
  8676. {
  8677. RemotePathComboBox->Text = L"";
  8678. }
  8679. Items->EndUpdate();
  8680. }
  8681. }
  8682. }
  8683. //---------------------------------------------------------------------------
  8684. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8685. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8686. int & ImageIndex)
  8687. {
  8688. if (AIndex < 0)
  8689. {
  8690. AIndex = Sender->ItemIndex;
  8691. }
  8692. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8693. }
  8694. //---------------------------------------------------------------------------
  8695. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8696. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8697. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8698. {
  8699. ARect.Left += (10 * AIndex);
  8700. }
  8701. //---------------------------------------------------------------------------
  8702. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8703. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8704. {
  8705. AWidth += (10 * AIndex);
  8706. }
  8707. //---------------------------------------------------------------------------
  8708. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8709. TObject * Sender)
  8710. {
  8711. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8712. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8713. int Index = RemotePathComboBox->ItemIndex;
  8714. while (Index < RemotePathComboBox->Strings->Count - 1)
  8715. {
  8716. APath = UnixExtractFileDir(APath);
  8717. Index++;
  8718. }
  8719. // VanDyke style paths
  8720. if (APath.IsEmpty())
  8721. {
  8722. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8723. APath = ROOTDIRECTORY;
  8724. }
  8725. if (RemoteDirView->Path != APath)
  8726. {
  8727. RemoteDirView->Path = APath;
  8728. }
  8729. }
  8730. //---------------------------------------------------------------------------
  8731. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8732. {
  8733. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8734. DebugUsedParam(Sender);
  8735. UpdateRemotePathComboBox(true);
  8736. }
  8737. //---------------------------------------------------------------------------
  8738. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8739. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8740. {
  8741. ResetIncrementalSearch();
  8742. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8743. DebugAssert(DirView != NULL);
  8744. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8745. {
  8746. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8747. // OnEditing is called also from TCustomListView::CanEdit
  8748. if (Edit != NULL)
  8749. {
  8750. EditSelectBaseName(Edit);
  8751. }
  8752. }
  8753. }
  8754. //---------------------------------------------------------------------------
  8755. //---------------------------------------------------------------------------
  8756. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8757. {
  8758. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8759. Result->AutoDetectDnD = false;
  8760. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8761. Result->RenderDataOn = rdoEnterAndDropSync;
  8762. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8763. return Result;
  8764. }
  8765. //---------------------------------------------------------------------------
  8766. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8767. {
  8768. TForm::CreateWnd();
  8769. if (FSessionsDragDropFilesEx == NULL)
  8770. {
  8771. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8772. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8773. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8774. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8775. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8776. }
  8777. if (FQueueDragDropFilesEx == NULL)
  8778. {
  8779. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8780. // No need to set OnDragOver as we do not have any restrictions
  8781. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8782. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8783. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8784. }
  8785. }
  8786. //---------------------------------------------------------------------------
  8787. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8788. {
  8789. TForm::DestroyWnd();
  8790. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8791. FQueueDragDropFilesEx->DragDropControl = NULL;
  8792. }
  8793. //---------------------------------------------------------------------------
  8794. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8795. {
  8796. SideEnter(FCurrentSide);
  8797. FEverShown = true;
  8798. }
  8799. //---------------------------------------------------------------------------
  8800. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8801. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8802. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8803. {
  8804. if (!NonVisualDataModule->Busy)
  8805. {
  8806. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8807. Configuration->Usage->Inc(L"FileFinds");
  8808. LockWindow(true);
  8809. NonVisualDataModule->StartBusy();
  8810. WinConfiguration->LockedInterface = true;
  8811. try
  8812. {
  8813. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8814. }
  8815. __finally
  8816. {
  8817. WinConfiguration->LockedInterface = false;
  8818. NonVisualDataModule->EndBusy();
  8819. UnlockWindow();
  8820. }
  8821. }
  8822. }
  8823. //---------------------------------------------------------------------------
  8824. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8825. {
  8826. if (!NonVisualDataModule->Busy)
  8827. {
  8828. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8829. SetFocus();
  8830. RemoteDirView->Path = UnixExtractFilePath(Path);
  8831. UnicodeString FileName = UnixExtractFileName(Path);
  8832. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  8833. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  8834. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  8835. if (Item == NULL)
  8836. {
  8837. ReloadDirectory(osRemote);
  8838. Item = RemoteDirView->FindFileItem(FileName);
  8839. }
  8840. if (Item != NULL)
  8841. {
  8842. RemoteDirView->ItemFocused = Item;
  8843. RemoteDirView->ItemFocused->MakeVisible(false);
  8844. RemoteDirView->SetFocus();
  8845. }
  8846. }
  8847. }
  8848. //---------------------------------------------------------------------------
  8849. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8850. {
  8851. bool Result = !NonVisualDataModule->Busy;
  8852. if (Result)
  8853. {
  8854. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8855. }
  8856. return Result;
  8857. }
  8858. //---------------------------------------------------------------------------
  8859. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8860. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8861. {
  8862. if (CanOperateOnFoundFiles(ATerminal))
  8863. {
  8864. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8865. FOnFileOperationFinished = OnFileOperationFinished;
  8866. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8867. }
  8868. }
  8869. //---------------------------------------------------------------------------
  8870. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8871. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8872. {
  8873. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8874. }
  8875. //---------------------------------------------------------------------------
  8876. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8877. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8878. {
  8879. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8880. }
  8881. //---------------------------------------------------------------------------
  8882. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8883. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8884. {
  8885. if (CanOperateOnFoundFiles(ATerminal))
  8886. {
  8887. for (int Index = 0; Index < FileList->Count; Index++)
  8888. {
  8889. UnicodeString FileName = FileList->Strings[Index];
  8890. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8891. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8892. OnFileOperationFinished(FileName, true);
  8893. }
  8894. }
  8895. }
  8896. //---------------------------------------------------------------------------
  8897. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8898. {
  8899. FFileFindTerminal = Terminal;
  8900. ShowFileFindDialog(
  8901. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8902. DoDownloadFoundFiles, DoEditFoundFiles);
  8903. }
  8904. //---------------------------------------------------------------------------
  8905. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8906. {
  8907. FTaskbarList = TaskbarList;
  8908. }
  8909. //---------------------------------------------------------------------------
  8910. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8911. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8912. {
  8913. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8914. if (Index >= 0)
  8915. {
  8916. if (Button == mbLeft)
  8917. {
  8918. // "Mouse down" is raised only after tab is switched.
  8919. // If switching tab (switching session) takes long enough for user
  8920. // to actually release the button, "mouse down" is still raised,
  8921. // but we do not get "mouse up" event, so dragging is not cancelled,
  8922. // prevent that by not beginning dragging in the first place.
  8923. if (IsKeyPressed(VK_LBUTTON))
  8924. {
  8925. // when user clicks the "+", we get mouse down only after the session
  8926. // is closed, when new session tab is already on X:Y, so dragging
  8927. // starts, prevent that
  8928. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8929. {
  8930. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8931. if (Terminal != NULL)
  8932. {
  8933. SessionsPageControl->BeginDrag(false);
  8934. }
  8935. }
  8936. }
  8937. }
  8938. else if (Button == mbMiddle)
  8939. {
  8940. // ignore middle-click for "New session tab"
  8941. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8942. if (Terminal != NULL)
  8943. {
  8944. CloseSessionTab(Index);
  8945. }
  8946. }
  8947. }
  8948. }
  8949. //---------------------------------------------------------------------------
  8950. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8951. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8952. {
  8953. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8954. // do not allow dropping on the "+" tab
  8955. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8956. if ((TargetTerminal != NULL) &&
  8957. (SessionsPageControl->ActivePage->PageIndex != Index))
  8958. {
  8959. Configuration->Usage->Inc(L"SessionTabMoves");
  8960. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8961. // but we want to actually prevent that to avoid flicker
  8962. SessionsPageControl->ActivePage->PageIndex = Index;
  8963. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8964. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8965. }
  8966. }
  8967. //---------------------------------------------------------------------------
  8968. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8969. TObject * Sender, TObject * Source, int X, int Y,
  8970. TDragState /*State*/, bool & Accept)
  8971. {
  8972. Accept = (Sender == Source);
  8973. if (Accept)
  8974. {
  8975. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8976. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8977. // do not allow dragging to the "+" tab
  8978. Accept = (Terminal != NULL);
  8979. }
  8980. }
  8981. //---------------------------------------------------------------------------
  8982. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8983. const TPoint & Point, int & Effect)
  8984. {
  8985. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8986. if (Index < 0)
  8987. {
  8988. Effect = DROPEFFECT_NONE;
  8989. }
  8990. else
  8991. {
  8992. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8993. // do not allow dropping on the "+" tab
  8994. if (TargetTerminal == NULL)
  8995. {
  8996. Effect = DROPEFFECT_NONE;
  8997. }
  8998. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  8999. {
  9000. Effect = DROPEFFECT_COPY;
  9001. }
  9002. }
  9003. }
  9004. //---------------------------------------------------------------------------
  9005. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9006. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9007. {
  9008. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9009. // do not allow dropping on the "+" tab
  9010. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9011. if (TargetTerminal != NULL)
  9012. {
  9013. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9014. if (!IsFileControl(DropSourceControl, osRemote))
  9015. {
  9016. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9017. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9018. // Why don't we use Terminal->CurrentDirectory directly?
  9019. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9020. // do not force queue, drag drop
  9021. false, true);
  9022. }
  9023. }
  9024. }
  9025. //---------------------------------------------------------------------------
  9026. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9027. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9028. {
  9029. // Downloads are handled in RemoteFileControlDDEnd
  9030. if (!IsFileControl(DropSourceControl, osRemote))
  9031. {
  9032. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9033. Terminal->CurrentDirectory,
  9034. // force queue, drag drop
  9035. true, true);
  9036. }
  9037. }
  9038. //---------------------------------------------------------------------------
  9039. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9040. {
  9041. FShowing = false;
  9042. // Do not save empty workspace
  9043. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9044. {
  9045. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9046. UnicodeString Name = WorkspaceName();
  9047. DoSaveWorkspace(
  9048. Name, DataList.get(),
  9049. !Configuration->DisablePasswordStoring &&
  9050. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9051. WinConfiguration->LastStoredSession = Name;
  9052. }
  9053. }
  9054. //---------------------------------------------------------------------------
  9055. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9056. {
  9057. TManagedTerminal * ManagedTerminal =
  9058. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9059. if (ManagedTerminal != NULL)
  9060. {
  9061. ManagedTerminal->DirectoryLoaded = Now();
  9062. }
  9063. }
  9064. //---------------------------------------------------------------------------
  9065. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9066. TListItem * /*Item*/, bool /*Selected*/)
  9067. {
  9068. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9069. switch (DirView->LastSelectMethod)
  9070. {
  9071. case smKeyboard:
  9072. Configuration->Usage->Inc(L"KeyboardSelections");
  9073. break;
  9074. case smMouse:
  9075. Configuration->Usage->Inc(L"MouseSelections");
  9076. break;
  9077. }
  9078. }
  9079. //---------------------------------------------------------------------------
  9080. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9081. {
  9082. if (Color != 0)
  9083. {
  9084. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9085. }
  9086. else
  9087. {
  9088. return FSessionTabImageIndex;
  9089. }
  9090. }
  9091. //---------------------------------------------------------------------------
  9092. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9093. {
  9094. TPngImageCollectionItem * Item =
  9095. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9096. FSessionColors->AddPng(Item->PngImage);
  9097. return FSessionColors->Count - 1;
  9098. }
  9099. //---------------------------------------------------------------------------
  9100. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9101. {
  9102. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9103. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9104. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9105. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9106. }
  9107. //---------------------------------------------------------------------------
  9108. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9109. {
  9110. for (int Index = 0; Index < Control->ControlCount; Index++)
  9111. {
  9112. TControl * AControl = Control->Controls[Index];
  9113. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9114. if (WinControl != NULL)
  9115. {
  9116. CollectItemsWithTextDisplayMode(WinControl);
  9117. }
  9118. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9119. if (Toolbar != NULL)
  9120. {
  9121. // we care for top-level items only
  9122. TTBCustomItem * Items = Toolbar->Items;
  9123. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9124. {
  9125. TTBCustomItem * Item = Items->Items[ItemIndex];
  9126. if (((Item->DisplayMode == nbdmImageAndText) ||
  9127. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9128. EligibleForImageDisplayMode(Item))
  9129. {
  9130. FItemsWithTextDisplayMode.insert(Item);
  9131. }
  9132. }
  9133. }
  9134. }
  9135. }
  9136. //---------------------------------------------------------------------------
  9137. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9138. {
  9139. return true;
  9140. }
  9141. //---------------------------------------------------------------------------
  9142. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9143. {
  9144. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9145. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9146. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9147. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9148. bool Result = true;
  9149. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9150. {
  9151. TTBCustomItem * Item = *i;
  9152. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9153. if (Label != NULL)
  9154. {
  9155. // optimization
  9156. if (Label->Visible == SelectiveToolbarText)
  9157. {
  9158. Result = false;
  9159. }
  9160. else
  9161. {
  9162. Label->Visible = SelectiveToolbarText;
  9163. }
  9164. }
  9165. else
  9166. {
  9167. // optimization
  9168. if (Item->DisplayMode == DisplayMode)
  9169. {
  9170. Result = false;
  9171. }
  9172. else
  9173. {
  9174. Item->DisplayMode = DisplayMode;
  9175. }
  9176. }
  9177. ++i;
  9178. }
  9179. if (Result)
  9180. {
  9181. UpdateNewSessionTab();
  9182. }
  9183. return Result;
  9184. }
  9185. //---------------------------------------------------------------------------
  9186. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9187. {
  9188. DebugFail();
  9189. }
  9190. //---------------------------------------------------------------------------
  9191. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9192. {
  9193. // Among other, a lock level is non zero, while directory is being loaded,
  9194. // even when it happens as a result of dirview navigation,
  9195. // i.e. when TNonVisualDataModule is NOT FBusy.
  9196. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9197. // Among other this prevents a panel auto update to occur while
  9198. // directory is changing.
  9199. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9200. }
  9201. //---------------------------------------------------------------------------
  9202. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9203. {
  9204. // While the window is disabled, we still seem to process menu shortcuts at least,
  9205. // so stop it at least here.
  9206. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9207. return
  9208. (Allowed == aaUpdate) ||
  9209. !NonVisualDataModule->Busy;
  9210. }
  9211. //---------------------------------------------------------------------------
  9212. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9213. {
  9214. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9215. }
  9216. //---------------------------------------------------------------------------
  9217. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9218. {
  9219. // This is somewhat redundant to LockWindow() call from
  9220. // TTerminalManager::TerminalReadDirectoryProgress.
  9221. // But disabling window is known not to block keyboard shorcuts
  9222. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9223. if (Busy > 0)
  9224. {
  9225. if (NonVisualDataModule->Busy)
  9226. {
  9227. State = false;
  9228. }
  9229. else
  9230. {
  9231. NonVisualDataModule->StartBusy();
  9232. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9233. LockWindow();
  9234. }
  9235. }
  9236. else if (Busy < 0)
  9237. {
  9238. UnlockWindow();
  9239. NonVisualDataModule->EndBusy();
  9240. }
  9241. else
  9242. {
  9243. State = NonVisualDataModule->Busy;
  9244. }
  9245. }
  9246. //---------------------------------------------------------------------------
  9247. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9248. {
  9249. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9250. // no context menu for "New session tab"
  9251. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9252. {
  9253. SessionsPageControl->ActivePageIndex = Index;
  9254. if (DebugAlwaysTrue(SessionTabSwitched()))
  9255. {
  9256. // copied from TControl.WMContextMenu
  9257. SendCancelMode(SessionsPageControl);
  9258. // explicit popup instead of using PopupMenu property
  9259. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9260. // while connecting yet not-connected session and hence
  9261. // allowing an access to commands over not-completelly connected session
  9262. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9263. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9264. PopupMenu->PopupComponent = SessionsPageControl;
  9265. PopupMenu->Popup(Point.x, Point.y);
  9266. }
  9267. }
  9268. Handled = true;
  9269. }
  9270. //---------------------------------------------------------------------------
  9271. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9272. {
  9273. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9274. }
  9275. //---------------------------------------------------------------------------
  9276. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9277. {
  9278. Configuration->Usage->Inc(L"PasswordChanges");
  9279. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9280. ChangePasswordData->DisableAuthentationsExceptPassword();
  9281. ChangePasswordData->ChangePassword = true;
  9282. std::unique_ptr<TTerminal> ChangePasswordSession(
  9283. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9284. ChangePasswordSession->AutoReadDirectory = false;
  9285. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9286. {
  9287. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9288. }
  9289. else
  9290. {
  9291. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9292. }
  9293. }
  9294. //---------------------------------------------------------------------------
  9295. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9296. {
  9297. // No nice way to assert SSH2
  9298. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9299. }
  9300. //---------------------------------------------------------------------------
  9301. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9302. {
  9303. TOperationVisualizer OperationVisualizer;
  9304. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9305. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9306. }
  9307. //---------------------------------------------------------------------------
  9308. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9309. {
  9310. TForm::ChangeScale(M, D);
  9311. int APixelsPerInch = GetControlPixelsPerInch(this);
  9312. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9313. try
  9314. {
  9315. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9316. }
  9317. __finally
  9318. {
  9319. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9320. }
  9321. }
  9322. //---------------------------------------------------------------------------
  9323. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9324. {
  9325. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9326. }
  9327. //---------------------------------------------------------------------
  9328. class TPublicControl : public TControl
  9329. {
  9330. friend class TCustomScpExplorerForm;
  9331. };
  9332. //---------------------------------------------------------------------------
  9333. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9334. {
  9335. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9336. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9337. }
  9338. //---------------------------------------------------------------------------
  9339. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9340. {
  9341. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9342. OpenBookmark(Side, Bookmark);
  9343. }
  9344. //---------------------------------------------------------------------------
  9345. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9346. {
  9347. DoBookmarkClick(osLocal, Sender);
  9348. }
  9349. //---------------------------------------------------------------------------
  9350. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9351. {
  9352. DoBookmarkClick(osRemote, Sender);
  9353. }
  9354. //---------------------------------------------------------------------------
  9355. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9356. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9357. {
  9358. if (BookmarkList != NULL)
  9359. {
  9360. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9361. if (!WinConfiguration->UseLocationProfiles)
  9362. {
  9363. std::unique_ptr<TStringList> Directories(new TStringList());
  9364. Directories->CaseSensitive = (Side == osRemote);
  9365. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9366. {
  9367. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9368. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9369. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9370. {
  9371. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9372. Item->Caption = EscapeHotkey(Directory);
  9373. Item->ShortCut = Bookmark->ShortCut;
  9374. Item->OnClick = OnBookmarkClick;
  9375. Item->Tag = reinterpret_cast<int>(Bookmark);
  9376. Directories->Add(Directory);
  9377. Menu->Add(Item.release());
  9378. }
  9379. }
  9380. }
  9381. else
  9382. {
  9383. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9384. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9385. {
  9386. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9387. if (!Bookmark->Node.IsEmpty())
  9388. {
  9389. Folders->Add(Bookmark->Node);
  9390. }
  9391. }
  9392. for (int Index = 0; Index < Folders->Count; Index++)
  9393. {
  9394. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9395. Item->Caption = Folders->Strings[Index];
  9396. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9397. Folders->Objects[Index] = Item.get();
  9398. Menu->Add(Item.release());
  9399. }
  9400. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9401. {
  9402. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9403. TTBCustomItem * Parent;
  9404. if (!Bookmark->Node.IsEmpty())
  9405. {
  9406. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9407. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9408. }
  9409. else
  9410. {
  9411. Parent = Menu;
  9412. }
  9413. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9414. Item->Caption = Bookmark->Name;
  9415. Item->ShortCut = Bookmark->ShortCut;
  9416. Item->OnClick = OnBookmarkClick;
  9417. Item->Tag = reinterpret_cast<int>(Bookmark);
  9418. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9419. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9420. {
  9421. // No hint for location profiles that are actually mere "bookmarks"
  9422. }
  9423. else
  9424. {
  9425. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9426. UnicodeString LongHint;
  9427. if (!Bookmark->Local.IsEmpty())
  9428. {
  9429. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9430. }
  9431. if (!Bookmark->Remote.IsEmpty())
  9432. {
  9433. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9434. }
  9435. AddToList(Hint, LongHint, L"|");
  9436. Item->Hint = Hint;
  9437. }
  9438. Parent->Add(Item.release());
  9439. }
  9440. }
  9441. AddMenuSeparator(Menu);
  9442. }
  9443. }
  9444. //---------------------------------------------------------------------------
  9445. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9446. {
  9447. Menu->Clear();
  9448. std::unique_ptr<TTBCustomItem> Item;
  9449. Item.reset(new TTBXItem(Owner));
  9450. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9451. Item->Options = Item->Options << tboDefault;
  9452. Menu->Add(Item.release());
  9453. AddMenuSeparator(Menu);
  9454. if (Terminal != NULL)
  9455. {
  9456. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9457. }
  9458. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9459. Item.reset(new TTBXItem(Owner));
  9460. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9461. Menu->Add(Item.release());
  9462. }
  9463. //---------------------------------------------------------------------------
  9464. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9465. {
  9466. bool Result = true;
  9467. try
  9468. {
  9469. if (Side == osRemote)
  9470. {
  9471. Terminal->ExceptionOnFail = true;
  9472. }
  9473. try
  9474. {
  9475. DirView(Side)->Path = Path;
  9476. }
  9477. __finally
  9478. {
  9479. if (Side == osRemote)
  9480. {
  9481. Terminal->ExceptionOnFail = false;
  9482. }
  9483. }
  9484. }
  9485. catch (Exception & E)
  9486. {
  9487. if ((Side != osRemote) || Terminal->Active)
  9488. {
  9489. ShowExtendedExceptionEx(Terminal, &E);
  9490. Result = false;
  9491. }
  9492. else
  9493. {
  9494. throw;
  9495. }
  9496. }
  9497. return Result;
  9498. }
  9499. //---------------------------------------------------------------------------
  9500. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9501. {
  9502. // Make sure there some feedback even when loading tine local folders
  9503. TInstantOperationVisualizer Visualizer;
  9504. DirView(Side)->ReloadDirectory();
  9505. }
  9506. //---------------------------------------------------------------------------
  9507. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9508. {
  9509. if (Index == SessionsPageControl->TabIndex)
  9510. {
  9511. CloseSession();
  9512. }
  9513. else
  9514. {
  9515. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9516. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9517. TTerminalManager * Manager = TTerminalManager::Instance();
  9518. if (!Terminal->Active)
  9519. {
  9520. Manager->FreeTerminal(Terminal);
  9521. }
  9522. else
  9523. {
  9524. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9525. if (CanCloseQueue(Queue))
  9526. {
  9527. Manager->FreeTerminal(Terminal);
  9528. }
  9529. }
  9530. }
  9531. }
  9532. //---------------------------------------------------------------------------
  9533. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9534. {
  9535. CloseSessionTab(Index);
  9536. }
  9537. //---------------------------------------------------------------------------
  9538. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9539. {
  9540. if (DebugAlwaysTrue(GetSide(Side) == osRemote))
  9541. {
  9542. TInstantOperationVisualizer Visualizer;
  9543. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9544. // before we create a new data object
  9545. ClipboardClear();
  9546. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9547. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9548. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9549. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9550. if (FClipboardDragDropFilesEx.get() == NULL)
  9551. {
  9552. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9553. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9554. }
  9555. FClipboardDragDropFilesEx->FileList->Clear();
  9556. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9557. FClipboardDragDropFilesEx->CopyToClipboard();
  9558. FClipboardTerminal = FTerminal;
  9559. // Need full paths, as cwd can be different once files are pasted
  9560. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9561. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9562. {
  9563. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9564. }
  9565. }
  9566. }
  9567. //---------------------------------------------------------------------------
  9568. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9569. {
  9570. if (OpenClipboard(0))
  9571. {
  9572. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9573. CloseClipboard();
  9574. }
  9575. }
  9576. //---------------------------------------------------------------------------
  9577. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9578. {
  9579. return !FClipboardFakeDirectory.IsEmpty();
  9580. }
  9581. //---------------------------------------------------------------------------
  9582. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9583. {
  9584. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9585. FClipboardFakeDirectory = UnicodeString();
  9586. FClipboardTerminal = NULL;
  9587. // Also called in Idle()
  9588. FClipboardFakeMonitors.reset(NULL);
  9589. }
  9590. //---------------------------------------------------------------------------
  9591. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9592. {
  9593. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9594. {
  9595. ClipboardStop();
  9596. }
  9597. }
  9598. //---------------------------------------------------------------------------
  9599. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9600. {
  9601. TTerminalManager * Manager = TTerminalManager::Instance();
  9602. Manager->ActiveTerminal = FClipboardTerminal;
  9603. TTransferOperationParam Params;
  9604. Params.TargetDirectory = TargetDirectory;
  9605. Params.DragDrop = DragDrop;
  9606. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9607. }
  9608. //---------------------------------------------------------------------------
  9609. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9610. {
  9611. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9612. // (as it pumps a Windows message queue while beign shutted down)
  9613. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9614. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9615. {
  9616. // Can fail as it can be e.g. locked by AV, so we retry that later
  9617. bool Removed = RemoveDir(ApiPath(FileName));
  9618. try
  9619. {
  9620. if (!NonVisualDataModule->Busy)
  9621. {
  9622. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9623. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9624. }
  9625. }
  9626. __finally
  9627. {
  9628. if (!Removed)
  9629. {
  9630. RemoveDir(ApiPath(FileName));
  9631. }
  9632. }
  9633. }
  9634. }
  9635. //---------------------------------------------------------------------------
  9636. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9637. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9638. {
  9639. TFileMasks::TParams MaskParams;
  9640. MaskParams.Size = Size;
  9641. MaskParams.Modification = Modification;
  9642. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9643. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9644. {
  9645. bool ImplicitMatch;
  9646. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9647. !ImplicitMatch)
  9648. {
  9649. Color = Iter->Color;
  9650. break;
  9651. }
  9652. }
  9653. }
  9654. //---------------------------------------------------------------------------
  9655. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9656. {
  9657. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9658. if (!DirView->IsEditing())
  9659. {
  9660. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9661. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9662. // while searching
  9663. if (Key == VK_BACK)
  9664. {
  9665. if (!FIncrementalSearch.IsEmpty())
  9666. {
  9667. if (FIncrementalSearch.Length() == 1)
  9668. {
  9669. ResetIncrementalSearch();
  9670. }
  9671. else
  9672. {
  9673. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9674. IncrementalSearch(NewText, false, false);
  9675. }
  9676. Key = 0;
  9677. }
  9678. }
  9679. }
  9680. }
  9681. //---------------------------------------------------------------------------
  9682. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9683. {
  9684. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9685. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9686. {
  9687. // Filter control sequences.
  9688. // When not searching yet, prefer use of the space for toggling file selection
  9689. // (so we cannot incrementally search for a string starting with a space).
  9690. if ((Key > VK_SPACE) ||
  9691. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9692. {
  9693. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9694. Key = 0;
  9695. }
  9696. }
  9697. }
  9698. //---------------------------------------------------------------------------
  9699. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9700. {
  9701. if (!FIncrementalSearch.IsEmpty())
  9702. {
  9703. FIncrementalSearch = L"";
  9704. DirView(osCurrent)->UpdateStatusBar();
  9705. }
  9706. }
  9707. //---------------------------------------------------------------------------
  9708. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9709. {
  9710. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9711. if (Item == NULL)
  9712. {
  9713. MessageBeep(MB_ICONHAND);
  9714. }
  9715. else
  9716. {
  9717. TCustomDirView * ADirView = DirView(osCurrent);
  9718. {
  9719. TAutoNestingCounter Guard(FIncrementalSearching);
  9720. ADirView->FocusItem(Item);
  9721. if (ADirView->NortonLike == nlOff)
  9722. {
  9723. if ((ADirView->Selected != Item) ||
  9724. (ADirView->SelCount > 1))
  9725. {
  9726. ADirView->ClearSelection();
  9727. ADirView->Selected = Item;
  9728. }
  9729. }
  9730. }
  9731. FIncrementalSearch = Text;
  9732. Item->MakeVisible(false);
  9733. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9734. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9735. ADirView->UpdateStatusBar();
  9736. }
  9737. }
  9738. //---------------------------------------------------------------------------
  9739. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9740. {
  9741. int Index = Item->Index;
  9742. if (!Reverse)
  9743. {
  9744. Index++;
  9745. if (Index >= Item->Owner->Count)
  9746. {
  9747. Index = 0;
  9748. }
  9749. }
  9750. else
  9751. {
  9752. Index--;
  9753. if (Index < 0)
  9754. {
  9755. Index = Item->Owner->Count - 1;
  9756. }
  9757. }
  9758. return Item->Owner->Item[Index];
  9759. }
  9760. //---------------------------------------------------------------------------
  9761. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9762. {
  9763. TCustomDirView * ADirView = DirView(osCurrent);
  9764. if (ADirView->Items->Count == 0)
  9765. {
  9766. return NULL;
  9767. }
  9768. else
  9769. {
  9770. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9771. TListItem * Item = CurrentItem;
  9772. if (SkipCurrent)
  9773. {
  9774. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9775. }
  9776. while (true)
  9777. {
  9778. bool Matches = false;
  9779. switch (WinConfiguration->PanelSearch)
  9780. {
  9781. case isNameStartOnly:
  9782. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9783. break;
  9784. case isName:
  9785. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9786. break;
  9787. case isAll:
  9788. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9789. int Index = 0;
  9790. while ((Index < ColCount) && !Matches)
  9791. {
  9792. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9793. if (Visible)
  9794. {
  9795. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9796. }
  9797. Index++;
  9798. }
  9799. break;
  9800. }
  9801. if (Matches)
  9802. {
  9803. return Item;
  9804. }
  9805. Item = GetNextFile(Item, Reverse);
  9806. if (Item == CurrentItem)
  9807. {
  9808. return NULL;
  9809. }
  9810. }
  9811. }
  9812. }
  9813. //---------------------------------------------------------------------------
  9814. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9815. {
  9816. ResetIncrementalSearch();
  9817. }
  9818. //---------------------------------------------------------------------------
  9819. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9820. {
  9821. if (Message.CharCode == VK_TAB)
  9822. {
  9823. if (!FIncrementalSearch.IsEmpty())
  9824. {
  9825. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  9826. bool Reverse = Shift.Contains(ssShift);
  9827. IncrementalSearch(FIncrementalSearch, true, Reverse);
  9828. Message.Result = 1;
  9829. return;
  9830. }
  9831. }
  9832. else if (Message.CharCode == VK_ESCAPE)
  9833. {
  9834. if (!FIncrementalSearch.IsEmpty())
  9835. {
  9836. ResetIncrementalSearch();
  9837. Message.Result = 1;
  9838. return;
  9839. }
  9840. }
  9841. TForm::Dispatch(&Message);
  9842. }
  9843. //---------------------------------------------------------------------------
  9844. void __fastcall TCustomScpExplorerForm::Deactivate()
  9845. {
  9846. TForm::Deactivate();
  9847. // E.g. when switching to an internal editor window
  9848. ResetIncrementalSearch();
  9849. }
  9850. //---------------------------------------------------------------------------
  9851. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  9852. {
  9853. // When switching to another application
  9854. ResetIncrementalSearch();
  9855. }
  9856. //---------------------------------------------------------------------------
  9857. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  9858. {
  9859. ResetIncrementalSearch();
  9860. }
  9861. //---------------------------------------------------------------------------
  9862. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  9863. {
  9864. if (FIncrementalSearching <= 0)
  9865. {
  9866. ResetIncrementalSearch();
  9867. }
  9868. }
  9869. //---------------------------------------------------------------------------
  9870. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  9871. {
  9872. CountClicksForWindowPrint(this);
  9873. }
  9874. //---------------------------------------------------------------------------