CustomScpExplorer.cpp 343 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. class TAutoBatch
  119. {
  120. public:
  121. TAutoBatch(TCustomScpExplorerForm * Form) :
  122. FForm(Form)
  123. {
  124. FForm->BatchStart(FBatchStorage);
  125. }
  126. ~TAutoBatch()
  127. {
  128. FForm->BatchEnd(FBatchStorage);
  129. }
  130. private:
  131. TCustomScpExplorerForm * FForm;
  132. void * FBatchStorage;
  133. };
  134. //---------------------------------------------------------------------------
  135. struct TTransferOperationParam
  136. {
  137. TTransferOperationParam();
  138. UnicodeString TargetDirectory;
  139. bool Temp;
  140. bool DragDrop;
  141. int Options;
  142. TAutoSwitch Queue;
  143. };
  144. //---------------------------------------------------------------------------
  145. TTransferOperationParam::TTransferOperationParam()
  146. {
  147. Temp = false;
  148. DragDrop = false;
  149. Options = 0;
  150. Queue = asAuto;
  151. }
  152. //---------------------------------------------------------------------------
  153. class TTransferPresetNoteData : public TObject
  154. {
  155. public:
  156. UnicodeString Message;
  157. };
  158. //---------------------------------------------------------------------------
  159. class TTerminalNoteData : public TObject
  160. {
  161. public:
  162. TTerminal * Terminal;
  163. };
  164. //---------------------------------------------------------------------------
  165. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  166. FFormRestored(false),
  167. TForm(Owner)
  168. {
  169. FInvalid = false;
  170. FCurrentSide = osRemote;
  171. FEverShown = false;
  172. FDocks = new TList();
  173. InitControls();
  174. RestoreParams();
  175. ConfigurationChanged();
  176. RemoteDirView->Invalidate();
  177. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  178. NonVisualDataModule->ScpExplorer = this;
  179. FAutoOperation = false;
  180. FOnFileOperationFinished = NULL;
  181. FForceExecution = false;
  182. FIgnoreNextDialogChar = 0;
  183. FErrorList = NULL;
  184. FSynchronizeProgressForm = NULL;
  185. FProgressForm = NULL;
  186. FRefreshLocalDirectory = false;
  187. FRefreshRemoteDirectory = false;
  188. FDDExtMapFile = NULL;
  189. // CreateMutexW keeps failing with ERROR_NOACCESS
  190. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  191. DebugAssert(FDDExtMutex != NULL);
  192. FDDTargetControl = NULL;
  193. FDelayedDeletionTimer = NULL;
  194. FDelayedDeletionList = new TStringList();
  195. FDDFileList = NULL;
  196. FPendingTempSpaceWarn = false;
  197. FCapturedLog = NULL;
  198. FDragDropOperation = false;
  199. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  200. FAllowTransferPresetAutoSelect = true;
  201. FCopyParamDefault = L"";
  202. FSynchronizeController = NULL;
  203. FPendingQueueActionItem = NULL;
  204. FLockLevel = 0;
  205. FLockSuspendLevel = 0;
  206. FDisabledOnLockSuspend = false;
  207. FAlternativeDelete = false;
  208. FTrayIcon = new ::TTrayIcon(0);
  209. FTrayIcon->OnClick = TrayIconClick;
  210. FMaxQueueLength = 0;
  211. FLastContextPopupScreenPoint = TPoint(-1, -1);
  212. FTransferResumeList = NULL;
  213. FMoveToQueue = false;
  214. FStandaloneEditing = false;
  215. FOnFeedSynchronizeError = NULL;
  216. FNeedSession = false;
  217. FDoNotIdleCurrentTerminal = 0;
  218. FIncrementalSearching = 0;
  219. FIncrementalSearchHaveNext = false;
  220. FEditorManager = new TEditorManager();
  221. FEditorManager->OnFileChange = ExecutedFileChanged;
  222. FEditorManager->OnFileReload = ExecutedFileReload;
  223. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  224. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  225. FLocalEditors = new TList();
  226. FQueueStatus = NULL;
  227. FQueueStatusSection = new TCriticalSection();
  228. FQueueStatusInvalidated = false;
  229. FQueueItemInvalidated = false;
  230. FQueueActedItem = NULL;
  231. FQueueController = new TQueueController(QueueView3);
  232. FUserActionTimer = new TTimer(this);
  233. FUserActionTimer->Enabled = false;
  234. FUserActionTimer->Interval = 10;
  235. FUserActionTimer->OnTimer = UserActionTimer;
  236. FNotes = new TStringList();
  237. FNoteTimer = new TTimer(this);
  238. FNoteTimer->Enabled = false;
  239. FNoteTimer->OnTimer = NoteTimer;
  240. FOnNoteClick = NULL;
  241. FOle32Library = LoadLibrary(L"Ole32.dll");
  242. FDragMoveCursor = FOle32Library != NULL ?
  243. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  244. UseSystemSettings(this);
  245. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  246. static_cast<TObject*>(GetComponent(fcTransferList)));
  247. DebugAssert(TransferList != NULL);
  248. FTransferListHoverIndex = -1;
  249. TransferList->OnChange = TransferListChange;
  250. TransferList->OnDrawItem = TransferListDrawItem;
  251. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  252. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  253. UseDesktopFont(SessionsPageControl);
  254. UpdateSessionsPageControlHeight();
  255. UseDesktopFont(RemoteDirView);
  256. UseDesktopFont(RemoteDriveView);
  257. UseDesktopFont(QueueView3);
  258. UseDesktopFont(QueueLabel);
  259. UseDesktopFont(RemoteStatusBar);
  260. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  261. QueueSplitter->ShowHint = true;
  262. RemotePanelSplitter->ShowHint = true;
  263. UpdateImages();
  264. FCustomCommandMenu = CreateTBXPopupMenu(this);
  265. FCustomCommandLocalFileList = NULL;
  266. FCustomCommandRemoteFileList = NULL;
  267. FSessionColors = new TPngImageList(this);
  268. FSessionColors->ColorDepth = cd32Bit;
  269. AddFixedSessionImages();
  270. SessionsPageControl->Images = FSessionColors;
  271. QueueLabel->FocusControl = QueueView3;
  272. UpdateQueueLabel();
  273. FRemoteDirViewWasFocused = true;
  274. CreateHiddenWindow();
  275. StartUpdates();
  276. }
  277. //---------------------------------------------------------------------------
  278. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  279. {
  280. FInvalid = true;
  281. // this has to be one of the very first things to do
  282. StopUpdateThread();
  283. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  284. TTerminalManager::Instance()->ScpExplorer = NULL;
  285. delete FCustomCommandLocalFileList;
  286. delete FCustomCommandRemoteFileList;
  287. delete FCustomCommandMenu;
  288. delete FTrayIcon;
  289. FTrayIcon = NULL;
  290. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  291. delete FEditorManager;
  292. ForceCloseLocalEditors();
  293. delete FLocalEditors;
  294. HideFileFindDialog();
  295. if (FDelayedDeletionTimer)
  296. {
  297. DoDelayedDeletion(NULL);
  298. SAFE_DESTROY(FDelayedDeletionTimer);
  299. }
  300. SAFE_DESTROY(FDelayedDeletionList);
  301. // sometimes we do not get DDEnd so the list is not released
  302. SAFE_DESTROY(FDDFileList);
  303. DebugAssert(FSynchronizeController == NULL);
  304. CloseHandle(FDDExtMutex);
  305. FDDExtMutex = NULL;
  306. FreeLibrary(FOle32Library);
  307. FOle32Library = NULL;
  308. FDragMoveCursor = NULL;
  309. DebugAssert(!FErrorList);
  310. if (FEverShown)
  311. {
  312. // when window is never shown (like when running command-line operation),
  313. // particularly window site is not restored correctly (BoundsRect value set
  314. // in RestoreForm gets lost during handle allocation), so we do not want
  315. // it to be stored
  316. StoreParams();
  317. }
  318. Terminal = NULL;
  319. Queue = NULL;
  320. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  321. NonVisualDataModule->ScpExplorer = NULL;
  322. delete FQueueController;
  323. FQueueController = NULL;
  324. delete FQueueStatusSection;
  325. FQueueStatusSection = NULL;
  326. delete FQueueStatus;
  327. FQueueStatus = NULL;
  328. delete FUserActionTimer;
  329. FUserActionTimer = NULL;
  330. delete FNoteTimer;
  331. delete FNotes;
  332. delete FNoteData;
  333. SAFE_DESTROY(FDocks);
  334. SessionsPageControl->Images = NULL;
  335. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  336. SAFE_DESTROY(FSessionColors);
  337. SAFE_DESTROY(FSessionsDragDropFilesEx);
  338. SAFE_DESTROY(FHistoryMenu[0][0]);
  339. SAFE_DESTROY(FHistoryMenu[0][1]);
  340. SAFE_DESTROY(FHistoryMenu[1][0]);
  341. SAFE_DESTROY(FHistoryMenu[1][1]);
  342. if (FHiddenWindow != NULL)
  343. {
  344. DestroyWindow(FHiddenWindow);
  345. FHiddenWindow = NULL;
  346. }
  347. }
  348. //---------------------------------------------------------------------------
  349. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  350. {
  351. std::unique_ptr<TSessionData> Data;
  352. if (!Session.IsEmpty())
  353. {
  354. bool DefaultsOnly;
  355. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  356. }
  357. TTerminalManager * Manager = TTerminalManager::Instance();
  358. for (int Index = 0; Index < Manager->Count; Index++)
  359. {
  360. TTerminal * Terminal = Manager->Terminals[Index];
  361. if (Session.IsEmpty() ||
  362. Terminal->SessionData->IsSameSite(Data.get()))
  363. {
  364. if (Path.IsEmpty())
  365. {
  366. Terminal->ClearCaches();
  367. }
  368. else
  369. {
  370. Terminal->DirectoryModified(Path, true);
  371. }
  372. }
  373. }
  374. // We should flag a pending refresh for the background terminals or busy foreground terminals
  375. if ((Terminal != NULL) && Terminal->Active &&
  376. CanCommandLineFromAnotherInstance() &&
  377. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  378. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  379. {
  380. Terminal->ReloadDirectory();
  381. }
  382. }
  383. //---------------------------------------------------------------------------
  384. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  385. {
  386. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  387. size_t MessageSize = sizeof(TCopyDataMessage);
  388. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  389. if (Result)
  390. {
  391. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  392. Result = (Message.Version == TCopyDataMessage::Version1);
  393. if (Result)
  394. {
  395. switch (Message.Command)
  396. {
  397. case TCopyDataMessage::CommandCanCommandLine:
  398. Result = CanCommandLineFromAnotherInstance();
  399. break;
  400. case TCopyDataMessage::CommandCommandLine:
  401. {
  402. UnicodeString CommandLine(Message.CommandLine);
  403. Result = CommandLineFromAnotherInstance(CommandLine);
  404. }
  405. break;
  406. case TCopyDataMessage::RefreshPanel:
  407. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  408. break;
  409. case TCopyDataMessage::MainWindowCheck:
  410. Result = true;
  411. break;
  412. default:
  413. Result = false;
  414. break;
  415. }
  416. }
  417. }
  418. Message.Result = Result ? 1 : 0;
  419. }
  420. //---------------------------------------------------------------------------
  421. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  422. {
  423. WNDCLASS WindowClass = {0};
  424. WindowClass.lpfnWndProc = DefWindowProc;
  425. WindowClass.hInstance = HInstance;
  426. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  427. FHiddenWindow = NULL;
  428. if (RegisterClass(&WindowClass))
  429. {
  430. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  431. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  432. }
  433. }
  434. //---------------------------------------------------------------------------
  435. bool __fastcall TCustomScpExplorerForm::CanConsole()
  436. {
  437. return
  438. (Terminal != NULL) &&
  439. Terminal->Active &&
  440. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  441. }
  442. //---------------------------------------------------------------------------
  443. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  444. {
  445. bool Result = Visible && !NonVisualDataModule->Busy;
  446. return Result;
  447. }
  448. //---------------------------------------------------------------------------
  449. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  450. const UnicodeString & CommandLine)
  451. {
  452. TProgramParams Params(CommandLine);
  453. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  454. if (Result)
  455. {
  456. NonVisualDataModule->StartBusy();
  457. try
  458. {
  459. // this action is initiated from another process,
  460. // so it's likely that our window is not visible,
  461. // and user won't see what is going on
  462. Application->BringToFront();
  463. // reload sessions as we may be asked to open a session
  464. // just stored by another instance
  465. StoredSessions->Reload();
  466. UnicodeString SessionName = Params.Param[1];
  467. std::unique_ptr<TObjectList> DataList(new TObjectList());
  468. UnicodeString DownloadFile; // unused
  469. try
  470. {
  471. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  472. }
  473. catch (EAbort &)
  474. {
  475. // Happens when opening session in PuTTY and in other situations,
  476. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  477. // to process the command-line on its own (or to pass it to yet another instance)
  478. }
  479. if (DataList->Count > 0)
  480. {
  481. TTerminalManager * Manager = TTerminalManager::Instance();
  482. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  483. }
  484. }
  485. __finally
  486. {
  487. NonVisualDataModule->EndBusy();
  488. }
  489. }
  490. return Result;
  491. }
  492. //---------------------------------------------------------------------------
  493. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  494. {
  495. if (FTerminal != value)
  496. {
  497. TerminalChanging();
  498. DoSetTerminal(value, false);
  499. }
  500. }
  501. //---------------------------------------------------------------------------
  502. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TTerminal * value, bool Replace)
  503. {
  504. FTerminal = value;
  505. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  506. FAllowTransferPresetAutoSelect = false;
  507. try
  508. {
  509. TerminalChanged(Replace);
  510. }
  511. __finally
  512. {
  513. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  514. }
  515. if (Terminal != NULL)
  516. {
  517. TransferPresetAutoSelect();
  518. }
  519. }
  520. //---------------------------------------------------------------------------
  521. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TTerminal * value)
  522. {
  523. DoSetTerminal(value, true);
  524. }
  525. //---------------------------------------------------------------------------
  526. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  527. {
  528. if (FTerminal != NULL)
  529. {
  530. UpdateTerminal(Terminal);
  531. }
  532. }
  533. //---------------------------------------------------------------------------
  534. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  535. {
  536. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  537. if (ManagedTerminal != NULL)
  538. {
  539. UpdateSessionColor((TColor)ManagedTerminal->StateData->Color);
  540. }
  541. DoTerminalListChanged();
  542. if (Replaced)
  543. {
  544. RemoteDirView->ReplaceTerminal(Terminal);
  545. }
  546. else
  547. {
  548. RemoteDirView->Terminal = Terminal;
  549. }
  550. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  551. if (Terminal != NULL)
  552. {
  553. if (Terminal->Active)
  554. {
  555. Terminal->RefreshDirectory();
  556. }
  557. DebugAssert(ManagedTerminal == Terminal);
  558. if (WinConfiguration->PreservePanelState)
  559. {
  560. if (ManagedTerminal->RemoteExplorerState != NULL)
  561. {
  562. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  563. }
  564. else
  565. {
  566. RemoteDirView->ClearState();
  567. }
  568. }
  569. if (!Replaced)
  570. {
  571. InitStatusBar();
  572. }
  573. }
  574. UpdateTransferList();
  575. // Update panels Enable state before refreshing the labels
  576. UpdateControls();
  577. }
  578. //---------------------------------------------------------------------------
  579. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  580. {
  581. if (Queue != value)
  582. {
  583. if (FQueue != NULL)
  584. {
  585. FQueue->OnListUpdate = NULL;
  586. FQueue->OnQueueItemUpdate = NULL;
  587. }
  588. FQueue = value;
  589. if (FQueue != NULL)
  590. {
  591. DebugAssert(FQueue->OnListUpdate == NULL);
  592. FQueue->OnListUpdate = QueueListUpdate;
  593. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  594. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  595. }
  596. QueueChanged();
  597. }
  598. }
  599. //---------------------------------------------------------------------------
  600. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  601. TListItem * Item)
  602. {
  603. if (FQueueActedItem == Item)
  604. {
  605. FQueueActedItem = NULL;
  606. if ((QueueView3->PopupMenu != NULL) &&
  607. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  608. {
  609. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  610. ReleaseCapture();
  611. }
  612. }
  613. if (Item->Data == FPendingQueueActionItem)
  614. {
  615. FPendingQueueActionItem = NULL;
  616. }
  617. }
  618. //---------------------------------------------------------------------------
  619. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  620. {
  621. {
  622. TGuard Guard(FQueueStatusSection);
  623. FQueueStatusInvalidated = false;
  624. if (FQueue != NULL)
  625. {
  626. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  627. }
  628. }
  629. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  630. {
  631. FMaxQueueLength = FQueueStatus->Count;
  632. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  633. }
  634. FQueueController->UpdateQueueStatus(FQueueStatus);
  635. SetQueueProgress();
  636. UpdateQueueView();
  637. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  638. if (IsEmpty && (Terminal != NULL))
  639. {
  640. TOnceDoneOperation OnceDoneOperation =
  641. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  642. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  643. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  644. {
  645. FQueue->Enabled = false;
  646. }
  647. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  648. {
  649. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  650. }
  651. }
  652. }
  653. //---------------------------------------------------------------------------
  654. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  655. {
  656. UnicodeString Result;
  657. if (FQueueStatus != NULL)
  658. {
  659. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  660. {
  661. TFileOperationProgressType * ProgressData =
  662. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  663. if ((ProgressData != NULL) && ProgressData->InProgress)
  664. {
  665. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  666. }
  667. }
  668. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  669. {
  670. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  671. }
  672. }
  673. return Result;
  674. }
  675. //---------------------------------------------------------------------------
  676. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  677. {
  678. ComponentVisible[fcQueueView] =
  679. (WinConfiguration->QueueView.Show == qvShow) ||
  680. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  681. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  682. }
  683. //---------------------------------------------------------------------------
  684. void __fastcall TCustomScpExplorerForm::QueueChanged()
  685. {
  686. if (FQueueStatus != NULL)
  687. {
  688. delete FQueueStatus;
  689. FQueueStatus = NULL;
  690. }
  691. UpdateQueueStatus(true);
  692. }
  693. //---------------------------------------------------------------------------
  694. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  695. {
  696. if (FQueue == Queue)
  697. {
  698. FQueueStatusInvalidated = true;
  699. }
  700. }
  701. //---------------------------------------------------------------------------
  702. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  703. TQueueItem * Item)
  704. {
  705. if (FQueue == Queue)
  706. {
  707. TGuard Guard(FQueueStatusSection);
  708. // this may be running in parallel with QueueChanged
  709. if (FQueueStatus != NULL)
  710. {
  711. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  712. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  713. {
  714. FRefreshLocalDirectory = (QueueItem == NULL) ||
  715. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  716. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  717. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  718. }
  719. if (QueueItem != NULL)
  720. {
  721. QueueItem->UserData = (void*)true;
  722. FQueueItemInvalidated = true;
  723. }
  724. }
  725. }
  726. }
  727. //---------------------------------------------------------------------------
  728. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  729. {
  730. // during standalone editing, we have no way to see/control queue,
  731. // so we have to always popup prompts automatically
  732. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  733. }
  734. //---------------------------------------------------------------------------
  735. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  736. {
  737. if (FQueueStatus != NULL)
  738. {
  739. bool QueueAutoPopup = IsQueueAutoPopup();
  740. bool NeedRefresh = FQueueController->NeedRefresh();
  741. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  742. FQueueItemInvalidated = false;
  743. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  744. bool Updated = false;
  745. TQueueItemProxy * QueueItem;
  746. bool WasUserAction;
  747. for (int Index = 0; Index < Limit; Index++)
  748. {
  749. bool Update = false;
  750. QueueItem = FQueueStatus->Items[Index];
  751. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  752. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  753. (FPendingQueueActionItem == NULL))
  754. {
  755. FPendingQueueActionItem = QueueItem;
  756. FUserActionTimer->Enabled = true;
  757. }
  758. if ((bool)QueueItem->UserData)
  759. {
  760. QueueItem->UserData = (void*)false;
  761. QueueItem->Update();
  762. Updated = true;
  763. Update = true;
  764. }
  765. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  766. {
  767. Update = true;
  768. }
  769. if (Update)
  770. {
  771. FQueueController->RefreshQueueItem(QueueItem);
  772. }
  773. }
  774. if (Updated)
  775. {
  776. NonVisualDataModule->UpdateNonVisibleActions();
  777. SetQueueProgress();
  778. }
  779. }
  780. }
  781. //---------------------------------------------------------------------------
  782. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  783. {
  784. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  785. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  786. }
  787. //---------------------------------------------------------------------------
  788. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  789. {
  790. if (ProgressData->Operation != foCalculateSize)
  791. {
  792. int OverallProgress;
  793. // FProgressForm is null when this is called from SetQueueProgress
  794. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  795. {
  796. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  797. }
  798. else
  799. {
  800. OverallProgress = ProgressData->OverallProgress();
  801. }
  802. // implies TBPF_NORMAL
  803. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  804. }
  805. else
  806. {
  807. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  808. }
  809. }
  810. //---------------------------------------------------------------------------
  811. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  812. {
  813. TTerminalManager::Instance()->QueueStatusUpdated();
  814. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  815. {
  816. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  817. {
  818. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  819. {
  820. TFileOperationProgressType * ProgressData = NULL;
  821. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  822. {
  823. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  824. }
  825. if ((ProgressData != NULL) &&
  826. ProgressData->InProgress)
  827. {
  828. SetTaskbarListProgressValue(ProgressData);
  829. }
  830. else
  831. {
  832. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  833. }
  834. }
  835. else
  836. {
  837. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  838. }
  839. }
  840. else
  841. {
  842. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  843. }
  844. }
  845. UpdateQueueLabel();
  846. }
  847. //---------------------------------------------------------------------------
  848. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  849. {
  850. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  851. if (FQueueStatus != NULL)
  852. {
  853. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  854. if (ActiveAndPendingPrimaryCount > 0)
  855. {
  856. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  857. }
  858. }
  859. QueueLabel->Caption = Caption;
  860. }
  861. //---------------------------------------------------------------------------
  862. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  863. {
  864. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  865. static_cast<TComponent*>(GetComponent(fcTransferList)));
  866. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  867. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  868. TransferList->Strings->BeginUpdate();
  869. try
  870. {
  871. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  872. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  873. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  874. if (FTransferDropDownHint.IsEmpty())
  875. {
  876. FTransferDropDownHint = TransferDropDown->Hint;
  877. }
  878. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  879. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  880. TransferDropDown->Text = StripHotkey(Name);
  881. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  882. (FTransferDropDownHint, StripHotkey(Name),
  883. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  884. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  885. // update the label, otherwise when it is updated only on the first draw
  886. // of the list, it is drawn "bold" for some reason
  887. FTransferListHoverIndex = TransferList->ItemIndex;
  888. UpdateTransferLabel();
  889. }
  890. __finally
  891. {
  892. TransferList->Strings->EndUpdate();
  893. }
  894. }
  895. //---------------------------------------------------------------------------
  896. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  897. {
  898. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  899. DebugAssert(Toolbar != NULL);
  900. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  901. }
  902. //---------------------------------------------------------------------------
  903. void __fastcall TCustomScpExplorerForm::UpdateActions()
  904. {
  905. TForm::UpdateActions();
  906. if (ComponentVisible[fcCustomCommandsBand])
  907. {
  908. UpdateCustomCommandsToolbar();
  909. }
  910. }
  911. //---------------------------------------------------------------------------
  912. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  913. {
  914. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  915. }
  916. //---------------------------------------------------------------------------
  917. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  918. {
  919. Color = GetBtnFaceColor();
  920. DebugAssert(Configuration && RemoteDirView);
  921. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  922. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  923. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  924. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  925. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  926. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  927. {
  928. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  929. // selection is not redrawn automatically when RowSelect changes
  930. RemoteDirView->Invalidate();
  931. }
  932. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  933. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  934. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  935. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  936. UpdateSessionsPageControlHeight();
  937. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  938. UpdateToolbarDisplayMode();
  939. UpdateTransferList();
  940. if (Terminal != NULL)
  941. {
  942. TransferPresetAutoSelect();
  943. }
  944. UpdateQueueView();
  945. UpdateControls();
  946. // this can be called even before constuctor finishes.
  947. if (FEditorManager != NULL)
  948. {
  949. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  950. }
  951. // this can be called even before constuctor finishes.
  952. if (FLocalEditors != NULL)
  953. {
  954. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  955. {
  956. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  957. }
  958. }
  959. if (ComponentVisible[fcCustomCommandsBand])
  960. {
  961. UpdateCustomCommandsToolbar();
  962. }
  963. // show only when keeping queue items forever,
  964. // otherwise, its enough to have in in the context menu
  965. QueueDeleteAllDoneQueueToolbarItem->Visible =
  966. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  967. if (FFileColorsCurrent != WinConfiguration->FileColors)
  968. {
  969. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  970. FFileColorsCurrent = WinConfiguration->FileColors;
  971. FileColorsChanged();
  972. }
  973. }
  974. //---------------------------------------------------------------------------
  975. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  976. {
  977. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  978. DirView->Invalidate();
  979. }
  980. //---------------------------------------------------------------------------
  981. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  982. {
  983. DoFileColorsChanged(RemoteDirView);
  984. }
  985. //---------------------------------------------------------------------------
  986. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  987. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  988. void * /*Arg*/)
  989. {
  990. if (Token != NULL)
  991. {
  992. TForm * Editor = dynamic_cast<TForm*>(Token);
  993. DebugAssert(Editor != NULL);
  994. ReconfigureEditorForm(Editor);
  995. }
  996. }
  997. //---------------------------------------------------------------------------
  998. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  999. {
  1000. WinConfiguration->DDTransferConfirmation = asOn;
  1001. }
  1002. //---------------------------------------------------------------------------
  1003. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1004. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1005. {
  1006. // noop
  1007. }
  1008. //---------------------------------------------------------------------------
  1009. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1010. TTransferDirection Direction, TTransferType Type, bool Temp,
  1011. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1012. bool Confirm, bool DragDrop, int Options)
  1013. {
  1014. bool Result = true;
  1015. DebugAssert(Terminal && Terminal->Active);
  1016. // these parameters are known in advance
  1017. int Params =
  1018. FLAGMASK(Type == ttMove, cpDelete);
  1019. bool ToTemp = (Temp && (Direction == tdToLocal));
  1020. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1021. {
  1022. bool DisableNewerOnly =
  1023. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1024. ToTemp;
  1025. Options |=
  1026. FLAGMASK(ToTemp, coTemp) |
  1027. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1028. coDoNotShowAgain;
  1029. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1030. int CopyParamAttrs = (Direction == tdToRemote ?
  1031. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1032. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1033. int OutputOptions =
  1034. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1035. cooDoNotShowAgain);
  1036. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1037. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1038. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1039. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1040. if (Result)
  1041. {
  1042. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1043. {
  1044. if (DragDrop)
  1045. {
  1046. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1047. {
  1048. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1049. NULL, 0, EnableDDTransferConfirmation, NULL);
  1050. }
  1051. WinConfiguration->DDTransferConfirmation = asOff;
  1052. }
  1053. else
  1054. {
  1055. WinConfiguration->ConfirmTransferring = false;
  1056. }
  1057. }
  1058. else
  1059. {
  1060. // User explicitly unchecked "do not show again",
  1061. // so show him the dialog the next time
  1062. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1063. {
  1064. WinConfiguration->DDTransferConfirmation = asOn;
  1065. }
  1066. }
  1067. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1068. }
  1069. }
  1070. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1071. {
  1072. Configuration->Usage->Inc(L"TransfersOnBackground");
  1073. // these parameter are known only after transfer dialog
  1074. Params |=
  1075. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1076. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1077. Result = false;
  1078. ClearTransferSourceSelection(Direction);
  1079. }
  1080. return Result;
  1081. }
  1082. //---------------------------------------------------------------------------
  1083. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1084. {
  1085. if (FOnFileOperationFinished != NULL)
  1086. {
  1087. FOnFileOperationFinished(UnicodeString(), true);
  1088. }
  1089. else
  1090. {
  1091. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1092. if (HasDirView[Side])
  1093. {
  1094. DirView(Side)->SelectAll(smNone);
  1095. }
  1096. }
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1100. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1101. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1102. int Params)
  1103. {
  1104. DebugAssert(Queue != NULL);
  1105. bool SingleFile = false;
  1106. if (FileList->Count == 1)
  1107. {
  1108. if (Direction == tdToRemote)
  1109. {
  1110. UnicodeString FileName = FileList->Strings[0];
  1111. SingleFile = FileExists(ApiPath(FileName));
  1112. }
  1113. else
  1114. {
  1115. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1116. SingleFile = !File->IsDirectory;
  1117. }
  1118. }
  1119. TQueueItem * QueueItem;
  1120. if (Direction == tdToRemote)
  1121. {
  1122. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1123. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1124. }
  1125. else
  1126. {
  1127. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1128. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1129. }
  1130. AddQueueItem(Queue, QueueItem, Terminal);
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1134. {
  1135. if (Queue->IsEmpty)
  1136. {
  1137. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1138. ManagedTerminal->QueueOperationStart = Now();
  1139. }
  1140. Queue->AddItem(QueueItem);
  1141. }
  1142. //---------------------------------------------------------------------------
  1143. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1144. {
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall TCustomScpExplorerForm::InitControls()
  1148. {
  1149. DebugAssert(FDocks != NULL);
  1150. for (int Index = 0; Index < ComponentCount; Index++)
  1151. {
  1152. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1153. if ((Dock != NULL) && (Dock->Tag == 0))
  1154. {
  1155. FDocks->Add(Dock);
  1156. }
  1157. }
  1158. CollectItemsWithTextDisplayMode(this);
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1162. {
  1163. ConfigurationChanged();
  1164. QueuePanel->Height =
  1165. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1166. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1167. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1168. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1169. }
  1170. //---------------------------------------------------------------------------
  1171. void __fastcall TCustomScpExplorerForm::StoreParams()
  1172. {
  1173. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1174. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1175. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1176. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1177. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1178. }
  1179. //---------------------------------------------------------------------------
  1180. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1181. {
  1182. if (!FFormRestored)
  1183. {
  1184. FFormRestored = true;
  1185. RestoreFormParams();
  1186. }
  1187. TForm::CreateParams(Params);
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1191. {
  1192. DebugAssert(FDocks != NULL);
  1193. for (int Index = 0; Index < FDocks->Count; Index++)
  1194. {
  1195. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1196. }
  1197. }
  1198. //---------------------------------------------------------------------------
  1199. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1200. {
  1201. SetDockAllowDrag(true);
  1202. while (!ButtonsStr.IsEmpty())
  1203. {
  1204. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1205. UnicodeString Name = CutToChar(S, L'=', true);
  1206. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1207. {
  1208. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1209. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1210. {
  1211. while (!S.IsEmpty())
  1212. {
  1213. UnicodeString S2 = CutToChar(S, L',', true);
  1214. UnicodeString Name = CutToChar(S2, L':', true);
  1215. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1216. {
  1217. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1218. if (SameText(GetToolbarItemName(Item), Name))
  1219. {
  1220. // forward compatibility
  1221. UnicodeString S3 = CutToChar(S2, L':', true);
  1222. bool Visible = StrToIntDef(S3, 0);
  1223. Item->Visible = Visible;
  1224. }
  1225. }
  1226. }
  1227. }
  1228. }
  1229. }
  1230. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1231. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1232. }
  1233. //---------------------------------------------------------------------------
  1234. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1235. {
  1236. UnicodeString Result;
  1237. SetDockAllowDrag(true);
  1238. Result = ::GetToolbarsLayoutStr(this);
  1239. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1240. return Result;
  1241. }
  1242. //---------------------------------------------------------------------------
  1243. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1244. {
  1245. UnicodeString Result;
  1246. if (Item->Action != NULL)
  1247. {
  1248. Result = Item->Action->Name;
  1249. Result = RemoveSuffix(Result, L"Action");
  1250. }
  1251. else
  1252. {
  1253. Result = Item->Name;
  1254. Result = RemoveSuffix(Result, L"SubmenuItem");
  1255. Result = RemoveSuffix(Result, L"Item");
  1256. }
  1257. return Result;
  1258. }
  1259. //---------------------------------------------------------------------------
  1260. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1261. {
  1262. UnicodeString Result;
  1263. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1264. {
  1265. UnicodeString ToolbarStr;
  1266. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1267. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1268. {
  1269. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1270. {
  1271. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1272. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1273. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1274. // and save the changes here only.
  1275. if (!Item->Visible)
  1276. {
  1277. UnicodeString Name = GetToolbarItemName(Item);
  1278. DebugAssert(Name.Pos(L"TBX") == 0);
  1279. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1280. }
  1281. }
  1282. }
  1283. if (!ToolbarStr.IsEmpty())
  1284. {
  1285. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1286. }
  1287. }
  1288. return Result;
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1292. {
  1293. DebugAssert(FProgressForm == NULL);
  1294. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1295. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1296. FProgressForm->DeleteLocalToRecycleBin =
  1297. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1298. FProgressForm->DeleteRemoteToRecycleBin =
  1299. (Terminal != NULL) &&
  1300. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1301. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1302. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1303. LockWindow();
  1304. if (FTaskbarList != NULL)
  1305. {
  1306. // Actually, do not know what hides the progress once the operation finishes
  1307. SetTaskbarListProgressState(TBPF_NORMAL);
  1308. }
  1309. }
  1310. //---------------------------------------------------------------------------
  1311. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1312. {
  1313. UnlockWindow();
  1314. SAFE_DESTROY(FProgressForm);
  1315. SetQueueProgress();
  1316. }
  1317. //---------------------------------------------------------------------------
  1318. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1319. TFileOperationProgressType & ProgressData)
  1320. {
  1321. // Download to temporary local directory
  1322. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1323. {
  1324. bool Continue = true;
  1325. FPendingTempSpaceWarn = false;
  1326. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1327. if (!Continue)
  1328. {
  1329. Abort();
  1330. }
  1331. }
  1332. // operation is being executed and we still didn't show up progress form
  1333. if (ProgressData.InProgress && !FProgressForm)
  1334. {
  1335. CreateProgressForm(NULL);
  1336. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1337. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1338. }
  1339. // operation is finished (or terminated), so we hide progress form
  1340. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1341. {
  1342. DestroyProgressForm();
  1343. if (ProgressData.Operation == foCalculateSize)
  1344. {
  1345. // When calculating size before transfer, the abort caused by
  1346. // cancel flag set due to "MoveToQueue" below,
  1347. // is not propagated back to us in ExecuteFileOperation,
  1348. // as it is silently swallowed.
  1349. // So we explicitly (re)throw it here.
  1350. if (FMoveToQueue)
  1351. {
  1352. Abort();
  1353. }
  1354. }
  1355. else
  1356. {
  1357. if ((ProgressData.Cancel == csContinue) &&
  1358. !FAutoOperation)
  1359. {
  1360. OperationComplete(ProgressData.StartTime);
  1361. }
  1362. }
  1363. }
  1364. if (FProgressForm &&
  1365. // Can happen only during synchronization, when ending one of the operations
  1366. ProgressData.InProgress)
  1367. {
  1368. FProgressForm->SetProgressData(ProgressData);
  1369. if (FTaskbarList != NULL)
  1370. {
  1371. DebugAssert(ProgressData.InProgress);
  1372. SetTaskbarListProgressValue(&ProgressData);
  1373. }
  1374. if (FProgressForm->Cancel > csContinue)
  1375. {
  1376. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1377. if (FProgressForm->Cancel == csCancelFile)
  1378. {
  1379. FProgressForm->ClearCancel();
  1380. }
  1381. // cancel cancels even the move
  1382. FMoveToQueue = false;
  1383. }
  1384. else if (FProgressForm->MoveToQueue)
  1385. {
  1386. FMoveToQueue = true;
  1387. ProgressData.SetCancelAtLeast(csCancel);
  1388. }
  1389. if ((FTransferResumeList != NULL) &&
  1390. ProgressData.InProgress &&
  1391. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1392. !ProgressData.FullFileName.IsEmpty())
  1393. {
  1394. if ((FTransferResumeList->Count == 0) ||
  1395. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1396. {
  1397. // note that we do not recognize directories from files here
  1398. FTransferResumeList->Add(ProgressData.FullFileName);
  1399. }
  1400. }
  1401. }
  1402. }
  1403. //---------------------------------------------------------------------------
  1404. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1405. const TDateTime & StartTime)
  1406. {
  1407. if (GUIConfiguration->BeepOnFinish &&
  1408. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1409. {
  1410. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1411. DWORD Sound;
  1412. if (!ExtractFileExt(BeepSound).IsEmpty())
  1413. {
  1414. Sound = SND_FILENAME;
  1415. }
  1416. else
  1417. {
  1418. Sound = SND_ALIAS;
  1419. }
  1420. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1421. }
  1422. }
  1423. //---------------------------------------------------------------------------
  1424. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1425. TFileOperationProgressType & ProgressData)
  1426. {
  1427. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1428. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1429. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1430. FileOperationProgress(ProgressData);
  1431. }
  1432. //---------------------------------------------------------------------------
  1433. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1434. {
  1435. UnicodeString Result;
  1436. if (FProgressForm != NULL)
  1437. {
  1438. Result = FProgressForm->ProgressStr();
  1439. }
  1440. return Result;
  1441. }
  1442. //---------------------------------------------------------------------------
  1443. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1444. bool DragDrop)
  1445. {
  1446. return (!DragDrop && (DropSourceControl == NULL)) ||
  1447. (DropSourceControl == RemoteDirView);
  1448. }
  1449. //---------------------------------------------------------------------------
  1450. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1451. TFileOperation Operation, TOperationSide Side,
  1452. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1453. TOnceDoneOperation & OnceDoneOperation)
  1454. {
  1455. if (!FAutoOperation)
  1456. {
  1457. // no selection on "/upload", form servers only as event handler
  1458. // (it is not displayed)
  1459. if (PanelOperation(Side, FDragDropOperation) &&
  1460. Visible && (Operation != foCalculateSize) &&
  1461. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1462. {
  1463. if (FOnFileOperationFinished != NULL)
  1464. {
  1465. FOnFileOperationFinished(FileName, Success);
  1466. }
  1467. else
  1468. {
  1469. TCustomDirView * DView = DirView(Side);
  1470. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1471. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1472. // this can happen when local drive is unplugged in the middle of the operation
  1473. if (Item != NULL)
  1474. {
  1475. if (Success) Item->Selected = false;
  1476. DView->MakeProgressVisible(Item);
  1477. }
  1478. }
  1479. }
  1480. if ((Operation == foCopy) || (Operation == foMove))
  1481. {
  1482. if (Side == osLocal)
  1483. {
  1484. Configuration->Usage->Inc(L"UploadedFiles");
  1485. }
  1486. else
  1487. {
  1488. Configuration->Usage->Inc(L"DownloadedFiles");
  1489. }
  1490. }
  1491. }
  1492. if (Success && (FSynchronizeController != NULL))
  1493. {
  1494. if (Operation == foCopy)
  1495. {
  1496. DebugAssert(Side == osLocal);
  1497. FSynchronizeController->LogOperation(soUpload, FileName);
  1498. }
  1499. else if (Operation == foDelete)
  1500. {
  1501. DebugAssert(Side == osRemote);
  1502. FSynchronizeController->LogOperation(soDelete, FileName);
  1503. }
  1504. }
  1505. if (FProgressForm)
  1506. {
  1507. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1508. }
  1509. }
  1510. //---------------------------------------------------------------------------
  1511. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1512. TFileOperation Operation, TOperationSide Side,
  1513. bool Temp, const UnicodeString & FileName, Boolean Success,
  1514. TOnceDoneOperation & OnceDoneOperation)
  1515. {
  1516. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1517. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1518. OnceDoneOperation);
  1519. }
  1520. //---------------------------------------------------------------------------
  1521. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1522. {
  1523. DebugAssert(GetSide(Side) == osRemote);
  1524. DebugUsedParam(Side);
  1525. return RemoteDirView;
  1526. }
  1527. //---------------------------------------------------------------------------
  1528. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1529. {
  1530. DebugAssert(GetSide(Side) == osRemote);
  1531. DebugUsedParam(Side);
  1532. return (Terminal != NULL);
  1533. }
  1534. //---------------------------------------------------------------------------
  1535. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1536. TOperationSide Side, int FilesOnly)
  1537. {
  1538. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1539. }
  1540. //---------------------------------------------------------------------------
  1541. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1542. TOperationSide Side, int FilesOnly)
  1543. {
  1544. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. struct THistoryItemData
  1548. {
  1549. short int Side;
  1550. short int Index;
  1551. };
  1552. //---------------------------------------------------------------------------
  1553. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1554. {
  1555. DirView(Side)->HistoryGo(Index);
  1556. }
  1557. //---------------------------------------------------------------------------
  1558. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1559. {
  1560. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1561. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1562. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1563. }
  1564. //---------------------------------------------------------------------------
  1565. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1566. bool Back)
  1567. {
  1568. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1569. {
  1570. TCustomDirView * DView = DirView(Side);
  1571. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1572. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1573. if (ICount > 10)
  1574. {
  1575. ICount = 10;
  1576. }
  1577. Menu->Items->Clear();
  1578. THistoryItemData Data;
  1579. Data.Side = Side;
  1580. for (short int i = 1; i <= ICount; i++)
  1581. {
  1582. TTBCustomItem * Item = new TTBXItem(Menu);
  1583. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1584. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1585. Item->Hint = DView->HistoryPath[Data.Index];
  1586. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1587. Item->Tag = *reinterpret_cast<int*>(&Data);
  1588. Item->OnClick = HistoryItemClick;
  1589. Menu->Items->Add(Item);
  1590. }
  1591. }
  1592. }
  1593. //---------------------------------------------------------------------------
  1594. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1595. TOperationSide Side, bool Back)
  1596. {
  1597. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1598. {
  1599. // workaround
  1600. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1601. // operator new allocates memory in Pascal code, but calls inline
  1602. // contructor in C++, leading in problems, the function does
  1603. // both in Pascal code
  1604. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1605. UpdateHistoryMenu(Side, Back);
  1606. }
  1607. return FHistoryMenu[Side == osLocal][Back];
  1608. }
  1609. //---------------------------------------------------------------------------
  1610. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1611. TCustomDirView *Sender)
  1612. {
  1613. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1614. UpdateHistoryMenu(Side, true);
  1615. UpdateHistoryMenu(Side, false);
  1616. }
  1617. //---------------------------------------------------------------------------
  1618. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1619. {
  1620. // remote custom commands can be executed only if the server supports shell commands
  1621. // or have secondary shell
  1622. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1626. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1627. {
  1628. int Result;
  1629. TFileCustomCommand RemoteCustomCommand;
  1630. TLocalCustomCommand LocalCustomCommand;
  1631. TFileCustomCommand * NonInteractiveCustomCommand =
  1632. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1633. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1634. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1635. if (FLAGCLEAR(Command.Params, ccLocal))
  1636. {
  1637. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1638. // custom command that does not operate with files can be executed anytime ...
  1639. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1640. {
  1641. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1642. {
  1643. // ... but do not show such command in remote file menu (TODO)
  1644. Result = AllowedState;
  1645. }
  1646. else
  1647. {
  1648. Result = -1;
  1649. }
  1650. }
  1651. else
  1652. {
  1653. if ((ListType == ccltAll) || (ListType == ccltFile))
  1654. {
  1655. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1656. }
  1657. else
  1658. {
  1659. Result = -1;
  1660. }
  1661. }
  1662. }
  1663. else
  1664. {
  1665. // custom command that does not operate with files can be executed anytime
  1666. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1667. {
  1668. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1669. }
  1670. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1671. {
  1672. if ((ListType == ccltAll) || (ListType == ccltFile))
  1673. {
  1674. // special case is "diff"-style command that can be executed over any side,
  1675. // if we have both sides
  1676. Result =
  1677. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1678. // directly (instead of EnableSelectedOperation) to pass
  1679. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1680. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1681. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1682. }
  1683. else if (ListType == ccltBoth)
  1684. {
  1685. Result = 1;
  1686. }
  1687. else
  1688. {
  1689. Result = -1;
  1690. }
  1691. }
  1692. else
  1693. {
  1694. if ((ListType == ccltAll) || (ListType == ccltFile))
  1695. {
  1696. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1697. }
  1698. else
  1699. {
  1700. Result = -1;
  1701. }
  1702. }
  1703. }
  1704. return Result;
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1708. TStrings * FileList, const TCustomCommandType & ACommand,
  1709. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1710. {
  1711. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1712. {
  1713. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1714. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1715. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1716. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1717. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1718. bool Capture =
  1719. FLAGSET(ACommand.Params, ccShowResults) ||
  1720. FLAGSET(ACommand.Params, ccCopyResults) ||
  1721. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1722. TCaptureOutputEvent OutputEvent = NULL;
  1723. DebugAssert(FCapturedLog == NULL);
  1724. if (Capture)
  1725. {
  1726. FCapturedLog = new TStringList();
  1727. OutputEvent = TerminalCaptureLog;
  1728. }
  1729. try
  1730. {
  1731. if (!RemoteCustomCommand.IsFileCommand(Command))
  1732. {
  1733. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1734. OutputEvent);
  1735. }
  1736. else
  1737. {
  1738. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1739. }
  1740. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1741. {
  1742. if (FLAGSET(ACommand.Params, ccCopyResults))
  1743. {
  1744. CopyToClipboard(FCapturedLog);
  1745. }
  1746. if (FLAGSET(ACommand.Params, ccShowResults))
  1747. {
  1748. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1749. }
  1750. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1751. {
  1752. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1753. }
  1754. }
  1755. }
  1756. __finally
  1757. {
  1758. SAFE_DESTROY(FCapturedLog);
  1759. }
  1760. }
  1761. }
  1762. //---------------------------------------------------------------------------
  1763. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1764. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1765. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1766. {
  1767. TStrings * LocalFileList = NULL;
  1768. TStrings * RemoteFileList = NULL;
  1769. TStrings * RemoteFileListFull = NULL;
  1770. try
  1771. {
  1772. if (LocalFileCommand)
  1773. {
  1774. if (ALocalFileList == NULL)
  1775. {
  1776. DebugAssert(HasDirView[osLocal]);
  1777. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1778. // directly (instead of EnableSelectedOperation) to pass
  1779. // false to FocusedFileOnlyWhenFocused
  1780. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1781. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1782. }
  1783. else
  1784. {
  1785. LocalFileList = ALocalFileList;
  1786. }
  1787. if (FileListCommand)
  1788. {
  1789. if (LocalFileList->Count != 1)
  1790. {
  1791. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1792. }
  1793. }
  1794. else
  1795. {
  1796. if ((LocalFileList->Count != 1) &&
  1797. (FileList->Count != 1) &&
  1798. (LocalFileList->Count != FileList->Count))
  1799. {
  1800. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1801. }
  1802. }
  1803. }
  1804. UnicodeString RootTempDir;
  1805. UnicodeString TempDir;
  1806. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1807. if (!RemoteFiles)
  1808. {
  1809. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1810. }
  1811. try
  1812. {
  1813. TDateTimes RemoteFileTimes;
  1814. if (RemoteFiles)
  1815. {
  1816. RemoteFileList = FileList;
  1817. }
  1818. else
  1819. {
  1820. RemoteFileList = new TStringList();
  1821. TMakeLocalFileListParams MakeFileListParam;
  1822. MakeFileListParam.FileList = RemoteFileList;
  1823. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1824. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1825. MakeFileListParam.Recursive =
  1826. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1827. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1828. if (!MakeFileListParam.Recursive)
  1829. {
  1830. RemoteFileTimes.clear();
  1831. RemoteFileListFull = new TStringList();
  1832. MakeFileListParam.FileList = RemoteFileListFull;
  1833. MakeFileListParam.Recursive = true;
  1834. MakeFileListParam.IncludeDirs = false;
  1835. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1836. }
  1837. else
  1838. {
  1839. RemoteFileListFull = RemoteFileList;
  1840. }
  1841. }
  1842. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1843. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1844. if (!NonBlocking)
  1845. {
  1846. int Count;
  1847. if (FileListCommand)
  1848. {
  1849. Count = 1;
  1850. }
  1851. else if (LocalFileCommand)
  1852. {
  1853. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1854. }
  1855. else
  1856. {
  1857. Count = RemoteFileList->Count;
  1858. }
  1859. Progress.Start(foCustomCommand, osRemote, Count);
  1860. DebugAssert(FProgressForm != NULL);
  1861. FProgressForm->ReadOnly = true;
  1862. }
  1863. try
  1864. {
  1865. if (FileListCommand)
  1866. {
  1867. UnicodeString LocalFile;
  1868. // MakeFileList does not delimit filenames
  1869. UnicodeString FileList = MakeFileList(RemoteFileList);
  1870. if (LocalFileCommand)
  1871. {
  1872. DebugAssert(LocalFileList->Count == 1);
  1873. LocalFile = LocalFileList->Strings[0];
  1874. }
  1875. TLocalCustomCommand CustomCommand(Data,
  1876. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1877. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1878. if (NonBlocking)
  1879. {
  1880. DebugAssert(!POutput);
  1881. ExecuteShellChecked(ShellCommand);
  1882. }
  1883. else
  1884. {
  1885. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1886. }
  1887. }
  1888. else if (LocalFileCommand)
  1889. {
  1890. if (LocalFileList->Count == 1)
  1891. {
  1892. UnicodeString LocalFile = LocalFileList->Strings[0];
  1893. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1894. {
  1895. UnicodeString FileName = RemoteFileList->Strings[Index];
  1896. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1897. Progress.SetFile(FileNameForProgress);
  1898. TLocalCustomCommand CustomCommand(Data,
  1899. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1900. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1901. TOnceDoneOperation OnceDoneOperation;
  1902. // Do not unselect anything, as it won't work with two panels anyway
  1903. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1904. }
  1905. }
  1906. else if (RemoteFileList->Count == 1)
  1907. {
  1908. UnicodeString FileName = RemoteFileList->Strings[0];
  1909. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1910. {
  1911. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1912. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1913. Progress.SetFile(LocalFileNameOnly);
  1914. TLocalCustomCommand CustomCommand(
  1915. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1916. FileName, LocalFileName, L"");
  1917. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1918. TOnceDoneOperation OnceDoneOperation;
  1919. // Do not unselect anything, as it won't work with two panels anyway
  1920. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1921. }
  1922. }
  1923. else
  1924. {
  1925. if (LocalFileList->Count != RemoteFileList->Count)
  1926. {
  1927. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1928. }
  1929. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1930. {
  1931. UnicodeString FileName = RemoteFileList->Strings[Index];
  1932. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1933. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1934. Progress.SetFile(LocalFileNameOnly); // sic
  1935. TLocalCustomCommand CustomCommand(
  1936. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1937. FileName, LocalFileName, L"");
  1938. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1939. TOnceDoneOperation OnceDoneOperation;
  1940. // Do not unselect anything, as it won't work with two panels anyway
  1941. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1942. }
  1943. }
  1944. }
  1945. else
  1946. {
  1947. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1948. {
  1949. UnicodeString FileName = RemoteFileList->Strings[Index];
  1950. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1951. Progress.SetFile(FileNameForProgress);
  1952. TLocalCustomCommand CustomCommand(Data,
  1953. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1954. FileName, L"", L"");
  1955. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1956. TOnceDoneOperation OnceDoneOperation;
  1957. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1958. }
  1959. }
  1960. }
  1961. __finally
  1962. {
  1963. if (!NonBlocking)
  1964. {
  1965. Progress.Stop();
  1966. }
  1967. }
  1968. DebugAssert(!FAutoOperation);
  1969. if (!RemoteFiles)
  1970. {
  1971. TempDir = IncludeTrailingBackslash(TempDir);
  1972. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1973. {
  1974. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1975. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1976. // Skip directories, process only nested files.
  1977. // The check is redundant as FileAge fails for directories anyway.
  1978. !DirectoryExists(FileName))
  1979. {
  1980. UnicodeString RemoteDir =
  1981. UnixExtractFileDir(
  1982. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1983. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1984. TDateTime NewTime;
  1985. if (FileAge(FileName, NewTime) &&
  1986. (NewTime != RemoteFileTimes[Index]))
  1987. {
  1988. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1989. TemporaryFileCopyParam(CopyParam);
  1990. CopyParam.FileMask = L"";
  1991. FAutoOperation = true;
  1992. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1993. TemporaryFilesList->Add(FileName);
  1994. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1995. }
  1996. }
  1997. }
  1998. }
  1999. }
  2000. __finally
  2001. {
  2002. FAutoOperation = false;
  2003. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2004. {
  2005. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2006. }
  2007. }
  2008. }
  2009. __finally
  2010. {
  2011. if (RemoteFileList != FileList)
  2012. {
  2013. delete RemoteFileList;
  2014. }
  2015. if (LocalFileList != ALocalFileList)
  2016. {
  2017. delete LocalFileList;
  2018. }
  2019. if (RemoteFileListFull != RemoteFileList)
  2020. {
  2021. delete RemoteFileListFull;
  2022. }
  2023. }
  2024. }
  2025. //---------------------------------------------------------------------------
  2026. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2027. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2028. bool FileListCommand, UnicodeString * POutput)
  2029. {
  2030. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2031. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2032. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2033. {
  2034. UnicodeString FileName = SelectedFileList->Strings[Index];
  2035. if (DirectoryExists(FileName))
  2036. {
  2037. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2038. {
  2039. LocalFileList->Add(FileName);
  2040. }
  2041. if (FLAGSET(ACommand.Params, ccRecursive))
  2042. {
  2043. TMakeLocalFileListParams MakeFileListParam;
  2044. MakeFileListParam.FileList = LocalFileList.get();
  2045. MakeFileListParam.FileTimes = NULL;
  2046. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2047. MakeFileListParam.Recursive = true;
  2048. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2049. }
  2050. }
  2051. else
  2052. {
  2053. LocalFileList->Add(FileName);
  2054. }
  2055. }
  2056. if (FileListCommand)
  2057. {
  2058. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2059. TLocalCustomCommand CustomCommand(
  2060. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2061. L"", L"", FileList);
  2062. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2063. }
  2064. else
  2065. {
  2066. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2067. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2068. DebugAssert(FProgressForm != NULL);
  2069. FProgressForm->ReadOnly = true;
  2070. try
  2071. {
  2072. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2073. {
  2074. UnicodeString FileName = LocalFileList->Strings[Index];
  2075. Progress.SetFile(FileName);
  2076. TLocalCustomCommand CustomCommand(
  2077. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2078. FileName, L"", L"");
  2079. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2080. TOnceDoneOperation OnceDoneOperation;
  2081. Progress.Finish(FileName, true, OnceDoneOperation);
  2082. }
  2083. }
  2084. __finally
  2085. {
  2086. Progress.Stop();
  2087. }
  2088. }
  2089. }
  2090. //---------------------------------------------------------------------------
  2091. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2092. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2093. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2094. {
  2095. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2096. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2097. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2098. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2099. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2100. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2101. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2102. std::unique_ptr<UnicodeString> POutput;
  2103. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2104. FLAGSET(ACommand.Params, ccCopyResults))
  2105. {
  2106. POutput.reset(new UnicodeString());
  2107. }
  2108. if (!LocalCustomCommand.IsFileCommand(Command))
  2109. {
  2110. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2111. }
  2112. // remote files?
  2113. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2114. {
  2115. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2116. }
  2117. // local files
  2118. else
  2119. {
  2120. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2121. }
  2122. if (POutput.get() != NULL)
  2123. {
  2124. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2125. int P = POutput->Pos(sLineBreak);
  2126. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2127. {
  2128. POutput->SetLength(P - 1);
  2129. }
  2130. // Copy even empty output to clipboard
  2131. if (FLAGSET(ACommand.Params, ccCopyResults))
  2132. {
  2133. CopyToClipboard(*POutput);
  2134. }
  2135. // But do not show an empty message box.
  2136. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2137. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2138. !POutput->IsEmpty())
  2139. {
  2140. TClipboardHandler ClipboardHandler;
  2141. ClipboardHandler.Text = *POutput;
  2142. TMessageParams Params;
  2143. TQueryButtonAlias Aliases[1];
  2144. Aliases[0].Button = qaRetry;
  2145. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2146. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2147. Params.Aliases = Aliases;
  2148. Params.AliasesCount = LENOF(Aliases);
  2149. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2150. }
  2151. }
  2152. }
  2153. //---------------------------------------------------------------------------
  2154. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2155. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2156. {
  2157. TCustomCommandData Data(Terminal);
  2158. UnicodeString Site = Terminal->SessionData->SessionKey;
  2159. UnicodeString HelpKeyword = ACommand.HomePage;
  2160. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2161. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2162. {
  2163. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2164. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2165. {
  2166. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2167. }
  2168. else
  2169. {
  2170. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2171. }
  2172. if (!DoCustomCommandOptionsDialog(
  2173. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2174. {
  2175. Abort();
  2176. }
  2177. }
  2178. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2179. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2180. {
  2181. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2182. }
  2183. else
  2184. {
  2185. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2186. }
  2187. }
  2188. //---------------------------------------------------------------------------
  2189. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2190. const TCustomCommandType & Command)
  2191. {
  2192. DebugAssert(FCustomCommandLocalFileList != NULL);
  2193. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2194. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2195. TStrings * LocalFileList = new TStringList();
  2196. TStrings * RemoteFileList = new TStringList();
  2197. try
  2198. {
  2199. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2200. {
  2201. LocalFileList->Clear();
  2202. LocalFileList->AddObject(
  2203. FCustomCommandLocalFileList->Strings[Index],
  2204. FCustomCommandLocalFileList->Objects[Index]);
  2205. RemoteFileList->Clear();
  2206. RemoteFileList->AddObject(
  2207. FCustomCommandRemoteFileList->Strings[Index],
  2208. FCustomCommandRemoteFileList->Objects[Index]);
  2209. CustomCommand(RemoteFileList, Command, LocalFileList);
  2210. }
  2211. }
  2212. __finally
  2213. {
  2214. delete LocalFileList;
  2215. delete RemoteFileList;
  2216. }
  2217. }
  2218. //---------------------------------------------------------------------------
  2219. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2220. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2221. {
  2222. delete FCustomCommandLocalFileList;
  2223. delete FCustomCommandRemoteFileList;
  2224. // takeover ownership,
  2225. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2226. FCustomCommandLocalFileList = LocalFileList;
  2227. FCustomCommandRemoteFileList = RemoteFileList;
  2228. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2229. if (Button != NULL)
  2230. {
  2231. FCustomCommandMenu->Items->Clear();
  2232. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2233. MenuPopup(FCustomCommandMenu, Button);
  2234. }
  2235. else
  2236. {
  2237. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2238. }
  2239. }
  2240. //---------------------------------------------------------------------------
  2241. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2242. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2243. {
  2244. DebugAssert(FCapturedLog != NULL);
  2245. if ((OutputType == cotOutput) || (OutputType == cotError))
  2246. {
  2247. FCapturedLog->Add(AddedLine);
  2248. }
  2249. }
  2250. //---------------------------------------------------------------------------
  2251. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2252. TOperationSide Side)
  2253. {
  2254. return (Side == osRemote) &&
  2255. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2256. }
  2257. //---------------------------------------------------------------------------
  2258. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2259. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2260. {
  2261. TTBItemOptions O;
  2262. O = Item->Options;
  2263. TCustomDirView * DView = DirView(Side);
  2264. if ((DView->ItemFocused != NULL) &&
  2265. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2266. // when resolving links is disabled, default action is to enter the directory,
  2267. // no matter what DoubleClickAction is configured to
  2268. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2269. // Can only Edit files, but can Open/Copy even directories
  2270. ((DoubleClickAction != dcaEdit) ||
  2271. !DView->ItemIsDirectory(DView->ItemFocused)))
  2272. {
  2273. Item->Options = O << tboDefault;
  2274. }
  2275. else
  2276. {
  2277. Item->Options = O >> tboDefault;
  2278. }
  2279. }
  2280. //---------------------------------------------------------------------------
  2281. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2282. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2283. {
  2284. TCustomDirView * DView = DirView(Side);
  2285. TListItem * Item = DView->ItemFocused;
  2286. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2287. EnableFocusedOperation[Side])
  2288. {
  2289. TPoint ClientPoint;
  2290. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2291. TPoint(0, 0) : MousePos;
  2292. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2293. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2294. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2295. }
  2296. }
  2297. //---------------------------------------------------------------------------
  2298. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2299. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2300. {
  2301. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2302. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2303. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2304. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2305. Handled = true;
  2306. }
  2307. //---------------------------------------------------------------------------
  2308. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2309. {
  2310. }
  2311. //---------------------------------------------------------------------------
  2312. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2313. {
  2314. DebugAssert(FErrorList == NULL);
  2315. if (WinConfiguration->ContinueOnError)
  2316. {
  2317. FErrorList = new TStringList();
  2318. Configuration->Usage->Inc(L"ContinuationsOnError");
  2319. }
  2320. NonVisualDataModule->StartBusy();
  2321. }
  2322. //---------------------------------------------------------------------------
  2323. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2324. {
  2325. NonVisualDataModule->EndBusy();
  2326. if (FErrorList)
  2327. {
  2328. HandleErrorList(FErrorList);
  2329. }
  2330. }
  2331. //---------------------------------------------------------------------------
  2332. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2333. const TCopyParamType & CopyParam)
  2334. {
  2335. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2336. {
  2337. Configuration->Usage->Inc(L"FileMaskUses");
  2338. }
  2339. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2340. {
  2341. Configuration->Usage->Inc(L"OperationMaskUses");
  2342. }
  2343. }
  2344. //---------------------------------------------------------------------------
  2345. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2346. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2347. {
  2348. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2349. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2350. TTransferOperationParam DefaultParam;
  2351. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2352. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2353. switch (Param.Queue)
  2354. {
  2355. case asOn:
  2356. CopyParam.Queue = true;
  2357. break;
  2358. case asOff:
  2359. CopyParam.Queue = false;
  2360. break;
  2361. case asAuto:
  2362. default:
  2363. // keep default
  2364. break;
  2365. }
  2366. bool Result =
  2367. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2368. if (Result)
  2369. {
  2370. DebugAssert(Terminal);
  2371. bool SelectionRestored = false;
  2372. TCustomDirView * DView = NULL;
  2373. if (HasDirView[Side])
  2374. {
  2375. DView = DirView(Side);
  2376. DView->SaveSelection();
  2377. DView->SaveSelectedNames();
  2378. }
  2379. UpdateCopyParamCounters(CopyParam);
  2380. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2381. DebugAssert(FTransferResumeList == NULL);
  2382. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2383. FMoveToQueue = false;
  2384. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2385. try
  2386. {
  2387. TStrings * PermanentFileList;
  2388. std::unique_ptr<TStrings> PermanentFileListOwner;
  2389. try
  2390. {
  2391. if (Side == osLocal)
  2392. {
  2393. PermanentFileList = FileList;
  2394. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2395. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2396. if (Operation == foMove)
  2397. {
  2398. ReloadLocalDirectory();
  2399. if (DView != NULL)
  2400. {
  2401. DView->RestoreSelection();
  2402. }
  2403. SelectionRestored = true;
  2404. }
  2405. }
  2406. else
  2407. {
  2408. // Clone the file list as it may refer to current directory files,
  2409. // which get destroyed, when the source directory is reloaded after foMove operation.
  2410. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2411. // But for a hotfix, we are not going to do such a big change.
  2412. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2413. PermanentFileList = PermanentFileListOwner.get();
  2414. try
  2415. {
  2416. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2417. }
  2418. __finally
  2419. {
  2420. if (Operation == foMove)
  2421. {
  2422. if (DView != NULL)
  2423. {
  2424. DView->RestoreSelection();
  2425. }
  2426. SelectionRestored = true;
  2427. }
  2428. ReloadLocalDirectory(Param.TargetDirectory);
  2429. }
  2430. }
  2431. }
  2432. catch (EAbort &)
  2433. {
  2434. if (FMoveToQueue)
  2435. {
  2436. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2437. DebugAssert(CopyParam.TransferSkipList == NULL);
  2438. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2439. if (TransferResumeList->Count > 0)
  2440. {
  2441. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2442. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2443. }
  2444. CopyParam.TransferSkipList = TransferResumeList.release();
  2445. // not really needed, just to keep it consistent with TransferResumeList
  2446. FTransferResumeList = NULL;
  2447. FMoveToQueue = false;
  2448. Configuration->Usage->Inc("MovesToBackground");
  2449. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2450. ClearTransferSourceSelection(Direction);
  2451. }
  2452. throw;
  2453. }
  2454. }
  2455. __finally
  2456. {
  2457. if (!SelectionRestored && (DView != NULL))
  2458. {
  2459. DView->DiscardSavedSelection();
  2460. }
  2461. FTransferResumeList = NULL;
  2462. }
  2463. }
  2464. return Result;
  2465. }
  2466. //---------------------------------------------------------------------------
  2467. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2468. TOperationSide Side, TStrings * FileList, void * Param)
  2469. {
  2470. DebugAssert(FileList->Count);
  2471. // We deliberately do not toggle alternative flag (Param), but use OR,
  2472. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2473. // shortcut of CurrentDeleteAlternativeAction
  2474. bool Alternative =
  2475. bool(Param) || UseAlternativeFunction();
  2476. bool Recycle;
  2477. if (Side == osLocal)
  2478. {
  2479. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2480. }
  2481. else
  2482. {
  2483. Recycle =
  2484. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2485. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2486. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2487. }
  2488. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2489. if (!Result)
  2490. {
  2491. UnicodeString Query;
  2492. if (FileList->Count == 1)
  2493. {
  2494. if (Side == osLocal)
  2495. {
  2496. Query = ExtractFileName(FileList->Strings[0]);
  2497. }
  2498. else
  2499. {
  2500. Query = UnixExtractFileName(FileList->Strings[0]);
  2501. }
  2502. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2503. }
  2504. else
  2505. {
  2506. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2507. }
  2508. TMessageParams Params(mpNeverAskAgainCheck);
  2509. Params.ImageName = L"Delete file";
  2510. unsigned int Answer =
  2511. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2512. if (Answer == qaNeverAskAgain)
  2513. {
  2514. Result = true;
  2515. if (Recycle)
  2516. {
  2517. WinConfiguration->ConfirmRecycling = false;
  2518. }
  2519. else
  2520. {
  2521. WinConfiguration->ConfirmDeleting = false;
  2522. }
  2523. }
  2524. else
  2525. {
  2526. Result = (Answer == qaOK);
  2527. }
  2528. }
  2529. if (Result)
  2530. {
  2531. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2532. }
  2533. return Result;
  2534. }
  2535. //---------------------------------------------------------------------------
  2536. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2537. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2538. {
  2539. TAutoBatch AutoBatch(this);
  2540. bool Result;
  2541. if ((Operation == foCopy) || (Operation == foMove))
  2542. {
  2543. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2544. }
  2545. else if (Operation == foRename)
  2546. {
  2547. DebugAssert(DirView(Side)->ItemFocused);
  2548. DirView(Side)->ItemFocused->EditCaption();
  2549. Result = true;
  2550. }
  2551. else if (Operation == foDelete)
  2552. {
  2553. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2554. }
  2555. else if (Operation == foSetProperties)
  2556. {
  2557. RemoteDirView->SaveSelectedNames();
  2558. Result = SetProperties(Side, FileList);
  2559. }
  2560. else if (Operation == foCustomCommand)
  2561. {
  2562. DebugAssert(Param);
  2563. DebugAssert(Side == osRemote);
  2564. RemoteDirView->SaveSelectedNames();
  2565. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2566. CustomCommand(FileList, *Command, NULL);
  2567. Result = true;
  2568. }
  2569. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2570. {
  2571. DebugAssert(Side == osRemote);
  2572. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2573. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2574. }
  2575. else if (Operation == foLock)
  2576. {
  2577. DebugAssert(Side == osRemote);
  2578. LockFiles(FileList, true);
  2579. Result = true;
  2580. }
  2581. else if (Operation == foUnlock)
  2582. {
  2583. DebugAssert(Side == osRemote);
  2584. LockFiles(FileList, false);
  2585. Result = true;
  2586. }
  2587. else
  2588. {
  2589. DebugFail();
  2590. }
  2591. return Result;
  2592. }
  2593. //---------------------------------------------------------------------------
  2594. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2595. {
  2596. if (Side == osCurrent)
  2597. {
  2598. Side = FCurrentSide;
  2599. }
  2600. return Side;
  2601. }
  2602. //---------------------------------------------------------------------------
  2603. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2604. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2605. {
  2606. Side = GetSide(Side);
  2607. bool Result;
  2608. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2609. try
  2610. {
  2611. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2612. }
  2613. __finally
  2614. {
  2615. delete FileList;
  2616. }
  2617. return Result;
  2618. }
  2619. //---------------------------------------------------------------------------
  2620. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2621. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2622. bool NoConfirmation, void * Param)
  2623. {
  2624. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2625. {
  2626. if ((Operation == foCopy) || (Operation == foMove))
  2627. {
  2628. if (GetSide(Side) == osLocal)
  2629. {
  2630. Configuration->Usage->Inc(L"UploadsCommand");
  2631. }
  2632. else
  2633. {
  2634. Configuration->Usage->Inc(L"DownloadsCommand");
  2635. }
  2636. }
  2637. }
  2638. }
  2639. //---------------------------------------------------------------------------
  2640. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2641. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2642. {
  2643. TTransferOperationParam Param;
  2644. if ((WinConfiguration->Interface != ifCommander) ||
  2645. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2646. {
  2647. Flags &= ~cocShortCutHint;
  2648. }
  2649. TCustomDirView * DView = DirView(Side);
  2650. Param.Options =
  2651. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2652. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2653. if (FLAGSET(Flags, cocQueue))
  2654. {
  2655. Param.Queue = asOn;
  2656. }
  2657. else if (FLAGSET(Flags, cocNonQueue))
  2658. {
  2659. Param.Queue = asOff;
  2660. }
  2661. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2662. }
  2663. //---------------------------------------------------------------------------
  2664. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2665. {
  2666. try
  2667. {
  2668. if (ErrorList->Count)
  2669. {
  2670. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2671. if (MessageDialog(Message, qtError,
  2672. qaOK | qaCancel, HELP_NONE) == qaOK)
  2673. {
  2674. unsigned int Answer;
  2675. int Index = 0;
  2676. do
  2677. {
  2678. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2679. TQueryButtonAlias Aliases[2];
  2680. Aliases[0].Button = qaYes;
  2681. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2682. Aliases[1].Button = qaNo;
  2683. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2684. TMessageParams Params;
  2685. Params.Aliases = Aliases;
  2686. Params.AliasesCount = LENOF(Aliases);
  2687. Answer = MoreMessageDialog(
  2688. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2689. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2690. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2691. qaOK, HELP_NONE, &Params);
  2692. if (Answer == qaNo)
  2693. {
  2694. Index++;
  2695. }
  2696. if (Answer == qaYes)
  2697. {
  2698. Index--;
  2699. }
  2700. }
  2701. while (Answer != qaOK);
  2702. }
  2703. }
  2704. }
  2705. __finally
  2706. {
  2707. TStrings * List = ErrorList;
  2708. ErrorList = NULL;
  2709. for (int i = 0; i < List->Count; i++)
  2710. {
  2711. delete List->Objects[i];
  2712. }
  2713. delete List;
  2714. }
  2715. }
  2716. //---------------------------------------------------------------------------
  2717. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2718. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2719. {
  2720. // needed for checking filemasks, as there's no directory object
  2721. // associated with the file object
  2722. File->FullFileName = FullFileName;
  2723. TFileMasks::TParams MaskParams;
  2724. MaskParams.Size = File->Size;
  2725. MaskParams.Modification = File->Modification;
  2726. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2727. }
  2728. //---------------------------------------------------------------------------
  2729. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2730. {
  2731. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2732. Side = GetSide(Side);
  2733. TCustomDirView * DView = DirView(Side);
  2734. TListItem * FocusedItem = DView->ItemFocused;
  2735. UnicodeString Name;
  2736. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2737. {
  2738. Name = DView->ItemFileName(FocusedItem);
  2739. }
  2740. else
  2741. {
  2742. Name = LoadStr(NEW_FILE);
  2743. }
  2744. UnicodeString Names = Name;
  2745. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2746. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2747. HELP_EDIT_NEW, History.get(), true))
  2748. {
  2749. while (!Names.IsEmpty())
  2750. {
  2751. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2752. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2753. UnicodeString TargetFileName;
  2754. UnicodeString LocalFileName;
  2755. UnicodeString RootTempDir;
  2756. UnicodeString TempDir;
  2757. UnicodeString RemoteDirectory;
  2758. bool ExistingFile = false;
  2759. if (Side == osRemote)
  2760. {
  2761. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2762. TRemoteFile * File = NULL;
  2763. if (FTerminal->FileExists(Name, &File) &&
  2764. !File->IsDirectory)
  2765. {
  2766. try
  2767. {
  2768. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2769. ExistingFile = true;
  2770. }
  2771. __finally
  2772. {
  2773. delete File;
  2774. }
  2775. }
  2776. if (!ExistingFile)
  2777. {
  2778. RemoteDirectory = UnixExtractFilePath(Name);
  2779. TemporaryDirectoryForRemoteFiles(
  2780. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2781. TargetFileName = UnixExtractFileName(Name);
  2782. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2783. LocalFileName = TempDir +
  2784. // We probably do not want to trim the VMS version here
  2785. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2786. }
  2787. }
  2788. else
  2789. {
  2790. if (ExtractFilePath(Name).IsEmpty())
  2791. {
  2792. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2793. }
  2794. else
  2795. {
  2796. LocalFileName = ExpandFileName(Name);
  2797. }
  2798. TargetFileName = ExtractFileName(Name);
  2799. }
  2800. if (!ExistingFile)
  2801. {
  2802. if (!FileExists(ApiPath(LocalFileName)))
  2803. {
  2804. int File = FileCreate(ApiPath(LocalFileName));
  2805. if (File < 0)
  2806. {
  2807. if (!RootTempDir.IsEmpty())
  2808. {
  2809. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2810. }
  2811. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2812. }
  2813. else
  2814. {
  2815. FileClose(File);
  2816. }
  2817. }
  2818. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2819. const TEditorData * ExternalEditor = NULL;
  2820. TFileMasks::TParams MaskParams; // size not known
  2821. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2822. false, MaskParams);
  2823. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2824. ExternalEditor, RootTempDir, RemoteDirectory);
  2825. }
  2826. }
  2827. }
  2828. }
  2829. //---------------------------------------------------------------------------
  2830. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2831. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2832. {
  2833. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2834. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2835. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2836. return
  2837. ((ExecuteFileBy == efInternalEditor)) ||
  2838. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2839. }
  2840. //---------------------------------------------------------------------------
  2841. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2842. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2843. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2844. UnicodeString RemoteDirectory)
  2845. {
  2846. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2847. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2848. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2849. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2850. Side = GetSide(Side);
  2851. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2852. if (Side == osRemote)
  2853. {
  2854. Data->Terminal = Terminal;
  2855. Data->Queue = Queue;
  2856. Data->SessionData = CloneCurrentSessionData();
  2857. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2858. Data->RemoteDirectory = RemoteDirectory;
  2859. Data->SessionName = Terminal->SessionData->SessionName;
  2860. Data->LocalRootDirectory = LocalRootDirectory;
  2861. Data->OriginalFileName = OriginalFileName;
  2862. Data->Command = L""; // will be changed later for external editor
  2863. }
  2864. if (ExecuteFileBy == efInternalEditor)
  2865. {
  2866. if (Side == osRemote)
  2867. {
  2868. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2869. L" - " + Terminal->SessionData->SessionName;
  2870. TForm * Editor;
  2871. try
  2872. {
  2873. TNotifyEvent OnFileChanged = NULL;
  2874. TNotifyEvent OnSaveAll = NULL;
  2875. TAnyModifiedEvent OnAnyModified = NULL;
  2876. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2877. if (Terminal->IsCapable[fsBackgroundTransfers])
  2878. {
  2879. OnFileChanged = FEditorManager->FileChanged;
  2880. OnSaveAll = SaveAllInternalEditors;
  2881. OnAnyModified = AnyInternalEditorModified;
  2882. }
  2883. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2884. FEditorManager->FileReload, FEditorManager->FileClosed,
  2885. OnSaveAll, OnAnyModified,
  2886. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2887. }
  2888. catch(...)
  2889. {
  2890. if (!LocalRootDirectory.IsEmpty())
  2891. {
  2892. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2893. }
  2894. throw;
  2895. }
  2896. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2897. }
  2898. else
  2899. {
  2900. DebugAssert(!FStandaloneEditing);
  2901. TForm * Editor =
  2902. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2903. SaveAllInternalEditors, AnyInternalEditorModified,
  2904. L"", false, SessionColor, -1);
  2905. FLocalEditors->Add(Editor);
  2906. }
  2907. }
  2908. else
  2909. {
  2910. HANDLE Process;
  2911. if (ExecuteFileBy == efExternalEditor)
  2912. {
  2913. UnicodeString Program, Params, Dir;
  2914. Data->Command = ExternalEditor->ExternalEditor;
  2915. ReformatFileNameCommand(Data->Command);
  2916. SplitCommand(Data->Command, Program, Params, Dir);
  2917. Params = ExpandFileNameCommand(Params, FileName);
  2918. Program = ExpandEnvironmentVariables(Program);
  2919. if (!ExecuteShell(Program, Params, Process))
  2920. {
  2921. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2922. }
  2923. }
  2924. else
  2925. {
  2926. DebugAssert(Side == osRemote);
  2927. if (!ExecuteShell(FileName, L"", Process))
  2928. {
  2929. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2930. }
  2931. }
  2932. if ((Side == osLocal) ||
  2933. ((ExecuteFileBy == efShell) &&
  2934. !WinConfiguration->Editor.SDIShellEditor) ||
  2935. ((ExecuteFileBy == efExternalEditor) &&
  2936. !ExternalEditor->SDIExternalEditor))
  2937. {
  2938. // no need for handle
  2939. if (Process != NULL)
  2940. {
  2941. DebugCheck(CloseHandle(Process));
  2942. }
  2943. Process = INVALID_HANDLE_VALUE;
  2944. }
  2945. else
  2946. {
  2947. if (Process == NULL)
  2948. {
  2949. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2950. }
  2951. }
  2952. if (Side == osRemote)
  2953. {
  2954. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2955. }
  2956. }
  2957. }
  2958. //---------------------------------------------------------------------------
  2959. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2960. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2961. void * /*Arg*/)
  2962. {
  2963. if (Token != NULL)
  2964. {
  2965. EditorFormFileSave(static_cast<TForm *>(Token));
  2966. }
  2967. }
  2968. //---------------------------------------------------------------------------
  2969. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2970. {
  2971. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2972. {
  2973. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2974. }
  2975. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2976. }
  2977. //---------------------------------------------------------------------------
  2978. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2979. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2980. void * Arg)
  2981. {
  2982. if ((Token != NULL) &&
  2983. IsEditorFormModified(static_cast<TForm *>(Token)))
  2984. {
  2985. *static_cast<bool *>(Arg) = true;
  2986. }
  2987. }
  2988. //---------------------------------------------------------------------------
  2989. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2990. TObject * /*Sender*/, bool & Modified)
  2991. {
  2992. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2993. {
  2994. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2995. {
  2996. Modified = true;
  2997. }
  2998. }
  2999. if (!Modified)
  3000. {
  3001. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3002. }
  3003. }
  3004. //---------------------------------------------------------------------------
  3005. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3006. {
  3007. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3008. }
  3009. //---------------------------------------------------------------------------
  3010. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3011. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3012. UnicodeString & Result, UnicodeString & RootDirectory)
  3013. {
  3014. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3015. {
  3016. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3017. Result = RootDirectory;
  3018. }
  3019. else
  3020. {
  3021. RootDirectory = L"";
  3022. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3023. Result = IncludeTrailingBackslash(Result);
  3024. }
  3025. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3026. {
  3027. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3028. }
  3029. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3030. {
  3031. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3032. {
  3033. RemoteDirectory.Delete(1, 1);
  3034. }
  3035. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3036. }
  3037. if (!ForceDirectories(ApiPath(Result)))
  3038. {
  3039. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3040. }
  3041. }
  3042. //---------------------------------------------------------------------------
  3043. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3044. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3045. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3046. {
  3047. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3048. if (ForceText)
  3049. {
  3050. CopyParam.TransferMode = tmAscii;
  3051. }
  3052. TemporaryFileCopyParam(CopyParam);
  3053. if (AllFiles)
  3054. {
  3055. CopyParam.IncludeFileMask = TFileMasks();
  3056. }
  3057. if (TempDir.IsEmpty())
  3058. {
  3059. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3060. }
  3061. DebugAssert(!FAutoOperation);
  3062. FAutoOperation = AutoOperation;
  3063. Terminal->ExceptionOnFail = true;
  3064. try
  3065. {
  3066. try
  3067. {
  3068. // turn off confirmations, as for MDI editors we may possibly download
  3069. // the same file over
  3070. Terminal->CopyToLocal(
  3071. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3072. if (GetTargetNames)
  3073. {
  3074. for (int i = 0; i < FileList->Count; i++)
  3075. {
  3076. FileList->Strings[i] =
  3077. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3078. }
  3079. }
  3080. }
  3081. catch(...)
  3082. {
  3083. if (!RootTempDir.IsEmpty())
  3084. {
  3085. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3086. }
  3087. throw;
  3088. }
  3089. }
  3090. __finally
  3091. {
  3092. FAutoOperation = false;
  3093. Terminal->ExceptionOnFail = false;
  3094. }
  3095. }
  3096. //---------------------------------------------------------------------------
  3097. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3098. {
  3099. // Do not waste time checking for default editor list the next time,
  3100. // or testing if default editor is not notepad.
  3101. bool TryNextTime = false;
  3102. UnicodeString UsageState;
  3103. if (!WinConfiguration->EditorList->IsDefaultList())
  3104. {
  3105. UsageState = "H";
  3106. }
  3107. else
  3108. {
  3109. UnicodeString Executable;
  3110. UnicodeString ExecutableDescription;
  3111. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3112. {
  3113. UnicodeString Message =
  3114. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3115. unsigned int Answer =
  3116. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3117. if (Answer != qaOK)
  3118. {
  3119. UsageState = "R";
  3120. }
  3121. else
  3122. {
  3123. UsageState = "A";
  3124. TEditorData EditorData;
  3125. EditorData.Editor = edExternal;
  3126. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3127. EditorData.DecideExternalEditorText();
  3128. TEditorList EditorList;
  3129. EditorList = *WinConfiguration->EditorList;
  3130. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3131. WinConfiguration->EditorList = &EditorList;
  3132. }
  3133. }
  3134. }
  3135. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3136. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3137. }
  3138. //---------------------------------------------------------------------------
  3139. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3140. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3141. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3142. {
  3143. if (ExecuteFileBy == efDefaultEditor)
  3144. {
  3145. if (!WinConfiguration->OfferedEditorAutoConfig)
  3146. {
  3147. EditorAutoConfig();
  3148. }
  3149. const TEditorPreferences * Editor =
  3150. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3151. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3152. {
  3153. ExecuteFileBy = efInternalEditor;
  3154. ExternalEditor = NULL;
  3155. }
  3156. else if (Editor->Data->Editor == edOpen)
  3157. {
  3158. ExecuteFileBy = efShell;
  3159. ExternalEditor = NULL;
  3160. }
  3161. else
  3162. {
  3163. ExecuteFileBy = efExternalEditor;
  3164. ExternalEditor = Editor->Data;
  3165. }
  3166. }
  3167. }
  3168. //---------------------------------------------------------------------------
  3169. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3170. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3171. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3172. {
  3173. UnicodeString OriginalFileName;
  3174. UnicodeString LocalRootDirectory;
  3175. UnicodeString RemoteDirectory;
  3176. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3177. (Side == osLocal), MaskParams);
  3178. UnicodeString Counter;
  3179. UnicodeString LocalFileName;
  3180. bool Handled = false;
  3181. if (Side == osRemote)
  3182. {
  3183. // We need to trim VMS version here, so that we use name without version
  3184. // when uploading back to create a new version of the file
  3185. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3186. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3187. TObject * Token = NULL;
  3188. UnicodeString LocalDirectory;
  3189. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3190. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3191. LocalDirectory))
  3192. {
  3193. if (Token != NULL)
  3194. {
  3195. TForm * Form = dynamic_cast<TForm *>(Token);
  3196. if (Form->WindowState == wsMinimized)
  3197. {
  3198. ShowWindow(Form->Handle, SW_RESTORE);
  3199. }
  3200. else
  3201. {
  3202. Form->SetFocus();
  3203. }
  3204. Handled = true;
  3205. }
  3206. else
  3207. {
  3208. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3209. }
  3210. }
  3211. if (!Handled)
  3212. {
  3213. TStringList * FileList1 = new TStringList();
  3214. try
  3215. {
  3216. FileList1->AddObject(FullFileName, Object);
  3217. TemporarilyDownloadFiles(FileList1,
  3218. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3219. LocalRootDirectory, LocalDirectory, true, true, true);
  3220. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3221. }
  3222. __finally
  3223. {
  3224. delete FileList1;
  3225. }
  3226. switch (ExecuteFileBy)
  3227. {
  3228. case efShell:
  3229. Counter = "RemoteFilesExecuted";
  3230. break;
  3231. case efInternalEditor:
  3232. Counter = "RemoteFilesOpenedInInternalEditor";
  3233. break;
  3234. case efExternalEditor:
  3235. Counter = "RemoteFilesOpenedInExternalEditor";
  3236. break;
  3237. default:
  3238. DebugFail();
  3239. }
  3240. }
  3241. }
  3242. else
  3243. {
  3244. LocalFileName = FullFileName;
  3245. OriginalFileName = ExtractFileName(FullFileName);
  3246. switch (ExecuteFileBy)
  3247. {
  3248. case efShell:
  3249. Counter = "LocalFilesExecuted";
  3250. break;
  3251. case efInternalEditor:
  3252. Counter = "LocalFilesOpenedInInternalEditor";
  3253. break;
  3254. case efExternalEditor:
  3255. Counter = "LocalFilesOpenedInExternalEditor";
  3256. break;
  3257. default:
  3258. DebugFail();
  3259. }
  3260. }
  3261. if (!Handled)
  3262. {
  3263. Configuration->Usage->Inc(Counter);
  3264. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3265. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3266. }
  3267. }
  3268. //---------------------------------------------------------------------------
  3269. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3270. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3271. bool AllSelected, bool OnFocused)
  3272. {
  3273. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3274. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3275. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3276. Side = GetSide(Side);
  3277. TCustomDirView * DView = DirView(Side);
  3278. TStrings * FileList = AllSelected ?
  3279. DView->CreateFileList(OnFocused, Side == osLocal) :
  3280. DView->CreateFocusedFileList(Side == osLocal);
  3281. try
  3282. {
  3283. DebugAssert(AllSelected || (FileList->Count == 1));
  3284. for (int i = 0; i < FileList->Count; i++)
  3285. {
  3286. UnicodeString ListFileName = FileList->Strings[i];
  3287. UnicodeString FileNameOnly = (Side == osRemote) ?
  3288. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3289. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3290. if (!DView->ItemIsDirectory(Item))
  3291. {
  3292. UnicodeString FullFileName;
  3293. if (Side == osRemote)
  3294. {
  3295. FullFileName = RemoteDirView->Path + ListFileName;
  3296. }
  3297. else
  3298. {
  3299. FullFileName = ListFileName;
  3300. }
  3301. TFileMasks::TParams MaskParams;
  3302. MaskParams.Size = DView->ItemFileSize(Item);
  3303. TDateTimePrecision Precision;
  3304. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3305. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3306. FileList->Objects[i], MaskParams);
  3307. }
  3308. }
  3309. }
  3310. __finally
  3311. {
  3312. delete FileList;
  3313. }
  3314. }
  3315. //---------------------------------------------------------------------------
  3316. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3317. {
  3318. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3319. CopyParam.FileNameCase = ncNoChange;
  3320. CopyParam.PreserveRights = false;
  3321. CopyParam.PreserveReadOnly = false;
  3322. CopyParam.ReplaceInvalidChars = true;
  3323. CopyParam.IncludeFileMask = TFileMasks();
  3324. CopyParam.NewerOnly = false;
  3325. CopyParam.FileMask = L"";
  3326. }
  3327. //---------------------------------------------------------------------------
  3328. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3329. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3330. {
  3331. TTerminalManager * Manager = TTerminalManager::Instance();
  3332. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3333. {
  3334. if (!NonVisualDataModule->Busy)
  3335. {
  3336. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3337. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3338. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3339. if (Data->Terminal == NULL)
  3340. {
  3341. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3342. if (SameSiteTerminal != NULL)
  3343. {
  3344. UnicodeString Message =
  3345. FMTLOAD(EDIT_SESSION_REATTACH,
  3346. (FileNameOnly, Data->SessionName, FileNameOnly));
  3347. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3348. {
  3349. Data->Terminal = Terminal;
  3350. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3351. Data->SessionName = Terminal->SessionData->SessionName;
  3352. // We might also overwrite session data
  3353. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3354. }
  3355. else
  3356. {
  3357. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3358. Abort();
  3359. }
  3360. }
  3361. }
  3362. // foreground session should reconnect itself
  3363. else if (Terminal != Data->Terminal)
  3364. {
  3365. UnicodeString Message =
  3366. MainInstructions(
  3367. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3368. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3369. {
  3370. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3371. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3372. }
  3373. else
  3374. {
  3375. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3376. Abort();
  3377. }
  3378. }
  3379. }
  3380. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3381. {
  3382. Configuration->Usage->Inc(L"EditInactiveSession");
  3383. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3384. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3385. (ExtractFileName(FileName), Data->SessionName)));
  3386. }
  3387. }
  3388. TStrings * FileList = new TStringList();
  3389. try
  3390. {
  3391. FileList->Add(FileName);
  3392. // consider using the same settings (preset) as when the file was downloaded
  3393. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3394. TemporaryFileCopyParam(CopyParam);
  3395. if (Data->ForceText)
  3396. {
  3397. CopyParam.TransferMode = tmAscii;
  3398. }
  3399. // so i do not need to worry if masking algorithm works in all cases
  3400. // ("" means "copy file name", no masking is actually done)
  3401. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3402. {
  3403. CopyParam.FileMask = L"";
  3404. }
  3405. else
  3406. {
  3407. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3408. }
  3409. DebugAssert(Data->Queue != NULL);
  3410. int Params = cpNoConfirmation | cpTemporary;
  3411. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3412. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3413. QueueItem->CompleteEvent = UploadCompleteEvent;
  3414. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3415. }
  3416. __finally
  3417. {
  3418. delete FileList;
  3419. }
  3420. }
  3421. //---------------------------------------------------------------------------
  3422. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3423. const UnicodeString FileName, const TEditedFileData * Data)
  3424. {
  3425. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3426. DebugAssert(!NonVisualDataModule->Busy);
  3427. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3428. {
  3429. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3430. (ExtractFileName(FileName), Data->SessionName)));
  3431. }
  3432. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3433. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3434. NonVisualDataModule->StartBusy();
  3435. try
  3436. {
  3437. std::unique_ptr<TRemoteFile> File;
  3438. UnicodeString RemoteFileName =
  3439. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3440. FTerminal->ExceptionOnFail = true;
  3441. try
  3442. {
  3443. TRemoteFile * AFile = NULL;
  3444. FTerminal->ReadFile(RemoteFileName, AFile);
  3445. File.reset(AFile);
  3446. if (!File->HaveFullFileName)
  3447. {
  3448. File->FullFileName = RemoteFileName;
  3449. }
  3450. }
  3451. __finally
  3452. {
  3453. FTerminal->ExceptionOnFail = false;
  3454. }
  3455. std::unique_ptr<TStrings> FileList(new TStringList());
  3456. FileList->AddObject(RemoteFileName, File.get());
  3457. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3458. UnicodeString TempDir = ExtractFilePath(FileName);
  3459. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3460. TempDir, true, true, true);
  3461. // sanity check, the target file name should be still the same
  3462. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3463. }
  3464. __finally
  3465. {
  3466. NonVisualDataModule->EndBusy();
  3467. // it actually may not exist anymore...
  3468. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3469. }
  3470. }
  3471. //---------------------------------------------------------------------------
  3472. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3473. const TEditedFileData * Data, bool & KeepOpen)
  3474. {
  3475. // Command is set for external editors only (not for "shell" open).
  3476. if (!Data->Command.IsEmpty())
  3477. {
  3478. bool AnyFound = false;
  3479. bool AnyMDI = false;
  3480. bool AnyNonMDI = false;
  3481. bool AnyDetect = false;
  3482. TEditorList * EditorList = new TEditorList();
  3483. try
  3484. {
  3485. *EditorList = *WinConfiguration->EditorList;
  3486. for (int i = 0; i < EditorList->Count; i++)
  3487. {
  3488. const TEditorPreferences * Editor = EditorList->Editors[i];
  3489. if ((Editor->Data->Editor == edExternal) &&
  3490. (Editor->Data->ExternalEditor == Data->Command))
  3491. {
  3492. AnyFound = true;
  3493. if (Editor->Data->SDIExternalEditor)
  3494. {
  3495. AnyNonMDI = true;
  3496. if (Editor->Data->DetectMDIExternalEditor)
  3497. {
  3498. AnyDetect = true;
  3499. }
  3500. }
  3501. else
  3502. {
  3503. AnyMDI = true;
  3504. }
  3505. }
  3506. }
  3507. bool EnableMDI = false;
  3508. bool DisableDetect = false;
  3509. if (AnyMDI)
  3510. {
  3511. KeepOpen = true;
  3512. if (AnyNonMDI)
  3513. {
  3514. // there is at least one instance of the editor with MDI support enabled,
  3515. // and one with disabled, enable it for all instances
  3516. EnableMDI = true;
  3517. }
  3518. }
  3519. else if (AnyFound && !AnyDetect)
  3520. {
  3521. // at least once instance found but all have MDI autodetection disabled
  3522. // => close the file (default action)
  3523. }
  3524. else
  3525. {
  3526. // no instance of the editor has MDI support enabled
  3527. TMessageParams Params;
  3528. if (AnyFound)
  3529. {
  3530. // there is at least one instance of the editor
  3531. Params.Params |= mpNeverAskAgainCheck;
  3532. }
  3533. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3534. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3535. switch (Answer)
  3536. {
  3537. case qaNeverAskAgain:
  3538. DisableDetect = true;
  3539. break;
  3540. case qaNo:
  3541. EnableMDI = true;
  3542. KeepOpen = true;
  3543. break;
  3544. }
  3545. }
  3546. if (AnyFound && (EnableMDI || DisableDetect))
  3547. {
  3548. bool Changed = false;
  3549. for (int i = 0; i < EditorList->Count; i++)
  3550. {
  3551. const TEditorPreferences * Editor = EditorList->Editors[i];
  3552. if ((Editor->Data->Editor == edExternal) &&
  3553. (Editor->Data->ExternalEditor == Data->Command) &&
  3554. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3555. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3556. {
  3557. Changed = true;
  3558. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3559. if (EnableMDI)
  3560. {
  3561. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3562. }
  3563. if (DisableDetect)
  3564. {
  3565. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3566. }
  3567. EditorList->Change(i, UpdatedEditor);
  3568. }
  3569. }
  3570. if (Changed)
  3571. {
  3572. WinConfiguration->EditorList = EditorList;
  3573. }
  3574. }
  3575. }
  3576. __finally
  3577. {
  3578. delete EditorList;
  3579. }
  3580. }
  3581. else
  3582. {
  3583. // "open" case
  3584. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3585. qaOK, HELP_APP_EARLY_CLOSED);
  3586. }
  3587. }
  3588. //---------------------------------------------------------------------------
  3589. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3590. {
  3591. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3592. }
  3593. //---------------------------------------------------------------------------
  3594. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3595. {
  3596. SideEnter(osRemote);
  3597. }
  3598. //---------------------------------------------------------------------------
  3599. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3600. {
  3601. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3602. SideEnter(osRemote);
  3603. }
  3604. //---------------------------------------------------------------------------
  3605. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3606. {
  3607. FCurrentSide = Side;
  3608. if (Visible)
  3609. {
  3610. UpdateControls();
  3611. }
  3612. }
  3613. //---------------------------------------------------------------------------
  3614. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3615. TStrings * FileList, bool Alternative)
  3616. {
  3617. TCustomDirView * DView = DirView(Side);
  3618. DView->SaveSelection();
  3619. DView->SaveSelectedNames();
  3620. DebugAssert(!FAlternativeDelete);
  3621. FAlternativeDelete = Alternative;
  3622. try
  3623. {
  3624. if (Side == osRemote)
  3625. {
  3626. DebugAssert(Terminal != NULL);
  3627. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3628. }
  3629. else
  3630. {
  3631. try
  3632. {
  3633. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3634. }
  3635. __finally
  3636. {
  3637. ReloadLocalDirectory();
  3638. }
  3639. }
  3640. FAlternativeDelete = false;
  3641. }
  3642. catch(...)
  3643. {
  3644. FAlternativeDelete = false;
  3645. DView->DiscardSavedSelection();
  3646. throw;
  3647. }
  3648. DView->RestoreSelection();
  3649. }
  3650. //---------------------------------------------------------------------------
  3651. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3652. {
  3653. DebugAssert(Terminal);
  3654. RemoteDirView->SaveSelection();
  3655. RemoteDirView->SaveSelectedNames();
  3656. try
  3657. {
  3658. if (Lock)
  3659. {
  3660. Terminal->LockFiles(FileList);
  3661. }
  3662. else
  3663. {
  3664. Terminal->UnlockFiles(FileList);
  3665. }
  3666. }
  3667. catch(...)
  3668. {
  3669. RemoteDirView->DiscardSavedSelection();
  3670. throw;
  3671. }
  3672. RemoteDirView->RestoreSelection();
  3673. }
  3674. //---------------------------------------------------------------------------
  3675. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3676. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3677. bool NoConfirmation, bool Move)
  3678. {
  3679. DebugAssert(Terminal != NULL);
  3680. // update Terminal->StateData->RemoteDirectory
  3681. UpdateTerminal(Terminal);
  3682. if (Session == NULL)
  3683. {
  3684. Session = TTerminalManager::Instance()->ActiveTerminal;
  3685. }
  3686. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3687. {
  3688. if (RemoteDriveView->DropTarget != NULL)
  3689. {
  3690. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3691. }
  3692. else if (RemoteDirView->DropTarget != NULL)
  3693. {
  3694. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3695. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3696. }
  3697. else
  3698. {
  3699. Target = RemoteDirView->Path;
  3700. }
  3701. }
  3702. else
  3703. {
  3704. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3705. Target = ManagedTerminal->StateData->RemoteDirectory;
  3706. }
  3707. Target = UnixIncludeTrailingBackslash(Target);
  3708. if (FileList->Count == 1)
  3709. {
  3710. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3711. }
  3712. else
  3713. {
  3714. FileMask = AnyMask;
  3715. }
  3716. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3717. bool Result = true;
  3718. if (!NoConfirmation)
  3719. {
  3720. bool Multi = (FileList->Count > 1);
  3721. if (Move)
  3722. {
  3723. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3724. }
  3725. else
  3726. {
  3727. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3728. TStrings * Directories = new TStringList;
  3729. try
  3730. {
  3731. for (int Index = 0; Index < Sessions->Count; Index++)
  3732. {
  3733. TManagedTerminal * Terminal =
  3734. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3735. Directories->Add(Terminal->StateData->RemoteDirectory);
  3736. }
  3737. TDirectRemoteCopy AllowDirectCopy;
  3738. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3739. {
  3740. DebugAssert(DirectCopy);
  3741. AllowDirectCopy = drcAllow;
  3742. }
  3743. else if (FTerminal->IsCapable[fcSecondaryShell])
  3744. {
  3745. DebugAssert(DirectCopy);
  3746. AllowDirectCopy = drcConfirmCommandSession;
  3747. }
  3748. else
  3749. {
  3750. DebugAssert(!DirectCopy);
  3751. AllowDirectCopy = drcDisallow;
  3752. }
  3753. void * ASession = Session;
  3754. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3755. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3756. Session = static_cast<TTerminal *>(ASession);
  3757. }
  3758. __finally
  3759. {
  3760. delete Directories;
  3761. }
  3762. }
  3763. }
  3764. return Result;
  3765. }
  3766. //---------------------------------------------------------------------------
  3767. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3768. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3769. {
  3770. bool DirectCopy;
  3771. UnicodeString Target, FileMask;
  3772. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3773. if (Result)
  3774. {
  3775. if (!Move && !DirectCopy)
  3776. {
  3777. Configuration->Usage->Inc("RemoteCopyTemp");
  3778. UnicodeString RootTempDir;
  3779. UnicodeString TempDir;
  3780. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3781. TStrings * TemporaryFilesList = new TStringList();
  3782. try
  3783. {
  3784. TMakeLocalFileListParams MakeFileListParam;
  3785. MakeFileListParam.FileList = TemporaryFilesList;
  3786. MakeFileListParam.FileTimes = NULL;
  3787. MakeFileListParam.IncludeDirs = true;
  3788. MakeFileListParam.Recursive = false;
  3789. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3790. TTerminalManager::Instance()->ActiveTerminal = Session;
  3791. if (TemporaryFilesList->Count > 0)
  3792. {
  3793. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3794. CopyParam.FileMask = FileMask;
  3795. DebugAssert(!FAutoOperation);
  3796. FAutoOperation = true;
  3797. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3798. }
  3799. }
  3800. __finally
  3801. {
  3802. delete TemporaryFilesList;
  3803. FAutoOperation = false;
  3804. if (!RootTempDir.IsEmpty())
  3805. {
  3806. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3807. }
  3808. }
  3809. }
  3810. else
  3811. {
  3812. RemoteDirView->SaveSelection();
  3813. RemoteDirView->SaveSelectedNames();
  3814. try
  3815. {
  3816. if (Move)
  3817. {
  3818. Configuration->Usage->Inc("RemoteMove");
  3819. Terminal->MoveFiles(FileList, Target, FileMask);
  3820. }
  3821. else
  3822. {
  3823. Configuration->Usage->Inc("RemoteCopyDirect");
  3824. DebugAssert(DirectCopy);
  3825. DebugAssert(Session == FTerminal);
  3826. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3827. FTerminal->CommandSessionOpened ||
  3828. CommandSessionFallback())
  3829. {
  3830. Terminal->CopyFiles(FileList, Target, FileMask);
  3831. }
  3832. }
  3833. }
  3834. catch(...)
  3835. {
  3836. RemoteDirView->DiscardSavedSelection();
  3837. throw;
  3838. }
  3839. RemoteDirView->RestoreSelection();
  3840. }
  3841. }
  3842. return Result;
  3843. }
  3844. //---------------------------------------------------------------------------
  3845. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3846. const UnicodeString & Path, TRemoteProperties & Properties)
  3847. {
  3848. Properties.Valid = Properties.Valid << vpEncrypt;
  3849. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3850. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3851. }
  3852. //---------------------------------------------------------------------------
  3853. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3854. {
  3855. Side = GetSide(Side);
  3856. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3857. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3858. UnicodeString Name = LoadStr(NEW_FOLDER);
  3859. int AllowedChanges =
  3860. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3861. bool SaveSettings = false;
  3862. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3863. {
  3864. TWindowLock Lock(this);
  3865. if (Side == osRemote)
  3866. {
  3867. if (SaveSettings)
  3868. {
  3869. GUIConfiguration->NewDirectoryProperties = Properties;
  3870. }
  3871. CreateRemoteDirectory(Name, Properties);
  3872. }
  3873. else
  3874. {
  3875. DirView(Side)->CreateDirectory(Name);
  3876. }
  3877. }
  3878. }
  3879. //---------------------------------------------------------------------------
  3880. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3881. {
  3882. TWindowLock Lock(this);
  3883. DirView(Side)->ExecuteHomeDirectory();
  3884. }
  3885. //---------------------------------------------------------------------------
  3886. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3887. {
  3888. DoOpenDirectoryDialog(odBrowse, Side);
  3889. }
  3890. //---------------------------------------------------------------------------
  3891. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3892. {
  3893. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3894. bool Result = !Path.IsEmpty();
  3895. if (Result)
  3896. {
  3897. // While we might get here when the session is closed (from location profiles),
  3898. // it's not a problem as the Path setter is noop then
  3899. DirView(Side)->Path = Path;
  3900. }
  3901. return Result;
  3902. }
  3903. //---------------------------------------------------------------------------
  3904. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3905. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3906. bool & Close)
  3907. {
  3908. // terminal can be already closed (e.g. dropped connection)
  3909. if (Terminal != NULL)
  3910. {
  3911. try
  3912. {
  3913. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3914. }
  3915. catch(...)
  3916. {
  3917. if (!Terminal->Active)
  3918. {
  3919. Close = true;
  3920. }
  3921. throw;
  3922. }
  3923. }
  3924. }
  3925. //---------------------------------------------------------------------------
  3926. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3927. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3928. bool & Close)
  3929. {
  3930. // terminal can be already closed (e.g. dropped connection)
  3931. if (Terminal != NULL)
  3932. {
  3933. Configuration->Usage->Inc(L"ChecksumCalculated");
  3934. try
  3935. {
  3936. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3937. }
  3938. catch(...)
  3939. {
  3940. if (!Terminal->Active)
  3941. {
  3942. Close = true;
  3943. }
  3944. throw;
  3945. }
  3946. }
  3947. }
  3948. //---------------------------------------------------------------------------
  3949. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3950. {
  3951. bool Result;
  3952. if (Side == osRemote)
  3953. {
  3954. TRemoteTokenList * GroupList = NULL;
  3955. TRemoteTokenList * UserList = NULL;
  3956. try
  3957. {
  3958. TRemoteProperties CurrentProperties;
  3959. if (Terminal->LoadFilesProperties(FileList))
  3960. {
  3961. RemoteDirView->Invalidate();
  3962. }
  3963. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3964. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3965. if (CapableGroupChanging || CapableOwnerChanging)
  3966. {
  3967. if (CapableGroupChanging)
  3968. {
  3969. GroupList = Terminal->Groups->Duplicate();
  3970. }
  3971. if (CapableOwnerChanging)
  3972. {
  3973. UserList = Terminal->Users->Duplicate();
  3974. }
  3975. TRemoteDirectory * Files = Terminal->Files;
  3976. int Count = Files->Count;
  3977. if (Count > 100)
  3978. {
  3979. Count = 100;
  3980. }
  3981. for (int Index = 0; Index < Count; Index++)
  3982. {
  3983. TRemoteFile * File = Files->Files[Index];
  3984. if (CapableGroupChanging)
  3985. {
  3986. GroupList->AddUnique(File->Group);
  3987. }
  3988. if (CapableOwnerChanging)
  3989. {
  3990. UserList->AddUnique(File->Owner);
  3991. }
  3992. }
  3993. // if we haven't collected tokens for all files in current directory,
  3994. // make sure we collect them at least for all selected files.
  3995. // (note that so far the files in FileList has to be from current direcotry)
  3996. if (Count < Files->Count)
  3997. {
  3998. for (int Index = 0; Index < FileList->Count; Index++)
  3999. {
  4000. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4001. if (CapableGroupChanging)
  4002. {
  4003. GroupList->AddUnique(File->Group);
  4004. }
  4005. if (CapableOwnerChanging)
  4006. {
  4007. UserList->AddUnique(File->Owner);
  4008. }
  4009. }
  4010. }
  4011. }
  4012. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4013. int Flags = 0;
  4014. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4015. if (CapableOwnerChanging) Flags |= cpOwner;
  4016. if (CapableGroupChanging) Flags |= cpGroup;
  4017. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4018. if (Terminal->IsCapable[fcCalculatingChecksum])
  4019. {
  4020. CalculateChecksumEvent = CalculateChecksum;
  4021. }
  4022. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4023. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4024. TRemoteProperties NewProperties = CurrentProperties;
  4025. Result =
  4026. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4027. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4028. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4029. CalculateSize, CalculateChecksumEvent);
  4030. if (Result)
  4031. {
  4032. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4033. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4034. }
  4035. }
  4036. __finally
  4037. {
  4038. delete GroupList;
  4039. delete UserList;
  4040. }
  4041. }
  4042. else
  4043. {
  4044. DirView(Side)->DisplayPropertiesMenu();
  4045. Result = true;
  4046. }
  4047. return Result;
  4048. }
  4049. //---------------------------------------------------------------------------
  4050. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4051. {
  4052. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4053. {
  4054. FIgnoreNextDialogChar = Key;
  4055. }
  4056. Key = 0;
  4057. }
  4058. //---------------------------------------------------------------------------
  4059. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4060. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4061. {
  4062. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4063. if (Command != NULL)
  4064. {
  4065. KeyProcessed(Key, Shift);
  4066. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4067. {
  4068. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4069. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4070. false, false, const_cast<TCustomCommandType *>(Command));
  4071. }
  4072. }
  4073. }
  4074. //---------------------------------------------------------------------------
  4075. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4076. {
  4077. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4078. {
  4079. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4080. }
  4081. if (!DirView(osCurrent)->IsEditing())
  4082. {
  4083. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4084. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4085. {
  4086. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4087. if (((Action->ShortCut == KeyShortCut) ||
  4088. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4089. AllowedAction(Action, aaShortCut))
  4090. {
  4091. // Has to be called before the action as the dialog char is already in queue.
  4092. // So when the action consumes message queue, we already need to have the
  4093. // FIgnoreNextDialogChar set
  4094. KeyProcessed(Key, Shift);
  4095. // Reset reference to previous component (when menu/toolbar was clicked).
  4096. // Needed to detect that action was invoked by keyboard shortcut
  4097. // in TNonVisualDataModule::ExplorerActionsExecute
  4098. Action->ActionComponent = NULL;
  4099. Action->Execute();
  4100. return;
  4101. }
  4102. }
  4103. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4104. {
  4105. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4106. {
  4107. KeyProcessed(Key, Shift);
  4108. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4109. return;
  4110. }
  4111. }
  4112. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4113. {
  4114. KeyProcessed(Key, Shift);
  4115. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4116. }
  4117. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4118. if (IsCustomShortCut(CustomShortCut))
  4119. {
  4120. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4121. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4122. if (WinConfiguration->SharedBookmarks != NULL)
  4123. {
  4124. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4125. if ((Bookmark != NULL) &&
  4126. OpenBookmark(FCurrentSide, Bookmark))
  4127. {
  4128. KeyProcessed(Key, Shift);
  4129. }
  4130. }
  4131. }
  4132. }
  4133. TForm::KeyDown(Key, Shift);
  4134. }
  4135. //---------------------------------------------------------------------------
  4136. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4137. {
  4138. DebugAssert(Terminal != NULL);
  4139. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4140. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4141. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4142. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4143. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4144. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4145. // expanded from ?: to avoid memory leaks
  4146. if (SecurityEnabled)
  4147. {
  4148. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4149. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4150. }
  4151. else
  4152. {
  4153. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4154. }
  4155. if (FileSystemInfo.ProtocolName.IsEmpty())
  4156. {
  4157. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4158. }
  4159. else
  4160. {
  4161. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4162. }
  4163. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4164. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4165. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4166. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4167. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4168. UnicodeString CompressionStr = CSCompressionStr;
  4169. if (CSCompressionStr != SCCompressionStr)
  4170. {
  4171. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4172. }
  4173. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4174. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4175. UpdateStatusBar();
  4176. }
  4177. //---------------------------------------------------------------------------
  4178. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4179. {
  4180. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4181. DebugAssert(SessionStatusBar != NULL);
  4182. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4183. {
  4184. // note: (Terminal->Status < sshReady) currently never happens here,
  4185. // so STATUS_CONNECTING is never used
  4186. SessionStatusBar->SimplePanel = true;
  4187. SessionStatusBar->SimpleText = LoadStr(
  4188. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4189. }
  4190. else
  4191. {
  4192. DebugAssert(Terminal);
  4193. SessionStatusBar->SimplePanel = false;
  4194. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4195. if (!FNote.IsEmpty())
  4196. {
  4197. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4198. }
  4199. else
  4200. {
  4201. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4202. }
  4203. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4204. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4205. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4206. }
  4207. }
  4208. //---------------------------------------------------------------------------
  4209. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4210. {
  4211. Panel->Caption = L"";
  4212. }
  4213. //---------------------------------------------------------------------------
  4214. void __fastcall TCustomScpExplorerForm::Idle()
  4215. {
  4216. if (FShowing || FStandaloneEditing)
  4217. {
  4218. FEditorManager->Check();
  4219. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4220. // that needs to know if queue view is visible (and it may be closed after queue update)
  4221. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4222. }
  4223. if (FShowing)
  4224. {
  4225. if (!NonVisualDataModule->Busy &&
  4226. // Menu is opened or drag&drop is going on
  4227. (Mouse->Capture == NULL))
  4228. {
  4229. if (FRefreshRemoteDirectory)
  4230. {
  4231. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4232. {
  4233. Terminal->RefreshDirectory();
  4234. }
  4235. FRefreshRemoteDirectory = false;
  4236. }
  4237. if (FRefreshLocalDirectory)
  4238. {
  4239. ReloadLocalDirectory();
  4240. FRefreshLocalDirectory = false;
  4241. }
  4242. if (WinConfiguration->RefreshRemotePanel)
  4243. {
  4244. TManagedTerminal * ManagedTerminal =
  4245. dynamic_cast<TManagedTerminal *>(Terminal);
  4246. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4247. (Now() - ManagedTerminal->DirectoryLoaded >
  4248. WinConfiguration->RefreshRemotePanelInterval))
  4249. {
  4250. RemoteDirView->ReloadDirectory();
  4251. }
  4252. }
  4253. }
  4254. }
  4255. if (FShowing || FStandaloneEditing)
  4256. {
  4257. if (FQueueStatusInvalidated)
  4258. {
  4259. UpdateQueueStatus(false);
  4260. }
  4261. RefreshQueueItems();
  4262. }
  4263. if (FShowing)
  4264. {
  4265. UpdateStatusBar();
  4266. }
  4267. FIgnoreNextDialogChar = 0;
  4268. }
  4269. //---------------------------------------------------------------------------
  4270. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4271. {
  4272. try
  4273. {
  4274. FUserActionTimer->Enabled = false;
  4275. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4276. {
  4277. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4278. {
  4279. FPendingQueueActionItem->ProcessUserAction();
  4280. }
  4281. }
  4282. }
  4283. __finally
  4284. {
  4285. FPendingQueueActionItem = NULL;
  4286. }
  4287. }
  4288. //---------------------------------------------------------------------------
  4289. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4290. {
  4291. if (WinConfiguration->MinimizeToTray)
  4292. {
  4293. UpdateTrayIcon();
  4294. FTrayIcon->Visible = true;
  4295. if (Visible)
  4296. {
  4297. ShowWindow(Handle, SW_HIDE);
  4298. }
  4299. }
  4300. }
  4301. //---------------------------------------------------------------------------
  4302. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4303. {
  4304. // WORKAROUND
  4305. // When restoring maximized window from minimization,
  4306. // rarely some controls do not align properly.
  4307. // Two instances seen (both for Commander):
  4308. // - When restoring, window is temporarily narrower (not maximized),
  4309. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4310. // Once maximized already, top dock shinks back, but the session PageControl,
  4311. // do not align up, leaving space between TopDock and PageControl.
  4312. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4313. for (int Index = 0; Index < ControlCount; Index++)
  4314. {
  4315. RealignControl(Controls[Index]);
  4316. }
  4317. if (FTrayIcon->Visible)
  4318. {
  4319. FTrayIcon->Visible = false;
  4320. if (Visible)
  4321. {
  4322. ShowWindow(Handle, SW_SHOW);
  4323. }
  4324. }
  4325. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4326. {
  4327. FNeedSession = false;
  4328. NonVisualDataModule->StartBusy();
  4329. try
  4330. {
  4331. NeedSession(false);
  4332. }
  4333. __finally
  4334. {
  4335. NonVisualDataModule->EndBusy();
  4336. }
  4337. }
  4338. }
  4339. //---------------------------------------------------------------------------
  4340. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4341. {
  4342. FTrayIcon->Hint = GetMainForm()->Caption;
  4343. }
  4344. //---------------------------------------------------------------------------
  4345. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4346. {
  4347. UpdateTrayIcon();
  4348. }
  4349. //---------------------------------------------------------------------------
  4350. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4351. {
  4352. // workaround
  4353. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4354. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4355. // But WM_ACTIVATEAPP message can be received even when
  4356. // the main window is minimized to the tray and internal editor window is focused
  4357. // (after another application was previously active)
  4358. if (::IsIconic(Handle))
  4359. {
  4360. if (!IsAppIconic())
  4361. {
  4362. SetAppIconic(true);
  4363. }
  4364. }
  4365. ::ApplicationRestore();
  4366. Application->BringToFront();
  4367. }
  4368. //---------------------------------------------------------------------------
  4369. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4370. {
  4371. RestoreApp();
  4372. }
  4373. //---------------------------------------------------------------------------
  4374. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4375. {
  4376. if (OpenInNewWindow())
  4377. {
  4378. // todo: Pass FromSite
  4379. ExecuteNewInstance(SessionUrl);
  4380. }
  4381. else
  4382. {
  4383. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4384. }
  4385. }
  4386. //---------------------------------------------------------------------------
  4387. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4388. {
  4389. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4390. // current working directories become defaults here, what is not right
  4391. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4392. StoredSessions->NewSession(SessionName, SessionData.get());
  4393. // modified only, explicit
  4394. StoredSessions->Save(false, true);
  4395. // encode session name because of slashes in hierarchical sessions
  4396. return EncodeUrlString(SessionName);
  4397. }
  4398. //---------------------------------------------------------------------------
  4399. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4400. {
  4401. if (OpenInNewWindow())
  4402. {
  4403. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4404. }
  4405. else
  4406. {
  4407. // current working directories become defaults here, what is not right
  4408. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4409. TTerminalManager * Manager = TTerminalManager::Instance();
  4410. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4411. Manager->ActiveTerminal = Terminal;
  4412. }
  4413. }
  4414. //---------------------------------------------------------------------------
  4415. void __fastcall TCustomScpExplorerForm::RenameSession()
  4416. {
  4417. UnicodeString Name = Terminal->SessionData->SessionName;
  4418. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4419. {
  4420. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4421. TSessionData::ValidateName(Name);
  4422. // This is inconsistent with how color (for example) is handled.
  4423. Terminal->SessionData->Name = Name;
  4424. UpdateControls();
  4425. // Add/Remove distinguishing paths from sessions of the same name.
  4426. DoTerminalListChanged();
  4427. }
  4428. }
  4429. //---------------------------------------------------------------------------
  4430. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4431. {
  4432. DebugAssert(Queue != NULL);
  4433. bool Result = Queue->IsEmpty;
  4434. if (!Result)
  4435. {
  4436. SetFocus();
  4437. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4438. }
  4439. return Result;
  4440. }
  4441. //---------------------------------------------------------------------------
  4442. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4443. {
  4444. return CanCloseQueue(FQueue);
  4445. }
  4446. //---------------------------------------------------------------------------
  4447. void __fastcall TCustomScpExplorerForm::CloseSession()
  4448. {
  4449. if (CanCloseQueue())
  4450. {
  4451. TTerminalManager::Instance()->FreeActiveTerminal();
  4452. }
  4453. }
  4454. //---------------------------------------------------------------------------
  4455. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4456. {
  4457. if (OpenInNewWindow())
  4458. {
  4459. // encode session name because of slashes in hierarchical sessions
  4460. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4461. }
  4462. else
  4463. {
  4464. TTerminalManager * Manager = TTerminalManager::Instance();
  4465. TTerminal * Terminal = Manager->NewTerminal(Data);
  4466. Manager->ActiveTerminal = Terminal;
  4467. }
  4468. }
  4469. //---------------------------------------------------------------------------
  4470. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4471. {
  4472. if (OpenInNewWindow())
  4473. {
  4474. ExecuteNewInstance(Name);
  4475. }
  4476. else
  4477. {
  4478. TTerminalManager * Manager = TTerminalManager::Instance();
  4479. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4480. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4481. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4482. }
  4483. }
  4484. //---------------------------------------------------------------------------
  4485. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4486. bool &CanClose)
  4487. {
  4488. if (Terminal != NULL)
  4489. {
  4490. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4491. {
  4492. unsigned int Result;
  4493. TMessageParams Params(mpNeverAskAgainCheck);
  4494. UnicodeString Message;
  4495. int Answers = qaOK | qaCancel;
  4496. if (TTerminalManager::Instance()->Count > 1)
  4497. {
  4498. if (!WinConfiguration->AutoSaveWorkspace)
  4499. {
  4500. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4501. Answers = qaYes | qaNo | qaCancel;
  4502. }
  4503. else
  4504. {
  4505. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4506. }
  4507. }
  4508. else
  4509. {
  4510. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4511. }
  4512. if (WinConfiguration->AutoSaveWorkspace)
  4513. {
  4514. Message =
  4515. FORMAT("%s\n\n%s", (Message,
  4516. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4517. }
  4518. SetFocus();
  4519. Result = MessageDialog(Message, qtConfirmation,
  4520. Answers, HELP_NONE, &Params);
  4521. if (Result == qaNeverAskAgain)
  4522. {
  4523. WinConfiguration->ConfirmClosingSession = false;
  4524. }
  4525. if (Result == qaNo)
  4526. {
  4527. CanClose = SaveWorkspace(true);
  4528. // note that the workspace will be saved redundatly again from FormClose
  4529. }
  4530. else
  4531. {
  4532. CanClose =
  4533. (Result == qaOK) ||
  4534. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4535. (Result == qaNeverAskAgain);
  4536. }
  4537. }
  4538. if (CanClose)
  4539. {
  4540. CanClose = CanCloseQueue();
  4541. }
  4542. }
  4543. if (CanClose)
  4544. {
  4545. CanClose =
  4546. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4547. FEditorManager->CloseExternalFilesWithoutProcess();
  4548. if (CanClose)
  4549. {
  4550. while (CanClose && (FLocalEditors->Count > 0))
  4551. {
  4552. int PrevCount = FLocalEditors->Count;
  4553. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4554. CanClose = (FLocalEditors->Count < PrevCount);
  4555. }
  4556. if (CanClose)
  4557. {
  4558. CanClose = FEditorManager->Empty(true);
  4559. if (!CanClose)
  4560. {
  4561. SetFocus();
  4562. CanClose =
  4563. (MessageDialog(
  4564. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4565. }
  4566. }
  4567. }
  4568. }
  4569. }
  4570. //---------------------------------------------------------------------------
  4571. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4572. {
  4573. TForm * Form = dynamic_cast<TForm *>(Sender);
  4574. DebugAssert(Form != NULL);
  4575. Form->Close();
  4576. }
  4577. //---------------------------------------------------------------------------
  4578. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4579. {
  4580. TForm * Form = dynamic_cast<TForm *>(Sender);
  4581. delete Form;
  4582. }
  4583. //---------------------------------------------------------------------------
  4584. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4585. {
  4586. while (FLocalEditors->Count > 0)
  4587. {
  4588. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4589. }
  4590. }
  4591. //---------------------------------------------------------------------------
  4592. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4593. TObject *Sender)
  4594. {
  4595. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4596. try
  4597. {
  4598. SetProperties(osRemote, FileList);
  4599. }
  4600. __finally
  4601. {
  4602. delete FileList;
  4603. }
  4604. }
  4605. //---------------------------------------------------------------------------
  4606. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4607. {
  4608. if (value)
  4609. {
  4610. if (Component == fcCustomCommandsBand)
  4611. {
  4612. UpdateCustomCommandsToolbar();
  4613. }
  4614. }
  4615. }
  4616. //---------------------------------------------------------------------------
  4617. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4618. {
  4619. TControl * Control = GetComponent(Component);
  4620. DebugAssert(Control);
  4621. bool Changed = (Control->Visible != value);
  4622. if (Changed)
  4623. {
  4624. ComponentShowing(Component, value);
  4625. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4626. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4627. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4628. if (value)
  4629. {
  4630. int RemainingHeight = Control->Parent->ClientHeight;
  4631. int RemainingWidth = Control->Parent->ClientWidth;
  4632. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4633. {
  4634. TControl * ChildControl = Control->Parent->Controls[i];
  4635. if (ChildControl->Visible)
  4636. {
  4637. switch (ChildControl->Align)
  4638. {
  4639. case alTop:
  4640. case alBottom:
  4641. RemainingHeight -= ChildControl->Height;
  4642. break;
  4643. case alLeft:
  4644. case alRight:
  4645. RemainingWidth -= ChildControl->Width;
  4646. break;
  4647. }
  4648. }
  4649. }
  4650. int Reserve = ScaleByTextHeight(this, 32);
  4651. // queue in explorer, trees in commander
  4652. if (Control->Height > RemainingHeight - Reserve)
  4653. {
  4654. Control->Height = RemainingHeight / 2;
  4655. }
  4656. if (Control->Width > RemainingWidth - Reserve)
  4657. {
  4658. Control->Width = RemainingWidth / 2;
  4659. }
  4660. }
  4661. Control->Visible = value;
  4662. if (WasFocused && Visible)
  4663. {
  4664. DirView(osCurrent)->SetFocus();
  4665. }
  4666. FixControlsPlacement();
  4667. }
  4668. }
  4669. //---------------------------------------------------------------------------
  4670. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4671. {
  4672. TControl * Control = GetComponent(Component);
  4673. if (Control == NULL)
  4674. {
  4675. return false;
  4676. }
  4677. else
  4678. {
  4679. return Control->Visible;
  4680. }
  4681. }
  4682. //---------------------------------------------------------------------------
  4683. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4684. {
  4685. bool Result;
  4686. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4687. {
  4688. Result = !IsUWP();
  4689. }
  4690. else
  4691. {
  4692. Result = true;
  4693. }
  4694. return Result;
  4695. }
  4696. //---------------------------------------------------------------------------
  4697. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4698. {
  4699. if (RemoteDirView->ItemFocused != NULL)
  4700. {
  4701. RemoteDirView->ItemFocused->MakeVisible(false);
  4702. }
  4703. QueueSplitter->Visible = QueuePanel->Visible;
  4704. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4705. }
  4706. //---------------------------------------------------------------------------
  4707. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4708. {
  4709. switch (Component) {
  4710. case fcStatusBar: return RemoteStatusBar;
  4711. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4712. case fcQueueView: return QueuePanel;
  4713. case fcQueueToolbar: return QueueDock;
  4714. case fcRemoteTree: return RemoteDriveView;
  4715. case fcSessionsTabs: return SessionsPageControl;
  4716. default: return NULL;
  4717. }
  4718. }
  4719. //---------------------------------------------------------------------------
  4720. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4721. TObject *Sender, TListColumn *Column, TPoint &Point)
  4722. {
  4723. DebugAssert(NonVisualDataModule && Column && Sender);
  4724. NonVisualDataModule->ListColumn = Column;
  4725. TPopupMenu * DirViewColumnMenu;
  4726. if (Sender == RemoteDirView)
  4727. {
  4728. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4729. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4730. (Column->Index == uvName);
  4731. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4732. (Column->Index == uvSize);
  4733. }
  4734. else
  4735. {
  4736. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4737. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4738. (Column->Index == dvName);
  4739. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4740. (Column->Index == dvSize);
  4741. }
  4742. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4743. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4744. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4745. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4746. }
  4747. //---------------------------------------------------------------------------
  4748. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4749. TObject *Sender, TListItem *Item, bool &AllowExec)
  4750. {
  4751. DoDirViewExecFile(Sender, Item, AllowExec);
  4752. }
  4753. //---------------------------------------------------------------------------
  4754. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4755. TListItem * Item, bool & AllowExec)
  4756. {
  4757. DebugAssert(Sender && Item && Configuration);
  4758. DebugAssert(AllowExec);
  4759. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4760. bool Remote = (ADirView == DirView(osRemote));
  4761. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4762. TOperationSide Side = (Remote ? osRemote : osLocal);
  4763. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4764. // on files only (not directories)
  4765. // and only when symlinks are resolved (apply to remote panel only)
  4766. if (!ADirView->ItemIsDirectory(Item) &&
  4767. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4768. {
  4769. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4770. !FForceExecution &&
  4771. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4772. {
  4773. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4774. {
  4775. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4776. UnlockWindow();
  4777. try
  4778. {
  4779. ExecuteFileOperation(
  4780. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4781. }
  4782. __finally
  4783. {
  4784. LockWindow();
  4785. }
  4786. AllowExec = false;
  4787. }
  4788. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4789. {
  4790. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4791. {
  4792. ExecuteFile(Side, efDefaultEditor);
  4793. AllowExec = false;
  4794. }
  4795. }
  4796. else
  4797. {
  4798. DebugFail();
  4799. }
  4800. }
  4801. // if we have not done anything special, fall back to default behavior
  4802. if (AllowExec)
  4803. {
  4804. if (Remote && !WinConfiguration->DisableOpenEdit)
  4805. {
  4806. ExecuteFile(osRemote, efShell);
  4807. AllowExec = false;
  4808. }
  4809. }
  4810. }
  4811. }
  4812. //---------------------------------------------------------------------------
  4813. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4814. {
  4815. return ((Side == osRemote) || (Side == osCurrent));
  4816. }
  4817. //---------------------------------------------------------------------------
  4818. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4819. {
  4820. DebugFail();
  4821. }
  4822. //---------------------------------------------------------------------------
  4823. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4824. {
  4825. DebugFail();
  4826. }
  4827. //---------------------------------------------------------------------------
  4828. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4829. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4830. {
  4831. TSynchronizeParamType Params;
  4832. Params.LocalDirectory = LocalDirectory;
  4833. Params.RemoteDirectory = RemoteDirectory;
  4834. int UnusedParams =
  4835. (GUIConfiguration->SynchronizeParams &
  4836. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4837. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4838. Params.Options = GUIConfiguration->SynchronizeOptions;
  4839. bool SaveSettings = false;
  4840. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4841. &DoSynchronizeTooManyDirectories);
  4842. DebugAssert(FSynchronizeController == NULL);
  4843. FSynchronizeController = &Controller;
  4844. bool Result;
  4845. try
  4846. {
  4847. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4848. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4849. int Options =
  4850. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4851. DebugAssert(FOnFeedSynchronizeError == NULL);
  4852. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4853. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4854. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4855. if (Result)
  4856. {
  4857. if (SaveSettings)
  4858. {
  4859. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4860. GUIConfiguration->SynchronizeOptions = Params.Options;
  4861. }
  4862. else
  4863. {
  4864. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4865. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4866. FLAGSET(Params.Options, soSynchronize))
  4867. {
  4868. GUIConfiguration->SynchronizeOptions =
  4869. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4870. soSynchronize;
  4871. }
  4872. }
  4873. LocalDirectory = Params.LocalDirectory;
  4874. RemoteDirectory = Params.RemoteDirectory;
  4875. }
  4876. }
  4877. __finally
  4878. {
  4879. FSynchronizeController = NULL;
  4880. DebugAssert(FOnFeedSynchronizeError == NULL);
  4881. FOnFeedSynchronizeError = NULL;
  4882. }
  4883. return Result;
  4884. }
  4885. //---------------------------------------------------------------------------
  4886. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4887. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4888. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4889. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4890. TSynchronizeOptions * Options, bool Full)
  4891. {
  4892. // Keep-up-to-date mode
  4893. if (Terminal->Status == ssOpened)
  4894. {
  4895. try
  4896. {
  4897. int PParams = Params.Params;
  4898. if (!Full)
  4899. {
  4900. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4901. if (FLAGSET(Params.Options, soRecurse))
  4902. {
  4903. PParams |= TTerminal::spSubDirs;
  4904. }
  4905. }
  4906. else
  4907. {
  4908. // if keepuptodate is non-recursive,
  4909. // full sync before has to be non-recursive as well
  4910. if (FLAGCLEAR(Params.Options, soRecurse))
  4911. {
  4912. PParams |= TTerminal::spNoRecurse;
  4913. }
  4914. }
  4915. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4916. PParams, Checklist, Options);
  4917. }
  4918. catch (Exception & E)
  4919. {
  4920. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4921. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4922. {
  4923. // noop, already logged in MoreMessageDialog
  4924. }
  4925. else
  4926. {
  4927. ShowExtendedExceptionEx(Terminal, &E);
  4928. throw;
  4929. }
  4930. }
  4931. }
  4932. }
  4933. //---------------------------------------------------------------------------
  4934. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4935. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4936. const UnicodeString ErrorStr)
  4937. {
  4938. if (!Directory.IsEmpty())
  4939. {
  4940. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4941. }
  4942. else
  4943. {
  4944. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4945. }
  4946. }
  4947. //---------------------------------------------------------------------------
  4948. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4949. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4950. {
  4951. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4952. {
  4953. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4954. }
  4955. else
  4956. {
  4957. TMessageParams Params(mpNeverAskAgainCheck);
  4958. unsigned int Result = MessageDialog(
  4959. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4960. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4961. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4962. {
  4963. MaxDirectories *= 2;
  4964. if (Result == qaNeverAskAgain)
  4965. {
  4966. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4967. }
  4968. }
  4969. else
  4970. {
  4971. Abort();
  4972. }
  4973. }
  4974. }
  4975. //---------------------------------------------------------------------------
  4976. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4977. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4978. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4979. TSynchronizeOptions * Options)
  4980. {
  4981. // Keep-up-to-date mode
  4982. DebugAssert(!FAutoOperation);
  4983. void * BatchStorage;
  4984. BatchStart(BatchStorage);
  4985. FAutoOperation = true;
  4986. bool AnyOperation = false;
  4987. TDateTime StartTime = Now();
  4988. TSynchronizeChecklist * AChecklist = NULL;
  4989. try
  4990. {
  4991. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4992. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4993. {
  4994. FSynchronizeProgressForm->Start();
  4995. }
  4996. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4997. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4998. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4999. Options);
  5000. SAFE_DESTROY(FSynchronizeProgressForm);
  5001. AnyOperation = (AChecklist->CheckedCount > 0);
  5002. if (AnyOperation)
  5003. {
  5004. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5005. CreateProgressForm(&SynchronizeProgress);
  5006. try
  5007. {
  5008. Terminal->SynchronizeApply(
  5009. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5010. SynchronizeProcessedItem, NULL, NULL, NULL);
  5011. }
  5012. __finally
  5013. {
  5014. DestroyProgressForm();
  5015. }
  5016. }
  5017. }
  5018. __finally
  5019. {
  5020. if (Checklist == NULL)
  5021. {
  5022. delete AChecklist;
  5023. }
  5024. else
  5025. {
  5026. *Checklist = AChecklist;
  5027. }
  5028. FAutoOperation = false;
  5029. SAFE_DESTROY(FSynchronizeProgressForm);
  5030. BatchEnd(BatchStorage);
  5031. ReloadLocalDirectory();
  5032. if (AnyOperation)
  5033. {
  5034. OperationComplete(StartTime);
  5035. }
  5036. }
  5037. }
  5038. //---------------------------------------------------------------------------
  5039. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5040. {
  5041. // can be called from command line
  5042. return Visible &&
  5043. ((DirView(osRemote)->SelCount > 0) ||
  5044. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5045. }
  5046. //---------------------------------------------------------------------------
  5047. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5048. {
  5049. LogSynchronizeEvent(Terminal, Message);
  5050. }
  5051. //---------------------------------------------------------------------------
  5052. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5053. int Params, TSynchronizeOptions & Options)
  5054. {
  5055. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5056. {
  5057. Options.Filter = new TStringList();
  5058. Options.Filter->CaseSensitive = false;
  5059. Options.Filter->Duplicates = Types::dupAccept;
  5060. if (DirView(osRemote)->SelCount > 0)
  5061. {
  5062. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5063. }
  5064. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5065. {
  5066. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5067. }
  5068. Options.Filter->Sort();
  5069. }
  5070. }
  5071. //---------------------------------------------------------------------------
  5072. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5073. {
  5074. TCopyParamType Defaults;
  5075. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5076. ConfigStorage->AccessMode = smRead;
  5077. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5078. {
  5079. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5080. }
  5081. ConfigStorage.reset(NULL);
  5082. std::unique_ptr<TStringList> Options(new TStringList());
  5083. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5084. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5085. UnicodeString Result;
  5086. if (Options->Count > 0)
  5087. {
  5088. Result =
  5089. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5090. }
  5091. return Result;
  5092. }
  5093. //---------------------------------------------------------------------------
  5094. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5095. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5096. {
  5097. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5098. UnicodeString AdditionalParams =
  5099. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5100. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5101. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5102. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5103. SerializeCopyParamForCommandLine(CopyParams)));
  5104. ExecuteNewInstance(SessionName, AdditionalParams);
  5105. }
  5106. //---------------------------------------------------------------------------
  5107. struct TSynchronizeParams
  5108. {
  5109. UnicodeString LocalDirectory;
  5110. UnicodeString RemoteDirectory;
  5111. TSynchronizeMode Mode;
  5112. TSynchronizeChecklist * Checklist;
  5113. int Params;
  5114. TCopyParamType * CopyParam;
  5115. TDateTime CollectElapsed;
  5116. TDateTime * StartTime;
  5117. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5118. };
  5119. //---------------------------------------------------------------------------
  5120. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5121. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5122. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5123. {
  5124. DebugAssert(!FAutoOperation);
  5125. void * BatchStorage;
  5126. BatchStart(BatchStorage);
  5127. FAutoOperation = true;
  5128. TFileOperationStatistics Statistics;
  5129. TDateTime Start = Now();
  5130. try
  5131. {
  5132. Params.OnProcessedItem = OnProcessedItem;
  5133. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5134. CreateProgressForm(&SynchronizeProgress);
  5135. Terminal->SynchronizeApply(
  5136. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5137. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5138. &Statistics);
  5139. }
  5140. __finally
  5141. {
  5142. Params.OnProcessedItem = NULL;
  5143. FAutoOperation = false;
  5144. DestroyProgressForm();
  5145. BatchEnd(BatchStorage);
  5146. ReloadLocalDirectory();
  5147. }
  5148. if (WinConfiguration->SynchronizeSummary)
  5149. {
  5150. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5151. // The statistics should be 0 anyway in this case
  5152. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5153. {
  5154. Message += L"\n";
  5155. if (Statistics.FilesUploaded > 0)
  5156. {
  5157. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5158. }
  5159. if (Statistics.FilesDownloaded > 0)
  5160. {
  5161. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5162. }
  5163. if (Statistics.FilesDeletedLocal > 0)
  5164. {
  5165. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5166. }
  5167. if (Statistics.FilesDeletedRemote > 0)
  5168. {
  5169. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5170. }
  5171. }
  5172. TDateTime Elapsed = (Now() - Start);
  5173. Message +=
  5174. L"\n" +
  5175. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5176. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5177. TMessageParams Params(mpNeverAskAgainCheck);
  5178. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5179. if (Result == qaNeverAskAgain)
  5180. {
  5181. WinConfiguration->SynchronizeSummary = false;
  5182. }
  5183. }
  5184. }
  5185. //---------------------------------------------------------------------------
  5186. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5187. {
  5188. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5189. {
  5190. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5191. }
  5192. // Not set in keep-up-to-date mode - Synchronize() method
  5193. if (Token != NULL)
  5194. {
  5195. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5196. if (Params.OnProcessedItem != NULL)
  5197. {
  5198. Params.OnProcessedItem(NULL, ChecklistItem);
  5199. }
  5200. }
  5201. }
  5202. //---------------------------------------------------------------------------
  5203. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5204. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5205. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5206. {
  5207. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5208. *Params.StartTime = Now();
  5209. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5210. }
  5211. //---------------------------------------------------------------------------
  5212. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5213. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5214. {
  5215. // terminal can be already closed (e.g. dropped connection)
  5216. if (Terminal != NULL)
  5217. {
  5218. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5219. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5220. }
  5221. }
  5222. //---------------------------------------------------------------------------
  5223. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5224. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5225. {
  5226. TAutoBatch AutoBatch(this);
  5227. TAutoFlag AutoOperationFlag(FAutoOperation);
  5228. if (Side == osRemote)
  5229. {
  5230. std::unique_ptr<TStrings> FileList(new TStringList());
  5231. FileList->AddObject(FileName, RemoteFile);
  5232. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5233. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5234. RemoteDirView->SaveSelection();
  5235. RemoteDirView->SaveSelectedNames();
  5236. try
  5237. {
  5238. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5239. }
  5240. catch(...)
  5241. {
  5242. RemoteDirView->DiscardSavedSelection();
  5243. throw;
  5244. }
  5245. RemoteDirView->RestoreSelection();
  5246. }
  5247. else if (DebugAlwaysTrue(Side == osLocal))
  5248. {
  5249. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5250. {
  5251. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5252. }
  5253. UnicodeString Directory = ExtractFileDir(FileName);
  5254. ReloadLocalDirectory(Directory);
  5255. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5256. if (!SamePaths(Directory, NewDirectory))
  5257. {
  5258. ReloadLocalDirectory(NewDirectory);
  5259. }
  5260. }
  5261. }
  5262. //---------------------------------------------------------------------------
  5263. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5264. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5265. const TCopyParamType * CopyParams)
  5266. {
  5267. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5268. UnicodeString AdditionalParams =
  5269. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5270. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5271. LocalDirectory, RemoteDirectory, Mode, Params,
  5272. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5273. SerializeCopyParamForCommandLine(CopyParams)));
  5274. ExecuteNewInstance(SessionName, AdditionalParams);
  5275. }
  5276. //---------------------------------------------------------------------------
  5277. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5278. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5279. TSynchronizeMode & Mode, int Params, bool & SaveMode, bool UseDefaults)
  5280. {
  5281. int Result;
  5282. bool SaveSettings = false;
  5283. int Options =
  5284. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5285. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5286. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5287. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5288. bool Continue =
  5289. UseDefaults ||
  5290. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5291. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow);
  5292. if (Continue)
  5293. {
  5294. Configuration->Usage->Inc(L"Synchronizations");
  5295. UpdateCopyParamCounters(CopyParam);
  5296. TSynchronizeOptions SynchronizeOptions;
  5297. GetSynchronizeOptions(Params, SynchronizeOptions);
  5298. if (SaveSettings)
  5299. {
  5300. GUIConfiguration->SynchronizeParams = Params;
  5301. }
  5302. else
  5303. {
  5304. SaveMode = false;
  5305. }
  5306. TDateTime StartTime = Now();
  5307. TSynchronizeChecklist * Checklist = NULL;
  5308. try
  5309. {
  5310. DebugAssert(!FAutoOperation);
  5311. FAutoOperation = true;
  5312. try
  5313. {
  5314. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5315. FSynchronizeProgressForm->Start();
  5316. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5317. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5318. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5319. &SynchronizeOptions);
  5320. }
  5321. __finally
  5322. {
  5323. FAutoOperation = false;
  5324. SAFE_DESTROY(FSynchronizeProgressForm);
  5325. }
  5326. TSynchronizeParams SynchronizeParams;
  5327. SynchronizeParams.LocalDirectory = LocalDirectory;
  5328. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5329. SynchronizeParams.Mode = Mode;
  5330. SynchronizeParams.CopyParam = &CopyParam;
  5331. SynchronizeParams.Params = Params;
  5332. SynchronizeParams.Checklist = Checklist;
  5333. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5334. SynchronizeParams.StartTime = &StartTime;
  5335. SynchronizeParams.OnProcessedItem = NULL;
  5336. Result = Checklist->Count;
  5337. if (Checklist->Count > 0)
  5338. {
  5339. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5340. {
  5341. if (!DoSynchronizeChecklistDialog(
  5342. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5343. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5344. {
  5345. Result = -1;
  5346. }
  5347. }
  5348. else
  5349. {
  5350. FullSynchronize(SynchronizeParams, NULL, NULL);
  5351. }
  5352. }
  5353. else
  5354. {
  5355. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5356. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5357. }
  5358. }
  5359. __finally
  5360. {
  5361. delete Checklist;
  5362. }
  5363. OperationComplete(StartTime);
  5364. }
  5365. else
  5366. {
  5367. Result = -1;
  5368. }
  5369. return Result;
  5370. }
  5371. //---------------------------------------------------------------------------
  5372. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5373. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5374. bool & Continue, bool Collect)
  5375. {
  5376. if (Collect)
  5377. {
  5378. DebugAssert(FSynchronizeProgressForm != NULL);
  5379. if (!FSynchronizeProgressForm->Started)
  5380. {
  5381. FSynchronizeProgressForm->Start();
  5382. }
  5383. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5384. }
  5385. else
  5386. {
  5387. DebugAssert(FSynchronizeProgressForm == NULL);
  5388. DebugAssert(FProgressForm != NULL);
  5389. }
  5390. }
  5391. //---------------------------------------------------------------------------
  5392. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5393. {
  5394. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5395. TRemoteFile * File = NULL;
  5396. Terminal->ReadFile(FullFileName, File);
  5397. if (File != NULL)
  5398. {
  5399. std::unique_ptr<TRemoteFile> FileOwner(File);
  5400. TAutoFlag Flag(FStandaloneEditing);
  5401. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5402. Application->ShowMainForm = false;
  5403. Application->Run();
  5404. }
  5405. }
  5406. //---------------------------------------------------------------------------
  5407. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5408. {
  5409. DebugFail();
  5410. }
  5411. //---------------------------------------------------------------------------
  5412. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5413. {
  5414. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5415. SessionData->Assign(Terminal->SessionData);
  5416. UpdateSessionData(SessionData.get());
  5417. Terminal->UpdateSessionCredentials(SessionData.get());
  5418. if (!Terminal->SessionData->HasSessionName())
  5419. {
  5420. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5421. // once IsWorkspace is cleared
  5422. SessionData->Name = UnicodeString();
  5423. SessionData->IsWorkspace = false;
  5424. }
  5425. return SessionData.release();
  5426. }
  5427. //---------------------------------------------------------------------------
  5428. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5429. {
  5430. TSessionData * SessionData = CloneCurrentSessionData();
  5431. try
  5432. {
  5433. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5434. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5435. }
  5436. __finally
  5437. {
  5438. delete SessionData;
  5439. }
  5440. }
  5441. //---------------------------------------------------------------------------
  5442. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5443. {
  5444. TTerminalManager * Manager = TTerminalManager::Instance();
  5445. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5446. if (DebugAlwaysTrue(Terminal != NULL))
  5447. {
  5448. // Update (Managed)Terminal->StateData
  5449. UpdateTerminal(Terminal);
  5450. }
  5451. Manager->SaveWorkspace(DataList.get());
  5452. return DataList.release();
  5453. }
  5454. //---------------------------------------------------------------------------
  5455. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5456. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5457. {
  5458. WinConfiguration->LastWorkspace = Name;
  5459. if (!SavePasswords)
  5460. {
  5461. for (int Index = 0; Index < DataList->Count; Index++)
  5462. {
  5463. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5464. if (SessionData->Link.IsEmpty())
  5465. {
  5466. SessionData->ClearSessionPasswords();
  5467. }
  5468. }
  5469. }
  5470. StoredSessions->NewWorkspace(Name, DataList);
  5471. // modified only
  5472. StoredSessions->Save(false, Explicit);
  5473. }
  5474. //---------------------------------------------------------------------------
  5475. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5476. {
  5477. return
  5478. DefaultStr(WinConfiguration->LastWorkspace,
  5479. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5480. }
  5481. //---------------------------------------------------------------------------
  5482. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5483. {
  5484. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5485. bool AnyNonStoredSessionWithPassword = false;
  5486. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5487. bool AllNonStoredSessionsAnonymous = true;
  5488. TTerminalManager * Manager = TTerminalManager::Instance();
  5489. for (int Index = 0; Index < DataList->Count; Index++)
  5490. {
  5491. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5492. if (SessionData->Link.IsEmpty())
  5493. {
  5494. if (SessionData->HasAnySessionPassword())
  5495. {
  5496. AnyNonStoredSessionWithPassword = true;
  5497. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5498. {
  5499. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5500. }
  5501. }
  5502. if (!SameText(SessionData->UserName, AnonymousUserName))
  5503. {
  5504. AllNonStoredSessionsAnonymous = false;
  5505. }
  5506. }
  5507. }
  5508. bool SavePasswords;
  5509. bool * PSavePasswords;
  5510. bool NotRecommendedSavingPasswords =
  5511. !CustomWinConfiguration->UseMasterPassword &&
  5512. !AllNonStoredSessionsAnonymous;
  5513. if (Configuration->DisablePasswordStoring ||
  5514. !AnyNonStoredSessionWithPassword)
  5515. {
  5516. PSavePasswords = NULL;
  5517. SavePasswords = false;
  5518. }
  5519. else
  5520. {
  5521. PSavePasswords = &SavePasswords;
  5522. SavePasswords =
  5523. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5524. !NotRecommendedSavingPasswords;
  5525. }
  5526. UnicodeString Name = WorkspaceName();
  5527. bool CreateShortcut = false;
  5528. bool Result =
  5529. DoSaveWorkspaceDialog(
  5530. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5531. EnableAutoSave);
  5532. if (Result)
  5533. {
  5534. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5535. if (CreateShortcut)
  5536. {
  5537. TOperationVisualizer Visualizer;
  5538. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5539. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5540. }
  5541. if (EnableAutoSave)
  5542. {
  5543. WinConfiguration->AutoSaveWorkspace = true;
  5544. WinConfiguration->AutoWorkspace = Name;
  5545. if (PSavePasswords != NULL)
  5546. {
  5547. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5548. }
  5549. }
  5550. }
  5551. return Result;
  5552. }
  5553. //---------------------------------------------------------------------------
  5554. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5555. {
  5556. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5557. DebugAssert(ManagedTerminal != NULL);
  5558. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5559. if (WinConfiguration->PreservePanelState)
  5560. {
  5561. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5562. }
  5563. UpdateSessionData(ManagedTerminal->StateData);
  5564. }
  5565. //---------------------------------------------------------------------------
  5566. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5567. {
  5568. // Keep in sync with TSessionData::CopyStateData
  5569. DebugAssert(Data != NULL);
  5570. // This is inconsistent with how color (for example) is handled.
  5571. Data->Name = Terminal->SessionData->Name;
  5572. // cannot use RemoteDirView->Path, because it is empty if connection
  5573. // was already closed
  5574. // also only peek, we may not be connected at all atm
  5575. // so make sure we do not try retrieving current directory from the server
  5576. // (particularly with FTP)
  5577. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5578. if (!ACurrentDirectory.IsEmpty())
  5579. {
  5580. Data->RemoteDirectory = ACurrentDirectory;
  5581. }
  5582. Data->Color = SessionColor;
  5583. }
  5584. //---------------------------------------------------------------------------
  5585. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5586. {
  5587. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5588. DebugAssert(Toolbar != NULL);
  5589. for (int i = 0; i < Toolbar->Items->Count; i++)
  5590. {
  5591. TTBXCustomDropDownItem * DropDownItem;
  5592. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5593. if (DropDownItem != NULL)
  5594. {
  5595. ToolbarItemResize(DropDownItem,
  5596. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5597. Toolbar->NonClientWidth);
  5598. break;
  5599. }
  5600. }
  5601. }
  5602. //---------------------------------------------------------------------------
  5603. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5604. {
  5605. Item->EditWidth = Width;
  5606. }
  5607. //---------------------------------------------------------------------------
  5608. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5609. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5610. {
  5611. for (int i = 0; i < Toolbar->Items->Count; i++)
  5612. {
  5613. TTBXCustomDropDownItem * DropDownItem;
  5614. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5615. if (DropDownItem != NULL)
  5616. {
  5617. ASize.x -= DropDownItem->EditWidth;
  5618. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5619. }
  5620. }
  5621. }
  5622. //---------------------------------------------------------------------------
  5623. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5624. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5625. {
  5626. if (WinConfiguration->DDWarnLackOfTempSpace)
  5627. {
  5628. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5629. if (!ADrive.IsEmpty())
  5630. {
  5631. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5632. DebugAssert(RequiredSpace >= 0);
  5633. __int64 RequiredWithReserve;
  5634. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5635. if (FreeSpace < RequiredWithReserve)
  5636. {
  5637. unsigned int Result;
  5638. TMessageParams Params(mpNeverAskAgainCheck);
  5639. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5640. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5641. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5642. if (Result == qaNeverAskAgain)
  5643. {
  5644. WinConfiguration->DDWarnLackOfTempSpace = false;
  5645. }
  5646. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5647. }
  5648. }
  5649. }
  5650. }
  5651. //---------------------------------------------------------------------------
  5652. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5653. {
  5654. DoOpenDirectoryDialog(odAddBookmark, Side);
  5655. }
  5656. //---------------------------------------------------------------------------
  5657. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5658. {
  5659. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5660. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5661. try
  5662. {
  5663. TCustomDirView * DView = DirView(Side);
  5664. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5665. {
  5666. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5667. }
  5668. }
  5669. catch (...)
  5670. {
  5671. delete VisitedDirectories;
  5672. throw;
  5673. }
  5674. return VisitedDirectories;
  5675. }
  5676. //---------------------------------------------------------------------------
  5677. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5678. TOpenDirectoryMode Mode, TOperationSide Side)
  5679. {
  5680. bool Continue = true;
  5681. if (Mode == odAddBookmark)
  5682. {
  5683. TMessageParams Params(mpNeverAskAgainCheck);
  5684. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5685. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5686. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5687. unsigned int Answer =
  5688. MessageDialog(Message,
  5689. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5690. if (Answer == qaNeverAskAgain)
  5691. {
  5692. Continue = true;
  5693. WinConfiguration->UseSharedBookmarks = true;
  5694. }
  5695. else if (Answer == qaOK)
  5696. {
  5697. Continue = true;
  5698. WinConfiguration->UseSharedBookmarks = false;
  5699. }
  5700. else
  5701. {
  5702. Continue = false;
  5703. }
  5704. }
  5705. if (Continue)
  5706. {
  5707. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5708. try
  5709. {
  5710. UnicodeString Name = DirView(Side)->PathName;
  5711. bool Repeat;
  5712. do
  5713. {
  5714. Repeat = false;
  5715. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5716. // do not allow switching to location profiles,
  5717. // if we are not connected
  5718. HasDirView[osLocal] && (Terminal != NULL)))
  5719. {
  5720. TWindowLock Lock(this);
  5721. if (!TryOpenDirectory(Side, Name))
  5722. {
  5723. Repeat = true;
  5724. Mode = odBrowse;
  5725. }
  5726. }
  5727. }
  5728. while (Repeat);
  5729. }
  5730. __finally
  5731. {
  5732. delete VisitedDirectories;
  5733. }
  5734. }
  5735. }
  5736. //---------------------------------------------------------------------------
  5737. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5738. {
  5739. DebugAssert(!FTerminal->CommandSessionOpened);
  5740. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5741. }
  5742. //---------------------------------------------------------------------------
  5743. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5744. {
  5745. bool Result = FTerminal->IsCapable[Capability] ||
  5746. FTerminal->CommandSessionOpened;
  5747. if (!Result)
  5748. {
  5749. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5750. if (!GUIConfiguration->ConfirmCommandSession)
  5751. {
  5752. Result = true;
  5753. }
  5754. else
  5755. {
  5756. TMessageParams Params(mpNeverAskAgainCheck);
  5757. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5758. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5759. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5760. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5761. if (Answer == qaNeverAskAgain)
  5762. {
  5763. GUIConfiguration->ConfirmCommandSession = false;
  5764. Result = true;
  5765. }
  5766. else if (Answer == qaOK)
  5767. {
  5768. Result = true;
  5769. }
  5770. }
  5771. if (Result)
  5772. {
  5773. Result = CommandSessionFallback();
  5774. }
  5775. }
  5776. return Result;
  5777. }
  5778. //---------------------------------------------------------------------------
  5779. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5780. {
  5781. if (EnsureCommandSessionFallback(fcAnyCommand))
  5782. {
  5783. DoConsoleDialog(Terminal, Command);
  5784. }
  5785. }
  5786. //---------------------------------------------------------------------------
  5787. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5788. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5789. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5790. {
  5791. if (IsFileControl(DropSourceControl, osRemote) &&
  5792. (FDDExtMapFile != NULL))
  5793. {
  5794. Accept = true;
  5795. }
  5796. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5797. DebugAssert(FDDTargetControl != NULL);
  5798. }
  5799. //---------------------------------------------------------------------------
  5800. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5801. _di_IDataObject DataObj, int KeyState,
  5802. const TPoint & Point, int & Effect, bool & Accept)
  5803. {
  5804. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5805. }
  5806. //---------------------------------------------------------------------------
  5807. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5808. _di_IDataObject DataObj, int KeyState,
  5809. const TPoint & Point, int & Effect, bool & Accept)
  5810. {
  5811. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5812. }
  5813. //---------------------------------------------------------------------------
  5814. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5815. TObject * Sender)
  5816. {
  5817. DebugUsedParam(Sender);
  5818. DebugAssert(FDDTargetControl == Sender);
  5819. FDDTargetControl = NULL;
  5820. }
  5821. //---------------------------------------------------------------------------
  5822. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5823. {
  5824. FileControlDDDragLeave(SessionsPageControl);
  5825. }
  5826. //---------------------------------------------------------------------------
  5827. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5828. {
  5829. FileControlDDDragLeave(QueueView3);
  5830. }
  5831. //---------------------------------------------------------------------------
  5832. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5833. {
  5834. DebugAssert(GetSide(Side) == osRemote);
  5835. DebugUsedParam(Side);
  5836. bool Edit = false;
  5837. TRemoteFile * File = NULL;
  5838. UnicodeString FileName;
  5839. UnicodeString PointTo;
  5840. bool SymbolicLink = true;
  5841. if (RemoteDirView->ItemFocused)
  5842. {
  5843. DebugAssert(RemoteDirView->ItemFocused->Data);
  5844. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5845. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5846. if (Edit)
  5847. {
  5848. FileName = File->FileName;
  5849. PointTo = File->LinkTo;
  5850. }
  5851. else
  5852. {
  5853. PointTo = File->FileName;
  5854. }
  5855. }
  5856. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5857. Terminal->IsCapable[fcHardLink]))
  5858. {
  5859. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5860. if (Edit)
  5861. {
  5862. DebugAssert(File->FileName == FileName);
  5863. int Params = dfNoRecursive;
  5864. Terminal->ExceptionOnFail = true;
  5865. try
  5866. {
  5867. Terminal->DeleteFile(L"", File, &Params);
  5868. }
  5869. __finally
  5870. {
  5871. Terminal->ExceptionOnFail = false;
  5872. }
  5873. }
  5874. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5875. }
  5876. }
  5877. //---------------------------------------------------------------------------
  5878. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5879. {
  5880. return
  5881. (Terminal != NULL) &&
  5882. ((GetSide(Side) != osRemote) ||
  5883. (Terminal->ResolvingSymlinks &&
  5884. Terminal->IsCapable[fcSymbolicLink]));
  5885. }
  5886. //---------------------------------------------------------------------------
  5887. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5888. {
  5889. return
  5890. (FCurrentSide == osRemote) &&
  5891. (RemoteDirView->ItemFocused != NULL) &&
  5892. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5893. Terminal->SessionData->ResolveSymlinks;
  5894. }
  5895. //---------------------------------------------------------------------------
  5896. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5897. {
  5898. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5899. FForceExecution = true;
  5900. try
  5901. {
  5902. DirView(osCurrent)->ExecuteCurrentFile();
  5903. }
  5904. __finally
  5905. {
  5906. FForceExecution = false;
  5907. }
  5908. }
  5909. //---------------------------------------------------------------------------
  5910. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5911. {
  5912. TEditorData ExternalEditor;
  5913. ExternalEditor.Editor = edExternal;
  5914. bool Remember = false;
  5915. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5916. (GetSide(osCurrent) == osRemote)))
  5917. {
  5918. if (Remember)
  5919. {
  5920. TEditorList * EditorList = new TEditorList;
  5921. try
  5922. {
  5923. *EditorList = *WinConfiguration->EditorList;
  5924. bool Found = false;
  5925. int i = 0;
  5926. while (!Found && (i < EditorList->Count))
  5927. {
  5928. const TEditorPreferences * Editor = EditorList->Editors[i];
  5929. if ((Editor->Data->Editor == edExternal) &&
  5930. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5931. {
  5932. Found = true;
  5933. }
  5934. i++;
  5935. }
  5936. if (!Found)
  5937. {
  5938. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5939. WinConfiguration->EditorList = EditorList;
  5940. }
  5941. }
  5942. __finally
  5943. {
  5944. delete EditorList;
  5945. }
  5946. }
  5947. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5948. }
  5949. }
  5950. //---------------------------------------------------------------------------
  5951. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5952. {
  5953. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5954. if (FFileFindTerminal == Sender)
  5955. {
  5956. FFileFindTerminal = NULL;
  5957. HideFileFindDialog();
  5958. }
  5959. if (FClipboardTerminal == Sender)
  5960. {
  5961. ClipboardClear(); // implies ClipboardStop
  5962. }
  5963. }
  5964. //---------------------------------------------------------------------------
  5965. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5966. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5967. {
  5968. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5969. DebugAssert(Terminal != NULL);
  5970. if (Data->Terminal == Terminal)
  5971. {
  5972. Data->Terminal = NULL;
  5973. }
  5974. }
  5975. //---------------------------------------------------------------------------
  5976. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5977. {
  5978. UpdateControls();
  5979. SessionColor = TColor(0);
  5980. UpdateRemotePathComboBox(false);
  5981. NeedSession(true);
  5982. }
  5983. //---------------------------------------------------------------------------
  5984. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5985. {
  5986. try
  5987. {
  5988. try
  5989. {
  5990. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5991. }
  5992. __finally
  5993. {
  5994. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5995. (!Terminal || !Terminal->Active))
  5996. {
  5997. TerminateApplication();
  5998. }
  5999. }
  6000. }
  6001. catch (EAbort &)
  6002. {
  6003. // swallow
  6004. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6005. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6006. }
  6007. }
  6008. //---------------------------------------------------------------------------
  6009. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6010. {
  6011. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6012. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6013. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6014. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6015. try
  6016. {
  6017. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6018. // Clear the design time unthemed tab
  6019. ((SessionsPageControl->PageCount > 0) &&
  6020. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6021. {
  6022. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6023. }
  6024. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6025. {
  6026. TThemeTabSheet * TabSheet;
  6027. if (Index >= SessionsPageControl->PageCount)
  6028. {
  6029. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6030. TabSheet->PageControl = SessionsPageControl;
  6031. }
  6032. else
  6033. {
  6034. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6035. }
  6036. bool IsSessionTab = (Index < TerminalList->Count);
  6037. if (IsSessionTab)
  6038. {
  6039. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6040. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6041. UpdateSessionTab(TabSheet);
  6042. }
  6043. else
  6044. {
  6045. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6046. TabSheet->Tag = 0; // not really needed
  6047. // 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)
  6048. UpdateNewSessionTab();
  6049. }
  6050. TabSheet->ShowCloseButton = IsSessionTab;
  6051. }
  6052. }
  6053. __finally
  6054. {
  6055. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6056. }
  6057. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6058. }
  6059. //---------------------------------------------------------------------------
  6060. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6061. {
  6062. DoTerminalListChanged();
  6063. }
  6064. //---------------------------------------------------------------------------
  6065. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6066. {
  6067. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6068. DebugAssert(TabSheet->ImageIndex == 0);
  6069. TabSheet->Caption =
  6070. WinConfiguration->SelectiveToolbarText ?
  6071. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6072. UnicodeString();
  6073. }
  6074. //---------------------------------------------------------------------------
  6075. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6076. {
  6077. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  6078. }
  6079. //---------------------------------------------------------------------------
  6080. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6081. {
  6082. if (DebugAlwaysTrue(TabSheet != NULL))
  6083. {
  6084. TManagedTerminal * ManagedTerminal =
  6085. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6086. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6087. {
  6088. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6089. TabSheet->ImageIndex = AddSessionColor(Color);
  6090. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6091. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6092. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6093. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6094. {
  6095. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6096. }
  6097. }
  6098. }
  6099. }
  6100. //---------------------------------------------------------------------------
  6101. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6102. {
  6103. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6104. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6105. bool Result = (Terminal != NULL);
  6106. if (Result)
  6107. {
  6108. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6109. }
  6110. else
  6111. {
  6112. try
  6113. {
  6114. NewSession(false);
  6115. }
  6116. __finally
  6117. {
  6118. DoTerminalListChanged();
  6119. }
  6120. FSessionsPageControlNewSessionTime = Now();
  6121. }
  6122. return Result;
  6123. }
  6124. //---------------------------------------------------------------------------
  6125. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6126. {
  6127. SessionTabSwitched();
  6128. }
  6129. //---------------------------------------------------------------------------
  6130. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6131. {
  6132. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6133. DebugAssert(TransferList != NULL);
  6134. UnicodeString Name;
  6135. if (TransferList->ItemIndex <= 0)
  6136. {
  6137. Name = L"";
  6138. }
  6139. else
  6140. {
  6141. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6142. }
  6143. if (FCopyParamAutoSelected.IsEmpty())
  6144. {
  6145. // if previous preset was not autoselected, make new preset the "default"
  6146. FCopyParamDefault = Name;
  6147. }
  6148. GUIConfiguration->CopyParamCurrent = Name;
  6149. }
  6150. //---------------------------------------------------------------------------
  6151. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6152. {
  6153. // sanity check
  6154. bool ExistingPreset =
  6155. (FTransferListHoverIndex >= 0) &&
  6156. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6157. DebugAssert(ExistingPreset);
  6158. if (ExistingPreset)
  6159. {
  6160. HDC DC = GetDC(0);
  6161. TCanvas * Canvas = new TCanvas();
  6162. try
  6163. {
  6164. Canvas->Handle = DC;
  6165. Canvas->Font = GetToolbarFont(this);
  6166. UnicodeString Name;
  6167. if (FTransferListHoverIndex == 0)
  6168. {
  6169. Name = L"";
  6170. }
  6171. else
  6172. {
  6173. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6174. }
  6175. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6176. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6177. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6178. static_cast<TObject*>(GetComponent(fcTransferList)));
  6179. UnicodeString InfoStr =
  6180. GUIConfiguration->CopyParamPreset[Name].
  6181. GetInfoStr(L"; ",
  6182. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6183. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6184. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6185. {
  6186. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6187. {
  6188. InfoStr.SetLength(InfoStr.Length() - 1);
  6189. }
  6190. InfoStr += Ellipsis;
  6191. }
  6192. // UpdateCaption does not cause invalidation of whole submenu, while
  6193. // setting Caption property does.
  6194. // also it probably does not resize the label, even if necessary
  6195. // (we do not want that anyway)
  6196. TransferLabel->UpdateCaption(InfoStr);
  6197. }
  6198. __finally
  6199. {
  6200. Canvas->Handle = NULL;
  6201. ReleaseDC(0, DC);
  6202. delete Canvas;
  6203. }
  6204. }
  6205. }
  6206. //---------------------------------------------------------------------------
  6207. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6208. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6209. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6210. {
  6211. if (FTransferListHoverIndex != AHoverIndex)
  6212. {
  6213. FTransferListHoverIndex = AHoverIndex;
  6214. UpdateTransferLabel();
  6215. }
  6216. }
  6217. //---------------------------------------------------------------------------
  6218. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6219. {
  6220. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6221. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6222. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6223. {
  6224. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6225. {
  6226. OpenDirectory(GetSide(osCurrent));
  6227. Message.Result = 1;
  6228. }
  6229. else
  6230. {
  6231. TForm::Dispatch(&Message);
  6232. }
  6233. }
  6234. else
  6235. {
  6236. TForm::Dispatch(&Message);
  6237. }
  6238. }
  6239. //---------------------------------------------------------------------------
  6240. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6241. {
  6242. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6243. if ((FIgnoreNextDialogChar != 0) &&
  6244. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6245. {
  6246. Message.Result = 1;
  6247. }
  6248. else
  6249. {
  6250. TForm::Dispatch(&Message);
  6251. }
  6252. FIgnoreNextDialogChar = 0;
  6253. }
  6254. //---------------------------------------------------------------------------
  6255. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6256. {
  6257. // The four low-order bits are used internally by Windows
  6258. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6259. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6260. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6261. // 61730 - restore thru double click - undocumented
  6262. // 61490 - maximize thru double click - undocumented
  6263. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6264. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6265. {
  6266. SysResizing(Cmd);
  6267. }
  6268. TForm::Dispatch(&Message);
  6269. }
  6270. //---------------------------------------------------------------------------
  6271. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6272. {
  6273. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6274. // Also there no point returning TRUE as we are not able to
  6275. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6276. // Hence the process termination might be safer :)
  6277. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6278. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6279. {
  6280. Message.Result = FALSE;
  6281. }
  6282. else
  6283. {
  6284. Message.Result = TRUE;
  6285. }
  6286. // Do not call default handling as that triggers OnCloseQuery,
  6287. // where our implementation will popup confirmation dialogs, what we do not want,
  6288. // as per Vista guidelines:
  6289. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6290. }
  6291. //---------------------------------------------------------------------------
  6292. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6293. {
  6294. if (Message.EndSession && IsApplicationMinimized())
  6295. {
  6296. // WORKAROUND
  6297. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6298. // when it receives WM_ENDSESSION,
  6299. // but that sometimes (particularly when application is minimized) cause crashes.
  6300. // Crash popups message that blocks log off.
  6301. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6302. // so we call ExitProcess() immediately, not even trying to cleanup.
  6303. // It still causes beep, so there's likely some popup, but it does not block
  6304. // log off.
  6305. ExitProcess(0);
  6306. }
  6307. TForm::Dispatch(&Message);
  6308. }
  6309. #ifdef _DEBUG
  6310. //---------------------------------------------------------------------------
  6311. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6312. {
  6313. TForm::Dispatch(&Message);
  6314. }
  6315. #endif
  6316. //---------------------------------------------------------------------------
  6317. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6318. {
  6319. }
  6320. //---------------------------------------------------------------------------
  6321. void __fastcall TCustomScpExplorerForm::DoShow()
  6322. {
  6323. // only now are the controls resized finally, so the size constraints
  6324. // will not conflict with possibly very small window size
  6325. RestoreFormParams();
  6326. // This indicates that the form was rescaled after parameters were loaded and
  6327. // as most probably the parameters were actually saved with the current form scaling
  6328. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6329. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6330. // and no rounding problems will occur.
  6331. // See also RestoreForm.
  6332. // (we should be safe to call RestoreParams unconditionally)
  6333. if (PixelsPerInch != Screen->PixelsPerInch)
  6334. {
  6335. RestoreParams();
  6336. }
  6337. FixControlsPlacement();
  6338. if (Position == poDefaultPosOnly)
  6339. {
  6340. CutFormToDesktop(this);
  6341. }
  6342. TForm::DoShow();
  6343. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6344. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6345. if (Terminal == NULL)
  6346. {
  6347. StartingDisconnected();
  6348. }
  6349. UpdatePixelsPerInchMainWindowCounter();
  6350. FShowing = true;
  6351. }
  6352. //---------------------------------------------------------------------------
  6353. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6354. {
  6355. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6356. }
  6357. //---------------------------------------------------------------------------
  6358. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6359. {
  6360. DoTerminalListChanged();
  6361. }
  6362. //---------------------------------------------------------------------------
  6363. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6364. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6365. TNotifyEvent OnBalloonClick, TObject * UserData)
  6366. {
  6367. bool Do;
  6368. UnicodeString Message;
  6369. if (E == NULL)
  6370. {
  6371. Message = Str;
  6372. Do = true;
  6373. }
  6374. else
  6375. {
  6376. Do = ExceptionMessage(E, Message);
  6377. }
  6378. if (Do && WinConfiguration->BalloonNotifications)
  6379. {
  6380. UnicodeString Title;
  6381. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6382. {
  6383. Message = TrimLeft(Message);
  6384. }
  6385. else
  6386. {
  6387. Message = UnformatMessage(Message);
  6388. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6389. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6390. Title = LoadResourceString(Captions[Type]);
  6391. if (Terminal != NULL)
  6392. {
  6393. Title = FORMAT(L"%s - %s",
  6394. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6395. }
  6396. }
  6397. if (Seconds == 0)
  6398. {
  6399. Seconds = WinConfiguration->NotificationsTimeout;
  6400. }
  6401. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6402. }
  6403. }
  6404. //---------------------------------------------------------------------------
  6405. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6406. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6407. UnicodeString HelpKeyword, const TMessageParams * Params,
  6408. TTerminal * Terminal)
  6409. {
  6410. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6411. (FOnFeedSynchronizeError != NULL)) &&
  6412. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6413. {
  6414. if (FOnFeedSynchronizeError != NULL)
  6415. {
  6416. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6417. }
  6418. else
  6419. {
  6420. DebugAssert(FErrorList != NULL);
  6421. TStringList * MoreMessagesCopy = NULL;
  6422. if (MoreMessages)
  6423. {
  6424. MoreMessagesCopy = new TStringList();
  6425. MoreMessagesCopy->Assign(MoreMessages);
  6426. }
  6427. FErrorList->AddObject(Message, MoreMessagesCopy);
  6428. }
  6429. PopupTrayBalloon(Terminal, Message, Type);
  6430. return ContinueAnswer(Answers);
  6431. }
  6432. else
  6433. {
  6434. bool UseBalloon = FTrayIcon->Visible;
  6435. if (UseBalloon)
  6436. {
  6437. PopupTrayBalloon(Terminal, Message, Type);
  6438. }
  6439. unsigned int Result;
  6440. try
  6441. {
  6442. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6443. }
  6444. __finally
  6445. {
  6446. // cancel only balloon we popped up, otherwise we may cancel notification
  6447. // balloon that was there before the message dialog
  6448. // (such as "dd confirmation opt in balloon)
  6449. if (UseBalloon)
  6450. {
  6451. FTrayIcon->CancelBalloon();
  6452. }
  6453. }
  6454. return Result;
  6455. }
  6456. }
  6457. //---------------------------------------------------------------------------
  6458. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6459. TTerminal * Terminal, Exception * E)
  6460. {
  6461. if (FTrayIcon->Visible)
  6462. {
  6463. PopupTrayBalloon(Terminal, L"", qtError, E);
  6464. }
  6465. // particularly prevent opening new session from jump list,
  6466. // while exception is shown
  6467. NonVisualDataModule->StartBusy();
  6468. try
  6469. {
  6470. ShowExtendedExceptionEx(Terminal, E);
  6471. }
  6472. __finally
  6473. {
  6474. NonVisualDataModule->EndBusy();
  6475. FTrayIcon->CancelBalloon();
  6476. }
  6477. }
  6478. //---------------------------------------------------------------------------
  6479. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6480. {
  6481. // cannot rely on active page being page for active terminal,
  6482. // as it can happen that active page is the "new session" page
  6483. // (e.g. when reconnecting active terminal, while login dialog
  6484. // invoked from "new session" page is modal)
  6485. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6486. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6487. }
  6488. //---------------------------------------------------------------------------
  6489. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6490. TTerminal * Terminal, Exception * E)
  6491. {
  6492. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6493. if (!Terminal->Active)
  6494. {
  6495. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6496. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6497. {
  6498. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6499. }
  6500. }
  6501. }
  6502. //---------------------------------------------------------------------------
  6503. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6504. UnicodeString Message, TQueryType Type,
  6505. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6506. {
  6507. if ((E == NULL) ||
  6508. ExceptionMessage(E, Message))
  6509. {
  6510. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6511. if (Important)
  6512. {
  6513. Seconds *= 5;
  6514. }
  6515. UnicodeString NoteMessage(UnformatMessage(Message));
  6516. if (Terminal != NULL)
  6517. {
  6518. NoteMessage = FORMAT(L"%s: %s",
  6519. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6520. }
  6521. if (WinConfiguration->BalloonNotifications)
  6522. {
  6523. AddNote(NoteMessage);
  6524. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6525. }
  6526. else
  6527. {
  6528. FlashOnBackground();
  6529. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6530. }
  6531. }
  6532. }
  6533. //---------------------------------------------------------------------------
  6534. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6535. {
  6536. RestoreApp();
  6537. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6538. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6539. !NonVisualDataModule->Busy)
  6540. {
  6541. TTerminal * Terminal = TerminalNoteData->Terminal;
  6542. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6543. if (!ComponentVisible[fcQueueView])
  6544. {
  6545. ToggleQueueVisibility();
  6546. GoToQueue();
  6547. }
  6548. }
  6549. }
  6550. //---------------------------------------------------------------------------
  6551. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6552. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6553. {
  6554. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6555. UnicodeString Message;
  6556. TNotifyEvent OnClick = NULL;
  6557. TObject * UserData = NULL;
  6558. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6559. switch (Event)
  6560. {
  6561. case qeEmptyButMonitored:
  6562. if ((ATerminal != Terminal) || QueueInvisible)
  6563. {
  6564. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6565. OnClick = QueueEmptyNoteClicked;
  6566. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6567. TerminalNoteData->Terminal = ATerminal;
  6568. UserData = TerminalNoteData;
  6569. }
  6570. break;
  6571. case qeEmpty:
  6572. OperationComplete(ManagedTerminal->QueueOperationStart);
  6573. break;
  6574. case qePendingUserAction:
  6575. if ((ATerminal != Terminal) ||
  6576. (QueueInvisible && !IsQueueAutoPopup()))
  6577. {
  6578. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6579. }
  6580. break;
  6581. default:
  6582. DebugFail();
  6583. }
  6584. if (!Message.IsEmpty())
  6585. {
  6586. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6587. }
  6588. }
  6589. //---------------------------------------------------------------------------
  6590. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6591. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6592. {
  6593. if (FDDExtMapFile != NULL)
  6594. {
  6595. CloseHandle(FDDExtMapFile);
  6596. FDDExtMapFile = NULL;
  6597. }
  6598. if (WinConfiguration->DDFakeFile)
  6599. {
  6600. DDFakeFileInitDrag(FileList, Created);
  6601. }
  6602. }
  6603. //---------------------------------------------------------------------------
  6604. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6605. {
  6606. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6607. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6608. {
  6609. FFakeFileDropTarget = FileName;
  6610. }
  6611. }
  6612. //---------------------------------------------------------------------------
  6613. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6614. {
  6615. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6616. if (!ForceDirectories(ApiPath(Result)))
  6617. {
  6618. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6619. }
  6620. FileSetAttr(ApiPath(Result), faHidden);
  6621. return Result;
  6622. }
  6623. //---------------------------------------------------------------------------
  6624. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6625. bool & Created)
  6626. {
  6627. FFakeFileDropTarget = UnicodeString();
  6628. FDragFakeDirectory = CreateFakeTransferDirectory();
  6629. FileList->AddItem(NULL, FDragFakeDirectory);
  6630. Created = true;
  6631. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6632. {
  6633. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6634. }
  6635. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6636. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6637. {
  6638. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6639. TDragExtCommStruct* CommStruct;
  6640. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6641. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6642. DebugAssert(CommStruct != NULL);
  6643. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6644. CommStruct->Dragging = true;
  6645. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6646. LENOF(CommStruct->DropDest));
  6647. NULL_TERMINATE(CommStruct->DropDest);
  6648. UnmapViewOfFile(CommStruct);
  6649. }
  6650. }
  6651. //---------------------------------------------------------------------------
  6652. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6653. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6654. {
  6655. bool Result;
  6656. if (Sender == RemoteDirView)
  6657. {
  6658. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6659. }
  6660. else
  6661. {
  6662. DebugAssert(Sender == RemoteDriveView);
  6663. TStrings * FileList = RemoteDriveView->DragFileList();
  6664. try
  6665. {
  6666. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6667. }
  6668. __finally
  6669. {
  6670. delete FileList;
  6671. }
  6672. }
  6673. if (FDDTargetControl == RemoteDriveView)
  6674. {
  6675. RemoteDriveView->UpdateDropTarget();
  6676. }
  6677. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6678. // or from tree view to dir view.
  6679. // foMove happens when file/dir is dragged from remote tree view outside of
  6680. // application via dragex
  6681. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6682. (DropSourceControl == RemoteDriveView))
  6683. {
  6684. RemoteDriveView->UpdateDropSource();
  6685. }
  6686. return Result;
  6687. }
  6688. //---------------------------------------------------------------------------
  6689. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6690. {
  6691. // This also handles drops of remote files to queue.
  6692. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6693. SAFE_DESTROY(FDDFileList);
  6694. if (!FFakeFileDropTarget.IsEmpty())
  6695. {
  6696. RemoveDir(ApiPath(FFakeFileDropTarget));
  6697. }
  6698. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6699. {
  6700. try
  6701. {
  6702. TDragResult DDResult = (Sender == RemoteDirView) ?
  6703. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6704. // Focus is moved to the target application,
  6705. // but as we are going to present the UI, we need to steal the focus back.
  6706. // This most likely won't work though (windows does not allow application
  6707. // to steal focus most of the time)
  6708. Application->BringToFront();
  6709. // On older version of Windows we never got drMove here, see also comment below.
  6710. // On Windows 10, we get the "move".
  6711. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6712. {
  6713. UnicodeString TargetDirectory;
  6714. TFileOperation Operation;
  6715. // drInvalid may mean drMove, see comment below
  6716. switch (DDResult)
  6717. {
  6718. case drCopy:
  6719. Operation = foCopy;
  6720. break;
  6721. case drMove:
  6722. Operation = foMove;
  6723. break;
  6724. default:
  6725. DebugFail();
  6726. case drInvalid:
  6727. // prefer "copy" for safety
  6728. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6729. break;
  6730. }
  6731. TTransferOperationParam Param;
  6732. UnicodeString CounterName;
  6733. bool ForceQueue;
  6734. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6735. {
  6736. // we get drInvalid both if d&d was intercepted by ddext,
  6737. // and when users drops on no-drop location.
  6738. // we tell the difference by existence of response from ddext,
  6739. // so we ignore absence of response in this case
  6740. if (DDResult != drInvalid)
  6741. {
  6742. // here we know that the extension is installed,
  6743. // as it is checked as soon as drag&drop starts from
  6744. // RemoteFileControlDDCreateDragFileList
  6745. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6746. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6747. {
  6748. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6749. }
  6750. else
  6751. {
  6752. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6753. }
  6754. }
  6755. }
  6756. else
  6757. {
  6758. // download using fake file
  6759. Param.Temp = false;
  6760. Param.DragDrop = true;
  6761. if (ForceQueue)
  6762. {
  6763. Param.Queue = asOn;
  6764. }
  6765. if (Sender == RemoteDirView)
  6766. {
  6767. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6768. }
  6769. if (RemoteFileControlFileOperation(Sender, Operation,
  6770. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6771. {
  6772. Configuration->Usage->Inc(CounterName);
  6773. }
  6774. }
  6775. }
  6776. }
  6777. __finally
  6778. {
  6779. CloseHandle(FDDExtMapFile);
  6780. FDDExtMapFile = NULL;
  6781. delete FDragFakeMonitors;
  6782. FDragFakeMonitors = NULL;
  6783. RemoveDir(ApiPath(FDragFakeDirectory));
  6784. FDragFakeDirectory = L"";
  6785. FFakeFileDropTarget = UnicodeString();
  6786. }
  6787. }
  6788. if (FDragDropSshTerminate.get() != NULL)
  6789. {
  6790. FDragDropSshTerminate->Rethrow();
  6791. }
  6792. }
  6793. //---------------------------------------------------------------------------
  6794. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6795. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6796. {
  6797. // Remember drop effect so we know (when user drops files), if we copy or move
  6798. FLastDropEffect = dwEffect;
  6799. }
  6800. //---------------------------------------------------------------------------
  6801. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6802. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6803. {
  6804. bool Result;
  6805. if (FDDTargetControl == QueueView3)
  6806. {
  6807. Directory = DefaultDownloadTargetDirectory();
  6808. Result = true;
  6809. CounterName = L"DownloadsDragDropQueue";
  6810. ForceQueue = true;
  6811. }
  6812. else if (!FFakeFileDropTarget.IsEmpty())
  6813. {
  6814. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6815. Result = true;
  6816. ForceQueue = false;
  6817. CounterName = L"DownloadsDragDropFakeFile";
  6818. }
  6819. else
  6820. {
  6821. ForceQueue = false;
  6822. Enabled = false;
  6823. try
  6824. {
  6825. int Timer = 0;
  6826. Result = false;
  6827. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6828. {
  6829. {
  6830. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6831. TDragExtCommStruct* CommStruct;
  6832. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6833. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6834. DebugAssert(CommStruct != NULL);
  6835. Result = !CommStruct->Dragging;
  6836. if (Result)
  6837. {
  6838. Directory = ExtractFilePath(CommStruct->DropDest);
  6839. CounterName = L"DownloadsDragDropExternalExt";
  6840. }
  6841. UnmapViewOfFile(CommStruct);
  6842. }
  6843. if (!Result)
  6844. {
  6845. Sleep(50);
  6846. Timer += 50;
  6847. Application->ProcessMessages();
  6848. }
  6849. }
  6850. }
  6851. __finally
  6852. {
  6853. Enabled = true;
  6854. }
  6855. }
  6856. return Result;
  6857. }
  6858. //---------------------------------------------------------------------------
  6859. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6860. const UnicodeString TempDir, int SecDelay)
  6861. {
  6862. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6863. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6864. if (FDelayedDeletionTimer == NULL)
  6865. {
  6866. DebugAssert(HandleAllocated());
  6867. FDelayedDeletionTimer = new TTimer(this);
  6868. FDelayedDeletionTimer->Interval = 10000;
  6869. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6870. }
  6871. else
  6872. {
  6873. FDelayedDeletionTimer->Enabled = true;
  6874. }
  6875. }
  6876. //---------------------------------------------------------------------------
  6877. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6878. {
  6879. DebugAssert(FDelayedDeletionList != NULL);
  6880. TDateTime N = Now();
  6881. TDateTime Alert;
  6882. UnicodeString Directory;
  6883. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6884. {
  6885. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6886. if ((N >= Alert) || (Sender == NULL))
  6887. {
  6888. Directory = FDelayedDeletionList->Strings[Index];
  6889. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6890. {
  6891. FDelayedDeletionList->Delete(Index);
  6892. }
  6893. }
  6894. }
  6895. if (FDelayedDeletionList->Count == 0)
  6896. {
  6897. FDelayedDeletionTimer->Enabled = false;
  6898. }
  6899. }
  6900. //---------------------------------------------------------------------------
  6901. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6902. {
  6903. if (IsFileControl(FDDTargetControl, osRemote) ||
  6904. (FDDTargetControl == SessionsPageControl))
  6905. {
  6906. TTerminal * TargetTerminal = NULL;
  6907. TFileOperation Operation = foNone;
  6908. if (FDDTargetControl == SessionsPageControl)
  6909. {
  6910. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6911. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6912. // do not allow dropping on the "+" tab
  6913. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6914. if (TargetTerminal != NULL)
  6915. {
  6916. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6917. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6918. {
  6919. Operation = foRemoteMove;
  6920. }
  6921. else
  6922. {
  6923. Operation = foRemoteCopy;
  6924. }
  6925. }
  6926. }
  6927. else
  6928. {
  6929. if (FLastDropEffect == DROPEFFECT_MOVE)
  6930. {
  6931. Operation = foRemoteMove;
  6932. }
  6933. else if (FLastDropEffect == DROPEFFECT_COPY)
  6934. {
  6935. Operation = foRemoteCopy;
  6936. }
  6937. }
  6938. if (Operation != foNone)
  6939. {
  6940. RemoteFileControlFileOperation(DropSourceControl,
  6941. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6942. }
  6943. // abort drag&drop
  6944. Abort();
  6945. }
  6946. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6947. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6948. (FDDTargetControl != QueueView3))
  6949. {
  6950. DebugAssert(!FDragTempDir.IsEmpty());
  6951. TTransferType Type;
  6952. UnicodeString TempDir = FDragTempDir;
  6953. // We clear FDragTempDir before calling
  6954. // just in case it fail (raises exception)
  6955. FDragTempDir = L"";
  6956. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6957. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6958. // empty directory parameter means temp directory -> don't display it!
  6959. UnicodeString TargetDir = L"";
  6960. int Options = 0;
  6961. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6962. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6963. {
  6964. Abort();
  6965. }
  6966. // TargetDir is set when dropped on local file control
  6967. // (this was workaround for legacy dirview event handling, now it should be
  6968. // made prettier)
  6969. if (TargetDir.IsEmpty())
  6970. {
  6971. TargetDir = TempDir;
  6972. if (ForceDirectories(ApiPath(TargetDir)))
  6973. {
  6974. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6975. FPendingTempSpaceWarn = true;
  6976. try
  6977. {
  6978. FDragDropOperation = true;
  6979. int Params = cpTemporary |
  6980. (Type == ttMove ? cpDelete : 0);
  6981. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6982. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6983. }
  6984. __finally
  6985. {
  6986. FDragDropOperation = false;
  6987. FPendingTempSpaceWarn = false;
  6988. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6989. }
  6990. }
  6991. else
  6992. {
  6993. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6994. }
  6995. }
  6996. }
  6997. }
  6998. //---------------------------------------------------------------------------
  6999. void __fastcall TCustomScpExplorerForm::DDDownload(
  7000. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7001. {
  7002. TAutoBatch AutoBatch(this);
  7003. UpdateCopyParamCounters(*CopyParam);
  7004. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7005. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7006. {
  7007. RemoteDriveView->UpdateDropSource();
  7008. }
  7009. }
  7010. //---------------------------------------------------------------------------
  7011. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7012. {
  7013. public:
  7014. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7015. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7016. {
  7017. }
  7018. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7019. {
  7020. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7021. TDataObjectFilesEx::AllowData(FormatEtc);
  7022. }
  7023. };
  7024. //---------------------------------------------------------------------------
  7025. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7026. TObject * Sender, TDataObject *& DataObject)
  7027. {
  7028. if (FDDExtMapFile != NULL)
  7029. {
  7030. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7031. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7032. {
  7033. Abort();
  7034. }
  7035. if (FileList->Count > 0)
  7036. {
  7037. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7038. if (!FilesObject->IsValid(true, true))
  7039. {
  7040. FilesObject->_Release();
  7041. }
  7042. else
  7043. {
  7044. DataObject = FilesObject;
  7045. }
  7046. }
  7047. }
  7048. }
  7049. //---------------------------------------------------------------------------
  7050. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7051. {
  7052. DebugFail();
  7053. }
  7054. //---------------------------------------------------------------------------
  7055. void __fastcall TCustomScpExplorerForm::GoToTree()
  7056. {
  7057. ComponentVisible[fcRemoteTree] = true;
  7058. RemoteDriveView->SetFocus();
  7059. }
  7060. //---------------------------------------------------------------------------
  7061. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7062. TPanelExport Export)
  7063. {
  7064. TCustomDirView * DirView = this->DirView(Side);
  7065. std::unique_ptr<TStrings> ExportData(new TStringList());
  7066. switch (Export)
  7067. {
  7068. case pePath:
  7069. ExportData->Add(DirView->PathName);
  7070. break;
  7071. case peFileList:
  7072. case peFullFileList:
  7073. {
  7074. bool FullPath = (Export == peFullFileList);
  7075. DirView->CreateFileList(false, FullPath, ExportData.get());
  7076. for (int Index = 0; Index < ExportData->Count; Index++)
  7077. {
  7078. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7079. {
  7080. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7081. }
  7082. }
  7083. }
  7084. break;
  7085. default:
  7086. DebugFail();
  7087. }
  7088. return ExportData.release();
  7089. }
  7090. //---------------------------------------------------------------------------
  7091. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7092. TPanelExport Export, TPanelExportDestination Destination)
  7093. {
  7094. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7095. PanelExportStore(Side, Export, Destination, ExportData.get());
  7096. }
  7097. //---------------------------------------------------------------------------
  7098. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7099. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7100. TStrings * ExportData)
  7101. {
  7102. if (Destination == pedClipboard)
  7103. {
  7104. TInstantOperationVisualizer Visualizer;
  7105. CopyToClipboard(ExportData);
  7106. }
  7107. else
  7108. {
  7109. DebugFail();
  7110. }
  7111. }
  7112. //---------------------------------------------------------------------------
  7113. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7114. {
  7115. TCustomDirView * DirView = this->DirView(Side);
  7116. UnicodeString Mask = DirView->Mask;
  7117. if (DoFilterMaskDialog(DirView, Mask))
  7118. {
  7119. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7120. Configuration->Usage->Inc(L"Filters");
  7121. }
  7122. }
  7123. //---------------------------------------------------------------------------
  7124. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7125. {
  7126. return FQueueController->DefaultOperation();
  7127. }
  7128. //---------------------------------------------------------------------------
  7129. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7130. TQueueOperation Operation, void ** Param)
  7131. {
  7132. switch (Operation)
  7133. {
  7134. case qoPreferences:
  7135. return true;
  7136. case qoGoTo:
  7137. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7138. case qoOnceEmpty:
  7139. return !FQueueController->Empty;
  7140. default:
  7141. return FQueueController->AllowOperation(Operation, Param);
  7142. }
  7143. }
  7144. //---------------------------------------------------------------------------
  7145. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7146. {
  7147. if (DebugAlwaysTrue(QueueView3->Visible))
  7148. {
  7149. QueueView3->SetFocus();
  7150. }
  7151. }
  7152. //---------------------------------------------------------------------------
  7153. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7154. TQueueOperation Operation, void * Param)
  7155. {
  7156. if (Operation == qoGoTo)
  7157. {
  7158. GoToQueue();
  7159. }
  7160. else if (Operation == qoPreferences)
  7161. {
  7162. PreferencesDialog(pmQueue);
  7163. }
  7164. else
  7165. {
  7166. FQueueController->ExecuteOperation(Operation, Param);
  7167. }
  7168. }
  7169. //---------------------------------------------------------------------------
  7170. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7171. {
  7172. return (Queue != NULL) && Queue->Enabled;
  7173. }
  7174. //---------------------------------------------------------------------------
  7175. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7176. {
  7177. DebugAssert(Queue != NULL);
  7178. if (Queue != NULL)
  7179. {
  7180. Queue->Enabled = !Queue->Enabled;
  7181. }
  7182. }
  7183. //---------------------------------------------------------------------------
  7184. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7185. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7186. {
  7187. FQueueActedItem = QueueView3->ItemFocused;
  7188. }
  7189. //---------------------------------------------------------------------------
  7190. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7191. {
  7192. return TopDock->Height + QueueSplitter->Height;
  7193. }
  7194. //---------------------------------------------------------------------------
  7195. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7196. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7197. {
  7198. // when queue is hidden by double-clicking splitter, stray attempt to
  7199. // resize the panel with strange value arrives, make sure it is ignored
  7200. if (ComponentVisible[fcQueueView])
  7201. {
  7202. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7203. RemotePanel->Constraints->MinHeight;
  7204. if (NewSize > HeightLimit)
  7205. {
  7206. NewSize = HeightLimit;
  7207. }
  7208. }
  7209. else
  7210. {
  7211. Accept = false;
  7212. }
  7213. }
  7214. //---------------------------------------------------------------------------
  7215. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7216. TDragObject *& /*DragObject*/)
  7217. {
  7218. FQueueActedItem = QueueView3->ItemFocused;
  7219. }
  7220. //---------------------------------------------------------------------------
  7221. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7222. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7223. {
  7224. Accept = true;
  7225. if (Source == QueueView3)
  7226. {
  7227. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7228. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7229. if (Accept)
  7230. {
  7231. TQueueItemProxy * QueueItem;
  7232. TQueueItemProxy * DestQueueItem;
  7233. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7234. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7235. Accept = (QueueItem != DestQueueItem) &&
  7236. (QueueItem->Status == TQueueItem::qsPending) &&
  7237. (DestQueueItem->Status == TQueueItem::qsPending);
  7238. }
  7239. }
  7240. }
  7241. //---------------------------------------------------------------------------
  7242. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7243. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7244. {
  7245. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7246. {
  7247. TQueueItemProxy * QueueItem;
  7248. TQueueItemProxy * DestQueueItem;
  7249. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7250. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7251. QueueItem->Move(DestQueueItem);
  7252. }
  7253. }
  7254. //---------------------------------------------------------------------------
  7255. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7256. {
  7257. if ((QueueView3->ItemFocused == NULL) &&
  7258. (QueueView3->Items->Count > 0))
  7259. {
  7260. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7261. }
  7262. QueueLabelUpdateStatus();
  7263. }
  7264. //---------------------------------------------------------------------------
  7265. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7266. {
  7267. QueueLabelUpdateStatus();
  7268. }
  7269. //---------------------------------------------------------------------------
  7270. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7271. {
  7272. QueueLabel->UpdateStatus();
  7273. }
  7274. //---------------------------------------------------------------------------
  7275. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7276. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7277. {
  7278. if (Selected)
  7279. {
  7280. NonVisualDataModule->UpdateNonVisibleActions();
  7281. }
  7282. }
  7283. //---------------------------------------------------------------------------
  7284. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7285. {
  7286. TDragDropFilesEx * Result = NULL;
  7287. if (Sender == SessionsPageControl)
  7288. {
  7289. Result = FSessionsDragDropFilesEx;
  7290. }
  7291. else if (Sender == QueueView3)
  7292. {
  7293. Result = FQueueDragDropFilesEx;
  7294. }
  7295. else
  7296. {
  7297. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7298. if (DirView != NULL)
  7299. {
  7300. Result = DirView->DragDropFilesEx;
  7301. }
  7302. else
  7303. {
  7304. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7305. if (DriveView != NULL)
  7306. {
  7307. Result = DriveView->DragDropFilesEx;
  7308. }
  7309. }
  7310. }
  7311. DebugAssert(Result != NULL);
  7312. return Result;
  7313. }
  7314. //---------------------------------------------------------------------------
  7315. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7316. {
  7317. return (DView->SelCount == DView->FilesCount);
  7318. }
  7319. //---------------------------------------------------------------------------
  7320. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7321. {
  7322. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7323. }
  7324. //---------------------------------------------------------------------------
  7325. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7326. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7327. {
  7328. TFileOperation Operation;
  7329. switch (Effect)
  7330. {
  7331. case DROPEFFECT_MOVE:
  7332. Operation = foMove;
  7333. break;
  7334. case DROPEFFECT_COPY:
  7335. // occures on WinXP (reported by user)
  7336. default:
  7337. Operation = foCopy;
  7338. break;
  7339. };
  7340. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7341. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7342. if (DragDropFilesEx->FileList->Count > 0)
  7343. {
  7344. TStrings * FileList = new TStringList();
  7345. try
  7346. {
  7347. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7348. {
  7349. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7350. }
  7351. FDragDropOperation = true;
  7352. TTransferOperationParam Param;
  7353. Param.TargetDirectory = TargetPath;
  7354. // upload, no temp dirs
  7355. Param.Temp = false;
  7356. Param.DragDrop = DragDrop;
  7357. Param.Options =
  7358. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7359. if (ForceQueue)
  7360. {
  7361. Param.Queue = asOn;
  7362. }
  7363. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7364. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7365. {
  7366. if (IsFileControl(DropSourceControl, osLocal))
  7367. {
  7368. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7369. }
  7370. else
  7371. {
  7372. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7373. }
  7374. }
  7375. }
  7376. __finally
  7377. {
  7378. FDragDropOperation = false;
  7379. delete FileList;
  7380. }
  7381. }
  7382. }
  7383. //---------------------------------------------------------------------------
  7384. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7385. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7386. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7387. {
  7388. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7389. }
  7390. //---------------------------------------------------------------------------
  7391. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7392. TObject * Sender, int grfKeyState, int & dwEffect)
  7393. {
  7394. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7395. if ((dwEffect != DROPEFFECT_NONE) &&
  7396. IsFileControl(DropSourceControl, osRemote))
  7397. {
  7398. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7399. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7400. (RemoteDirView->DropTarget == NULL))
  7401. {
  7402. dwEffect = DROPEFFECT_NONE;
  7403. }
  7404. else
  7405. {
  7406. if (dwEffect == DROPEFFECT_COPY)
  7407. {
  7408. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7409. // currently we support copying always (at least via temporary directory);
  7410. // remove associated checks once this all proves stable and working
  7411. bool CopyCapable = true;
  7412. // if we do not support neither of operations, there's no discussion
  7413. if (!MoveCapable && !CopyCapable)
  7414. {
  7415. dwEffect = DROPEFFECT_NONE;
  7416. }
  7417. else
  7418. {
  7419. // The default effect inside remote panel is move,
  7420. // unless we do not support it, but support copy
  7421. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7422. {
  7423. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7424. }
  7425. else
  7426. {
  7427. // with ctrl-down, we want copy unless it is not supported
  7428. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7429. }
  7430. }
  7431. }
  7432. }
  7433. }
  7434. }
  7435. //---------------------------------------------------------------------------
  7436. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7437. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7438. {
  7439. if (FDDTotalSize >= 0)
  7440. {
  7441. if (File->IsDirectory)
  7442. {
  7443. FDDTotalSize = -1;
  7444. }
  7445. else
  7446. {
  7447. FDDTotalSize += File->Size;
  7448. }
  7449. }
  7450. DebugAssert(!FDragTempDir.IsEmpty());
  7451. // TODO: this is quite ineffective
  7452. // TODO: what if invalid character replacement is disabled?
  7453. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7454. UnicodeString TransferFileName;
  7455. if (Sender == RemoteDriveView)
  7456. {
  7457. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7458. }
  7459. else
  7460. {
  7461. TransferFileName = File->FileName;
  7462. }
  7463. FDDFileList->AddObject(TransferFileName, File);
  7464. }
  7465. //---------------------------------------------------------------------------
  7466. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7467. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7468. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7469. {
  7470. // sometimes we do not get DDEnd so the list is not released
  7471. SAFE_DESTROY(FDDFileList);
  7472. FDDFileList = new TStringList();
  7473. FDragTempDir = WinConfiguration->TemporaryDir();
  7474. FDDTotalSize = 0;
  7475. FDragDropSshTerminate.reset(NULL);
  7476. }
  7477. //---------------------------------------------------------------------------
  7478. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7479. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7480. HRESULT & Result)
  7481. {
  7482. if (Result == DRAGDROP_S_DROP)
  7483. {
  7484. try
  7485. {
  7486. GlobalDragImageList->HideDragImage();
  7487. try
  7488. {
  7489. RemoteFileControlDDTargetDrop();
  7490. }
  7491. catch(ESshTerminate & E)
  7492. {
  7493. FDragDropSshTerminate.reset(E.Clone());
  7494. }
  7495. }
  7496. catch (Exception &E)
  7497. {
  7498. // If downloading fails we need to cancel drag&drop, otherwise
  7499. // Explorer shows error
  7500. // But by the way exception probably never reach this point as
  7501. // it's catched on way.
  7502. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7503. Result = DRAGDROP_S_CANCEL;
  7504. ShowExtendedException(Terminal, &E);
  7505. }
  7506. }
  7507. }
  7508. //---------------------------------------------------------------------------
  7509. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7510. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7511. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7512. {
  7513. TFileMasks::TParams MaskParams;
  7514. MaskParams.Size = Size;
  7515. MaskParams.Modification = Modification;
  7516. // this does not re-parse the mask if it is the same as the last time
  7517. FDirViewMatchMask = Masks;
  7518. bool ImplicitMatch;
  7519. Matches =
  7520. // RecurseInclude parameter has no effect as the Path is empty
  7521. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7522. (AllowImplicitMatches || !ImplicitMatch);
  7523. }
  7524. //---------------------------------------------------------------------------
  7525. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7526. TObject * Sender, TListItem * Item, WORD & Indexes)
  7527. {
  7528. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7529. UnicodeString Ext;
  7530. if (DirView == RemoteDirView)
  7531. {
  7532. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7533. }
  7534. else
  7535. {
  7536. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7537. }
  7538. if (SameText(Ext, Configuration->PartialExt))
  7539. {
  7540. Indexes |= oiPartial;
  7541. }
  7542. }
  7543. //---------------------------------------------------------------------------
  7544. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7545. {
  7546. return
  7547. DirViewEnabled(osCurrent) &&
  7548. DirView(osCurrent)->CanPasteFromClipBoard();
  7549. }
  7550. //---------------------------------------------------------------------------
  7551. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7552. {
  7553. bool Result = false;
  7554. if (CanPasteToDirViewFromClipBoard())
  7555. {
  7556. Result = true;
  7557. }
  7558. else
  7559. {
  7560. UnicodeString ClipboardText;
  7561. if (NonEmptyTextFromClipboard(ClipboardText))
  7562. {
  7563. if (StoredSessions->IsUrl(ClipboardText))
  7564. {
  7565. Result = true;
  7566. }
  7567. else
  7568. {
  7569. Result = DirViewEnabled(osCurrent);
  7570. }
  7571. }
  7572. }
  7573. return Result;
  7574. }
  7575. //---------------------------------------------------------------------------
  7576. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7577. {
  7578. if (DoesClipboardContainOurFiles())
  7579. {
  7580. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7581. {
  7582. TTerminalManager * Manager = TTerminalManager::Instance();
  7583. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7584. Manager->ActiveTerminal = FClipboardTerminal;
  7585. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7586. }
  7587. }
  7588. else if (CanPasteToDirViewFromClipBoard())
  7589. {
  7590. DirView(osCurrent)->PasteFromClipBoard();
  7591. }
  7592. else
  7593. {
  7594. UnicodeString ClipboardText;
  7595. if (NonEmptyTextFromClipboard(ClipboardText))
  7596. {
  7597. if (StoredSessions->IsUrl(ClipboardText))
  7598. {
  7599. NewSession(false, ClipboardText);
  7600. }
  7601. else
  7602. {
  7603. DirView(osCurrent)->Path = ClipboardText;
  7604. }
  7605. }
  7606. }
  7607. }
  7608. //---------------------------------------------------------------------------
  7609. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7610. {
  7611. // TBD
  7612. }
  7613. //---------------------------------------------------------------------------
  7614. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7615. {
  7616. TCustomDirView * ADirView = DirView(Side);
  7617. ADirView->SelectAll(Mode);
  7618. ADirView->SetFocus();
  7619. }
  7620. //---------------------------------------------------------------------------
  7621. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7622. {
  7623. TCustomDirView * ADirView = DirView(Side);
  7624. TFileFilter Filter;
  7625. DefaultFileFilter(Filter);
  7626. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7627. {
  7628. Configuration->Usage->Inc(L"MaskSelections");
  7629. ADirView->SelectFiles(Filter, Select);
  7630. ADirView->SetFocus();
  7631. }
  7632. }
  7633. //---------------------------------------------------------------------------
  7634. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7635. {
  7636. TCustomDirView * ADirView = DirView(Side);
  7637. ADirView->RestoreSelectedNames();
  7638. ADirView->SetFocus();
  7639. }
  7640. //---------------------------------------------------------------------------
  7641. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7642. {
  7643. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7644. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7645. {
  7646. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7647. UnicodeString Ext;
  7648. if (GetSide(osCurrent) == osRemote)
  7649. {
  7650. Ext = UnixExtractFileExt(FileName);
  7651. }
  7652. else
  7653. {
  7654. Ext = ExtractFileExt(FileName);
  7655. }
  7656. if (Ext.IsEmpty())
  7657. {
  7658. Ext = L".";
  7659. }
  7660. TFileFilter Filter;
  7661. Filter.Masks = FORMAT(L"*%s", (Ext));
  7662. Filter.Directories = false;
  7663. CurrentDirView->SelectFiles(Filter, Select);
  7664. }
  7665. }
  7666. //---------------------------------------------------------------------------
  7667. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7668. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7669. {
  7670. UnicodeString Result;
  7671. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7672. {
  7673. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7674. }
  7675. else if ((Side == osRemote) && (Terminal == NULL))
  7676. {
  7677. // noop
  7678. }
  7679. else
  7680. {
  7681. Result =
  7682. FMTLOAD(FILE_INFO_FORMAT,
  7683. (FormatBytes(FileInfo.SelectedSize),
  7684. FormatBytes(FileInfo.FilesSize),
  7685. FormatNumber(FileInfo.SelectedCount),
  7686. FormatNumber(FileInfo.FilesCount)));
  7687. }
  7688. return Result;
  7689. }
  7690. //---------------------------------------------------------------------------
  7691. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7692. TTBXStatusPanel * Panel, TOperationSide Side)
  7693. {
  7694. if (Panel->Index == 1)
  7695. {
  7696. ToggleShowHiddenFiles();
  7697. }
  7698. else if (Panel->Index == 2)
  7699. {
  7700. Filter(Side);
  7701. }
  7702. }
  7703. //---------------------------------------------------------------------------
  7704. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7705. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7706. {
  7707. DebugAssert(!StatusBar->SimplePanel);
  7708. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7709. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7710. }
  7711. //---------------------------------------------------------------------------
  7712. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7713. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7714. {
  7715. DebugAssert(StatusBar->Panels->Count >= 3);
  7716. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7717. if (FileInfo.HiddenCount > 0)
  7718. {
  7719. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7720. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7721. }
  7722. else
  7723. {
  7724. HiddenFilesPanel->ViewPriority = 0;
  7725. // not really necessary, just to cleanup no-longer-valid data
  7726. HiddenFilesPanel->Caption = L"";
  7727. }
  7728. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7729. if (FileInfo.FilteredCount > 0)
  7730. {
  7731. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7732. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7733. }
  7734. else
  7735. {
  7736. FilteredFilesPanel->ViewPriority = 0;
  7737. // not really necessary, just to cleanup no-longer-valid data
  7738. FilteredFilesPanel->Caption = L"";
  7739. }
  7740. }
  7741. //---------------------------------------------------------------------------
  7742. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7743. TObject * /*Sender*/)
  7744. {
  7745. if (RemoteDirView->Enabled)
  7746. {
  7747. RemoteDirView->SetFocus();
  7748. }
  7749. }
  7750. //---------------------------------------------------------------------------
  7751. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7752. {
  7753. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7754. switch (Config.Show)
  7755. {
  7756. case qvShow:
  7757. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7758. {
  7759. Config.Show = qvHide;
  7760. }
  7761. else
  7762. {
  7763. Config.Show = Config.LastHideShow;
  7764. }
  7765. break;
  7766. case qvHideWhenEmpty:
  7767. if (ComponentVisible[fcQueueView])
  7768. {
  7769. Config.Show = qvHide;
  7770. }
  7771. else
  7772. {
  7773. Config.LastHideShow = Config.Show;
  7774. Config.Show = qvShow;
  7775. }
  7776. break;
  7777. case qvHide:
  7778. Config.LastHideShow = Config.Show;
  7779. Config.Show = qvShow;
  7780. break;
  7781. }
  7782. WinConfiguration->QueueView = Config;
  7783. }
  7784. //---------------------------------------------------------------------------
  7785. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7786. {
  7787. UnicodeString Result;
  7788. if (FTerminal != NULL)
  7789. {
  7790. switch (WinConfiguration->PathInCaption)
  7791. {
  7792. case picShort:
  7793. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7794. break;
  7795. case picFull:
  7796. Result = FTerminal->CurrentDirectory;
  7797. break;
  7798. }
  7799. }
  7800. return Result;
  7801. }
  7802. //---------------------------------------------------------------------------
  7803. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7804. {
  7805. TColor Result = GetWindowColor(FSessionColor);
  7806. return Result;
  7807. }
  7808. //---------------------------------------------------------------------------
  7809. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7810. {
  7811. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7812. return Result;
  7813. }
  7814. //---------------------------------------------------------------------------
  7815. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7816. {
  7817. TTerminalManager::Instance()->UpdateAppTitle();
  7818. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7819. // background to gray on Windows 7 (works on Windows 10).
  7820. // See also EnableControl
  7821. if (Showing)
  7822. {
  7823. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7824. if (HasTerminal)
  7825. {
  7826. // Update path when it changes
  7827. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7828. {
  7829. UpdateSessionTab(SessionsPageControl->ActivePage);
  7830. }
  7831. if (!RemoteDirView->Enabled)
  7832. {
  7833. RemoteDirView->Enabled = true;
  7834. if (FRemoteDirViewWasFocused)
  7835. {
  7836. ActiveControl = RemoteDirView;
  7837. }
  7838. }
  7839. RemoteDirView->Color = PanelColor();
  7840. }
  7841. else
  7842. {
  7843. if (RemoteDirView->Enabled)
  7844. {
  7845. // This is first called when the form is being constructed
  7846. // (not anymore due to Showing test above)
  7847. // but the false is overriden in the constructor later.
  7848. // An even later in TScpCommanderForm::DoShow()
  7849. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7850. RemoteDirView->Enabled = false;
  7851. RemoteDirView->Color = DisabledPanelColor();
  7852. }
  7853. }
  7854. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  7855. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  7856. RemoteDriveView->Color = RemoteDirView->Color;
  7857. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  7858. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  7859. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  7860. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  7861. QueueLabelUpdateStatus();
  7862. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  7863. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  7864. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7865. }
  7866. }
  7867. //---------------------------------------------------------------------------
  7868. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7869. {
  7870. UpdateControls();
  7871. }
  7872. //---------------------------------------------------------------------------
  7873. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7874. TObject * Sender)
  7875. {
  7876. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7877. DebugAssert(DirView != NULL);
  7878. DoDirViewLoaded(DirView);
  7879. TransferPresetAutoSelect();
  7880. }
  7881. //---------------------------------------------------------------------------
  7882. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7883. {
  7884. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7885. // first run after installation
  7886. if (double(Updates.LastCheck) == 0)
  7887. {
  7888. // make sure next time there will be an update (if enabled)
  7889. Updates.LastCheck = TDateTime(1);
  7890. WinConfiguration->Updates = Updates;
  7891. }
  7892. else if ((double(Updates.Period) > 0) &&
  7893. (Now() - Updates.LastCheck >= Updates.Period))
  7894. {
  7895. TThreadMethod OnUpdatesChecked = NULL;
  7896. if (!IsUWP())
  7897. {
  7898. OnUpdatesChecked = UpdatesChecked;
  7899. }
  7900. StartUpdateThread(OnUpdatesChecked);
  7901. }
  7902. }
  7903. //---------------------------------------------------------------------------
  7904. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7905. {
  7906. UnicodeString Message;
  7907. bool New;
  7908. TQueryType Type;
  7909. GetUpdatesMessage(Message, New, Type, false);
  7910. if (!Message.IsEmpty())
  7911. {
  7912. if (New)
  7913. {
  7914. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7915. }
  7916. if (!New && (Type != qtWarning))
  7917. {
  7918. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7919. }
  7920. else
  7921. {
  7922. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7923. }
  7924. if (New)
  7925. {
  7926. Configuration->Usage->Inc(L"UpdateNotifications");
  7927. }
  7928. }
  7929. }
  7930. //---------------------------------------------------------------------------
  7931. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7932. {
  7933. RestoreApp();
  7934. bool CanDisplay = !NonVisualDataModule->Busy;
  7935. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7936. {
  7937. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7938. }
  7939. if (CanDisplay)
  7940. {
  7941. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7942. CheckForUpdates(true);
  7943. }
  7944. else
  7945. {
  7946. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7947. }
  7948. }
  7949. //---------------------------------------------------------------------------
  7950. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7951. TCopyParamRuleData & Data)
  7952. {
  7953. DebugAssert(Terminal != NULL);
  7954. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7955. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7956. Data.RemoteDirectory = RemoteDirView->PathName;
  7957. }
  7958. //---------------------------------------------------------------------------
  7959. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7960. {
  7961. // Terminal can be null when we are changing local directory implicitly,
  7962. // because it has been deleted, while no session is connected
  7963. // (Login dialog is open)
  7964. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7965. {
  7966. TCopyParamRuleData Data;
  7967. GetTransferPresetAutoSelectData(Data);
  7968. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7969. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7970. if (CopyParamIndex < 0)
  7971. {
  7972. // there is no preset that matches autoselection
  7973. // set preset that we consider "default"
  7974. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7975. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7976. }
  7977. else
  7978. {
  7979. // there is preset matching autoselection
  7980. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7981. if (CopyParamName == FCopyParamAutoSelected)
  7982. {
  7983. // autoselected the same preset as the last time
  7984. // make no change (i.e. preserve custom user preset, if any)
  7985. }
  7986. else
  7987. {
  7988. // autoselected the different preset then the last time (or there
  7989. // was default preset set)
  7990. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7991. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7992. }
  7993. }
  7994. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7995. {
  7996. if (CopyParamIndex >= 0)
  7997. {
  7998. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7999. }
  8000. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8001. try
  8002. {
  8003. int Fmt =
  8004. (CopyParamIndex < 0) ?
  8005. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8006. COPY_PARAM_AUTOSELECTED;
  8007. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8008. Data->Message = MainInstructions(Message);
  8009. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8010. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8011. CopyParamAttrs);
  8012. if (CopyParamIndex >= 0)
  8013. {
  8014. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8015. Info = FORMAT(L"%s\n \n%s", (Info,
  8016. FMTLOAD(COPY_PARAM_RULE,
  8017. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8018. }
  8019. Data->Message += L"\n\n" + Info;
  8020. if (WinConfiguration->CopyParamAutoSelectNotice)
  8021. {
  8022. TransferPresetNoteMessage(Data, true);
  8023. }
  8024. else
  8025. {
  8026. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8027. Data = NULL; // ownership passed
  8028. }
  8029. }
  8030. __finally
  8031. {
  8032. delete Data;
  8033. }
  8034. }
  8035. }
  8036. }
  8037. //---------------------------------------------------------------------------
  8038. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8039. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8040. {
  8041. DebugAssert(NoteData != NULL);
  8042. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8043. TQueryButtonAlias Aliases[1];
  8044. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8045. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8046. Params.Aliases = Aliases;
  8047. Params.AliasesCount = LENOF(Aliases);
  8048. unsigned int Result =
  8049. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8050. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8051. switch (Result)
  8052. {
  8053. case qaNeverAskAgain:
  8054. DebugAssert(AllowNeverAskAgain);
  8055. WinConfiguration->CopyParamAutoSelectNotice = false;
  8056. break;
  8057. case qaIgnore:
  8058. PreferencesDialog(pmPresets);
  8059. break;
  8060. }
  8061. }
  8062. //---------------------------------------------------------------------------
  8063. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8064. {
  8065. // as of now useless, as this is used for notes only, never for balloons, ...
  8066. RestoreApp();
  8067. // .. and we should never be busy here
  8068. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8069. {
  8070. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8071. }
  8072. }
  8073. //---------------------------------------------------------------------------
  8074. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8075. TPreferencesMode APreferencesMode)
  8076. {
  8077. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8078. TCopyParamRuleData Data;
  8079. if (Terminal != NULL)
  8080. {
  8081. PreferencesData.reset(new TPreferencesDialogData());
  8082. GetTransferPresetAutoSelectData(Data);
  8083. PreferencesData->CopyParamRuleData = &Data;
  8084. }
  8085. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8086. }
  8087. //---------------------------------------------------------------------------
  8088. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8089. const TCustomCommandType & Command)
  8090. {
  8091. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8092. {
  8093. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8094. }
  8095. }
  8096. //---------------------------------------------------------------------------
  8097. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8098. {
  8099. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8100. TCustomCommandType Command;
  8101. // make sure we use local custom command when remote are not supported
  8102. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8103. {
  8104. Command = FLastCustomCommand;
  8105. }
  8106. else
  8107. {
  8108. Command.Params = Command.Params | ccLocal;
  8109. }
  8110. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8111. FEditingFocusedAdHocCommand = OnFocused;
  8112. bool LocalSide = (FCurrentSide == osLocal);
  8113. int Options =
  8114. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8115. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8116. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8117. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8118. {
  8119. FLastCustomCommand = Command;
  8120. UpdateCustomCommandsToolbar();
  8121. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8122. }
  8123. }
  8124. //---------------------------------------------------------------------------
  8125. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8126. {
  8127. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8128. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8129. DebugAssert(State > 0);
  8130. if (State <= 0)
  8131. {
  8132. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8133. }
  8134. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8135. }
  8136. //---------------------------------------------------------------------------
  8137. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8138. TCustomCommandType & Command, int & State)
  8139. {
  8140. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8141. if (Result)
  8142. {
  8143. Command = FLastCustomCommand;
  8144. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8145. }
  8146. return Result;
  8147. }
  8148. //---------------------------------------------------------------------------
  8149. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8150. {
  8151. if (RemoteDirView->ItemFocused != NULL)
  8152. {
  8153. RemoteDirView->ItemFocused->CancelEdit();
  8154. }
  8155. ResetIncrementalSearch();
  8156. }
  8157. //---------------------------------------------------------------------------
  8158. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8159. {
  8160. DebugAssert(ComponentVisible[Component]);
  8161. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8162. }
  8163. //---------------------------------------------------------------------------
  8164. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8165. {
  8166. // when queue panel is resized here directly, the status bar is stretched
  8167. // over whole space the panel occupied
  8168. PostComponentHide(fcQueueView);
  8169. }
  8170. //---------------------------------------------------------------------------
  8171. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8172. {
  8173. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8174. if (FInvalid)
  8175. {
  8176. WinConfiguration->ResetSysDarkTheme();
  8177. ConfigurationChanged();
  8178. ConfigureInterface();
  8179. }
  8180. TForm::Dispatch(&Message);
  8181. }
  8182. //---------------------------------------------------------------------------
  8183. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8184. {
  8185. TMessage * M = static_cast<TMessage*>(Message);
  8186. switch (M->Msg)
  8187. {
  8188. case CM_DIALOGCHAR:
  8189. CMDialogChar(*M);
  8190. break;
  8191. case WM_APPCOMMAND:
  8192. WMAppCommand(*M);
  8193. break;
  8194. case WM_SYSCOMMAND:
  8195. WMSysCommand(*M);
  8196. break;
  8197. case WM_QUERYENDSESSION:
  8198. WMQueryEndSession(*M);
  8199. break;
  8200. case WM_ENDSESSION:
  8201. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8202. break;
  8203. #ifdef _DEBUG
  8204. case WM_WINDOWPOSCHANGED:
  8205. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8206. break;
  8207. case CM_CUSTOMSTYLECHANGED:
  8208. TForm::Dispatch(Message);
  8209. break;
  8210. #endif
  8211. case WM_COMPONENT_HIDE:
  8212. {
  8213. Byte Component = static_cast<Byte>(M->WParam);
  8214. // sanity check
  8215. if (ComponentVisible[Component])
  8216. {
  8217. // special treatment
  8218. if (Component == fcQueueView)
  8219. {
  8220. ToggleQueueVisibility();
  8221. DebugAssert(!ComponentVisible[fcQueueView]);
  8222. }
  8223. else
  8224. {
  8225. ComponentVisible[Component] = false;
  8226. }
  8227. }
  8228. }
  8229. break;
  8230. case WM_COPYDATA:
  8231. WMCopyData(*M);
  8232. break;
  8233. case WM_MANAGES_CAPTION:
  8234. // caption managed in TTerminalManager::UpdateAppTitle()
  8235. M->Result = 1;
  8236. break;
  8237. case WM_WANTS_MOUSEWHEEL:
  8238. M->Result = 1;
  8239. break;
  8240. case CM_SHOWINGCHANGED:
  8241. CMShowingChanged(*M);
  8242. break;
  8243. case WM_CLOSE:
  8244. WMClose(*M);
  8245. break;
  8246. case WM_DPICHANGED:
  8247. WMDpiChanged(*M);
  8248. break;
  8249. case CM_DPICHANGED:
  8250. CMDpiChanged(*M);
  8251. break;
  8252. case WM_WININICHANGE:
  8253. WMWinIniChange(*M);
  8254. break;
  8255. case CM_DIALOGKEY:
  8256. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8257. break;
  8258. default:
  8259. TForm::Dispatch(Message);
  8260. break;
  8261. }
  8262. }
  8263. //---------------------------------------------------------------------------
  8264. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8265. {
  8266. // noop
  8267. }
  8268. //---------------------------------------------------------------------------
  8269. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8270. {
  8271. TForm::Dispatch(&Message);
  8272. FSessionColors->Clear();
  8273. AddFixedSessionImages();
  8274. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8275. UpdateImages();
  8276. }
  8277. //---------------------------------------------------------------------------
  8278. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8279. {
  8280. TForm::Dispatch(&Message);
  8281. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8282. UpdatePixelsPerInchMainWindowCounter();
  8283. }
  8284. //---------------------------------------------------------------------------
  8285. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8286. {
  8287. // Cannot close window while we are busy.
  8288. // We cannot test this in FormCloseQuery as that is called also from
  8289. // Close(), which is called by CloseApplicationAction. So we can be busy
  8290. // there already even, when it is legitimate to close the application.
  8291. // Possibly a better place to handle this would be WMSysCommand.
  8292. if (NonVisualDataModule->Busy)
  8293. {
  8294. Message.Result = 1;
  8295. }
  8296. else
  8297. {
  8298. TForm::Dispatch(&Message);
  8299. }
  8300. }
  8301. //---------------------------------------------------------------------------
  8302. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8303. {
  8304. TForm::Dispatch(&Message);
  8305. if (Showing && (Terminal == NULL))
  8306. {
  8307. // When we are starting minimized (i.e. from an installer),
  8308. // postpone showing Login dialog until we get restored.
  8309. // Otherwise the Login dialog (and Authentication window) show restored
  8310. // over invidible (minimized) main window.
  8311. if (WindowState == wsMinimized)
  8312. {
  8313. FNeedSession = true;
  8314. }
  8315. else
  8316. {
  8317. // This happens before application ever goes idle, so the toolbars would
  8318. // stay enabled (initial state) until the Login dialog is dismissed.
  8319. UpdateActions();
  8320. NonVisualDataModule->StartBusy();
  8321. try
  8322. {
  8323. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8324. // otherwise the dialog does not receive focus.
  8325. // With Commander interface the ProcessMessages is called already
  8326. // by TDriveView, but with Explorer interface, we need to call it explicily
  8327. Application->ProcessMessages();
  8328. // do not reload sessions, they have been loaded just now (optimization)
  8329. NeedSession(false);
  8330. }
  8331. __finally
  8332. {
  8333. NonVisualDataModule->EndBusy();
  8334. }
  8335. }
  8336. }
  8337. }
  8338. //---------------------------------------------------------------------------
  8339. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8340. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8341. int & MaxHeight)
  8342. {
  8343. // workaround for bug in TWinControl.CalcConstraints
  8344. // Check for empty rect (restore from iconic state) is done there only after
  8345. // call to AdjustClientRect, which enlarges the rect (for forms).
  8346. TRect R = GetClientRect();
  8347. // when restoring from iconic state, place no restrictions
  8348. if (IsRectEmpty(R))
  8349. {
  8350. MinWidth = 0;
  8351. MinHeight = 0;
  8352. MaxWidth = 32000;
  8353. MaxHeight = 32000;
  8354. }
  8355. }
  8356. //---------------------------------------------------------------------------
  8357. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8358. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8359. {
  8360. // terminal can be already closed (e.g. dropped connection)
  8361. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8362. {
  8363. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8364. try
  8365. {
  8366. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8367. }
  8368. catch(...)
  8369. {
  8370. if (!Terminal->Active)
  8371. {
  8372. Close = true;
  8373. }
  8374. throw;
  8375. }
  8376. }
  8377. }
  8378. //---------------------------------------------------------------------------
  8379. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8380. {
  8381. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8382. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8383. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8384. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8385. {
  8386. OnGetSpaceAvailable = GetSpaceAvailable;
  8387. }
  8388. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8389. OnGetSpaceAvailable);
  8390. }
  8391. //---------------------------------------------------------------------------
  8392. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8393. {
  8394. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8395. Terminal->FillSessionDataForCode(Data.get());
  8396. return Data.release();
  8397. }
  8398. //---------------------------------------------------------------------------
  8399. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8400. {
  8401. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8402. DoGenerateUrlDialog(Data.get(), Paths);
  8403. }
  8404. //---------------------------------------------------------------------------
  8405. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8406. {
  8407. GenerateUrl(NULL);
  8408. }
  8409. //---------------------------------------------------------------------------
  8410. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8411. {
  8412. std::unique_ptr<TStrings> Paths(new TStringList());
  8413. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8414. GenerateUrl(Paths.get());
  8415. }
  8416. //---------------------------------------------------------------------------
  8417. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8418. {
  8419. FSessionColor = value;
  8420. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8421. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8422. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8423. DebugAssert(ColorItem != NULL);
  8424. ColorItem->Color = C;
  8425. NonVisualDataModule->ColorMenuItem->Color = C;
  8426. UpdateControls();
  8427. }
  8428. //---------------------------------------------------------------------------
  8429. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8430. {
  8431. if (value != FSessionColor)
  8432. {
  8433. UpdateSessionColor(value);
  8434. // Is null when called from LastTerminalClosed
  8435. if (Terminal != NULL)
  8436. {
  8437. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8438. }
  8439. }
  8440. }
  8441. //---------------------------------------------------------------------------
  8442. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8443. {
  8444. bool Result = FNoteTimer->Enabled;
  8445. if (Result)
  8446. {
  8447. // cannot cancel note too early
  8448. bool NotEarly =
  8449. (Now() - FNoteShown >
  8450. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8451. if (Force || NotEarly)
  8452. {
  8453. FNoteTimer->Enabled = false;
  8454. FNote = L"";
  8455. SAFE_DESTROY(FNoteData);
  8456. FOnNoteClick = NULL;
  8457. FNoteHints = FNotes->Text;
  8458. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8459. UpdateStatusBar();
  8460. }
  8461. }
  8462. return Result;
  8463. }
  8464. //---------------------------------------------------------------------------
  8465. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8466. {
  8467. DebugAssert(FNoteTimer->Enabled);
  8468. CancelNote(true);
  8469. }
  8470. //---------------------------------------------------------------------------
  8471. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8472. {
  8473. int P = Note.Pos(L"\n");
  8474. if (P > 0)
  8475. {
  8476. Note.SetLength(P - 1);
  8477. }
  8478. FNotes->Add(FORMAT(L"[%s] %s",
  8479. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8480. while (FNotes->Count > 10)
  8481. {
  8482. FNotes->Delete(0);
  8483. }
  8484. if (UpdateNow)
  8485. {
  8486. FNoteHints = FNotes->Text;
  8487. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8488. UpdateStatusBar();
  8489. }
  8490. }
  8491. //---------------------------------------------------------------------------
  8492. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8493. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8494. {
  8495. int P = Note.Pos(L"\n");
  8496. if (P > 0)
  8497. {
  8498. Note.SetLength(P - 1);
  8499. }
  8500. FNoteHints = FNotes->Text;
  8501. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8502. FNote = Note;
  8503. SAFE_DESTROY(FNoteData);
  8504. FNoteData = NoteData;
  8505. FOnNoteClick = OnNoteClick;
  8506. AddNote(Note, false);
  8507. UpdateStatusBar();
  8508. FNoteShown = Now();
  8509. FNoteTimer->Enabled = false;
  8510. if (Seconds == 0)
  8511. {
  8512. Seconds = WinConfiguration->NotificationsTimeout;
  8513. }
  8514. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8515. FNoteTimer->Enabled = true;
  8516. }
  8517. //---------------------------------------------------------------------------
  8518. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8519. {
  8520. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8521. }
  8522. //---------------------------------------------------------------------------
  8523. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8524. {
  8525. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8526. {
  8527. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8528. }
  8529. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8530. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8531. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8532. 0, NULL, NULL);
  8533. }
  8534. //---------------------------------------------------------------------------
  8535. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8536. {
  8537. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8538. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8539. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8540. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8541. }
  8542. //---------------------------------------------------------------------------
  8543. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8544. {
  8545. WinConfiguration->FormatSizeBytes = Style;
  8546. }
  8547. //---------------------------------------------------------------------------
  8548. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8549. {
  8550. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8551. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8552. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8553. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8554. }
  8555. //---------------------------------------------------------------------------
  8556. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8557. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8558. {
  8559. if (Panel->Index == 0)
  8560. {
  8561. if (FOnNoteClick != NULL)
  8562. {
  8563. // prevent the user data from being freed by possible call
  8564. // to CancelNote or PostNote during call to OnNoteClick
  8565. std::unique_ptr<TObject> NoteData(FNoteData);
  8566. TNotifyEvent OnNoteClick = FOnNoteClick;
  8567. FNoteData = NULL;
  8568. // need to cancel the note as we are going to delete its user data
  8569. CancelNote(true);
  8570. OnNoteClick(NoteData.get());
  8571. }
  8572. }
  8573. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8574. {
  8575. FileSystemInfo();
  8576. }
  8577. }
  8578. //---------------------------------------------------------------------------
  8579. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8580. {
  8581. // workaround:
  8582. // 1) for unknown reason, disabling window, while minimized,
  8583. // prevents it from restoring, even if it was enabled again meanwhile
  8584. // 2) when disabling the main window, while another has focus
  8585. // minimize is no longer possible
  8586. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8587. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8588. // Shouldn't we use IsApplicationMinimized() here?
  8589. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8590. {
  8591. Enabled = false;
  8592. }
  8593. FLockLevel++;
  8594. }
  8595. //---------------------------------------------------------------------------
  8596. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8597. {
  8598. DebugAssert(FLockLevel > 0);
  8599. FLockLevel--;
  8600. if (FLockLevel == 0)
  8601. {
  8602. DebugAssert(FLockSuspendLevel == 0);
  8603. Enabled = true;
  8604. }
  8605. }
  8606. //---------------------------------------------------------------------------
  8607. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8608. {
  8609. // We need to make sure that window is enabled when the last modal window closes
  8610. // otherwise focus is not restored correctly.
  8611. // So we re-enable the window when modal window opens and
  8612. // disable it back after is closes
  8613. if (FLockLevel > 0)
  8614. {
  8615. // while we have nesting counter, we know that we never be called
  8616. // recursivelly as Application->OnModalBegin is called only
  8617. // for the top-level modal window
  8618. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8619. {
  8620. // won't be disabled when conditions in LockWindow() were not satisfied
  8621. FDisabledOnLockSuspend = !Enabled;
  8622. // When minimized to tray (or actually when set to SW_HIDE),
  8623. // setting Enabled makes the window focusable even when there's
  8624. // modal window over it
  8625. if (!FTrayIcon->Visible)
  8626. {
  8627. Enabled = true;
  8628. }
  8629. }
  8630. FLockSuspendLevel++;
  8631. }
  8632. }
  8633. //---------------------------------------------------------------------------
  8634. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8635. {
  8636. if (FLockSuspendLevel > 0)
  8637. {
  8638. DebugAssert(FLockLevel > 0);
  8639. FLockSuspendLevel--;
  8640. // see comment in SuspendWindowLock
  8641. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8642. {
  8643. // Note that window can be enabled here, when we were minized to tray when
  8644. // was SuspendWindowLock() called.
  8645. // We should possibly do the same check as in LockWindow(),
  8646. // if it is ever possible that the consitions change between
  8647. // SuspendWindowLock() and ResumeWindowLock()
  8648. Enabled = !FDisabledOnLockSuspend;
  8649. }
  8650. }
  8651. }
  8652. //---------------------------------------------------------------------------
  8653. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8654. {
  8655. if (!TextOnly)
  8656. {
  8657. TTBXComboBoxItem * RemotePathComboBox =
  8658. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8659. TStrings * Items = RemotePathComboBox->Strings;
  8660. Items->BeginUpdate();
  8661. try
  8662. {
  8663. Items->Clear();
  8664. if (Terminal != NULL)
  8665. {
  8666. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8667. while (!IsUnixRootPath(APath))
  8668. {
  8669. int P = APath.LastDelimiter(L'/');
  8670. DebugAssert(P >= 0);
  8671. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8672. APath.SetLength(P - 1);
  8673. }
  8674. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8675. }
  8676. }
  8677. __finally
  8678. {
  8679. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8680. // Setting ItemIndex to -1 does not reset its text
  8681. if (Items->Count == 0)
  8682. {
  8683. RemotePathComboBox->Text = L"";
  8684. }
  8685. Items->EndUpdate();
  8686. }
  8687. }
  8688. }
  8689. //---------------------------------------------------------------------------
  8690. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8691. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8692. int & ImageIndex)
  8693. {
  8694. if (AIndex < 0)
  8695. {
  8696. AIndex = Sender->ItemIndex;
  8697. }
  8698. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8699. }
  8700. //---------------------------------------------------------------------------
  8701. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8702. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8703. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8704. {
  8705. ARect.Left += (10 * AIndex);
  8706. }
  8707. //---------------------------------------------------------------------------
  8708. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8709. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8710. {
  8711. AWidth += (10 * AIndex);
  8712. }
  8713. //---------------------------------------------------------------------------
  8714. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8715. TObject * Sender)
  8716. {
  8717. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8718. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8719. int Index = RemotePathComboBox->ItemIndex;
  8720. while (Index < RemotePathComboBox->Strings->Count - 1)
  8721. {
  8722. APath = UnixExtractFileDir(APath);
  8723. Index++;
  8724. }
  8725. // VanDyke style paths
  8726. if (APath.IsEmpty())
  8727. {
  8728. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8729. APath = ROOTDIRECTORY;
  8730. }
  8731. if (RemoteDirView->Path != APath)
  8732. {
  8733. RemoteDirView->Path = APath;
  8734. }
  8735. }
  8736. //---------------------------------------------------------------------------
  8737. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8738. {
  8739. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8740. DebugUsedParam(Sender);
  8741. UpdateRemotePathComboBox(true);
  8742. }
  8743. //---------------------------------------------------------------------------
  8744. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8745. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8746. {
  8747. ResetIncrementalSearch();
  8748. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8749. DebugAssert(DirView != NULL);
  8750. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8751. {
  8752. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8753. // OnEditing is called also from TCustomListView::CanEdit
  8754. if (Edit != NULL)
  8755. {
  8756. EditSelectBaseName(Edit);
  8757. }
  8758. }
  8759. }
  8760. //---------------------------------------------------------------------------
  8761. //---------------------------------------------------------------------------
  8762. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8763. {
  8764. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8765. Result->AutoDetectDnD = false;
  8766. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8767. Result->RenderDataOn = rdoEnterAndDropSync;
  8768. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8769. return Result;
  8770. }
  8771. //---------------------------------------------------------------------------
  8772. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8773. {
  8774. TForm::CreateWnd();
  8775. if (FSessionsDragDropFilesEx == NULL)
  8776. {
  8777. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8778. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8779. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8780. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8781. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8782. }
  8783. if (FQueueDragDropFilesEx == NULL)
  8784. {
  8785. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8786. // No need to set OnDragOver as we do not have any restrictions
  8787. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8788. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8789. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8790. }
  8791. }
  8792. //---------------------------------------------------------------------------
  8793. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8794. {
  8795. TForm::DestroyWnd();
  8796. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8797. FQueueDragDropFilesEx->DragDropControl = NULL;
  8798. }
  8799. //---------------------------------------------------------------------------
  8800. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8801. {
  8802. SideEnter(FCurrentSide);
  8803. FEverShown = true;
  8804. }
  8805. //---------------------------------------------------------------------------
  8806. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8807. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8808. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8809. {
  8810. if (!NonVisualDataModule->Busy)
  8811. {
  8812. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8813. Configuration->Usage->Inc(L"FileFinds");
  8814. LockWindow(true);
  8815. NonVisualDataModule->StartBusy();
  8816. WinConfiguration->LockedInterface = true;
  8817. try
  8818. {
  8819. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8820. }
  8821. __finally
  8822. {
  8823. WinConfiguration->LockedInterface = false;
  8824. NonVisualDataModule->EndBusy();
  8825. UnlockWindow();
  8826. }
  8827. }
  8828. }
  8829. //---------------------------------------------------------------------------
  8830. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8831. {
  8832. if (!NonVisualDataModule->Busy)
  8833. {
  8834. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8835. SetFocus();
  8836. RemoteDirView->Path = UnixExtractFilePath(Path);
  8837. UnicodeString FileName = UnixExtractFileName(Path);
  8838. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  8839. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  8840. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  8841. if (Item == NULL)
  8842. {
  8843. ReloadDirectory(osRemote);
  8844. Item = RemoteDirView->FindFileItem(FileName);
  8845. }
  8846. if (Item != NULL)
  8847. {
  8848. RemoteDirView->ItemFocused = Item;
  8849. RemoteDirView->ItemFocused->MakeVisible(false);
  8850. RemoteDirView->SetFocus();
  8851. }
  8852. }
  8853. }
  8854. //---------------------------------------------------------------------------
  8855. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8856. {
  8857. bool Result = !NonVisualDataModule->Busy;
  8858. if (Result)
  8859. {
  8860. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8861. }
  8862. return Result;
  8863. }
  8864. //---------------------------------------------------------------------------
  8865. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8866. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8867. {
  8868. if (CanOperateOnFoundFiles(ATerminal))
  8869. {
  8870. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8871. FOnFileOperationFinished = OnFileOperationFinished;
  8872. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8873. }
  8874. }
  8875. //---------------------------------------------------------------------------
  8876. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8877. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8878. {
  8879. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8880. }
  8881. //---------------------------------------------------------------------------
  8882. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8883. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8884. {
  8885. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8886. }
  8887. //---------------------------------------------------------------------------
  8888. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8889. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8890. {
  8891. if (CanOperateOnFoundFiles(ATerminal))
  8892. {
  8893. for (int Index = 0; Index < FileList->Count; Index++)
  8894. {
  8895. UnicodeString FileName = FileList->Strings[Index];
  8896. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8897. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8898. OnFileOperationFinished(FileName, true);
  8899. }
  8900. }
  8901. }
  8902. //---------------------------------------------------------------------------
  8903. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8904. {
  8905. FFileFindTerminal = Terminal;
  8906. ShowFileFindDialog(
  8907. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8908. DoDownloadFoundFiles, DoEditFoundFiles);
  8909. }
  8910. //---------------------------------------------------------------------------
  8911. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8912. {
  8913. FTaskbarList = TaskbarList;
  8914. }
  8915. //---------------------------------------------------------------------------
  8916. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8917. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8918. {
  8919. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8920. if (Index >= 0)
  8921. {
  8922. if (Button == mbLeft)
  8923. {
  8924. // "Mouse down" is raised only after tab is switched.
  8925. // If switching tab (switching session) takes long enough for user
  8926. // to actually release the button, "mouse down" is still raised,
  8927. // but we do not get "mouse up" event, so dragging is not cancelled,
  8928. // prevent that by not beginning dragging in the first place.
  8929. if (IsKeyPressed(VK_LBUTTON))
  8930. {
  8931. // when user clicks the "+", we get mouse down only after the session
  8932. // is closed, when new session tab is already on X:Y, so dragging
  8933. // starts, prevent that
  8934. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8935. {
  8936. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8937. if (Terminal != NULL)
  8938. {
  8939. SessionsPageControl->BeginDrag(false);
  8940. }
  8941. }
  8942. }
  8943. }
  8944. else if (Button == mbMiddle)
  8945. {
  8946. // ignore middle-click for "New session tab"
  8947. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8948. if (Terminal != NULL)
  8949. {
  8950. CloseSessionTab(Index);
  8951. }
  8952. }
  8953. }
  8954. }
  8955. //---------------------------------------------------------------------------
  8956. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8957. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8958. {
  8959. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8960. // do not allow dropping on the "+" tab
  8961. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8962. if ((TargetTerminal != NULL) &&
  8963. (SessionsPageControl->ActivePage->PageIndex != Index))
  8964. {
  8965. Configuration->Usage->Inc(L"SessionTabMoves");
  8966. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8967. // but we want to actually prevent that to avoid flicker
  8968. SessionsPageControl->ActivePage->PageIndex = Index;
  8969. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8970. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8971. }
  8972. }
  8973. //---------------------------------------------------------------------------
  8974. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8975. TObject * Sender, TObject * Source, int X, int Y,
  8976. TDragState /*State*/, bool & Accept)
  8977. {
  8978. Accept = (Sender == Source);
  8979. if (Accept)
  8980. {
  8981. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8982. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8983. // do not allow dragging to the "+" tab
  8984. Accept = (Terminal != NULL);
  8985. }
  8986. }
  8987. //---------------------------------------------------------------------------
  8988. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8989. const TPoint & Point, int & Effect)
  8990. {
  8991. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8992. if (Index < 0)
  8993. {
  8994. Effect = DROPEFFECT_NONE;
  8995. }
  8996. else
  8997. {
  8998. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8999. // do not allow dropping on the "+" tab
  9000. if (TargetTerminal == NULL)
  9001. {
  9002. Effect = DROPEFFECT_NONE;
  9003. }
  9004. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9005. {
  9006. Effect = DROPEFFECT_COPY;
  9007. }
  9008. }
  9009. }
  9010. //---------------------------------------------------------------------------
  9011. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9012. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9013. {
  9014. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9015. // do not allow dropping on the "+" tab
  9016. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9017. if (TargetTerminal != NULL)
  9018. {
  9019. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9020. if (!IsFileControl(DropSourceControl, osRemote))
  9021. {
  9022. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9023. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9024. // Why don't we use Terminal->CurrentDirectory directly?
  9025. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9026. // do not force queue, drag drop
  9027. false, true);
  9028. }
  9029. }
  9030. }
  9031. //---------------------------------------------------------------------------
  9032. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9033. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9034. {
  9035. // Downloads are handled in RemoteFileControlDDEnd
  9036. if (!IsFileControl(DropSourceControl, osRemote))
  9037. {
  9038. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9039. Terminal->CurrentDirectory,
  9040. // force queue, drag drop
  9041. true, true);
  9042. }
  9043. }
  9044. //---------------------------------------------------------------------------
  9045. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9046. {
  9047. FShowing = false;
  9048. // Do not save empty workspace
  9049. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9050. {
  9051. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9052. UnicodeString Name = WorkspaceName();
  9053. DoSaveWorkspace(
  9054. Name, DataList.get(),
  9055. !Configuration->DisablePasswordStoring &&
  9056. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9057. WinConfiguration->LastStoredSession = Name;
  9058. }
  9059. }
  9060. //---------------------------------------------------------------------------
  9061. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9062. {
  9063. TManagedTerminal * ManagedTerminal =
  9064. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9065. if (ManagedTerminal != NULL)
  9066. {
  9067. ManagedTerminal->DirectoryLoaded = Now();
  9068. }
  9069. }
  9070. //---------------------------------------------------------------------------
  9071. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9072. TListItem * /*Item*/, bool /*Selected*/)
  9073. {
  9074. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9075. switch (DirView->LastSelectMethod)
  9076. {
  9077. case smKeyboard:
  9078. Configuration->Usage->Inc(L"KeyboardSelections");
  9079. break;
  9080. case smMouse:
  9081. Configuration->Usage->Inc(L"MouseSelections");
  9082. break;
  9083. }
  9084. }
  9085. //---------------------------------------------------------------------------
  9086. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9087. {
  9088. if (Color != 0)
  9089. {
  9090. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9091. }
  9092. else
  9093. {
  9094. return FSessionTabImageIndex;
  9095. }
  9096. }
  9097. //---------------------------------------------------------------------------
  9098. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9099. {
  9100. TPngImageCollectionItem * Item =
  9101. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9102. FSessionColors->AddPng(Item->PngImage);
  9103. return FSessionColors->Count - 1;
  9104. }
  9105. //---------------------------------------------------------------------------
  9106. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9107. {
  9108. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9109. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9110. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9111. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9112. }
  9113. //---------------------------------------------------------------------------
  9114. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9115. {
  9116. for (int Index = 0; Index < Control->ControlCount; Index++)
  9117. {
  9118. TControl * AControl = Control->Controls[Index];
  9119. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9120. if (WinControl != NULL)
  9121. {
  9122. CollectItemsWithTextDisplayMode(WinControl);
  9123. }
  9124. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9125. if (Toolbar != NULL)
  9126. {
  9127. // we care for top-level items only
  9128. TTBCustomItem * Items = Toolbar->Items;
  9129. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9130. {
  9131. TTBCustomItem * Item = Items->Items[ItemIndex];
  9132. if (((Item->DisplayMode == nbdmImageAndText) ||
  9133. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9134. EligibleForImageDisplayMode(Item))
  9135. {
  9136. FItemsWithTextDisplayMode.insert(Item);
  9137. }
  9138. }
  9139. }
  9140. }
  9141. }
  9142. //---------------------------------------------------------------------------
  9143. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9144. {
  9145. return true;
  9146. }
  9147. //---------------------------------------------------------------------------
  9148. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9149. {
  9150. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9151. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9152. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9153. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9154. bool Result = true;
  9155. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9156. {
  9157. TTBCustomItem * Item = *i;
  9158. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9159. if (Label != NULL)
  9160. {
  9161. // optimization
  9162. if (Label->Visible == SelectiveToolbarText)
  9163. {
  9164. Result = false;
  9165. }
  9166. else
  9167. {
  9168. Label->Visible = SelectiveToolbarText;
  9169. }
  9170. }
  9171. else
  9172. {
  9173. // optimization
  9174. if (Item->DisplayMode == DisplayMode)
  9175. {
  9176. Result = false;
  9177. }
  9178. else
  9179. {
  9180. Item->DisplayMode = DisplayMode;
  9181. }
  9182. }
  9183. ++i;
  9184. }
  9185. if (Result)
  9186. {
  9187. UpdateNewSessionTab();
  9188. }
  9189. return Result;
  9190. }
  9191. //---------------------------------------------------------------------------
  9192. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9193. {
  9194. DebugFail();
  9195. }
  9196. //---------------------------------------------------------------------------
  9197. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9198. {
  9199. // Among other, a lock level is non zero, while directory is being loaded,
  9200. // even when it happens as a result of dirview navigation,
  9201. // i.e. when TNonVisualDataModule is NOT FBusy.
  9202. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9203. // Among other this prevents a panel auto update to occur while
  9204. // directory is changing.
  9205. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9206. }
  9207. //---------------------------------------------------------------------------
  9208. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9209. {
  9210. // While the window is disabled, we still seem to process menu shortcuts at least,
  9211. // so stop it at least here.
  9212. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9213. return
  9214. (Allowed == aaUpdate) ||
  9215. !NonVisualDataModule->Busy;
  9216. }
  9217. //---------------------------------------------------------------------------
  9218. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9219. {
  9220. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9221. }
  9222. //---------------------------------------------------------------------------
  9223. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9224. {
  9225. // This is somewhat redundant to LockWindow() call from
  9226. // TTerminalManager::TerminalReadDirectoryProgress.
  9227. // But disabling window is known not to block keyboard shorcuts
  9228. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9229. if (Busy > 0)
  9230. {
  9231. if (NonVisualDataModule->Busy)
  9232. {
  9233. State = false;
  9234. }
  9235. else
  9236. {
  9237. NonVisualDataModule->StartBusy();
  9238. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9239. LockWindow();
  9240. }
  9241. }
  9242. else if (Busy < 0)
  9243. {
  9244. UnlockWindow();
  9245. NonVisualDataModule->EndBusy();
  9246. }
  9247. else
  9248. {
  9249. State = NonVisualDataModule->Busy;
  9250. }
  9251. }
  9252. //---------------------------------------------------------------------------
  9253. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9254. {
  9255. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9256. // no context menu for "New session tab"
  9257. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9258. {
  9259. SessionsPageControl->ActivePageIndex = Index;
  9260. if (DebugAlwaysTrue(SessionTabSwitched()))
  9261. {
  9262. // copied from TControl.WMContextMenu
  9263. SendCancelMode(SessionsPageControl);
  9264. // explicit popup instead of using PopupMenu property
  9265. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9266. // while connecting yet not-connected session and hence
  9267. // allowing an access to commands over not-completelly connected session
  9268. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9269. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9270. PopupMenu->PopupComponent = SessionsPageControl;
  9271. PopupMenu->Popup(Point.x, Point.y);
  9272. }
  9273. }
  9274. Handled = true;
  9275. }
  9276. //---------------------------------------------------------------------------
  9277. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9278. {
  9279. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9280. }
  9281. //---------------------------------------------------------------------------
  9282. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9283. {
  9284. Configuration->Usage->Inc(L"PasswordChanges");
  9285. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9286. ChangePasswordData->DisableAuthentationsExceptPassword();
  9287. ChangePasswordData->ChangePassword = true;
  9288. std::unique_ptr<TTerminal> ChangePasswordSession(
  9289. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9290. ChangePasswordSession->AutoReadDirectory = false;
  9291. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9292. {
  9293. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9294. }
  9295. else
  9296. {
  9297. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9298. }
  9299. }
  9300. //---------------------------------------------------------------------------
  9301. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9302. {
  9303. // No nice way to assert SSH2
  9304. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9305. }
  9306. //---------------------------------------------------------------------------
  9307. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9308. {
  9309. TOperationVisualizer OperationVisualizer;
  9310. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9311. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9312. }
  9313. //---------------------------------------------------------------------------
  9314. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9315. {
  9316. TForm::ChangeScale(M, D);
  9317. int APixelsPerInch = GetControlPixelsPerInch(this);
  9318. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9319. try
  9320. {
  9321. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9322. }
  9323. __finally
  9324. {
  9325. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9326. }
  9327. }
  9328. //---------------------------------------------------------------------------
  9329. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9330. {
  9331. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9332. }
  9333. //---------------------------------------------------------------------
  9334. class TPublicControl : public TControl
  9335. {
  9336. friend class TCustomScpExplorerForm;
  9337. };
  9338. //---------------------------------------------------------------------------
  9339. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9340. {
  9341. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9342. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9343. }
  9344. //---------------------------------------------------------------------------
  9345. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9346. {
  9347. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9348. OpenBookmark(Side, Bookmark);
  9349. }
  9350. //---------------------------------------------------------------------------
  9351. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9352. {
  9353. DoBookmarkClick(osLocal, Sender);
  9354. }
  9355. //---------------------------------------------------------------------------
  9356. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9357. {
  9358. DoBookmarkClick(osRemote, Sender);
  9359. }
  9360. //---------------------------------------------------------------------------
  9361. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9362. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9363. {
  9364. if (BookmarkList != NULL)
  9365. {
  9366. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9367. if (!WinConfiguration->UseLocationProfiles)
  9368. {
  9369. std::unique_ptr<TStringList> Directories(new TStringList());
  9370. Directories->CaseSensitive = (Side == osRemote);
  9371. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9372. {
  9373. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9374. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9375. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9376. {
  9377. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9378. Item->Caption = EscapeHotkey(Directory);
  9379. Item->ShortCut = Bookmark->ShortCut;
  9380. Item->OnClick = OnBookmarkClick;
  9381. Item->Tag = reinterpret_cast<int>(Bookmark);
  9382. Directories->Add(Directory);
  9383. Menu->Add(Item.release());
  9384. }
  9385. }
  9386. }
  9387. else
  9388. {
  9389. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9390. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9391. {
  9392. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9393. if (!Bookmark->Node.IsEmpty())
  9394. {
  9395. Folders->Add(Bookmark->Node);
  9396. }
  9397. }
  9398. for (int Index = 0; Index < Folders->Count; Index++)
  9399. {
  9400. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9401. Item->Caption = Folders->Strings[Index];
  9402. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9403. Folders->Objects[Index] = Item.get();
  9404. Menu->Add(Item.release());
  9405. }
  9406. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9407. {
  9408. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9409. TTBCustomItem * Parent;
  9410. if (!Bookmark->Node.IsEmpty())
  9411. {
  9412. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9413. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9414. }
  9415. else
  9416. {
  9417. Parent = Menu;
  9418. }
  9419. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9420. Item->Caption = Bookmark->Name;
  9421. Item->ShortCut = Bookmark->ShortCut;
  9422. Item->OnClick = OnBookmarkClick;
  9423. Item->Tag = reinterpret_cast<int>(Bookmark);
  9424. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9425. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9426. {
  9427. // No hint for location profiles that are actually mere "bookmarks"
  9428. }
  9429. else
  9430. {
  9431. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9432. UnicodeString LongHint;
  9433. if (!Bookmark->Local.IsEmpty())
  9434. {
  9435. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9436. }
  9437. if (!Bookmark->Remote.IsEmpty())
  9438. {
  9439. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9440. }
  9441. AddToList(Hint, LongHint, L"|");
  9442. Item->Hint = Hint;
  9443. }
  9444. Parent->Add(Item.release());
  9445. }
  9446. }
  9447. AddMenuSeparator(Menu);
  9448. }
  9449. }
  9450. //---------------------------------------------------------------------------
  9451. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9452. {
  9453. Menu->Clear();
  9454. std::unique_ptr<TTBCustomItem> Item;
  9455. Item.reset(new TTBXItem(Owner));
  9456. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9457. Item->Options = Item->Options << tboDefault;
  9458. Menu->Add(Item.release());
  9459. AddMenuSeparator(Menu);
  9460. if (Terminal != NULL)
  9461. {
  9462. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9463. }
  9464. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9465. Item.reset(new TTBXItem(Owner));
  9466. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9467. Menu->Add(Item.release());
  9468. }
  9469. //---------------------------------------------------------------------------
  9470. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9471. {
  9472. bool Result = true;
  9473. try
  9474. {
  9475. if (Side == osRemote)
  9476. {
  9477. Terminal->ExceptionOnFail = true;
  9478. }
  9479. try
  9480. {
  9481. DirView(Side)->Path = Path;
  9482. }
  9483. __finally
  9484. {
  9485. if (Side == osRemote)
  9486. {
  9487. Terminal->ExceptionOnFail = false;
  9488. }
  9489. }
  9490. }
  9491. catch (Exception & E)
  9492. {
  9493. if ((Side != osRemote) || Terminal->Active)
  9494. {
  9495. ShowExtendedExceptionEx(Terminal, &E);
  9496. Result = false;
  9497. }
  9498. else
  9499. {
  9500. throw;
  9501. }
  9502. }
  9503. return Result;
  9504. }
  9505. //---------------------------------------------------------------------------
  9506. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9507. {
  9508. // Make sure there some feedback even when loading tine local folders
  9509. TInstantOperationVisualizer Visualizer;
  9510. DirView(Side)->ReloadDirectory();
  9511. }
  9512. //---------------------------------------------------------------------------
  9513. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9514. {
  9515. if (Index == SessionsPageControl->TabIndex)
  9516. {
  9517. CloseSession();
  9518. }
  9519. else
  9520. {
  9521. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9522. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9523. TTerminalManager * Manager = TTerminalManager::Instance();
  9524. if (!Terminal->Active)
  9525. {
  9526. Manager->FreeTerminal(Terminal);
  9527. }
  9528. else
  9529. {
  9530. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9531. if (CanCloseQueue(Queue))
  9532. {
  9533. Manager->FreeTerminal(Terminal);
  9534. }
  9535. }
  9536. }
  9537. }
  9538. //---------------------------------------------------------------------------
  9539. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9540. {
  9541. CloseSessionTab(Index);
  9542. }
  9543. //---------------------------------------------------------------------------
  9544. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9545. {
  9546. if (DebugAlwaysTrue(GetSide(Side) == osRemote))
  9547. {
  9548. TInstantOperationVisualizer Visualizer;
  9549. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9550. // before we create a new data object
  9551. ClipboardClear();
  9552. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9553. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9554. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9555. // Intermediate variable to WORKAROUND a crash in compiler
  9556. TObjectList * ClipboardFakeMonitors =
  9557. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9558. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9559. if (FClipboardDragDropFilesEx.get() == NULL)
  9560. {
  9561. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9562. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9563. }
  9564. FClipboardDragDropFilesEx->FileList->Clear();
  9565. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9566. FClipboardDragDropFilesEx->CopyToClipboard();
  9567. FClipboardTerminal = FTerminal;
  9568. // Need full paths, as cwd can be different once files are pasted
  9569. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9570. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9571. {
  9572. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9573. }
  9574. }
  9575. }
  9576. //---------------------------------------------------------------------------
  9577. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9578. {
  9579. if (OpenClipboard(0))
  9580. {
  9581. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9582. CloseClipboard();
  9583. }
  9584. }
  9585. //---------------------------------------------------------------------------
  9586. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9587. {
  9588. return !FClipboardFakeDirectory.IsEmpty();
  9589. }
  9590. //---------------------------------------------------------------------------
  9591. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9592. {
  9593. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9594. FClipboardFakeDirectory = UnicodeString();
  9595. FClipboardTerminal = NULL;
  9596. // Also called in Idle()
  9597. FClipboardFakeMonitors.reset(NULL);
  9598. }
  9599. //---------------------------------------------------------------------------
  9600. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9601. {
  9602. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9603. {
  9604. ClipboardStop();
  9605. }
  9606. }
  9607. //---------------------------------------------------------------------------
  9608. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9609. {
  9610. TTerminalManager * Manager = TTerminalManager::Instance();
  9611. Manager->ActiveTerminal = FClipboardTerminal;
  9612. TTransferOperationParam Params;
  9613. Params.TargetDirectory = TargetDirectory;
  9614. Params.DragDrop = DragDrop;
  9615. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9616. }
  9617. //---------------------------------------------------------------------------
  9618. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9619. {
  9620. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9621. // (as it pumps a Windows message queue while beign shutted down)
  9622. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9623. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9624. {
  9625. // Can fail as it can be e.g. locked by AV, so we retry that later
  9626. bool Removed = RemoveDir(ApiPath(FileName));
  9627. try
  9628. {
  9629. if (!NonVisualDataModule->Busy)
  9630. {
  9631. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9632. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9633. }
  9634. }
  9635. __finally
  9636. {
  9637. if (!Removed)
  9638. {
  9639. RemoveDir(ApiPath(FileName));
  9640. }
  9641. }
  9642. }
  9643. }
  9644. //---------------------------------------------------------------------------
  9645. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9646. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9647. {
  9648. TFileMasks::TParams MaskParams;
  9649. MaskParams.Size = Size;
  9650. MaskParams.Modification = Modification;
  9651. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9652. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9653. {
  9654. bool ImplicitMatch;
  9655. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9656. !ImplicitMatch)
  9657. {
  9658. Color = Iter->Color;
  9659. break;
  9660. }
  9661. }
  9662. }
  9663. //---------------------------------------------------------------------------
  9664. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9665. {
  9666. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9667. if (!DirView->IsEditing())
  9668. {
  9669. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9670. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9671. // while searching
  9672. if (Key == VK_BACK)
  9673. {
  9674. if (!FIncrementalSearch.IsEmpty())
  9675. {
  9676. if (FIncrementalSearch.Length() == 1)
  9677. {
  9678. ResetIncrementalSearch();
  9679. }
  9680. else
  9681. {
  9682. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9683. IncrementalSearch(NewText, false, false);
  9684. }
  9685. Key = 0;
  9686. }
  9687. }
  9688. }
  9689. }
  9690. //---------------------------------------------------------------------------
  9691. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9692. {
  9693. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9694. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9695. {
  9696. // Filter control sequences.
  9697. // When not searching yet, prefer use of the space for toggling file selection
  9698. // (so we cannot incrementally search for a string starting with a space).
  9699. if ((Key > VK_SPACE) ||
  9700. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9701. {
  9702. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9703. Key = 0;
  9704. }
  9705. }
  9706. }
  9707. //---------------------------------------------------------------------------
  9708. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9709. {
  9710. if (!FIncrementalSearch.IsEmpty())
  9711. {
  9712. FIncrementalSearch = L"";
  9713. DirView(osCurrent)->UpdateStatusBar();
  9714. }
  9715. }
  9716. //---------------------------------------------------------------------------
  9717. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9718. {
  9719. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9720. if (Item == NULL)
  9721. {
  9722. MessageBeep(MB_ICONHAND);
  9723. }
  9724. else
  9725. {
  9726. TCustomDirView * ADirView = DirView(osCurrent);
  9727. {
  9728. TAutoNestingCounter Guard(FIncrementalSearching);
  9729. ADirView->FocusItem(Item);
  9730. if (ADirView->NortonLike == nlOff)
  9731. {
  9732. if ((ADirView->Selected != Item) ||
  9733. (ADirView->SelCount > 1))
  9734. {
  9735. ADirView->ClearSelection();
  9736. ADirView->Selected = Item;
  9737. }
  9738. }
  9739. }
  9740. FIncrementalSearch = Text;
  9741. Item->MakeVisible(false);
  9742. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9743. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9744. ADirView->UpdateStatusBar();
  9745. }
  9746. }
  9747. //---------------------------------------------------------------------------
  9748. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9749. {
  9750. int Index = Item->Index;
  9751. if (!Reverse)
  9752. {
  9753. Index++;
  9754. if (Index >= Item->Owner->Count)
  9755. {
  9756. Index = 0;
  9757. }
  9758. }
  9759. else
  9760. {
  9761. Index--;
  9762. if (Index < 0)
  9763. {
  9764. Index = Item->Owner->Count - 1;
  9765. }
  9766. }
  9767. return Item->Owner->Item[Index];
  9768. }
  9769. //---------------------------------------------------------------------------
  9770. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9771. {
  9772. TCustomDirView * ADirView = DirView(osCurrent);
  9773. if (ADirView->Items->Count == 0)
  9774. {
  9775. return NULL;
  9776. }
  9777. else
  9778. {
  9779. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9780. TListItem * Item = CurrentItem;
  9781. if (SkipCurrent)
  9782. {
  9783. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9784. }
  9785. while (true)
  9786. {
  9787. bool Matches = false;
  9788. switch (WinConfiguration->PanelSearch)
  9789. {
  9790. case isNameStartOnly:
  9791. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9792. break;
  9793. case isName:
  9794. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9795. break;
  9796. case isAll:
  9797. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9798. int Index = 0;
  9799. while ((Index < ColCount) && !Matches)
  9800. {
  9801. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9802. if (Visible)
  9803. {
  9804. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9805. }
  9806. Index++;
  9807. }
  9808. break;
  9809. }
  9810. if (Matches)
  9811. {
  9812. return Item;
  9813. }
  9814. Item = GetNextFile(Item, Reverse);
  9815. if (Item == CurrentItem)
  9816. {
  9817. return NULL;
  9818. }
  9819. }
  9820. }
  9821. }
  9822. //---------------------------------------------------------------------------
  9823. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9824. {
  9825. ResetIncrementalSearch();
  9826. }
  9827. //---------------------------------------------------------------------------
  9828. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9829. {
  9830. if (Message.CharCode == VK_TAB)
  9831. {
  9832. if (!FIncrementalSearch.IsEmpty())
  9833. {
  9834. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  9835. bool Reverse = Shift.Contains(ssShift);
  9836. IncrementalSearch(FIncrementalSearch, true, Reverse);
  9837. Message.Result = 1;
  9838. return;
  9839. }
  9840. }
  9841. else if (Message.CharCode == VK_ESCAPE)
  9842. {
  9843. if (!FIncrementalSearch.IsEmpty())
  9844. {
  9845. ResetIncrementalSearch();
  9846. Message.Result = 1;
  9847. return;
  9848. }
  9849. }
  9850. TForm::Dispatch(&Message);
  9851. }
  9852. //---------------------------------------------------------------------------
  9853. void __fastcall TCustomScpExplorerForm::Deactivate()
  9854. {
  9855. TForm::Deactivate();
  9856. // E.g. when switching to an internal editor window
  9857. ResetIncrementalSearch();
  9858. }
  9859. //---------------------------------------------------------------------------
  9860. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  9861. {
  9862. // When switching to another application
  9863. ResetIncrementalSearch();
  9864. }
  9865. //---------------------------------------------------------------------------
  9866. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  9867. {
  9868. ResetIncrementalSearch();
  9869. }
  9870. //---------------------------------------------------------------------------
  9871. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  9872. {
  9873. if (FIncrementalSearching <= 0)
  9874. {
  9875. ResetIncrementalSearch();
  9876. }
  9877. }
  9878. //---------------------------------------------------------------------------
  9879. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  9880. {
  9881. CountClicksForWindowPrint(this);
  9882. }
  9883. //---------------------------------------------------------------------------