CustomScpExplorer.cpp 344 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538
  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 WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 4;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. class TAutoBatch
  101. {
  102. public:
  103. TAutoBatch(TCustomScpExplorerForm * Form) :
  104. FForm(Form)
  105. {
  106. FForm->BatchStart(FBatchStorage);
  107. }
  108. ~TAutoBatch()
  109. {
  110. FForm->BatchEnd(FBatchStorage);
  111. }
  112. private:
  113. TCustomScpExplorerForm * FForm;
  114. void * FBatchStorage;
  115. };
  116. //---------------------------------------------------------------------------
  117. struct TTransferOperationParam
  118. {
  119. TTransferOperationParam();
  120. UnicodeString TargetDirectory;
  121. bool Temp;
  122. bool DragDrop;
  123. int Options;
  124. TAutoSwitch Queue;
  125. };
  126. //---------------------------------------------------------------------------
  127. TTransferOperationParam::TTransferOperationParam()
  128. {
  129. Temp = false;
  130. DragDrop = false;
  131. Options = 0;
  132. Queue = asAuto;
  133. }
  134. //---------------------------------------------------------------------------
  135. class TTransferPresetNoteData : public TObject
  136. {
  137. public:
  138. UnicodeString Message;
  139. };
  140. //---------------------------------------------------------------------------
  141. class TTerminalNoteData : public TObject
  142. {
  143. public:
  144. TTerminal * Terminal;
  145. };
  146. //---------------------------------------------------------------------------
  147. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  148. FFormRestored(false),
  149. TForm(Owner)
  150. {
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  158. NonVisualDataModule->ScpExplorer = this;
  159. FAutoOperation = false;
  160. FOnFileOperationFinished = NULL;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDExtMapFile = NULL;
  169. // CreateMutexW keeps failing with ERROR_NOACCESS
  170. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  171. DebugAssert(FDDExtMutex != NULL);
  172. FDDTargetControl = NULL;
  173. FDelayedDeletionTimer = NULL;
  174. FDelayedDeletionList = new TStringList();
  175. FDDFileList = NULL;
  176. FPendingTempSpaceWarn = false;
  177. FCapturedLog = NULL;
  178. FDragDropOperation = false;
  179. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  180. FAllowTransferPresetAutoSelect = true;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FMoveToQueue = false;
  194. FStandaloneEditing = false;
  195. FOnFeedSynchronizeError = NULL;
  196. FNeedSession = false;
  197. FDoNotIdleCurrentTerminal = 0;
  198. FIncrementalSearching = 0;
  199. FIncrementalSearchHaveNext = false;
  200. FEditorManager = new TEditorManager();
  201. FEditorManager->OnFileChange = ExecutedFileChanged;
  202. FEditorManager->OnFileReload = ExecutedFileReload;
  203. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  204. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  205. FLocalEditors = new TList();
  206. FQueueStatus = NULL;
  207. FQueueStatusSection = new TCriticalSection();
  208. FQueueStatusInvalidated = false;
  209. FQueueItemInvalidated = false;
  210. FQueueActedItem = NULL;
  211. FQueueController = new TQueueController(QueueView3);
  212. FUserActionTimer = new TTimer(this);
  213. FUserActionTimer->Enabled = false;
  214. FUserActionTimer->Interval = 10;
  215. FUserActionTimer->OnTimer = UserActionTimer;
  216. FNotes = new TStringList();
  217. FNoteTimer = new TTimer(this);
  218. FNoteTimer->Enabled = false;
  219. FNoteTimer->OnTimer = NoteTimer;
  220. FOnNoteClick = NULL;
  221. FOle32Library = LoadLibrary(L"Ole32.dll");
  222. FDragMoveCursor = FOle32Library != NULL ?
  223. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  224. UseSystemSettings(this);
  225. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  226. static_cast<TObject*>(GetComponent(fcTransferList)));
  227. DebugAssert(TransferList != NULL);
  228. FTransferListHoverIndex = -1;
  229. TransferList->OnChange = TransferListChange;
  230. TransferList->OnDrawItem = TransferListDrawItem;
  231. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueLabel);
  239. UseDesktopFont(RemoteStatusBar);
  240. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  241. QueueSplitter->ShowHint = true;
  242. RemotePanelSplitter->ShowHint = true;
  243. UpdateImages();
  244. FCustomCommandMenu = CreateTBXPopupMenu(this);
  245. FCustomCommandLocalFileList = NULL;
  246. FCustomCommandRemoteFileList = NULL;
  247. FSessionColors = new TPngImageList(this);
  248. FSessionColors->ColorDepth = cd32Bit;
  249. AddFixedSessionImages();
  250. SessionsPageControl->Images = FSessionColors;
  251. QueueLabel->FocusControl = QueueView3;
  252. UpdateQueueLabel();
  253. FRemoteDirViewWasFocused = true;
  254. CreateHiddenWindow();
  255. StartUpdates();
  256. }
  257. //---------------------------------------------------------------------------
  258. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  259. {
  260. // this has to be one of the very first things to do
  261. StopUpdateThread();
  262. delete FCustomCommandLocalFileList;
  263. delete FCustomCommandRemoteFileList;
  264. delete FCustomCommandMenu;
  265. delete FTrayIcon;
  266. FTrayIcon = NULL;
  267. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  268. delete FEditorManager;
  269. ForceCloseLocalEditors();
  270. delete FLocalEditors;
  271. HideFileFindDialog();
  272. if (FDelayedDeletionTimer)
  273. {
  274. DoDelayedDeletion(NULL);
  275. SAFE_DESTROY(FDelayedDeletionTimer);
  276. }
  277. SAFE_DESTROY(FDelayedDeletionList);
  278. // sometimes we do not get DDEnd so the list is not released
  279. SAFE_DESTROY(FDDFileList);
  280. DebugAssert(FSynchronizeController == NULL);
  281. CloseHandle(FDDExtMutex);
  282. FDDExtMutex = NULL;
  283. FreeLibrary(FOle32Library);
  284. FOle32Library = NULL;
  285. FDragMoveCursor = NULL;
  286. DebugAssert(!FErrorList);
  287. if (FEverShown)
  288. {
  289. // when window is never shown (like when running command-line operation),
  290. // particularly window site is not restored correctly (BoundsRect value set
  291. // in RestoreForm gets lost during handle allocation), so we do not want
  292. // it to be stored
  293. StoreParams();
  294. }
  295. Terminal = NULL;
  296. Queue = NULL;
  297. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  298. NonVisualDataModule->ScpExplorer = NULL;
  299. delete FQueueController;
  300. FQueueController = NULL;
  301. delete FQueueStatusSection;
  302. FQueueStatusSection = NULL;
  303. delete FQueueStatus;
  304. FQueueStatus = NULL;
  305. delete FUserActionTimer;
  306. FUserActionTimer = NULL;
  307. delete FNoteTimer;
  308. delete FNotes;
  309. delete FNoteData;
  310. SAFE_DESTROY(FDocks);
  311. SessionsPageControl->Images = NULL;
  312. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  313. SAFE_DESTROY(FSessionColors);
  314. SAFE_DESTROY(FSessionsDragDropFilesEx);
  315. SAFE_DESTROY(FHistoryMenu[0][0]);
  316. SAFE_DESTROY(FHistoryMenu[0][1]);
  317. SAFE_DESTROY(FHistoryMenu[1][0]);
  318. SAFE_DESTROY(FHistoryMenu[1][1]);
  319. if (FHiddenWindow != NULL)
  320. {
  321. DestroyWindow(FHiddenWindow);
  322. FHiddenWindow = NULL;
  323. }
  324. }
  325. //---------------------------------------------------------------------------
  326. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  327. {
  328. std::unique_ptr<TSessionData> Data;
  329. if (!Session.IsEmpty())
  330. {
  331. bool DefaultsOnly;
  332. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  333. }
  334. TTerminalManager * Manager = TTerminalManager::Instance();
  335. for (int Index = 0; Index < Manager->Count; Index++)
  336. {
  337. TTerminal * Terminal = Manager->Terminals[Index];
  338. if (Session.IsEmpty() ||
  339. Terminal->SessionData->IsSameSite(Data.get()))
  340. {
  341. if (Path.IsEmpty())
  342. {
  343. Terminal->ClearCaches();
  344. }
  345. else
  346. {
  347. Terminal->DirectoryModified(Path, true);
  348. }
  349. }
  350. }
  351. // We should flag a pending refresh for the background terminals or busy foreground terminals
  352. if ((Terminal != NULL) && Terminal->Active &&
  353. CanCommandLineFromAnotherInstance() &&
  354. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  355. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  356. {
  357. Terminal->ReloadDirectory();
  358. }
  359. }
  360. //---------------------------------------------------------------------------
  361. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  362. {
  363. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  364. size_t MessageSize = sizeof(TCopyDataMessage);
  365. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  366. if (Result)
  367. {
  368. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  369. Result = (Message.Version == TCopyDataMessage::Version1);
  370. if (Result)
  371. {
  372. switch (Message.Command)
  373. {
  374. case TCopyDataMessage::CommandCanCommandLine:
  375. Result = CanCommandLineFromAnotherInstance();
  376. break;
  377. case TCopyDataMessage::CommandCommandLine:
  378. {
  379. UnicodeString CommandLine(Message.CommandLine);
  380. Result = CommandLineFromAnotherInstance(CommandLine);
  381. }
  382. break;
  383. case TCopyDataMessage::RefreshPanel:
  384. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  385. break;
  386. case TCopyDataMessage::MainWindowCheck:
  387. Result = true;
  388. break;
  389. default:
  390. Result = false;
  391. break;
  392. }
  393. }
  394. }
  395. Message.Result = Result ? 1 : 0;
  396. }
  397. //---------------------------------------------------------------------------
  398. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  399. {
  400. WNDCLASS WindowClass = {0};
  401. WindowClass.lpfnWndProc = DefWindowProc;
  402. WindowClass.hInstance = HInstance;
  403. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  404. FHiddenWindow = NULL;
  405. if (RegisterClass(&WindowClass))
  406. {
  407. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  408. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  409. }
  410. }
  411. //---------------------------------------------------------------------------
  412. bool __fastcall TCustomScpExplorerForm::CanConsole()
  413. {
  414. return
  415. (Terminal != NULL) &&
  416. Terminal->Active &&
  417. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  418. }
  419. //---------------------------------------------------------------------------
  420. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  421. {
  422. bool Result = Visible && !NonVisualDataModule->Busy;
  423. return Result;
  424. }
  425. //---------------------------------------------------------------------------
  426. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  427. const UnicodeString & CommandLine)
  428. {
  429. TProgramParams Params(CommandLine);
  430. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  431. if (Result)
  432. {
  433. NonVisualDataModule->StartBusy();
  434. try
  435. {
  436. // this action is initiated from another process,
  437. // so it's likely that our window is not visible,
  438. // and user won't see what is going on
  439. Application->BringToFront();
  440. // reload sessions as we may be asked to open a session
  441. // just stored by another instance
  442. StoredSessions->Reload();
  443. UnicodeString SessionName = Params.Param[1];
  444. std::unique_ptr<TObjectList> DataList(new TObjectList());
  445. UnicodeString DownloadFile; // unused
  446. try
  447. {
  448. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  449. }
  450. catch (EAbort &)
  451. {
  452. // Happens when opening session in PuTTY and in other situations,
  453. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  454. // to process the command-line on its own (or to pass it to yet another instance)
  455. }
  456. if (DataList->Count > 0)
  457. {
  458. TTerminalManager * Manager = TTerminalManager::Instance();
  459. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  460. }
  461. }
  462. __finally
  463. {
  464. NonVisualDataModule->EndBusy();
  465. }
  466. }
  467. return Result;
  468. }
  469. //---------------------------------------------------------------------------
  470. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  471. {
  472. if (FTerminal != value)
  473. {
  474. TerminalChanging();
  475. DoSetTerminal(value, false);
  476. }
  477. }
  478. //---------------------------------------------------------------------------
  479. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TTerminal * value, bool Replace)
  480. {
  481. FTerminal = value;
  482. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  483. FAllowTransferPresetAutoSelect = false;
  484. try
  485. {
  486. TerminalChanged(Replace);
  487. }
  488. __finally
  489. {
  490. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  491. }
  492. if (Terminal != NULL)
  493. {
  494. TransferPresetAutoSelect();
  495. }
  496. }
  497. //---------------------------------------------------------------------------
  498. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TTerminal * value)
  499. {
  500. DoSetTerminal(value, true);
  501. }
  502. //---------------------------------------------------------------------------
  503. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  504. {
  505. if (FTerminal != NULL)
  506. {
  507. UpdateTerminal(Terminal);
  508. }
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  512. {
  513. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  514. if (ManagedTerminal != NULL)
  515. {
  516. UpdateSessionColor((TColor)ManagedTerminal->StateData->Color);
  517. }
  518. DoTerminalListChanged();
  519. DebugAssert(!IsLocalBrowserMode()); // TODO
  520. if (Replaced)
  521. {
  522. RemoteDirView->ReplaceTerminal(Terminal);
  523. }
  524. else
  525. {
  526. RemoteDirView->Terminal = Terminal;
  527. }
  528. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  529. if (Terminal != NULL)
  530. {
  531. if (Terminal->Active)
  532. {
  533. Terminal->RefreshDirectory();
  534. }
  535. DebugAssert(ManagedTerminal == Terminal);
  536. if (WinConfiguration->PreservePanelState)
  537. {
  538. if (ManagedTerminal->RemoteExplorerState != NULL)
  539. {
  540. DirView(osRemote)->RestoreState(ManagedTerminal->RemoteExplorerState);
  541. }
  542. else
  543. {
  544. DirView(osRemote)->ClearState();
  545. }
  546. }
  547. if (!Replaced)
  548. {
  549. InitStatusBar();
  550. }
  551. }
  552. UpdateTransferList();
  553. // Update panels Enable state before refreshing the labels
  554. UpdateControls();
  555. }
  556. //---------------------------------------------------------------------------
  557. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  558. {
  559. if (Queue != value)
  560. {
  561. if (FQueue != NULL)
  562. {
  563. FQueue->OnListUpdate = NULL;
  564. FQueue->OnQueueItemUpdate = NULL;
  565. }
  566. FQueue = value;
  567. if (FQueue != NULL)
  568. {
  569. DebugAssert(FQueue->OnListUpdate == NULL);
  570. FQueue->OnListUpdate = QueueListUpdate;
  571. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  572. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  573. }
  574. QueueChanged();
  575. }
  576. }
  577. //---------------------------------------------------------------------------
  578. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  579. TListItem * Item)
  580. {
  581. if (FQueueActedItem == Item)
  582. {
  583. FQueueActedItem = NULL;
  584. if ((QueueView3->PopupMenu != NULL) &&
  585. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  586. {
  587. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  588. ReleaseCapture();
  589. }
  590. }
  591. if (Item->Data == FPendingQueueActionItem)
  592. {
  593. FPendingQueueActionItem = NULL;
  594. }
  595. }
  596. //---------------------------------------------------------------------------
  597. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  598. {
  599. {
  600. TGuard Guard(FQueueStatusSection);
  601. FQueueStatusInvalidated = false;
  602. if (FQueue != NULL)
  603. {
  604. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  605. }
  606. }
  607. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  608. {
  609. FMaxQueueLength = FQueueStatus->Count;
  610. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  611. }
  612. FQueueController->UpdateQueueStatus(FQueueStatus);
  613. SetQueueProgress();
  614. UpdateQueueView();
  615. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  616. if (IsEmpty && (Terminal != NULL))
  617. {
  618. TOnceDoneOperation OnceDoneOperation =
  619. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  620. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  621. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  622. {
  623. FQueue->Enabled = false;
  624. }
  625. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  626. {
  627. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  628. }
  629. }
  630. }
  631. //---------------------------------------------------------------------------
  632. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  633. {
  634. UnicodeString Result;
  635. if (FQueueStatus != NULL)
  636. {
  637. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  638. {
  639. TFileOperationProgressType * ProgressData =
  640. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  641. if ((ProgressData != NULL) && ProgressData->InProgress)
  642. {
  643. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  644. }
  645. }
  646. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  647. {
  648. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  649. }
  650. }
  651. return Result;
  652. }
  653. //---------------------------------------------------------------------------
  654. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  655. {
  656. ComponentVisible[fcQueueView] =
  657. (WinConfiguration->QueueView.Show == qvShow) ||
  658. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  659. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  660. }
  661. //---------------------------------------------------------------------------
  662. void __fastcall TCustomScpExplorerForm::QueueChanged()
  663. {
  664. if (FQueueStatus != NULL)
  665. {
  666. delete FQueueStatus;
  667. FQueueStatus = NULL;
  668. }
  669. UpdateQueueStatus(true);
  670. }
  671. //---------------------------------------------------------------------------
  672. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  673. {
  674. if (FQueue == Queue)
  675. {
  676. FQueueStatusInvalidated = true;
  677. }
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  681. TQueueItem * Item)
  682. {
  683. if (FQueue == Queue)
  684. {
  685. TGuard Guard(FQueueStatusSection);
  686. // this may be running in parallel with QueueChanged
  687. if (FQueueStatus != NULL)
  688. {
  689. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  690. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  691. {
  692. FRefreshLocalDirectory = (QueueItem == NULL) ||
  693. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  694. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  695. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  696. }
  697. if (QueueItem != NULL)
  698. {
  699. QueueItem->UserData = (void*)true;
  700. FQueueItemInvalidated = true;
  701. }
  702. }
  703. }
  704. }
  705. //---------------------------------------------------------------------------
  706. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  707. {
  708. // during standalone editing, we have no way to see/control queue,
  709. // so we have to always popup prompts automatically
  710. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  711. }
  712. //---------------------------------------------------------------------------
  713. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  714. {
  715. if (FQueueStatus != NULL)
  716. {
  717. bool QueueAutoPopup = IsQueueAutoPopup();
  718. bool NeedRefresh = FQueueController->NeedRefresh();
  719. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  720. FQueueItemInvalidated = false;
  721. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  722. bool Updated = false;
  723. TQueueItemProxy * QueueItem;
  724. bool WasUserAction;
  725. for (int Index = 0; Index < Limit; Index++)
  726. {
  727. bool Update = false;
  728. QueueItem = FQueueStatus->Items[Index];
  729. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  730. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  731. (FPendingQueueActionItem == NULL))
  732. {
  733. FPendingQueueActionItem = QueueItem;
  734. FUserActionTimer->Enabled = true;
  735. }
  736. if ((bool)QueueItem->UserData)
  737. {
  738. QueueItem->UserData = (void*)false;
  739. QueueItem->Update();
  740. Updated = true;
  741. Update = true;
  742. }
  743. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  744. {
  745. Update = true;
  746. }
  747. if (Update)
  748. {
  749. FQueueController->RefreshQueueItem(QueueItem);
  750. }
  751. }
  752. if (Updated)
  753. {
  754. NonVisualDataModule->UpdateNonVisibleActions();
  755. SetQueueProgress();
  756. }
  757. }
  758. }
  759. //---------------------------------------------------------------------------
  760. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  761. {
  762. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  763. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  764. }
  765. //---------------------------------------------------------------------------
  766. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  767. {
  768. if (ProgressData->Operation != foCalculateSize)
  769. {
  770. int OverallProgress;
  771. // FProgressForm is null when this is called from SetQueueProgress
  772. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  773. {
  774. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  775. }
  776. else
  777. {
  778. OverallProgress = ProgressData->OverallProgress();
  779. }
  780. // implies TBPF_NORMAL
  781. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  782. }
  783. else
  784. {
  785. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  786. }
  787. }
  788. //---------------------------------------------------------------------------
  789. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  790. {
  791. TTerminalManager::Instance()->QueueStatusUpdated();
  792. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  793. {
  794. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  795. {
  796. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  797. {
  798. TFileOperationProgressType * ProgressData = NULL;
  799. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  800. {
  801. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  802. }
  803. if ((ProgressData != NULL) &&
  804. ProgressData->InProgress)
  805. {
  806. SetTaskbarListProgressValue(ProgressData);
  807. }
  808. else
  809. {
  810. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  811. }
  812. }
  813. else
  814. {
  815. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  816. }
  817. }
  818. else
  819. {
  820. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  821. }
  822. }
  823. UpdateQueueLabel();
  824. }
  825. //---------------------------------------------------------------------------
  826. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  827. {
  828. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  829. if (FQueueStatus != NULL)
  830. {
  831. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  832. if (ActiveAndPendingPrimaryCount > 0)
  833. {
  834. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  835. }
  836. }
  837. QueueLabel->Caption = Caption;
  838. }
  839. //---------------------------------------------------------------------------
  840. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  841. {
  842. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  843. static_cast<TComponent*>(GetComponent(fcTransferList)));
  844. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  845. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  846. TransferList->Strings->BeginUpdate();
  847. try
  848. {
  849. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  850. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  851. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  852. if (FTransferDropDownHint.IsEmpty())
  853. {
  854. FTransferDropDownHint = TransferDropDown->Hint;
  855. }
  856. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  857. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  858. TransferDropDown->Text = StripHotkey(Name);
  859. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  860. (FTransferDropDownHint, StripHotkey(Name),
  861. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  862. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  863. // update the label, otherwise when it is updated only on the first draw
  864. // of the list, it is drawn "bold" for some reason
  865. FTransferListHoverIndex = TransferList->ItemIndex;
  866. UpdateTransferLabel();
  867. }
  868. __finally
  869. {
  870. TransferList->Strings->EndUpdate();
  871. }
  872. }
  873. //---------------------------------------------------------------------------
  874. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  875. {
  876. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  877. DebugAssert(Toolbar != NULL);
  878. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  879. }
  880. //---------------------------------------------------------------------------
  881. void __fastcall TCustomScpExplorerForm::UpdateActions()
  882. {
  883. TForm::UpdateActions();
  884. if (ComponentVisible[fcCustomCommandsBand])
  885. {
  886. UpdateCustomCommandsToolbar();
  887. }
  888. }
  889. //---------------------------------------------------------------------------
  890. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  891. {
  892. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  893. }
  894. //---------------------------------------------------------------------------
  895. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  896. {
  897. Color = GetBtnFaceColor();
  898. DebugAssert(Configuration && RemoteDirView);
  899. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  900. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  901. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  902. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  903. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  904. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  905. {
  906. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  907. // selection is not redrawn automatically when RowSelect changes
  908. RemoteDirView->Invalidate();
  909. }
  910. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  911. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  912. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  913. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  914. UpdateSessionsPageControlHeight();
  915. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  916. UpdateToolbarDisplayMode();
  917. UpdateTransferList();
  918. if (Terminal != NULL)
  919. {
  920. TransferPresetAutoSelect();
  921. }
  922. UpdateQueueView();
  923. UpdateControls();
  924. // this can be called even before constuctor finishes.
  925. if (FEditorManager != NULL)
  926. {
  927. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  928. }
  929. // this can be called even before constuctor finishes.
  930. if (FLocalEditors != NULL)
  931. {
  932. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  933. {
  934. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  935. }
  936. }
  937. if (ComponentVisible[fcCustomCommandsBand])
  938. {
  939. UpdateCustomCommandsToolbar();
  940. }
  941. // show only when keeping queue items forever,
  942. // otherwise, its enough to have in in the context menu
  943. QueueDeleteAllDoneQueueToolbarItem->Visible =
  944. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  945. if (FFileColorsCurrent != WinConfiguration->FileColors)
  946. {
  947. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  948. FFileColorsCurrent = WinConfiguration->FileColors;
  949. FileColorsChanged();
  950. }
  951. }
  952. //---------------------------------------------------------------------------
  953. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  954. {
  955. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  956. DirView->Invalidate();
  957. }
  958. //---------------------------------------------------------------------------
  959. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  960. {
  961. DoFileColorsChanged(RemoteDirView);
  962. }
  963. //---------------------------------------------------------------------------
  964. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  965. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  966. void * /*Arg*/)
  967. {
  968. if (Token != NULL)
  969. {
  970. TForm * Editor = dynamic_cast<TForm*>(Token);
  971. DebugAssert(Editor != NULL);
  972. ReconfigureEditorForm(Editor);
  973. }
  974. }
  975. //---------------------------------------------------------------------------
  976. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  977. {
  978. WinConfiguration->DDTransferConfirmation = asOn;
  979. }
  980. //---------------------------------------------------------------------------
  981. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  982. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  983. {
  984. // noop
  985. }
  986. //---------------------------------------------------------------------------
  987. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  988. TTransferDirection Direction, TTransferType Type, bool Temp,
  989. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  990. bool Confirm, bool DragDrop, int Options)
  991. {
  992. bool Result = true;
  993. DebugAssert(Terminal && Terminal->Active);
  994. // these parameters are known in advance
  995. int Params =
  996. FLAGMASK(Type == ttMove, cpDelete);
  997. bool ToTemp = (Temp && (Direction == tdToLocal));
  998. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  999. {
  1000. bool DisableNewerOnly =
  1001. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1002. ToTemp;
  1003. Options |=
  1004. FLAGMASK(ToTemp, coTemp) |
  1005. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1006. coDoNotShowAgain;
  1007. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1008. int CopyParamAttrs = (Direction == tdToRemote ?
  1009. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1010. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1011. int OutputOptions =
  1012. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1013. cooDoNotShowAgain);
  1014. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1015. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1016. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1017. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1018. if (Result)
  1019. {
  1020. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1021. {
  1022. if (DragDrop)
  1023. {
  1024. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1025. {
  1026. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1027. NULL, 0, EnableDDTransferConfirmation, NULL);
  1028. }
  1029. WinConfiguration->DDTransferConfirmation = asOff;
  1030. }
  1031. else
  1032. {
  1033. WinConfiguration->ConfirmTransferring = false;
  1034. }
  1035. }
  1036. else
  1037. {
  1038. // User explicitly unchecked "do not show again",
  1039. // so show him the dialog the next time
  1040. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1041. {
  1042. WinConfiguration->DDTransferConfirmation = asOn;
  1043. }
  1044. }
  1045. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1046. }
  1047. }
  1048. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1049. {
  1050. Configuration->Usage->Inc(L"TransfersOnBackground");
  1051. // these parameter are known only after transfer dialog
  1052. Params |=
  1053. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1054. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1055. Result = false;
  1056. ClearTransferSourceSelection(Direction);
  1057. }
  1058. return Result;
  1059. }
  1060. //---------------------------------------------------------------------------
  1061. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1062. {
  1063. if (FOnFileOperationFinished != NULL)
  1064. {
  1065. FOnFileOperationFinished(UnicodeString(), true);
  1066. }
  1067. else
  1068. {
  1069. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1070. if (HasDirView[Side])
  1071. {
  1072. DirView(Side)->SelectAll(smNone);
  1073. }
  1074. }
  1075. }
  1076. //---------------------------------------------------------------------------
  1077. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1078. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1079. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1080. int Params)
  1081. {
  1082. DebugAssert(Queue != NULL);
  1083. bool SingleFile = false;
  1084. if (FileList->Count == 1)
  1085. {
  1086. if (Direction == tdToRemote)
  1087. {
  1088. UnicodeString FileName = FileList->Strings[0];
  1089. SingleFile = FileExists(ApiPath(FileName));
  1090. }
  1091. else
  1092. {
  1093. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1094. SingleFile = !File->IsDirectory;
  1095. }
  1096. }
  1097. TQueueItem * QueueItem;
  1098. if (Direction == tdToRemote)
  1099. {
  1100. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1101. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1102. }
  1103. else
  1104. {
  1105. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1106. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1107. }
  1108. AddQueueItem(Queue, QueueItem, Terminal);
  1109. }
  1110. //---------------------------------------------------------------------------
  1111. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1112. {
  1113. if (Queue->IsEmpty)
  1114. {
  1115. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1116. ManagedTerminal->QueueOperationStart = Now();
  1117. }
  1118. Queue->AddItem(QueueItem);
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1122. {
  1123. }
  1124. //---------------------------------------------------------------------------
  1125. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1126. {
  1127. DebugAssert(FDocks != NULL);
  1128. for (int Index = 0; Index < ComponentCount; Index++)
  1129. {
  1130. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1131. if ((Dock != NULL) && (Dock->Tag == 0))
  1132. {
  1133. FDocks->Add(Dock);
  1134. }
  1135. }
  1136. CollectItemsWithTextDisplayMode(this);
  1137. ConfigurationChanged();
  1138. QueuePanel->Height =
  1139. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1140. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1141. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1142. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1143. }
  1144. //---------------------------------------------------------------------------
  1145. void __fastcall TCustomScpExplorerForm::StoreParams()
  1146. {
  1147. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1148. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1149. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1150. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1151. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1152. }
  1153. //---------------------------------------------------------------------------
  1154. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1155. {
  1156. if (!FFormRestored)
  1157. {
  1158. FFormRestored = true;
  1159. RestoreFormParams();
  1160. }
  1161. TForm::CreateParams(Params);
  1162. }
  1163. //---------------------------------------------------------------------------
  1164. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1165. {
  1166. DebugAssert(FDocks != NULL);
  1167. for (int Index = 0; Index < FDocks->Count; Index++)
  1168. {
  1169. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1170. }
  1171. }
  1172. //---------------------------------------------------------------------------
  1173. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1174. {
  1175. SetDockAllowDrag(true);
  1176. while (!ButtonsStr.IsEmpty())
  1177. {
  1178. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1179. UnicodeString Name = CutToChar(S, L'=', true);
  1180. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1181. {
  1182. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1183. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1184. {
  1185. while (!S.IsEmpty())
  1186. {
  1187. UnicodeString S2 = CutToChar(S, L',', true);
  1188. UnicodeString Name = CutToChar(S2, L':', true);
  1189. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1190. {
  1191. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1192. if (SameText(GetToolbarItemName(Item), Name))
  1193. {
  1194. // forward compatibility
  1195. UnicodeString S3 = CutToChar(S2, L':', true);
  1196. bool Visible = StrToIntDef(S3, 0);
  1197. Item->Visible = Visible;
  1198. }
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1205. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1206. }
  1207. //---------------------------------------------------------------------------
  1208. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1209. {
  1210. UnicodeString Result;
  1211. SetDockAllowDrag(true);
  1212. Result = ::GetToolbarsLayoutStr(this);
  1213. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1214. return Result;
  1215. }
  1216. //---------------------------------------------------------------------------
  1217. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1218. {
  1219. UnicodeString Result;
  1220. if (Item->Action != NULL)
  1221. {
  1222. Result = Item->Action->Name;
  1223. Result = RemoveSuffix(Result, L"Action");
  1224. }
  1225. else
  1226. {
  1227. Result = Item->Name;
  1228. Result = RemoveSuffix(Result, L"SubmenuItem");
  1229. Result = RemoveSuffix(Result, L"Item");
  1230. }
  1231. return Result;
  1232. }
  1233. //---------------------------------------------------------------------------
  1234. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1235. {
  1236. UnicodeString Result;
  1237. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1238. {
  1239. UnicodeString ToolbarStr;
  1240. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1241. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1242. {
  1243. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1244. {
  1245. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1246. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1247. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1248. // and save the changes here only.
  1249. if (!Item->Visible)
  1250. {
  1251. UnicodeString Name = GetToolbarItemName(Item);
  1252. DebugAssert(Name.Pos(L"TBX") == 0);
  1253. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1254. }
  1255. }
  1256. }
  1257. if (!ToolbarStr.IsEmpty())
  1258. {
  1259. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1260. }
  1261. }
  1262. return Result;
  1263. }
  1264. //---------------------------------------------------------------------------
  1265. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1266. {
  1267. DebugAssert(FProgressForm == NULL);
  1268. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1269. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1270. FProgressForm->DeleteLocalToRecycleBin =
  1271. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1272. FProgressForm->DeleteRemoteToRecycleBin =
  1273. (Terminal != NULL) &&
  1274. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1275. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1276. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1277. LockWindow();
  1278. if (FTaskbarList != NULL)
  1279. {
  1280. // Actually, do not know what hides the progress once the operation finishes
  1281. SetTaskbarListProgressState(TBPF_NORMAL);
  1282. }
  1283. }
  1284. //---------------------------------------------------------------------------
  1285. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1286. {
  1287. UnlockWindow();
  1288. SAFE_DESTROY(FProgressForm);
  1289. SetQueueProgress();
  1290. }
  1291. //---------------------------------------------------------------------------
  1292. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1293. TFileOperationProgressType & ProgressData)
  1294. {
  1295. // Download to temporary local directory
  1296. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1297. {
  1298. bool Continue = true;
  1299. FPendingTempSpaceWarn = false;
  1300. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1301. if (!Continue)
  1302. {
  1303. Abort();
  1304. }
  1305. }
  1306. // operation is being executed and we still didn't show up progress form
  1307. if (ProgressData.InProgress && !FProgressForm)
  1308. {
  1309. CreateProgressForm(NULL);
  1310. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1311. // we suppose "/upload" or URL download mode
  1312. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1313. (ProgressData.Operation != foCalculateChecksum) &&
  1314. // Hack to prevent renames from DoSynchronizeMove (synchronize checklist) closing the connection
  1315. (ProgressData.Operation != foRemoteMove) &&
  1316. (FSynchronizeProgressForm == NULL) &&
  1317. !FStandaloneEditing)
  1318. {
  1319. FProgressForm->OnceDoneOperation = odoDisconnect;
  1320. }
  1321. }
  1322. // operation is finished (or terminated), so we hide progress form
  1323. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1324. {
  1325. DestroyProgressForm();
  1326. if (ProgressData.Operation == foCalculateSize)
  1327. {
  1328. // When calculating size before transfer, the abort caused by
  1329. // cancel flag set due to "MoveToQueue" below,
  1330. // is not propagated back to us in ExecuteFileOperation,
  1331. // as it is silently swallowed.
  1332. // So we explicitly (re)throw it here.
  1333. if (FMoveToQueue)
  1334. {
  1335. Abort();
  1336. }
  1337. }
  1338. else
  1339. {
  1340. if ((ProgressData.Cancel == csContinue) &&
  1341. !FAutoOperation)
  1342. {
  1343. OperationComplete(ProgressData.StartTime);
  1344. }
  1345. }
  1346. }
  1347. if (FProgressForm &&
  1348. // Can happen only during synchronization, when ending one of the operations
  1349. ProgressData.InProgress)
  1350. {
  1351. FProgressForm->SetProgressData(ProgressData);
  1352. if (FTaskbarList != NULL)
  1353. {
  1354. DebugAssert(ProgressData.InProgress);
  1355. SetTaskbarListProgressValue(&ProgressData);
  1356. }
  1357. if (FProgressForm->Cancel > csContinue)
  1358. {
  1359. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1360. if (FProgressForm->Cancel == csCancelFile)
  1361. {
  1362. FProgressForm->ClearCancel();
  1363. }
  1364. // cancel cancels even the move
  1365. FMoveToQueue = false;
  1366. }
  1367. else if (FProgressForm->MoveToQueue)
  1368. {
  1369. FMoveToQueue = true;
  1370. ProgressData.SetCancelAtLeast(csCancel);
  1371. }
  1372. if ((FTransferResumeList != NULL) &&
  1373. ProgressData.InProgress &&
  1374. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1375. !ProgressData.FullFileName.IsEmpty())
  1376. {
  1377. if ((FTransferResumeList->Count == 0) ||
  1378. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1379. {
  1380. // note that we do not recognize directories from files here
  1381. FTransferResumeList->Add(ProgressData.FullFileName);
  1382. }
  1383. }
  1384. }
  1385. }
  1386. //---------------------------------------------------------------------------
  1387. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1388. const TDateTime & StartTime)
  1389. {
  1390. if (GUIConfiguration->BeepOnFinish &&
  1391. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1392. {
  1393. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1394. DWORD Sound;
  1395. if (!ExtractFileExt(BeepSound).IsEmpty())
  1396. {
  1397. Sound = SND_FILENAME;
  1398. }
  1399. else
  1400. {
  1401. Sound = SND_ALIAS;
  1402. }
  1403. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1404. }
  1405. }
  1406. //---------------------------------------------------------------------------
  1407. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1408. TFileOperationProgressType & ProgressData)
  1409. {
  1410. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1411. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1412. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1413. FileOperationProgress(ProgressData);
  1414. }
  1415. //---------------------------------------------------------------------------
  1416. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1417. {
  1418. UnicodeString Result;
  1419. if (FProgressForm != NULL)
  1420. {
  1421. Result = FProgressForm->ProgressStr();
  1422. }
  1423. return Result;
  1424. }
  1425. //---------------------------------------------------------------------------
  1426. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1427. bool DragDrop)
  1428. {
  1429. return (!DragDrop && (DropSourceControl == NULL)) ||
  1430. (DropSourceControl == DirView(osOther));
  1431. }
  1432. //---------------------------------------------------------------------------
  1433. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1434. TFileOperation Operation, TOperationSide Side,
  1435. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1436. TOnceDoneOperation & OnceDoneOperation)
  1437. {
  1438. if (!FAutoOperation)
  1439. {
  1440. // no selection on "/upload", form servers only as event handler
  1441. // (it is not displayed)
  1442. if (PanelOperation(Side, FDragDropOperation) &&
  1443. Visible && (Operation != foCalculateSize) &&
  1444. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1445. {
  1446. if (FOnFileOperationFinished != NULL)
  1447. {
  1448. FOnFileOperationFinished(FileName, Success);
  1449. }
  1450. else
  1451. {
  1452. TCustomDirView * DView = DirView(Side);
  1453. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1454. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1455. // this can happen when local drive is unplugged in the middle of the operation
  1456. if (Item != NULL)
  1457. {
  1458. if (Success) Item->Selected = false;
  1459. DView->MakeProgressVisible(Item);
  1460. }
  1461. }
  1462. }
  1463. if ((Operation == foCopy) || (Operation == foMove))
  1464. {
  1465. DebugAssert(!IsLocalBrowserMode()); // TODO
  1466. if (Side == osLocal)
  1467. {
  1468. Configuration->Usage->Inc(L"UploadedFiles");
  1469. }
  1470. else
  1471. {
  1472. Configuration->Usage->Inc(L"DownloadedFiles");
  1473. }
  1474. }
  1475. }
  1476. if (Success && (FSynchronizeController != NULL))
  1477. {
  1478. DebugAssert(!IsSideLocalBrowser(Side));
  1479. if (Operation == foCopy)
  1480. {
  1481. DebugAssert(Side == osLocal);
  1482. FSynchronizeController->LogOperation(soUpload, FileName);
  1483. }
  1484. else if (Operation == foDelete)
  1485. {
  1486. DebugAssert(Side == osRemote);
  1487. FSynchronizeController->LogOperation(soDelete, FileName);
  1488. }
  1489. }
  1490. if (FProgressForm)
  1491. {
  1492. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1493. }
  1494. }
  1495. //---------------------------------------------------------------------------
  1496. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1497. TFileOperation Operation, TOperationSide Side,
  1498. bool Temp, const UnicodeString & FileName, Boolean Success,
  1499. TOnceDoneOperation & OnceDoneOperation)
  1500. {
  1501. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1502. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1503. OnceDoneOperation);
  1504. }
  1505. //---------------------------------------------------------------------------
  1506. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1507. {
  1508. return false;
  1509. }
  1510. //---------------------------------------------------------------------------
  1511. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1512. {
  1513. return false;
  1514. }
  1515. //---------------------------------------------------------------------------
  1516. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1517. {
  1518. return NULL;
  1519. }
  1520. //---------------------------------------------------------------------------
  1521. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1522. {
  1523. DebugAssert(GetSide(Side) == osRemote);
  1524. DebugUsedParam(Side);
  1525. return RemoteDirView;
  1526. }
  1527. //---------------------------------------------------------------------------
  1528. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1529. {
  1530. DebugAssert(GetSide(Side) == osRemote);
  1531. DebugUsedParam(Side);
  1532. return RemoteDriveView;
  1533. }
  1534. //---------------------------------------------------------------------------
  1535. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1536. {
  1537. DebugAssert(GetSide(Side) == osRemote);
  1538. DebugUsedParam(Side);
  1539. return (Terminal != NULL);
  1540. }
  1541. //---------------------------------------------------------------------------
  1542. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1543. TOperationSide Side, int FilesOnly)
  1544. {
  1545. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1546. }
  1547. //---------------------------------------------------------------------------
  1548. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1549. TOperationSide Side, int FilesOnly)
  1550. {
  1551. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1552. }
  1553. //---------------------------------------------------------------------------
  1554. struct THistoryItemData
  1555. {
  1556. short int Side;
  1557. short int Index;
  1558. };
  1559. //---------------------------------------------------------------------------
  1560. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1561. {
  1562. DirView(Side)->HistoryGo(Index);
  1563. }
  1564. //---------------------------------------------------------------------------
  1565. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1566. {
  1567. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1568. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1569. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1570. }
  1571. //---------------------------------------------------------------------------
  1572. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1573. bool Back)
  1574. {
  1575. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1576. {
  1577. TCustomDirView * DView = DirView(Side);
  1578. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1579. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1580. if (ICount > 10)
  1581. {
  1582. ICount = 10;
  1583. }
  1584. Menu->Items->Clear();
  1585. THistoryItemData Data;
  1586. Data.Side = Side;
  1587. for (short int i = 1; i <= ICount; i++)
  1588. {
  1589. TTBCustomItem * Item = new TTBXItem(Menu);
  1590. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1591. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1592. Item->Hint = DView->HistoryPath[Data.Index];
  1593. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1594. Item->Tag = *reinterpret_cast<int*>(&Data);
  1595. Item->OnClick = HistoryItemClick;
  1596. Menu->Items->Add(Item);
  1597. }
  1598. }
  1599. }
  1600. //---------------------------------------------------------------------------
  1601. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1602. TOperationSide Side, bool Back)
  1603. {
  1604. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1605. {
  1606. // workaround
  1607. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1608. // operator new allocates memory in Pascal code, but calls inline
  1609. // contructor in C++, leading in problems, the function does
  1610. // both in Pascal code
  1611. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1612. UpdateHistoryMenu(Side, Back);
  1613. }
  1614. return FHistoryMenu[Side == osLocal][Back];
  1615. }
  1616. //---------------------------------------------------------------------------
  1617. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1618. TCustomDirView *Sender)
  1619. {
  1620. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1621. UpdateHistoryMenu(Side, true);
  1622. UpdateHistoryMenu(Side, false);
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1626. {
  1627. // remote custom commands can be executed only if the server supports shell commands
  1628. // or have secondary shell
  1629. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1630. }
  1631. //---------------------------------------------------------------------------
  1632. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1633. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1634. {
  1635. int Result;
  1636. TFileCustomCommand RemoteCustomCommand;
  1637. TLocalCustomCommand LocalCustomCommand;
  1638. TFileCustomCommand * NonInteractiveCustomCommand =
  1639. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1640. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1641. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1642. if (FLAGCLEAR(Command.Params, ccLocal))
  1643. {
  1644. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1645. // custom command that does not operate with files can be executed anytime ...
  1646. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1647. {
  1648. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1649. {
  1650. // ... but do not show such command in remote file menu (TODO)
  1651. Result = AllowedState;
  1652. }
  1653. else
  1654. {
  1655. Result = -1;
  1656. }
  1657. }
  1658. else
  1659. {
  1660. if ((ListType == ccltAll) || (ListType == ccltFile))
  1661. {
  1662. Result = (!IsSideLocalBrowser(FCurrentSide) && DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1663. }
  1664. else
  1665. {
  1666. Result = -1;
  1667. }
  1668. }
  1669. }
  1670. else
  1671. {
  1672. // custom command that does not operate with files can be executed anytime
  1673. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1674. {
  1675. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1676. }
  1677. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1678. {
  1679. if ((ListType == ccltAll) || (ListType == ccltFile))
  1680. {
  1681. // special case is "diff"-style command that can be executed over any side,
  1682. // if we have both sides
  1683. Result =
  1684. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1685. // directly (instead of EnableSelectedOperation) to pass
  1686. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1687. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1688. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1689. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1690. }
  1691. else if (ListType == ccltBoth)
  1692. {
  1693. DebugAssert(!IsLocalBrowserMode());
  1694. Result = 1;
  1695. }
  1696. else
  1697. {
  1698. Result = -1;
  1699. }
  1700. }
  1701. else
  1702. {
  1703. if ((ListType == ccltAll) || (ListType == ccltFile))
  1704. {
  1705. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1706. }
  1707. else
  1708. {
  1709. Result = -1;
  1710. }
  1711. }
  1712. }
  1713. return Result;
  1714. }
  1715. //---------------------------------------------------------------------------
  1716. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1717. TStrings * FileList, const TCustomCommandType & ACommand,
  1718. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1719. {
  1720. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1721. {
  1722. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1723. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1724. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1725. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1726. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1727. bool Capture =
  1728. FLAGSET(ACommand.Params, ccShowResults) ||
  1729. FLAGSET(ACommand.Params, ccCopyResults) ||
  1730. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1731. TCaptureOutputEvent OutputEvent = NULL;
  1732. DebugAssert(FCapturedLog == NULL);
  1733. if (Capture)
  1734. {
  1735. FCapturedLog = new TStringList();
  1736. OutputEvent = TerminalCaptureLog;
  1737. }
  1738. try
  1739. {
  1740. if (!RemoteCustomCommand.IsFileCommand(Command))
  1741. {
  1742. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1743. OutputEvent);
  1744. }
  1745. else
  1746. {
  1747. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1748. }
  1749. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1750. {
  1751. if (FLAGSET(ACommand.Params, ccCopyResults))
  1752. {
  1753. CopyToClipboard(FCapturedLog);
  1754. }
  1755. if (FLAGSET(ACommand.Params, ccShowResults))
  1756. {
  1757. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1758. }
  1759. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1760. {
  1761. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1762. }
  1763. }
  1764. }
  1765. __finally
  1766. {
  1767. SAFE_DESTROY(FCapturedLog);
  1768. }
  1769. }
  1770. }
  1771. //---------------------------------------------------------------------------
  1772. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1773. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1774. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1775. {
  1776. DebugAssert(!IsLocalBrowserMode());
  1777. TStrings * LocalFileList = NULL;
  1778. TStrings * RemoteFileList = NULL;
  1779. TStrings * RemoteFileListFull = NULL;
  1780. try
  1781. {
  1782. if (LocalFileCommand)
  1783. {
  1784. if (ALocalFileList == NULL)
  1785. {
  1786. DebugAssert(HasDirView[osLocal]);
  1787. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1788. // directly (instead of EnableSelectedOperation) to pass
  1789. // false to FocusedFileOnlyWhenFocused
  1790. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1791. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1792. }
  1793. else
  1794. {
  1795. LocalFileList = ALocalFileList;
  1796. }
  1797. if (FileListCommand)
  1798. {
  1799. if (LocalFileList->Count != 1)
  1800. {
  1801. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1802. }
  1803. }
  1804. else
  1805. {
  1806. if ((LocalFileList->Count != 1) &&
  1807. (FileList->Count != 1) &&
  1808. (LocalFileList->Count != FileList->Count))
  1809. {
  1810. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1811. }
  1812. }
  1813. }
  1814. UnicodeString RootTempDir;
  1815. UnicodeString TempDir;
  1816. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1817. if (!RemoteFiles)
  1818. {
  1819. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1820. }
  1821. try
  1822. {
  1823. TDateTimes RemoteFileTimes;
  1824. if (RemoteFiles)
  1825. {
  1826. RemoteFileList = FileList;
  1827. }
  1828. else
  1829. {
  1830. RemoteFileList = new TStringList();
  1831. TMakeLocalFileListParams MakeFileListParam;
  1832. MakeFileListParam.FileList = RemoteFileList;
  1833. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1834. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1835. MakeFileListParam.Recursive =
  1836. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1837. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1838. if (!MakeFileListParam.Recursive)
  1839. {
  1840. RemoteFileTimes.clear();
  1841. RemoteFileListFull = new TStringList();
  1842. MakeFileListParam.FileList = RemoteFileListFull;
  1843. MakeFileListParam.Recursive = true;
  1844. MakeFileListParam.IncludeDirs = false;
  1845. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1846. }
  1847. else
  1848. {
  1849. RemoteFileListFull = RemoteFileList;
  1850. }
  1851. }
  1852. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1853. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1854. if (!NonBlocking)
  1855. {
  1856. int Count;
  1857. if (FileListCommand)
  1858. {
  1859. Count = 1;
  1860. }
  1861. else if (LocalFileCommand)
  1862. {
  1863. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1864. }
  1865. else
  1866. {
  1867. Count = RemoteFileList->Count;
  1868. }
  1869. Progress.Start(foCustomCommand, osRemote, Count);
  1870. DebugAssert(FProgressForm != NULL);
  1871. FProgressForm->ReadOnly = true;
  1872. }
  1873. try
  1874. {
  1875. if (FileListCommand)
  1876. {
  1877. UnicodeString LocalFile;
  1878. // MakeFileList does not delimit filenames
  1879. UnicodeString FileList = MakeFileList(RemoteFileList);
  1880. if (LocalFileCommand)
  1881. {
  1882. DebugAssert(LocalFileList->Count == 1);
  1883. LocalFile = LocalFileList->Strings[0];
  1884. }
  1885. TLocalCustomCommand CustomCommand(Data,
  1886. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1887. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1888. if (NonBlocking)
  1889. {
  1890. DebugAssert(!POutput);
  1891. ExecuteShellChecked(ShellCommand);
  1892. }
  1893. else
  1894. {
  1895. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1896. }
  1897. }
  1898. else if (LocalFileCommand)
  1899. {
  1900. if (LocalFileList->Count == 1)
  1901. {
  1902. UnicodeString LocalFile = LocalFileList->Strings[0];
  1903. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1904. {
  1905. UnicodeString FileName = RemoteFileList->Strings[Index];
  1906. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1907. Progress.SetFile(FileNameForProgress);
  1908. TLocalCustomCommand CustomCommand(Data,
  1909. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1910. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1911. TOnceDoneOperation OnceDoneOperation;
  1912. // Do not unselect anything, as it won't work with two panels anyway
  1913. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1914. }
  1915. }
  1916. else if (RemoteFileList->Count == 1)
  1917. {
  1918. UnicodeString FileName = RemoteFileList->Strings[0];
  1919. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1920. {
  1921. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1922. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1923. Progress.SetFile(LocalFileNameOnly);
  1924. TLocalCustomCommand CustomCommand(
  1925. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1926. FileName, LocalFileName, L"");
  1927. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1928. TOnceDoneOperation OnceDoneOperation;
  1929. // Do not unselect anything, as it won't work with two panels anyway
  1930. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1931. }
  1932. }
  1933. else
  1934. {
  1935. if (LocalFileList->Count != RemoteFileList->Count)
  1936. {
  1937. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1938. }
  1939. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1940. {
  1941. UnicodeString FileName = RemoteFileList->Strings[Index];
  1942. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1943. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1944. Progress.SetFile(LocalFileNameOnly); // sic
  1945. TLocalCustomCommand CustomCommand(
  1946. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1947. FileName, LocalFileName, L"");
  1948. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1949. TOnceDoneOperation OnceDoneOperation;
  1950. // Do not unselect anything, as it won't work with two panels anyway
  1951. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1952. }
  1953. }
  1954. }
  1955. else
  1956. {
  1957. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1958. {
  1959. UnicodeString FileName = RemoteFileList->Strings[Index];
  1960. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1961. Progress.SetFile(FileNameForProgress);
  1962. TLocalCustomCommand CustomCommand(Data,
  1963. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1964. FileName, L"", L"");
  1965. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1966. TOnceDoneOperation OnceDoneOperation;
  1967. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1968. }
  1969. }
  1970. }
  1971. __finally
  1972. {
  1973. if (!NonBlocking)
  1974. {
  1975. Progress.Stop();
  1976. }
  1977. }
  1978. DebugAssert(!FAutoOperation);
  1979. if (!RemoteFiles)
  1980. {
  1981. TempDir = IncludeTrailingBackslash(TempDir);
  1982. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1983. {
  1984. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1985. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1986. // Skip directories, process only nested files.
  1987. // The check is redundant as FileAge fails for directories anyway.
  1988. !DirectoryExists(FileName))
  1989. {
  1990. UnicodeString RemoteDir =
  1991. UnixExtractFileDir(
  1992. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1993. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1994. TDateTime NewTime;
  1995. if (FileAge(FileName, NewTime) &&
  1996. (NewTime != RemoteFileTimes[Index]))
  1997. {
  1998. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1999. TemporaryFileCopyParam(CopyParam);
  2000. CopyParam.FileMask = L"";
  2001. FAutoOperation = true;
  2002. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2003. TemporaryFilesList->Add(FileName);
  2004. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2005. }
  2006. }
  2007. }
  2008. }
  2009. }
  2010. __finally
  2011. {
  2012. FAutoOperation = false;
  2013. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2014. {
  2015. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2016. }
  2017. }
  2018. }
  2019. __finally
  2020. {
  2021. if (RemoteFileList != FileList)
  2022. {
  2023. delete RemoteFileList;
  2024. }
  2025. if (LocalFileList != ALocalFileList)
  2026. {
  2027. delete LocalFileList;
  2028. }
  2029. if (RemoteFileListFull != RemoteFileList)
  2030. {
  2031. delete RemoteFileListFull;
  2032. }
  2033. }
  2034. }
  2035. //---------------------------------------------------------------------------
  2036. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2037. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2038. bool FileListCommand, UnicodeString * POutput)
  2039. {
  2040. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2041. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2042. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2043. {
  2044. UnicodeString FileName = SelectedFileList->Strings[Index];
  2045. if (DirectoryExists(FileName))
  2046. {
  2047. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2048. {
  2049. LocalFileList->Add(FileName);
  2050. }
  2051. if (FLAGSET(ACommand.Params, ccRecursive))
  2052. {
  2053. TMakeLocalFileListParams MakeFileListParam;
  2054. MakeFileListParam.FileList = LocalFileList.get();
  2055. MakeFileListParam.FileTimes = NULL;
  2056. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2057. MakeFileListParam.Recursive = true;
  2058. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2059. }
  2060. }
  2061. else
  2062. {
  2063. LocalFileList->Add(FileName);
  2064. }
  2065. }
  2066. UnicodeString RemotePath;
  2067. if (Terminal != NULL)
  2068. {
  2069. RemotePath = Terminal->CurrentDirectory;
  2070. }
  2071. if (FileListCommand)
  2072. {
  2073. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2074. TLocalCustomCommand CustomCommand(
  2075. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2076. L"", L"", FileList);
  2077. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2078. }
  2079. else
  2080. {
  2081. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2082. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2083. DebugAssert(FProgressForm != NULL);
  2084. FProgressForm->ReadOnly = true;
  2085. try
  2086. {
  2087. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2088. {
  2089. UnicodeString FileName = LocalFileList->Strings[Index];
  2090. Progress.SetFile(FileName);
  2091. TLocalCustomCommand CustomCommand(
  2092. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2093. FileName, L"", L"");
  2094. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2095. TOnceDoneOperation OnceDoneOperation;
  2096. Progress.Finish(FileName, true, OnceDoneOperation);
  2097. }
  2098. }
  2099. __finally
  2100. {
  2101. Progress.Stop();
  2102. }
  2103. }
  2104. }
  2105. //---------------------------------------------------------------------------
  2106. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2107. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2108. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2109. {
  2110. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2111. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2112. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2113. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2114. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2115. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2116. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2117. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2118. std::unique_ptr<UnicodeString> POutput;
  2119. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2120. FLAGSET(ACommand.Params, ccCopyResults))
  2121. {
  2122. POutput.reset(new UnicodeString());
  2123. }
  2124. if (!LocalCustomCommand.IsFileCommand(Command))
  2125. {
  2126. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2127. }
  2128. // remote files?
  2129. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2130. {
  2131. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2132. }
  2133. // local files
  2134. else
  2135. {
  2136. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2137. }
  2138. if (POutput.get() != NULL)
  2139. {
  2140. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2141. int P = POutput->Pos(sLineBreak);
  2142. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2143. {
  2144. POutput->SetLength(P - 1);
  2145. }
  2146. // Copy even empty output to clipboard
  2147. if (FLAGSET(ACommand.Params, ccCopyResults))
  2148. {
  2149. CopyToClipboard(*POutput);
  2150. }
  2151. // But do not show an empty message box.
  2152. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2153. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2154. !POutput->IsEmpty())
  2155. {
  2156. TClipboardHandler ClipboardHandler;
  2157. ClipboardHandler.Text = *POutput;
  2158. TMessageParams Params;
  2159. TQueryButtonAlias Aliases[1];
  2160. Aliases[0].Button = qaRetry;
  2161. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2162. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2163. Params.Aliases = Aliases;
  2164. Params.AliasesCount = LENOF(Aliases);
  2165. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2166. }
  2167. }
  2168. }
  2169. //---------------------------------------------------------------------------
  2170. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2171. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2172. {
  2173. TCustomCommandData Data;
  2174. UnicodeString Site;
  2175. UnicodeString RemotePath;
  2176. if (Terminal != NULL)
  2177. {
  2178. Data = TCustomCommandData(Terminal);
  2179. Site = Terminal->SessionData->SessionKey;
  2180. RemotePath = Terminal->CurrentDirectory;
  2181. }
  2182. UnicodeString HelpKeyword = ACommand.HomePage;
  2183. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2184. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2185. {
  2186. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2187. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2188. {
  2189. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2190. }
  2191. else
  2192. {
  2193. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2194. }
  2195. if (!DoCustomCommandOptionsDialog(
  2196. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2197. {
  2198. Abort();
  2199. }
  2200. }
  2201. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2202. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2203. {
  2204. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2205. }
  2206. else
  2207. {
  2208. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2209. }
  2210. }
  2211. //---------------------------------------------------------------------------
  2212. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2213. const TCustomCommandType & Command)
  2214. {
  2215. DebugAssert(FCustomCommandLocalFileList != NULL);
  2216. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2217. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2218. TStrings * LocalFileList = new TStringList();
  2219. TStrings * RemoteFileList = new TStringList();
  2220. try
  2221. {
  2222. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2223. {
  2224. LocalFileList->Clear();
  2225. LocalFileList->AddObject(
  2226. FCustomCommandLocalFileList->Strings[Index],
  2227. FCustomCommandLocalFileList->Objects[Index]);
  2228. RemoteFileList->Clear();
  2229. RemoteFileList->AddObject(
  2230. FCustomCommandRemoteFileList->Strings[Index],
  2231. FCustomCommandRemoteFileList->Objects[Index]);
  2232. CustomCommand(RemoteFileList, Command, LocalFileList);
  2233. }
  2234. }
  2235. __finally
  2236. {
  2237. delete LocalFileList;
  2238. delete RemoteFileList;
  2239. }
  2240. }
  2241. //---------------------------------------------------------------------------
  2242. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2243. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2244. {
  2245. delete FCustomCommandLocalFileList;
  2246. delete FCustomCommandRemoteFileList;
  2247. // takeover ownership,
  2248. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2249. FCustomCommandLocalFileList = LocalFileList;
  2250. FCustomCommandRemoteFileList = RemoteFileList;
  2251. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2252. if (Button != NULL)
  2253. {
  2254. FCustomCommandMenu->Items->Clear();
  2255. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2256. MenuPopup(FCustomCommandMenu, Button);
  2257. }
  2258. else
  2259. {
  2260. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2261. }
  2262. }
  2263. //---------------------------------------------------------------------------
  2264. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2265. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2266. {
  2267. DebugAssert(FCapturedLog != NULL);
  2268. if ((OutputType == cotOutput) || (OutputType == cotError))
  2269. {
  2270. FCapturedLog->Add(AddedLine);
  2271. }
  2272. }
  2273. //---------------------------------------------------------------------------
  2274. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2275. TOperationSide Side)
  2276. {
  2277. return
  2278. (Side == osOther) &&
  2279. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2280. }
  2281. //---------------------------------------------------------------------------
  2282. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2283. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2284. {
  2285. TTBItemOptions O;
  2286. O = Item->Options;
  2287. TCustomDirView * DView = DirView(Side);
  2288. if ((DView->ItemFocused != NULL) &&
  2289. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2290. // when resolving links is disabled, default action is to enter the directory,
  2291. // no matter what DoubleClickAction is configured to
  2292. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2293. // Can only Edit files, but can Open/Copy even directories
  2294. ((DoubleClickAction != dcaEdit) ||
  2295. !DView->ItemIsDirectory(DView->ItemFocused)))
  2296. {
  2297. Item->Options = O << tboDefault;
  2298. }
  2299. else
  2300. {
  2301. Item->Options = O >> tboDefault;
  2302. }
  2303. }
  2304. //---------------------------------------------------------------------------
  2305. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2306. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2307. {
  2308. TCustomDirView * DView = DirView(Side);
  2309. TListItem * Item = DView->ItemFocused;
  2310. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2311. EnableFocusedOperation[Side])
  2312. {
  2313. TPoint ClientPoint;
  2314. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2315. TPoint(0, 0) : MousePos;
  2316. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2317. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2318. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2319. }
  2320. }
  2321. //---------------------------------------------------------------------------
  2322. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2323. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2324. {
  2325. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2326. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2327. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2328. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2329. Handled = true;
  2330. }
  2331. //---------------------------------------------------------------------------
  2332. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2333. {
  2334. }
  2335. //---------------------------------------------------------------------------
  2336. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2337. {
  2338. DebugAssert(FErrorList == NULL);
  2339. if (WinConfiguration->ContinueOnError)
  2340. {
  2341. FErrorList = new TStringList();
  2342. Configuration->Usage->Inc(L"ContinuationsOnError");
  2343. }
  2344. NonVisualDataModule->StartBusy();
  2345. }
  2346. //---------------------------------------------------------------------------
  2347. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2348. {
  2349. NonVisualDataModule->EndBusy();
  2350. if (FErrorList)
  2351. {
  2352. HandleErrorList(FErrorList);
  2353. }
  2354. }
  2355. //---------------------------------------------------------------------------
  2356. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2357. const TCopyParamType & CopyParam)
  2358. {
  2359. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2360. {
  2361. Configuration->Usage->Inc(L"FileMaskUses");
  2362. }
  2363. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2364. {
  2365. Configuration->Usage->Inc(L"OperationMaskUses");
  2366. }
  2367. }
  2368. //---------------------------------------------------------------------------
  2369. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2370. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2371. {
  2372. DebugAssert(!IsLocalBrowserMode());
  2373. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2374. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2375. TTransferOperationParam DefaultParam;
  2376. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2377. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2378. switch (Param.Queue)
  2379. {
  2380. case asOn:
  2381. CopyParam.Queue = true;
  2382. break;
  2383. case asOff:
  2384. CopyParam.Queue = false;
  2385. break;
  2386. case asAuto:
  2387. default:
  2388. // keep default
  2389. break;
  2390. }
  2391. bool Result =
  2392. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2393. if (Result)
  2394. {
  2395. DebugAssert(Terminal);
  2396. bool SelectionRestored = false;
  2397. TCustomDirView * DView = NULL;
  2398. if (HasDirView[Side])
  2399. {
  2400. DView = DirView(Side);
  2401. DView->SaveSelection();
  2402. DView->SaveSelectedNames();
  2403. }
  2404. UpdateCopyParamCounters(CopyParam);
  2405. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2406. DebugAssert(FTransferResumeList == NULL);
  2407. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2408. FMoveToQueue = false;
  2409. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2410. try
  2411. {
  2412. TStrings * PermanentFileList;
  2413. std::unique_ptr<TStrings> PermanentFileListOwner;
  2414. try
  2415. {
  2416. if (Side == osLocal)
  2417. {
  2418. PermanentFileList = FileList;
  2419. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2420. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2421. if (Operation == foMove)
  2422. {
  2423. ReloadLocalDirectory();
  2424. if (DView != NULL)
  2425. {
  2426. DView->RestoreSelection();
  2427. }
  2428. SelectionRestored = true;
  2429. }
  2430. }
  2431. else
  2432. {
  2433. // Clone the file list as it may refer to current directory files,
  2434. // which get destroyed, when the source directory is reloaded after foMove operation.
  2435. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2436. // But for a hotfix, we are not going to do such a big change.
  2437. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2438. PermanentFileList = PermanentFileListOwner.get();
  2439. try
  2440. {
  2441. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2442. }
  2443. __finally
  2444. {
  2445. if (Operation == foMove)
  2446. {
  2447. if (DView != NULL)
  2448. {
  2449. DView->RestoreSelection();
  2450. }
  2451. SelectionRestored = true;
  2452. }
  2453. ReloadLocalDirectory(Param.TargetDirectory);
  2454. }
  2455. }
  2456. }
  2457. catch (EAbort &)
  2458. {
  2459. if (FMoveToQueue)
  2460. {
  2461. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2462. DebugAssert(CopyParam.TransferSkipList == NULL);
  2463. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2464. if (TransferResumeList->Count > 0)
  2465. {
  2466. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2467. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2468. }
  2469. CopyParam.TransferSkipList = TransferResumeList.release();
  2470. // not really needed, just to keep it consistent with TransferResumeList
  2471. FTransferResumeList = NULL;
  2472. FMoveToQueue = false;
  2473. Configuration->Usage->Inc("MovesToBackground");
  2474. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2475. ClearTransferSourceSelection(Direction);
  2476. }
  2477. throw;
  2478. }
  2479. }
  2480. __finally
  2481. {
  2482. if (!SelectionRestored && (DView != NULL))
  2483. {
  2484. DView->DiscardSavedSelection();
  2485. }
  2486. FTransferResumeList = NULL;
  2487. }
  2488. }
  2489. return Result;
  2490. }
  2491. //---------------------------------------------------------------------------
  2492. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2493. TOperationSide Side, TStrings * FileList, void * Param)
  2494. {
  2495. DebugAssert(FileList->Count);
  2496. // We deliberately do not toggle alternative flag (Param), but use OR,
  2497. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2498. // shortcut of CurrentDeleteAlternativeAction
  2499. bool Alternative =
  2500. bool(Param) || UseAlternativeFunction();
  2501. bool Recycle;
  2502. if (IsSideLocalBrowser(Side))
  2503. {
  2504. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2505. }
  2506. else
  2507. {
  2508. Recycle =
  2509. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2510. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2511. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2512. }
  2513. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2514. if (!Result)
  2515. {
  2516. UnicodeString Query;
  2517. if (FileList->Count == 1)
  2518. {
  2519. if (IsSideLocalBrowser(Side))
  2520. {
  2521. Query = ExtractFileName(FileList->Strings[0]);
  2522. }
  2523. else
  2524. {
  2525. Query = UnixExtractFileName(FileList->Strings[0]);
  2526. }
  2527. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2528. }
  2529. else
  2530. {
  2531. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2532. }
  2533. TMessageParams Params(mpNeverAskAgainCheck);
  2534. Params.ImageName = L"Delete file";
  2535. unsigned int Answer =
  2536. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2537. if (Answer == qaNeverAskAgain)
  2538. {
  2539. Result = true;
  2540. if (Recycle)
  2541. {
  2542. WinConfiguration->ConfirmRecycling = false;
  2543. }
  2544. else
  2545. {
  2546. WinConfiguration->ConfirmDeleting = false;
  2547. }
  2548. }
  2549. else
  2550. {
  2551. Result = (Answer == qaOK);
  2552. }
  2553. }
  2554. if (Result)
  2555. {
  2556. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2557. }
  2558. return Result;
  2559. }
  2560. //---------------------------------------------------------------------------
  2561. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2562. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2563. {
  2564. TAutoBatch AutoBatch(this);
  2565. bool Result;
  2566. if ((Operation == foCopy) || (Operation == foMove))
  2567. {
  2568. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2569. }
  2570. else if (Operation == foRename)
  2571. {
  2572. DebugAssert(DirView(Side)->ItemFocused);
  2573. DirView(Side)->ItemFocused->EditCaption();
  2574. Result = true;
  2575. }
  2576. else if (Operation == foDelete)
  2577. {
  2578. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2579. }
  2580. else if (Operation == foSetProperties)
  2581. {
  2582. DirView(osRemote)->SaveSelectedNames();
  2583. Result = SetProperties(Side, FileList);
  2584. }
  2585. else if (Operation == foCustomCommand)
  2586. {
  2587. DebugAssert(Param);
  2588. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2589. DirView(osRemote)->SaveSelectedNames();
  2590. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2591. CustomCommand(FileList, *Command, NULL);
  2592. Result = true;
  2593. }
  2594. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2595. {
  2596. DebugAssert(!IsSideLocalBrowser(Side));
  2597. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2598. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2599. }
  2600. else if (Operation == foLock)
  2601. {
  2602. DebugAssert(!IsSideLocalBrowser(Side));
  2603. LockFiles(FileList, true);
  2604. Result = true;
  2605. }
  2606. else if (Operation == foUnlock)
  2607. {
  2608. DebugAssert(!IsSideLocalBrowser(Side));
  2609. LockFiles(FileList, false);
  2610. Result = true;
  2611. }
  2612. else
  2613. {
  2614. DebugFail();
  2615. }
  2616. return Result;
  2617. }
  2618. //---------------------------------------------------------------------------
  2619. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2620. {
  2621. if (Side == osCurrent)
  2622. {
  2623. Side = FCurrentSide;
  2624. }
  2625. return Side;
  2626. }
  2627. //---------------------------------------------------------------------------
  2628. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2629. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2630. {
  2631. Side = GetSide(Side);
  2632. bool Result;
  2633. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2634. try
  2635. {
  2636. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2637. }
  2638. __finally
  2639. {
  2640. delete FileList;
  2641. }
  2642. return Result;
  2643. }
  2644. //---------------------------------------------------------------------------
  2645. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2646. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2647. bool NoConfirmation, void * Param)
  2648. {
  2649. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2650. {
  2651. if ((Operation == foCopy) || (Operation == foMove))
  2652. {
  2653. DebugAssert(!IsLocalBrowserMode());
  2654. if (GetSide(Side) == osLocal)
  2655. {
  2656. Configuration->Usage->Inc(L"UploadsCommand");
  2657. }
  2658. else
  2659. {
  2660. Configuration->Usage->Inc(L"DownloadsCommand");
  2661. }
  2662. }
  2663. }
  2664. }
  2665. //---------------------------------------------------------------------------
  2666. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2667. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2668. {
  2669. TTransferOperationParam Param;
  2670. if ((WinConfiguration->Interface != ifCommander) ||
  2671. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2672. {
  2673. Flags &= ~cocShortCutHint;
  2674. }
  2675. TCustomDirView * DView = DirView(Side);
  2676. Param.Options =
  2677. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2678. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2679. if (FLAGSET(Flags, cocQueue))
  2680. {
  2681. Param.Queue = asOn;
  2682. }
  2683. else if (FLAGSET(Flags, cocNonQueue))
  2684. {
  2685. Param.Queue = asOff;
  2686. }
  2687. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2688. }
  2689. //---------------------------------------------------------------------------
  2690. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2691. {
  2692. try
  2693. {
  2694. if (ErrorList->Count)
  2695. {
  2696. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2697. if (MessageDialog(Message, qtError,
  2698. qaOK | qaCancel, HELP_NONE) == qaOK)
  2699. {
  2700. unsigned int Answer;
  2701. int Index = 0;
  2702. do
  2703. {
  2704. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2705. TQueryButtonAlias Aliases[2];
  2706. Aliases[0].Button = qaYes;
  2707. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2708. Aliases[1].Button = qaNo;
  2709. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2710. TMessageParams Params;
  2711. Params.Aliases = Aliases;
  2712. Params.AliasesCount = LENOF(Aliases);
  2713. Answer = MoreMessageDialog(
  2714. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2715. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2716. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2717. qaOK, HELP_NONE, &Params);
  2718. if (Answer == qaNo)
  2719. {
  2720. Index++;
  2721. }
  2722. if (Answer == qaYes)
  2723. {
  2724. Index--;
  2725. }
  2726. }
  2727. while (Answer != qaOK);
  2728. }
  2729. }
  2730. }
  2731. __finally
  2732. {
  2733. TStrings * List = ErrorList;
  2734. ErrorList = NULL;
  2735. for (int i = 0; i < List->Count; i++)
  2736. {
  2737. delete List->Objects[i];
  2738. }
  2739. delete List;
  2740. }
  2741. }
  2742. //---------------------------------------------------------------------------
  2743. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2744. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2745. {
  2746. // needed for checking filemasks, as there's no directory object
  2747. // associated with the file object
  2748. File->FullFileName = FullFileName;
  2749. TFileMasks::TParams MaskParams;
  2750. MaskParams.Size = File->Size;
  2751. MaskParams.Modification = File->Modification;
  2752. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2753. }
  2754. //---------------------------------------------------------------------------
  2755. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2756. {
  2757. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2758. Side = GetSide(Side);
  2759. TCustomDirView * DView = DirView(Side);
  2760. TListItem * FocusedItem = DView->ItemFocused;
  2761. UnicodeString Name;
  2762. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2763. {
  2764. Name = DView->ItemFileName(FocusedItem);
  2765. }
  2766. else
  2767. {
  2768. Name = LoadStr(NEW_FILE);
  2769. }
  2770. UnicodeString Names = Name;
  2771. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2772. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2773. HELP_EDIT_NEW, History.get(), true))
  2774. {
  2775. while (!Names.IsEmpty())
  2776. {
  2777. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2778. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2779. UnicodeString TargetFileName;
  2780. UnicodeString LocalFileName;
  2781. UnicodeString RootTempDir;
  2782. UnicodeString TempDir;
  2783. UnicodeString RemoteDirectory;
  2784. bool ExistingFile = false;
  2785. if (!IsSideLocalBrowser(Side))
  2786. {
  2787. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2788. TRemoteFile * File = NULL;
  2789. if (FTerminal->FileExists(Name, &File) &&
  2790. !File->IsDirectory)
  2791. {
  2792. try
  2793. {
  2794. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2795. ExistingFile = true;
  2796. }
  2797. __finally
  2798. {
  2799. delete File;
  2800. }
  2801. }
  2802. if (!ExistingFile)
  2803. {
  2804. RemoteDirectory = UnixExtractFilePath(Name);
  2805. TemporaryDirectoryForRemoteFiles(
  2806. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2807. TargetFileName = UnixExtractFileName(Name);
  2808. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2809. LocalFileName = TempDir +
  2810. // We probably do not want to trim the VMS version here
  2811. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2812. }
  2813. }
  2814. else
  2815. {
  2816. if (ExtractFilePath(Name).IsEmpty())
  2817. {
  2818. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2819. }
  2820. else
  2821. {
  2822. LocalFileName = ExpandFileName(Name);
  2823. }
  2824. TargetFileName = ExtractFileName(Name);
  2825. }
  2826. if (!ExistingFile)
  2827. {
  2828. if (!FileExists(ApiPath(LocalFileName)))
  2829. {
  2830. int File = FileCreate(ApiPath(LocalFileName));
  2831. if (File < 0)
  2832. {
  2833. if (!RootTempDir.IsEmpty())
  2834. {
  2835. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2836. }
  2837. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2838. }
  2839. else
  2840. {
  2841. FileClose(File);
  2842. }
  2843. }
  2844. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2845. const TEditorData * ExternalEditor = NULL;
  2846. TFileMasks::TParams MaskParams; // size not known
  2847. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2848. false, MaskParams);
  2849. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2850. ExternalEditor, RootTempDir, RemoteDirectory);
  2851. }
  2852. }
  2853. }
  2854. }
  2855. //---------------------------------------------------------------------------
  2856. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2857. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2858. {
  2859. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2860. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2861. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2862. return
  2863. ((ExecuteFileBy == efInternalEditor)) ||
  2864. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2865. }
  2866. //---------------------------------------------------------------------------
  2867. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2868. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2869. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2870. UnicodeString RemoteDirectory)
  2871. {
  2872. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2873. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2874. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2875. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2876. Side = GetSide(Side);
  2877. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2878. if (!IsSideLocalBrowser(Side))
  2879. {
  2880. Data->Terminal = Terminal;
  2881. Data->Queue = Queue;
  2882. Data->SessionData = CloneCurrentSessionData();
  2883. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2884. Data->RemoteDirectory = RemoteDirectory;
  2885. Data->SessionName = Terminal->SessionData->SessionName;
  2886. Data->LocalRootDirectory = LocalRootDirectory;
  2887. Data->OriginalFileName = OriginalFileName;
  2888. Data->Command = L""; // will be changed later for external editor
  2889. }
  2890. if (ExecuteFileBy == efInternalEditor)
  2891. {
  2892. if (!IsSideLocalBrowser(Side))
  2893. {
  2894. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2895. L" - " + Terminal->SessionData->SessionName;
  2896. TForm * Editor;
  2897. try
  2898. {
  2899. TNotifyEvent OnFileChanged = NULL;
  2900. TNotifyEvent OnSaveAll = NULL;
  2901. TAnyModifiedEvent OnAnyModified = NULL;
  2902. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2903. if (Terminal->IsCapable[fsBackgroundTransfers])
  2904. {
  2905. OnFileChanged = FEditorManager->FileChanged;
  2906. OnSaveAll = SaveAllInternalEditors;
  2907. OnAnyModified = AnyInternalEditorModified;
  2908. }
  2909. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2910. FEditorManager->FileReload, FEditorManager->FileClosed,
  2911. OnSaveAll, OnAnyModified,
  2912. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2913. }
  2914. catch(...)
  2915. {
  2916. if (!LocalRootDirectory.IsEmpty())
  2917. {
  2918. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2919. }
  2920. throw;
  2921. }
  2922. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2923. }
  2924. else
  2925. {
  2926. DebugAssert(!FStandaloneEditing);
  2927. TForm * Editor =
  2928. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2929. SaveAllInternalEditors, AnyInternalEditorModified,
  2930. L"", false, SessionColor, -1);
  2931. FLocalEditors->Add(Editor);
  2932. }
  2933. }
  2934. else
  2935. {
  2936. HANDLE Process;
  2937. if (ExecuteFileBy == efExternalEditor)
  2938. {
  2939. UnicodeString Program, Params, Dir;
  2940. Data->Command = ExternalEditor->ExternalEditor;
  2941. ReformatFileNameCommand(Data->Command);
  2942. SplitCommand(Data->Command, Program, Params, Dir);
  2943. Params = ExpandFileNameCommand(Params, FileName);
  2944. Program = ExpandEnvironmentVariables(Program);
  2945. if (!ExecuteShell(Program, Params, Process))
  2946. {
  2947. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2948. }
  2949. }
  2950. else
  2951. {
  2952. DebugAssert(IsSideLocalBrowser(Side));
  2953. if (!ExecuteShell(FileName, L"", Process))
  2954. {
  2955. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2956. }
  2957. }
  2958. if (IsSideLocalBrowser(Side) ||
  2959. ((ExecuteFileBy == efShell) &&
  2960. !WinConfiguration->Editor.SDIShellEditor) ||
  2961. ((ExecuteFileBy == efExternalEditor) &&
  2962. !ExternalEditor->SDIExternalEditor))
  2963. {
  2964. // no need for handle
  2965. if (Process != NULL)
  2966. {
  2967. DebugCheck(CloseHandle(Process));
  2968. }
  2969. Process = INVALID_HANDLE_VALUE;
  2970. }
  2971. else
  2972. {
  2973. if (Process == NULL)
  2974. {
  2975. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2976. }
  2977. }
  2978. if (!IsSideLocalBrowser(Side))
  2979. {
  2980. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2981. }
  2982. }
  2983. }
  2984. //---------------------------------------------------------------------------
  2985. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2986. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2987. void * /*Arg*/)
  2988. {
  2989. if (Token != NULL)
  2990. {
  2991. EditorFormFileSave(static_cast<TForm *>(Token));
  2992. }
  2993. }
  2994. //---------------------------------------------------------------------------
  2995. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2996. {
  2997. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2998. {
  2999. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3000. }
  3001. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3002. }
  3003. //---------------------------------------------------------------------------
  3004. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3005. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3006. void * Arg)
  3007. {
  3008. if ((Token != NULL) &&
  3009. IsEditorFormModified(static_cast<TForm *>(Token)))
  3010. {
  3011. *static_cast<bool *>(Arg) = true;
  3012. }
  3013. }
  3014. //---------------------------------------------------------------------------
  3015. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3016. TObject * /*Sender*/, bool & Modified)
  3017. {
  3018. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3019. {
  3020. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3021. {
  3022. Modified = true;
  3023. }
  3024. }
  3025. if (!Modified)
  3026. {
  3027. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3028. }
  3029. }
  3030. //---------------------------------------------------------------------------
  3031. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3032. {
  3033. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3034. }
  3035. //---------------------------------------------------------------------------
  3036. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3037. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3038. UnicodeString & Result, UnicodeString & RootDirectory)
  3039. {
  3040. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3041. {
  3042. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3043. Result = RootDirectory;
  3044. }
  3045. else
  3046. {
  3047. RootDirectory = L"";
  3048. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3049. Result = IncludeTrailingBackslash(Result);
  3050. }
  3051. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3052. {
  3053. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3054. }
  3055. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3056. {
  3057. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3058. {
  3059. RemoteDirectory.Delete(1, 1);
  3060. }
  3061. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3062. }
  3063. if (!ForceDirectories(ApiPath(Result)))
  3064. {
  3065. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3066. }
  3067. }
  3068. //---------------------------------------------------------------------------
  3069. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3070. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3071. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3072. {
  3073. DebugAssert(!IsLocalBrowserMode());
  3074. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3075. if (ForceText)
  3076. {
  3077. CopyParam.TransferMode = tmAscii;
  3078. }
  3079. TemporaryFileCopyParam(CopyParam);
  3080. if (AllFiles)
  3081. {
  3082. CopyParam.IncludeFileMask = TFileMasks();
  3083. }
  3084. if (TempDir.IsEmpty())
  3085. {
  3086. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3087. }
  3088. DebugAssert(!FAutoOperation);
  3089. FAutoOperation = AutoOperation;
  3090. Terminal->ExceptionOnFail = true;
  3091. try
  3092. {
  3093. try
  3094. {
  3095. // turn off confirmations, as for MDI editors we may possibly download
  3096. // the same file over
  3097. Terminal->CopyToLocal(
  3098. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3099. if (GetTargetNames)
  3100. {
  3101. for (int i = 0; i < FileList->Count; i++)
  3102. {
  3103. FileList->Strings[i] =
  3104. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3105. }
  3106. }
  3107. }
  3108. catch(...)
  3109. {
  3110. if (!RootTempDir.IsEmpty())
  3111. {
  3112. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3113. }
  3114. throw;
  3115. }
  3116. }
  3117. __finally
  3118. {
  3119. FAutoOperation = false;
  3120. Terminal->ExceptionOnFail = false;
  3121. }
  3122. }
  3123. //---------------------------------------------------------------------------
  3124. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3125. {
  3126. // Do not waste time checking for default editor list the next time,
  3127. // or testing if default editor is not notepad.
  3128. bool TryNextTime = false;
  3129. UnicodeString UsageState;
  3130. if (!WinConfiguration->EditorList->IsDefaultList())
  3131. {
  3132. UsageState = "H";
  3133. }
  3134. else
  3135. {
  3136. UnicodeString Executable;
  3137. UnicodeString ExecutableDescription;
  3138. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3139. {
  3140. UnicodeString Message =
  3141. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3142. unsigned int Answer =
  3143. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3144. if (Answer != qaOK)
  3145. {
  3146. UsageState = "R";
  3147. }
  3148. else
  3149. {
  3150. UsageState = "A";
  3151. TEditorData EditorData;
  3152. EditorData.Editor = edExternal;
  3153. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3154. EditorData.DecideExternalEditorText();
  3155. TEditorList EditorList;
  3156. EditorList = *WinConfiguration->EditorList;
  3157. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3158. WinConfiguration->EditorList = &EditorList;
  3159. }
  3160. }
  3161. }
  3162. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3163. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3164. }
  3165. //---------------------------------------------------------------------------
  3166. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3167. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3168. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3169. {
  3170. if (ExecuteFileBy == efDefaultEditor)
  3171. {
  3172. if (!WinConfiguration->OfferedEditorAutoConfig)
  3173. {
  3174. EditorAutoConfig();
  3175. }
  3176. const TEditorPreferences * Editor =
  3177. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3178. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3179. {
  3180. ExecuteFileBy = efInternalEditor;
  3181. ExternalEditor = NULL;
  3182. }
  3183. else if (Editor->Data->Editor == edOpen)
  3184. {
  3185. ExecuteFileBy = efShell;
  3186. ExternalEditor = NULL;
  3187. }
  3188. else
  3189. {
  3190. ExecuteFileBy = efExternalEditor;
  3191. ExternalEditor = Editor->Data;
  3192. }
  3193. }
  3194. }
  3195. //---------------------------------------------------------------------------
  3196. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3197. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3198. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3199. {
  3200. UnicodeString OriginalFileName;
  3201. UnicodeString LocalRootDirectory;
  3202. UnicodeString RemoteDirectory;
  3203. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3204. IsSideLocalBrowser(Side), MaskParams);
  3205. UnicodeString Counter;
  3206. UnicodeString LocalFileName;
  3207. bool Handled = false;
  3208. if (!IsSideLocalBrowser(Side))
  3209. {
  3210. // We need to trim VMS version here, so that we use name without version
  3211. // when uploading back to create a new version of the file
  3212. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3213. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3214. TObject * Token = NULL;
  3215. UnicodeString LocalDirectory;
  3216. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3217. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3218. LocalDirectory))
  3219. {
  3220. if (Token != NULL)
  3221. {
  3222. TForm * Form = dynamic_cast<TForm *>(Token);
  3223. if (Form->WindowState == wsMinimized)
  3224. {
  3225. ShowWindow(Form->Handle, SW_RESTORE);
  3226. }
  3227. else
  3228. {
  3229. Form->SetFocus();
  3230. }
  3231. Handled = true;
  3232. }
  3233. else
  3234. {
  3235. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3236. }
  3237. }
  3238. if (!Handled)
  3239. {
  3240. TStringList * FileList1 = new TStringList();
  3241. try
  3242. {
  3243. FileList1->AddObject(FullFileName, Object);
  3244. TemporarilyDownloadFiles(FileList1,
  3245. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3246. LocalRootDirectory, LocalDirectory, true, true, true);
  3247. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3248. }
  3249. __finally
  3250. {
  3251. delete FileList1;
  3252. }
  3253. switch (ExecuteFileBy)
  3254. {
  3255. case efShell:
  3256. Counter = "RemoteFilesExecuted";
  3257. break;
  3258. case efInternalEditor:
  3259. Counter = "RemoteFilesOpenedInInternalEditor";
  3260. break;
  3261. case efExternalEditor:
  3262. Counter = "RemoteFilesOpenedInExternalEditor";
  3263. break;
  3264. default:
  3265. DebugFail();
  3266. }
  3267. }
  3268. }
  3269. else
  3270. {
  3271. LocalFileName = FullFileName;
  3272. OriginalFileName = ExtractFileName(FullFileName);
  3273. switch (ExecuteFileBy)
  3274. {
  3275. case efShell:
  3276. Counter = "LocalFilesExecuted";
  3277. break;
  3278. case efInternalEditor:
  3279. Counter = "LocalFilesOpenedInInternalEditor";
  3280. break;
  3281. case efExternalEditor:
  3282. Counter = "LocalFilesOpenedInExternalEditor";
  3283. break;
  3284. default:
  3285. DebugFail();
  3286. }
  3287. }
  3288. if (!Handled)
  3289. {
  3290. Configuration->Usage->Inc(Counter);
  3291. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3292. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3293. }
  3294. }
  3295. //---------------------------------------------------------------------------
  3296. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3297. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3298. bool AllSelected, bool OnFocused)
  3299. {
  3300. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3301. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3302. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3303. Side = GetSide(Side);
  3304. TCustomDirView * DView = DirView(Side);
  3305. TStrings * FileList =
  3306. AllSelected ?
  3307. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3308. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3309. try
  3310. {
  3311. DebugAssert(AllSelected || (FileList->Count == 1));
  3312. for (int i = 0; i < FileList->Count; i++)
  3313. {
  3314. UnicodeString ListFileName = FileList->Strings[i];
  3315. UnicodeString FileNameOnly =
  3316. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3317. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3318. if (!DView->ItemIsDirectory(Item))
  3319. {
  3320. UnicodeString FullFileName;
  3321. if (!IsSideLocalBrowser(Side))
  3322. {
  3323. FullFileName = DirView(Side)->Path + ListFileName;
  3324. }
  3325. else
  3326. {
  3327. FullFileName = ListFileName;
  3328. }
  3329. TFileMasks::TParams MaskParams;
  3330. MaskParams.Size = DView->ItemFileSize(Item);
  3331. TDateTimePrecision Precision;
  3332. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3333. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3334. FileList->Objects[i], MaskParams);
  3335. }
  3336. }
  3337. }
  3338. __finally
  3339. {
  3340. delete FileList;
  3341. }
  3342. }
  3343. //---------------------------------------------------------------------------
  3344. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3345. {
  3346. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3347. CopyParam.FileNameCase = ncNoChange;
  3348. CopyParam.PreserveRights = false;
  3349. CopyParam.PreserveReadOnly = false;
  3350. CopyParam.ReplaceInvalidChars = true;
  3351. CopyParam.IncludeFileMask = TFileMasks();
  3352. CopyParam.NewerOnly = false;
  3353. CopyParam.FileMask = L"";
  3354. }
  3355. //---------------------------------------------------------------------------
  3356. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3357. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3358. {
  3359. TTerminalManager * Manager = TTerminalManager::Instance();
  3360. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3361. {
  3362. if (!NonVisualDataModule->Busy)
  3363. {
  3364. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3365. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3366. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3367. if (Data->Terminal == NULL)
  3368. {
  3369. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3370. if (SameSiteTerminal != NULL)
  3371. {
  3372. UnicodeString Message =
  3373. FMTLOAD(EDIT_SESSION_REATTACH,
  3374. (FileNameOnly, Data->SessionName, FileNameOnly));
  3375. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3376. {
  3377. Data->Terminal = Terminal;
  3378. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3379. Data->SessionName = Terminal->SessionData->SessionName;
  3380. // We might also overwrite session data
  3381. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3382. }
  3383. else
  3384. {
  3385. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3386. Abort();
  3387. }
  3388. }
  3389. }
  3390. // foreground session should reconnect itself
  3391. else if (Terminal != Data->Terminal)
  3392. {
  3393. UnicodeString Message =
  3394. MainInstructions(
  3395. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3396. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3397. {
  3398. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3399. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3400. }
  3401. else
  3402. {
  3403. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3404. Abort();
  3405. }
  3406. }
  3407. }
  3408. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3409. {
  3410. Configuration->Usage->Inc(L"EditInactiveSession");
  3411. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3412. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3413. (ExtractFileName(FileName), Data->SessionName)));
  3414. }
  3415. }
  3416. TStrings * FileList = new TStringList();
  3417. try
  3418. {
  3419. FileList->Add(FileName);
  3420. // consider using the same settings (preset) as when the file was downloaded
  3421. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3422. TemporaryFileCopyParam(CopyParam);
  3423. if (Data->ForceText)
  3424. {
  3425. CopyParam.TransferMode = tmAscii;
  3426. }
  3427. // so i do not need to worry if masking algorithm works in all cases
  3428. // ("" means "copy file name", no masking is actually done)
  3429. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3430. {
  3431. CopyParam.FileMask = L"";
  3432. }
  3433. else
  3434. {
  3435. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3436. }
  3437. DebugAssert(Data->Queue != NULL);
  3438. int Params = cpNoConfirmation | cpTemporary;
  3439. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3440. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3441. QueueItem->CompleteEvent = UploadCompleteEvent;
  3442. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3443. }
  3444. __finally
  3445. {
  3446. delete FileList;
  3447. }
  3448. }
  3449. //---------------------------------------------------------------------------
  3450. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3451. const UnicodeString FileName, const TEditedFileData * Data)
  3452. {
  3453. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3454. DebugAssert(!NonVisualDataModule->Busy);
  3455. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3456. {
  3457. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3458. (ExtractFileName(FileName), Data->SessionName)));
  3459. }
  3460. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3461. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3462. NonVisualDataModule->StartBusy();
  3463. try
  3464. {
  3465. std::unique_ptr<TRemoteFile> File;
  3466. UnicodeString RemoteFileName =
  3467. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3468. FTerminal->ExceptionOnFail = true;
  3469. try
  3470. {
  3471. TRemoteFile * AFile = NULL;
  3472. FTerminal->ReadFile(RemoteFileName, AFile);
  3473. File.reset(AFile);
  3474. if (!File->HaveFullFileName)
  3475. {
  3476. File->FullFileName = RemoteFileName;
  3477. }
  3478. }
  3479. __finally
  3480. {
  3481. FTerminal->ExceptionOnFail = false;
  3482. }
  3483. std::unique_ptr<TStrings> FileList(new TStringList());
  3484. FileList->AddObject(RemoteFileName, File.get());
  3485. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3486. UnicodeString TempDir = ExtractFilePath(FileName);
  3487. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3488. TempDir, true, true, true);
  3489. // sanity check, the target file name should be still the same
  3490. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3491. }
  3492. __finally
  3493. {
  3494. NonVisualDataModule->EndBusy();
  3495. // it actually may not exist anymore...
  3496. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3497. }
  3498. }
  3499. //---------------------------------------------------------------------------
  3500. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3501. const TEditedFileData * Data, bool & KeepOpen)
  3502. {
  3503. // Command is set for external editors only (not for "shell" open).
  3504. if (!Data->Command.IsEmpty())
  3505. {
  3506. bool AnyFound = false;
  3507. bool AnyMDI = false;
  3508. bool AnyNonMDI = false;
  3509. bool AnyDetect = false;
  3510. TEditorList * EditorList = new TEditorList();
  3511. try
  3512. {
  3513. *EditorList = *WinConfiguration->EditorList;
  3514. for (int i = 0; i < EditorList->Count; i++)
  3515. {
  3516. const TEditorPreferences * Editor = EditorList->Editors[i];
  3517. if ((Editor->Data->Editor == edExternal) &&
  3518. (Editor->Data->ExternalEditor == Data->Command))
  3519. {
  3520. AnyFound = true;
  3521. if (Editor->Data->SDIExternalEditor)
  3522. {
  3523. AnyNonMDI = true;
  3524. if (Editor->Data->DetectMDIExternalEditor)
  3525. {
  3526. AnyDetect = true;
  3527. }
  3528. }
  3529. else
  3530. {
  3531. AnyMDI = true;
  3532. }
  3533. }
  3534. }
  3535. bool EnableMDI = false;
  3536. bool DisableDetect = false;
  3537. if (AnyMDI)
  3538. {
  3539. KeepOpen = true;
  3540. if (AnyNonMDI)
  3541. {
  3542. // there is at least one instance of the editor with MDI support enabled,
  3543. // and one with disabled, enable it for all instances
  3544. EnableMDI = true;
  3545. }
  3546. }
  3547. else if (AnyFound && !AnyDetect)
  3548. {
  3549. // at least once instance found but all have MDI autodetection disabled
  3550. // => close the file (default action)
  3551. }
  3552. else
  3553. {
  3554. // no instance of the editor has MDI support enabled
  3555. TMessageParams Params;
  3556. if (AnyFound)
  3557. {
  3558. // there is at least one instance of the editor
  3559. Params.Params |= mpNeverAskAgainCheck;
  3560. }
  3561. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3562. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3563. switch (Answer)
  3564. {
  3565. case qaNeverAskAgain:
  3566. DisableDetect = true;
  3567. break;
  3568. case qaNo:
  3569. EnableMDI = true;
  3570. KeepOpen = true;
  3571. break;
  3572. }
  3573. }
  3574. if (AnyFound && (EnableMDI || DisableDetect))
  3575. {
  3576. bool Changed = false;
  3577. for (int i = 0; i < EditorList->Count; i++)
  3578. {
  3579. const TEditorPreferences * Editor = EditorList->Editors[i];
  3580. if ((Editor->Data->Editor == edExternal) &&
  3581. (Editor->Data->ExternalEditor == Data->Command) &&
  3582. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3583. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3584. {
  3585. Changed = true;
  3586. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3587. if (EnableMDI)
  3588. {
  3589. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3590. }
  3591. if (DisableDetect)
  3592. {
  3593. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3594. }
  3595. EditorList->Change(i, UpdatedEditor);
  3596. }
  3597. }
  3598. if (Changed)
  3599. {
  3600. WinConfiguration->EditorList = EditorList;
  3601. }
  3602. }
  3603. }
  3604. __finally
  3605. {
  3606. delete EditorList;
  3607. }
  3608. }
  3609. else
  3610. {
  3611. // "open" case
  3612. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3613. qaOK, HELP_APP_EARLY_CLOSED);
  3614. }
  3615. }
  3616. //---------------------------------------------------------------------------
  3617. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3618. {
  3619. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3620. }
  3621. //---------------------------------------------------------------------------
  3622. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3623. {
  3624. SideEnter(osRemote);
  3625. }
  3626. //---------------------------------------------------------------------------
  3627. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3628. {
  3629. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3630. SideEnter(osRemote);
  3631. }
  3632. //---------------------------------------------------------------------------
  3633. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3634. {
  3635. FCurrentSide = Side;
  3636. if (Visible)
  3637. {
  3638. UpdateControls();
  3639. }
  3640. }
  3641. //---------------------------------------------------------------------------
  3642. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3643. TStrings * FileList, bool Alternative)
  3644. {
  3645. TCustomDirView * DView = DirView(Side);
  3646. DView->SaveSelection();
  3647. DView->SaveSelectedNames();
  3648. DebugAssert(!FAlternativeDelete);
  3649. FAlternativeDelete = Alternative;
  3650. try
  3651. {
  3652. if (!IsSideLocalBrowser(Side))
  3653. {
  3654. DebugAssert(Terminal != NULL);
  3655. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3656. }
  3657. else
  3658. {
  3659. try
  3660. {
  3661. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3662. }
  3663. __finally
  3664. {
  3665. ReloadLocalDirectory();
  3666. }
  3667. }
  3668. FAlternativeDelete = false;
  3669. }
  3670. catch(...)
  3671. {
  3672. FAlternativeDelete = false;
  3673. DView->DiscardSavedSelection();
  3674. throw;
  3675. }
  3676. DView->RestoreSelection();
  3677. }
  3678. //---------------------------------------------------------------------------
  3679. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3680. {
  3681. DebugAssert(Terminal);
  3682. DebugAssert(!IsLocalBrowserMode());
  3683. RemoteDirView->SaveSelection();
  3684. RemoteDirView->SaveSelectedNames();
  3685. try
  3686. {
  3687. if (Lock)
  3688. {
  3689. Terminal->LockFiles(FileList);
  3690. }
  3691. else
  3692. {
  3693. Terminal->UnlockFiles(FileList);
  3694. }
  3695. }
  3696. catch(...)
  3697. {
  3698. RemoteDirView->DiscardSavedSelection();
  3699. throw;
  3700. }
  3701. RemoteDirView->RestoreSelection();
  3702. }
  3703. //---------------------------------------------------------------------------
  3704. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3705. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3706. bool NoConfirmation, bool Move)
  3707. {
  3708. DebugAssert(Terminal != NULL);
  3709. DebugAssert(!IsLocalBrowserMode());
  3710. // update Terminal->StateData->RemoteDirectory
  3711. UpdateTerminal(Terminal);
  3712. if (Session == NULL)
  3713. {
  3714. Session = TTerminalManager::Instance()->ActiveTerminal;
  3715. }
  3716. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3717. {
  3718. if (RemoteDriveView->DropTarget != NULL)
  3719. {
  3720. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3721. }
  3722. else if (RemoteDirView->DropTarget != NULL)
  3723. {
  3724. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3725. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3726. }
  3727. else
  3728. {
  3729. Target = RemoteDirView->Path;
  3730. }
  3731. }
  3732. else
  3733. {
  3734. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3735. Target = ManagedTerminal->StateData->RemoteDirectory;
  3736. }
  3737. Target = UnixIncludeTrailingBackslash(Target);
  3738. if (FileList->Count == 1)
  3739. {
  3740. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3741. }
  3742. else
  3743. {
  3744. FileMask = AnyMask;
  3745. }
  3746. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3747. bool Result = true;
  3748. if (!NoConfirmation)
  3749. {
  3750. bool Multi = (FileList->Count > 1);
  3751. if (Move)
  3752. {
  3753. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3754. }
  3755. else
  3756. {
  3757. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3758. TStrings * Directories = new TStringList;
  3759. try
  3760. {
  3761. for (int Index = 0; Index < Sessions->Count; Index++)
  3762. {
  3763. TManagedTerminal * Terminal =
  3764. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3765. Directories->Add(Terminal->StateData->RemoteDirectory);
  3766. }
  3767. TDirectRemoteCopy AllowDirectCopy;
  3768. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3769. {
  3770. DebugAssert(DirectCopy);
  3771. AllowDirectCopy = drcAllow;
  3772. }
  3773. else if (FTerminal->IsCapable[fcSecondaryShell])
  3774. {
  3775. DebugAssert(DirectCopy);
  3776. AllowDirectCopy = drcConfirmCommandSession;
  3777. }
  3778. else
  3779. {
  3780. DebugAssert(!DirectCopy);
  3781. AllowDirectCopy = drcDisallow;
  3782. }
  3783. void * ASession = Session;
  3784. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3785. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3786. Session = static_cast<TTerminal *>(ASession);
  3787. }
  3788. __finally
  3789. {
  3790. delete Directories;
  3791. }
  3792. }
  3793. }
  3794. return Result;
  3795. }
  3796. //---------------------------------------------------------------------------
  3797. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3798. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3799. {
  3800. DebugAssert(!IsLocalBrowserMode());
  3801. bool DirectCopy;
  3802. UnicodeString Target, FileMask;
  3803. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3804. if (Result)
  3805. {
  3806. if (!Move && !DirectCopy)
  3807. {
  3808. Configuration->Usage->Inc("RemoteCopyTemp");
  3809. UnicodeString RootTempDir;
  3810. UnicodeString TempDir;
  3811. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3812. TStrings * TemporaryFilesList = new TStringList();
  3813. try
  3814. {
  3815. TMakeLocalFileListParams MakeFileListParam;
  3816. MakeFileListParam.FileList = TemporaryFilesList;
  3817. MakeFileListParam.FileTimes = NULL;
  3818. MakeFileListParam.IncludeDirs = true;
  3819. MakeFileListParam.Recursive = false;
  3820. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3821. TTerminalManager::Instance()->ActiveTerminal = Session;
  3822. if (TemporaryFilesList->Count > 0)
  3823. {
  3824. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3825. CopyParam.FileMask = FileMask;
  3826. DebugAssert(!FAutoOperation);
  3827. FAutoOperation = true;
  3828. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3829. }
  3830. }
  3831. __finally
  3832. {
  3833. delete TemporaryFilesList;
  3834. FAutoOperation = false;
  3835. if (!RootTempDir.IsEmpty())
  3836. {
  3837. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3838. }
  3839. }
  3840. }
  3841. else
  3842. {
  3843. RemoteDirView->SaveSelection();
  3844. RemoteDirView->SaveSelectedNames();
  3845. try
  3846. {
  3847. if (Move)
  3848. {
  3849. Configuration->Usage->Inc("RemoteMove");
  3850. Terminal->MoveFiles(FileList, Target, FileMask);
  3851. }
  3852. else
  3853. {
  3854. Configuration->Usage->Inc("RemoteCopyDirect");
  3855. DebugAssert(DirectCopy);
  3856. DebugAssert(Session == FTerminal);
  3857. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3858. FTerminal->CommandSessionOpened ||
  3859. CommandSessionFallback())
  3860. {
  3861. Terminal->CopyFiles(FileList, Target, FileMask);
  3862. }
  3863. }
  3864. }
  3865. catch(...)
  3866. {
  3867. RemoteDirView->DiscardSavedSelection();
  3868. throw;
  3869. }
  3870. RemoteDirView->RestoreSelection();
  3871. }
  3872. }
  3873. return Result;
  3874. }
  3875. //---------------------------------------------------------------------------
  3876. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3877. const UnicodeString & Path, TRemoteProperties & Properties)
  3878. {
  3879. DebugAssert(!IsLocalBrowserMode());
  3880. Properties.Valid = Properties.Valid << vpEncrypt;
  3881. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3882. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3883. }
  3884. //---------------------------------------------------------------------------
  3885. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3886. {
  3887. Side = GetSide(Side);
  3888. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3889. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3890. UnicodeString Name = LoadStr(NEW_FOLDER);
  3891. int AllowedChanges =
  3892. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3893. bool SaveSettings = false;
  3894. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3895. {
  3896. TWindowLock Lock(this);
  3897. if (!IsSideLocalBrowser(Side))
  3898. {
  3899. if (SaveSettings)
  3900. {
  3901. GUIConfiguration->NewDirectoryProperties = Properties;
  3902. }
  3903. CreateRemoteDirectory(Name, Properties);
  3904. }
  3905. else
  3906. {
  3907. DirView(Side)->CreateDirectory(Name);
  3908. }
  3909. }
  3910. }
  3911. //---------------------------------------------------------------------------
  3912. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3913. {
  3914. TWindowLock Lock(this);
  3915. DirView(Side)->ExecuteHomeDirectory();
  3916. }
  3917. //---------------------------------------------------------------------------
  3918. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3919. {
  3920. DoOpenDirectoryDialog(odBrowse, Side);
  3921. }
  3922. //---------------------------------------------------------------------------
  3923. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3924. {
  3925. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3926. bool Result = !Path.IsEmpty();
  3927. if (Result)
  3928. {
  3929. // While we might get here when the session is closed (from location profiles),
  3930. // it's not a problem as the Path setter is noop then
  3931. DirView(Side)->Path = Path;
  3932. }
  3933. return Result;
  3934. }
  3935. //---------------------------------------------------------------------------
  3936. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3937. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3938. bool & Close)
  3939. {
  3940. // terminal can be already closed (e.g. dropped connection)
  3941. if (Terminal != NULL)
  3942. {
  3943. try
  3944. {
  3945. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3946. }
  3947. catch(...)
  3948. {
  3949. if (!Terminal->Active)
  3950. {
  3951. Close = true;
  3952. }
  3953. throw;
  3954. }
  3955. }
  3956. }
  3957. //---------------------------------------------------------------------------
  3958. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3959. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3960. bool & Close)
  3961. {
  3962. // terminal can be already closed (e.g. dropped connection)
  3963. if (Terminal != NULL)
  3964. {
  3965. Configuration->Usage->Inc(L"ChecksumCalculated");
  3966. try
  3967. {
  3968. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3969. }
  3970. catch(...)
  3971. {
  3972. if (!Terminal->Active)
  3973. {
  3974. Close = true;
  3975. }
  3976. throw;
  3977. }
  3978. }
  3979. }
  3980. //---------------------------------------------------------------------------
  3981. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3982. {
  3983. bool Result;
  3984. if (!IsSideLocalBrowser(Side))
  3985. {
  3986. TRemoteTokenList * GroupList = NULL;
  3987. TRemoteTokenList * UserList = NULL;
  3988. try
  3989. {
  3990. TRemoteProperties CurrentProperties;
  3991. if (Terminal->LoadFilesProperties(FileList))
  3992. {
  3993. RemoteDirView->Invalidate();
  3994. }
  3995. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3996. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3997. if (CapableGroupChanging || CapableOwnerChanging)
  3998. {
  3999. if (CapableGroupChanging)
  4000. {
  4001. GroupList = Terminal->Groups->Duplicate();
  4002. }
  4003. if (CapableOwnerChanging)
  4004. {
  4005. UserList = Terminal->Users->Duplicate();
  4006. }
  4007. TRemoteDirectory * Files = Terminal->Files;
  4008. int Count = Files->Count;
  4009. if (Count > 100)
  4010. {
  4011. Count = 100;
  4012. }
  4013. for (int Index = 0; Index < Count; Index++)
  4014. {
  4015. TRemoteFile * File = Files->Files[Index];
  4016. if (CapableGroupChanging)
  4017. {
  4018. GroupList->AddUnique(File->Group);
  4019. }
  4020. if (CapableOwnerChanging)
  4021. {
  4022. UserList->AddUnique(File->Owner);
  4023. }
  4024. }
  4025. // if we haven't collected tokens for all files in current directory,
  4026. // make sure we collect them at least for all selected files.
  4027. // (note that so far the files in FileList has to be from current direcotry)
  4028. if (Count < Files->Count)
  4029. {
  4030. for (int Index = 0; Index < FileList->Count; Index++)
  4031. {
  4032. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4033. if (CapableGroupChanging)
  4034. {
  4035. GroupList->AddUnique(File->Group);
  4036. }
  4037. if (CapableOwnerChanging)
  4038. {
  4039. UserList->AddUnique(File->Owner);
  4040. }
  4041. }
  4042. }
  4043. }
  4044. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4045. int Flags = 0;
  4046. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4047. if (CapableOwnerChanging) Flags |= cpOwner;
  4048. if (CapableGroupChanging) Flags |= cpGroup;
  4049. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4050. if (Terminal->IsCapable[fcCalculatingChecksum])
  4051. {
  4052. CalculateChecksumEvent = CalculateChecksum;
  4053. }
  4054. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4055. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4056. TRemoteProperties NewProperties = CurrentProperties;
  4057. Result =
  4058. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4059. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4060. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4061. CalculateSize, CalculateChecksumEvent);
  4062. if (Result)
  4063. {
  4064. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4065. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4066. }
  4067. }
  4068. __finally
  4069. {
  4070. delete GroupList;
  4071. delete UserList;
  4072. }
  4073. }
  4074. else
  4075. {
  4076. DirView(Side)->DisplayPropertiesMenu();
  4077. Result = true;
  4078. }
  4079. return Result;
  4080. }
  4081. //---------------------------------------------------------------------------
  4082. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4083. {
  4084. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4085. {
  4086. FIgnoreNextDialogChar = Key;
  4087. }
  4088. Key = 0;
  4089. }
  4090. //---------------------------------------------------------------------------
  4091. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4092. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4093. {
  4094. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4095. if (Command != NULL)
  4096. {
  4097. KeyProcessed(Key, Shift);
  4098. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4099. {
  4100. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4101. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4102. false, false, const_cast<TCustomCommandType *>(Command));
  4103. }
  4104. }
  4105. }
  4106. //---------------------------------------------------------------------------
  4107. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4108. {
  4109. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4110. {
  4111. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4112. }
  4113. if (!DirView(osCurrent)->IsEditing())
  4114. {
  4115. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4116. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4117. {
  4118. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4119. if (((Action->ShortCut == KeyShortCut) ||
  4120. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4121. AllowedAction(Action, aaShortCut))
  4122. {
  4123. // Has to be called before the action as the dialog char is already in queue.
  4124. // So when the action consumes message queue, we already need to have the
  4125. // FIgnoreNextDialogChar set
  4126. KeyProcessed(Key, Shift);
  4127. // Reset reference to previous component (when menu/toolbar was clicked).
  4128. // Needed to detect that action was invoked by keyboard shortcut
  4129. // in TNonVisualDataModule::ExplorerActionsExecute
  4130. Action->ActionComponent = NULL;
  4131. Action->Execute();
  4132. return;
  4133. }
  4134. }
  4135. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4136. {
  4137. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4138. {
  4139. KeyProcessed(Key, Shift);
  4140. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4141. return;
  4142. }
  4143. }
  4144. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4145. {
  4146. KeyProcessed(Key, Shift);
  4147. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4148. }
  4149. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4150. if (IsCustomShortCut(CustomShortCut))
  4151. {
  4152. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4153. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4154. if (WinConfiguration->SharedBookmarks != NULL)
  4155. {
  4156. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4157. if ((Bookmark != NULL) &&
  4158. OpenBookmark(FCurrentSide, Bookmark))
  4159. {
  4160. KeyProcessed(Key, Shift);
  4161. }
  4162. }
  4163. }
  4164. }
  4165. TForm::KeyDown(Key, Shift);
  4166. }
  4167. //---------------------------------------------------------------------------
  4168. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4169. {
  4170. DebugAssert(Terminal != NULL);
  4171. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4172. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4173. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4174. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4175. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4176. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4177. // expanded from ?: to avoid memory leaks
  4178. if (SecurityEnabled)
  4179. {
  4180. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4181. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4182. }
  4183. else
  4184. {
  4185. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4186. }
  4187. if (FileSystemInfo.ProtocolName.IsEmpty())
  4188. {
  4189. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4190. }
  4191. else
  4192. {
  4193. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4194. }
  4195. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4196. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4197. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4198. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4199. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4200. UnicodeString CompressionStr = CSCompressionStr;
  4201. if (CSCompressionStr != SCCompressionStr)
  4202. {
  4203. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4204. }
  4205. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4206. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4207. UpdateStatusBar();
  4208. }
  4209. //---------------------------------------------------------------------------
  4210. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4211. {
  4212. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4213. DebugAssert(SessionStatusBar != NULL);
  4214. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4215. {
  4216. // note: (Terminal->Status < sshReady) currently never happens here,
  4217. // so STATUS_CONNECTING is never used
  4218. SessionStatusBar->SimplePanel = true;
  4219. SessionStatusBar->SimpleText = LoadStr(
  4220. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4221. }
  4222. else
  4223. {
  4224. DebugAssert(Terminal);
  4225. SessionStatusBar->SimplePanel = false;
  4226. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4227. if (!FNote.IsEmpty())
  4228. {
  4229. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4230. }
  4231. else
  4232. {
  4233. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4234. }
  4235. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4236. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4237. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4238. }
  4239. }
  4240. //---------------------------------------------------------------------------
  4241. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4242. {
  4243. Panel->Caption = L"";
  4244. }
  4245. //---------------------------------------------------------------------------
  4246. void __fastcall TCustomScpExplorerForm::Idle()
  4247. {
  4248. if (FShowing || FStandaloneEditing)
  4249. {
  4250. FEditorManager->Check();
  4251. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4252. // that needs to know if queue view is visible (and it may be closed after queue update)
  4253. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4254. }
  4255. if (FShowing)
  4256. {
  4257. if (!NonVisualDataModule->Busy &&
  4258. // Menu is opened or drag&drop is going on
  4259. (Mouse->Capture == NULL))
  4260. {
  4261. if (FRefreshRemoteDirectory)
  4262. {
  4263. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4264. {
  4265. Terminal->RefreshDirectory();
  4266. }
  4267. FRefreshRemoteDirectory = false;
  4268. }
  4269. if (FRefreshLocalDirectory)
  4270. {
  4271. ReloadLocalDirectory();
  4272. FRefreshLocalDirectory = false;
  4273. }
  4274. if (WinConfiguration->RefreshRemotePanel)
  4275. {
  4276. TManagedTerminal * ManagedTerminal =
  4277. dynamic_cast<TManagedTerminal *>(Terminal);
  4278. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4279. (Now() - ManagedTerminal->DirectoryLoaded >
  4280. WinConfiguration->RefreshRemotePanelInterval))
  4281. {
  4282. RemoteDirView->ReloadDirectory();
  4283. }
  4284. }
  4285. }
  4286. }
  4287. if (FShowing || FStandaloneEditing)
  4288. {
  4289. if (FQueueStatusInvalidated)
  4290. {
  4291. UpdateQueueStatus(false);
  4292. }
  4293. RefreshQueueItems();
  4294. }
  4295. if (FShowing)
  4296. {
  4297. UpdateStatusBar();
  4298. }
  4299. FIgnoreNextDialogChar = 0;
  4300. }
  4301. //---------------------------------------------------------------------------
  4302. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4303. {
  4304. try
  4305. {
  4306. FUserActionTimer->Enabled = false;
  4307. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4308. {
  4309. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4310. {
  4311. FPendingQueueActionItem->ProcessUserAction();
  4312. }
  4313. }
  4314. }
  4315. __finally
  4316. {
  4317. FPendingQueueActionItem = NULL;
  4318. }
  4319. }
  4320. //---------------------------------------------------------------------------
  4321. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4322. {
  4323. if (WinConfiguration->MinimizeToTray)
  4324. {
  4325. UpdateTrayIcon();
  4326. FTrayIcon->Visible = true;
  4327. if (Visible)
  4328. {
  4329. ShowWindow(Handle, SW_HIDE);
  4330. }
  4331. }
  4332. }
  4333. //---------------------------------------------------------------------------
  4334. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4335. {
  4336. // WORKAROUND
  4337. // When restoring maximized window from minimization,
  4338. // rarely some controls do not align properly.
  4339. // Two instances seen (both for Commander):
  4340. // - When restoring, window is temporarily narrower (not maximized),
  4341. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4342. // Once maximized already, top dock shinks back, but the session PageControl,
  4343. // do not align up, leaving space between TopDock and PageControl.
  4344. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4345. for (int Index = 0; Index < ControlCount; Index++)
  4346. {
  4347. RealignControl(Controls[Index]);
  4348. }
  4349. if (FTrayIcon->Visible)
  4350. {
  4351. FTrayIcon->Visible = false;
  4352. if (Visible)
  4353. {
  4354. ShowWindow(Handle, SW_SHOW);
  4355. }
  4356. }
  4357. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4358. {
  4359. FNeedSession = false;
  4360. NonVisualDataModule->StartBusy();
  4361. try
  4362. {
  4363. NeedSession(false);
  4364. }
  4365. __finally
  4366. {
  4367. NonVisualDataModule->EndBusy();
  4368. }
  4369. }
  4370. }
  4371. //---------------------------------------------------------------------------
  4372. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4373. {
  4374. FTrayIcon->Hint = GetMainForm()->Caption;
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4378. {
  4379. UpdateTrayIcon();
  4380. }
  4381. //---------------------------------------------------------------------------
  4382. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4383. {
  4384. // workaround
  4385. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4386. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4387. // But WM_ACTIVATEAPP message can be received even when
  4388. // the main window is minimized to the tray and internal editor window is focused
  4389. // (after another application was previously active)
  4390. if (::IsIconic(Handle))
  4391. {
  4392. if (!IsAppIconic())
  4393. {
  4394. SetAppIconic(true);
  4395. }
  4396. }
  4397. ::ApplicationRestore();
  4398. Application->BringToFront();
  4399. }
  4400. //---------------------------------------------------------------------------
  4401. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4402. {
  4403. RestoreApp();
  4404. }
  4405. //---------------------------------------------------------------------------
  4406. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4407. {
  4408. if (OpenInNewWindow())
  4409. {
  4410. // todo: Pass FromSite
  4411. ExecuteNewInstance(SessionUrl);
  4412. }
  4413. else
  4414. {
  4415. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4416. }
  4417. }
  4418. //---------------------------------------------------------------------------
  4419. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4420. {
  4421. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4422. // current working directories become defaults here, what is not right
  4423. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4424. StoredSessions->NewSession(SessionName, SessionData.get());
  4425. // modified only, explicit
  4426. StoredSessions->Save(false, true);
  4427. // encode session name because of slashes in hierarchical sessions
  4428. return EncodeUrlString(SessionName);
  4429. }
  4430. //---------------------------------------------------------------------------
  4431. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4432. {
  4433. if (OpenInNewWindow())
  4434. {
  4435. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4436. }
  4437. else
  4438. {
  4439. // current working directories become defaults here, what is not right
  4440. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4441. TTerminalManager * Manager = TTerminalManager::Instance();
  4442. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4443. Manager->ActiveTerminal = Terminal;
  4444. }
  4445. }
  4446. //---------------------------------------------------------------------------
  4447. void __fastcall TCustomScpExplorerForm::RenameSession()
  4448. {
  4449. UnicodeString Name = Terminal->SessionData->Name;
  4450. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4451. {
  4452. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4453. TSessionData::ValidateName(Name);
  4454. // This is inconsistent with how color (for example) is handled.
  4455. Terminal->SessionData->Name = Name;
  4456. UpdateControls();
  4457. // Add/Remove distinguishing paths from sessions of the same name.
  4458. DoTerminalListChanged();
  4459. }
  4460. }
  4461. //---------------------------------------------------------------------------
  4462. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4463. {
  4464. DebugAssert(Queue != NULL);
  4465. bool Result = Queue->IsEmpty;
  4466. if (!Result)
  4467. {
  4468. SetFocus();
  4469. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4470. }
  4471. return Result;
  4472. }
  4473. //---------------------------------------------------------------------------
  4474. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4475. {
  4476. return CanCloseQueue(FQueue);
  4477. }
  4478. //---------------------------------------------------------------------------
  4479. void __fastcall TCustomScpExplorerForm::CloseSession()
  4480. {
  4481. if (CanCloseQueue())
  4482. {
  4483. TTerminalManager::Instance()->FreeActiveTerminal();
  4484. }
  4485. }
  4486. //---------------------------------------------------------------------------
  4487. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4488. {
  4489. if (OpenInNewWindow())
  4490. {
  4491. // encode session name because of slashes in hierarchical sessions
  4492. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4493. }
  4494. else
  4495. {
  4496. TTerminalManager * Manager = TTerminalManager::Instance();
  4497. TTerminal * Terminal = Manager->NewTerminal(Data);
  4498. Manager->ActiveTerminal = Terminal;
  4499. }
  4500. }
  4501. //---------------------------------------------------------------------------
  4502. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4503. {
  4504. if (OpenInNewWindow())
  4505. {
  4506. ExecuteNewInstance(Name);
  4507. }
  4508. else
  4509. {
  4510. TTerminalManager * Manager = TTerminalManager::Instance();
  4511. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4512. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4513. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4514. }
  4515. }
  4516. //---------------------------------------------------------------------------
  4517. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4518. bool &CanClose)
  4519. {
  4520. if (Terminal != NULL)
  4521. {
  4522. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4523. {
  4524. unsigned int Result;
  4525. TMessageParams Params(mpNeverAskAgainCheck);
  4526. UnicodeString Message;
  4527. int Answers = qaOK | qaCancel;
  4528. if (TTerminalManager::Instance()->Count > 1)
  4529. {
  4530. if (!WinConfiguration->AutoSaveWorkspace)
  4531. {
  4532. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4533. Answers = qaYes | qaNo | qaCancel;
  4534. }
  4535. else
  4536. {
  4537. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4538. }
  4539. }
  4540. else
  4541. {
  4542. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4543. }
  4544. if (WinConfiguration->AutoSaveWorkspace)
  4545. {
  4546. Message =
  4547. FORMAT("%s\n\n%s", (Message,
  4548. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4549. }
  4550. SetFocus();
  4551. Result = MessageDialog(Message, qtConfirmation,
  4552. Answers, HELP_NONE, &Params);
  4553. if (Result == qaNeverAskAgain)
  4554. {
  4555. WinConfiguration->ConfirmClosingSession = false;
  4556. }
  4557. if (Result == qaNo)
  4558. {
  4559. CanClose = SaveWorkspace(true);
  4560. // note that the workspace will be saved redundatly again from FormClose
  4561. }
  4562. else
  4563. {
  4564. CanClose =
  4565. (Result == qaOK) ||
  4566. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4567. (Result == qaNeverAskAgain);
  4568. }
  4569. }
  4570. if (CanClose)
  4571. {
  4572. CanClose = CanCloseQueue();
  4573. }
  4574. }
  4575. if (CanClose)
  4576. {
  4577. CanClose =
  4578. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4579. FEditorManager->CloseExternalFilesWithoutProcess();
  4580. if (CanClose)
  4581. {
  4582. while (CanClose && (FLocalEditors->Count > 0))
  4583. {
  4584. int PrevCount = FLocalEditors->Count;
  4585. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4586. CanClose = (FLocalEditors->Count < PrevCount);
  4587. }
  4588. if (CanClose)
  4589. {
  4590. CanClose = FEditorManager->Empty(true);
  4591. if (!CanClose)
  4592. {
  4593. SetFocus();
  4594. CanClose =
  4595. (MessageDialog(
  4596. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4597. }
  4598. }
  4599. }
  4600. }
  4601. }
  4602. //---------------------------------------------------------------------------
  4603. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4604. {
  4605. TForm * Form = dynamic_cast<TForm *>(Sender);
  4606. DebugAssert(Form != NULL);
  4607. Form->Close();
  4608. }
  4609. //---------------------------------------------------------------------------
  4610. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4611. {
  4612. TForm * Form = dynamic_cast<TForm *>(Sender);
  4613. delete Form;
  4614. }
  4615. //---------------------------------------------------------------------------
  4616. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4617. {
  4618. while (FLocalEditors->Count > 0)
  4619. {
  4620. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4621. }
  4622. }
  4623. //---------------------------------------------------------------------------
  4624. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4625. TObject *Sender)
  4626. {
  4627. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4628. try
  4629. {
  4630. SetProperties(osRemote, FileList);
  4631. }
  4632. __finally
  4633. {
  4634. delete FileList;
  4635. }
  4636. }
  4637. //---------------------------------------------------------------------------
  4638. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4639. {
  4640. if (value)
  4641. {
  4642. if (Component == fcCustomCommandsBand)
  4643. {
  4644. UpdateCustomCommandsToolbar();
  4645. }
  4646. }
  4647. }
  4648. //---------------------------------------------------------------------------
  4649. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4650. {
  4651. TControl * Control = GetComponent(Component);
  4652. DebugAssert(Control);
  4653. bool Changed = (Control->Visible != value);
  4654. if (Changed)
  4655. {
  4656. ComponentShowing(Component, value);
  4657. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4658. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4659. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4660. if (value)
  4661. {
  4662. int RemainingHeight = Control->Parent->ClientHeight;
  4663. int RemainingWidth = Control->Parent->ClientWidth;
  4664. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4665. {
  4666. TControl * ChildControl = Control->Parent->Controls[i];
  4667. if (ChildControl->Visible)
  4668. {
  4669. switch (ChildControl->Align)
  4670. {
  4671. case alTop:
  4672. case alBottom:
  4673. RemainingHeight -= ChildControl->Height;
  4674. break;
  4675. case alLeft:
  4676. case alRight:
  4677. RemainingWidth -= ChildControl->Width;
  4678. break;
  4679. }
  4680. }
  4681. }
  4682. int Reserve = ScaleByTextHeight(this, 32);
  4683. // queue in explorer, trees in commander
  4684. if (Control->Height > RemainingHeight - Reserve)
  4685. {
  4686. Control->Height = RemainingHeight / 2;
  4687. }
  4688. if (Control->Width > RemainingWidth - Reserve)
  4689. {
  4690. Control->Width = RemainingWidth / 2;
  4691. }
  4692. }
  4693. Control->Visible = value;
  4694. if (WasFocused && Visible)
  4695. {
  4696. DirView(osCurrent)->SetFocus();
  4697. }
  4698. FixControlsPlacement();
  4699. }
  4700. }
  4701. //---------------------------------------------------------------------------
  4702. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4703. {
  4704. TControl * Control = GetComponent(Component);
  4705. if (Control == NULL)
  4706. {
  4707. return false;
  4708. }
  4709. else
  4710. {
  4711. return Control->Visible;
  4712. }
  4713. }
  4714. //---------------------------------------------------------------------------
  4715. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4716. {
  4717. bool Result;
  4718. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4719. {
  4720. Result = !IsUWP();
  4721. }
  4722. else
  4723. {
  4724. Result = true;
  4725. }
  4726. return Result;
  4727. }
  4728. //---------------------------------------------------------------------------
  4729. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4730. {
  4731. if (DirView(osOther)->ItemFocused != NULL)
  4732. {
  4733. DirView(osOther)->ItemFocused->MakeVisible(false);
  4734. }
  4735. QueueSplitter->Visible = QueuePanel->Visible;
  4736. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4737. }
  4738. //---------------------------------------------------------------------------
  4739. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4740. {
  4741. switch (Component) {
  4742. case fcStatusBar: return RemoteStatusBar;
  4743. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4744. case fcQueueView: return QueuePanel;
  4745. case fcQueueToolbar: return QueueDock;
  4746. case fcRemoteTree: return RemoteDrivePanel;
  4747. case fcSessionsTabs: return SessionsPageControl;
  4748. default: return NULL;
  4749. }
  4750. }
  4751. //---------------------------------------------------------------------------
  4752. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4753. TObject *Sender, TListColumn *Column, TPoint &Point)
  4754. {
  4755. DebugAssert(NonVisualDataModule && Column && Sender);
  4756. NonVisualDataModule->ListColumn = Column;
  4757. TPopupMenu * DirViewColumnMenu;
  4758. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4759. {
  4760. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4761. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4762. (Column->Index == uvName);
  4763. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4764. (Column->Index == uvSize);
  4765. }
  4766. else
  4767. {
  4768. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4769. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4770. (Column->Index == dvName);
  4771. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4772. (Column->Index == dvSize);
  4773. }
  4774. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4775. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4776. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4777. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4778. }
  4779. //---------------------------------------------------------------------------
  4780. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4781. TObject *Sender, TListItem *Item, bool &AllowExec)
  4782. {
  4783. DoDirViewExecFile(Sender, Item, AllowExec);
  4784. }
  4785. //---------------------------------------------------------------------------
  4786. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4787. TListItem * Item, bool & AllowExec)
  4788. {
  4789. DebugAssert(Sender && Item && Configuration);
  4790. DebugAssert(AllowExec);
  4791. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4792. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4793. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4794. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4795. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4796. // on files only (not directories)
  4797. // and only when symlinks are resolved (apply to remote panel only)
  4798. if (!ADirView->ItemIsDirectory(Item) &&
  4799. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4800. {
  4801. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4802. !FForceExecution &&
  4803. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4804. {
  4805. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4806. {
  4807. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4808. UnlockWindow();
  4809. try
  4810. {
  4811. ExecuteFileOperation(
  4812. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4813. }
  4814. __finally
  4815. {
  4816. LockWindow();
  4817. }
  4818. AllowExec = false;
  4819. }
  4820. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4821. {
  4822. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4823. {
  4824. ExecuteFile(Side, efDefaultEditor);
  4825. AllowExec = false;
  4826. }
  4827. }
  4828. else
  4829. {
  4830. DebugFail();
  4831. }
  4832. }
  4833. // if we have not done anything special, fall back to default behavior
  4834. if (AllowExec)
  4835. {
  4836. if (Remote && !WinConfiguration->DisableOpenEdit)
  4837. {
  4838. ExecuteFile(osRemote, efShell);
  4839. AllowExec = false;
  4840. }
  4841. }
  4842. }
  4843. }
  4844. //---------------------------------------------------------------------------
  4845. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4846. {
  4847. return ((Side == osRemote) || (Side == osCurrent));
  4848. }
  4849. //---------------------------------------------------------------------------
  4850. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4851. {
  4852. DebugFail();
  4853. }
  4854. //---------------------------------------------------------------------------
  4855. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4856. {
  4857. DebugFail();
  4858. }
  4859. //---------------------------------------------------------------------------
  4860. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4861. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4862. {
  4863. TSynchronizeParamType Params;
  4864. Params.LocalDirectory = LocalDirectory;
  4865. Params.RemoteDirectory = RemoteDirectory;
  4866. int UnusedParams =
  4867. (GUIConfiguration->SynchronizeParams &
  4868. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4869. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4870. Params.Options = GUIConfiguration->SynchronizeOptions;
  4871. bool SaveSettings = false;
  4872. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4873. &DoSynchronizeTooManyDirectories);
  4874. DebugAssert(FSynchronizeController == NULL);
  4875. FSynchronizeController = &Controller;
  4876. bool Result;
  4877. try
  4878. {
  4879. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4880. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4881. int Options =
  4882. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4883. DebugAssert(FOnFeedSynchronizeError == NULL);
  4884. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4885. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4886. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4887. if (Result)
  4888. {
  4889. if (SaveSettings)
  4890. {
  4891. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4892. GUIConfiguration->SynchronizeOptions = Params.Options;
  4893. }
  4894. else
  4895. {
  4896. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4897. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4898. FLAGSET(Params.Options, soSynchronize))
  4899. {
  4900. GUIConfiguration->SynchronizeOptions =
  4901. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4902. soSynchronize;
  4903. }
  4904. }
  4905. LocalDirectory = Params.LocalDirectory;
  4906. RemoteDirectory = Params.RemoteDirectory;
  4907. }
  4908. }
  4909. __finally
  4910. {
  4911. FSynchronizeController = NULL;
  4912. DebugAssert(FOnFeedSynchronizeError == NULL);
  4913. FOnFeedSynchronizeError = NULL;
  4914. }
  4915. return Result;
  4916. }
  4917. //---------------------------------------------------------------------------
  4918. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4919. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4920. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4921. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4922. TSynchronizeOptions * Options, bool Full)
  4923. {
  4924. // Keep-up-to-date mode
  4925. if (Terminal->Status == ssOpened)
  4926. {
  4927. try
  4928. {
  4929. int PParams = Params.Params;
  4930. if (!Full)
  4931. {
  4932. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4933. if (FLAGSET(Params.Options, soRecurse))
  4934. {
  4935. PParams |= TTerminal::spSubDirs;
  4936. }
  4937. }
  4938. else
  4939. {
  4940. // if keepuptodate is non-recursive,
  4941. // full sync before has to be non-recursive as well
  4942. if (FLAGCLEAR(Params.Options, soRecurse))
  4943. {
  4944. PParams |= TTerminal::spNoRecurse;
  4945. }
  4946. }
  4947. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4948. PParams, Checklist, Options);
  4949. }
  4950. catch (Exception & E)
  4951. {
  4952. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4953. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4954. {
  4955. // noop, already logged in MoreMessageDialog
  4956. }
  4957. else
  4958. {
  4959. ShowExtendedExceptionEx(Terminal, &E);
  4960. throw;
  4961. }
  4962. }
  4963. }
  4964. }
  4965. //---------------------------------------------------------------------------
  4966. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4967. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4968. const UnicodeString ErrorStr)
  4969. {
  4970. if (!Directory.IsEmpty())
  4971. {
  4972. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4973. }
  4974. else
  4975. {
  4976. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4977. }
  4978. }
  4979. //---------------------------------------------------------------------------
  4980. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4981. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4982. {
  4983. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4984. {
  4985. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4986. }
  4987. else
  4988. {
  4989. TMessageParams Params(mpNeverAskAgainCheck);
  4990. unsigned int Result = MessageDialog(
  4991. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4992. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4993. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4994. {
  4995. MaxDirectories *= 2;
  4996. if (Result == qaNeverAskAgain)
  4997. {
  4998. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4999. }
  5000. }
  5001. else
  5002. {
  5003. Abort();
  5004. }
  5005. }
  5006. }
  5007. //---------------------------------------------------------------------------
  5008. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5009. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5010. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5011. TSynchronizeOptions * Options)
  5012. {
  5013. // Keep-up-to-date mode
  5014. DebugAssert(!FAutoOperation);
  5015. void * BatchStorage;
  5016. BatchStart(BatchStorage);
  5017. FAutoOperation = true;
  5018. bool AnyOperation = false;
  5019. TDateTime StartTime = Now();
  5020. TSynchronizeChecklist * AChecklist = NULL;
  5021. try
  5022. {
  5023. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5024. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5025. {
  5026. FSynchronizeProgressForm->Start();
  5027. }
  5028. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5029. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5030. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5031. Options);
  5032. SAFE_DESTROY(FSynchronizeProgressForm);
  5033. AnyOperation = (AChecklist->CheckedCount > 0);
  5034. if (AnyOperation)
  5035. {
  5036. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5037. CreateProgressForm(&SynchronizeProgress);
  5038. try
  5039. {
  5040. Terminal->SynchronizeApply(
  5041. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5042. SynchronizeProcessedItem, NULL, NULL, NULL);
  5043. }
  5044. __finally
  5045. {
  5046. DestroyProgressForm();
  5047. }
  5048. }
  5049. }
  5050. __finally
  5051. {
  5052. if (Checklist == NULL)
  5053. {
  5054. delete AChecklist;
  5055. }
  5056. else
  5057. {
  5058. *Checklist = AChecklist;
  5059. }
  5060. FAutoOperation = false;
  5061. SAFE_DESTROY(FSynchronizeProgressForm);
  5062. BatchEnd(BatchStorage);
  5063. ReloadLocalDirectory();
  5064. if (AnyOperation)
  5065. {
  5066. OperationComplete(StartTime);
  5067. }
  5068. }
  5069. }
  5070. //---------------------------------------------------------------------------
  5071. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5072. {
  5073. // can be called from command line
  5074. return Visible &&
  5075. ((DirView(osOther)->SelCount > 0) ||
  5076. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5077. }
  5078. //---------------------------------------------------------------------------
  5079. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5080. {
  5081. LogSynchronizeEvent(Terminal, Message);
  5082. }
  5083. //---------------------------------------------------------------------------
  5084. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5085. int Params, TSynchronizeOptions & Options)
  5086. {
  5087. DebugAssert(!IsLocalBrowserMode());
  5088. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5089. {
  5090. Options.Filter = new TStringList();
  5091. Options.Filter->CaseSensitive = false;
  5092. Options.Filter->Duplicates = Types::dupAccept;
  5093. if (DirView(osRemote)->SelCount > 0)
  5094. {
  5095. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5096. }
  5097. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5098. {
  5099. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5100. }
  5101. Options.Filter->Sort();
  5102. }
  5103. }
  5104. //---------------------------------------------------------------------------
  5105. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5106. {
  5107. TCopyParamType Defaults;
  5108. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5109. ConfigStorage->AccessMode = smRead;
  5110. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5111. {
  5112. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5113. }
  5114. ConfigStorage.reset(NULL);
  5115. std::unique_ptr<TStringList> Options(new TStringList());
  5116. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5117. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5118. UnicodeString Result;
  5119. if (Options->Count > 0)
  5120. {
  5121. Result =
  5122. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5123. }
  5124. return Result;
  5125. }
  5126. //---------------------------------------------------------------------------
  5127. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5128. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5129. {
  5130. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5131. UnicodeString AdditionalParams =
  5132. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5133. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5134. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5135. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5136. SerializeCopyParamForCommandLine(CopyParams)));
  5137. ExecuteNewInstance(SessionName, AdditionalParams);
  5138. }
  5139. //---------------------------------------------------------------------------
  5140. struct TSynchronizeParams
  5141. {
  5142. UnicodeString LocalDirectory;
  5143. UnicodeString RemoteDirectory;
  5144. TSynchronizeMode Mode;
  5145. TSynchronizeChecklist * Checklist;
  5146. int Params;
  5147. TCopyParamType * CopyParam;
  5148. TDateTime CollectElapsed;
  5149. TDateTime * StartTime;
  5150. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5151. };
  5152. //---------------------------------------------------------------------------
  5153. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5154. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5155. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5156. {
  5157. DebugAssert(!FAutoOperation);
  5158. void * BatchStorage;
  5159. BatchStart(BatchStorage);
  5160. FAutoOperation = true;
  5161. TFileOperationStatistics Statistics;
  5162. TDateTime Start = Now();
  5163. try
  5164. {
  5165. Params.OnProcessedItem = OnProcessedItem;
  5166. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5167. CreateProgressForm(&SynchronizeProgress);
  5168. Terminal->SynchronizeApply(
  5169. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5170. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5171. &Statistics);
  5172. }
  5173. __finally
  5174. {
  5175. Params.OnProcessedItem = NULL;
  5176. FAutoOperation = false;
  5177. DestroyProgressForm();
  5178. BatchEnd(BatchStorage);
  5179. ReloadLocalDirectory();
  5180. }
  5181. if (WinConfiguration->SynchronizeSummary)
  5182. {
  5183. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5184. // The statistics should be 0 anyway in this case
  5185. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5186. {
  5187. Message += L"\n";
  5188. if (Statistics.FilesUploaded > 0)
  5189. {
  5190. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5191. }
  5192. if (Statistics.FilesDownloaded > 0)
  5193. {
  5194. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5195. }
  5196. if (Statistics.FilesDeletedLocal > 0)
  5197. {
  5198. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5199. }
  5200. if (Statistics.FilesDeletedRemote > 0)
  5201. {
  5202. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5203. }
  5204. }
  5205. TDateTime Elapsed = (Now() - Start);
  5206. Message +=
  5207. L"\n" +
  5208. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5209. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5210. TMessageParams Params(mpNeverAskAgainCheck);
  5211. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5212. if (Result == qaNeverAskAgain)
  5213. {
  5214. WinConfiguration->SynchronizeSummary = false;
  5215. }
  5216. }
  5217. }
  5218. //---------------------------------------------------------------------------
  5219. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5220. {
  5221. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5222. {
  5223. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5224. }
  5225. // Not set in keep-up-to-date mode - Synchronize() method
  5226. if (Token != NULL)
  5227. {
  5228. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5229. if (Params.OnProcessedItem != NULL)
  5230. {
  5231. Params.OnProcessedItem(NULL, ChecklistItem);
  5232. }
  5233. }
  5234. }
  5235. //---------------------------------------------------------------------------
  5236. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5237. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5238. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5239. {
  5240. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5241. *Params.StartTime = Now();
  5242. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5243. }
  5244. //---------------------------------------------------------------------------
  5245. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5246. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5247. {
  5248. // terminal can be already closed (e.g. dropped connection)
  5249. if (Terminal != NULL)
  5250. {
  5251. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5252. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5253. }
  5254. }
  5255. //---------------------------------------------------------------------------
  5256. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5257. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5258. {
  5259. DebugAssert(!IsLocalBrowserMode());
  5260. TAutoBatch AutoBatch(this);
  5261. TAutoFlag AutoOperationFlag(FAutoOperation);
  5262. if (Side == osRemote)
  5263. {
  5264. std::unique_ptr<TStrings> FileList(new TStringList());
  5265. FileList->AddObject(FileName, RemoteFile);
  5266. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5267. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5268. RemoteDirView->SaveSelection();
  5269. RemoteDirView->SaveSelectedNames();
  5270. try
  5271. {
  5272. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5273. }
  5274. catch(...)
  5275. {
  5276. RemoteDirView->DiscardSavedSelection();
  5277. throw;
  5278. }
  5279. RemoteDirView->RestoreSelection();
  5280. }
  5281. else if (DebugAlwaysTrue(Side == osLocal))
  5282. {
  5283. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5284. {
  5285. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5286. }
  5287. UnicodeString Directory = ExtractFileDir(FileName);
  5288. ReloadLocalDirectory(Directory);
  5289. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5290. if (!SamePaths(Directory, NewDirectory))
  5291. {
  5292. ReloadLocalDirectory(NewDirectory);
  5293. }
  5294. }
  5295. }
  5296. //---------------------------------------------------------------------------
  5297. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5298. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5299. const TCopyParamType * CopyParams)
  5300. {
  5301. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5302. UnicodeString AdditionalParams =
  5303. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5304. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5305. LocalDirectory, RemoteDirectory, Mode, Params,
  5306. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5307. SerializeCopyParamForCommandLine(CopyParams)));
  5308. ExecuteNewInstance(SessionName, AdditionalParams);
  5309. }
  5310. //---------------------------------------------------------------------------
  5311. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5312. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5313. TSynchronizeMode & Mode, int Params, bool & SaveMode, bool UseDefaults)
  5314. {
  5315. int Result;
  5316. bool SaveSettings = false;
  5317. int Options =
  5318. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5319. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5320. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5321. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5322. bool Continue =
  5323. UseDefaults ||
  5324. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5325. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow);
  5326. if (Continue)
  5327. {
  5328. Configuration->Usage->Inc(L"Synchronizations");
  5329. UpdateCopyParamCounters(CopyParam);
  5330. TSynchronizeOptions SynchronizeOptions;
  5331. GetSynchronizeOptions(Params, SynchronizeOptions);
  5332. if (SaveSettings)
  5333. {
  5334. GUIConfiguration->SynchronizeParams = Params;
  5335. }
  5336. else
  5337. {
  5338. SaveMode = false;
  5339. }
  5340. TDateTime StartTime = Now();
  5341. TSynchronizeChecklist * Checklist = NULL;
  5342. try
  5343. {
  5344. DebugAssert(!FAutoOperation);
  5345. FAutoOperation = true;
  5346. try
  5347. {
  5348. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5349. FSynchronizeProgressForm->Start();
  5350. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5351. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5352. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5353. &SynchronizeOptions);
  5354. }
  5355. __finally
  5356. {
  5357. FAutoOperation = false;
  5358. SAFE_DESTROY(FSynchronizeProgressForm);
  5359. }
  5360. TSynchronizeParams SynchronizeParams;
  5361. SynchronizeParams.LocalDirectory = LocalDirectory;
  5362. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5363. SynchronizeParams.Mode = Mode;
  5364. SynchronizeParams.CopyParam = &CopyParam;
  5365. SynchronizeParams.Params = Params;
  5366. SynchronizeParams.Checklist = Checklist;
  5367. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5368. SynchronizeParams.StartTime = &StartTime;
  5369. SynchronizeParams.OnProcessedItem = NULL;
  5370. Result = Checklist->Count;
  5371. if (Checklist->Count > 0)
  5372. {
  5373. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5374. {
  5375. if (!DoSynchronizeChecklistDialog(
  5376. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5377. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5378. {
  5379. Result = -1;
  5380. }
  5381. }
  5382. else
  5383. {
  5384. FullSynchronize(SynchronizeParams, NULL, NULL);
  5385. }
  5386. }
  5387. else
  5388. {
  5389. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5390. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5391. }
  5392. }
  5393. __finally
  5394. {
  5395. delete Checklist;
  5396. }
  5397. OperationComplete(StartTime);
  5398. }
  5399. else
  5400. {
  5401. Result = -1;
  5402. }
  5403. return Result;
  5404. }
  5405. //---------------------------------------------------------------------------
  5406. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5407. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5408. bool & Continue, bool Collect)
  5409. {
  5410. if (Collect)
  5411. {
  5412. DebugAssert(FSynchronizeProgressForm != NULL);
  5413. if (!FSynchronizeProgressForm->Started)
  5414. {
  5415. FSynchronizeProgressForm->Start();
  5416. }
  5417. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5418. }
  5419. else
  5420. {
  5421. DebugAssert(FSynchronizeProgressForm == NULL);
  5422. DebugAssert(FProgressForm != NULL);
  5423. }
  5424. }
  5425. //---------------------------------------------------------------------------
  5426. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5427. {
  5428. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5429. TRemoteFile * File = NULL;
  5430. Terminal->ReadFile(FullFileName, File);
  5431. if (File != NULL)
  5432. {
  5433. std::unique_ptr<TRemoteFile> FileOwner(File);
  5434. TAutoFlag Flag(FStandaloneEditing);
  5435. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5436. Application->ShowMainForm = false;
  5437. Application->Run();
  5438. }
  5439. }
  5440. //---------------------------------------------------------------------------
  5441. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5442. {
  5443. DebugFail();
  5444. }
  5445. //---------------------------------------------------------------------------
  5446. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5447. {
  5448. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5449. SessionData->Assign(Terminal->SessionData);
  5450. UpdateSessionData(SessionData.get());
  5451. Terminal->UpdateSessionCredentials(SessionData.get());
  5452. if (!Terminal->SessionData->HasSessionName())
  5453. {
  5454. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5455. // once IsWorkspace is cleared
  5456. SessionData->Name = UnicodeString();
  5457. SessionData->IsWorkspace = false;
  5458. }
  5459. return SessionData.release();
  5460. }
  5461. //---------------------------------------------------------------------------
  5462. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5463. {
  5464. TSessionData * SessionData = CloneCurrentSessionData();
  5465. try
  5466. {
  5467. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5468. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5469. }
  5470. __finally
  5471. {
  5472. delete SessionData;
  5473. }
  5474. }
  5475. //---------------------------------------------------------------------------
  5476. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5477. {
  5478. TTerminalManager * Manager = TTerminalManager::Instance();
  5479. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5480. if (DebugAlwaysTrue(Terminal != NULL))
  5481. {
  5482. // Update (Managed)Terminal->StateData
  5483. UpdateTerminal(Terminal);
  5484. }
  5485. Manager->SaveWorkspace(DataList.get());
  5486. return DataList.release();
  5487. }
  5488. //---------------------------------------------------------------------------
  5489. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5490. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5491. {
  5492. WinConfiguration->LastWorkspace = Name;
  5493. if (!SavePasswords)
  5494. {
  5495. for (int Index = 0; Index < DataList->Count; Index++)
  5496. {
  5497. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5498. if (SessionData->Link.IsEmpty())
  5499. {
  5500. SessionData->ClearSessionPasswords();
  5501. }
  5502. }
  5503. }
  5504. StoredSessions->NewWorkspace(Name, DataList);
  5505. // modified only
  5506. StoredSessions->Save(false, Explicit);
  5507. }
  5508. //---------------------------------------------------------------------------
  5509. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5510. {
  5511. return
  5512. DefaultStr(WinConfiguration->LastWorkspace,
  5513. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5514. }
  5515. //---------------------------------------------------------------------------
  5516. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5517. {
  5518. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5519. bool AnyNonStoredSessionWithPassword = false;
  5520. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5521. bool AllNonStoredSessionsAnonymous = true;
  5522. TTerminalManager * Manager = TTerminalManager::Instance();
  5523. for (int Index = 0; Index < DataList->Count; Index++)
  5524. {
  5525. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5526. if (SessionData->Link.IsEmpty())
  5527. {
  5528. if (SessionData->HasAnySessionPassword())
  5529. {
  5530. AnyNonStoredSessionWithPassword = true;
  5531. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5532. {
  5533. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5534. }
  5535. }
  5536. if (!SameText(SessionData->UserName, AnonymousUserName))
  5537. {
  5538. AllNonStoredSessionsAnonymous = false;
  5539. }
  5540. }
  5541. }
  5542. bool SavePasswords;
  5543. bool * PSavePasswords;
  5544. bool NotRecommendedSavingPasswords =
  5545. !CustomWinConfiguration->UseMasterPassword &&
  5546. !AllNonStoredSessionsAnonymous;
  5547. if (Configuration->DisablePasswordStoring ||
  5548. !AnyNonStoredSessionWithPassword)
  5549. {
  5550. PSavePasswords = NULL;
  5551. SavePasswords = false;
  5552. }
  5553. else
  5554. {
  5555. PSavePasswords = &SavePasswords;
  5556. SavePasswords =
  5557. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5558. !NotRecommendedSavingPasswords;
  5559. }
  5560. UnicodeString Name = WorkspaceName();
  5561. bool CreateShortcut = false;
  5562. bool Result =
  5563. DoSaveWorkspaceDialog(
  5564. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5565. EnableAutoSave);
  5566. if (Result)
  5567. {
  5568. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5569. if (CreateShortcut)
  5570. {
  5571. TOperationVisualizer Visualizer;
  5572. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5573. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5574. }
  5575. if (EnableAutoSave)
  5576. {
  5577. WinConfiguration->AutoSaveWorkspace = true;
  5578. WinConfiguration->AutoWorkspace = Name;
  5579. if (PSavePasswords != NULL)
  5580. {
  5581. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5582. }
  5583. }
  5584. }
  5585. return Result;
  5586. }
  5587. //---------------------------------------------------------------------------
  5588. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5589. {
  5590. DebugAssert(!IsLocalBrowserMode());
  5591. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5592. DebugAssert(ManagedTerminal != NULL);
  5593. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5594. if (WinConfiguration->PreservePanelState)
  5595. {
  5596. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5597. }
  5598. UpdateSessionData(ManagedTerminal->StateData);
  5599. }
  5600. //---------------------------------------------------------------------------
  5601. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5602. {
  5603. // Keep in sync with TSessionData::CopyStateData
  5604. DebugAssert(!IsLocalBrowserMode());
  5605. DebugAssert(Data != NULL);
  5606. // This is inconsistent with how color (for example) is handled.
  5607. Data->Name = Terminal->SessionData->Name;
  5608. // cannot use RemoteDirView->Path, because it is empty if connection
  5609. // was already closed
  5610. // also only peek, we may not be connected at all atm
  5611. // so make sure we do not try retrieving current directory from the server
  5612. // (particularly with FTP)
  5613. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5614. if (!ACurrentDirectory.IsEmpty())
  5615. {
  5616. Data->RemoteDirectory = ACurrentDirectory;
  5617. }
  5618. Data->Color = SessionColor;
  5619. }
  5620. //---------------------------------------------------------------------------
  5621. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5622. {
  5623. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5624. DebugAssert(Toolbar != NULL);
  5625. for (int i = 0; i < Toolbar->Items->Count; i++)
  5626. {
  5627. TTBXCustomDropDownItem * DropDownItem;
  5628. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5629. if (DropDownItem != NULL)
  5630. {
  5631. ToolbarItemResize(DropDownItem,
  5632. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5633. Toolbar->NonClientWidth);
  5634. break;
  5635. }
  5636. }
  5637. }
  5638. //---------------------------------------------------------------------------
  5639. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5640. {
  5641. Item->EditWidth = Width;
  5642. }
  5643. //---------------------------------------------------------------------------
  5644. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5645. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5646. {
  5647. for (int i = 0; i < Toolbar->Items->Count; i++)
  5648. {
  5649. TTBXCustomDropDownItem * DropDownItem;
  5650. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5651. if (DropDownItem != NULL)
  5652. {
  5653. ASize.x -= DropDownItem->EditWidth;
  5654. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5655. }
  5656. }
  5657. }
  5658. //---------------------------------------------------------------------------
  5659. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5660. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5661. {
  5662. if (WinConfiguration->DDWarnLackOfTempSpace)
  5663. {
  5664. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5665. if (!ADrive.IsEmpty())
  5666. {
  5667. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5668. DebugAssert(RequiredSpace >= 0);
  5669. __int64 RequiredWithReserve;
  5670. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5671. if (FreeSpace < RequiredWithReserve)
  5672. {
  5673. unsigned int Result;
  5674. TMessageParams Params(mpNeverAskAgainCheck);
  5675. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5676. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5677. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5678. if (Result == qaNeverAskAgain)
  5679. {
  5680. WinConfiguration->DDWarnLackOfTempSpace = false;
  5681. }
  5682. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5683. }
  5684. }
  5685. }
  5686. }
  5687. //---------------------------------------------------------------------------
  5688. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5689. {
  5690. DoOpenDirectoryDialog(odAddBookmark, Side);
  5691. }
  5692. //---------------------------------------------------------------------------
  5693. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5694. {
  5695. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5696. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5697. try
  5698. {
  5699. TCustomDirView * DView = DirView(Side);
  5700. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5701. {
  5702. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5703. }
  5704. }
  5705. catch (...)
  5706. {
  5707. delete VisitedDirectories;
  5708. throw;
  5709. }
  5710. return VisitedDirectories;
  5711. }
  5712. //---------------------------------------------------------------------------
  5713. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5714. TOpenDirectoryMode Mode, TOperationSide Side)
  5715. {
  5716. bool Continue = true;
  5717. if (Mode == odAddBookmark)
  5718. {
  5719. TMessageParams Params(mpNeverAskAgainCheck);
  5720. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5721. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5722. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5723. unsigned int Answer =
  5724. MessageDialog(Message,
  5725. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5726. if (Answer == qaNeverAskAgain)
  5727. {
  5728. Continue = true;
  5729. WinConfiguration->UseSharedBookmarks = true;
  5730. }
  5731. else if (Answer == qaOK)
  5732. {
  5733. Continue = true;
  5734. WinConfiguration->UseSharedBookmarks = false;
  5735. }
  5736. else
  5737. {
  5738. Continue = false;
  5739. }
  5740. }
  5741. if (Continue)
  5742. {
  5743. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5744. try
  5745. {
  5746. UnicodeString Name = DirView(Side)->PathName;
  5747. bool Repeat;
  5748. do
  5749. {
  5750. Repeat = false;
  5751. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5752. // do not allow switching to location profiles,
  5753. // if we are not connected
  5754. HasDirView[osLocal] && (Terminal != NULL)))
  5755. {
  5756. TWindowLock Lock(this);
  5757. if (!TryOpenDirectory(Side, Name))
  5758. {
  5759. Repeat = true;
  5760. Mode = odBrowse;
  5761. }
  5762. }
  5763. }
  5764. while (Repeat);
  5765. }
  5766. __finally
  5767. {
  5768. delete VisitedDirectories;
  5769. }
  5770. }
  5771. }
  5772. //---------------------------------------------------------------------------
  5773. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5774. {
  5775. DebugAssert(!FTerminal->CommandSessionOpened);
  5776. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5777. }
  5778. //---------------------------------------------------------------------------
  5779. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5780. {
  5781. bool Result = FTerminal->IsCapable[Capability] ||
  5782. FTerminal->CommandSessionOpened;
  5783. if (!Result)
  5784. {
  5785. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5786. if (!GUIConfiguration->ConfirmCommandSession)
  5787. {
  5788. Result = true;
  5789. }
  5790. else
  5791. {
  5792. TMessageParams Params(mpNeverAskAgainCheck);
  5793. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5794. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5795. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5796. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5797. if (Answer == qaNeverAskAgain)
  5798. {
  5799. GUIConfiguration->ConfirmCommandSession = false;
  5800. Result = true;
  5801. }
  5802. else if (Answer == qaOK)
  5803. {
  5804. Result = true;
  5805. }
  5806. }
  5807. if (Result)
  5808. {
  5809. Result = CommandSessionFallback();
  5810. }
  5811. }
  5812. return Result;
  5813. }
  5814. //---------------------------------------------------------------------------
  5815. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5816. {
  5817. if (EnsureCommandSessionFallback(fcAnyCommand))
  5818. {
  5819. DoConsoleDialog(Terminal, Command);
  5820. }
  5821. }
  5822. //---------------------------------------------------------------------------
  5823. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5824. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5825. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5826. {
  5827. if (!IsSideLocalBrowser(osRemote) &&
  5828. IsFileControl(DropSourceControl, osRemote) &&
  5829. (FDDExtMapFile != NULL))
  5830. {
  5831. Accept = true;
  5832. }
  5833. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5834. DebugAssert(FDDTargetControl != NULL);
  5835. }
  5836. //---------------------------------------------------------------------------
  5837. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5838. _di_IDataObject DataObj, int KeyState,
  5839. const TPoint & Point, int & Effect, bool & Accept)
  5840. {
  5841. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5842. }
  5843. //---------------------------------------------------------------------------
  5844. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5845. _di_IDataObject DataObj, int KeyState,
  5846. const TPoint & Point, int & Effect, bool & Accept)
  5847. {
  5848. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5849. }
  5850. //---------------------------------------------------------------------------
  5851. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5852. TObject * Sender)
  5853. {
  5854. DebugUsedParam(Sender);
  5855. DebugAssert(FDDTargetControl == Sender);
  5856. FDDTargetControl = NULL;
  5857. }
  5858. //---------------------------------------------------------------------------
  5859. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5860. {
  5861. FileControlDDDragLeave(SessionsPageControl);
  5862. }
  5863. //---------------------------------------------------------------------------
  5864. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5865. {
  5866. FileControlDDDragLeave(QueueView3);
  5867. }
  5868. //---------------------------------------------------------------------------
  5869. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5870. {
  5871. DebugAssert(!IsSideLocalBrowser(Side));
  5872. DebugUsedParam(Side);
  5873. bool Edit = false;
  5874. TRemoteFile * File = NULL;
  5875. UnicodeString FileName;
  5876. UnicodeString PointTo;
  5877. bool SymbolicLink = true;
  5878. if (RemoteDirView->ItemFocused)
  5879. {
  5880. DebugAssert(RemoteDirView->ItemFocused->Data);
  5881. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5882. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5883. if (Edit)
  5884. {
  5885. FileName = File->FileName;
  5886. PointTo = File->LinkTo;
  5887. }
  5888. else
  5889. {
  5890. PointTo = File->FileName;
  5891. }
  5892. }
  5893. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5894. Terminal->IsCapable[fcHardLink]))
  5895. {
  5896. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5897. if (Edit)
  5898. {
  5899. DebugAssert(File->FileName == FileName);
  5900. int Params = dfNoRecursive;
  5901. Terminal->ExceptionOnFail = true;
  5902. try
  5903. {
  5904. Terminal->DeleteFile(L"", File, &Params);
  5905. }
  5906. __finally
  5907. {
  5908. Terminal->ExceptionOnFail = false;
  5909. }
  5910. }
  5911. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5912. }
  5913. }
  5914. //---------------------------------------------------------------------------
  5915. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5916. {
  5917. return
  5918. (IsSideLocalBrowser(Side) ||
  5919. ((Terminal != NULL) &&
  5920. Terminal->ResolvingSymlinks &&
  5921. Terminal->IsCapable[fcSymbolicLink]));
  5922. }
  5923. //---------------------------------------------------------------------------
  5924. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5925. {
  5926. return
  5927. !IsSideLocalBrowser(FCurrentSide) &&
  5928. (RemoteDirView->ItemFocused != NULL) &&
  5929. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5930. Terminal->SessionData->ResolveSymlinks;
  5931. }
  5932. //---------------------------------------------------------------------------
  5933. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5934. {
  5935. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5936. FForceExecution = true;
  5937. try
  5938. {
  5939. DirView(osCurrent)->ExecuteCurrentFile();
  5940. }
  5941. __finally
  5942. {
  5943. FForceExecution = false;
  5944. }
  5945. }
  5946. //---------------------------------------------------------------------------
  5947. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5948. {
  5949. TEditorData ExternalEditor;
  5950. ExternalEditor.Editor = edExternal;
  5951. bool Remember = false;
  5952. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  5953. {
  5954. if (Remember)
  5955. {
  5956. TEditorList * EditorList = new TEditorList;
  5957. try
  5958. {
  5959. *EditorList = *WinConfiguration->EditorList;
  5960. bool Found = false;
  5961. int i = 0;
  5962. while (!Found && (i < EditorList->Count))
  5963. {
  5964. const TEditorPreferences * Editor = EditorList->Editors[i];
  5965. if ((Editor->Data->Editor == edExternal) &&
  5966. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5967. {
  5968. Found = true;
  5969. }
  5970. i++;
  5971. }
  5972. if (!Found)
  5973. {
  5974. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5975. WinConfiguration->EditorList = EditorList;
  5976. }
  5977. }
  5978. __finally
  5979. {
  5980. delete EditorList;
  5981. }
  5982. }
  5983. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5984. }
  5985. }
  5986. //---------------------------------------------------------------------------
  5987. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5988. {
  5989. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5990. if (FFileFindTerminal == Sender)
  5991. {
  5992. FFileFindTerminal = NULL;
  5993. HideFileFindDialog();
  5994. }
  5995. if (FClipboardTerminal == Sender)
  5996. {
  5997. ClipboardClear(); // implies ClipboardStop
  5998. }
  5999. }
  6000. //---------------------------------------------------------------------------
  6001. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6002. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6003. {
  6004. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6005. DebugAssert(Terminal != NULL);
  6006. if (Data->Terminal == Terminal)
  6007. {
  6008. Data->Terminal = NULL;
  6009. }
  6010. }
  6011. //---------------------------------------------------------------------------
  6012. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6013. {
  6014. UpdateControls();
  6015. SessionColor = TColor(0);
  6016. UpdateRemotePathComboBox(false);
  6017. NeedSession(true);
  6018. }
  6019. //---------------------------------------------------------------------------
  6020. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  6021. {
  6022. try
  6023. {
  6024. try
  6025. {
  6026. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6027. }
  6028. __finally
  6029. {
  6030. if (!WinConfiguration->KeepOpenWhenNoSession &&
  6031. (!Terminal || !Terminal->Active))
  6032. {
  6033. TerminateApplication();
  6034. }
  6035. }
  6036. }
  6037. catch (EAbort &)
  6038. {
  6039. // swallow
  6040. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6041. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6042. }
  6043. }
  6044. //---------------------------------------------------------------------------
  6045. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6046. {
  6047. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6048. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6049. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6050. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6051. try
  6052. {
  6053. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6054. // Clear the design time unthemed tab
  6055. ((SessionsPageControl->PageCount > 0) &&
  6056. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6057. {
  6058. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6059. }
  6060. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6061. {
  6062. TThemeTabSheet * TabSheet;
  6063. if (Index >= SessionsPageControl->PageCount)
  6064. {
  6065. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6066. TabSheet->PageControl = SessionsPageControl;
  6067. }
  6068. else
  6069. {
  6070. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6071. }
  6072. bool IsSessionTab = (Index < TerminalList->Count);
  6073. if (IsSessionTab)
  6074. {
  6075. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6076. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6077. UpdateSessionTab(TabSheet);
  6078. }
  6079. else
  6080. {
  6081. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6082. TabSheet->Tag = 0; // not really needed
  6083. // 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)
  6084. UpdateNewSessionTab();
  6085. }
  6086. TabSheet->ShowCloseButton = IsSessionTab;
  6087. }
  6088. }
  6089. __finally
  6090. {
  6091. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6092. }
  6093. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6094. }
  6095. //---------------------------------------------------------------------------
  6096. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6097. {
  6098. DoTerminalListChanged();
  6099. }
  6100. //---------------------------------------------------------------------------
  6101. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6102. {
  6103. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6104. DebugAssert(TabSheet->ImageIndex == 0);
  6105. TabSheet->Caption =
  6106. WinConfiguration->SelectiveToolbarText ?
  6107. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6108. UnicodeString();
  6109. }
  6110. //---------------------------------------------------------------------------
  6111. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6112. {
  6113. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  6114. }
  6115. //---------------------------------------------------------------------------
  6116. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6117. {
  6118. if (DebugAlwaysTrue(TabSheet != NULL))
  6119. {
  6120. TManagedTerminal * ManagedTerminal =
  6121. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6122. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6123. {
  6124. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6125. TabSheet->ImageIndex = AddSessionColor(Color);
  6126. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6127. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6128. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6129. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6130. {
  6131. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6132. }
  6133. }
  6134. }
  6135. }
  6136. //---------------------------------------------------------------------------
  6137. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6138. {
  6139. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6140. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6141. bool Result = (Terminal != NULL);
  6142. if (Result)
  6143. {
  6144. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6145. }
  6146. else
  6147. {
  6148. try
  6149. {
  6150. NewSession(false);
  6151. }
  6152. __finally
  6153. {
  6154. DoTerminalListChanged();
  6155. }
  6156. FSessionsPageControlNewSessionTime = Now();
  6157. }
  6158. return Result;
  6159. }
  6160. //---------------------------------------------------------------------------
  6161. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6162. {
  6163. SessionTabSwitched();
  6164. }
  6165. //---------------------------------------------------------------------------
  6166. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6167. {
  6168. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6169. DebugAssert(TransferList != NULL);
  6170. UnicodeString Name;
  6171. if (TransferList->ItemIndex <= 0)
  6172. {
  6173. Name = L"";
  6174. }
  6175. else
  6176. {
  6177. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6178. }
  6179. if (FCopyParamAutoSelected.IsEmpty())
  6180. {
  6181. // if previous preset was not autoselected, make new preset the "default"
  6182. FCopyParamDefault = Name;
  6183. }
  6184. GUIConfiguration->CopyParamCurrent = Name;
  6185. }
  6186. //---------------------------------------------------------------------------
  6187. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6188. {
  6189. // sanity check
  6190. bool ExistingPreset =
  6191. (FTransferListHoverIndex >= 0) &&
  6192. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6193. DebugAssert(ExistingPreset);
  6194. if (ExistingPreset)
  6195. {
  6196. HDC DC = GetDC(0);
  6197. TCanvas * Canvas = new TCanvas();
  6198. try
  6199. {
  6200. Canvas->Handle = DC;
  6201. Canvas->Font = GetToolbarFont(this);
  6202. UnicodeString Name;
  6203. if (FTransferListHoverIndex == 0)
  6204. {
  6205. Name = L"";
  6206. }
  6207. else
  6208. {
  6209. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6210. }
  6211. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6212. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6213. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6214. static_cast<TObject*>(GetComponent(fcTransferList)));
  6215. UnicodeString InfoStr =
  6216. GUIConfiguration->CopyParamPreset[Name].
  6217. GetInfoStr(L"; ",
  6218. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6219. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6220. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6221. {
  6222. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6223. {
  6224. InfoStr.SetLength(InfoStr.Length() - 1);
  6225. }
  6226. InfoStr += Ellipsis;
  6227. }
  6228. // UpdateCaption does not cause invalidation of whole submenu, while
  6229. // setting Caption property does.
  6230. // also it probably does not resize the label, even if necessary
  6231. // (we do not want that anyway)
  6232. TransferLabel->UpdateCaption(InfoStr);
  6233. }
  6234. __finally
  6235. {
  6236. Canvas->Handle = NULL;
  6237. ReleaseDC(0, DC);
  6238. delete Canvas;
  6239. }
  6240. }
  6241. }
  6242. //---------------------------------------------------------------------------
  6243. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6244. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6245. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6246. {
  6247. if (FTransferListHoverIndex != AHoverIndex)
  6248. {
  6249. FTransferListHoverIndex = AHoverIndex;
  6250. UpdateTransferLabel();
  6251. }
  6252. }
  6253. //---------------------------------------------------------------------------
  6254. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6255. {
  6256. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6257. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6258. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6259. {
  6260. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6261. {
  6262. OpenDirectory(GetSide(osCurrent));
  6263. Message.Result = 1;
  6264. }
  6265. else
  6266. {
  6267. TForm::Dispatch(&Message);
  6268. }
  6269. }
  6270. else
  6271. {
  6272. TForm::Dispatch(&Message);
  6273. }
  6274. }
  6275. //---------------------------------------------------------------------------
  6276. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6277. {
  6278. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6279. if ((FIgnoreNextDialogChar != 0) &&
  6280. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6281. {
  6282. Message.Result = 1;
  6283. }
  6284. else
  6285. {
  6286. TForm::Dispatch(&Message);
  6287. }
  6288. FIgnoreNextDialogChar = 0;
  6289. }
  6290. //---------------------------------------------------------------------------
  6291. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6292. {
  6293. // The four low-order bits are used internally by Windows
  6294. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6295. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6296. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6297. // 61730 - restore thru double click - undocumented
  6298. // 61490 - maximize thru double click - undocumented
  6299. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6300. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6301. {
  6302. SysResizing(Cmd);
  6303. }
  6304. TForm::Dispatch(&Message);
  6305. }
  6306. //---------------------------------------------------------------------------
  6307. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6308. {
  6309. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6310. // Also there no point returning TRUE as we are not able to
  6311. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6312. // Hence the process termination might be safer :)
  6313. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6314. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6315. {
  6316. Message.Result = FALSE;
  6317. }
  6318. else
  6319. {
  6320. Message.Result = TRUE;
  6321. }
  6322. // Do not call default handling as that triggers OnCloseQuery,
  6323. // where our implementation will popup confirmation dialogs, what we do not want,
  6324. // as per Vista guidelines:
  6325. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6326. }
  6327. //---------------------------------------------------------------------------
  6328. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6329. {
  6330. if (Message.EndSession && IsApplicationMinimized())
  6331. {
  6332. // WORKAROUND
  6333. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6334. // when it receives WM_ENDSESSION,
  6335. // but that sometimes (particularly when application is minimized) cause crashes.
  6336. // Crash popups message that blocks log off.
  6337. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6338. // so we call ExitProcess() immediately, not even trying to cleanup.
  6339. // It still causes beep, so there's likely some popup, but it does not block
  6340. // log off.
  6341. ExitProcess(0);
  6342. }
  6343. TForm::Dispatch(&Message);
  6344. }
  6345. #ifdef _DEBUG
  6346. //---------------------------------------------------------------------------
  6347. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6348. {
  6349. TForm::Dispatch(&Message);
  6350. }
  6351. #endif
  6352. //---------------------------------------------------------------------------
  6353. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6354. {
  6355. }
  6356. //---------------------------------------------------------------------------
  6357. void __fastcall TCustomScpExplorerForm::DoShow()
  6358. {
  6359. // only now are the controls resized finally, so the size constraints
  6360. // will not conflict with possibly very small window size
  6361. RestoreFormParams();
  6362. FixControlsPlacement();
  6363. if (Position == poDefaultPosOnly)
  6364. {
  6365. CutFormToDesktop(this);
  6366. }
  6367. TForm::DoShow();
  6368. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6369. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6370. if (Terminal == NULL)
  6371. {
  6372. StartingDisconnected();
  6373. }
  6374. UpdatePixelsPerInchMainWindowCounter();
  6375. FShowing = true;
  6376. }
  6377. //---------------------------------------------------------------------------
  6378. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6379. {
  6380. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6381. }
  6382. //---------------------------------------------------------------------------
  6383. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6384. {
  6385. DoTerminalListChanged();
  6386. }
  6387. //---------------------------------------------------------------------------
  6388. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6389. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6390. TNotifyEvent OnBalloonClick, TObject * UserData)
  6391. {
  6392. bool Do;
  6393. UnicodeString Message;
  6394. if (E == NULL)
  6395. {
  6396. Message = Str;
  6397. Do = true;
  6398. }
  6399. else
  6400. {
  6401. Do = ExceptionMessage(E, Message);
  6402. }
  6403. if (Do && WinConfiguration->BalloonNotifications)
  6404. {
  6405. UnicodeString Title;
  6406. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6407. {
  6408. Message = TrimLeft(Message);
  6409. }
  6410. else
  6411. {
  6412. Message = UnformatMessage(Message);
  6413. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6414. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6415. Title = LoadResourceString(Captions[Type]);
  6416. if (Terminal != NULL)
  6417. {
  6418. Title = FORMAT(L"%s - %s",
  6419. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6420. }
  6421. }
  6422. if (Seconds == 0)
  6423. {
  6424. Seconds = WinConfiguration->NotificationsTimeout;
  6425. }
  6426. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6427. }
  6428. }
  6429. //---------------------------------------------------------------------------
  6430. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6431. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6432. UnicodeString HelpKeyword, const TMessageParams * Params,
  6433. TTerminal * Terminal)
  6434. {
  6435. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6436. (FOnFeedSynchronizeError != NULL)) &&
  6437. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6438. {
  6439. if (FOnFeedSynchronizeError != NULL)
  6440. {
  6441. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6442. }
  6443. else
  6444. {
  6445. DebugAssert(FErrorList != NULL);
  6446. TStringList * MoreMessagesCopy = NULL;
  6447. if (MoreMessages)
  6448. {
  6449. MoreMessagesCopy = new TStringList();
  6450. MoreMessagesCopy->Assign(MoreMessages);
  6451. }
  6452. FErrorList->AddObject(Message, MoreMessagesCopy);
  6453. }
  6454. PopupTrayBalloon(Terminal, Message, Type);
  6455. return ContinueAnswer(Answers);
  6456. }
  6457. else
  6458. {
  6459. bool UseBalloon = FTrayIcon->Visible;
  6460. if (UseBalloon)
  6461. {
  6462. PopupTrayBalloon(Terminal, Message, Type);
  6463. }
  6464. unsigned int Result;
  6465. try
  6466. {
  6467. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6468. }
  6469. __finally
  6470. {
  6471. // cancel only balloon we popped up, otherwise we may cancel notification
  6472. // balloon that was there before the message dialog
  6473. // (such as "dd confirmation opt in balloon)
  6474. if (UseBalloon)
  6475. {
  6476. FTrayIcon->CancelBalloon();
  6477. }
  6478. }
  6479. return Result;
  6480. }
  6481. }
  6482. //---------------------------------------------------------------------------
  6483. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6484. TTerminal * Terminal, Exception * E)
  6485. {
  6486. if (FTrayIcon->Visible)
  6487. {
  6488. PopupTrayBalloon(Terminal, L"", qtError, E);
  6489. }
  6490. // particularly prevent opening new session from jump list,
  6491. // while exception is shown
  6492. NonVisualDataModule->StartBusy();
  6493. try
  6494. {
  6495. ShowExtendedExceptionEx(Terminal, E);
  6496. }
  6497. __finally
  6498. {
  6499. NonVisualDataModule->EndBusy();
  6500. FTrayIcon->CancelBalloon();
  6501. }
  6502. }
  6503. //---------------------------------------------------------------------------
  6504. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6505. {
  6506. // cannot rely on active page being page for active terminal,
  6507. // as it can happen that active page is the "new session" page
  6508. // (e.g. when reconnecting active terminal, while login dialog
  6509. // invoked from "new session" page is modal)
  6510. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6511. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6512. }
  6513. //---------------------------------------------------------------------------
  6514. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6515. TTerminal * Terminal, Exception * E)
  6516. {
  6517. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6518. if (!Terminal->Active)
  6519. {
  6520. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6521. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6522. {
  6523. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6524. }
  6525. }
  6526. }
  6527. //---------------------------------------------------------------------------
  6528. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6529. UnicodeString Message, TQueryType Type,
  6530. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6531. {
  6532. if ((E == NULL) ||
  6533. ExceptionMessage(E, Message))
  6534. {
  6535. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6536. if (Important)
  6537. {
  6538. Seconds *= 5;
  6539. }
  6540. UnicodeString NoteMessage(UnformatMessage(Message));
  6541. if (Terminal != NULL)
  6542. {
  6543. NoteMessage = FORMAT(L"%s: %s",
  6544. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6545. }
  6546. if (WinConfiguration->BalloonNotifications)
  6547. {
  6548. AddNote(NoteMessage);
  6549. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6550. }
  6551. else
  6552. {
  6553. FlashOnBackground();
  6554. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6555. }
  6556. }
  6557. }
  6558. //---------------------------------------------------------------------------
  6559. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6560. {
  6561. RestoreApp();
  6562. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6563. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6564. !NonVisualDataModule->Busy)
  6565. {
  6566. TTerminal * Terminal = TerminalNoteData->Terminal;
  6567. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6568. if (!ComponentVisible[fcQueueView])
  6569. {
  6570. ToggleQueueVisibility();
  6571. GoToQueue();
  6572. }
  6573. }
  6574. }
  6575. //---------------------------------------------------------------------------
  6576. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6577. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6578. {
  6579. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6580. UnicodeString Message;
  6581. TNotifyEvent OnClick = NULL;
  6582. TObject * UserData = NULL;
  6583. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6584. switch (Event)
  6585. {
  6586. case qeEmptyButMonitored:
  6587. if ((ATerminal != Terminal) || QueueInvisible)
  6588. {
  6589. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6590. OnClick = QueueEmptyNoteClicked;
  6591. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6592. TerminalNoteData->Terminal = ATerminal;
  6593. UserData = TerminalNoteData;
  6594. }
  6595. break;
  6596. case qeEmpty:
  6597. OperationComplete(ManagedTerminal->QueueOperationStart);
  6598. break;
  6599. case qePendingUserAction:
  6600. if ((ATerminal != Terminal) ||
  6601. (QueueInvisible && !IsQueueAutoPopup()))
  6602. {
  6603. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6604. }
  6605. break;
  6606. default:
  6607. DebugFail();
  6608. }
  6609. if (!Message.IsEmpty())
  6610. {
  6611. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6612. }
  6613. }
  6614. //---------------------------------------------------------------------------
  6615. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6616. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6617. {
  6618. if (FDDExtMapFile != NULL)
  6619. {
  6620. CloseHandle(FDDExtMapFile);
  6621. FDDExtMapFile = NULL;
  6622. }
  6623. if (WinConfiguration->DDFakeFile)
  6624. {
  6625. DDFakeFileInitDrag(FileList, Created);
  6626. }
  6627. }
  6628. //---------------------------------------------------------------------------
  6629. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6630. {
  6631. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6632. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6633. {
  6634. FFakeFileDropTarget = FileName;
  6635. }
  6636. }
  6637. //---------------------------------------------------------------------------
  6638. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6639. {
  6640. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6641. if (!ForceDirectories(ApiPath(Result)))
  6642. {
  6643. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6644. }
  6645. FileSetAttr(ApiPath(Result), faHidden);
  6646. return Result;
  6647. }
  6648. //---------------------------------------------------------------------------
  6649. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6650. bool & Created)
  6651. {
  6652. FFakeFileDropTarget = UnicodeString();
  6653. FDragFakeDirectory = CreateFakeTransferDirectory();
  6654. FileList->AddItem(NULL, FDragFakeDirectory);
  6655. Created = true;
  6656. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6657. {
  6658. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6659. }
  6660. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6661. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6662. {
  6663. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6664. TDragExtCommStruct* CommStruct;
  6665. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6666. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6667. DebugAssert(CommStruct != NULL);
  6668. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6669. CommStruct->Dragging = true;
  6670. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6671. LENOF(CommStruct->DropDest));
  6672. NULL_TERMINATE(CommStruct->DropDest);
  6673. UnmapViewOfFile(CommStruct);
  6674. }
  6675. }
  6676. //---------------------------------------------------------------------------
  6677. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6678. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6679. {
  6680. bool Result;
  6681. DebugAssert(!IsLocalBrowserMode());
  6682. if (Sender == RemoteDirView)
  6683. {
  6684. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6685. }
  6686. else
  6687. {
  6688. DebugAssert(Sender == RemoteDriveView);
  6689. TStrings * FileList = RemoteDriveView->DragFileList();
  6690. try
  6691. {
  6692. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6693. }
  6694. __finally
  6695. {
  6696. delete FileList;
  6697. }
  6698. }
  6699. if (FDDTargetControl == RemoteDriveView)
  6700. {
  6701. RemoteDriveView->UpdateDropTarget();
  6702. }
  6703. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6704. // or from tree view to dir view.
  6705. // foMove happens when file/dir is dragged from remote tree view outside of
  6706. // application via dragex
  6707. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6708. (DropSourceControl == RemoteDriveView))
  6709. {
  6710. RemoteDriveView->UpdateDropSource();
  6711. }
  6712. return Result;
  6713. }
  6714. //---------------------------------------------------------------------------
  6715. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6716. {
  6717. DebugAssert(!IsLocalBrowserMode());
  6718. // This also handles drops of remote files to queue.
  6719. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6720. SAFE_DESTROY(FDDFileList);
  6721. if (!FFakeFileDropTarget.IsEmpty())
  6722. {
  6723. RemoveDir(ApiPath(FFakeFileDropTarget));
  6724. }
  6725. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6726. {
  6727. try
  6728. {
  6729. TDragResult DDResult = (Sender == RemoteDirView) ?
  6730. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6731. // Focus is moved to the target application,
  6732. // but as we are going to present the UI, we need to steal the focus back.
  6733. // This most likely won't work though (windows does not allow application
  6734. // to steal focus most of the time)
  6735. Application->BringToFront();
  6736. // On older version of Windows we never got drMove here, see also comment below.
  6737. // On Windows 10, we get the "move".
  6738. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6739. {
  6740. UnicodeString TargetDirectory;
  6741. TFileOperation Operation;
  6742. // drInvalid may mean drMove, see comment below
  6743. switch (DDResult)
  6744. {
  6745. case drCopy:
  6746. Operation = foCopy;
  6747. break;
  6748. case drMove:
  6749. Operation = foMove;
  6750. break;
  6751. default:
  6752. DebugFail();
  6753. case drInvalid:
  6754. // prefer "copy" for safety
  6755. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6756. break;
  6757. }
  6758. TTransferOperationParam Param;
  6759. UnicodeString CounterName;
  6760. bool ForceQueue;
  6761. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6762. {
  6763. // we get drInvalid both if d&d was intercepted by ddext,
  6764. // and when users drops on no-drop location.
  6765. // we tell the difference by existence of response from ddext,
  6766. // so we ignore absence of response in this case
  6767. if (DDResult != drInvalid)
  6768. {
  6769. // here we know that the extension is installed,
  6770. // as it is checked as soon as drag&drop starts from
  6771. // RemoteFileControlDDCreateDragFileList
  6772. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6773. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6774. {
  6775. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6776. }
  6777. else
  6778. {
  6779. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6780. }
  6781. }
  6782. }
  6783. else
  6784. {
  6785. // download using fake file
  6786. Param.Temp = false;
  6787. Param.DragDrop = true;
  6788. if (ForceQueue)
  6789. {
  6790. Param.Queue = asOn;
  6791. }
  6792. if (Sender == RemoteDirView)
  6793. {
  6794. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6795. }
  6796. if (RemoteFileControlFileOperation(Sender, Operation,
  6797. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6798. {
  6799. Configuration->Usage->Inc(CounterName);
  6800. }
  6801. }
  6802. }
  6803. }
  6804. __finally
  6805. {
  6806. CloseHandle(FDDExtMapFile);
  6807. FDDExtMapFile = NULL;
  6808. delete FDragFakeMonitors;
  6809. FDragFakeMonitors = NULL;
  6810. RemoveDir(ApiPath(FDragFakeDirectory));
  6811. FDragFakeDirectory = L"";
  6812. FFakeFileDropTarget = UnicodeString();
  6813. }
  6814. }
  6815. if (FDragDropSshTerminate.get() != NULL)
  6816. {
  6817. FDragDropSshTerminate->Rethrow();
  6818. }
  6819. }
  6820. //---------------------------------------------------------------------------
  6821. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6822. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6823. {
  6824. // Remember drop effect so we know (when user drops files), if we copy or move
  6825. FLastDropEffect = dwEffect;
  6826. }
  6827. //---------------------------------------------------------------------------
  6828. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6829. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6830. {
  6831. bool Result;
  6832. if (FDDTargetControl == QueueView3)
  6833. {
  6834. Directory = DefaultDownloadTargetDirectory();
  6835. Result = true;
  6836. CounterName = L"DownloadsDragDropQueue";
  6837. ForceQueue = true;
  6838. }
  6839. else if (!FFakeFileDropTarget.IsEmpty())
  6840. {
  6841. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6842. Result = true;
  6843. ForceQueue = false;
  6844. CounterName = L"DownloadsDragDropFakeFile";
  6845. }
  6846. else
  6847. {
  6848. ForceQueue = false;
  6849. Enabled = false;
  6850. try
  6851. {
  6852. int Timer = 0;
  6853. Result = false;
  6854. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6855. {
  6856. {
  6857. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6858. TDragExtCommStruct* CommStruct;
  6859. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6860. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6861. DebugAssert(CommStruct != NULL);
  6862. Result = !CommStruct->Dragging;
  6863. if (Result)
  6864. {
  6865. Directory = ExtractFilePath(CommStruct->DropDest);
  6866. CounterName = L"DownloadsDragDropExternalExt";
  6867. }
  6868. UnmapViewOfFile(CommStruct);
  6869. }
  6870. if (!Result)
  6871. {
  6872. Sleep(50);
  6873. Timer += 50;
  6874. Application->ProcessMessages();
  6875. }
  6876. }
  6877. }
  6878. __finally
  6879. {
  6880. Enabled = true;
  6881. }
  6882. }
  6883. return Result;
  6884. }
  6885. //---------------------------------------------------------------------------
  6886. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6887. const UnicodeString TempDir, int SecDelay)
  6888. {
  6889. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6890. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6891. if (FDelayedDeletionTimer == NULL)
  6892. {
  6893. DebugAssert(HandleAllocated());
  6894. FDelayedDeletionTimer = new TTimer(this);
  6895. FDelayedDeletionTimer->Interval = 10000;
  6896. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6897. }
  6898. else
  6899. {
  6900. FDelayedDeletionTimer->Enabled = true;
  6901. }
  6902. }
  6903. //---------------------------------------------------------------------------
  6904. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6905. {
  6906. DebugAssert(FDelayedDeletionList != NULL);
  6907. TDateTime N = Now();
  6908. TDateTime Alert;
  6909. UnicodeString Directory;
  6910. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6911. {
  6912. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6913. if ((N >= Alert) || (Sender == NULL))
  6914. {
  6915. Directory = FDelayedDeletionList->Strings[Index];
  6916. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6917. {
  6918. FDelayedDeletionList->Delete(Index);
  6919. }
  6920. }
  6921. }
  6922. if (FDelayedDeletionList->Count == 0)
  6923. {
  6924. FDelayedDeletionTimer->Enabled = false;
  6925. }
  6926. }
  6927. //---------------------------------------------------------------------------
  6928. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6929. {
  6930. DebugAssert(!IsLocalBrowserMode());
  6931. if (IsFileControl(FDDTargetControl, osRemote) ||
  6932. (FDDTargetControl == SessionsPageControl))
  6933. {
  6934. TTerminal * TargetTerminal = NULL;
  6935. TFileOperation Operation = foNone;
  6936. if (FDDTargetControl == SessionsPageControl)
  6937. {
  6938. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6939. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6940. // do not allow dropping on the "+" tab
  6941. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6942. if (TargetTerminal != NULL)
  6943. {
  6944. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6945. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6946. {
  6947. Operation = foRemoteMove;
  6948. }
  6949. else
  6950. {
  6951. Operation = foRemoteCopy;
  6952. }
  6953. }
  6954. }
  6955. else
  6956. {
  6957. if (FLastDropEffect == DROPEFFECT_MOVE)
  6958. {
  6959. Operation = foRemoteMove;
  6960. }
  6961. else if (FLastDropEffect == DROPEFFECT_COPY)
  6962. {
  6963. Operation = foRemoteCopy;
  6964. }
  6965. }
  6966. if (Operation != foNone)
  6967. {
  6968. RemoteFileControlFileOperation(DropSourceControl,
  6969. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6970. }
  6971. // abort drag&drop
  6972. Abort();
  6973. }
  6974. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6975. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6976. (FDDTargetControl != QueueView3))
  6977. {
  6978. DebugAssert(!FDragTempDir.IsEmpty());
  6979. TTransferType Type;
  6980. UnicodeString TempDir = FDragTempDir;
  6981. // We clear FDragTempDir before calling
  6982. // just in case it fail (raises exception)
  6983. FDragTempDir = L"";
  6984. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6985. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6986. // empty directory parameter means temp directory -> don't display it!
  6987. UnicodeString TargetDir = L"";
  6988. int Options = 0;
  6989. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6990. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6991. {
  6992. Abort();
  6993. }
  6994. // TargetDir is set when dropped on local file control
  6995. // (this was workaround for legacy dirview event handling, now it should be
  6996. // made prettier)
  6997. if (TargetDir.IsEmpty())
  6998. {
  6999. TargetDir = TempDir;
  7000. if (ForceDirectories(ApiPath(TargetDir)))
  7001. {
  7002. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7003. FPendingTempSpaceWarn = true;
  7004. try
  7005. {
  7006. FDragDropOperation = true;
  7007. int Params = cpTemporary |
  7008. (Type == ttMove ? cpDelete : 0);
  7009. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7010. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7011. }
  7012. __finally
  7013. {
  7014. FDragDropOperation = false;
  7015. FPendingTempSpaceWarn = false;
  7016. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7017. }
  7018. }
  7019. else
  7020. {
  7021. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7022. }
  7023. }
  7024. }
  7025. }
  7026. //---------------------------------------------------------------------------
  7027. void __fastcall TCustomScpExplorerForm::DDDownload(
  7028. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7029. {
  7030. DebugAssert(!IsLocalBrowserMode());
  7031. TAutoBatch AutoBatch(this);
  7032. UpdateCopyParamCounters(*CopyParam);
  7033. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7034. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7035. {
  7036. RemoteDriveView->UpdateDropSource();
  7037. }
  7038. }
  7039. //---------------------------------------------------------------------------
  7040. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7041. {
  7042. public:
  7043. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7044. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7045. {
  7046. }
  7047. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7048. {
  7049. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7050. TDataObjectFilesEx::AllowData(FormatEtc);
  7051. }
  7052. };
  7053. //---------------------------------------------------------------------------
  7054. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7055. TObject * Sender, TDataObject *& DataObject)
  7056. {
  7057. if (FDDExtMapFile != NULL)
  7058. {
  7059. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7060. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7061. {
  7062. Abort();
  7063. }
  7064. if (FileList->Count > 0)
  7065. {
  7066. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7067. if (!FilesObject->IsValid(true, true))
  7068. {
  7069. FilesObject->_Release();
  7070. }
  7071. else
  7072. {
  7073. DataObject = FilesObject;
  7074. }
  7075. }
  7076. }
  7077. }
  7078. //---------------------------------------------------------------------------
  7079. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7080. {
  7081. DebugFail();
  7082. }
  7083. //---------------------------------------------------------------------------
  7084. void __fastcall TCustomScpExplorerForm::GoToTree()
  7085. {
  7086. ComponentVisible[fcRemoteTree] = true;
  7087. DriveView(osRemote)->SetFocus();
  7088. }
  7089. //---------------------------------------------------------------------------
  7090. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7091. TPanelExport Export)
  7092. {
  7093. TCustomDirView * DirView = this->DirView(Side);
  7094. std::unique_ptr<TStrings> ExportData(new TStringList());
  7095. switch (Export)
  7096. {
  7097. case pePath:
  7098. ExportData->Add(DirView->PathName);
  7099. break;
  7100. case peFileList:
  7101. case peFullFileList:
  7102. {
  7103. bool FullPath = (Export == peFullFileList);
  7104. DirView->CreateFileList(false, FullPath, ExportData.get());
  7105. for (int Index = 0; Index < ExportData->Count; Index++)
  7106. {
  7107. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7108. {
  7109. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7110. }
  7111. }
  7112. }
  7113. break;
  7114. default:
  7115. DebugFail();
  7116. }
  7117. return ExportData.release();
  7118. }
  7119. //---------------------------------------------------------------------------
  7120. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7121. TPanelExport Export, TPanelExportDestination Destination)
  7122. {
  7123. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7124. PanelExportStore(Side, Export, Destination, ExportData.get());
  7125. }
  7126. //---------------------------------------------------------------------------
  7127. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7128. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7129. TStrings * ExportData)
  7130. {
  7131. if (Destination == pedClipboard)
  7132. {
  7133. TInstantOperationVisualizer Visualizer;
  7134. CopyToClipboard(ExportData);
  7135. }
  7136. else
  7137. {
  7138. DebugFail();
  7139. }
  7140. }
  7141. //---------------------------------------------------------------------------
  7142. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7143. {
  7144. TCustomDirView * DirView = this->DirView(Side);
  7145. UnicodeString Mask = DirView->Mask;
  7146. if (DoFilterMaskDialog(DirView, Mask))
  7147. {
  7148. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7149. Configuration->Usage->Inc(L"Filters");
  7150. }
  7151. }
  7152. //---------------------------------------------------------------------------
  7153. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7154. {
  7155. return FQueueController->DefaultOperation();
  7156. }
  7157. //---------------------------------------------------------------------------
  7158. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7159. TQueueOperation Operation, void ** Param)
  7160. {
  7161. switch (Operation)
  7162. {
  7163. case qoPreferences:
  7164. return true;
  7165. case qoGoTo:
  7166. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7167. case qoOnceEmpty:
  7168. return !FQueueController->Empty;
  7169. default:
  7170. return FQueueController->AllowOperation(Operation, Param);
  7171. }
  7172. }
  7173. //---------------------------------------------------------------------------
  7174. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7175. {
  7176. if (DebugAlwaysTrue(QueueView3->Visible))
  7177. {
  7178. QueueView3->SetFocus();
  7179. }
  7180. }
  7181. //---------------------------------------------------------------------------
  7182. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7183. TQueueOperation Operation, void * Param)
  7184. {
  7185. if (Operation == qoGoTo)
  7186. {
  7187. GoToQueue();
  7188. }
  7189. else if (Operation == qoPreferences)
  7190. {
  7191. PreferencesDialog(pmQueue);
  7192. }
  7193. else
  7194. {
  7195. FQueueController->ExecuteOperation(Operation, Param);
  7196. }
  7197. }
  7198. //---------------------------------------------------------------------------
  7199. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7200. {
  7201. return (Queue != NULL) && Queue->Enabled;
  7202. }
  7203. //---------------------------------------------------------------------------
  7204. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7205. {
  7206. DebugAssert(Queue != NULL);
  7207. if (Queue != NULL)
  7208. {
  7209. Queue->Enabled = !Queue->Enabled;
  7210. }
  7211. }
  7212. //---------------------------------------------------------------------------
  7213. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7214. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7215. {
  7216. FQueueActedItem = QueueView3->ItemFocused;
  7217. }
  7218. //---------------------------------------------------------------------------
  7219. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7220. {
  7221. return TopDock->Height + QueueSplitter->Height;
  7222. }
  7223. //---------------------------------------------------------------------------
  7224. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7225. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7226. {
  7227. // when queue is hidden by double-clicking splitter, stray attempt to
  7228. // resize the panel with strange value arrives, make sure it is ignored
  7229. if (ComponentVisible[fcQueueView])
  7230. {
  7231. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7232. RemotePanel->Constraints->MinHeight;
  7233. if (NewSize > HeightLimit)
  7234. {
  7235. NewSize = HeightLimit;
  7236. }
  7237. }
  7238. else
  7239. {
  7240. Accept = false;
  7241. }
  7242. }
  7243. //---------------------------------------------------------------------------
  7244. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7245. TDragObject *& /*DragObject*/)
  7246. {
  7247. FQueueActedItem = QueueView3->ItemFocused;
  7248. }
  7249. //---------------------------------------------------------------------------
  7250. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7251. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7252. {
  7253. Accept = true;
  7254. if (Source == QueueView3)
  7255. {
  7256. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7257. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7258. if (Accept)
  7259. {
  7260. TQueueItemProxy * QueueItem;
  7261. TQueueItemProxy * DestQueueItem;
  7262. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7263. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7264. Accept = (QueueItem != DestQueueItem) &&
  7265. (QueueItem->Status == TQueueItem::qsPending) &&
  7266. (DestQueueItem->Status == TQueueItem::qsPending);
  7267. }
  7268. }
  7269. }
  7270. //---------------------------------------------------------------------------
  7271. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7272. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7273. {
  7274. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7275. {
  7276. TQueueItemProxy * QueueItem;
  7277. TQueueItemProxy * DestQueueItem;
  7278. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7279. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7280. QueueItem->Move(DestQueueItem);
  7281. }
  7282. }
  7283. //---------------------------------------------------------------------------
  7284. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7285. {
  7286. if ((QueueView3->ItemFocused == NULL) &&
  7287. (QueueView3->Items->Count > 0))
  7288. {
  7289. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7290. }
  7291. QueueLabelUpdateStatus();
  7292. }
  7293. //---------------------------------------------------------------------------
  7294. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7295. {
  7296. QueueLabelUpdateStatus();
  7297. }
  7298. //---------------------------------------------------------------------------
  7299. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7300. {
  7301. QueueLabel->UpdateStatus();
  7302. }
  7303. //---------------------------------------------------------------------------
  7304. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7305. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7306. {
  7307. if (Selected)
  7308. {
  7309. NonVisualDataModule->UpdateNonVisibleActions();
  7310. }
  7311. }
  7312. //---------------------------------------------------------------------------
  7313. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7314. {
  7315. TDragDropFilesEx * Result = NULL;
  7316. if (Sender == SessionsPageControl)
  7317. {
  7318. Result = FSessionsDragDropFilesEx;
  7319. }
  7320. else if (Sender == QueueView3)
  7321. {
  7322. Result = FQueueDragDropFilesEx;
  7323. }
  7324. else
  7325. {
  7326. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7327. if (DirView != NULL)
  7328. {
  7329. Result = DirView->DragDropFilesEx;
  7330. }
  7331. else
  7332. {
  7333. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7334. if (DriveView != NULL)
  7335. {
  7336. Result = DriveView->DragDropFilesEx;
  7337. }
  7338. }
  7339. }
  7340. DebugAssert(Result != NULL);
  7341. return Result;
  7342. }
  7343. //---------------------------------------------------------------------------
  7344. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7345. {
  7346. return (DView->SelCount == DView->FilesCount);
  7347. }
  7348. //---------------------------------------------------------------------------
  7349. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7350. {
  7351. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7352. }
  7353. //---------------------------------------------------------------------------
  7354. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7355. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7356. {
  7357. DebugAssert(!IsLocalBrowserMode());
  7358. TFileOperation Operation;
  7359. switch (Effect)
  7360. {
  7361. case DROPEFFECT_MOVE:
  7362. Operation = foMove;
  7363. break;
  7364. case DROPEFFECT_COPY:
  7365. // occures on WinXP (reported by user)
  7366. default:
  7367. Operation = foCopy;
  7368. break;
  7369. };
  7370. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7371. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7372. if (DragDropFilesEx->FileList->Count > 0)
  7373. {
  7374. TStrings * FileList = new TStringList();
  7375. try
  7376. {
  7377. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7378. {
  7379. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7380. }
  7381. FDragDropOperation = true;
  7382. TTransferOperationParam Param;
  7383. Param.TargetDirectory = TargetPath;
  7384. // upload, no temp dirs
  7385. Param.Temp = false;
  7386. Param.DragDrop = DragDrop;
  7387. Param.Options =
  7388. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7389. if (ForceQueue)
  7390. {
  7391. Param.Queue = asOn;
  7392. }
  7393. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7394. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7395. {
  7396. if (IsFileControl(DropSourceControl, osLocal))
  7397. {
  7398. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7399. }
  7400. else
  7401. {
  7402. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7403. }
  7404. }
  7405. }
  7406. __finally
  7407. {
  7408. FDragDropOperation = false;
  7409. delete FileList;
  7410. }
  7411. }
  7412. }
  7413. //---------------------------------------------------------------------------
  7414. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7415. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7416. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7417. {
  7418. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7419. }
  7420. //---------------------------------------------------------------------------
  7421. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7422. TObject * Sender, int grfKeyState, int & dwEffect)
  7423. {
  7424. DebugAssert(!IsLocalBrowserMode());
  7425. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7426. if ((dwEffect != DROPEFFECT_NONE) &&
  7427. IsFileControl(DropSourceControl, osRemote))
  7428. {
  7429. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7430. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7431. (RemoteDirView->DropTarget == NULL))
  7432. {
  7433. dwEffect = DROPEFFECT_NONE;
  7434. }
  7435. else
  7436. {
  7437. if (dwEffect == DROPEFFECT_COPY)
  7438. {
  7439. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7440. // currently we support copying always (at least via temporary directory);
  7441. // remove associated checks once this all proves stable and working
  7442. bool CopyCapable = true;
  7443. // if we do not support neither of operations, there's no discussion
  7444. if (!MoveCapable && !CopyCapable)
  7445. {
  7446. dwEffect = DROPEFFECT_NONE;
  7447. }
  7448. else
  7449. {
  7450. // The default effect inside remote panel is move,
  7451. // unless we do not support it, but support copy
  7452. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7453. {
  7454. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7455. }
  7456. else
  7457. {
  7458. // with ctrl-down, we want copy unless it is not supported
  7459. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7460. }
  7461. }
  7462. }
  7463. }
  7464. }
  7465. }
  7466. //---------------------------------------------------------------------------
  7467. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7468. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7469. {
  7470. DebugAssert(!IsLocalBrowserMode());
  7471. if (FDDTotalSize >= 0)
  7472. {
  7473. if (File->IsDirectory)
  7474. {
  7475. FDDTotalSize = -1;
  7476. }
  7477. else
  7478. {
  7479. FDDTotalSize += File->Size;
  7480. }
  7481. }
  7482. DebugAssert(!FDragTempDir.IsEmpty());
  7483. // TODO: this is quite ineffective
  7484. // TODO: what if invalid character replacement is disabled?
  7485. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7486. UnicodeString TransferFileName;
  7487. if (Sender == RemoteDriveView)
  7488. {
  7489. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7490. }
  7491. else
  7492. {
  7493. TransferFileName = File->FileName;
  7494. }
  7495. FDDFileList->AddObject(TransferFileName, File);
  7496. }
  7497. //---------------------------------------------------------------------------
  7498. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7499. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7500. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7501. {
  7502. // sometimes we do not get DDEnd so the list is not released
  7503. SAFE_DESTROY(FDDFileList);
  7504. FDDFileList = new TStringList();
  7505. FDragTempDir = WinConfiguration->TemporaryDir();
  7506. FDDTotalSize = 0;
  7507. FDragDropSshTerminate.reset(NULL);
  7508. }
  7509. //---------------------------------------------------------------------------
  7510. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7511. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7512. HRESULT & Result)
  7513. {
  7514. if (Result == DRAGDROP_S_DROP)
  7515. {
  7516. try
  7517. {
  7518. GlobalDragImageList->HideDragImage();
  7519. try
  7520. {
  7521. RemoteFileControlDDTargetDrop();
  7522. }
  7523. catch(ESshTerminate & E)
  7524. {
  7525. FDragDropSshTerminate.reset(E.Clone());
  7526. }
  7527. }
  7528. catch (Exception &E)
  7529. {
  7530. // If downloading fails we need to cancel drag&drop, otherwise
  7531. // Explorer shows error
  7532. // But by the way exception probably never reach this point as
  7533. // it's catched on way.
  7534. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7535. Result = DRAGDROP_S_CANCEL;
  7536. ShowExtendedException(Terminal, &E);
  7537. }
  7538. }
  7539. }
  7540. //---------------------------------------------------------------------------
  7541. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7542. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7543. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7544. {
  7545. TFileMasks::TParams MaskParams;
  7546. MaskParams.Size = Size;
  7547. MaskParams.Modification = Modification;
  7548. // this does not re-parse the mask if it is the same as the last time
  7549. FDirViewMatchMask = Masks;
  7550. bool ImplicitMatch;
  7551. Matches =
  7552. // RecurseInclude parameter has no effect as the Path is empty
  7553. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7554. (AllowImplicitMatches || !ImplicitMatch);
  7555. }
  7556. //---------------------------------------------------------------------------
  7557. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7558. TObject * Sender, TListItem * Item, WORD & Indexes)
  7559. {
  7560. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7561. UnicodeString Ext;
  7562. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7563. {
  7564. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7565. }
  7566. else
  7567. {
  7568. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7569. }
  7570. if (SameText(Ext, Configuration->PartialExt))
  7571. {
  7572. Indexes |= oiPartial;
  7573. }
  7574. }
  7575. //---------------------------------------------------------------------------
  7576. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7577. {
  7578. return
  7579. DirViewEnabled(osCurrent) &&
  7580. DirView(osCurrent)->CanPasteFromClipBoard();
  7581. }
  7582. //---------------------------------------------------------------------------
  7583. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7584. {
  7585. bool Result = false;
  7586. if (CanPasteToDirViewFromClipBoard())
  7587. {
  7588. Result = true;
  7589. }
  7590. else
  7591. {
  7592. UnicodeString ClipboardText;
  7593. if (NonEmptyTextFromClipboard(ClipboardText))
  7594. {
  7595. if (StoredSessions->IsUrl(ClipboardText))
  7596. {
  7597. Result = true;
  7598. }
  7599. else
  7600. {
  7601. Result = DirViewEnabled(osCurrent);
  7602. }
  7603. }
  7604. }
  7605. return Result;
  7606. }
  7607. //---------------------------------------------------------------------------
  7608. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7609. {
  7610. if (DoesClipboardContainOurFiles())
  7611. {
  7612. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7613. {
  7614. DebugAssert(!IsLocalBrowserMode());
  7615. TTerminalManager * Manager = TTerminalManager::Instance();
  7616. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7617. Manager->ActiveTerminal = FClipboardTerminal;
  7618. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7619. }
  7620. }
  7621. else if (CanPasteToDirViewFromClipBoard())
  7622. {
  7623. DirView(osCurrent)->PasteFromClipBoard();
  7624. }
  7625. else
  7626. {
  7627. UnicodeString ClipboardText;
  7628. if (NonEmptyTextFromClipboard(ClipboardText))
  7629. {
  7630. if (StoredSessions->IsUrl(ClipboardText))
  7631. {
  7632. NewSession(false, ClipboardText);
  7633. }
  7634. else
  7635. {
  7636. DirView(osCurrent)->Path = ClipboardText;
  7637. }
  7638. }
  7639. }
  7640. }
  7641. //---------------------------------------------------------------------------
  7642. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7643. {
  7644. // TBD
  7645. }
  7646. //---------------------------------------------------------------------------
  7647. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7648. {
  7649. TCustomDirView * ADirView = DirView(Side);
  7650. ADirView->SelectAll(Mode);
  7651. ADirView->SetFocus();
  7652. }
  7653. //---------------------------------------------------------------------------
  7654. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7655. {
  7656. TCustomDirView * ADirView = DirView(Side);
  7657. TFileFilter Filter;
  7658. DefaultFileFilter(Filter);
  7659. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7660. {
  7661. Configuration->Usage->Inc(L"MaskSelections");
  7662. ADirView->SelectFiles(Filter, Select);
  7663. ADirView->SetFocus();
  7664. }
  7665. }
  7666. //---------------------------------------------------------------------------
  7667. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7668. {
  7669. TCustomDirView * ADirView = DirView(Side);
  7670. ADirView->RestoreSelectedNames();
  7671. ADirView->SetFocus();
  7672. }
  7673. //---------------------------------------------------------------------------
  7674. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7675. {
  7676. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7677. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7678. {
  7679. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7680. UnicodeString Ext;
  7681. if (!IsSideLocalBrowser(osCurrent))
  7682. {
  7683. Ext = UnixExtractFileExt(FileName);
  7684. }
  7685. else
  7686. {
  7687. Ext = ExtractFileExt(FileName);
  7688. }
  7689. if (Ext.IsEmpty())
  7690. {
  7691. Ext = L".";
  7692. }
  7693. TFileFilter Filter;
  7694. Filter.Masks = FORMAT(L"*%s", (Ext));
  7695. Filter.Directories = false;
  7696. CurrentDirView->SelectFiles(Filter, Select);
  7697. }
  7698. }
  7699. //---------------------------------------------------------------------------
  7700. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7701. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7702. {
  7703. UnicodeString Result;
  7704. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7705. {
  7706. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7707. }
  7708. else if (!IsSideLocalBrowser(Side) && (Terminal == NULL))
  7709. {
  7710. // noop
  7711. }
  7712. else
  7713. {
  7714. Result =
  7715. FMTLOAD(FILE_INFO_FORMAT,
  7716. (FormatBytes(FileInfo.SelectedSize),
  7717. FormatBytes(FileInfo.FilesSize),
  7718. FormatNumber(FileInfo.SelectedCount),
  7719. FormatNumber(FileInfo.FilesCount)));
  7720. }
  7721. return Result;
  7722. }
  7723. //---------------------------------------------------------------------------
  7724. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7725. TTBXStatusPanel * Panel, TOperationSide Side)
  7726. {
  7727. if (Panel->Index == 1)
  7728. {
  7729. ToggleShowHiddenFiles();
  7730. }
  7731. else if (Panel->Index == 2)
  7732. {
  7733. Filter(Side);
  7734. }
  7735. }
  7736. //---------------------------------------------------------------------------
  7737. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7738. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7739. {
  7740. DebugAssert(!StatusBar->SimplePanel);
  7741. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7742. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7743. }
  7744. //---------------------------------------------------------------------------
  7745. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7746. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7747. {
  7748. DebugAssert(StatusBar->Panels->Count >= 3);
  7749. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7750. if (FileInfo.HiddenCount > 0)
  7751. {
  7752. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7753. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7754. }
  7755. else
  7756. {
  7757. HiddenFilesPanel->ViewPriority = 0;
  7758. // not really necessary, just to cleanup no-longer-valid data
  7759. HiddenFilesPanel->Caption = L"";
  7760. }
  7761. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7762. if (FileInfo.FilteredCount > 0)
  7763. {
  7764. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7765. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7766. }
  7767. else
  7768. {
  7769. FilteredFilesPanel->ViewPriority = 0;
  7770. // not really necessary, just to cleanup no-longer-valid data
  7771. FilteredFilesPanel->Caption = L"";
  7772. }
  7773. }
  7774. //---------------------------------------------------------------------------
  7775. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7776. TObject * /*Sender*/)
  7777. {
  7778. if (DirView(osRemote)->Enabled)
  7779. {
  7780. DirView(osRemote)->SetFocus();
  7781. }
  7782. }
  7783. //---------------------------------------------------------------------------
  7784. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7785. {
  7786. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7787. switch (Config.Show)
  7788. {
  7789. case qvShow:
  7790. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7791. {
  7792. Config.Show = qvHide;
  7793. }
  7794. else
  7795. {
  7796. Config.Show = Config.LastHideShow;
  7797. }
  7798. break;
  7799. case qvHideWhenEmpty:
  7800. if (ComponentVisible[fcQueueView])
  7801. {
  7802. Config.Show = qvHide;
  7803. }
  7804. else
  7805. {
  7806. Config.LastHideShow = Config.Show;
  7807. Config.Show = qvShow;
  7808. }
  7809. break;
  7810. case qvHide:
  7811. Config.LastHideShow = Config.Show;
  7812. Config.Show = qvShow;
  7813. break;
  7814. }
  7815. WinConfiguration->QueueView = Config;
  7816. }
  7817. //---------------------------------------------------------------------------
  7818. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7819. {
  7820. UnicodeString Result;
  7821. if (FTerminal != NULL)
  7822. {
  7823. switch (WinConfiguration->PathInCaption)
  7824. {
  7825. case picShort:
  7826. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7827. break;
  7828. case picFull:
  7829. Result = FTerminal->CurrentDirectory;
  7830. break;
  7831. }
  7832. }
  7833. return Result;
  7834. }
  7835. //---------------------------------------------------------------------------
  7836. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7837. {
  7838. TColor Result = GetWindowColor(FSessionColor);
  7839. return Result;
  7840. }
  7841. //---------------------------------------------------------------------------
  7842. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7843. {
  7844. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7845. return Result;
  7846. }
  7847. //---------------------------------------------------------------------------
  7848. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7849. {
  7850. TTerminalManager::Instance()->UpdateAppTitle();
  7851. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7852. // background to gray on Windows 7 (works on Windows 10).
  7853. // See also EnableControl
  7854. if (Showing)
  7855. {
  7856. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7857. if (HasTerminal)
  7858. {
  7859. // Update path when it changes
  7860. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7861. {
  7862. UpdateSessionTab(SessionsPageControl->ActivePage);
  7863. }
  7864. // TODO needed yet with second local browser?
  7865. if (!RemoteDirView->Enabled)
  7866. {
  7867. RemoteDirView->Enabled = true;
  7868. if (FRemoteDirViewWasFocused)
  7869. {
  7870. ActiveControl = RemoteDirView;
  7871. }
  7872. }
  7873. RemoteDirView->Color = PanelColor();
  7874. }
  7875. else
  7876. {
  7877. if (RemoteDirView->Enabled)
  7878. {
  7879. // This is first called when the form is being constructed
  7880. // (not anymore due to Showing test above)
  7881. // but the false is overriden in the constructor later.
  7882. // An even later in TScpCommanderForm::DoShow()
  7883. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7884. RemoteDirView->Enabled = false;
  7885. RemoteDirView->Color = DisabledPanelColor();
  7886. }
  7887. }
  7888. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  7889. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  7890. RemoteDriveView->Color = RemoteDirView->Color;
  7891. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  7892. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  7893. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  7894. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  7895. QueueLabelUpdateStatus();
  7896. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  7897. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  7898. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7899. }
  7900. }
  7901. //---------------------------------------------------------------------------
  7902. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7903. {
  7904. UpdateControls();
  7905. }
  7906. //---------------------------------------------------------------------------
  7907. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7908. TObject * Sender)
  7909. {
  7910. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7911. DebugAssert(DirView != NULL);
  7912. DoDirViewLoaded(DirView);
  7913. TransferPresetAutoSelect();
  7914. }
  7915. //---------------------------------------------------------------------------
  7916. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7917. {
  7918. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7919. // first run after installation
  7920. if (double(Updates.LastCheck) == 0)
  7921. {
  7922. // make sure next time there will be an update (if enabled)
  7923. Updates.LastCheck = TDateTime(1);
  7924. WinConfiguration->Updates = Updates;
  7925. }
  7926. else if ((double(Updates.Period) > 0) &&
  7927. (Now() - Updates.LastCheck >= Updates.Period))
  7928. {
  7929. TThreadMethod OnUpdatesChecked = NULL;
  7930. if (!IsUWP())
  7931. {
  7932. OnUpdatesChecked = UpdatesChecked;
  7933. }
  7934. StartUpdateThread(OnUpdatesChecked);
  7935. }
  7936. }
  7937. //---------------------------------------------------------------------------
  7938. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7939. {
  7940. UnicodeString Message;
  7941. bool New;
  7942. TQueryType Type;
  7943. GetUpdatesMessage(Message, New, Type, false);
  7944. if (!Message.IsEmpty())
  7945. {
  7946. if (New)
  7947. {
  7948. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7949. }
  7950. if (!New && (Type != qtWarning))
  7951. {
  7952. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7953. }
  7954. else
  7955. {
  7956. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7957. }
  7958. if (New)
  7959. {
  7960. Configuration->Usage->Inc(L"UpdateNotifications");
  7961. }
  7962. }
  7963. }
  7964. //---------------------------------------------------------------------------
  7965. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7966. {
  7967. RestoreApp();
  7968. bool CanDisplay = !NonVisualDataModule->Busy;
  7969. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7970. {
  7971. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7972. }
  7973. if (CanDisplay)
  7974. {
  7975. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7976. CheckForUpdates(true);
  7977. }
  7978. else
  7979. {
  7980. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7981. }
  7982. }
  7983. //---------------------------------------------------------------------------
  7984. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7985. TCopyParamRuleData & Data)
  7986. {
  7987. DebugAssert(Terminal != NULL);
  7988. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7989. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7990. Data.RemoteDirectory = RemoteDirView->PathName;
  7991. }
  7992. //---------------------------------------------------------------------------
  7993. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7994. {
  7995. // Terminal can be null when we are changing local directory implicitly,
  7996. // because it has been deleted, while no session is connected
  7997. // (Login dialog is open)
  7998. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7999. {
  8000. DebugAssert(!IsLocalBrowserMode());
  8001. TCopyParamRuleData Data;
  8002. GetTransferPresetAutoSelectData(Data);
  8003. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8004. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8005. if (CopyParamIndex < 0)
  8006. {
  8007. // there is no preset that matches autoselection
  8008. // set preset that we consider "default"
  8009. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8010. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8011. }
  8012. else
  8013. {
  8014. // there is preset matching autoselection
  8015. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8016. if (CopyParamName == FCopyParamAutoSelected)
  8017. {
  8018. // autoselected the same preset as the last time
  8019. // make no change (i.e. preserve custom user preset, if any)
  8020. }
  8021. else
  8022. {
  8023. // autoselected the different preset then the last time (or there
  8024. // was default preset set)
  8025. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8026. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8027. }
  8028. }
  8029. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8030. {
  8031. if (CopyParamIndex >= 0)
  8032. {
  8033. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8034. }
  8035. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8036. try
  8037. {
  8038. int Fmt =
  8039. (CopyParamIndex < 0) ?
  8040. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8041. COPY_PARAM_AUTOSELECTED;
  8042. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8043. Data->Message = MainInstructions(Message);
  8044. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8045. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8046. CopyParamAttrs);
  8047. if (CopyParamIndex >= 0)
  8048. {
  8049. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8050. Info = FORMAT(L"%s\n \n%s", (Info,
  8051. FMTLOAD(COPY_PARAM_RULE,
  8052. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8053. }
  8054. Data->Message += L"\n\n" + Info;
  8055. if (WinConfiguration->CopyParamAutoSelectNotice)
  8056. {
  8057. TransferPresetNoteMessage(Data, true);
  8058. }
  8059. else
  8060. {
  8061. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8062. Data = NULL; // ownership passed
  8063. }
  8064. }
  8065. __finally
  8066. {
  8067. delete Data;
  8068. }
  8069. }
  8070. }
  8071. }
  8072. //---------------------------------------------------------------------------
  8073. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8074. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8075. {
  8076. DebugAssert(NoteData != NULL);
  8077. DebugAssert(!IsLocalBrowserMode());
  8078. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8079. TQueryButtonAlias Aliases[1];
  8080. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8081. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8082. Params.Aliases = Aliases;
  8083. Params.AliasesCount = LENOF(Aliases);
  8084. unsigned int Result =
  8085. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8086. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8087. switch (Result)
  8088. {
  8089. case qaNeverAskAgain:
  8090. DebugAssert(AllowNeverAskAgain);
  8091. WinConfiguration->CopyParamAutoSelectNotice = false;
  8092. break;
  8093. case qaIgnore:
  8094. PreferencesDialog(pmPresets);
  8095. break;
  8096. }
  8097. }
  8098. //---------------------------------------------------------------------------
  8099. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8100. {
  8101. // as of now useless, as this is used for notes only, never for balloons, ...
  8102. RestoreApp();
  8103. // .. and we should never be busy here
  8104. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8105. {
  8106. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8107. }
  8108. }
  8109. //---------------------------------------------------------------------------
  8110. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8111. TPreferencesMode APreferencesMode)
  8112. {
  8113. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8114. TCopyParamRuleData Data;
  8115. if (Terminal != NULL)
  8116. {
  8117. PreferencesData.reset(new TPreferencesDialogData());
  8118. GetTransferPresetAutoSelectData(Data);
  8119. PreferencesData->CopyParamRuleData = &Data;
  8120. }
  8121. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8122. }
  8123. //---------------------------------------------------------------------------
  8124. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8125. const TCustomCommandType & Command)
  8126. {
  8127. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8128. {
  8129. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8130. }
  8131. }
  8132. //---------------------------------------------------------------------------
  8133. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8134. {
  8135. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8136. TCustomCommandType Command;
  8137. // make sure we use local custom command when remote are not supported
  8138. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8139. {
  8140. Command = FLastCustomCommand;
  8141. }
  8142. else
  8143. {
  8144. Command.Params = Command.Params | ccLocal;
  8145. }
  8146. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8147. FEditingFocusedAdHocCommand = OnFocused;
  8148. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8149. int Options =
  8150. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8151. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8152. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8153. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8154. {
  8155. FLastCustomCommand = Command;
  8156. UpdateCustomCommandsToolbar();
  8157. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8158. }
  8159. }
  8160. //---------------------------------------------------------------------------
  8161. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8162. {
  8163. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8164. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8165. DebugAssert(State > 0);
  8166. if (State <= 0)
  8167. {
  8168. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8169. }
  8170. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8171. }
  8172. //---------------------------------------------------------------------------
  8173. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8174. TCustomCommandType & Command, int & State)
  8175. {
  8176. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8177. if (Result)
  8178. {
  8179. Command = FLastCustomCommand;
  8180. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8181. }
  8182. return Result;
  8183. }
  8184. //---------------------------------------------------------------------------
  8185. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8186. {
  8187. ResetIncrementalSearch();
  8188. }
  8189. //---------------------------------------------------------------------------
  8190. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8191. {
  8192. DebugAssert(ComponentVisible[Component]);
  8193. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8194. }
  8195. //---------------------------------------------------------------------------
  8196. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8197. {
  8198. // when queue panel is resized here directly, the status bar is stretched
  8199. // over whole space the panel occupied
  8200. PostComponentHide(fcQueueView);
  8201. }
  8202. //---------------------------------------------------------------------------
  8203. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8204. {
  8205. WinConfiguration->ResetSysDarkTheme();
  8206. ConfigurationChanged();
  8207. ConfigureInterface();
  8208. TForm::Dispatch(&Message);
  8209. }
  8210. //---------------------------------------------------------------------------
  8211. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8212. {
  8213. TMessage * M = static_cast<TMessage*>(Message);
  8214. switch (M->Msg)
  8215. {
  8216. case CM_DIALOGCHAR:
  8217. CMDialogChar(*M);
  8218. break;
  8219. case WM_APPCOMMAND:
  8220. WMAppCommand(*M);
  8221. break;
  8222. case WM_SYSCOMMAND:
  8223. WMSysCommand(*M);
  8224. break;
  8225. case WM_QUERYENDSESSION:
  8226. WMQueryEndSession(*M);
  8227. break;
  8228. case WM_ENDSESSION:
  8229. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8230. break;
  8231. #ifdef _DEBUG
  8232. case WM_WINDOWPOSCHANGED:
  8233. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8234. break;
  8235. case CM_CUSTOMSTYLECHANGED:
  8236. TForm::Dispatch(Message);
  8237. break;
  8238. #endif
  8239. case WM_COMPONENT_HIDE:
  8240. {
  8241. Byte Component = static_cast<Byte>(M->WParam);
  8242. // sanity check
  8243. if (ComponentVisible[Component])
  8244. {
  8245. // special treatment
  8246. if (Component == fcQueueView)
  8247. {
  8248. ToggleQueueVisibility();
  8249. DebugAssert(!ComponentVisible[fcQueueView]);
  8250. }
  8251. else
  8252. {
  8253. ComponentVisible[Component] = false;
  8254. }
  8255. }
  8256. }
  8257. break;
  8258. case WM_COPYDATA:
  8259. WMCopyData(*M);
  8260. break;
  8261. case WM_MANAGES_CAPTION:
  8262. // caption managed in TTerminalManager::UpdateAppTitle()
  8263. M->Result = 1;
  8264. break;
  8265. case WM_WANTS_MOUSEWHEEL:
  8266. M->Result = 1;
  8267. break;
  8268. case CM_SHOWINGCHANGED:
  8269. CMShowingChanged(*M);
  8270. break;
  8271. case WM_CLOSE:
  8272. WMClose(*M);
  8273. break;
  8274. case WM_DPICHANGED:
  8275. WMDpiChanged(*M);
  8276. break;
  8277. case CM_DPICHANGED:
  8278. CMDpiChanged(*M);
  8279. break;
  8280. case WM_WININICHANGE:
  8281. WMWinIniChange(*M);
  8282. break;
  8283. case CM_DIALOGKEY:
  8284. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8285. break;
  8286. default:
  8287. TForm::Dispatch(Message);
  8288. break;
  8289. }
  8290. }
  8291. //---------------------------------------------------------------------------
  8292. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8293. {
  8294. // noop
  8295. }
  8296. //---------------------------------------------------------------------------
  8297. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8298. {
  8299. TForm::Dispatch(&Message);
  8300. FSessionColors->Clear();
  8301. AddFixedSessionImages();
  8302. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8303. UpdateImages();
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8307. {
  8308. TForm::Dispatch(&Message);
  8309. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8310. UpdatePixelsPerInchMainWindowCounter();
  8311. }
  8312. //---------------------------------------------------------------------------
  8313. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8314. {
  8315. // Cannot close window while we are busy.
  8316. // We cannot test this in FormCloseQuery as that is called also from
  8317. // Close(), which is called by CloseApplicationAction. So we can be busy
  8318. // there already even, when it is legitimate to close the application.
  8319. // Possibly a better place to handle this would be WMSysCommand.
  8320. if (NonVisualDataModule->Busy)
  8321. {
  8322. Message.Result = 1;
  8323. }
  8324. else
  8325. {
  8326. TForm::Dispatch(&Message);
  8327. }
  8328. }
  8329. //---------------------------------------------------------------------------
  8330. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8331. {
  8332. TForm::Dispatch(&Message);
  8333. if (Showing && (Terminal == NULL))
  8334. {
  8335. // When we are starting minimized (i.e. from an installer),
  8336. // postpone showing Login dialog until we get restored.
  8337. // Otherwise the Login dialog (and Authentication window) show restored
  8338. // over invidible (minimized) main window.
  8339. if (WindowState == wsMinimized)
  8340. {
  8341. FNeedSession = true;
  8342. }
  8343. else
  8344. {
  8345. // This happens before application ever goes idle, so the toolbars would
  8346. // stay enabled (initial state) until the Login dialog is dismissed.
  8347. UpdateActions();
  8348. NonVisualDataModule->StartBusy();
  8349. try
  8350. {
  8351. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8352. // otherwise the dialog does not receive focus.
  8353. // With Commander interface the ProcessMessages is called already
  8354. // by TDriveView, but with Explorer interface, we need to call it explicily
  8355. Application->ProcessMessages();
  8356. // do not reload sessions, they have been loaded just now (optimization)
  8357. NeedSession(false);
  8358. }
  8359. __finally
  8360. {
  8361. NonVisualDataModule->EndBusy();
  8362. }
  8363. }
  8364. }
  8365. }
  8366. //---------------------------------------------------------------------------
  8367. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8368. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8369. int & MaxHeight)
  8370. {
  8371. // workaround for bug in TWinControl.CalcConstraints
  8372. // Check for empty rect (restore from iconic state) is done there only after
  8373. // call to AdjustClientRect, which enlarges the rect (for forms).
  8374. TRect R = GetClientRect();
  8375. // when restoring from iconic state, place no restrictions
  8376. if (IsRectEmpty(R))
  8377. {
  8378. MinWidth = 0;
  8379. MinHeight = 0;
  8380. MaxWidth = 32000;
  8381. MaxHeight = 32000;
  8382. }
  8383. }
  8384. //---------------------------------------------------------------------------
  8385. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8386. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8387. {
  8388. // terminal can be already closed (e.g. dropped connection)
  8389. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8390. {
  8391. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8392. try
  8393. {
  8394. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8395. }
  8396. catch(...)
  8397. {
  8398. if (!Terminal->Active)
  8399. {
  8400. Close = true;
  8401. }
  8402. throw;
  8403. }
  8404. }
  8405. }
  8406. //---------------------------------------------------------------------------
  8407. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8408. {
  8409. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8410. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8411. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8412. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8413. {
  8414. OnGetSpaceAvailable = GetSpaceAvailable;
  8415. }
  8416. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8417. OnGetSpaceAvailable);
  8418. }
  8419. //---------------------------------------------------------------------------
  8420. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8421. {
  8422. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8423. Terminal->FillSessionDataForCode(Data.get());
  8424. return Data.release();
  8425. }
  8426. //---------------------------------------------------------------------------
  8427. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8428. {
  8429. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8430. DoGenerateUrlDialog(Data.get(), Paths);
  8431. }
  8432. //---------------------------------------------------------------------------
  8433. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8434. {
  8435. GenerateUrl(NULL);
  8436. }
  8437. //---------------------------------------------------------------------------
  8438. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8439. {
  8440. std::unique_ptr<TStrings> Paths(new TStringList());
  8441. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8442. GenerateUrl(Paths.get());
  8443. }
  8444. //---------------------------------------------------------------------------
  8445. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8446. {
  8447. FSessionColor = value;
  8448. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8449. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8450. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8451. DebugAssert(ColorItem != NULL);
  8452. ColorItem->Color = C;
  8453. NonVisualDataModule->ColorMenuItem->Color = C;
  8454. UpdateControls();
  8455. }
  8456. //---------------------------------------------------------------------------
  8457. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8458. {
  8459. if (value != FSessionColor)
  8460. {
  8461. UpdateSessionColor(value);
  8462. // Is null when called from LastTerminalClosed
  8463. if (Terminal != NULL)
  8464. {
  8465. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8466. }
  8467. }
  8468. }
  8469. //---------------------------------------------------------------------------
  8470. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8471. {
  8472. bool Result = FNoteTimer->Enabled;
  8473. if (Result)
  8474. {
  8475. // cannot cancel note too early
  8476. bool NotEarly =
  8477. (Now() - FNoteShown >
  8478. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8479. if (Force || NotEarly)
  8480. {
  8481. FNoteTimer->Enabled = false;
  8482. FNote = L"";
  8483. SAFE_DESTROY(FNoteData);
  8484. FOnNoteClick = NULL;
  8485. FNoteHints = FNotes->Text;
  8486. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8487. UpdateStatusBar();
  8488. }
  8489. }
  8490. return Result;
  8491. }
  8492. //---------------------------------------------------------------------------
  8493. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8494. {
  8495. DebugAssert(FNoteTimer->Enabled);
  8496. CancelNote(true);
  8497. }
  8498. //---------------------------------------------------------------------------
  8499. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8500. {
  8501. int P = Note.Pos(L"\n");
  8502. if (P > 0)
  8503. {
  8504. Note.SetLength(P - 1);
  8505. }
  8506. FNotes->Add(FORMAT(L"[%s] %s",
  8507. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8508. while (FNotes->Count > 10)
  8509. {
  8510. FNotes->Delete(0);
  8511. }
  8512. if (UpdateNow)
  8513. {
  8514. FNoteHints = FNotes->Text;
  8515. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8516. UpdateStatusBar();
  8517. }
  8518. }
  8519. //---------------------------------------------------------------------------
  8520. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8521. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8522. {
  8523. int P = Note.Pos(L"\n");
  8524. if (P > 0)
  8525. {
  8526. Note.SetLength(P - 1);
  8527. }
  8528. FNoteHints = FNotes->Text;
  8529. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8530. FNote = Note;
  8531. SAFE_DESTROY(FNoteData);
  8532. FNoteData = NoteData;
  8533. FOnNoteClick = OnNoteClick;
  8534. AddNote(Note, false);
  8535. UpdateStatusBar();
  8536. FNoteShown = Now();
  8537. FNoteTimer->Enabled = false;
  8538. if (Seconds == 0)
  8539. {
  8540. Seconds = WinConfiguration->NotificationsTimeout;
  8541. }
  8542. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8543. FNoteTimer->Enabled = true;
  8544. }
  8545. //---------------------------------------------------------------------------
  8546. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8547. {
  8548. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8549. }
  8550. //---------------------------------------------------------------------------
  8551. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8552. {
  8553. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8554. {
  8555. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8556. }
  8557. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8558. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8559. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8560. 0, NULL, NULL);
  8561. }
  8562. //---------------------------------------------------------------------------
  8563. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8564. {
  8565. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8566. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8567. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8568. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8569. }
  8570. //---------------------------------------------------------------------------
  8571. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8572. {
  8573. WinConfiguration->FormatSizeBytes = Style;
  8574. }
  8575. //---------------------------------------------------------------------------
  8576. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8577. {
  8578. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8579. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8580. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8581. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8582. }
  8583. //---------------------------------------------------------------------------
  8584. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8585. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8586. {
  8587. if (Panel->Index == 0)
  8588. {
  8589. if (FOnNoteClick != NULL)
  8590. {
  8591. // prevent the user data from being freed by possible call
  8592. // to CancelNote or PostNote during call to OnNoteClick
  8593. std::unique_ptr<TObject> NoteData(FNoteData);
  8594. TNotifyEvent OnNoteClick = FOnNoteClick;
  8595. FNoteData = NULL;
  8596. // need to cancel the note as we are going to delete its user data
  8597. CancelNote(true);
  8598. OnNoteClick(NoteData.get());
  8599. }
  8600. }
  8601. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8602. {
  8603. FileSystemInfo();
  8604. }
  8605. }
  8606. //---------------------------------------------------------------------------
  8607. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8608. {
  8609. // workaround:
  8610. // 1) for unknown reason, disabling window, while minimized,
  8611. // prevents it from restoring, even if it was enabled again meanwhile
  8612. // 2) when disabling the main window, while another has focus
  8613. // minimize is no longer possible
  8614. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8615. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8616. // Shouldn't we use IsApplicationMinimized() here?
  8617. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8618. {
  8619. Enabled = false;
  8620. }
  8621. FLockLevel++;
  8622. }
  8623. //---------------------------------------------------------------------------
  8624. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8625. {
  8626. DebugAssert(FLockLevel > 0);
  8627. FLockLevel--;
  8628. if (FLockLevel == 0)
  8629. {
  8630. DebugAssert(FLockSuspendLevel == 0);
  8631. Enabled = true;
  8632. }
  8633. }
  8634. //---------------------------------------------------------------------------
  8635. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8636. {
  8637. // We need to make sure that window is enabled when the last modal window closes
  8638. // otherwise focus is not restored correctly.
  8639. // So we re-enable the window when modal window opens and
  8640. // disable it back after is closes
  8641. if (FLockLevel > 0)
  8642. {
  8643. // while we have nesting counter, we know that we never be called
  8644. // recursivelly as Application->OnModalBegin is called only
  8645. // for the top-level modal window
  8646. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8647. {
  8648. // won't be disabled when conditions in LockWindow() were not satisfied
  8649. FDisabledOnLockSuspend = !Enabled;
  8650. // When minimized to tray (or actually when set to SW_HIDE),
  8651. // setting Enabled makes the window focusable even when there's
  8652. // modal window over it
  8653. if (!FTrayIcon->Visible)
  8654. {
  8655. Enabled = true;
  8656. }
  8657. }
  8658. FLockSuspendLevel++;
  8659. }
  8660. }
  8661. //---------------------------------------------------------------------------
  8662. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8663. {
  8664. if (FLockSuspendLevel > 0)
  8665. {
  8666. DebugAssert(FLockLevel > 0);
  8667. FLockSuspendLevel--;
  8668. // see comment in SuspendWindowLock
  8669. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8670. {
  8671. // Note that window can be enabled here, when we were minized to tray when
  8672. // was SuspendWindowLock() called.
  8673. // We should possibly do the same check as in LockWindow(),
  8674. // if it is ever possible that the consitions change between
  8675. // SuspendWindowLock() and ResumeWindowLock()
  8676. Enabled = !FDisabledOnLockSuspend;
  8677. }
  8678. }
  8679. }
  8680. //---------------------------------------------------------------------------
  8681. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8682. {
  8683. if (!TextOnly)
  8684. {
  8685. TTBXComboBoxItem * RemotePathComboBox =
  8686. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8687. TStrings * Items = RemotePathComboBox->Strings;
  8688. Items->BeginUpdate();
  8689. try
  8690. {
  8691. Items->Clear();
  8692. if (Terminal != NULL)
  8693. {
  8694. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8695. while (!IsUnixRootPath(APath))
  8696. {
  8697. int P = APath.LastDelimiter(L'/');
  8698. DebugAssert(P >= 0);
  8699. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8700. APath.SetLength(P - 1);
  8701. }
  8702. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8703. }
  8704. }
  8705. __finally
  8706. {
  8707. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8708. // Setting ItemIndex to -1 does not reset its text
  8709. if (Items->Count == 0)
  8710. {
  8711. RemotePathComboBox->Text = L"";
  8712. }
  8713. Items->EndUpdate();
  8714. }
  8715. }
  8716. }
  8717. //---------------------------------------------------------------------------
  8718. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8719. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8720. int & ImageIndex)
  8721. {
  8722. if (AIndex < 0)
  8723. {
  8724. AIndex = Sender->ItemIndex;
  8725. }
  8726. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8727. }
  8728. //---------------------------------------------------------------------------
  8729. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8730. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8731. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8732. {
  8733. ARect.Left += (10 * AIndex);
  8734. }
  8735. //---------------------------------------------------------------------------
  8736. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8737. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8738. {
  8739. AWidth += (10 * AIndex);
  8740. }
  8741. //---------------------------------------------------------------------------
  8742. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8743. TObject * Sender)
  8744. {
  8745. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8746. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8747. int Index = RemotePathComboBox->ItemIndex;
  8748. while (Index < RemotePathComboBox->Strings->Count - 1)
  8749. {
  8750. APath = UnixExtractFileDir(APath);
  8751. Index++;
  8752. }
  8753. // VanDyke style paths
  8754. if (APath.IsEmpty())
  8755. {
  8756. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8757. APath = ROOTDIRECTORY;
  8758. }
  8759. if (RemoteDirView->Path != APath)
  8760. {
  8761. RemoteDirView->Path = APath;
  8762. }
  8763. }
  8764. //---------------------------------------------------------------------------
  8765. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8766. {
  8767. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8768. DebugUsedParam(Sender);
  8769. UpdateRemotePathComboBox(true);
  8770. }
  8771. //---------------------------------------------------------------------------
  8772. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8773. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8774. {
  8775. ResetIncrementalSearch();
  8776. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8777. DebugAssert(DirView != NULL);
  8778. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8779. {
  8780. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8781. // OnEditing is called also from TCustomListView::CanEdit
  8782. if (Edit != NULL)
  8783. {
  8784. EditSelectBaseName(Edit);
  8785. }
  8786. }
  8787. }
  8788. //---------------------------------------------------------------------------
  8789. //---------------------------------------------------------------------------
  8790. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8791. {
  8792. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8793. Result->AutoDetectDnD = false;
  8794. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8795. Result->RenderDataOn = rdoEnterAndDropSync;
  8796. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8797. return Result;
  8798. }
  8799. //---------------------------------------------------------------------------
  8800. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8801. {
  8802. TForm::CreateWnd();
  8803. if (FSessionsDragDropFilesEx == NULL)
  8804. {
  8805. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8806. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8807. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8808. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8809. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8810. }
  8811. if (FQueueDragDropFilesEx == NULL)
  8812. {
  8813. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8814. // No need to set OnDragOver as we do not have any restrictions
  8815. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8816. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8817. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8818. }
  8819. }
  8820. //---------------------------------------------------------------------------
  8821. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8822. {
  8823. TForm::DestroyWnd();
  8824. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8825. FQueueDragDropFilesEx->DragDropControl = NULL;
  8826. }
  8827. //---------------------------------------------------------------------------
  8828. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8829. {
  8830. SideEnter(FCurrentSide);
  8831. FEverShown = true;
  8832. }
  8833. //---------------------------------------------------------------------------
  8834. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8835. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8836. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8837. {
  8838. if (!NonVisualDataModule->Busy)
  8839. {
  8840. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8841. Configuration->Usage->Inc(L"FileFinds");
  8842. LockWindow(true);
  8843. NonVisualDataModule->StartBusy();
  8844. WinConfiguration->LockedInterface = true;
  8845. try
  8846. {
  8847. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8848. }
  8849. __finally
  8850. {
  8851. WinConfiguration->LockedInterface = false;
  8852. NonVisualDataModule->EndBusy();
  8853. UnlockWindow();
  8854. }
  8855. }
  8856. }
  8857. //---------------------------------------------------------------------------
  8858. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8859. {
  8860. if (!NonVisualDataModule->Busy)
  8861. {
  8862. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8863. SetFocus();
  8864. RemoteDirView->Path = UnixExtractFilePath(Path);
  8865. UnicodeString FileName = UnixExtractFileName(Path);
  8866. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  8867. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  8868. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  8869. if (Item == NULL)
  8870. {
  8871. ReloadDirectory(osRemote);
  8872. Item = RemoteDirView->FindFileItem(FileName);
  8873. }
  8874. if (Item != NULL)
  8875. {
  8876. RemoteDirView->ItemFocused = Item;
  8877. RemoteDirView->ItemFocused->MakeVisible(false);
  8878. RemoteDirView->SetFocus();
  8879. }
  8880. }
  8881. }
  8882. //---------------------------------------------------------------------------
  8883. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8884. {
  8885. bool Result = !NonVisualDataModule->Busy;
  8886. if (Result)
  8887. {
  8888. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8889. }
  8890. return Result;
  8891. }
  8892. //---------------------------------------------------------------------------
  8893. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8894. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8895. {
  8896. if (CanOperateOnFoundFiles(ATerminal))
  8897. {
  8898. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8899. FOnFileOperationFinished = OnFileOperationFinished;
  8900. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8901. }
  8902. }
  8903. //---------------------------------------------------------------------------
  8904. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8905. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8906. {
  8907. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8908. }
  8909. //---------------------------------------------------------------------------
  8910. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8911. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8912. {
  8913. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8914. }
  8915. //---------------------------------------------------------------------------
  8916. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8917. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8918. {
  8919. if (CanOperateOnFoundFiles(ATerminal))
  8920. {
  8921. for (int Index = 0; Index < FileList->Count; Index++)
  8922. {
  8923. UnicodeString FileName = FileList->Strings[Index];
  8924. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8925. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8926. OnFileOperationFinished(FileName, true);
  8927. }
  8928. }
  8929. }
  8930. //---------------------------------------------------------------------------
  8931. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8932. {
  8933. FFileFindTerminal = Terminal;
  8934. ShowFileFindDialog(
  8935. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8936. DoDownloadFoundFiles, DoEditFoundFiles);
  8937. }
  8938. //---------------------------------------------------------------------------
  8939. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8940. {
  8941. FTaskbarList = TaskbarList;
  8942. }
  8943. //---------------------------------------------------------------------------
  8944. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8945. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8946. {
  8947. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8948. if (Index >= 0)
  8949. {
  8950. if (Button == mbLeft)
  8951. {
  8952. // "Mouse down" is raised only after tab is switched.
  8953. // If switching tab (switching session) takes long enough for user
  8954. // to actually release the button, "mouse down" is still raised,
  8955. // but we do not get "mouse up" event, so dragging is not cancelled,
  8956. // prevent that by not beginning dragging in the first place.
  8957. if (IsKeyPressed(VK_LBUTTON))
  8958. {
  8959. // when user clicks the "+", we get mouse down only after the session
  8960. // is closed, when new session tab is already on X:Y, so dragging
  8961. // starts, prevent that
  8962. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8963. {
  8964. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8965. if (Terminal != NULL)
  8966. {
  8967. SessionsPageControl->BeginDrag(false);
  8968. }
  8969. }
  8970. }
  8971. }
  8972. else if (Button == mbMiddle)
  8973. {
  8974. // ignore middle-click for "New session tab"
  8975. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8976. if (Terminal != NULL)
  8977. {
  8978. CloseSessionTab(Index);
  8979. }
  8980. }
  8981. }
  8982. }
  8983. //---------------------------------------------------------------------------
  8984. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8985. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8986. {
  8987. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8988. // do not allow dropping on the "+" tab
  8989. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8990. if ((TargetTerminal != NULL) &&
  8991. (SessionsPageControl->ActivePage->PageIndex != Index))
  8992. {
  8993. Configuration->Usage->Inc(L"SessionTabMoves");
  8994. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8995. // but we want to actually prevent that to avoid flicker
  8996. SessionsPageControl->ActivePage->PageIndex = Index;
  8997. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8998. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8999. }
  9000. }
  9001. //---------------------------------------------------------------------------
  9002. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9003. TObject * Sender, TObject * Source, int X, int Y,
  9004. TDragState /*State*/, bool & Accept)
  9005. {
  9006. Accept = (Sender == Source);
  9007. if (Accept)
  9008. {
  9009. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9010. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9011. // do not allow dragging to the "+" tab
  9012. Accept = (Terminal != NULL);
  9013. }
  9014. }
  9015. //---------------------------------------------------------------------------
  9016. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9017. const TPoint & Point, int & Effect)
  9018. {
  9019. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9020. if (Index < 0)
  9021. {
  9022. Effect = DROPEFFECT_NONE;
  9023. }
  9024. else
  9025. {
  9026. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9027. // do not allow dropping on the "+" tab
  9028. if (TargetTerminal == NULL)
  9029. {
  9030. Effect = DROPEFFECT_NONE;
  9031. }
  9032. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9033. {
  9034. Effect = DROPEFFECT_COPY;
  9035. }
  9036. }
  9037. }
  9038. //---------------------------------------------------------------------------
  9039. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9040. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9041. {
  9042. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9043. // do not allow dropping on the "+" tab
  9044. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9045. if (TargetTerminal != NULL)
  9046. {
  9047. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9048. if (!IsFileControl(DropSourceControl, osRemote))
  9049. {
  9050. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9051. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9052. // Why don't we use Terminal->CurrentDirectory directly?
  9053. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9054. // do not force queue, drag drop
  9055. false, true);
  9056. }
  9057. }
  9058. }
  9059. //---------------------------------------------------------------------------
  9060. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9061. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9062. {
  9063. // Downloads are handled in RemoteFileControlDDEnd
  9064. if (!IsFileControl(DropSourceControl, osRemote))
  9065. {
  9066. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9067. Terminal->CurrentDirectory,
  9068. // force queue, drag drop
  9069. true, true);
  9070. }
  9071. }
  9072. //---------------------------------------------------------------------------
  9073. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9074. {
  9075. FShowing = false;
  9076. // Do not save empty workspace
  9077. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9078. {
  9079. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9080. UnicodeString Name = WorkspaceName();
  9081. DoSaveWorkspace(
  9082. Name, DataList.get(),
  9083. !Configuration->DisablePasswordStoring &&
  9084. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9085. WinConfiguration->LastStoredSession = Name;
  9086. }
  9087. }
  9088. //---------------------------------------------------------------------------
  9089. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9090. {
  9091. TManagedTerminal * ManagedTerminal =
  9092. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9093. if (ManagedTerminal != NULL)
  9094. {
  9095. ManagedTerminal->DirectoryLoaded = Now();
  9096. }
  9097. }
  9098. //---------------------------------------------------------------------------
  9099. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9100. TListItem * /*Item*/, bool /*Selected*/)
  9101. {
  9102. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9103. switch (DirView->LastSelectMethod)
  9104. {
  9105. case smKeyboard:
  9106. Configuration->Usage->Inc(L"KeyboardSelections");
  9107. break;
  9108. case smMouse:
  9109. Configuration->Usage->Inc(L"MouseSelections");
  9110. break;
  9111. }
  9112. }
  9113. //---------------------------------------------------------------------------
  9114. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9115. {
  9116. if (Color != 0)
  9117. {
  9118. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9119. }
  9120. else
  9121. {
  9122. return FSessionTabImageIndex;
  9123. }
  9124. }
  9125. //---------------------------------------------------------------------------
  9126. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9127. {
  9128. TPngImageCollectionItem * Item =
  9129. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9130. FSessionColors->AddPng(Item->PngImage);
  9131. return FSessionColors->Count - 1;
  9132. }
  9133. //---------------------------------------------------------------------------
  9134. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9135. {
  9136. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9137. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9138. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9139. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9140. }
  9141. //---------------------------------------------------------------------------
  9142. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9143. {
  9144. for (int Index = 0; Index < Control->ControlCount; Index++)
  9145. {
  9146. TControl * AControl = Control->Controls[Index];
  9147. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9148. if (WinControl != NULL)
  9149. {
  9150. CollectItemsWithTextDisplayMode(WinControl);
  9151. }
  9152. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9153. if (Toolbar != NULL)
  9154. {
  9155. // we care for top-level items only
  9156. TTBCustomItem * Items = Toolbar->Items;
  9157. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9158. {
  9159. TTBCustomItem * Item = Items->Items[ItemIndex];
  9160. if (((Item->DisplayMode == nbdmImageAndText) ||
  9161. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9162. EligibleForImageDisplayMode(Item))
  9163. {
  9164. FItemsWithTextDisplayMode.insert(Item);
  9165. }
  9166. }
  9167. }
  9168. }
  9169. }
  9170. //---------------------------------------------------------------------------
  9171. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9172. {
  9173. return true;
  9174. }
  9175. //---------------------------------------------------------------------------
  9176. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9177. {
  9178. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9179. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9180. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9181. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9182. bool Result = true;
  9183. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9184. {
  9185. TTBCustomItem * Item = *i;
  9186. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9187. if (Label != NULL)
  9188. {
  9189. // optimization
  9190. if (Label->Visible == SelectiveToolbarText)
  9191. {
  9192. Result = false;
  9193. }
  9194. else
  9195. {
  9196. Label->Visible = SelectiveToolbarText;
  9197. }
  9198. }
  9199. else
  9200. {
  9201. // optimization
  9202. if (Item->DisplayMode == DisplayMode)
  9203. {
  9204. Result = false;
  9205. }
  9206. else
  9207. {
  9208. Item->DisplayMode = DisplayMode;
  9209. }
  9210. }
  9211. ++i;
  9212. }
  9213. if (Result)
  9214. {
  9215. UpdateNewSessionTab();
  9216. }
  9217. return Result;
  9218. }
  9219. //---------------------------------------------------------------------------
  9220. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9221. {
  9222. DebugFail();
  9223. }
  9224. //---------------------------------------------------------------------------
  9225. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9226. {
  9227. // Among other, a lock level is non zero, while directory is being loaded,
  9228. // even when it happens as a result of dirview navigation,
  9229. // i.e. when TNonVisualDataModule is NOT FBusy.
  9230. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9231. // Among other this prevents a panel auto update to occur while
  9232. // directory is changing.
  9233. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9234. }
  9235. //---------------------------------------------------------------------------
  9236. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9237. {
  9238. // While the window is disabled, we still seem to process menu shortcuts at least,
  9239. // so stop it at least here.
  9240. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9241. return
  9242. (Allowed == aaUpdate) ||
  9243. !NonVisualDataModule->Busy;
  9244. }
  9245. //---------------------------------------------------------------------------
  9246. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9247. {
  9248. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9249. }
  9250. //---------------------------------------------------------------------------
  9251. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9252. {
  9253. // This is somewhat redundant to LockWindow() call from
  9254. // TTerminalManager::TerminalReadDirectoryProgress.
  9255. // But disabling window is known not to block keyboard shorcuts
  9256. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9257. if (Busy > 0)
  9258. {
  9259. if (NonVisualDataModule->Busy)
  9260. {
  9261. State = false;
  9262. }
  9263. else
  9264. {
  9265. NonVisualDataModule->StartBusy();
  9266. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9267. LockWindow();
  9268. }
  9269. }
  9270. else if (Busy < 0)
  9271. {
  9272. UnlockWindow();
  9273. NonVisualDataModule->EndBusy();
  9274. }
  9275. else
  9276. {
  9277. State = NonVisualDataModule->Busy;
  9278. }
  9279. }
  9280. //---------------------------------------------------------------------------
  9281. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9282. {
  9283. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9284. // no context menu for "New session tab"
  9285. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9286. {
  9287. SessionsPageControl->ActivePageIndex = Index;
  9288. if (DebugAlwaysTrue(SessionTabSwitched()))
  9289. {
  9290. // copied from TControl.WMContextMenu
  9291. SendCancelMode(SessionsPageControl);
  9292. // explicit popup instead of using PopupMenu property
  9293. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9294. // while connecting yet not-connected session and hence
  9295. // allowing an access to commands over not-completelly connected session
  9296. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9297. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9298. PopupMenu->PopupComponent = SessionsPageControl;
  9299. PopupMenu->Popup(Point.x, Point.y);
  9300. }
  9301. }
  9302. Handled = true;
  9303. }
  9304. //---------------------------------------------------------------------------
  9305. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9306. {
  9307. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9308. }
  9309. //---------------------------------------------------------------------------
  9310. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9311. {
  9312. Configuration->Usage->Inc(L"PasswordChanges");
  9313. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9314. ChangePasswordData->DisableAuthentationsExceptPassword();
  9315. ChangePasswordData->ChangePassword = true;
  9316. std::unique_ptr<TTerminal> ChangePasswordSession(
  9317. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9318. ChangePasswordSession->AutoReadDirectory = false;
  9319. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9320. {
  9321. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9322. }
  9323. else
  9324. {
  9325. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9326. }
  9327. }
  9328. //---------------------------------------------------------------------------
  9329. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9330. {
  9331. // No nice way to assert SSH2
  9332. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9333. }
  9334. //---------------------------------------------------------------------------
  9335. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9336. {
  9337. TOperationVisualizer OperationVisualizer;
  9338. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9339. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9340. }
  9341. //---------------------------------------------------------------------------
  9342. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9343. {
  9344. TForm::ChangeScale(M, D);
  9345. int APixelsPerInch = GetControlPixelsPerInch(this);
  9346. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9347. try
  9348. {
  9349. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9350. }
  9351. __finally
  9352. {
  9353. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9354. }
  9355. }
  9356. //---------------------------------------------------------------------------
  9357. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9358. {
  9359. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9360. }
  9361. //---------------------------------------------------------------------
  9362. class TPublicControl : public TControl
  9363. {
  9364. friend class TCustomScpExplorerForm;
  9365. };
  9366. //---------------------------------------------------------------------------
  9367. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9368. {
  9369. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9370. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9371. }
  9372. //---------------------------------------------------------------------------
  9373. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9374. {
  9375. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9376. OpenBookmark(Side, Bookmark);
  9377. }
  9378. //---------------------------------------------------------------------------
  9379. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9380. {
  9381. DoBookmarkClick(osLocal, Sender);
  9382. }
  9383. //---------------------------------------------------------------------------
  9384. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9385. {
  9386. DoBookmarkClick(osRemote, Sender);
  9387. }
  9388. //---------------------------------------------------------------------------
  9389. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9390. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9391. {
  9392. // TODO
  9393. if (BookmarkList != NULL)
  9394. {
  9395. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9396. if (!WinConfiguration->UseLocationProfiles)
  9397. {
  9398. std::unique_ptr<TStringList> Directories(new TStringList());
  9399. Directories->CaseSensitive = (Side == osRemote);
  9400. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9401. {
  9402. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9403. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9404. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9405. {
  9406. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9407. Item->Caption = EscapeHotkey(Directory);
  9408. Item->ShortCut = Bookmark->ShortCut;
  9409. Item->OnClick = OnBookmarkClick;
  9410. Item->Tag = reinterpret_cast<int>(Bookmark);
  9411. Directories->Add(Directory);
  9412. Menu->Add(Item.release());
  9413. }
  9414. }
  9415. }
  9416. else
  9417. {
  9418. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9419. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9420. {
  9421. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9422. if (!Bookmark->Node.IsEmpty())
  9423. {
  9424. Folders->Add(Bookmark->Node);
  9425. }
  9426. }
  9427. for (int Index = 0; Index < Folders->Count; Index++)
  9428. {
  9429. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9430. Item->Caption = Folders->Strings[Index];
  9431. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9432. Folders->Objects[Index] = Item.get();
  9433. Menu->Add(Item.release());
  9434. }
  9435. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9436. {
  9437. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9438. TTBCustomItem * Parent;
  9439. if (!Bookmark->Node.IsEmpty())
  9440. {
  9441. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9442. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9443. }
  9444. else
  9445. {
  9446. Parent = Menu;
  9447. }
  9448. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9449. Item->Caption = Bookmark->Name;
  9450. Item->ShortCut = Bookmark->ShortCut;
  9451. Item->OnClick = OnBookmarkClick;
  9452. Item->Tag = reinterpret_cast<int>(Bookmark);
  9453. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9454. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9455. {
  9456. // No hint for location profiles that are actually mere "bookmarks"
  9457. }
  9458. else
  9459. {
  9460. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9461. UnicodeString LongHint;
  9462. if (!Bookmark->Local.IsEmpty())
  9463. {
  9464. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9465. }
  9466. if (!Bookmark->Remote.IsEmpty())
  9467. {
  9468. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9469. }
  9470. AddToList(Hint, LongHint, L"|");
  9471. Item->Hint = Hint;
  9472. }
  9473. Parent->Add(Item.release());
  9474. }
  9475. }
  9476. AddMenuSeparator(Menu);
  9477. }
  9478. }
  9479. //---------------------------------------------------------------------------
  9480. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9481. {
  9482. Menu->Clear();
  9483. std::unique_ptr<TTBCustomItem> Item;
  9484. Item.reset(new TTBXItem(Owner));
  9485. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9486. Item->Options = Item->Options << tboDefault;
  9487. Menu->Add(Item.release());
  9488. AddMenuSeparator(Menu);
  9489. if (Terminal != NULL)
  9490. {
  9491. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9492. }
  9493. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9494. Item.reset(new TTBXItem(Owner));
  9495. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9496. Menu->Add(Item.release());
  9497. }
  9498. //---------------------------------------------------------------------------
  9499. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9500. {
  9501. bool Result = true;
  9502. try
  9503. {
  9504. if (Side == osRemote)
  9505. {
  9506. Terminal->ExceptionOnFail = true;
  9507. }
  9508. try
  9509. {
  9510. DirView(Side)->Path = Path;
  9511. }
  9512. __finally
  9513. {
  9514. if (Side == osRemote)
  9515. {
  9516. Terminal->ExceptionOnFail = false;
  9517. }
  9518. }
  9519. }
  9520. catch (Exception & E)
  9521. {
  9522. if ((Side != osRemote) || Terminal->Active)
  9523. {
  9524. ShowExtendedExceptionEx(Terminal, &E);
  9525. Result = false;
  9526. }
  9527. else
  9528. {
  9529. throw;
  9530. }
  9531. }
  9532. return Result;
  9533. }
  9534. //---------------------------------------------------------------------------
  9535. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9536. {
  9537. // Make sure there some feedback even when loading tiny local folders
  9538. TInstantOperationVisualizer Visualizer;
  9539. DirView(Side)->ReloadDirectory();
  9540. }
  9541. //---------------------------------------------------------------------------
  9542. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9543. {
  9544. if (Index == SessionsPageControl->TabIndex)
  9545. {
  9546. CloseSession();
  9547. }
  9548. else
  9549. {
  9550. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9551. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9552. TTerminalManager * Manager = TTerminalManager::Instance();
  9553. if (!Terminal->Active)
  9554. {
  9555. Manager->FreeTerminal(Terminal);
  9556. }
  9557. else
  9558. {
  9559. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9560. if (CanCloseQueue(Queue))
  9561. {
  9562. Manager->FreeTerminal(Terminal);
  9563. }
  9564. }
  9565. }
  9566. }
  9567. //---------------------------------------------------------------------------
  9568. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9569. {
  9570. CloseSessionTab(Index);
  9571. }
  9572. //---------------------------------------------------------------------------
  9573. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9574. {
  9575. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9576. {
  9577. TInstantOperationVisualizer Visualizer;
  9578. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9579. // before we create a new data object
  9580. ClipboardClear();
  9581. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9582. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9583. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9584. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9585. if (FClipboardDragDropFilesEx.get() == NULL)
  9586. {
  9587. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9588. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9589. }
  9590. FClipboardDragDropFilesEx->FileList->Clear();
  9591. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9592. FClipboardDragDropFilesEx->CopyToClipboard();
  9593. FClipboardTerminal = FTerminal;
  9594. // Need full paths, as cwd can be different once files are pasted
  9595. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9596. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9597. {
  9598. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9599. }
  9600. }
  9601. }
  9602. //---------------------------------------------------------------------------
  9603. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9604. {
  9605. if (OpenClipboard(0))
  9606. {
  9607. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9608. CloseClipboard();
  9609. }
  9610. }
  9611. //---------------------------------------------------------------------------
  9612. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9613. {
  9614. return !FClipboardFakeDirectory.IsEmpty();
  9615. }
  9616. //---------------------------------------------------------------------------
  9617. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9618. {
  9619. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9620. FClipboardFakeDirectory = UnicodeString();
  9621. FClipboardTerminal = NULL;
  9622. // Also called in Idle()
  9623. FClipboardFakeMonitors.reset(NULL);
  9624. }
  9625. //---------------------------------------------------------------------------
  9626. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9627. {
  9628. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9629. {
  9630. ClipboardStop();
  9631. }
  9632. }
  9633. //---------------------------------------------------------------------------
  9634. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9635. {
  9636. TTerminalManager * Manager = TTerminalManager::Instance();
  9637. Manager->ActiveTerminal = FClipboardTerminal;
  9638. TTransferOperationParam Params;
  9639. Params.TargetDirectory = TargetDirectory;
  9640. Params.DragDrop = DragDrop;
  9641. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9642. }
  9643. //---------------------------------------------------------------------------
  9644. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9645. {
  9646. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9647. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9648. {
  9649. RemoveDir(ApiPath(FileName));
  9650. if (!NonVisualDataModule->Busy)
  9651. {
  9652. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9653. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9654. }
  9655. }
  9656. }
  9657. //---------------------------------------------------------------------------
  9658. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9659. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9660. {
  9661. TFileMasks::TParams MaskParams;
  9662. MaskParams.Size = Size;
  9663. MaskParams.Modification = Modification;
  9664. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9665. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9666. {
  9667. bool ImplicitMatch;
  9668. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9669. !ImplicitMatch)
  9670. {
  9671. Color = Iter->Color;
  9672. break;
  9673. }
  9674. }
  9675. }
  9676. //---------------------------------------------------------------------------
  9677. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9678. {
  9679. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9680. if (!DirView->IsEditing())
  9681. {
  9682. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9683. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9684. // while searching
  9685. if (Key == VK_BACK)
  9686. {
  9687. if (!FIncrementalSearch.IsEmpty())
  9688. {
  9689. if (FIncrementalSearch.Length() == 1)
  9690. {
  9691. ResetIncrementalSearch();
  9692. }
  9693. else
  9694. {
  9695. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9696. IncrementalSearch(NewText, false, false);
  9697. }
  9698. Key = 0;
  9699. }
  9700. }
  9701. }
  9702. }
  9703. //---------------------------------------------------------------------------
  9704. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9705. {
  9706. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9707. if (!DirView->IsEditing())
  9708. {
  9709. // Filter control sequences.
  9710. // When not searching yet, prefer use of the space for toggling file selection
  9711. // (so we cannot incrementally search for a string starting with a space).
  9712. if ((Key > VK_SPACE) ||
  9713. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9714. {
  9715. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9716. Key = 0;
  9717. }
  9718. }
  9719. }
  9720. //---------------------------------------------------------------------------
  9721. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9722. {
  9723. if (!FIncrementalSearch.IsEmpty())
  9724. {
  9725. FIncrementalSearch = L"";
  9726. DirView(osCurrent)->UpdateStatusBar();
  9727. }
  9728. }
  9729. //---------------------------------------------------------------------------
  9730. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9731. {
  9732. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9733. if (Item == NULL)
  9734. {
  9735. MessageBeep(MB_ICONHAND);
  9736. }
  9737. else
  9738. {
  9739. TCustomDirView * ADirView = DirView(osCurrent);
  9740. {
  9741. TAutoNestingCounter Guard(FIncrementalSearching);
  9742. ADirView->FocusItem(Item);
  9743. if (ADirView->NortonLike == nlOff)
  9744. {
  9745. if ((ADirView->Selected != Item) ||
  9746. (ADirView->SelCount > 1))
  9747. {
  9748. ADirView->ClearSelection();
  9749. ADirView->Selected = Item;
  9750. }
  9751. }
  9752. }
  9753. FIncrementalSearch = Text;
  9754. Item->MakeVisible(false);
  9755. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9756. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9757. ADirView->UpdateStatusBar();
  9758. }
  9759. }
  9760. //---------------------------------------------------------------------------
  9761. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9762. {
  9763. int Index = Item->Index;
  9764. if (!Reverse)
  9765. {
  9766. Index++;
  9767. if (Index >= Item->Owner->Count)
  9768. {
  9769. Index = 0;
  9770. }
  9771. }
  9772. else
  9773. {
  9774. Index--;
  9775. if (Index < 0)
  9776. {
  9777. Index = Item->Owner->Count - 1;
  9778. }
  9779. }
  9780. return Item->Owner->Item[Index];
  9781. }
  9782. //---------------------------------------------------------------------------
  9783. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9784. {
  9785. TCustomDirView * ADirView = DirView(osCurrent);
  9786. if (ADirView->Items->Count == 0)
  9787. {
  9788. return NULL;
  9789. }
  9790. else
  9791. {
  9792. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9793. TListItem * Item = CurrentItem;
  9794. if (SkipCurrent)
  9795. {
  9796. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9797. }
  9798. while (true)
  9799. {
  9800. bool Matches = false;
  9801. switch (WinConfiguration->PanelSearch)
  9802. {
  9803. case isNameStartOnly:
  9804. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9805. break;
  9806. case isName:
  9807. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9808. break;
  9809. case isAll:
  9810. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9811. int Index = 0;
  9812. while ((Index < ColCount) && !Matches)
  9813. {
  9814. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9815. if (Visible)
  9816. {
  9817. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9818. }
  9819. Index++;
  9820. }
  9821. break;
  9822. }
  9823. if (Matches)
  9824. {
  9825. return Item;
  9826. }
  9827. Item = GetNextFile(Item, Reverse);
  9828. if (Item == CurrentItem)
  9829. {
  9830. return NULL;
  9831. }
  9832. }
  9833. }
  9834. }
  9835. //---------------------------------------------------------------------------
  9836. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9837. {
  9838. ResetIncrementalSearch();
  9839. }
  9840. //---------------------------------------------------------------------------
  9841. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9842. {
  9843. if (Message.CharCode == VK_TAB)
  9844. {
  9845. if (!FIncrementalSearch.IsEmpty())
  9846. {
  9847. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  9848. bool Reverse = Shift.Contains(ssShift);
  9849. IncrementalSearch(FIncrementalSearch, true, Reverse);
  9850. Message.Result = 1;
  9851. return;
  9852. }
  9853. }
  9854. else if (Message.CharCode == VK_ESCAPE)
  9855. {
  9856. if (!FIncrementalSearch.IsEmpty())
  9857. {
  9858. ResetIncrementalSearch();
  9859. Message.Result = 1;
  9860. return;
  9861. }
  9862. }
  9863. TForm::Dispatch(&Message);
  9864. }
  9865. //---------------------------------------------------------------------------
  9866. void __fastcall TCustomScpExplorerForm::Deactivate()
  9867. {
  9868. TForm::Deactivate();
  9869. // E.g. when switching to an internal editor window
  9870. ResetIncrementalSearch();
  9871. }
  9872. //---------------------------------------------------------------------------
  9873. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  9874. {
  9875. // When switching to another application
  9876. ResetIncrementalSearch();
  9877. }
  9878. //---------------------------------------------------------------------------
  9879. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  9880. {
  9881. ResetIncrementalSearch();
  9882. }
  9883. //---------------------------------------------------------------------------
  9884. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  9885. {
  9886. if (FIncrementalSearching <= 0)
  9887. {
  9888. ResetIncrementalSearch();
  9889. }
  9890. }
  9891. //---------------------------------------------------------------------------
  9892. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  9893. {
  9894. CountClicksForWindowPrint(this);
  9895. }
  9896. //---------------------------------------------------------------------------