CustomScpExplorer.cpp 366 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. //---------------------------------------------------------------------------
  35. #pragma package(smart_init)
  36. #pragma link "CustomDirView"
  37. #pragma link "CustomUnixDirView"
  38. #pragma link "IEListView"
  39. #pragma link "NortonLikeListView"
  40. #pragma link "UnixDirView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "UnixDriveView"
  43. #pragma link "CustomDriveView"
  44. #pragma link "TB2Dock"
  45. #pragma link "TBXStatusBars"
  46. #pragma link "TB2Item"
  47. #pragma link "TB2Toolbar"
  48. #pragma link "ThemePageControl"
  49. #pragma link "PathLabel"
  50. #pragma resource "*.dfm"
  51. //---------------------------------------------------------------------------
  52. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  53. static const int SessionPanelCount = 4;
  54. //---------------------------------------------------------------------------
  55. class TMutexGuard
  56. {
  57. public:
  58. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  59. int Timeout = 5000)
  60. {
  61. FMutex = NULL;
  62. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  63. if (WaitResult == WAIT_TIMEOUT)
  64. {
  65. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  66. }
  67. else
  68. {
  69. FMutex = AMutex;
  70. }
  71. }
  72. ~TMutexGuard()
  73. {
  74. if (FMutex != NULL)
  75. {
  76. ReleaseMutex(FMutex);
  77. }
  78. }
  79. private:
  80. HANDLE FMutex;
  81. };
  82. //---------------------------------------------------------------------------
  83. class TWindowLock
  84. {
  85. public:
  86. TWindowLock(TCustomScpExplorerForm * Form) :
  87. FForm(Form)
  88. {
  89. FForm->LockWindow();
  90. }
  91. ~TWindowLock()
  92. {
  93. FForm->UnlockWindow();
  94. }
  95. private:
  96. TCustomScpExplorerForm * FForm;
  97. };
  98. //---------------------------------------------------------------------------
  99. class TAutoBatch
  100. {
  101. public:
  102. TAutoBatch(TCustomScpExplorerForm * Form) :
  103. FForm(Form)
  104. {
  105. FForm->BatchStart(FBatchStorage);
  106. }
  107. ~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. private:
  112. TCustomScpExplorerForm * FForm;
  113. void * FBatchStorage;
  114. };
  115. //---------------------------------------------------------------------------
  116. struct TTransferOperationParam
  117. {
  118. TTransferOperationParam();
  119. UnicodeString TargetDirectory;
  120. bool Temp;
  121. bool DragDrop;
  122. int Options;
  123. TAutoSwitch Queue;
  124. };
  125. //---------------------------------------------------------------------------
  126. TTransferOperationParam::TTransferOperationParam()
  127. {
  128. Temp = false;
  129. DragDrop = false;
  130. Options = 0;
  131. Queue = asAuto;
  132. }
  133. //---------------------------------------------------------------------------
  134. class TTransferPresetNoteData : public TObject
  135. {
  136. public:
  137. UnicodeString Message;
  138. };
  139. //---------------------------------------------------------------------------
  140. class TTerminalNoteData : public TObject
  141. {
  142. public:
  143. TManagedTerminal * Terminal;
  144. };
  145. //---------------------------------------------------------------------------
  146. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  147. FFormRestored(false),
  148. TForm(Owner)
  149. {
  150. FInvalid = false;
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. InitControls();
  155. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  156. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  157. NonVisualDataModule->ScpExplorer = this;
  158. RestoreParams();
  159. ConfigurationChanged();
  160. RemoteDirView->Invalidate();
  161. FAutoOperation = false;
  162. FOnFileOperationFinished = NULL;
  163. FForceExecution = false;
  164. FIgnoreNextDialogChar = 0;
  165. FErrorList = NULL;
  166. FSynchronizeProgressForm = NULL;
  167. FProgressForm = NULL;
  168. FRefreshLocalDirectory = false;
  169. FRefreshRemoteDirectory = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. StandaloneOperation = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FOnSynchronizeAbort = NULL;
  199. FSynchronizeTerminal = NULL;
  200. FNeedSession = false;
  201. FDoNotIdleCurrentTerminal = 0;
  202. FIncrementalSearching = 0;
  203. FIncrementalSearchHaveNext = false;
  204. FQueueFileList.reset(new TQueueFileList());
  205. FDownloadingFromClipboard = false;
  206. FClipboardFakeMonitorsPendingReset = false;
  207. FEditorManager = new TEditorManager();
  208. FEditorManager->OnFileChange = ExecutedFileChanged;
  209. FEditorManager->OnFileReload = ExecutedFileReload;
  210. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  211. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  212. FLocalEditors = new TList();
  213. FQueueStatus = NULL;
  214. FQueueStatusSection = new TCriticalSection();
  215. FQueueStatusInvalidated = false;
  216. FQueueItemInvalidated = false;
  217. FQueueStatusUpdating = false;
  218. FQueueActedItem = NULL;
  219. FQueueController = new TQueueController(QueueView3);
  220. FUserActionTimer = new TTimer(this);
  221. FUserActionTimer->Enabled = false;
  222. FUserActionTimer->Interval = 10;
  223. FUserActionTimer->OnTimer = UserActionTimer;
  224. FNotes = new TStringList();
  225. FNoteTimer = new TTimer(this);
  226. FNoteTimer->Enabled = false;
  227. FNoteTimer->OnTimer = NoteTimer;
  228. FOnNoteClick = NULL;
  229. FOle32Library = LoadLibrary(L"Ole32.dll");
  230. FDragMoveCursor = FOle32Library != NULL ?
  231. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  232. UseSystemSettings(this);
  233. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  234. static_cast<TObject*>(GetComponent(fcTransferList)));
  235. DebugAssert(TransferList != NULL);
  236. FTransferListHoverIndex = -1;
  237. TransferList->OnChange = TransferListChange;
  238. TransferList->OnDrawItem = TransferListDrawItem;
  239. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  240. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  241. UseDesktopFont(SessionsPageControl);
  242. UpdateSessionsPageControlHeight();
  243. UseDesktopFont(RemoteDirView);
  244. UseDesktopFont(RemoteDriveView);
  245. UseDesktopFont(QueueView3);
  246. UseDesktopFont(QueueFileList);
  247. UseDesktopFont(QueueLabel);
  248. UseDesktopFont(RemoteStatusBar);
  249. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  250. QueueSplitter->ShowHint = true;
  251. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  252. QueueFileListSplitter->ShowHint = true;
  253. RemotePanelSplitter->ShowHint = true;
  254. UpdateImages();
  255. FCustomCommandMenu = CreateTBXPopupMenu(this);
  256. FCustomCommandLocalFileList = NULL;
  257. FCustomCommandRemoteFileList = NULL;
  258. FSessionColors = new TPngImageList(this);
  259. FSessionColors->ColorDepth = cd32Bit;
  260. AddFixedSessionImages();
  261. SessionsPageControl->Images = FSessionColors;
  262. UpdateQueueLabel();
  263. FRemoteDirViewWasFocused = true;
  264. CreateHiddenWindow();
  265. StartUpdates();
  266. }
  267. //---------------------------------------------------------------------------
  268. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  269. {
  270. FInvalid = true;
  271. // this has to be one of the very first things to do
  272. StopUpdateThread();
  273. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  274. TTerminalManager::Instance()->ScpExplorer = NULL;
  275. delete FCustomCommandLocalFileList;
  276. delete FCustomCommandRemoteFileList;
  277. delete FCustomCommandMenu;
  278. delete FTrayIcon;
  279. FTrayIcon = NULL;
  280. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  281. delete FEditorManager;
  282. ForceCloseLocalEditors();
  283. delete FLocalEditors;
  284. HideFileFindDialog();
  285. if (FDelayedDeletionTimer)
  286. {
  287. DoDelayedDeletion(NULL);
  288. SAFE_DESTROY(FDelayedDeletionTimer);
  289. }
  290. SAFE_DESTROY(FDelayedDeletionList);
  291. // sometimes we do not get DDEnd so the list is not released
  292. SAFE_DESTROY(FDDFileList);
  293. DebugAssert(FSynchronizeController == NULL);
  294. CloseHandle(FDDExtMutex);
  295. FDDExtMutex = NULL;
  296. FreeLibrary(FOle32Library);
  297. FOle32Library = NULL;
  298. FDragMoveCursor = NULL;
  299. DebugAssert(!FErrorList);
  300. if (FEverShown)
  301. {
  302. // when window is never shown (like when running command-line operation),
  303. // particularly window site is not restored correctly (BoundsRect value set
  304. // in RestoreForm gets lost during handle allocation), so we do not want
  305. // it to be stored
  306. StoreParams();
  307. }
  308. Terminal = NULL;
  309. Queue = NULL;
  310. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  311. NonVisualDataModule->ScpExplorer = NULL;
  312. delete FQueueController;
  313. FQueueController = NULL;
  314. delete FQueueStatusSection;
  315. FQueueStatusSection = NULL;
  316. delete FQueueStatus;
  317. FQueueStatus = NULL;
  318. delete FUserActionTimer;
  319. FUserActionTimer = NULL;
  320. delete FNoteTimer;
  321. delete FNotes;
  322. delete FNoteData;
  323. SAFE_DESTROY(FDocks);
  324. SessionsPageControl->Images = NULL;
  325. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  326. SAFE_DESTROY(FSessionColors);
  327. SAFE_DESTROY(FSessionsDragDropFilesEx);
  328. SAFE_DESTROY(FHistoryMenu[0][0]);
  329. SAFE_DESTROY(FHistoryMenu[0][1]);
  330. SAFE_DESTROY(FHistoryMenu[1][0]);
  331. SAFE_DESTROY(FHistoryMenu[1][1]);
  332. if (FHiddenWindow != NULL)
  333. {
  334. DestroyWindow(FHiddenWindow);
  335. FHiddenWindow = NULL;
  336. }
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  340. {
  341. std::unique_ptr<TSessionData> Data;
  342. if (!Session.IsEmpty())
  343. {
  344. bool DefaultsOnly;
  345. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  346. }
  347. TTerminalManager * Manager = TTerminalManager::Instance();
  348. for (int Index = 0; Index < Manager->Count; Index++)
  349. {
  350. TTerminal * Terminal = Manager->Terminals[Index];
  351. if (Session.IsEmpty() ||
  352. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  353. {
  354. if (Path.IsEmpty())
  355. {
  356. Terminal->ClearCaches();
  357. }
  358. else
  359. {
  360. Terminal->DirectoryModified(Path, true);
  361. }
  362. }
  363. }
  364. // We should flag a pending refresh for the background terminals or busy foreground terminals
  365. if (HasActiveTerminal() &&
  366. CanCommandLineFromAnotherInstance() &&
  367. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  368. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  369. {
  370. Terminal->ReloadDirectory();
  371. }
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  375. {
  376. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  377. size_t MessageSize = sizeof(TCopyDataMessage);
  378. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  379. if (Result)
  380. {
  381. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  382. Result = (Message.Version == TCopyDataMessage::Version1);
  383. if (Result)
  384. {
  385. switch (Message.Command)
  386. {
  387. case TCopyDataMessage::CommandCanCommandLine:
  388. Result = CanCommandLineFromAnotherInstance();
  389. break;
  390. case TCopyDataMessage::CommandCommandLine:
  391. {
  392. UnicodeString CommandLine(Message.CommandLine);
  393. Result = CommandLineFromAnotherInstance(CommandLine);
  394. }
  395. break;
  396. case TCopyDataMessage::RefreshPanel:
  397. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  398. break;
  399. case TCopyDataMessage::MainWindowCheck:
  400. Result = true;
  401. break;
  402. default:
  403. Result = false;
  404. break;
  405. }
  406. }
  407. }
  408. Message.Result = Result ? 1 : 0;
  409. }
  410. //---------------------------------------------------------------------------
  411. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  412. {
  413. WNDCLASS WindowClass = {0};
  414. WindowClass.lpfnWndProc = DefWindowProc;
  415. WindowClass.hInstance = HInstance;
  416. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  417. FHiddenWindow = NULL;
  418. if (RegisterClass(&WindowClass))
  419. {
  420. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  421. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  422. }
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanConsole()
  426. {
  427. return
  428. HasActiveTerminal() &&
  429. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  430. }
  431. //---------------------------------------------------------------------------
  432. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  433. {
  434. bool Result = Visible && !NonVisualDataModule->Busy;
  435. return Result;
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  439. const UnicodeString & CommandLine)
  440. {
  441. TProgramParams Params(CommandLine);
  442. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  443. if (Result)
  444. {
  445. NonVisualDataModule->StartBusy();
  446. try
  447. {
  448. // this action is initiated from another process,
  449. // so it's likely that our window is not visible,
  450. // and user won't see what is going on
  451. Application->BringToFront();
  452. // reload sessions as we may be asked to open a session
  453. // just stored by another instance
  454. StoredSessions->Reload();
  455. UnicodeString SessionName = Params.Param[1];
  456. std::unique_ptr<TObjectList> DataList(new TObjectList());
  457. try
  458. {
  459. UnicodeString DownloadFile; // unused
  460. int Flags = GetCommandLineParseUrlFlags(&Params);
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  484. {
  485. if (FTerminal != value)
  486. {
  487. TerminalChanging();
  488. DoSetTerminal(value, false);
  489. }
  490. }
  491. //---------------------------------------------------------------------------
  492. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  493. {
  494. FTerminal = value;
  495. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  496. FAllowTransferPresetAutoSelect = false;
  497. try
  498. {
  499. TerminalChanged(Replace);
  500. }
  501. __finally
  502. {
  503. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  504. }
  505. if (Terminal != NULL)
  506. {
  507. TransferPresetAutoSelect();
  508. }
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  512. {
  513. DoSetTerminal(value, true);
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  517. {
  518. if (FTerminal != NULL)
  519. {
  520. UpdateTerminal(Terminal);
  521. }
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  525. {
  526. if (Terminal != NULL)
  527. {
  528. UpdateSessionColor((TColor)Terminal->StateData->Color);
  529. }
  530. DoTerminalListChanged();
  531. DebugAssert(!IsLocalBrowserMode());
  532. if (Replaced)
  533. {
  534. RemoteDirView->ReplaceTerminal(Terminal);
  535. }
  536. else
  537. {
  538. RemoteDirView->Terminal = Terminal;
  539. }
  540. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  541. if (Terminal != NULL)
  542. {
  543. if (Terminal->Active)
  544. {
  545. Terminal->RefreshDirectory();
  546. }
  547. if (WinConfiguration->PreservePanelState)
  548. {
  549. if (Terminal->RemoteExplorerState != NULL)
  550. {
  551. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  552. }
  553. else
  554. {
  555. DirView(osRemote)->ClearState();
  556. }
  557. }
  558. if (!Replaced && Terminal->Active)
  559. {
  560. InitStatusBar();
  561. }
  562. }
  563. UpdateTransferList();
  564. // Update panels Enable state before refreshing the labels
  565. UpdateControls();
  566. }
  567. //---------------------------------------------------------------------------
  568. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  569. {
  570. if (Queue != value)
  571. {
  572. if (FQueue != NULL)
  573. {
  574. FQueue->OnListUpdate = NULL;
  575. FQueue->OnQueueItemUpdate = NULL;
  576. }
  577. FQueue = value;
  578. if (FQueue != NULL)
  579. {
  580. DebugAssert(FQueue->OnListUpdate == NULL);
  581. FQueue->OnListUpdate = QueueListUpdate;
  582. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  583. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  584. }
  585. QueueChanged();
  586. }
  587. }
  588. //---------------------------------------------------------------------------
  589. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  590. TListItem * Item)
  591. {
  592. if (FQueueActedItem == Item)
  593. {
  594. FQueueActedItem = NULL;
  595. if ((QueueView3->PopupMenu != NULL) &&
  596. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  597. {
  598. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  599. ReleaseCapture();
  600. }
  601. }
  602. if (Item->Data == FPendingQueueActionItem)
  603. {
  604. FPendingQueueActionItem = NULL;
  605. }
  606. }
  607. //---------------------------------------------------------------------------
  608. bool TCustomScpExplorerForm::IsAnythingQueued()
  609. {
  610. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  611. }
  612. //---------------------------------------------------------------------------
  613. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  614. {
  615. {
  616. TGuard Guard(FQueueStatusSection);
  617. FQueueStatusInvalidated = false;
  618. if (FQueue != NULL)
  619. {
  620. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  621. }
  622. }
  623. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  624. {
  625. FMaxQueueLength = FQueueStatus->Count;
  626. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  627. }
  628. {
  629. TAutoFlag Flag(FQueueStatusUpdating);
  630. FQueueController->UpdateQueueStatus(FQueueStatus);
  631. }
  632. UpdateQueueFileList();
  633. SetQueueProgress();
  634. UpdateQueueView();
  635. bool IsEmpty = !IsAnythingQueued();
  636. if (IsEmpty && (Terminal != NULL))
  637. {
  638. TOnceDoneOperation OnceDoneOperation =
  639. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  640. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  641. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  642. {
  643. FQueue->Enabled = false;
  644. }
  645. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  646. {
  647. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  648. }
  649. }
  650. }
  651. //---------------------------------------------------------------------------
  652. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  653. {
  654. UnicodeString Result;
  655. if (FQueueStatus != NULL)
  656. {
  657. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  658. {
  659. TFileOperationProgressType * ProgressData =
  660. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  661. if ((ProgressData != NULL) && ProgressData->InProgress)
  662. {
  663. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  664. }
  665. }
  666. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  667. {
  668. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  669. }
  670. }
  671. return Result;
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  675. {
  676. ComponentVisible[fcQueueView] =
  677. (WinConfiguration->QueueView.Show == qvShow) ||
  678. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  679. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  680. }
  681. //---------------------------------------------------------------------------
  682. void __fastcall TCustomScpExplorerForm::QueueChanged()
  683. {
  684. if (FQueueStatus != NULL)
  685. {
  686. // UpdateFileList implementation relies on the status being recreated when session changes
  687. delete FQueueStatus;
  688. FQueueStatus = NULL;
  689. }
  690. UpdateQueueStatus(true);
  691. }
  692. //---------------------------------------------------------------------------
  693. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  694. {
  695. if (FQueue == Queue)
  696. {
  697. FQueueStatusInvalidated = true;
  698. }
  699. }
  700. //---------------------------------------------------------------------------
  701. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  702. TQueueItem * Item)
  703. {
  704. if (FQueue == Queue)
  705. {
  706. TGuard Guard(FQueueStatusSection);
  707. // this may be running in parallel with QueueChanged
  708. if (FQueueStatus != NULL)
  709. {
  710. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  711. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  712. {
  713. FRefreshLocalDirectory = (QueueItem == NULL) ||
  714. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  715. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  716. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  717. }
  718. if (QueueItem != NULL)
  719. {
  720. QueueItem->UserData = (void*)true;
  721. FQueueItemInvalidated = true;
  722. }
  723. }
  724. }
  725. }
  726. //---------------------------------------------------------------------------
  727. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  728. {
  729. // during standalone editing, we have no way to see/control queue,
  730. // so we have to always popup prompts automatically
  731. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  732. }
  733. //---------------------------------------------------------------------------
  734. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  735. {
  736. if (FQueueStatus != NULL)
  737. {
  738. bool QueueAutoPopup = IsQueueAutoPopup();
  739. bool NeedRefresh = FQueueController->NeedRefresh();
  740. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  741. FQueueItemInvalidated = false;
  742. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  743. bool Updated = false;
  744. TQueueItemProxy * QueueItem;
  745. bool WasUserAction;
  746. for (int Index = 0; Index < Limit; Index++)
  747. {
  748. bool Update = false;
  749. QueueItem = FQueueStatus->Items[Index];
  750. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  751. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  752. (FPendingQueueActionItem == NULL))
  753. {
  754. FPendingQueueActionItem = QueueItem;
  755. FUserActionTimer->Enabled = true;
  756. }
  757. if ((bool)QueueItem->UserData)
  758. {
  759. QueueItem->UserData = (void*)false;
  760. QueueItem->Update();
  761. Updated = true;
  762. Update = true;
  763. }
  764. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  765. {
  766. Update = true;
  767. }
  768. if (Update)
  769. {
  770. FQueueController->RefreshQueueItem(QueueItem);
  771. }
  772. }
  773. if (Updated)
  774. {
  775. NonVisualDataModule->UpdateNonVisibleActions();
  776. SetQueueProgress();
  777. UpdateQueueFileList();
  778. }
  779. }
  780. }
  781. //---------------------------------------------------------------------------
  782. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  783. {
  784. if (FTaskbarList != NULL)
  785. {
  786. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  787. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  788. }
  789. }
  790. //---------------------------------------------------------------------------
  791. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  792. {
  793. if (Progress >= 0)
  794. {
  795. if (FTaskbarList != NULL)
  796. {
  797. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  798. }
  799. }
  800. else
  801. {
  802. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  803. }
  804. }
  805. //---------------------------------------------------------------------------
  806. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  807. {
  808. int OverallProgress;
  809. // FProgressForm is null when this is called from SetQueueProgress
  810. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  811. {
  812. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  813. }
  814. else if (!ProgressData->IsIndeterminate())
  815. {
  816. OverallProgress = ProgressData->OverallProgress();
  817. }
  818. else
  819. {
  820. OverallProgress = -1;
  821. }
  822. SetTaskbarListProgressValue(OverallProgress);
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  826. {
  827. TTerminalManager::Instance()->QueueStatusUpdated();
  828. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  829. {
  830. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  831. {
  832. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  833. {
  834. TFileOperationProgressType * ProgressData = NULL;
  835. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  836. {
  837. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  838. }
  839. if ((ProgressData != NULL) &&
  840. ProgressData->InProgress)
  841. {
  842. SetTaskbarListProgressValue(ProgressData);
  843. }
  844. else
  845. {
  846. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  847. }
  848. }
  849. else
  850. {
  851. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  852. }
  853. }
  854. else
  855. {
  856. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  857. }
  858. }
  859. UpdateQueueLabel();
  860. }
  861. //---------------------------------------------------------------------------
  862. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  863. {
  864. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  865. if (FQueueStatus != NULL)
  866. {
  867. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  868. if (ActiveAndPendingPrimaryCount > 0)
  869. {
  870. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  871. }
  872. }
  873. QueueLabel->Caption = Caption;
  874. }
  875. //---------------------------------------------------------------------------
  876. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  877. {
  878. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  879. static_cast<TComponent*>(GetComponent(fcTransferList)));
  880. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  881. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  882. TransferList->Strings->BeginUpdate();
  883. try
  884. {
  885. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  886. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  887. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  888. if (FTransferDropDownHint.IsEmpty())
  889. {
  890. FTransferDropDownHint = TransferDropDown->Hint;
  891. }
  892. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  893. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  894. TransferDropDown->Text = StripHotkey(Name);
  895. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  896. (FTransferDropDownHint, StripHotkey(Name),
  897. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  898. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  899. // update the label, otherwise when it is updated only on the first draw
  900. // of the list, it is drawn "bold" for some reason
  901. FTransferListHoverIndex = TransferList->ItemIndex;
  902. UpdateTransferLabel();
  903. }
  904. __finally
  905. {
  906. TransferList->Strings->EndUpdate();
  907. }
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  911. {
  912. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  913. DebugAssert(Toolbar != NULL);
  914. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  915. }
  916. //---------------------------------------------------------------------------
  917. void __fastcall TCustomScpExplorerForm::UpdateActions()
  918. {
  919. TForm::UpdateActions();
  920. if (ComponentVisible[fcCustomCommandsBand])
  921. {
  922. UpdateCustomCommandsToolbar();
  923. }
  924. }
  925. //---------------------------------------------------------------------------
  926. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  927. {
  928. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  929. }
  930. //---------------------------------------------------------------------------
  931. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  932. {
  933. Color = GetBtnFaceColor();
  934. DebugAssert(Configuration && RemoteDirView);
  935. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  936. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  937. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  938. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  939. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  940. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  941. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  942. {
  943. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  944. // Without this, the panel turns gray and unused part of header turns black
  945. RemoteDirView->Perform(CM_COLORCHANGED, 0, 0);
  946. }
  947. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  948. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  949. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  950. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  951. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  952. UpdateSessionsPageControlHeight();
  953. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  954. UpdateToolbarDisplayMode();
  955. UpdateTransferList();
  956. if (Terminal != NULL)
  957. {
  958. TransferPresetAutoSelect();
  959. }
  960. UpdateQueueView();
  961. UpdateControls();
  962. // this can be called even before constuctor finishes.
  963. if (FEditorManager != NULL)
  964. {
  965. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  966. }
  967. // this can be called even before constuctor finishes.
  968. if (FLocalEditors != NULL)
  969. {
  970. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  971. {
  972. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  973. }
  974. }
  975. if (ComponentVisible[fcCustomCommandsBand])
  976. {
  977. UpdateCustomCommandsToolbar();
  978. }
  979. // show only when keeping queue items forever,
  980. // otherwise, it's enough to have it in the context menu
  981. QueueDeleteAllDoneQueueToolbarItem->Visible =
  982. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  983. if (FFileColorsCurrent != WinConfiguration->FileColors)
  984. {
  985. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  986. FFileColorsCurrent = WinConfiguration->FileColors;
  987. FileColorsChanged();
  988. }
  989. }
  990. //---------------------------------------------------------------------------
  991. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  992. {
  993. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  994. DirView->Invalidate();
  995. }
  996. //---------------------------------------------------------------------------
  997. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  998. {
  999. DoFileColorsChanged(RemoteDirView);
  1000. }
  1001. //---------------------------------------------------------------------------
  1002. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1003. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1004. void * /*Arg*/)
  1005. {
  1006. if (Token != NULL)
  1007. {
  1008. TForm * Editor = dynamic_cast<TForm*>(Token);
  1009. DebugAssert(Editor != NULL);
  1010. ReconfigureEditorForm(Editor);
  1011. }
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1015. {
  1016. WinConfiguration->DDTransferConfirmation = asOn;
  1017. }
  1018. //---------------------------------------------------------------------------
  1019. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1020. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1021. {
  1022. // noop
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1026. TTransferDirection Direction, TTransferType Type, bool Temp,
  1027. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1028. bool Confirm, bool DragDrop, int Options)
  1029. {
  1030. bool Result = true;
  1031. DebugAssert(HasActiveTerminal());
  1032. // these parameters are known in advance
  1033. int Params =
  1034. FLAGMASK(Type == ttMove, cpDelete);
  1035. bool ToTemp = (Temp && (Direction == tdToLocal));
  1036. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1037. {
  1038. bool DisableNewerOnly =
  1039. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1040. ToTemp;
  1041. Options |=
  1042. FLAGMASK(ToTemp, coTemp) |
  1043. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1044. coDoNotShowAgain;
  1045. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1046. int CopyParamAttrs = (Direction == tdToRemote ?
  1047. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1048. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1049. int OutputOptions =
  1050. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1051. cooDoNotShowAgain);
  1052. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1053. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1054. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1055. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1056. if (Result)
  1057. {
  1058. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1059. {
  1060. if (DragDrop)
  1061. {
  1062. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1063. {
  1064. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1065. NULL, 0, EnableDDTransferConfirmation, NULL);
  1066. }
  1067. WinConfiguration->DDTransferConfirmation = asOff;
  1068. }
  1069. else
  1070. {
  1071. WinConfiguration->ConfirmTransferring = false;
  1072. }
  1073. }
  1074. else
  1075. {
  1076. // User explicitly unchecked "do not show again",
  1077. // so show him the dialog the next time
  1078. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1079. {
  1080. WinConfiguration->DDTransferConfirmation = asOn;
  1081. }
  1082. }
  1083. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1084. }
  1085. }
  1086. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1087. {
  1088. Configuration->Usage->Inc(L"TransfersOnBackground");
  1089. // these parameter are known only after transfer dialog
  1090. Params |=
  1091. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1092. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1093. Result = false;
  1094. ClearTransferSourceSelection(Direction);
  1095. }
  1096. return Result;
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1100. {
  1101. if (FOnFileOperationFinished != NULL)
  1102. {
  1103. FOnFileOperationFinished(UnicodeString(), true);
  1104. }
  1105. else
  1106. {
  1107. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1108. if (HasDirView[Side])
  1109. {
  1110. DirView(Side)->SelectAll(smNone);
  1111. }
  1112. }
  1113. }
  1114. //---------------------------------------------------------------------------
  1115. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1116. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1117. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1118. int Params)
  1119. {
  1120. DebugAssert(Queue != NULL);
  1121. bool SingleFile = false;
  1122. if (FileList->Count == 1)
  1123. {
  1124. if (Direction == tdToRemote)
  1125. {
  1126. UnicodeString FileName = FileList->Strings[0];
  1127. SingleFile = FileExists(ApiPath(FileName));
  1128. }
  1129. else
  1130. {
  1131. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1132. SingleFile = !File->IsDirectory;
  1133. }
  1134. }
  1135. TQueueItem * QueueItem;
  1136. if (Direction == tdToRemote)
  1137. {
  1138. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1139. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1140. }
  1141. else
  1142. {
  1143. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1144. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1145. }
  1146. AddQueueItem(Queue, QueueItem, Terminal);
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1150. {
  1151. if (Queue->IsEmpty)
  1152. {
  1153. Terminal->QueueOperationStart = Now();
  1154. }
  1155. Queue->AddItem(QueueItem);
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1159. {
  1160. }
  1161. //---------------------------------------------------------------------------
  1162. void __fastcall TCustomScpExplorerForm::InitControls()
  1163. {
  1164. DebugAssert(FDocks != NULL);
  1165. for (int Index = 0; Index < ComponentCount; Index++)
  1166. {
  1167. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1168. if ((Dock != NULL) && (Dock->Tag == 0))
  1169. {
  1170. FDocks->Add(Dock);
  1171. }
  1172. }
  1173. CollectItemsWithTextDisplayMode(this);
  1174. }
  1175. //---------------------------------------------------------------------------
  1176. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1177. {
  1178. ConfigurationChanged();
  1179. QueuePanel->Height =
  1180. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1181. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1182. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1183. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1184. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1185. QueueFileList->Height =
  1186. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1187. if (QueueFileList->Visible)
  1188. {
  1189. AdjustQueueLayout();
  1190. }
  1191. UpdateCustomCommandsToolbar();
  1192. }
  1193. //---------------------------------------------------------------------------
  1194. void __fastcall TCustomScpExplorerForm::StoreParams()
  1195. {
  1196. int APixelsPerInch = GetControlPixelsPerInch(this);
  1197. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1198. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1199. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1200. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1201. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1202. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1203. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1204. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1205. }
  1206. //---------------------------------------------------------------------------
  1207. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1208. {
  1209. if (!FFormRestored)
  1210. {
  1211. FFormRestored = true;
  1212. RestoreFormParams();
  1213. }
  1214. TForm::CreateParams(Params);
  1215. }
  1216. //---------------------------------------------------------------------------
  1217. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1218. {
  1219. DebugAssert(FDocks != NULL);
  1220. for (int Index = 0; Index < FDocks->Count; Index++)
  1221. {
  1222. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1223. }
  1224. }
  1225. //---------------------------------------------------------------------------
  1226. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1227. {
  1228. SetDockAllowDrag(true);
  1229. while (!ButtonsStr.IsEmpty())
  1230. {
  1231. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1232. UnicodeString Name = CutToChar(S, L'=', true);
  1233. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1234. {
  1235. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1236. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1237. {
  1238. while (!S.IsEmpty())
  1239. {
  1240. UnicodeString S2 = CutToChar(S, L',', true);
  1241. UnicodeString Name = CutToChar(S2, L':', true);
  1242. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1243. {
  1244. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1245. if (SameText(GetToolbarItemName(Item), Name))
  1246. {
  1247. // forward compatibility
  1248. UnicodeString S3 = CutToChar(S2, L':', true);
  1249. bool Visible = StrToIntDef(S3, 0);
  1250. Item->Visible = Visible;
  1251. }
  1252. }
  1253. }
  1254. }
  1255. }
  1256. }
  1257. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1258. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1259. }
  1260. //---------------------------------------------------------------------------
  1261. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1262. {
  1263. UnicodeString Result;
  1264. SetDockAllowDrag(true);
  1265. Result = ::GetToolbarsLayoutStr(this);
  1266. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1267. return Result;
  1268. }
  1269. //---------------------------------------------------------------------------
  1270. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1271. {
  1272. UnicodeString Result;
  1273. if (Item->Action != NULL)
  1274. {
  1275. Result = Item->Action->Name;
  1276. Result = RemoveSuffix(Result, L"Action", true);
  1277. }
  1278. else
  1279. {
  1280. Result = Item->Name;
  1281. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1282. Result = RemoveSuffix(Result, L"Item", true);
  1283. }
  1284. return Result;
  1285. }
  1286. //---------------------------------------------------------------------------
  1287. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1288. {
  1289. UnicodeString Result;
  1290. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1291. {
  1292. UnicodeString ToolbarStr;
  1293. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1294. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1295. {
  1296. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1297. {
  1298. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1299. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1300. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1301. // and save the changes here only.
  1302. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1303. {
  1304. UnicodeString Name = GetToolbarItemName(Item);
  1305. DebugAssert(Name.Pos(L"TBX") == 0);
  1306. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1307. }
  1308. }
  1309. }
  1310. if (!ToolbarStr.IsEmpty())
  1311. {
  1312. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1313. }
  1314. }
  1315. return Result;
  1316. }
  1317. //---------------------------------------------------------------------------
  1318. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1319. {
  1320. DebugAssert(FProgressForm == NULL);
  1321. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fcSkipTransfer] : false;
  1322. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1323. FProgressForm->DeleteLocalToRecycleBin =
  1324. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1325. FProgressForm->DeleteRemoteToRecycleBin =
  1326. (Terminal != NULL) &&
  1327. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1328. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1329. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1330. LockWindow();
  1331. // Actually, do not know what hides the progress once the operation finishes
  1332. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1333. SetTaskbarListProgressState(TBPF_NORMAL);
  1334. }
  1335. //---------------------------------------------------------------------------
  1336. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1337. {
  1338. UnlockWindow();
  1339. SAFE_DESTROY(FProgressForm);
  1340. SetQueueProgress();
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1344. TFileOperationProgressType & ProgressData)
  1345. {
  1346. // Download to temporary local directory
  1347. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1348. {
  1349. bool Continue = true;
  1350. FPendingTempSpaceWarn = false;
  1351. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1352. if (!Continue)
  1353. {
  1354. Abort();
  1355. }
  1356. }
  1357. // operation is being executed and we still didn't show up progress form
  1358. if (ProgressData.InProgress && !FProgressForm)
  1359. {
  1360. CreateProgressForm(NULL);
  1361. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1362. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1363. }
  1364. // operation is finished (or terminated), so we hide progress form
  1365. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1366. {
  1367. DestroyProgressForm();
  1368. if (ProgressData.Operation == foCalculateSize)
  1369. {
  1370. // When calculating size before transfer, the abort caused by
  1371. // cancel flag set due to "MoveToQueue" below,
  1372. // is not propagated back to us in ExecuteFileOperation,
  1373. // as it is silently swallowed.
  1374. // So we explicitly (re)throw it here.
  1375. if (FMoveToQueue)
  1376. {
  1377. Abort();
  1378. }
  1379. }
  1380. else
  1381. {
  1382. if ((ProgressData.Cancel == csContinue) &&
  1383. !FAutoOperation)
  1384. {
  1385. OperationComplete(ProgressData.StartTime);
  1386. }
  1387. }
  1388. }
  1389. if (FProgressForm &&
  1390. // Can happen only during synchronization, when ending one of the operations
  1391. ProgressData.InProgress)
  1392. {
  1393. FProgressForm->SetProgressData(ProgressData);
  1394. DebugAssert(ProgressData.InProgress);
  1395. SetTaskbarListProgressValue(&ProgressData);
  1396. if (FProgressForm->Cancel > csContinue)
  1397. {
  1398. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1399. if (FProgressForm->Cancel == csCancelFile)
  1400. {
  1401. FProgressForm->ClearCancel();
  1402. }
  1403. // cancel cancels even the move
  1404. FMoveToQueue = false;
  1405. }
  1406. else if (FProgressForm->MoveToQueue)
  1407. {
  1408. FMoveToQueue = true;
  1409. ProgressData.SetCancelAtLeast(csCancel);
  1410. }
  1411. if ((FTransferResumeList != NULL) &&
  1412. ProgressData.InProgress &&
  1413. ProgressData.IsTransfer() &&
  1414. !ProgressData.FullFileName.IsEmpty())
  1415. {
  1416. if ((FTransferResumeList->Count == 0) ||
  1417. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1418. {
  1419. // note that we do not recognize directories from files here
  1420. FTransferResumeList->Add(ProgressData.FullFileName);
  1421. }
  1422. }
  1423. }
  1424. }
  1425. //---------------------------------------------------------------------------
  1426. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1427. const TDateTime & StartTime)
  1428. {
  1429. if (GUIConfiguration->BeepOnFinish &&
  1430. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1431. {
  1432. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1433. DWORD Sound;
  1434. if (!ExtractFileExt(BeepSound).IsEmpty())
  1435. {
  1436. Sound = SND_FILENAME;
  1437. }
  1438. else
  1439. {
  1440. Sound = SND_ALIAS;
  1441. }
  1442. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1443. }
  1444. }
  1445. //---------------------------------------------------------------------------
  1446. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1447. TFileOperationProgressType & ProgressData)
  1448. {
  1449. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1450. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1451. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1452. FileOperationProgress(ProgressData);
  1453. }
  1454. //---------------------------------------------------------------------------
  1455. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1456. {
  1457. UnicodeString Result;
  1458. if (FProgressForm != NULL)
  1459. {
  1460. Result = FProgressForm->ProgressStr();
  1461. }
  1462. return Result;
  1463. }
  1464. //---------------------------------------------------------------------------
  1465. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1466. bool DragDrop)
  1467. {
  1468. return (!DragDrop && (DropSourceControl == NULL)) ||
  1469. (DropSourceControl == DirView(osOther));
  1470. }
  1471. //---------------------------------------------------------------------------
  1472. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1473. TFileOperation Operation, TOperationSide Side,
  1474. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1475. TOnceDoneOperation & OnceDoneOperation)
  1476. {
  1477. if (!FAutoOperation)
  1478. {
  1479. // no selection on "/upload", form serves only as event handler
  1480. // (it is not displayed)
  1481. if (PanelOperation(Side, FDragDropOperation) &&
  1482. Visible && (Operation != foCalculateSize) &&
  1483. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1484. {
  1485. if (FOnFileOperationFinished != NULL)
  1486. {
  1487. FOnFileOperationFinished(FileName, Success);
  1488. }
  1489. else
  1490. {
  1491. TCustomDirView * DView = DirView(Side);
  1492. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1493. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1494. // this can happen when local drive is unplugged in the middle of the operation
  1495. if (Item != NULL)
  1496. {
  1497. if (Success) Item->Selected = false;
  1498. DView->MakeProgressVisible(Item);
  1499. }
  1500. }
  1501. }
  1502. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1503. {
  1504. DebugAssert(!IsLocalBrowserMode());
  1505. if (Side == osLocal)
  1506. {
  1507. Configuration->Usage->Inc(L"UploadedFiles");
  1508. }
  1509. else
  1510. {
  1511. Configuration->Usage->Inc(L"DownloadedFiles");
  1512. }
  1513. }
  1514. }
  1515. if (Success && (FSynchronizeController != NULL))
  1516. {
  1517. DebugAssert(!IsSideLocalBrowser(osRemote));
  1518. if (Operation == foCopy)
  1519. {
  1520. DebugAssert(Side == osLocal);
  1521. FSynchronizeController->LogOperation(soUpload, FileName);
  1522. }
  1523. else if (Operation == foDelete)
  1524. {
  1525. DebugAssert(Side == osRemote);
  1526. FSynchronizeController->LogOperation(soDelete, FileName);
  1527. }
  1528. }
  1529. if (FProgressForm)
  1530. {
  1531. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1532. }
  1533. }
  1534. //---------------------------------------------------------------------------
  1535. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1536. TFileOperation Operation, TOperationSide Side,
  1537. bool Temp, const UnicodeString & FileName, Boolean Success,
  1538. TOnceDoneOperation & OnceDoneOperation)
  1539. {
  1540. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1541. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1542. OnceDoneOperation);
  1543. }
  1544. //---------------------------------------------------------------------------
  1545. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1546. {
  1547. return false;
  1548. }
  1549. //---------------------------------------------------------------------------
  1550. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1551. {
  1552. return false;
  1553. }
  1554. //---------------------------------------------------------------------------
  1555. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1556. {
  1557. return NULL;
  1558. }
  1559. //---------------------------------------------------------------------------
  1560. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1561. {
  1562. DebugAssert(GetSide(Side) == osRemote);
  1563. DebugUsedParam(Side);
  1564. return RemoteDirView;
  1565. }
  1566. //---------------------------------------------------------------------------
  1567. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1568. {
  1569. DebugAssert(GetSide(Side) == osRemote);
  1570. DebugUsedParam(Side);
  1571. return RemoteDriveView;
  1572. }
  1573. //---------------------------------------------------------------------------
  1574. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1575. {
  1576. DebugAssert(GetSide(Side) == osRemote);
  1577. DebugUsedParam(Side);
  1578. return HasActiveTerminal();
  1579. }
  1580. //---------------------------------------------------------------------------
  1581. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1582. TOperationSide Side, int FilesOnly)
  1583. {
  1584. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1585. }
  1586. //---------------------------------------------------------------------------
  1587. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1588. TOperationSide Side, int FilesOnly)
  1589. {
  1590. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1591. }
  1592. //---------------------------------------------------------------------------
  1593. struct THistoryItemData
  1594. {
  1595. short int Side;
  1596. short int Index;
  1597. };
  1598. //---------------------------------------------------------------------------
  1599. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1600. {
  1601. DirView(Side)->HistoryGo(Index);
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1605. {
  1606. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1607. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1608. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1609. }
  1610. //---------------------------------------------------------------------------
  1611. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1612. bool Back)
  1613. {
  1614. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1615. {
  1616. TCustomDirView * DView = DirView(Side);
  1617. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1618. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1619. if (ICount > 10)
  1620. {
  1621. ICount = 10;
  1622. }
  1623. Menu->Items->Clear();
  1624. THistoryItemData Data;
  1625. Data.Side = Side;
  1626. for (short int i = 1; i <= ICount; i++)
  1627. {
  1628. TTBCustomItem * Item = new TTBXItem(Menu);
  1629. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1630. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1631. Item->Hint = DView->HistoryPath[Data.Index];
  1632. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1633. Item->Tag = *reinterpret_cast<int*>(&Data);
  1634. Item->OnClick = HistoryItemClick;
  1635. Menu->Items->Add(Item);
  1636. }
  1637. }
  1638. }
  1639. //---------------------------------------------------------------------------
  1640. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1641. TOperationSide Side, bool Back)
  1642. {
  1643. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1644. {
  1645. // workaround
  1646. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1647. // operator new allocates memory in Pascal code, but calls inline
  1648. // contructor in C++, leading in problems, the function does
  1649. // both in Pascal code
  1650. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1651. UpdateHistoryMenu(Side, Back);
  1652. }
  1653. return FHistoryMenu[Side == osLocal][Back];
  1654. }
  1655. //---------------------------------------------------------------------------
  1656. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1657. TCustomDirView *Sender)
  1658. {
  1659. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1660. UpdateHistoryMenu(Side, true);
  1661. UpdateHistoryMenu(Side, false);
  1662. }
  1663. //---------------------------------------------------------------------------
  1664. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1665. {
  1666. // remote custom commands can be executed only if the server supports shell commands
  1667. // or have secondary shell
  1668. return HasActiveTerminal() && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1669. }
  1670. //---------------------------------------------------------------------------
  1671. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1672. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1673. {
  1674. // -1 = hidden, 0 = disabled, 1 = enabled
  1675. int Result;
  1676. TFileCustomCommand RemoteCustomCommand;
  1677. TLocalCustomCommand LocalCustomCommand;
  1678. TFileCustomCommand * NonInteractiveCustomCommand =
  1679. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1680. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1681. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1682. if (FLAGCLEAR(Command.Params, ccLocal))
  1683. {
  1684. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1685. // custom command that does not operate with files can be executed anytime ...
  1686. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1687. {
  1688. // ... but do not show such command in remote file menu
  1689. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1690. {
  1691. Result = AllowedState;
  1692. }
  1693. else
  1694. {
  1695. Result = -1;
  1696. }
  1697. }
  1698. else
  1699. {
  1700. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1701. {
  1702. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1703. }
  1704. else
  1705. {
  1706. Result = -1;
  1707. }
  1708. }
  1709. }
  1710. else
  1711. {
  1712. // custom command that does not operate with files can be executed anytime
  1713. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1714. {
  1715. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1716. }
  1717. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1718. {
  1719. if ((ListType == ccltAll) || (ListType == ccltFile))
  1720. {
  1721. // special case is "diff"-style command that can be executed over any side,
  1722. // if we have both sides
  1723. Result =
  1724. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1725. // directly (instead of EnableSelectedOperation) to pass
  1726. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1727. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1728. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1729. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1730. }
  1731. else if (ListType == ccltBoth)
  1732. {
  1733. DebugAssert(!IsLocalBrowserMode());
  1734. Result = 1;
  1735. }
  1736. else
  1737. {
  1738. Result = -1;
  1739. }
  1740. }
  1741. else
  1742. {
  1743. if ((ListType == ccltAll) ||
  1744. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1745. {
  1746. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1747. }
  1748. else
  1749. {
  1750. Result = -1;
  1751. }
  1752. }
  1753. if ((Result > 0) &&
  1754. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1755. !HasActiveTerminal())
  1756. {
  1757. Result = 0;
  1758. }
  1759. }
  1760. return Result;
  1761. }
  1762. //---------------------------------------------------------------------------
  1763. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1764. TStrings * FileList, const TCustomCommandType & ACommand,
  1765. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1766. {
  1767. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1768. {
  1769. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1770. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1771. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1772. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1773. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1774. bool Capture =
  1775. FLAGSET(ACommand.Params, ccShowResults) ||
  1776. FLAGSET(ACommand.Params, ccCopyResults) ||
  1777. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1778. TCaptureOutputEvent OutputEvent = NULL;
  1779. DebugAssert(FCapturedLog == NULL);
  1780. if (Capture)
  1781. {
  1782. FCapturedLog = new TStringList();
  1783. OutputEvent = TerminalCaptureLog;
  1784. }
  1785. try
  1786. {
  1787. if (!RemoteCustomCommand.IsFileCommand(Command))
  1788. {
  1789. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1790. OutputEvent);
  1791. }
  1792. else
  1793. {
  1794. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1795. }
  1796. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1797. {
  1798. if (FLAGSET(ACommand.Params, ccCopyResults))
  1799. {
  1800. CopyToClipboard(FCapturedLog);
  1801. }
  1802. if (FLAGSET(ACommand.Params, ccShowResults))
  1803. {
  1804. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1805. }
  1806. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1807. {
  1808. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1809. }
  1810. }
  1811. }
  1812. __finally
  1813. {
  1814. SAFE_DESTROY(FCapturedLog);
  1815. }
  1816. }
  1817. }
  1818. //---------------------------------------------------------------------------
  1819. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1820. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1821. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1822. {
  1823. DebugAssert(!IsLocalBrowserMode());
  1824. TStrings * LocalFileList = NULL;
  1825. TStrings * RemoteFileList = NULL;
  1826. TStrings * RemoteFileListFull = NULL;
  1827. try
  1828. {
  1829. if (LocalFileCommand)
  1830. {
  1831. if (ALocalFileList == NULL)
  1832. {
  1833. DebugAssert(HasDirView[osLocal]);
  1834. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1835. // directly (instead of EnableSelectedOperation) to pass
  1836. // false to FocusedFileOnlyWhenFocused
  1837. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1838. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1839. }
  1840. else
  1841. {
  1842. LocalFileList = ALocalFileList;
  1843. }
  1844. if (FileListCommand)
  1845. {
  1846. if (LocalFileList->Count != 1)
  1847. {
  1848. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1849. }
  1850. }
  1851. else
  1852. {
  1853. if ((LocalFileList->Count != 1) &&
  1854. (FileList->Count != 1) &&
  1855. (LocalFileList->Count != FileList->Count))
  1856. {
  1857. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1858. }
  1859. }
  1860. }
  1861. UnicodeString RootTempDir;
  1862. UnicodeString TempDir;
  1863. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1864. if (!RemoteFiles)
  1865. {
  1866. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1867. }
  1868. try
  1869. {
  1870. TDateTimes RemoteFileTimes;
  1871. if (RemoteFiles)
  1872. {
  1873. RemoteFileList = FileList;
  1874. }
  1875. else
  1876. {
  1877. RemoteFileList = new TStringList();
  1878. TMakeLocalFileListParams MakeFileListParam;
  1879. MakeFileListParam.FileList = RemoteFileList;
  1880. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1881. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1882. MakeFileListParam.Recursive =
  1883. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1884. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1885. if (!MakeFileListParam.Recursive)
  1886. {
  1887. RemoteFileTimes.clear();
  1888. RemoteFileListFull = new TStringList();
  1889. MakeFileListParam.FileList = RemoteFileListFull;
  1890. MakeFileListParam.Recursive = true;
  1891. MakeFileListParam.IncludeDirs = false;
  1892. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1893. }
  1894. else
  1895. {
  1896. RemoteFileListFull = RemoteFileList;
  1897. }
  1898. }
  1899. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1900. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1901. if (!NonBlocking)
  1902. {
  1903. int Count;
  1904. if (FileListCommand)
  1905. {
  1906. Count = 1;
  1907. }
  1908. else if (LocalFileCommand)
  1909. {
  1910. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1911. }
  1912. else
  1913. {
  1914. Count = RemoteFileList->Count;
  1915. }
  1916. Progress.Start(foCustomCommand, osRemote, Count);
  1917. DebugAssert(FProgressForm != NULL);
  1918. FProgressForm->ReadOnly = true;
  1919. }
  1920. try
  1921. {
  1922. if (FileListCommand)
  1923. {
  1924. UnicodeString LocalFile;
  1925. // MakeFileList does not delimit filenames
  1926. UnicodeString FileList = MakeFileList(RemoteFileList);
  1927. if (LocalFileCommand)
  1928. {
  1929. DebugAssert(LocalFileList->Count == 1);
  1930. LocalFile = LocalFileList->Strings[0];
  1931. }
  1932. TLocalCustomCommand CustomCommand(Data,
  1933. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1934. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1935. if (NonBlocking)
  1936. {
  1937. DebugAssert(!POutput);
  1938. ExecuteShellChecked(ShellCommand);
  1939. }
  1940. else
  1941. {
  1942. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1943. }
  1944. }
  1945. else if (LocalFileCommand)
  1946. {
  1947. if (LocalFileList->Count == 1)
  1948. {
  1949. UnicodeString LocalFile = LocalFileList->Strings[0];
  1950. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1951. {
  1952. UnicodeString FileName = RemoteFileList->Strings[Index];
  1953. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1954. Progress.SetFile(FileNameForProgress);
  1955. TLocalCustomCommand CustomCommand(Data,
  1956. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1957. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1958. TOnceDoneOperation OnceDoneOperation;
  1959. // Do not unselect anything, as it won't work with two panels anyway
  1960. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1961. }
  1962. }
  1963. else if (RemoteFileList->Count == 1)
  1964. {
  1965. UnicodeString FileName = RemoteFileList->Strings[0];
  1966. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1967. {
  1968. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1969. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1970. Progress.SetFile(LocalFileNameOnly);
  1971. TLocalCustomCommand CustomCommand(
  1972. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1973. FileName, LocalFileName, L"");
  1974. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1975. TOnceDoneOperation OnceDoneOperation;
  1976. // Do not unselect anything, as it won't work with two panels anyway
  1977. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1978. }
  1979. }
  1980. else
  1981. {
  1982. if (LocalFileList->Count != RemoteFileList->Count)
  1983. {
  1984. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1985. }
  1986. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1987. {
  1988. UnicodeString FileName = RemoteFileList->Strings[Index];
  1989. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1990. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1991. Progress.SetFile(LocalFileNameOnly); // sic
  1992. TLocalCustomCommand CustomCommand(
  1993. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1994. FileName, LocalFileName, L"");
  1995. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1996. TOnceDoneOperation OnceDoneOperation;
  1997. // Do not unselect anything, as it won't work with two panels anyway
  1998. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1999. }
  2000. }
  2001. }
  2002. else
  2003. {
  2004. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2005. {
  2006. UnicodeString FileName = RemoteFileList->Strings[Index];
  2007. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2008. Progress.SetFile(FileNameForProgress);
  2009. TLocalCustomCommand CustomCommand(Data,
  2010. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2011. FileName, L"", L"");
  2012. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2013. TOnceDoneOperation OnceDoneOperation;
  2014. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2015. }
  2016. }
  2017. }
  2018. __finally
  2019. {
  2020. if (!NonBlocking)
  2021. {
  2022. Progress.Stop();
  2023. }
  2024. }
  2025. DebugAssert(!FAutoOperation);
  2026. if (!RemoteFiles)
  2027. {
  2028. TempDir = IncludeTrailingBackslash(TempDir);
  2029. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2030. {
  2031. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2032. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2033. // Skip directories, process only nested files.
  2034. // The check is redundant as FileAge fails for directories anyway.
  2035. !DirectoryExists(FileName))
  2036. {
  2037. UnicodeString RemoteDir =
  2038. UnixExtractFileDir(
  2039. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  2040. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2041. TDateTime NewTime;
  2042. if (FileAge(FileName, NewTime) &&
  2043. (NewTime != RemoteFileTimes[Index]))
  2044. {
  2045. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2046. TemporaryFileCopyParam(CopyParam);
  2047. CopyParam.FileMask = L"";
  2048. FAutoOperation = true;
  2049. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2050. TemporaryFilesList->Add(FileName);
  2051. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2052. }
  2053. }
  2054. }
  2055. }
  2056. }
  2057. __finally
  2058. {
  2059. FAutoOperation = false;
  2060. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2061. {
  2062. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2063. }
  2064. }
  2065. }
  2066. __finally
  2067. {
  2068. if (RemoteFileList != FileList)
  2069. {
  2070. delete RemoteFileList;
  2071. }
  2072. if (LocalFileList != ALocalFileList)
  2073. {
  2074. delete LocalFileList;
  2075. }
  2076. if (RemoteFileListFull != RemoteFileList)
  2077. {
  2078. delete RemoteFileListFull;
  2079. }
  2080. }
  2081. }
  2082. //---------------------------------------------------------------------------
  2083. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2084. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2085. bool FileListCommand, UnicodeString * POutput)
  2086. {
  2087. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2088. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2089. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2090. {
  2091. UnicodeString FileName = SelectedFileList->Strings[Index];
  2092. if (DirectoryExists(FileName))
  2093. {
  2094. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2095. {
  2096. LocalFileList->Add(FileName);
  2097. }
  2098. if (FLAGSET(ACommand.Params, ccRecursive))
  2099. {
  2100. TMakeLocalFileListParams MakeFileListParam;
  2101. MakeFileListParam.FileList = LocalFileList.get();
  2102. MakeFileListParam.FileTimes = NULL;
  2103. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2104. MakeFileListParam.Recursive = true;
  2105. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2106. }
  2107. }
  2108. else
  2109. {
  2110. LocalFileList->Add(FileName);
  2111. }
  2112. }
  2113. UnicodeString RemotePath;
  2114. if (Terminal != NULL)
  2115. {
  2116. RemotePath = Terminal->CurrentDirectory;
  2117. }
  2118. if (FileListCommand)
  2119. {
  2120. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2121. TLocalCustomCommand CustomCommand(
  2122. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2123. L"", L"", FileList);
  2124. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2125. }
  2126. else
  2127. {
  2128. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2129. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2130. DebugAssert(FProgressForm != NULL);
  2131. FProgressForm->ReadOnly = true;
  2132. try
  2133. {
  2134. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2135. {
  2136. UnicodeString FileName = LocalFileList->Strings[Index];
  2137. Progress.SetFile(FileName);
  2138. TLocalCustomCommand CustomCommand(
  2139. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2140. FileName, L"", L"");
  2141. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2142. TOnceDoneOperation OnceDoneOperation;
  2143. Progress.Finish(FileName, true, OnceDoneOperation);
  2144. }
  2145. }
  2146. __finally
  2147. {
  2148. Progress.Stop();
  2149. }
  2150. }
  2151. }
  2152. //---------------------------------------------------------------------------
  2153. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2154. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2155. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2156. {
  2157. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2158. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2159. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2160. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2161. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2162. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2163. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2164. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2165. std::unique_ptr<UnicodeString> POutput;
  2166. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2167. FLAGSET(ACommand.Params, ccCopyResults))
  2168. {
  2169. POutput.reset(new UnicodeString());
  2170. }
  2171. if (!LocalCustomCommand.IsFileCommand(Command))
  2172. {
  2173. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2174. }
  2175. // remote files?
  2176. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2177. {
  2178. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2179. }
  2180. // local files
  2181. else
  2182. {
  2183. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2184. }
  2185. if (POutput.get() != NULL)
  2186. {
  2187. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2188. int P = POutput->Pos(sLineBreak);
  2189. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2190. {
  2191. POutput->SetLength(P - 1);
  2192. }
  2193. // Copy even empty output to clipboard
  2194. if (FLAGSET(ACommand.Params, ccCopyResults))
  2195. {
  2196. CopyToClipboard(*POutput);
  2197. }
  2198. // But do not show an empty message box.
  2199. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2200. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2201. !POutput->IsEmpty())
  2202. {
  2203. TClipboardHandler ClipboardHandler;
  2204. ClipboardHandler.Text = *POutput;
  2205. TMessageParams Params;
  2206. TQueryButtonAlias Aliases[1];
  2207. Aliases[0].Button = qaRetry;
  2208. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2209. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2210. Params.Aliases = Aliases;
  2211. Params.AliasesCount = LENOF(Aliases);
  2212. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2213. }
  2214. }
  2215. }
  2216. //---------------------------------------------------------------------------
  2217. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2218. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2219. {
  2220. TCustomCommandData Data;
  2221. UnicodeString Site;
  2222. UnicodeString RemotePath;
  2223. if (HasActiveTerminal())
  2224. {
  2225. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2226. Data = TCustomCommandData(SessionData.get());
  2227. Site = Terminal->SessionData->SessionKey;
  2228. RemotePath = Terminal->CurrentDirectory;
  2229. }
  2230. UnicodeString HelpKeyword = ACommand.HomePage;
  2231. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2232. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2233. {
  2234. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2235. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2236. {
  2237. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2238. }
  2239. else
  2240. {
  2241. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2242. }
  2243. if (!DoCustomCommandOptionsDialog(
  2244. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2245. {
  2246. Abort();
  2247. }
  2248. }
  2249. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2250. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2251. {
  2252. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2253. }
  2254. else
  2255. {
  2256. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2257. }
  2258. }
  2259. //---------------------------------------------------------------------------
  2260. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2261. const TCustomCommandType & Command)
  2262. {
  2263. DebugAssert(FCustomCommandLocalFileList != NULL);
  2264. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2265. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2266. TStrings * LocalFileList = new TStringList();
  2267. TStrings * RemoteFileList = new TStringList();
  2268. try
  2269. {
  2270. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2271. {
  2272. LocalFileList->Clear();
  2273. LocalFileList->AddObject(
  2274. FCustomCommandLocalFileList->Strings[Index],
  2275. FCustomCommandLocalFileList->Objects[Index]);
  2276. RemoteFileList->Clear();
  2277. RemoteFileList->AddObject(
  2278. FCustomCommandRemoteFileList->Strings[Index],
  2279. FCustomCommandRemoteFileList->Objects[Index]);
  2280. CustomCommand(RemoteFileList, Command, LocalFileList);
  2281. }
  2282. }
  2283. __finally
  2284. {
  2285. delete LocalFileList;
  2286. delete RemoteFileList;
  2287. }
  2288. }
  2289. //---------------------------------------------------------------------------
  2290. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2291. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2292. {
  2293. delete FCustomCommandLocalFileList;
  2294. delete FCustomCommandRemoteFileList;
  2295. // takeover ownership,
  2296. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2297. FCustomCommandLocalFileList = LocalFileList;
  2298. FCustomCommandRemoteFileList = RemoteFileList;
  2299. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2300. if (Button != NULL)
  2301. {
  2302. FCustomCommandMenu->Items->Clear();
  2303. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2304. MenuPopup(FCustomCommandMenu, Button);
  2305. }
  2306. else
  2307. {
  2308. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2309. }
  2310. }
  2311. //---------------------------------------------------------------------------
  2312. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2313. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2314. {
  2315. DebugAssert(FCapturedLog != NULL);
  2316. if ((OutputType == cotOutput) || (OutputType == cotError))
  2317. {
  2318. FCapturedLog->Add(AddedLine);
  2319. }
  2320. }
  2321. //---------------------------------------------------------------------------
  2322. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2323. TOperationSide Side)
  2324. {
  2325. return
  2326. (Side == osOther) &&
  2327. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2328. }
  2329. //---------------------------------------------------------------------------
  2330. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2331. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2332. {
  2333. TTBItemOptions O;
  2334. O = Item->Options;
  2335. TCustomDirView * DView = DirView(Side);
  2336. if ((DView->ItemFocused != NULL) &&
  2337. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2338. // when resolving links is disabled, default action is to enter the directory,
  2339. // no matter what DoubleClickAction is configured to
  2340. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2341. // Can only Edit files, but can Open/Copy even directories
  2342. ((DoubleClickAction != dcaEdit) ||
  2343. !DView->ItemIsDirectory(DView->ItemFocused)))
  2344. {
  2345. Item->Options = O << tboDefault;
  2346. }
  2347. else
  2348. {
  2349. Item->Options = O >> tboDefault;
  2350. }
  2351. }
  2352. //---------------------------------------------------------------------------
  2353. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2354. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2355. {
  2356. TCustomDirView * DView = DirView(Side);
  2357. TListItem * Item = DView->ItemFocused;
  2358. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2359. EnableFocusedOperation[Side])
  2360. {
  2361. TPoint ClientPoint;
  2362. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2363. TPoint(0, 0) : MousePos;
  2364. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2365. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2366. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2367. }
  2368. }
  2369. //---------------------------------------------------------------------------
  2370. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2371. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2372. {
  2373. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2374. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2375. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2376. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2377. Handled = true;
  2378. }
  2379. //---------------------------------------------------------------------------
  2380. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2381. {
  2382. }
  2383. //---------------------------------------------------------------------------
  2384. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2385. {
  2386. DebugAssert(FErrorList == NULL);
  2387. if (WinConfiguration->ContinueOnError)
  2388. {
  2389. FErrorList = new TStringList();
  2390. Configuration->Usage->Inc(L"ContinuationsOnError");
  2391. }
  2392. NonVisualDataModule->StartBusy();
  2393. }
  2394. //---------------------------------------------------------------------------
  2395. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2396. {
  2397. NonVisualDataModule->EndBusy();
  2398. if (FErrorList)
  2399. {
  2400. HandleErrorList(FErrorList);
  2401. }
  2402. }
  2403. //---------------------------------------------------------------------------
  2404. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2405. const TCopyParamType & CopyParam)
  2406. {
  2407. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2408. {
  2409. Configuration->Usage->Inc(L"FileMaskUses");
  2410. }
  2411. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2412. {
  2413. Configuration->Usage->Inc(L"OperationMaskUses");
  2414. }
  2415. }
  2416. //---------------------------------------------------------------------------
  2417. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2418. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2419. {
  2420. DebugAssert(!IsLocalBrowserMode());
  2421. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2422. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2423. TTransferOperationParam DefaultParam;
  2424. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2425. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2426. switch (Param.Queue)
  2427. {
  2428. case asOn:
  2429. CopyParam.Queue = true;
  2430. break;
  2431. case asOff:
  2432. CopyParam.Queue = false;
  2433. break;
  2434. case asAuto:
  2435. default:
  2436. // keep default
  2437. break;
  2438. }
  2439. bool Result =
  2440. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2441. if (Result)
  2442. {
  2443. DebugAssert(Terminal);
  2444. bool SelectionRestored = false;
  2445. TCustomDirView * DView = NULL;
  2446. if (HasDirView[Side])
  2447. {
  2448. DView = DirView(Side);
  2449. DView->SaveSelection();
  2450. DView->SaveSelectedNames();
  2451. }
  2452. UpdateCopyParamCounters(CopyParam);
  2453. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2454. DebugAssert(FTransferResumeList == NULL);
  2455. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2456. FMoveToQueue = false;
  2457. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2458. try
  2459. {
  2460. TStrings * PermanentFileList;
  2461. std::unique_ptr<TStrings> PermanentFileListOwner;
  2462. try
  2463. {
  2464. if (Side == osLocal)
  2465. {
  2466. PermanentFileList = FileList;
  2467. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2468. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2469. if (Operation == foMove)
  2470. {
  2471. ReloadLocalDirectory();
  2472. if (DView != NULL)
  2473. {
  2474. DView->RestoreSelection();
  2475. }
  2476. SelectionRestored = true;
  2477. }
  2478. }
  2479. else
  2480. {
  2481. // Clone the file list as it may refer to current directory files,
  2482. // which get destroyed, when the source directory is reloaded after foMove operation.
  2483. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2484. // But for a hotfix, we are not going to do such a big change.
  2485. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2486. PermanentFileList = PermanentFileListOwner.get();
  2487. try
  2488. {
  2489. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2490. }
  2491. __finally
  2492. {
  2493. if (Operation == foMove)
  2494. {
  2495. if (DView != NULL)
  2496. {
  2497. DView->RestoreSelection();
  2498. }
  2499. SelectionRestored = true;
  2500. }
  2501. ReloadLocalDirectory(Param.TargetDirectory);
  2502. }
  2503. }
  2504. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2505. if (FMoveToQueue)
  2506. {
  2507. Abort();
  2508. }
  2509. }
  2510. catch (EAbort &)
  2511. {
  2512. if (FMoveToQueue)
  2513. {
  2514. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2515. DebugAssert(CopyParam.TransferSkipList == NULL);
  2516. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2517. if (TransferResumeList->Count > 0)
  2518. {
  2519. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2520. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2521. }
  2522. CopyParam.TransferSkipList = TransferResumeList.release();
  2523. // not really needed, just to keep it consistent with TransferResumeList
  2524. FTransferResumeList = NULL;
  2525. FMoveToQueue = false;
  2526. Configuration->Usage->Inc("MovesToBackground");
  2527. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2528. ClearTransferSourceSelection(Direction);
  2529. }
  2530. throw;
  2531. }
  2532. }
  2533. __finally
  2534. {
  2535. if (!SelectionRestored && (DView != NULL))
  2536. {
  2537. DView->DiscardSavedSelection();
  2538. }
  2539. FTransferResumeList = NULL;
  2540. }
  2541. }
  2542. return Result;
  2543. }
  2544. //---------------------------------------------------------------------------
  2545. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2546. TOperationSide Side, TStrings * FileList, void * Param)
  2547. {
  2548. DebugAssert(FileList->Count);
  2549. // We deliberately do not toggle alternative flag (Param), but use OR,
  2550. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2551. // shortcut of CurrentDeleteAlternativeAction
  2552. bool Alternative =
  2553. bool(Param) || UseAlternativeFunction();
  2554. bool Recycle;
  2555. if (IsSideLocalBrowser(Side))
  2556. {
  2557. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2558. }
  2559. else
  2560. {
  2561. Recycle =
  2562. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2563. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2564. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2565. }
  2566. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2567. if (!Result)
  2568. {
  2569. UnicodeString Query;
  2570. if (FileList->Count == 1)
  2571. {
  2572. if (IsSideLocalBrowser(Side))
  2573. {
  2574. Query = ExtractFileName(FileList->Strings[0]);
  2575. }
  2576. else
  2577. {
  2578. Query = UnixExtractFileName(FileList->Strings[0]);
  2579. }
  2580. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2581. }
  2582. else
  2583. {
  2584. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2585. }
  2586. TMessageParams Params(mpNeverAskAgainCheck);
  2587. Params.ImageName = L"Delete file";
  2588. unsigned int Answer =
  2589. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2590. if (Answer == qaNeverAskAgain)
  2591. {
  2592. Result = true;
  2593. if (Recycle)
  2594. {
  2595. WinConfiguration->ConfirmRecycling = false;
  2596. }
  2597. else
  2598. {
  2599. WinConfiguration->ConfirmDeleting = false;
  2600. }
  2601. }
  2602. else
  2603. {
  2604. Result = (Answer == qaOK);
  2605. }
  2606. }
  2607. if (Result)
  2608. {
  2609. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2610. }
  2611. return Result;
  2612. }
  2613. //---------------------------------------------------------------------------
  2614. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2615. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2616. {
  2617. TAutoBatch AutoBatch(this);
  2618. bool Result;
  2619. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2620. {
  2621. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2622. }
  2623. else if (Operation == foRename)
  2624. {
  2625. DebugAssert(DirView(Side)->ItemFocused);
  2626. DirView(Side)->ItemFocused->EditCaption();
  2627. Result = true;
  2628. }
  2629. else if (Operation == foDelete)
  2630. {
  2631. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2632. }
  2633. else if (Operation == foSetProperties)
  2634. {
  2635. DirView(osRemote)->SaveSelectedNames();
  2636. Result = SetProperties(Side, FileList);
  2637. }
  2638. else if (Operation == foCustomCommand)
  2639. {
  2640. DebugAssert(Param);
  2641. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2642. DirView(osRemote)->SaveSelectedNames();
  2643. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2644. CustomCommand(FileList, *Command, NULL);
  2645. Result = true;
  2646. }
  2647. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2648. {
  2649. DebugAssert(!IsSideLocalBrowser(Side));
  2650. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2651. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2652. }
  2653. else if (Operation == foLock)
  2654. {
  2655. DebugAssert(!IsSideLocalBrowser(Side));
  2656. LockFiles(FileList, true);
  2657. Result = true;
  2658. }
  2659. else if (Operation == foUnlock)
  2660. {
  2661. DebugAssert(!IsSideLocalBrowser(Side));
  2662. LockFiles(FileList, false);
  2663. Result = true;
  2664. }
  2665. else
  2666. {
  2667. DebugFail();
  2668. }
  2669. return Result;
  2670. }
  2671. //---------------------------------------------------------------------------
  2672. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2673. {
  2674. if (Side == osCurrent)
  2675. {
  2676. Side = FCurrentSide;
  2677. }
  2678. return Side;
  2679. }
  2680. //---------------------------------------------------------------------------
  2681. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2682. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2683. {
  2684. Side = GetSide(Side);
  2685. bool FullPath = IsSideLocalBrowser(Side);
  2686. TCustomDirView * ADirView = DirView(Side);
  2687. std::unique_ptr<TStringList> FileList(new TStringList());
  2688. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2689. bool ReloadProperties =
  2690. (Side == osRemote) && (Operation == foSetProperties) &&
  2691. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2692. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2693. if (Result && ReloadProperties)
  2694. {
  2695. std::unique_ptr<TStringList> FileList2(new TStringList());
  2696. FileList->CaseSensitive = true;
  2697. FileList->Sort();
  2698. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2699. {
  2700. TListItem * Item = ADirView->Items->Item[Index];
  2701. UnicodeString FileName = ADirView->ItemFileName(Item);
  2702. int Index2;
  2703. if (FileList->Find(FileName, Index2))
  2704. {
  2705. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2706. }
  2707. }
  2708. LoadFilesProperties(FileList2.get());
  2709. }
  2710. return Result;
  2711. }
  2712. //---------------------------------------------------------------------------
  2713. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2714. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2715. bool NoConfirmation, void * Param)
  2716. {
  2717. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2718. {
  2719. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2720. {
  2721. DebugAssert(!IsLocalBrowserMode());
  2722. if (GetSide(Side) == osLocal)
  2723. {
  2724. Configuration->Usage->Inc(L"UploadsCommand");
  2725. }
  2726. else
  2727. {
  2728. Configuration->Usage->Inc(L"DownloadsCommand");
  2729. }
  2730. }
  2731. }
  2732. }
  2733. //---------------------------------------------------------------------------
  2734. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2735. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2736. {
  2737. TTransferOperationParam Param;
  2738. if ((WinConfiguration->Interface != ifCommander) ||
  2739. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2740. {
  2741. Flags &= ~cocShortCutHint;
  2742. }
  2743. TCustomDirView * DView = DirView(Side);
  2744. Param.Options =
  2745. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2746. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2747. if (FLAGSET(Flags, cocQueue))
  2748. {
  2749. Param.Queue = asOn;
  2750. }
  2751. else if (FLAGSET(Flags, cocNonQueue))
  2752. {
  2753. Param.Queue = asOff;
  2754. }
  2755. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2756. }
  2757. //---------------------------------------------------------------------------
  2758. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2759. {
  2760. int Count = ErrorList->Count;
  2761. try
  2762. {
  2763. if (ErrorList->Count)
  2764. {
  2765. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2766. if (MessageDialog(Message, qtError,
  2767. qaOK | qaCancel, HELP_NONE) == qaOK)
  2768. {
  2769. unsigned int Answer;
  2770. int Index = 0;
  2771. do
  2772. {
  2773. DebugAssert(Index >= 0 && Index < Count);
  2774. TQueryButtonAlias Aliases[2];
  2775. Aliases[0].Button = qaYes;
  2776. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2777. Aliases[1].Button = qaNo;
  2778. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2779. TMessageParams Params;
  2780. Params.Aliases = Aliases;
  2781. Params.AliasesCount = LENOF(Aliases);
  2782. int No = Index + 1;
  2783. UnicodeString Message = ErrorList->Strings[Index];
  2784. UnicodeString AMainInstructions;
  2785. UnicodeString Details;
  2786. if (ExtractMainInstructions(Message, AMainInstructions))
  2787. {
  2788. Details = Message;
  2789. Message = AMainInstructions;
  2790. }
  2791. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2792. Answer = MoreMessageDialog(
  2793. Message,
  2794. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2795. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2796. qaOK, HELP_NONE, &Params);
  2797. if (Answer == qaNo)
  2798. {
  2799. Index++;
  2800. }
  2801. if (Answer == qaYes)
  2802. {
  2803. Index--;
  2804. }
  2805. }
  2806. while (Answer != qaOK);
  2807. }
  2808. }
  2809. }
  2810. __finally
  2811. {
  2812. TStrings * List = ErrorList;
  2813. ErrorList = NULL;
  2814. for (int i = 0; i < Count; i++)
  2815. {
  2816. delete List->Objects[i];
  2817. }
  2818. delete List;
  2819. }
  2820. }
  2821. //---------------------------------------------------------------------------
  2822. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2823. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2824. {
  2825. // needed for checking filemasks, as there's no directory object
  2826. // associated with the file object
  2827. File->FullFileName = FullFileName;
  2828. TFileMasks::TParams MaskParams;
  2829. MaskParams.Size = File->Size;
  2830. MaskParams.Modification = File->Modification;
  2831. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2832. }
  2833. //---------------------------------------------------------------------------
  2834. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2835. {
  2836. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2837. Side = GetSide(Side);
  2838. TCustomDirView * DView = DirView(Side);
  2839. TListItem * FocusedItem = DView->ItemFocused;
  2840. UnicodeString Name;
  2841. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2842. {
  2843. Name = DView->ItemFileName(FocusedItem);
  2844. }
  2845. else
  2846. {
  2847. Name = LoadStr(NEW_FILE);
  2848. }
  2849. UnicodeString Names = Name;
  2850. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2851. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2852. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2853. {
  2854. while (!Names.IsEmpty())
  2855. {
  2856. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2857. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2858. UnicodeString TargetFileName;
  2859. UnicodeString LocalFileName;
  2860. UnicodeString RootTempDir;
  2861. UnicodeString TempDir;
  2862. UnicodeString RemoteDirectory;
  2863. bool ExistingFile = false;
  2864. if (!IsSideLocalBrowser(Side))
  2865. {
  2866. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2867. TRemoteFile * File = NULL;
  2868. if (FTerminal->FileExists(Name, &File) &&
  2869. !File->IsDirectory)
  2870. {
  2871. try
  2872. {
  2873. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2874. ExistingFile = true;
  2875. }
  2876. __finally
  2877. {
  2878. delete File;
  2879. }
  2880. }
  2881. if (!ExistingFile)
  2882. {
  2883. RemoteDirectory = UnixExtractFilePath(Name);
  2884. TemporaryDirectoryForRemoteFiles(
  2885. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2886. TargetFileName = UnixExtractFileName(Name);
  2887. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2888. LocalFileName = TempDir +
  2889. // We probably do not want to trim the VMS version here
  2890. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2891. }
  2892. }
  2893. else
  2894. {
  2895. if (ExtractFilePath(Name).IsEmpty())
  2896. {
  2897. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2898. }
  2899. else
  2900. {
  2901. LocalFileName = ExpandFileName(Name);
  2902. }
  2903. TargetFileName = ExtractFileName(Name);
  2904. }
  2905. if (!ExistingFile)
  2906. {
  2907. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2908. if (NewFile)
  2909. {
  2910. int File = FileCreate(ApiPath(LocalFileName));
  2911. if (File < 0)
  2912. {
  2913. if (!RootTempDir.IsEmpty())
  2914. {
  2915. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2916. }
  2917. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2918. }
  2919. else
  2920. {
  2921. FileClose(File);
  2922. }
  2923. }
  2924. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2925. const TEditorData * ExternalEditor = NULL;
  2926. TFileMasks::TParams MaskParams; // size not known
  2927. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2928. false, MaskParams);
  2929. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2930. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2931. }
  2932. }
  2933. }
  2934. }
  2935. //---------------------------------------------------------------------------
  2936. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2937. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2938. {
  2939. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2940. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2941. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2942. return
  2943. ((ExecuteFileBy == efInternalEditor)) ||
  2944. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2945. }
  2946. //---------------------------------------------------------------------------
  2947. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2948. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2949. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2950. UnicodeString RemoteDirectory, bool NewFile)
  2951. {
  2952. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2953. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2954. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2955. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2956. Side = GetSide(Side);
  2957. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2958. if (!IsSideLocalBrowser(Side))
  2959. {
  2960. Data->Terminal = Terminal;
  2961. Data->Queue = Queue;
  2962. Data->SessionData = CloneCurrentSessionData();
  2963. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2964. Data->RemoteDirectory = RemoteDirectory;
  2965. Data->SessionName = Terminal->SessionData->SessionName;
  2966. Data->LocalRootDirectory = LocalRootDirectory;
  2967. Data->OriginalFileName = OriginalFileName;
  2968. Data->Command = L""; // will be changed later for external editor
  2969. }
  2970. if (ExecuteFileBy == efInternalEditor)
  2971. {
  2972. if (!IsSideLocalBrowser(Side))
  2973. {
  2974. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2975. L" - " + Terminal->SessionData->SessionName;
  2976. TForm * Editor;
  2977. try
  2978. {
  2979. TNotifyEvent OnFileChanged = NULL;
  2980. TNotifyEvent OnSaveAll = NULL;
  2981. TAnyModifiedEvent OnAnyModified = NULL;
  2982. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2983. if (Terminal->IsCapable[fcBackgroundTransfers])
  2984. {
  2985. OnFileChanged = FEditorManager->FileChanged;
  2986. OnSaveAll = SaveAllInternalEditors;
  2987. OnAnyModified = AnyInternalEditorModified;
  2988. }
  2989. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2990. FEditorManager->FileReload, FEditorManager->FileClosed,
  2991. OnSaveAll, OnAnyModified,
  2992. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2993. }
  2994. catch(...)
  2995. {
  2996. if (!LocalRootDirectory.IsEmpty())
  2997. {
  2998. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2999. }
  3000. throw;
  3001. }
  3002. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3003. }
  3004. else
  3005. {
  3006. DebugAssert(!StandaloneOperation);
  3007. TForm * Editor =
  3008. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3009. SaveAllInternalEditors, AnyInternalEditorModified,
  3010. L"", false, SessionColor, -1, NewFile);
  3011. FLocalEditors->Add(Editor);
  3012. }
  3013. }
  3014. else
  3015. {
  3016. HANDLE Process;
  3017. if (ExecuteFileBy == efExternalEditor)
  3018. {
  3019. UnicodeString Program, Params, Dir;
  3020. Data->Command = ExternalEditor->ExternalEditor;
  3021. ReformatFileNameCommand(Data->Command);
  3022. SplitCommand(Data->Command, Program, Params, Dir);
  3023. Params = ExpandFileNameCommand(Params, FileName);
  3024. Program = ExpandEnvironmentVariables(Program);
  3025. if (!ExecuteShell(Program, Params, Process))
  3026. {
  3027. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3028. }
  3029. }
  3030. else
  3031. {
  3032. DebugAssert(!IsSideLocalBrowser(Side));
  3033. if (!ExecuteShell(FileName, L"", Process))
  3034. {
  3035. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3036. }
  3037. }
  3038. if (IsSideLocalBrowser(Side) ||
  3039. ((ExecuteFileBy == efShell) &&
  3040. !WinConfiguration->Editor.SDIShellEditor) ||
  3041. ((ExecuteFileBy == efExternalEditor) &&
  3042. !ExternalEditor->SDIExternalEditor))
  3043. {
  3044. // no need for handle
  3045. if (Process != NULL)
  3046. {
  3047. DebugCheck(CloseHandle(Process));
  3048. }
  3049. Process = INVALID_HANDLE_VALUE;
  3050. }
  3051. else
  3052. {
  3053. if (Process == NULL)
  3054. {
  3055. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3056. }
  3057. }
  3058. if (!IsSideLocalBrowser(Side))
  3059. {
  3060. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3061. }
  3062. }
  3063. }
  3064. //---------------------------------------------------------------------------
  3065. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3066. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3067. void * /*Arg*/)
  3068. {
  3069. if (Token != NULL)
  3070. {
  3071. EditorFormFileSave(static_cast<TForm *>(Token));
  3072. }
  3073. }
  3074. //---------------------------------------------------------------------------
  3075. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3076. {
  3077. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3078. {
  3079. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3080. }
  3081. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3082. }
  3083. //---------------------------------------------------------------------------
  3084. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3085. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3086. void * Arg)
  3087. {
  3088. if ((Token != NULL) &&
  3089. IsEditorFormModified(static_cast<TForm *>(Token)))
  3090. {
  3091. *static_cast<bool *>(Arg) = true;
  3092. }
  3093. }
  3094. //---------------------------------------------------------------------------
  3095. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3096. TObject * /*Sender*/, bool & Modified)
  3097. {
  3098. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3099. {
  3100. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3101. {
  3102. Modified = true;
  3103. }
  3104. }
  3105. if (!Modified)
  3106. {
  3107. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3108. }
  3109. }
  3110. //---------------------------------------------------------------------------
  3111. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3112. {
  3113. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3114. }
  3115. //---------------------------------------------------------------------------
  3116. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3117. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3118. UnicodeString & Result, UnicodeString & RootDirectory)
  3119. {
  3120. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3121. {
  3122. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3123. Result = RootDirectory;
  3124. }
  3125. else
  3126. {
  3127. RootDirectory = L"";
  3128. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3129. Result = IncludeTrailingBackslash(Result);
  3130. }
  3131. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3132. {
  3133. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3134. }
  3135. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3136. {
  3137. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3138. {
  3139. RemoteDirectory.Delete(1, 1);
  3140. }
  3141. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3142. }
  3143. if (!ForceDirectories(ApiPath(Result)))
  3144. {
  3145. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3146. }
  3147. }
  3148. //---------------------------------------------------------------------------
  3149. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3150. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3151. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3152. {
  3153. DebugAssert(!IsLocalBrowserMode());
  3154. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3155. if (ForceText)
  3156. {
  3157. CopyParam.TransferMode = tmAscii;
  3158. }
  3159. TemporaryFileCopyParam(CopyParam);
  3160. if (AllFiles)
  3161. {
  3162. CopyParam.IncludeFileMask = TFileMasks();
  3163. }
  3164. if (TempDir.IsEmpty())
  3165. {
  3166. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3167. }
  3168. DebugAssert(!FAutoOperation);
  3169. FAutoOperation = AutoOperation;
  3170. Terminal->ExceptionOnFail = true;
  3171. try
  3172. {
  3173. try
  3174. {
  3175. // turn off confirmations, as for MDI editors we may possibly download
  3176. // the same file over
  3177. Terminal->CopyToLocal(
  3178. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3179. if (GetTargetNames)
  3180. {
  3181. for (int i = 0; i < FileList->Count; i++)
  3182. {
  3183. FileList->Strings[i] =
  3184. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3185. }
  3186. }
  3187. }
  3188. catch(...)
  3189. {
  3190. if (!RootTempDir.IsEmpty())
  3191. {
  3192. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3193. }
  3194. throw;
  3195. }
  3196. }
  3197. __finally
  3198. {
  3199. FAutoOperation = false;
  3200. Terminal->ExceptionOnFail = false;
  3201. }
  3202. }
  3203. //---------------------------------------------------------------------------
  3204. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3205. {
  3206. // Do not waste time checking for default editor list the next time,
  3207. // or testing if default editor is not notepad.
  3208. bool TryNextTime = false;
  3209. UnicodeString UsageState;
  3210. if (!WinConfiguration->EditorList->IsDefaultList())
  3211. {
  3212. UsageState = "H";
  3213. }
  3214. else
  3215. {
  3216. UnicodeString Executable;
  3217. UnicodeString ExecutableDescription;
  3218. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3219. {
  3220. UnicodeString Message =
  3221. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3222. unsigned int Answer =
  3223. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3224. if (Answer != qaOK)
  3225. {
  3226. UsageState = "R";
  3227. }
  3228. else
  3229. {
  3230. UsageState = "A";
  3231. TEditorData EditorData;
  3232. EditorData.Editor = edExternal;
  3233. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3234. EditorData.ExternalEditorOptionsAutodetect();
  3235. TEditorList EditorList;
  3236. EditorList = *WinConfiguration->EditorList;
  3237. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3238. WinConfiguration->EditorList = &EditorList;
  3239. }
  3240. }
  3241. }
  3242. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3243. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3244. }
  3245. //---------------------------------------------------------------------------
  3246. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3247. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3248. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3249. {
  3250. if (ExecuteFileBy == efDefaultEditor)
  3251. {
  3252. if (!WinConfiguration->OfferedEditorAutoConfig)
  3253. {
  3254. EditorAutoConfig();
  3255. }
  3256. const TEditorPreferences * Editor =
  3257. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3258. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3259. {
  3260. ExecuteFileBy = efInternalEditor;
  3261. ExternalEditor = NULL;
  3262. }
  3263. else if (Editor->Data->Editor == edOpen)
  3264. {
  3265. ExecuteFileBy = efShell;
  3266. ExternalEditor = NULL;
  3267. }
  3268. else
  3269. {
  3270. ExecuteFileBy = efExternalEditor;
  3271. ExternalEditor = Editor->Data;
  3272. }
  3273. }
  3274. }
  3275. //---------------------------------------------------------------------------
  3276. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3277. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3278. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3279. {
  3280. UnicodeString OriginalFileName;
  3281. UnicodeString LocalRootDirectory;
  3282. UnicodeString RemoteDirectory;
  3283. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3284. IsSideLocalBrowser(Side), MaskParams);
  3285. UnicodeString Counter;
  3286. UnicodeString LocalFileName;
  3287. bool Handled = false;
  3288. if (!IsSideLocalBrowser(Side))
  3289. {
  3290. // We need to trim VMS version here, so that we use name without version
  3291. // when uploading back to create a new version of the file
  3292. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3293. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3294. TObject * Token = NULL;
  3295. UnicodeString LocalDirectory;
  3296. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3297. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3298. LocalDirectory))
  3299. {
  3300. if (Token != NULL)
  3301. {
  3302. TForm * Form = dynamic_cast<TForm *>(Token);
  3303. if (Form->WindowState == wsMinimized)
  3304. {
  3305. ShowWindow(Form->Handle, SW_RESTORE);
  3306. }
  3307. else
  3308. {
  3309. Form->SetFocus();
  3310. }
  3311. Handled = true;
  3312. }
  3313. else
  3314. {
  3315. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3316. }
  3317. }
  3318. if (!Handled)
  3319. {
  3320. TStringList * FileList1 = new TStringList();
  3321. try
  3322. {
  3323. FileList1->AddObject(FullFileName, Object);
  3324. TemporarilyDownloadFiles(FileList1,
  3325. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3326. LocalRootDirectory, LocalDirectory, true, true, true);
  3327. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3328. }
  3329. __finally
  3330. {
  3331. delete FileList1;
  3332. }
  3333. switch (ExecuteFileBy)
  3334. {
  3335. case efShell:
  3336. Counter = "RemoteFilesExecuted";
  3337. break;
  3338. case efInternalEditor:
  3339. Counter = "RemoteFilesOpenedInInternalEditor";
  3340. break;
  3341. case efExternalEditor:
  3342. Counter = "RemoteFilesOpenedInExternalEditor";
  3343. break;
  3344. default:
  3345. DebugFail();
  3346. }
  3347. }
  3348. }
  3349. else
  3350. {
  3351. LocalFileName = FullFileName;
  3352. OriginalFileName = ExtractFileName(FullFileName);
  3353. switch (ExecuteFileBy)
  3354. {
  3355. case efShell:
  3356. Counter = "LocalFilesExecuted";
  3357. break;
  3358. case efInternalEditor:
  3359. Counter = "LocalFilesOpenedInInternalEditor";
  3360. break;
  3361. case efExternalEditor:
  3362. Counter = "LocalFilesOpenedInExternalEditor";
  3363. break;
  3364. default:
  3365. DebugFail();
  3366. }
  3367. }
  3368. if (!Handled)
  3369. {
  3370. Configuration->Usage->Inc(Counter);
  3371. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3372. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3373. }
  3374. }
  3375. //---------------------------------------------------------------------------
  3376. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3377. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3378. bool AllSelected, bool OnFocused)
  3379. {
  3380. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3381. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3382. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3383. Side = GetSide(Side);
  3384. TCustomDirView * DView = DirView(Side);
  3385. TStrings * FileList =
  3386. AllSelected ?
  3387. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3388. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3389. try
  3390. {
  3391. DebugAssert(AllSelected || (FileList->Count == 1));
  3392. for (int i = 0; i < FileList->Count; i++)
  3393. {
  3394. UnicodeString ListFileName = FileList->Strings[i];
  3395. UnicodeString FileNameOnly =
  3396. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3397. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3398. if (!DView->ItemIsDirectory(Item))
  3399. {
  3400. UnicodeString FullFileName;
  3401. if (!IsSideLocalBrowser(Side))
  3402. {
  3403. FullFileName = DirView(Side)->Path + ListFileName;
  3404. }
  3405. else
  3406. {
  3407. FullFileName = ListFileName;
  3408. }
  3409. TFileMasks::TParams MaskParams;
  3410. MaskParams.Size = DView->ItemFileSize(Item);
  3411. TDateTimePrecision Precision;
  3412. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3413. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3414. FileList->Objects[i], MaskParams);
  3415. }
  3416. }
  3417. }
  3418. __finally
  3419. {
  3420. delete FileList;
  3421. }
  3422. }
  3423. //---------------------------------------------------------------------------
  3424. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3425. {
  3426. CopyParam.FileNameCase = ncNoChange;
  3427. CopyParam.PreserveRights = false;
  3428. CopyParam.PreserveReadOnly = false;
  3429. CopyParam.ReplaceInvalidChars = true;
  3430. CopyParam.IncludeFileMask = TFileMasks();
  3431. CopyParam.NewerOnly = false;
  3432. CopyParam.FileMask = L"";
  3433. }
  3434. //---------------------------------------------------------------------------
  3435. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3436. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3437. {
  3438. TTerminalManager * Manager = TTerminalManager::Instance();
  3439. if (!IsActiveTerminal(Data->Terminal))
  3440. {
  3441. if (!NonVisualDataModule->Busy)
  3442. {
  3443. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3444. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3445. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3446. if (Data->Terminal == NULL)
  3447. {
  3448. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3449. if (SameSiteTerminal != NULL)
  3450. {
  3451. UnicodeString Message =
  3452. FMTLOAD(EDIT_SESSION_REATTACH,
  3453. (FileNameOnly, Data->SessionName, FileNameOnly));
  3454. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3455. {
  3456. Data->Terminal = Terminal;
  3457. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3458. Data->SessionName = Terminal->SessionData->SessionName;
  3459. // We might also overwrite session data
  3460. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3461. }
  3462. else
  3463. {
  3464. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3465. Abort();
  3466. }
  3467. }
  3468. }
  3469. // foreground session should reconnect itself
  3470. else if (Terminal != Data->Terminal)
  3471. {
  3472. UnicodeString Message =
  3473. MainInstructions(
  3474. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3475. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3476. {
  3477. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3478. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3479. }
  3480. else
  3481. {
  3482. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3483. Abort();
  3484. }
  3485. }
  3486. }
  3487. if (!IsActiveTerminal(Data->Terminal))
  3488. {
  3489. Configuration->Usage->Inc(L"EditInactiveSession");
  3490. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3491. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3492. (ExtractFileName(FileName), Data->SessionName)));
  3493. }
  3494. }
  3495. TStrings * FileList = new TStringList();
  3496. try
  3497. {
  3498. FileList->Add(FileName);
  3499. // consider using the same settings (preset) as when the file was downloaded
  3500. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3501. TemporaryFileCopyParam(CopyParam);
  3502. if (Data->ForceText)
  3503. {
  3504. CopyParam.TransferMode = tmAscii;
  3505. }
  3506. // so i do not need to worry if masking algorithm works in all cases
  3507. // ("" means "copy file name", no masking is actually done)
  3508. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3509. {
  3510. CopyParam.FileMask = L"";
  3511. }
  3512. else
  3513. {
  3514. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3515. }
  3516. DebugAssert(Data->Queue != NULL);
  3517. int Params = cpNoConfirmation | cpTemporary;
  3518. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3519. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3520. QueueItem->CompleteEvent = UploadCompleteEvent;
  3521. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3522. }
  3523. __finally
  3524. {
  3525. delete FileList;
  3526. }
  3527. }
  3528. //---------------------------------------------------------------------------
  3529. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3530. const UnicodeString FileName, const TEditedFileData * Data)
  3531. {
  3532. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3533. DebugAssert(!NonVisualDataModule->Busy);
  3534. if (!IsActiveTerminal(Data->Terminal))
  3535. {
  3536. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3537. (ExtractFileName(FileName), Data->SessionName)));
  3538. }
  3539. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3540. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3541. NonVisualDataModule->StartBusy();
  3542. try
  3543. {
  3544. std::unique_ptr<TRemoteFile> File;
  3545. UnicodeString RemoteFileName =
  3546. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3547. FTerminal->ExceptionOnFail = true;
  3548. try
  3549. {
  3550. TRemoteFile * AFile = NULL;
  3551. FTerminal->ReadFile(RemoteFileName, AFile);
  3552. File.reset(AFile);
  3553. if (!File->HaveFullFileName)
  3554. {
  3555. File->FullFileName = RemoteFileName;
  3556. }
  3557. }
  3558. __finally
  3559. {
  3560. FTerminal->ExceptionOnFail = false;
  3561. }
  3562. std::unique_ptr<TStrings> FileList(new TStringList());
  3563. FileList->AddObject(RemoteFileName, File.get());
  3564. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3565. UnicodeString TempDir = ExtractFilePath(FileName);
  3566. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3567. TempDir, true, true, true);
  3568. // sanity check, the target file name should be still the same
  3569. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3570. }
  3571. __finally
  3572. {
  3573. NonVisualDataModule->EndBusy();
  3574. // it actually may not exist anymore...
  3575. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3576. }
  3577. }
  3578. //---------------------------------------------------------------------------
  3579. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3580. const TEditedFileData * Data, bool & KeepOpen)
  3581. {
  3582. // Command is set for external editors only (not for "shell" open).
  3583. if (!Data->Command.IsEmpty())
  3584. {
  3585. bool AnyFound = false;
  3586. bool AnyMDI = false;
  3587. bool AnyNonMDI = false;
  3588. bool AnyDetect = false;
  3589. TEditorList * EditorList = new TEditorList();
  3590. try
  3591. {
  3592. *EditorList = *WinConfiguration->EditorList;
  3593. for (int i = 0; i < EditorList->Count; i++)
  3594. {
  3595. const TEditorPreferences * Editor = EditorList->Editors[i];
  3596. if ((Editor->Data->Editor == edExternal) &&
  3597. (Editor->Data->ExternalEditor == Data->Command))
  3598. {
  3599. AnyFound = true;
  3600. if (Editor->Data->SDIExternalEditor)
  3601. {
  3602. AnyNonMDI = true;
  3603. if (Editor->Data->DetectMDIExternalEditor)
  3604. {
  3605. AnyDetect = true;
  3606. }
  3607. }
  3608. else
  3609. {
  3610. AnyMDI = true;
  3611. }
  3612. }
  3613. }
  3614. bool EnableMDI = false;
  3615. bool DisableDetect = false;
  3616. if (AnyMDI)
  3617. {
  3618. KeepOpen = true;
  3619. if (AnyNonMDI)
  3620. {
  3621. // there is at least one instance of the editor with MDI support enabled,
  3622. // and one with disabled, enable it for all instances
  3623. EnableMDI = true;
  3624. }
  3625. }
  3626. else if (AnyFound && !AnyDetect)
  3627. {
  3628. // at least once instance found but all have MDI autodetection disabled
  3629. // => close the file (default action)
  3630. }
  3631. else
  3632. {
  3633. // no instance of the editor has MDI support enabled
  3634. TMessageParams Params;
  3635. if (AnyFound)
  3636. {
  3637. // there is at least one instance of the editor
  3638. Params.Params |= mpNeverAskAgainCheck;
  3639. }
  3640. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3641. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3642. switch (Answer)
  3643. {
  3644. case qaNeverAskAgain:
  3645. DisableDetect = true;
  3646. break;
  3647. case qaNo:
  3648. EnableMDI = true;
  3649. KeepOpen = true;
  3650. break;
  3651. }
  3652. }
  3653. if (AnyFound && (EnableMDI || DisableDetect))
  3654. {
  3655. bool Changed = false;
  3656. for (int i = 0; i < EditorList->Count; i++)
  3657. {
  3658. const TEditorPreferences * Editor = EditorList->Editors[i];
  3659. if ((Editor->Data->Editor == edExternal) &&
  3660. (Editor->Data->ExternalEditor == Data->Command) &&
  3661. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3662. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3663. {
  3664. Changed = true;
  3665. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3666. if (EnableMDI)
  3667. {
  3668. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3669. }
  3670. if (DisableDetect)
  3671. {
  3672. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3673. }
  3674. EditorList->Change(i, UpdatedEditor);
  3675. }
  3676. }
  3677. if (Changed)
  3678. {
  3679. WinConfiguration->EditorList = EditorList;
  3680. }
  3681. }
  3682. }
  3683. __finally
  3684. {
  3685. delete EditorList;
  3686. }
  3687. }
  3688. else
  3689. {
  3690. // "open" case
  3691. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3692. qaOK, HELP_APP_EARLY_CLOSED);
  3693. }
  3694. }
  3695. //---------------------------------------------------------------------------
  3696. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3697. {
  3698. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3699. }
  3700. //---------------------------------------------------------------------------
  3701. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3702. {
  3703. SideEnter(osRemote);
  3704. }
  3705. //---------------------------------------------------------------------------
  3706. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3707. {
  3708. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3709. SideEnter(osRemote);
  3710. }
  3711. //---------------------------------------------------------------------------
  3712. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3713. {
  3714. FCurrentSide = Side;
  3715. if (Visible)
  3716. {
  3717. UpdateControls();
  3718. }
  3719. }
  3720. //---------------------------------------------------------------------------
  3721. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3722. TStrings * FileList, bool Alternative)
  3723. {
  3724. TCustomDirView * DView = DirView(Side);
  3725. DView->SaveSelection();
  3726. DView->SaveSelectedNames();
  3727. DebugAssert(!FAlternativeDelete);
  3728. FAlternativeDelete = Alternative;
  3729. try
  3730. {
  3731. if (!IsSideLocalBrowser(Side))
  3732. {
  3733. DebugAssert(Terminal != NULL);
  3734. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3735. }
  3736. else
  3737. {
  3738. try
  3739. {
  3740. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3741. }
  3742. __finally
  3743. {
  3744. ReloadLocalDirectory();
  3745. }
  3746. }
  3747. FAlternativeDelete = false;
  3748. }
  3749. catch(...)
  3750. {
  3751. FAlternativeDelete = false;
  3752. DView->DiscardSavedSelection();
  3753. throw;
  3754. }
  3755. DView->RestoreSelection();
  3756. }
  3757. //---------------------------------------------------------------------------
  3758. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3759. {
  3760. DebugAssert(Terminal);
  3761. DebugAssert(!IsLocalBrowserMode());
  3762. RemoteDirView->SaveSelection();
  3763. RemoteDirView->SaveSelectedNames();
  3764. try
  3765. {
  3766. if (Lock)
  3767. {
  3768. Terminal->LockFiles(FileList);
  3769. }
  3770. else
  3771. {
  3772. Terminal->UnlockFiles(FileList);
  3773. }
  3774. }
  3775. catch(...)
  3776. {
  3777. RemoteDirView->DiscardSavedSelection();
  3778. throw;
  3779. }
  3780. RemoteDirView->RestoreSelection();
  3781. }
  3782. //---------------------------------------------------------------------------
  3783. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3784. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3785. bool NoConfirmation, bool Move)
  3786. {
  3787. DebugAssert(Terminal != NULL);
  3788. DebugAssert(!IsLocalBrowserMode());
  3789. // update Terminal->StateData->RemoteDirectory
  3790. UpdateTerminal(Terminal);
  3791. if (Session == NULL)
  3792. {
  3793. Session = TTerminalManager::Instance()->ActiveTerminal;
  3794. }
  3795. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3796. {
  3797. if (RemoteDriveView->DropTarget != NULL)
  3798. {
  3799. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3800. }
  3801. else if (RemoteDirView->DropTarget != NULL)
  3802. {
  3803. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3804. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3805. }
  3806. else
  3807. {
  3808. Target = RemoteDirView->Path;
  3809. }
  3810. }
  3811. else
  3812. {
  3813. Target = Session->StateData->RemoteDirectory;
  3814. }
  3815. Target = UnixIncludeTrailingBackslash(Target);
  3816. if (FileList->Count == 1)
  3817. {
  3818. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3819. }
  3820. else
  3821. {
  3822. FileMask = AnyMask;
  3823. }
  3824. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3825. bool Result = true;
  3826. if (!NoConfirmation)
  3827. {
  3828. bool Multi = (FileList->Count > 1);
  3829. if (Move)
  3830. {
  3831. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3832. }
  3833. else
  3834. {
  3835. std::unique_ptr<TStrings> Sessions(new TStringList);
  3836. std::unique_ptr<TStrings> Directories(new TStringList);
  3837. TStrings * SessionList = TTerminalManager::Instance()->TerminalList;
  3838. for (int Index = 0; Index < SessionList->Count; Index++)
  3839. {
  3840. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3841. if (IsActiveTerminal(ASession))
  3842. {
  3843. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3844. Directories->Add(ASession->StateData->RemoteDirectory);
  3845. }
  3846. }
  3847. TDirectRemoteCopy AllowDirectCopy;
  3848. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3849. {
  3850. DebugAssert(DirectCopy);
  3851. AllowDirectCopy = drcAllow;
  3852. }
  3853. else if (FTerminal->IsCapable[fcSecondaryShell])
  3854. {
  3855. DebugAssert(DirectCopy);
  3856. AllowDirectCopy = drcConfirmCommandSession;
  3857. }
  3858. else
  3859. {
  3860. DebugAssert(!DirectCopy);
  3861. AllowDirectCopy = drcDisallow;
  3862. }
  3863. void * ASession = Session;
  3864. Result = DoRemoteCopyDialog(
  3865. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3866. Session = static_cast<TManagedTerminal *>(ASession);
  3867. }
  3868. }
  3869. return Result;
  3870. }
  3871. //---------------------------------------------------------------------------
  3872. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3873. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3874. {
  3875. DebugAssert(!IsLocalBrowserMode());
  3876. bool DirectCopy;
  3877. UnicodeString Target, FileMask;
  3878. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3879. if (Result)
  3880. {
  3881. if (!Move && !DirectCopy)
  3882. {
  3883. Configuration->Usage->Inc("RemoteCopyTemp");
  3884. UnicodeString RootTempDir;
  3885. UnicodeString TempDir;
  3886. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3887. TStrings * TemporaryFilesList = new TStringList();
  3888. try
  3889. {
  3890. TMakeLocalFileListParams MakeFileListParam;
  3891. MakeFileListParam.FileList = TemporaryFilesList;
  3892. MakeFileListParam.FileTimes = NULL;
  3893. MakeFileListParam.IncludeDirs = true;
  3894. MakeFileListParam.Recursive = false;
  3895. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3896. TTerminalManager::Instance()->ActiveTerminal = Session;
  3897. if (TemporaryFilesList->Count > 0)
  3898. {
  3899. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3900. CopyParam.FileMask = FileMask;
  3901. DebugAssert(!FAutoOperation);
  3902. FAutoOperation = true;
  3903. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3904. }
  3905. }
  3906. __finally
  3907. {
  3908. delete TemporaryFilesList;
  3909. FAutoOperation = false;
  3910. if (!RootTempDir.IsEmpty())
  3911. {
  3912. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3913. }
  3914. }
  3915. }
  3916. else
  3917. {
  3918. RemoteDirView->SaveSelection();
  3919. RemoteDirView->SaveSelectedNames();
  3920. try
  3921. {
  3922. if (Move)
  3923. {
  3924. Configuration->Usage->Inc("RemoteMove");
  3925. Terminal->MoveFiles(FileList, Target, FileMask);
  3926. }
  3927. else
  3928. {
  3929. Configuration->Usage->Inc("RemoteCopyDirect");
  3930. DebugAssert(DirectCopy);
  3931. DebugAssert(Session == FTerminal);
  3932. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3933. FTerminal->CommandSessionOpened ||
  3934. CommandSessionFallback())
  3935. {
  3936. Terminal->CopyFiles(FileList, Target, FileMask);
  3937. }
  3938. }
  3939. }
  3940. catch(...)
  3941. {
  3942. RemoteDirView->DiscardSavedSelection();
  3943. throw;
  3944. }
  3945. RemoteDirView->RestoreSelection();
  3946. }
  3947. }
  3948. return Result;
  3949. }
  3950. //---------------------------------------------------------------------------
  3951. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3952. const UnicodeString & Path, TRemoteProperties & Properties)
  3953. {
  3954. DebugAssert(!IsLocalBrowserMode());
  3955. Properties.Valid = Properties.Valid << vpEncrypt;
  3956. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3957. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3958. }
  3959. //---------------------------------------------------------------------------
  3960. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3961. {
  3962. Side = GetSide(Side);
  3963. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3964. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3965. UnicodeString Name = LoadStr(NEW_FOLDER);
  3966. int AllowedChanges =
  3967. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3968. bool SaveSettings = false;
  3969. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3970. {
  3971. TWindowLock Lock(this);
  3972. if (!IsSideLocalBrowser(Side))
  3973. {
  3974. if (SaveSettings)
  3975. {
  3976. GUIConfiguration->NewDirectoryProperties = Properties;
  3977. }
  3978. CreateRemoteDirectory(Name, Properties);
  3979. }
  3980. else
  3981. {
  3982. DirView(Side)->CreateDirectory(Name);
  3983. }
  3984. }
  3985. }
  3986. //---------------------------------------------------------------------------
  3987. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3988. {
  3989. TWindowLock Lock(this);
  3990. DirView(Side)->ExecuteHomeDirectory();
  3991. }
  3992. //---------------------------------------------------------------------------
  3993. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3994. {
  3995. DoOpenDirectoryDialog(odBrowse, Side);
  3996. }
  3997. //---------------------------------------------------------------------------
  3998. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3999. {
  4000. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  4001. bool Result = !Path.IsEmpty();
  4002. if (Result)
  4003. {
  4004. // While we might get here when the session is closed (from location profiles),
  4005. // it's not a problem as the Path setter is noop then
  4006. DirView(Side)->Path = Path;
  4007. }
  4008. return Result;
  4009. }
  4010. //---------------------------------------------------------------------------
  4011. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4012. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4013. bool & Close)
  4014. {
  4015. // terminal can be already closed (e.g. dropped connection)
  4016. if (Terminal != NULL)
  4017. {
  4018. try
  4019. {
  4020. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  4021. }
  4022. catch(...)
  4023. {
  4024. if (!Terminal->Active)
  4025. {
  4026. Close = true;
  4027. }
  4028. throw;
  4029. }
  4030. }
  4031. }
  4032. //---------------------------------------------------------------------------
  4033. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4034. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4035. bool & Close)
  4036. {
  4037. // terminal can be already closed (e.g. dropped connection)
  4038. if (Terminal != NULL)
  4039. {
  4040. Configuration->Usage->Inc(L"ChecksumCalculated");
  4041. try
  4042. {
  4043. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4044. }
  4045. catch(...)
  4046. {
  4047. if (!Terminal->Active)
  4048. {
  4049. Close = true;
  4050. }
  4051. throw;
  4052. }
  4053. }
  4054. }
  4055. //---------------------------------------------------------------------------
  4056. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4057. {
  4058. if (Terminal->LoadFilesProperties(FileList))
  4059. {
  4060. RemoteDirView->Invalidate();
  4061. }
  4062. }
  4063. //---------------------------------------------------------------------------
  4064. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4065. {
  4066. bool Result;
  4067. if (!IsSideLocalBrowser(Side))
  4068. {
  4069. TRemoteTokenList * GroupList = NULL;
  4070. TRemoteTokenList * UserList = NULL;
  4071. try
  4072. {
  4073. TRemoteProperties CurrentProperties;
  4074. LoadFilesProperties(FileList);
  4075. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4076. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4077. if (CapableGroupChanging || CapableOwnerChanging)
  4078. {
  4079. if (CapableGroupChanging)
  4080. {
  4081. GroupList = Terminal->Groups->Duplicate();
  4082. }
  4083. if (CapableOwnerChanging)
  4084. {
  4085. UserList = Terminal->Users->Duplicate();
  4086. }
  4087. TRemoteDirectory * Files = Terminal->Files;
  4088. int Count = Files->Count;
  4089. if (Count > 100)
  4090. {
  4091. Count = 100;
  4092. }
  4093. for (int Index = 0; Index < Count; Index++)
  4094. {
  4095. TRemoteFile * File = Files->Files[Index];
  4096. if (CapableGroupChanging)
  4097. {
  4098. GroupList->AddUnique(File->Group);
  4099. }
  4100. if (CapableOwnerChanging)
  4101. {
  4102. UserList->AddUnique(File->Owner);
  4103. }
  4104. }
  4105. // if we haven't collected tokens for all files in current directory,
  4106. // make sure we collect them at least for all selected files.
  4107. // (note that so far the files in FileList has to be from current direcotry)
  4108. if (Count < Files->Count)
  4109. {
  4110. for (int Index = 0; Index < FileList->Count; Index++)
  4111. {
  4112. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4113. if (CapableGroupChanging)
  4114. {
  4115. GroupList->AddUnique(File->Group);
  4116. }
  4117. if (CapableOwnerChanging)
  4118. {
  4119. UserList->AddUnique(File->Owner);
  4120. }
  4121. }
  4122. }
  4123. }
  4124. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4125. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4126. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4127. {
  4128. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4129. {
  4130. CapableAclChanging = false;
  4131. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4132. }
  4133. }
  4134. int Flags = 0;
  4135. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4136. if (CapableAclChanging) Flags |= cpAcl;
  4137. if (CapableOwnerChanging) Flags |= cpOwner;
  4138. if (CapableGroupChanging) Flags |= cpGroup;
  4139. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4140. if (Terminal->IsCapable[fcCalculatingChecksum])
  4141. {
  4142. CalculateChecksumEvent = CalculateChecksum;
  4143. }
  4144. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4145. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4146. TRemoteProperties NewProperties = CurrentProperties;
  4147. Result =
  4148. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4149. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4150. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4151. CalculateSize, CalculateChecksumEvent);
  4152. if (Result)
  4153. {
  4154. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4155. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4156. }
  4157. }
  4158. __finally
  4159. {
  4160. delete GroupList;
  4161. delete UserList;
  4162. }
  4163. }
  4164. else
  4165. {
  4166. DirView(Side)->DisplayPropertiesMenu();
  4167. Result = true;
  4168. }
  4169. return Result;
  4170. }
  4171. //---------------------------------------------------------------------------
  4172. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4173. {
  4174. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4175. {
  4176. FIgnoreNextDialogChar = Key;
  4177. }
  4178. Key = 0;
  4179. }
  4180. //---------------------------------------------------------------------------
  4181. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4182. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4183. {
  4184. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4185. if (Command != NULL)
  4186. {
  4187. KeyProcessed(Key, Shift);
  4188. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4189. {
  4190. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4191. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4192. false, false, const_cast<TCustomCommandType *>(Command));
  4193. }
  4194. }
  4195. }
  4196. //---------------------------------------------------------------------------
  4197. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4198. {
  4199. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4200. {
  4201. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4202. }
  4203. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4204. {
  4205. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4206. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4207. {
  4208. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4209. if (((Action->ShortCut == KeyShortCut) ||
  4210. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4211. AllowedAction(Action, aaShortCut))
  4212. {
  4213. // Has to be called before the action as the dialog char is already in queue.
  4214. // So when the action consumes message queue, we already need to have the
  4215. // FIgnoreNextDialogChar set
  4216. KeyProcessed(Key, Shift);
  4217. // Reset reference to previous component (when menu/toolbar was clicked).
  4218. // Needed to detect that action was invoked by keyboard shortcut
  4219. // in TNonVisualDataModule::ExplorerActionsExecute
  4220. Action->ActionComponent = NULL;
  4221. Action->Execute();
  4222. return;
  4223. }
  4224. }
  4225. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4226. {
  4227. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4228. {
  4229. KeyProcessed(Key, Shift);
  4230. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4231. return;
  4232. }
  4233. }
  4234. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4235. {
  4236. KeyProcessed(Key, Shift);
  4237. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4238. }
  4239. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4240. if (IsCustomShortCut(CustomShortCut))
  4241. {
  4242. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4243. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4244. if (WinConfiguration->SharedBookmarks != NULL)
  4245. {
  4246. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4247. if ((Bookmark != NULL) &&
  4248. OpenBookmark(FCurrentSide, Bookmark))
  4249. {
  4250. KeyProcessed(Key, Shift);
  4251. }
  4252. }
  4253. }
  4254. }
  4255. TForm::KeyDown(Key, Shift);
  4256. }
  4257. //---------------------------------------------------------------------------
  4258. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4259. {
  4260. DebugAssert(Terminal != NULL);
  4261. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4262. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4263. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4264. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4265. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4266. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4267. // expanded from ?: to avoid memory leaks
  4268. if (SecurityEnabled)
  4269. {
  4270. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4271. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4272. }
  4273. else
  4274. {
  4275. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4276. }
  4277. if (FileSystemInfo.ProtocolName.IsEmpty())
  4278. {
  4279. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4280. }
  4281. else
  4282. {
  4283. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4284. }
  4285. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4286. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4287. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4288. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4289. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4290. UnicodeString CompressionStr = CSCompressionStr;
  4291. if (CSCompressionStr != SCCompressionStr)
  4292. {
  4293. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4294. }
  4295. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4296. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4297. UpdateStatusBar();
  4298. }
  4299. //---------------------------------------------------------------------------
  4300. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4301. {
  4302. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4303. DebugAssert(SessionStatusBar != NULL);
  4304. if (!HasActiveTerminal() || (Terminal->Status < ssOpened))
  4305. {
  4306. // note: (Terminal->Status < sshReady) currently never happens here,
  4307. // so STATUS_CONNECTING is never used
  4308. SessionStatusBar->SimplePanel = true;
  4309. SessionStatusBar->SimpleText = LoadStr(
  4310. !HasActiveTerminal() ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4311. }
  4312. else
  4313. {
  4314. DebugAssert(Terminal);
  4315. SessionStatusBar->SimplePanel = false;
  4316. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4317. if (!FNote.IsEmpty())
  4318. {
  4319. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4320. }
  4321. else
  4322. {
  4323. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4324. }
  4325. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4326. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4327. }
  4328. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4329. }
  4330. //---------------------------------------------------------------------------
  4331. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4332. {
  4333. Panel->Caption = L"";
  4334. }
  4335. //---------------------------------------------------------------------------
  4336. void __fastcall TCustomScpExplorerForm::Idle()
  4337. {
  4338. if (FShowing ||
  4339. // Particularly for
  4340. // - Standalone editing
  4341. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4342. // while "keeping remote directory up to date".
  4343. StandaloneOperation)
  4344. {
  4345. FEditorManager->Check();
  4346. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4347. // that needs to know if queue view is visible (and it may be closed after queue update)
  4348. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4349. }
  4350. if (FShowing)
  4351. {
  4352. if (!NonVisualDataModule->Busy &&
  4353. // Menu is opened or drag&drop is going on
  4354. (Mouse->Capture == NULL))
  4355. {
  4356. if (FRefreshRemoteDirectory)
  4357. {
  4358. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4359. {
  4360. Terminal->RefreshDirectory();
  4361. }
  4362. FRefreshRemoteDirectory = false;
  4363. }
  4364. if (FRefreshLocalDirectory)
  4365. {
  4366. ReloadLocalDirectory();
  4367. FRefreshLocalDirectory = false;
  4368. }
  4369. if (WinConfiguration->RefreshRemotePanel)
  4370. {
  4371. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4372. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4373. {
  4374. RemoteDirView->ReloadDirectory();
  4375. }
  4376. }
  4377. }
  4378. }
  4379. if (FShowing || StandaloneOperation)
  4380. {
  4381. if (FQueueStatusInvalidated)
  4382. {
  4383. UpdateQueueStatus(false);
  4384. }
  4385. RefreshQueueItems();
  4386. }
  4387. if (FShowing)
  4388. {
  4389. UpdateStatusBar();
  4390. }
  4391. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4392. {
  4393. FClipboardFakeMonitorsPendingReset = false;
  4394. FClipboardFakeMonitors.reset(NULL);
  4395. }
  4396. FIgnoreNextDialogChar = 0;
  4397. }
  4398. //---------------------------------------------------------------------------
  4399. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4400. {
  4401. try
  4402. {
  4403. FUserActionTimer->Enabled = false;
  4404. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4405. {
  4406. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4407. {
  4408. FPendingQueueActionItem->ProcessUserAction();
  4409. }
  4410. }
  4411. }
  4412. __finally
  4413. {
  4414. FPendingQueueActionItem = NULL;
  4415. }
  4416. }
  4417. //---------------------------------------------------------------------------
  4418. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4419. {
  4420. if (WinConfiguration->MinimizeToTray)
  4421. {
  4422. UpdateTrayIcon();
  4423. FTrayIcon->Visible = true;
  4424. if (Visible)
  4425. {
  4426. ShowWindow(Handle, SW_HIDE);
  4427. }
  4428. }
  4429. }
  4430. //---------------------------------------------------------------------------
  4431. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4432. {
  4433. // WORKAROUND
  4434. // When restoring maximized window from minimization,
  4435. // rarely some controls do not align properly.
  4436. // Two instances seen (both for Commander):
  4437. // - When restoring, window is temporarily narrower (not maximized),
  4438. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4439. // Once maximized already, top dock shinks back, but the session PageControl,
  4440. // do not align up, leaving space between TopDock and PageControl.
  4441. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4442. for (int Index = 0; Index < ControlCount; Index++)
  4443. {
  4444. RealignControl(Controls[Index]);
  4445. }
  4446. if (FTrayIcon->Visible)
  4447. {
  4448. FTrayIcon->Visible = false;
  4449. if (Visible)
  4450. {
  4451. ShowWindow(Handle, SW_SHOW);
  4452. }
  4453. }
  4454. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4455. {
  4456. FNeedSession = false;
  4457. NonVisualDataModule->StartBusy();
  4458. try
  4459. {
  4460. NeedSession(true);
  4461. }
  4462. __finally
  4463. {
  4464. NonVisualDataModule->EndBusy();
  4465. }
  4466. }
  4467. }
  4468. //---------------------------------------------------------------------------
  4469. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4470. {
  4471. FTrayIcon->Hint = GetMainForm()->Caption;
  4472. }
  4473. //---------------------------------------------------------------------------
  4474. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4475. {
  4476. UpdateTrayIcon();
  4477. }
  4478. //---------------------------------------------------------------------------
  4479. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4480. {
  4481. // workaround
  4482. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4483. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4484. // But WM_ACTIVATEAPP message can be received even when
  4485. // the main window is minimized to the tray and internal editor window is focused
  4486. // (after another application was previously active)
  4487. if (::IsIconic(Handle))
  4488. {
  4489. if (!IsAppIconic())
  4490. {
  4491. SetAppIconic(true);
  4492. }
  4493. }
  4494. ::ApplicationRestore();
  4495. Application->BringToFront();
  4496. }
  4497. //---------------------------------------------------------------------------
  4498. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4499. {
  4500. RestoreApp();
  4501. }
  4502. //---------------------------------------------------------------------------
  4503. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4504. {
  4505. if (OpenInNewWindow())
  4506. {
  4507. ExecuteNewInstance(SessionUrl);
  4508. }
  4509. else
  4510. {
  4511. TTerminalManager::Instance()->NewSession(SessionUrl);
  4512. }
  4513. }
  4514. //---------------------------------------------------------------------------
  4515. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4516. {
  4517. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4518. StoredSessions->NewSession(SessionName, SessionData);
  4519. // modified only, explicit
  4520. StoredSessions->Save(false, true);
  4521. // encode session name because of slashes in hierarchical sessions
  4522. return EncodeUrlString(SessionName);
  4523. }
  4524. //---------------------------------------------------------------------------
  4525. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4526. {
  4527. // current working directories become defaults here, what is not right
  4528. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4529. return SaveHiddenDuplicateSession(SessionData.get());
  4530. }
  4531. //---------------------------------------------------------------------------
  4532. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4533. {
  4534. if (OpenInNewWindow())
  4535. {
  4536. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4537. }
  4538. else
  4539. {
  4540. // current working directories become defaults here, what is not right
  4541. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4542. TTerminalManager * Manager = TTerminalManager::Instance();
  4543. TManagedTerminal * ATerminal = Manager->NewManagedTerminal(SessionData.get());
  4544. // We definitelly want this
  4545. ATerminal->Disconnected = Terminal->Disconnected;
  4546. // Not sure about these two
  4547. ATerminal->Permanent = Terminal->Permanent;
  4548. ATerminal->DisconnectedTemporarily = Terminal->DisconnectedTemporarily;
  4549. Manager->ActiveTerminal = ATerminal;
  4550. }
  4551. }
  4552. //---------------------------------------------------------------------------
  4553. void __fastcall TCustomScpExplorerForm::RenameSession()
  4554. {
  4555. UnicodeString Name = Terminal->SessionData->SessionName;
  4556. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4557. {
  4558. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4559. TSessionData::ValidateName(Name);
  4560. // This is inconsistent with how color (for example) is handled.
  4561. Terminal->SessionData->Name = Name;
  4562. UpdateControls();
  4563. // Add/Remove distinguishing paths from sessions of the same name.
  4564. DoTerminalListChanged();
  4565. }
  4566. }
  4567. //---------------------------------------------------------------------------
  4568. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4569. {
  4570. DebugAssert(Queue != NULL);
  4571. bool Result = Queue->IsEmpty;
  4572. if (!Result)
  4573. {
  4574. SetFocus();
  4575. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4576. }
  4577. return Result;
  4578. }
  4579. //---------------------------------------------------------------------------
  4580. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4581. {
  4582. return CanCloseQueue(FQueue);
  4583. }
  4584. //---------------------------------------------------------------------------
  4585. void __fastcall TCustomScpExplorerForm::CloseSession()
  4586. {
  4587. if (CanCloseQueue())
  4588. {
  4589. TTerminalManager::Instance()->FreeActiveTerminal();
  4590. }
  4591. }
  4592. //---------------------------------------------------------------------------
  4593. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4594. {
  4595. if (CanCloseQueue())
  4596. {
  4597. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4598. }
  4599. }
  4600. //---------------------------------------------------------------------------
  4601. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4602. {
  4603. DetachTerminal(Terminal);
  4604. RemoteDirView->Terminal = Terminal;
  4605. UpdateRemotePathComboBox(false);
  4606. UpdateControls();
  4607. }
  4608. //---------------------------------------------------------------------------
  4609. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4610. {
  4611. if (DebugAlwaysTrue(!Terminal->Active))
  4612. {
  4613. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4614. }
  4615. }
  4616. //---------------------------------------------------------------------------
  4617. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4618. {
  4619. if (OpenInNewWindow())
  4620. {
  4621. // encode session name because of slashes in hierarchical sessions
  4622. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4623. }
  4624. else
  4625. {
  4626. TTerminalManager * Manager = TTerminalManager::Instance();
  4627. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4628. Manager->ActiveTerminal = Terminal;
  4629. }
  4630. }
  4631. //---------------------------------------------------------------------------
  4632. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4633. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4634. {
  4635. TTerminalManager * Manager = TTerminalManager::Instance();
  4636. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4637. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4638. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4639. {
  4640. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4641. // FirstTerminal can be null, if some of the
  4642. if (!ConnectFirstTerminal && (FirstTerminal != NULL))
  4643. {
  4644. FirstTerminal->Disconnected = true;
  4645. FirstTerminal->DisconnectedTemporarily = true;
  4646. }
  4647. Manager->ActiveTerminal = FirstTerminal;
  4648. }
  4649. }
  4650. //---------------------------------------------------------------------------
  4651. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4652. {
  4653. if (OpenInNewWindow())
  4654. {
  4655. ExecuteNewInstance(Name);
  4656. }
  4657. else
  4658. {
  4659. DoOpenFolderOrWorkspace(Name, true, false);
  4660. }
  4661. }
  4662. //---------------------------------------------------------------------------
  4663. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4664. bool &CanClose)
  4665. {
  4666. // See the comment in CloseApp()
  4667. if (NonVisualDataModule->Busy)
  4668. {
  4669. CanClose = false;
  4670. }
  4671. else
  4672. {
  4673. TTerminalManager * Manager = TTerminalManager::Instance();
  4674. int ActiveSessions = 0;
  4675. for (int Index = 0; Index < Manager->Count; Index++)
  4676. {
  4677. if (Manager->Terminals[Index]->Active)
  4678. {
  4679. ActiveSessions++;
  4680. }
  4681. }
  4682. // Confirm, when there's at least one active remote session or
  4683. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4684. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4685. WinConfiguration->ConfirmClosingSession)
  4686. {
  4687. unsigned int Result;
  4688. TMessageParams Params(mpNeverAskAgainCheck);
  4689. UnicodeString Message;
  4690. int Answers = qaOK | qaCancel;
  4691. // The current session is the only active session
  4692. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4693. {
  4694. if (!WinConfiguration->AutoSaveWorkspace)
  4695. {
  4696. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4697. }
  4698. else
  4699. {
  4700. Message = LoadStr(CLOSE_SESSION);
  4701. }
  4702. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4703. }
  4704. // Multiple active sessions or one active session, but it's not the current one
  4705. else if (ActiveSessions > 0)
  4706. {
  4707. if (!WinConfiguration->AutoSaveWorkspace)
  4708. {
  4709. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4710. }
  4711. else
  4712. {
  4713. Message = LoadStr(CLOSE_SESSIONS);
  4714. }
  4715. }
  4716. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4717. else
  4718. {
  4719. DebugAlwaysTrue(Manager->Count > 1);
  4720. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4721. Message = LoadStr(CLOSE_WORKSPACE);
  4722. }
  4723. UnicodeString Note;
  4724. if (WinConfiguration->AutoSaveWorkspace)
  4725. {
  4726. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4727. }
  4728. else
  4729. {
  4730. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4731. Answers = qaYes | qaNo | qaCancel;
  4732. }
  4733. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4734. SetFocus();
  4735. Result = MessageDialog(Message, qtConfirmation,
  4736. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4737. if (Result == qaNeverAskAgain)
  4738. {
  4739. WinConfiguration->ConfirmClosingSession = false;
  4740. }
  4741. if (Result == qaNo)
  4742. {
  4743. CanClose = SaveWorkspace(true);
  4744. // note that the workspace will be saved redundatly again from FormClose
  4745. }
  4746. else
  4747. {
  4748. CanClose =
  4749. (Result == qaOK) ||
  4750. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4751. (Result == qaNeverAskAgain);
  4752. }
  4753. }
  4754. if (CanClose && HasActiveTerminal())
  4755. {
  4756. CanClose = CanCloseQueue();
  4757. }
  4758. }
  4759. if (CanClose)
  4760. {
  4761. CanClose =
  4762. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4763. FEditorManager->CloseExternalFilesWithoutProcess();
  4764. if (CanClose)
  4765. {
  4766. while (CanClose && (FLocalEditors->Count > 0))
  4767. {
  4768. int PrevCount = FLocalEditors->Count;
  4769. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4770. CanClose = (FLocalEditors->Count < PrevCount);
  4771. }
  4772. if (CanClose)
  4773. {
  4774. CanClose = FEditorManager->Empty(true);
  4775. if (!CanClose)
  4776. {
  4777. SetFocus();
  4778. CanClose =
  4779. (MessageDialog(
  4780. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4781. }
  4782. }
  4783. }
  4784. }
  4785. }
  4786. //---------------------------------------------------------------------------
  4787. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4788. {
  4789. TForm * Form = dynamic_cast<TForm *>(Sender);
  4790. DebugAssert(Form != NULL);
  4791. Form->Close();
  4792. }
  4793. //---------------------------------------------------------------------------
  4794. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4795. {
  4796. TForm * Form = dynamic_cast<TForm *>(Sender);
  4797. delete Form;
  4798. }
  4799. //---------------------------------------------------------------------------
  4800. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4801. {
  4802. while (FLocalEditors->Count > 0)
  4803. {
  4804. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4805. }
  4806. }
  4807. //---------------------------------------------------------------------------
  4808. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4809. TObject *Sender)
  4810. {
  4811. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4812. try
  4813. {
  4814. SetProperties(osRemote, FileList);
  4815. }
  4816. __finally
  4817. {
  4818. delete FileList;
  4819. }
  4820. }
  4821. //---------------------------------------------------------------------------
  4822. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4823. {
  4824. if (value)
  4825. {
  4826. if (Component == fcCustomCommandsBand)
  4827. {
  4828. UpdateCustomCommandsToolbar();
  4829. }
  4830. if (Component == fcQueueFileList)
  4831. {
  4832. DebugAssert(!QueueFileListSplitter->Visible);
  4833. AdjustQueueLayout();
  4834. UpdateQueueFileList();
  4835. }
  4836. }
  4837. }
  4838. //---------------------------------------------------------------------------
  4839. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4840. {
  4841. int MinHeightLimit =
  4842. GetStaticQueuePanelComponentsHeight() +
  4843. QueueFileListSplitter->Height +
  4844. QueueFileList->Height +
  4845. GetMinQueueViewHeight();
  4846. if (QueuePanel->ClientHeight < MinHeightLimit)
  4847. {
  4848. QueuePanel->ClientHeight = MinHeightLimit;
  4849. }
  4850. }
  4851. //---------------------------------------------------------------------------
  4852. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4853. {
  4854. TControl * Control = GetComponent(Component);
  4855. DebugAssert(Control);
  4856. bool Changed = (Control->Visible != value);
  4857. if (Changed)
  4858. {
  4859. ComponentShowing(Component, value);
  4860. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4861. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4862. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4863. if (value)
  4864. {
  4865. int RemainingHeight = Control->Parent->ClientHeight;
  4866. int RemainingWidth = Control->Parent->ClientWidth;
  4867. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4868. {
  4869. TControl * ChildControl = Control->Parent->Controls[i];
  4870. if (ChildControl->Visible)
  4871. {
  4872. switch (ChildControl->Align)
  4873. {
  4874. case alTop:
  4875. case alBottom:
  4876. RemainingHeight -= ChildControl->Height;
  4877. break;
  4878. case alLeft:
  4879. case alRight:
  4880. RemainingWidth -= ChildControl->Width;
  4881. break;
  4882. }
  4883. }
  4884. }
  4885. int Reserve = ScaleByTextHeight(this, 32);
  4886. // queue in explorer, trees in commander
  4887. if (Control->Height > RemainingHeight - Reserve)
  4888. {
  4889. Control->Height = RemainingHeight / 2;
  4890. }
  4891. if (Control->Width > RemainingWidth - Reserve)
  4892. {
  4893. Control->Width = RemainingWidth / 2;
  4894. }
  4895. }
  4896. Control->Visible = value;
  4897. if (WasFocused && Visible)
  4898. {
  4899. DirView(osCurrent)->SetFocus();
  4900. }
  4901. FixControlsPlacement();
  4902. }
  4903. }
  4904. //---------------------------------------------------------------------------
  4905. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4906. {
  4907. TControl * Control = GetComponent(Component);
  4908. if (Control == NULL)
  4909. {
  4910. return false;
  4911. }
  4912. else
  4913. {
  4914. return Control->Visible;
  4915. }
  4916. }
  4917. //---------------------------------------------------------------------------
  4918. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4919. {
  4920. bool Result;
  4921. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4922. {
  4923. Result = !IsUWP();
  4924. }
  4925. else
  4926. {
  4927. Result = true;
  4928. }
  4929. return Result;
  4930. }
  4931. //---------------------------------------------------------------------------
  4932. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4933. {
  4934. if (DirView(osOther)->ItemFocused != NULL)
  4935. {
  4936. DirView(osOther)->ItemFocused->MakeVisible(false);
  4937. }
  4938. QueueSplitter->Visible = QueuePanel->Visible;
  4939. QueueFileListSplitter->Visible = QueueFileList->Visible;
  4940. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4941. TControl * QueueControlsOrder[] =
  4942. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  4943. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  4944. }
  4945. //---------------------------------------------------------------------------
  4946. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4947. {
  4948. switch (Component) {
  4949. case fcStatusBar: return RemoteStatusBar;
  4950. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4951. case fcQueueView: return QueuePanel;
  4952. case fcQueueToolbar: return QueueDock;
  4953. case fcRemoteTree: return RemoteDrivePanel;
  4954. case fcSessionsTabs: return SessionsPageControl;
  4955. case fcQueueFileList: return QueueFileList;
  4956. default: return NULL;
  4957. }
  4958. }
  4959. //---------------------------------------------------------------------------
  4960. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4961. TObject *Sender, TListColumn *Column, TPoint &Point)
  4962. {
  4963. DebugAssert(NonVisualDataModule && Column && Sender);
  4964. NonVisualDataModule->ListColumn = Column;
  4965. TPopupMenu * DirViewColumnMenu;
  4966. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4967. {
  4968. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4969. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4970. (Column->Index == uvName);
  4971. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4972. (Column->Index == uvSize);
  4973. }
  4974. else
  4975. {
  4976. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4977. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4978. (Column->Index == dvName);
  4979. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4980. (Column->Index == dvSize);
  4981. }
  4982. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4983. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4984. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4985. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4986. }
  4987. //---------------------------------------------------------------------------
  4988. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4989. TObject *Sender, TListItem *Item, bool &AllowExec)
  4990. {
  4991. DoDirViewExecFile(Sender, Item, AllowExec);
  4992. }
  4993. //---------------------------------------------------------------------------
  4994. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4995. TListItem * Item, bool & AllowExec)
  4996. {
  4997. DebugAssert(Sender && Item && Configuration);
  4998. DebugAssert(AllowExec);
  4999. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5000. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  5001. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  5002. TOperationSide Side = (Remote ? osRemote : osLocal);
  5003. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  5004. // on files only (not directories)
  5005. // and only when symlinks are resolved (apply to remote panel only)
  5006. if (!ADirView->ItemIsDirectory(Item) &&
  5007. (ResolvedSymlinks || FForceExecution))
  5008. {
  5009. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5010. !FForceExecution && ResolvedSymlinks)
  5011. {
  5012. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5013. {
  5014. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5015. UnlockWindow();
  5016. try
  5017. {
  5018. ExecuteFileOperation(
  5019. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5020. }
  5021. __finally
  5022. {
  5023. LockWindow();
  5024. }
  5025. AllowExec = false;
  5026. }
  5027. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5028. {
  5029. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5030. {
  5031. ExecuteFile(Side, efDefaultEditor);
  5032. AllowExec = false;
  5033. }
  5034. }
  5035. else
  5036. {
  5037. DebugFail();
  5038. }
  5039. }
  5040. // if we have not done anything special, fall back to default behavior
  5041. if (AllowExec)
  5042. {
  5043. if (Remote && !WinConfiguration->DisableOpenEdit)
  5044. {
  5045. ExecuteFile(osRemote, efShell);
  5046. AllowExec = false;
  5047. }
  5048. }
  5049. }
  5050. }
  5051. //---------------------------------------------------------------------------
  5052. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5053. {
  5054. return ((Side == osRemote) || (Side == osCurrent));
  5055. }
  5056. //---------------------------------------------------------------------------
  5057. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5058. {
  5059. DebugFail();
  5060. }
  5061. //---------------------------------------------------------------------------
  5062. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5063. {
  5064. DebugFail();
  5065. }
  5066. //---------------------------------------------------------------------------
  5067. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5068. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5069. {
  5070. TSynchronizeParamType Params;
  5071. Params.LocalDirectory = LocalDirectory;
  5072. Params.RemoteDirectory = RemoteDirectory;
  5073. int UnusedParams =
  5074. (GUIConfiguration->SynchronizeParams &
  5075. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5076. TTerminal::spCaseSensitive));
  5077. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5078. Params.Options = GUIConfiguration->SynchronizeOptions;
  5079. bool SaveSettings = false;
  5080. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5081. &DoSynchronizeTooManyDirectories);
  5082. DebugAssert(FSynchronizeController == NULL);
  5083. FSynchronizeController = &Controller;
  5084. bool Result;
  5085. try
  5086. {
  5087. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5088. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5089. int Options =
  5090. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5091. DebugAssert(FOnFeedSynchronizeError == NULL);
  5092. DebugAssert(FOnSynchronizeAbort == NULL);
  5093. DebugAssert(FSynchronizeTerminal == NULL);
  5094. FSynchronizeTerminal = Terminal;
  5095. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5096. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5097. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5098. if (Result)
  5099. {
  5100. if (SaveSettings)
  5101. {
  5102. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5103. GUIConfiguration->SynchronizeOptions = Params.Options;
  5104. }
  5105. else
  5106. {
  5107. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5108. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5109. FLAGSET(Params.Options, soSynchronize))
  5110. {
  5111. GUIConfiguration->SynchronizeOptions =
  5112. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5113. soSynchronize;
  5114. }
  5115. }
  5116. LocalDirectory = Params.LocalDirectory;
  5117. RemoteDirectory = Params.RemoteDirectory;
  5118. }
  5119. }
  5120. __finally
  5121. {
  5122. FSynchronizeController = NULL;
  5123. DebugAssert(FOnFeedSynchronizeError == NULL);
  5124. FOnFeedSynchronizeError = NULL;
  5125. DebugAssert(FOnSynchronizeAbort == NULL);
  5126. FOnSynchronizeAbort = NULL;
  5127. FSynchronizeTerminal = NULL;
  5128. }
  5129. return Result;
  5130. }
  5131. //---------------------------------------------------------------------------
  5132. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5133. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5134. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5135. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5136. TSynchronizeOptions * Options, bool Full)
  5137. {
  5138. // Keep-up-to-date mode
  5139. if (Terminal->Status == ssOpened)
  5140. {
  5141. try
  5142. {
  5143. int PParams = Params.Params;
  5144. if (!Full)
  5145. {
  5146. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5147. if (FLAGSET(Params.Options, soRecurse))
  5148. {
  5149. PParams |= TTerminal::spSubDirs;
  5150. }
  5151. }
  5152. else
  5153. {
  5154. // if keepuptodate is non-recursive,
  5155. // full sync before has to be non-recursive as well
  5156. if (FLAGCLEAR(Params.Options, soRecurse))
  5157. {
  5158. PParams |= TTerminal::spNoRecurse;
  5159. }
  5160. }
  5161. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5162. PParams, Checklist, Options);
  5163. }
  5164. catch (Exception & E)
  5165. {
  5166. if (!Terminal->Active)
  5167. {
  5168. ShowExtendedExceptionEx(Terminal, &E);
  5169. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5170. if (!Terminal->Active)
  5171. {
  5172. throw;
  5173. }
  5174. }
  5175. else
  5176. {
  5177. if (FLAGSET(Params.Options, soContinueOnError) &&
  5178. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5179. {
  5180. // noop, already logged in MoreMessageDialog
  5181. }
  5182. else
  5183. {
  5184. // We get mostly EAbort here, so this is noop.
  5185. // Exception is an error when listing directories while looking for differences.
  5186. ShowExtendedExceptionEx(Terminal, &E);
  5187. throw;
  5188. }
  5189. }
  5190. }
  5191. }
  5192. }
  5193. //---------------------------------------------------------------------------
  5194. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5195. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5196. const UnicodeString ErrorStr)
  5197. {
  5198. if (!Directory.IsEmpty())
  5199. {
  5200. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5201. }
  5202. else
  5203. {
  5204. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5205. }
  5206. }
  5207. //---------------------------------------------------------------------------
  5208. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5209. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5210. {
  5211. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5212. {
  5213. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5214. }
  5215. else
  5216. {
  5217. TMessageParams Params(mpNeverAskAgainCheck);
  5218. unsigned int Result = MessageDialog(
  5219. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5220. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5221. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5222. {
  5223. MaxDirectories *= 2;
  5224. if (Result == qaNeverAskAgain)
  5225. {
  5226. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5227. }
  5228. }
  5229. else
  5230. {
  5231. Abort();
  5232. }
  5233. }
  5234. }
  5235. //---------------------------------------------------------------------------
  5236. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5237. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5238. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5239. TSynchronizeOptions * Options)
  5240. {
  5241. // Keep-up-to-date mode
  5242. DebugAssert(!FAutoOperation);
  5243. void * BatchStorage;
  5244. BatchStart(BatchStorage);
  5245. FAutoOperation = true;
  5246. bool AnyOperation = false;
  5247. TDateTime StartTime = Now();
  5248. TSynchronizeChecklist * AChecklist = NULL;
  5249. try
  5250. {
  5251. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5252. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5253. {
  5254. FSynchronizeProgressForm->Start();
  5255. }
  5256. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5257. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5258. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5259. Options);
  5260. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5261. SAFE_DESTROY(FSynchronizeProgressForm);
  5262. AnyOperation = (AChecklist->CheckedCount > 0);
  5263. if (AnyOperation)
  5264. {
  5265. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5266. CreateProgressForm(&SynchronizeProgress);
  5267. try
  5268. {
  5269. Terminal->SynchronizeApply(
  5270. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5271. SynchronizeProcessedItem, NULL, NULL, NULL);
  5272. }
  5273. __finally
  5274. {
  5275. DestroyProgressForm();
  5276. }
  5277. }
  5278. }
  5279. __finally
  5280. {
  5281. if (Checklist == NULL)
  5282. {
  5283. delete AChecklist;
  5284. }
  5285. else
  5286. {
  5287. *Checklist = AChecklist;
  5288. }
  5289. FAutoOperation = false;
  5290. SAFE_DESTROY(FSynchronizeProgressForm);
  5291. BatchEnd(BatchStorage);
  5292. ReloadLocalDirectory();
  5293. if (AnyOperation)
  5294. {
  5295. OperationComplete(StartTime);
  5296. }
  5297. }
  5298. }
  5299. //---------------------------------------------------------------------------
  5300. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5301. {
  5302. // can be called from command line
  5303. return Visible &&
  5304. ((DirView(osRemote)->SelCount > 0) ||
  5305. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5306. }
  5307. //---------------------------------------------------------------------------
  5308. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5309. {
  5310. LogSynchronizeEvent(Terminal, Message);
  5311. }
  5312. //---------------------------------------------------------------------------
  5313. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5314. int Params, TSynchronizeOptions & Options)
  5315. {
  5316. DebugAssert(!IsLocalBrowserMode());
  5317. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5318. {
  5319. Options.Filter = new TStringList();
  5320. Options.Filter->CaseSensitive = false;
  5321. Options.Filter->Duplicates = Types::dupAccept;
  5322. if (DirView(osRemote)->SelCount > 0)
  5323. {
  5324. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5325. }
  5326. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5327. {
  5328. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5329. }
  5330. Options.Filter->Sort();
  5331. }
  5332. }
  5333. //---------------------------------------------------------------------------
  5334. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5335. {
  5336. TCopyParamType Defaults;
  5337. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5338. ConfigStorage->AccessMode = smRead;
  5339. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5340. {
  5341. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5342. }
  5343. ConfigStorage.reset(NULL);
  5344. std::unique_ptr<TStringList> Options(new TStringList());
  5345. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5346. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5347. UnicodeString Result;
  5348. if (Options->Count > 0)
  5349. {
  5350. Result =
  5351. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5352. }
  5353. return Result;
  5354. }
  5355. //---------------------------------------------------------------------------
  5356. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5357. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5358. {
  5359. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5360. UnicodeString AdditionalParams =
  5361. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5362. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5363. SerializeCopyParamForCommandLine(CopyParams),
  5364. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5365. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5366. ExecuteNewInstance(SessionName, AdditionalParams);
  5367. }
  5368. //---------------------------------------------------------------------------
  5369. struct TSynchronizeParams
  5370. {
  5371. UnicodeString LocalDirectory;
  5372. UnicodeString RemoteDirectory;
  5373. TSynchronizeMode Mode;
  5374. TSynchronizeChecklist * Checklist;
  5375. int Params;
  5376. TCopyParamType * CopyParam;
  5377. TDateTime CollectElapsed;
  5378. TDateTime * StartTime;
  5379. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5380. };
  5381. //---------------------------------------------------------------------------
  5382. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5383. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5384. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5385. {
  5386. DebugAssert(!FAutoOperation);
  5387. void * BatchStorage;
  5388. BatchStart(BatchStorage);
  5389. FAutoOperation = true;
  5390. TFileOperationStatistics Statistics;
  5391. TDateTime Start = Now();
  5392. try
  5393. {
  5394. Params.OnProcessedItem = OnProcessedItem;
  5395. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5396. CreateProgressForm(&SynchronizeProgress);
  5397. Terminal->SynchronizeApply(
  5398. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5399. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5400. &Statistics);
  5401. }
  5402. __finally
  5403. {
  5404. Params.OnProcessedItem = NULL;
  5405. FAutoOperation = false;
  5406. DestroyProgressForm();
  5407. BatchEnd(BatchStorage);
  5408. ReloadLocalDirectory();
  5409. }
  5410. if (WinConfiguration->SynchronizeSummary)
  5411. {
  5412. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5413. // The statistics should be 0 anyway in this case
  5414. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5415. {
  5416. Message += L"\n";
  5417. if (Statistics.FilesUploaded > 0)
  5418. {
  5419. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5420. }
  5421. if (Statistics.FilesDownloaded > 0)
  5422. {
  5423. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5424. }
  5425. if (Statistics.FilesDeletedLocal > 0)
  5426. {
  5427. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5428. }
  5429. if (Statistics.FilesDeletedRemote > 0)
  5430. {
  5431. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5432. }
  5433. }
  5434. TDateTime Elapsed = (Now() - Start);
  5435. Message +=
  5436. L"\n" +
  5437. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5438. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5439. TMessageParams Params(mpNeverAskAgainCheck);
  5440. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5441. if (Result == qaNeverAskAgain)
  5442. {
  5443. WinConfiguration->SynchronizeSummary = false;
  5444. }
  5445. }
  5446. }
  5447. //---------------------------------------------------------------------------
  5448. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5449. {
  5450. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5451. {
  5452. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5453. }
  5454. // Not set in keep-up-to-date mode - Synchronize() method
  5455. if (Token != NULL)
  5456. {
  5457. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5458. if (Params.OnProcessedItem != NULL)
  5459. {
  5460. Params.OnProcessedItem(NULL, ChecklistItem);
  5461. }
  5462. }
  5463. }
  5464. //---------------------------------------------------------------------------
  5465. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5466. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5467. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5468. {
  5469. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5470. *Params.StartTime = Now();
  5471. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5472. }
  5473. //---------------------------------------------------------------------------
  5474. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5475. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5476. {
  5477. // terminal can be already closed (e.g. dropped connection)
  5478. if (Terminal != NULL)
  5479. {
  5480. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5481. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5482. }
  5483. }
  5484. //---------------------------------------------------------------------------
  5485. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5486. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5487. {
  5488. DebugAssert(!IsLocalBrowserMode());
  5489. TAutoBatch AutoBatch(this);
  5490. TAutoFlag AutoOperationFlag(FAutoOperation);
  5491. if (Side == osRemote)
  5492. {
  5493. std::unique_ptr<TStrings> FileList(new TStringList());
  5494. FileList->AddObject(FileName, RemoteFile);
  5495. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5496. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5497. RemoteDirView->SaveSelection();
  5498. RemoteDirView->SaveSelectedNames();
  5499. try
  5500. {
  5501. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5502. }
  5503. catch(...)
  5504. {
  5505. RemoteDirView->DiscardSavedSelection();
  5506. throw;
  5507. }
  5508. RemoteDirView->RestoreSelection();
  5509. }
  5510. else if (DebugAlwaysTrue(Side == osLocal))
  5511. {
  5512. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5513. {
  5514. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5515. }
  5516. UnicodeString Directory = ExtractFileDir(FileName);
  5517. ReloadLocalDirectory(Directory);
  5518. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5519. if (!SamePaths(Directory, NewDirectory))
  5520. {
  5521. ReloadLocalDirectory(NewDirectory);
  5522. }
  5523. }
  5524. }
  5525. //---------------------------------------------------------------------------
  5526. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5527. {
  5528. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5529. if (Side == osLocal)
  5530. {
  5531. if (Action == TSynchronizeChecklist::saDownloadNew)
  5532. {
  5533. OpenFolderInExplorer(LocalPath);
  5534. }
  5535. else
  5536. {
  5537. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5538. }
  5539. }
  5540. else if (DebugAlwaysTrue(Side == osRemote))
  5541. {
  5542. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5543. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5544. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5545. if (!LocalPath.IsEmpty())
  5546. {
  5547. SessionData->LocalDirectory = LocalPath;
  5548. }
  5549. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5550. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5551. }
  5552. }
  5553. //---------------------------------------------------------------------------
  5554. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5555. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5556. const TCopyParamType * CopyParams)
  5557. {
  5558. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5559. UnicodeString AdditionalParams =
  5560. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5561. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5562. SerializeCopyParamForCommandLine(CopyParams),
  5563. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5564. LocalDirectory, RemoteDirectory, Mode, Params));
  5565. ExecuteNewInstance(SessionName, AdditionalParams);
  5566. }
  5567. //---------------------------------------------------------------------------
  5568. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5569. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5570. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5571. {
  5572. int Result;
  5573. bool SaveSettings = false;
  5574. int Options =
  5575. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5576. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5577. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5578. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5579. bool Continue =
  5580. (UseDefaults == 0) ||
  5581. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5582. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5583. if (Continue)
  5584. {
  5585. Configuration->Usage->Inc(L"Synchronizations");
  5586. UpdateCopyParamCounters(CopyParam);
  5587. TSynchronizeOptions SynchronizeOptions;
  5588. GetSynchronizeOptions(Params, SynchronizeOptions);
  5589. if (SaveSettings)
  5590. {
  5591. GUIConfiguration->SynchronizeParams = Params;
  5592. }
  5593. else
  5594. {
  5595. SaveMode = false;
  5596. }
  5597. TDateTime StartTime = Now();
  5598. TSynchronizeChecklist * Checklist = NULL;
  5599. try
  5600. {
  5601. DebugAssert(!FAutoOperation);
  5602. FAutoOperation = true;
  5603. try
  5604. {
  5605. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5606. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5607. int Files = -1;
  5608. if (DataList->Count >= 1)
  5609. {
  5610. Files = StrToIntDef(DataList->Strings[0], Files);
  5611. }
  5612. else
  5613. {
  5614. DataList->Add(UnicodeString());
  5615. }
  5616. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5617. FSynchronizeProgressForm->Start();
  5618. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5619. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5620. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5621. &SynchronizeOptions);
  5622. if (Terminal->SessionData->CacheDirectories)
  5623. {
  5624. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5625. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5626. }
  5627. }
  5628. __finally
  5629. {
  5630. FAutoOperation = false;
  5631. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5632. SAFE_DESTROY(FSynchronizeProgressForm);
  5633. }
  5634. TSynchronizeParams SynchronizeParams;
  5635. SynchronizeParams.LocalDirectory = LocalDirectory;
  5636. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5637. SynchronizeParams.Mode = Mode;
  5638. SynchronizeParams.CopyParam = &CopyParam;
  5639. SynchronizeParams.Params = Params;
  5640. SynchronizeParams.Checklist = Checklist;
  5641. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5642. SynchronizeParams.StartTime = &StartTime;
  5643. SynchronizeParams.OnProcessedItem = NULL;
  5644. Result = Checklist->Count;
  5645. if (Checklist->Count > 0)
  5646. {
  5647. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5648. {
  5649. if (!DoSynchronizeChecklistDialog(
  5650. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5651. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5652. {
  5653. Result = -1;
  5654. }
  5655. }
  5656. else
  5657. {
  5658. FullSynchronize(SynchronizeParams, NULL, NULL);
  5659. }
  5660. }
  5661. else
  5662. {
  5663. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5664. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5665. }
  5666. }
  5667. __finally
  5668. {
  5669. delete Checklist;
  5670. }
  5671. OperationComplete(StartTime);
  5672. }
  5673. else
  5674. {
  5675. Result = -1;
  5676. }
  5677. return Result;
  5678. }
  5679. //---------------------------------------------------------------------------
  5680. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5681. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5682. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5683. {
  5684. if (Collect)
  5685. {
  5686. DebugAssert(FSynchronizeProgressForm != NULL);
  5687. if (!FSynchronizeProgressForm->Started)
  5688. {
  5689. FSynchronizeProgressForm->Start();
  5690. }
  5691. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5692. SetTaskbarListProgressValue(CompareProgress);
  5693. }
  5694. else
  5695. {
  5696. DebugAssert(FSynchronizeProgressForm == NULL);
  5697. DebugAssert(FProgressForm != NULL);
  5698. }
  5699. }
  5700. //---------------------------------------------------------------------------
  5701. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5702. {
  5703. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5704. TRemoteFile * File = NULL;
  5705. Terminal->ReadFile(FullFileName, File);
  5706. if (File != NULL)
  5707. {
  5708. std::unique_ptr<TRemoteFile> FileOwner(File);
  5709. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5710. Application->ShowMainForm = false;
  5711. Application->Run();
  5712. }
  5713. }
  5714. //---------------------------------------------------------------------------
  5715. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5716. {
  5717. DebugFail();
  5718. }
  5719. //---------------------------------------------------------------------------
  5720. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5721. {
  5722. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5723. SessionData->Assign(Terminal->SessionData);
  5724. UpdateSessionData(SessionData.get());
  5725. if (Terminal->Active)
  5726. {
  5727. Terminal->UpdateSessionCredentials(SessionData.get());
  5728. }
  5729. if (!Terminal->SessionData->HasSessionName())
  5730. {
  5731. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5732. // once IsWorkspace is cleared
  5733. SessionData->Name = UnicodeString();
  5734. }
  5735. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5736. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5737. SessionData->IsWorkspace = false;
  5738. return SessionData.release();
  5739. }
  5740. //---------------------------------------------------------------------------
  5741. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5742. {
  5743. TSessionData * SessionData = CloneCurrentSessionData();
  5744. try
  5745. {
  5746. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5747. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5748. }
  5749. __finally
  5750. {
  5751. delete SessionData;
  5752. }
  5753. }
  5754. //---------------------------------------------------------------------------
  5755. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5756. {
  5757. TTerminalManager * Manager = TTerminalManager::Instance();
  5758. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5759. if (DebugAlwaysTrue(Terminal != NULL))
  5760. {
  5761. // Update (Managed)Terminal->StateData
  5762. UpdateTerminal(Terminal);
  5763. }
  5764. Manager->SaveWorkspace(DataList.get());
  5765. return DataList.release();
  5766. }
  5767. //---------------------------------------------------------------------------
  5768. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5769. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5770. {
  5771. WinConfiguration->LastWorkspace = Name;
  5772. if (!SavePasswords)
  5773. {
  5774. for (int Index = 0; Index < DataList->Count; Index++)
  5775. {
  5776. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5777. if (SessionData->Link.IsEmpty())
  5778. {
  5779. SessionData->ClearSessionPasswords();
  5780. }
  5781. }
  5782. }
  5783. StoredSessions->NewWorkspace(Name, DataList);
  5784. // modified only
  5785. StoredSessions->Save(false, Explicit);
  5786. }
  5787. //---------------------------------------------------------------------------
  5788. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5789. {
  5790. return
  5791. DefaultStr(WinConfiguration->LastWorkspace,
  5792. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5793. }
  5794. //---------------------------------------------------------------------------
  5795. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5796. {
  5797. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5798. bool AnyNonStoredSessionWithPassword = false;
  5799. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5800. bool AllNonStoredSessionsAnonymous = true;
  5801. TTerminalManager * Manager = TTerminalManager::Instance();
  5802. for (int Index = 0; Index < DataList->Count; Index++)
  5803. {
  5804. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5805. if (SessionData->Link.IsEmpty())
  5806. {
  5807. if (SessionData->HasAnySessionPassword())
  5808. {
  5809. AnyNonStoredSessionWithPassword = true;
  5810. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5811. {
  5812. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5813. }
  5814. }
  5815. if (!SameText(SessionData->UserName, AnonymousUserName))
  5816. {
  5817. AllNonStoredSessionsAnonymous = false;
  5818. }
  5819. }
  5820. }
  5821. bool SavePasswords;
  5822. bool * PSavePasswords;
  5823. bool NotRecommendedSavingPasswords =
  5824. !CustomWinConfiguration->UseMasterPassword &&
  5825. !AllNonStoredSessionsAnonymous;
  5826. if (Configuration->DisablePasswordStoring ||
  5827. !AnyNonStoredSessionWithPassword)
  5828. {
  5829. PSavePasswords = NULL;
  5830. SavePasswords = false;
  5831. }
  5832. else
  5833. {
  5834. PSavePasswords = &SavePasswords;
  5835. SavePasswords =
  5836. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5837. !NotRecommendedSavingPasswords;
  5838. }
  5839. UnicodeString Name = WorkspaceName();
  5840. bool CreateShortcut = false;
  5841. bool Result =
  5842. DoSaveWorkspaceDialog(
  5843. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5844. EnableAutoSave);
  5845. if (Result)
  5846. {
  5847. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5848. if (CreateShortcut)
  5849. {
  5850. TOperationVisualizer Visualizer;
  5851. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5852. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5853. }
  5854. if (EnableAutoSave)
  5855. {
  5856. WinConfiguration->AutoSaveWorkspace = true;
  5857. WinConfiguration->AutoWorkspace = Name;
  5858. if (PSavePasswords != NULL)
  5859. {
  5860. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5861. }
  5862. }
  5863. }
  5864. return Result;
  5865. }
  5866. //---------------------------------------------------------------------------
  5867. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5868. {
  5869. DebugAssert(!IsLocalBrowserMode());
  5870. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5871. if (WinConfiguration->PreservePanelState)
  5872. {
  5873. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5874. }
  5875. UpdateSessionData(Terminal->StateData);
  5876. }
  5877. //---------------------------------------------------------------------------
  5878. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5879. {
  5880. // Keep in sync with TSessionData::CopyStateData
  5881. DebugAssert(!IsLocalBrowserMode());
  5882. DebugAssert(Data != NULL);
  5883. // This is inconsistent with how color (for example) is handled.
  5884. Data->Name = Terminal->SessionData->Name;
  5885. // cannot use RemoteDirView->Path, because it is empty if connection
  5886. // was already closed
  5887. // also only peek, we may not be connected at all atm
  5888. // so make sure we do not try retrieving current directory from the server
  5889. // (particularly with FTP)
  5890. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5891. if (!ACurrentDirectory.IsEmpty())
  5892. {
  5893. Data->RemoteDirectory = ACurrentDirectory;
  5894. }
  5895. Data->Color = SessionColor;
  5896. }
  5897. //---------------------------------------------------------------------------
  5898. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5899. {
  5900. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5901. DebugAssert(Toolbar != NULL);
  5902. for (int i = 0; i < Toolbar->Items->Count; i++)
  5903. {
  5904. TTBXCustomDropDownItem * DropDownItem;
  5905. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5906. if (DropDownItem != NULL)
  5907. {
  5908. ToolbarItemResize(DropDownItem,
  5909. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5910. Toolbar->NonClientWidth);
  5911. break;
  5912. }
  5913. }
  5914. }
  5915. //---------------------------------------------------------------------------
  5916. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5917. {
  5918. Item->EditWidth = Width;
  5919. }
  5920. //---------------------------------------------------------------------------
  5921. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5922. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5923. {
  5924. for (int i = 0; i < Toolbar->Items->Count; i++)
  5925. {
  5926. TTBXCustomDropDownItem * DropDownItem;
  5927. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5928. if (DropDownItem != NULL)
  5929. {
  5930. ASize.x -= DropDownItem->EditWidth;
  5931. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5932. }
  5933. }
  5934. }
  5935. //---------------------------------------------------------------------------
  5936. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5937. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5938. {
  5939. if (WinConfiguration->DDWarnLackOfTempSpace)
  5940. {
  5941. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5942. if (!ADrive.IsEmpty())
  5943. {
  5944. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5945. DebugAssert(RequiredSpace >= 0);
  5946. __int64 RequiredWithReserve;
  5947. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5948. if (FreeSpace < RequiredWithReserve)
  5949. {
  5950. unsigned int Result;
  5951. TMessageParams Params(mpNeverAskAgainCheck);
  5952. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5953. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5954. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5955. if (Result == qaNeverAskAgain)
  5956. {
  5957. WinConfiguration->DDWarnLackOfTempSpace = false;
  5958. }
  5959. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5960. }
  5961. }
  5962. }
  5963. }
  5964. //---------------------------------------------------------------------------
  5965. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5966. {
  5967. DoOpenDirectoryDialog(odAddBookmark, Side);
  5968. }
  5969. //---------------------------------------------------------------------------
  5970. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5971. {
  5972. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5973. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5974. try
  5975. {
  5976. TCustomDirView * DView = DirView(Side);
  5977. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5978. {
  5979. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5980. }
  5981. }
  5982. catch (...)
  5983. {
  5984. delete VisitedDirectories;
  5985. throw;
  5986. }
  5987. return VisitedDirectories;
  5988. }
  5989. //---------------------------------------------------------------------------
  5990. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5991. TOpenDirectoryMode Mode, TOperationSide Side)
  5992. {
  5993. bool Continue = true;
  5994. if (Mode == odAddBookmark)
  5995. {
  5996. TMessageParams Params(mpNeverAskAgainCheck);
  5997. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5998. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5999. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6000. unsigned int Answer =
  6001. MessageDialog(Message,
  6002. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6003. if (Answer == qaNeverAskAgain)
  6004. {
  6005. Continue = true;
  6006. WinConfiguration->UseSharedBookmarks = true;
  6007. }
  6008. else if (Answer == qaOK)
  6009. {
  6010. Continue = true;
  6011. WinConfiguration->UseSharedBookmarks = false;
  6012. }
  6013. else
  6014. {
  6015. Continue = false;
  6016. }
  6017. }
  6018. if (Continue)
  6019. {
  6020. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6021. try
  6022. {
  6023. UnicodeString Name = DirView(Side)->PathName;
  6024. bool Repeat;
  6025. do
  6026. {
  6027. Repeat = false;
  6028. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  6029. // do not allow switching to location profiles,
  6030. // if we are not connected
  6031. HasDirView[osLocal] && (Terminal != NULL)))
  6032. {
  6033. TWindowLock Lock(this);
  6034. if (!TryOpenDirectory(Side, Name))
  6035. {
  6036. Repeat = true;
  6037. Mode = odBrowse;
  6038. }
  6039. }
  6040. }
  6041. while (Repeat);
  6042. }
  6043. __finally
  6044. {
  6045. delete VisitedDirectories;
  6046. }
  6047. }
  6048. }
  6049. //---------------------------------------------------------------------------
  6050. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6051. {
  6052. DebugAssert(!FTerminal->CommandSessionOpened);
  6053. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  6054. }
  6055. //---------------------------------------------------------------------------
  6056. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6057. {
  6058. bool Result = FTerminal->IsCapable[Capability] ||
  6059. FTerminal->CommandSessionOpened;
  6060. if (!Result)
  6061. {
  6062. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  6063. if (!GUIConfiguration->ConfirmCommandSession)
  6064. {
  6065. Result = true;
  6066. }
  6067. else
  6068. {
  6069. TMessageParams Params(mpNeverAskAgainCheck);
  6070. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6071. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6072. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6073. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6074. if (Answer == qaNeverAskAgain)
  6075. {
  6076. GUIConfiguration->ConfirmCommandSession = false;
  6077. Result = true;
  6078. }
  6079. else if (Answer == qaOK)
  6080. {
  6081. Result = true;
  6082. }
  6083. }
  6084. if (Result)
  6085. {
  6086. Result = CommandSessionFallback();
  6087. }
  6088. }
  6089. return Result;
  6090. }
  6091. //---------------------------------------------------------------------------
  6092. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6093. {
  6094. if (EnsureCommandSessionFallback(fcAnyCommand))
  6095. {
  6096. DoConsoleDialog(Terminal, Command);
  6097. }
  6098. }
  6099. //---------------------------------------------------------------------------
  6100. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6101. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6102. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6103. {
  6104. if (!IsSideLocalBrowser(osRemote) &&
  6105. IsFileControl(DropSourceControl, osRemote) &&
  6106. (FDDExtMapFile != NULL))
  6107. {
  6108. Accept = true;
  6109. }
  6110. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6111. DebugAssert(FDDTargetControl != NULL);
  6112. }
  6113. //---------------------------------------------------------------------------
  6114. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6115. _di_IDataObject DataObj, int KeyState,
  6116. const TPoint & Point, int & Effect, bool & Accept)
  6117. {
  6118. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6119. }
  6120. //---------------------------------------------------------------------------
  6121. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6122. _di_IDataObject DataObj, int KeyState,
  6123. const TPoint & Point, int & Effect, bool & Accept)
  6124. {
  6125. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6126. }
  6127. //---------------------------------------------------------------------------
  6128. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6129. TObject * Sender)
  6130. {
  6131. DebugUsedParam(Sender);
  6132. DebugAssert(FDDTargetControl == Sender);
  6133. FDDTargetControl = NULL;
  6134. }
  6135. //---------------------------------------------------------------------------
  6136. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6137. {
  6138. FileControlDDDragLeave(SessionsPageControl);
  6139. }
  6140. //---------------------------------------------------------------------------
  6141. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6142. {
  6143. FileControlDDDragLeave(QueueView3);
  6144. }
  6145. //---------------------------------------------------------------------------
  6146. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6147. {
  6148. DebugAssert(!IsSideLocalBrowser(Side));
  6149. DebugUsedParam(Side);
  6150. bool Edit = false;
  6151. TRemoteFile * File = NULL;
  6152. UnicodeString FileName;
  6153. UnicodeString PointTo;
  6154. bool SymbolicLink = true;
  6155. if (RemoteDirView->ItemFocused)
  6156. {
  6157. DebugAssert(RemoteDirView->ItemFocused->Data);
  6158. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6159. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6160. if (Edit)
  6161. {
  6162. FileName = File->FileName;
  6163. PointTo = File->LinkTo;
  6164. }
  6165. else
  6166. {
  6167. PointTo = File->FileName;
  6168. }
  6169. }
  6170. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6171. Terminal->IsCapable[fcHardLink]))
  6172. {
  6173. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6174. if (Edit)
  6175. {
  6176. DebugAssert(File->FileName == FileName);
  6177. int Params = dfNoRecursive;
  6178. Terminal->ExceptionOnFail = true;
  6179. try
  6180. {
  6181. Terminal->DeleteFile(L"", File, &Params);
  6182. }
  6183. __finally
  6184. {
  6185. Terminal->ExceptionOnFail = false;
  6186. }
  6187. }
  6188. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6189. }
  6190. }
  6191. //---------------------------------------------------------------------------
  6192. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6193. {
  6194. return
  6195. (IsSideLocalBrowser(Side) ||
  6196. (HasActiveTerminal() &&
  6197. Terminal->ResolvingSymlinks &&
  6198. Terminal->IsCapable[fcSymbolicLink]));
  6199. }
  6200. //---------------------------------------------------------------------------
  6201. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6202. {
  6203. return
  6204. !IsSideLocalBrowser(FCurrentSide) &&
  6205. (RemoteDirView->ItemFocused != NULL) &&
  6206. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6207. Terminal->SessionData->ResolveSymlinks;
  6208. }
  6209. //---------------------------------------------------------------------------
  6210. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6211. {
  6212. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6213. FForceExecution = true;
  6214. try
  6215. {
  6216. DirView(osCurrent)->ExecuteCurrentFile();
  6217. }
  6218. __finally
  6219. {
  6220. FForceExecution = false;
  6221. }
  6222. }
  6223. //---------------------------------------------------------------------------
  6224. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6225. {
  6226. TEditorData ExternalEditor;
  6227. ExternalEditor.Editor = edExternal;
  6228. bool Remember = false;
  6229. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6230. {
  6231. if (Remember)
  6232. {
  6233. TEditorList * EditorList = new TEditorList;
  6234. try
  6235. {
  6236. *EditorList = *WinConfiguration->EditorList;
  6237. bool Found = false;
  6238. int i = 0;
  6239. while (!Found && (i < EditorList->Count))
  6240. {
  6241. const TEditorPreferences * Editor = EditorList->Editors[i];
  6242. if ((Editor->Data->Editor == edExternal) &&
  6243. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6244. {
  6245. Found = true;
  6246. }
  6247. i++;
  6248. }
  6249. if (!Found)
  6250. {
  6251. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6252. WinConfiguration->EditorList = EditorList;
  6253. }
  6254. }
  6255. __finally
  6256. {
  6257. delete EditorList;
  6258. }
  6259. }
  6260. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6261. }
  6262. }
  6263. //---------------------------------------------------------------------------
  6264. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6265. {
  6266. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6267. if (FFileFindTerminal == ATerminal)
  6268. {
  6269. FFileFindTerminal = NULL;
  6270. HideFileFindDialog();
  6271. }
  6272. if (FClipboardTerminal == ATerminal)
  6273. {
  6274. ClipboardClear(); // implies ClipboardStop
  6275. }
  6276. if ((FOnSynchronizeAbort != NULL) &&
  6277. (ATerminal == FTerminal))
  6278. {
  6279. FOnSynchronizeAbort(NULL);
  6280. }
  6281. }
  6282. //---------------------------------------------------------------------------
  6283. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6284. {
  6285. DetachTerminal(Sender);
  6286. }
  6287. //---------------------------------------------------------------------------
  6288. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6289. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6290. {
  6291. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6292. DebugAssert(Terminal != NULL);
  6293. if (Data->Terminal == Terminal)
  6294. {
  6295. Data->Terminal = NULL;
  6296. }
  6297. }
  6298. //---------------------------------------------------------------------------
  6299. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6300. {
  6301. UpdateControls();
  6302. SessionColor = TColor(0);
  6303. UpdateRemotePathComboBox(false);
  6304. NeedSession(false);
  6305. }
  6306. //---------------------------------------------------------------------------
  6307. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6308. {
  6309. try
  6310. {
  6311. // Cache, as the login dialog can change its value
  6312. // See also DoShow. Also see TTerminalManager::NewSession.
  6313. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6314. try
  6315. {
  6316. if (ShowLogin)
  6317. {
  6318. bool ReloadSessions = !Startup;
  6319. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this);
  6320. }
  6321. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6322. // This detects if workspace was saved the last time the main widow was closed
  6323. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6324. {
  6325. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6326. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6327. }
  6328. }
  6329. __finally
  6330. {
  6331. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6332. // (so there was no chance for the user to open any session yet)
  6333. if ((ShowLogin || !Startup) &&
  6334. !WinConfiguration->KeepOpenWhenNoSession &&
  6335. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6336. {
  6337. TerminateApplication();
  6338. }
  6339. }
  6340. }
  6341. catch (EAbort &)
  6342. {
  6343. // swallow
  6344. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6345. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6346. }
  6347. }
  6348. //---------------------------------------------------------------------------
  6349. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6350. {
  6351. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6352. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6353. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6354. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6355. try
  6356. {
  6357. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6358. // Clear the design time unthemed tab
  6359. ((SessionsPageControl->PageCount > 0) &&
  6360. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6361. {
  6362. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6363. }
  6364. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6365. {
  6366. TThemeTabSheet * TabSheet;
  6367. if (Index >= SessionsPageControl->PageCount)
  6368. {
  6369. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6370. TabSheet->PageControl = SessionsPageControl;
  6371. }
  6372. else
  6373. {
  6374. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6375. }
  6376. bool IsSessionTab = (Index < TerminalList->Count);
  6377. if (IsSessionTab)
  6378. {
  6379. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6380. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6381. UpdateSessionTab(TabSheet);
  6382. }
  6383. else
  6384. {
  6385. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6386. TabSheet->Tag = 0; // not really needed
  6387. TabSheet->Shadowed = false;
  6388. // We know that we are at the last page, otherwise we could not call this (it assumes that new session tab is the last one)
  6389. UpdateNewSessionTab();
  6390. }
  6391. TabSheet->ShowCloseButton = IsSessionTab;
  6392. }
  6393. }
  6394. __finally
  6395. {
  6396. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6397. }
  6398. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6399. }
  6400. //---------------------------------------------------------------------------
  6401. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6402. {
  6403. DoTerminalListChanged();
  6404. }
  6405. //---------------------------------------------------------------------------
  6406. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6407. {
  6408. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6409. DebugAssert(TabSheet->ImageIndex == 0);
  6410. TabSheet->Caption =
  6411. WinConfiguration->SelectiveToolbarText ?
  6412. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6413. UnicodeString();
  6414. }
  6415. //---------------------------------------------------------------------------
  6416. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6417. {
  6418. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6419. }
  6420. //---------------------------------------------------------------------------
  6421. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6422. {
  6423. if (DebugAlwaysTrue(TabSheet != NULL))
  6424. {
  6425. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6426. if (DebugAlwaysTrue(ATerminal != NULL))
  6427. {
  6428. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6429. TabSheet->ImageIndex = AddSessionColor(Color);
  6430. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6431. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6432. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6433. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6434. {
  6435. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6436. }
  6437. }
  6438. }
  6439. }
  6440. //---------------------------------------------------------------------------
  6441. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6442. {
  6443. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6444. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6445. bool Result = (Terminal != NULL);
  6446. if (Result)
  6447. {
  6448. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6449. }
  6450. else
  6451. {
  6452. try
  6453. {
  6454. NewSession();
  6455. }
  6456. __finally
  6457. {
  6458. DoTerminalListChanged();
  6459. }
  6460. FSessionsPageControlNewSessionTime = Now();
  6461. }
  6462. return Result;
  6463. }
  6464. //---------------------------------------------------------------------------
  6465. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6466. {
  6467. SessionTabSwitched();
  6468. }
  6469. //---------------------------------------------------------------------------
  6470. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6471. {
  6472. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6473. DebugAssert(TransferList != NULL);
  6474. UnicodeString Name;
  6475. if (TransferList->ItemIndex <= 0)
  6476. {
  6477. Name = L"";
  6478. }
  6479. else
  6480. {
  6481. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6482. }
  6483. if (FCopyParamAutoSelected.IsEmpty())
  6484. {
  6485. // if previous preset was not autoselected, make new preset the "default"
  6486. FCopyParamDefault = Name;
  6487. }
  6488. GUIConfiguration->CopyParamCurrent = Name;
  6489. }
  6490. //---------------------------------------------------------------------------
  6491. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6492. {
  6493. // sanity check
  6494. bool ExistingPreset =
  6495. (FTransferListHoverIndex >= 0) &&
  6496. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6497. DebugAssert(ExistingPreset);
  6498. if (ExistingPreset)
  6499. {
  6500. HDC DC = GetDC(0);
  6501. TCanvas * Canvas = new TCanvas();
  6502. try
  6503. {
  6504. Canvas->Handle = DC;
  6505. Canvas->Font = GetToolbarFont(this);
  6506. UnicodeString Name;
  6507. if (FTransferListHoverIndex == 0)
  6508. {
  6509. Name = L"";
  6510. }
  6511. else
  6512. {
  6513. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6514. }
  6515. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6516. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6517. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6518. static_cast<TObject*>(GetComponent(fcTransferList)));
  6519. UnicodeString InfoStr =
  6520. GUIConfiguration->CopyParamPreset[Name].
  6521. GetInfoStr(L"; ",
  6522. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6523. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6524. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6525. {
  6526. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6527. {
  6528. InfoStr.SetLength(InfoStr.Length() - 1);
  6529. }
  6530. InfoStr += Ellipsis;
  6531. }
  6532. // UpdateCaption does not cause invalidation of whole submenu, while
  6533. // setting Caption property does.
  6534. // also it probably does not resize the label, even if necessary
  6535. // (we do not want that anyway)
  6536. TransferLabel->UpdateCaption(InfoStr);
  6537. }
  6538. __finally
  6539. {
  6540. Canvas->Handle = NULL;
  6541. ReleaseDC(0, DC);
  6542. delete Canvas;
  6543. }
  6544. }
  6545. }
  6546. //---------------------------------------------------------------------------
  6547. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6548. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6549. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6550. {
  6551. if (FTransferListHoverIndex != AHoverIndex)
  6552. {
  6553. FTransferListHoverIndex = AHoverIndex;
  6554. UpdateTransferLabel();
  6555. }
  6556. }
  6557. //---------------------------------------------------------------------------
  6558. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6559. {
  6560. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6561. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6562. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6563. {
  6564. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6565. {
  6566. if (!NonVisualDataModule->Busy)
  6567. {
  6568. OpenDirectory(GetSide(osCurrent));
  6569. }
  6570. Message.Result = 1;
  6571. }
  6572. else
  6573. {
  6574. TForm::Dispatch(&Message);
  6575. }
  6576. }
  6577. else
  6578. {
  6579. TForm::Dispatch(&Message);
  6580. }
  6581. }
  6582. //---------------------------------------------------------------------------
  6583. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6584. {
  6585. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6586. if ((FIgnoreNextDialogChar != 0) &&
  6587. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6588. {
  6589. Message.Result = 1;
  6590. }
  6591. else
  6592. {
  6593. TForm::Dispatch(&Message);
  6594. }
  6595. FIgnoreNextDialogChar = 0;
  6596. }
  6597. //---------------------------------------------------------------------------
  6598. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6599. {
  6600. // The four low-order bits are used internally by Windows
  6601. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6602. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6603. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6604. // 61730 - restore thru double click - undocumented
  6605. // 61490 - maximize thru double click - undocumented
  6606. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6607. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6608. {
  6609. SysResizing(Cmd);
  6610. }
  6611. TForm::Dispatch(&Message);
  6612. }
  6613. //---------------------------------------------------------------------------
  6614. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6615. {
  6616. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6617. // Also there no point returning TRUE as we are not able to
  6618. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6619. // Hence the process termination might be safer :)
  6620. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6621. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6622. {
  6623. Message.Result = FALSE;
  6624. }
  6625. else
  6626. {
  6627. Message.Result = TRUE;
  6628. }
  6629. // Do not call default handling as that triggers OnCloseQuery,
  6630. // where our implementation will popup confirmation dialogs, what we do not want,
  6631. // as per Vista guidelines:
  6632. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6633. }
  6634. //---------------------------------------------------------------------------
  6635. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6636. {
  6637. if (Message.EndSession && IsApplicationMinimized())
  6638. {
  6639. // WORKAROUND
  6640. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6641. // when it receives WM_ENDSESSION,
  6642. // but that sometimes (particularly when application is minimized) cause crashes.
  6643. // Crash popups message that blocks log off.
  6644. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6645. // so we call ExitProcess() immediately, not even trying to cleanup.
  6646. // It still causes beep, so there's likely some popup, but it does not block
  6647. // log off.
  6648. ExitProcess(0);
  6649. }
  6650. TForm::Dispatch(&Message);
  6651. }
  6652. #ifdef _DEBUG
  6653. //---------------------------------------------------------------------------
  6654. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6655. {
  6656. TForm::Dispatch(&Message);
  6657. }
  6658. #endif
  6659. //---------------------------------------------------------------------------
  6660. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6661. {
  6662. }
  6663. //---------------------------------------------------------------------------
  6664. void __fastcall TCustomScpExplorerForm::DoShow()
  6665. {
  6666. // only now are the controls resized finally, so the size constraints
  6667. // will not conflict with possibly very small window size
  6668. RestoreFormParams();
  6669. // This indicates that the form was rescaled after parameters were loaded and
  6670. // as most probably the parameters were actually saved with the current form scaling
  6671. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6672. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6673. // and no rounding problems will occur.
  6674. // See also RestoreForm.
  6675. // (we should be safe to call RestoreParams unconditionally)
  6676. if (PixelsPerInch != Screen->PixelsPerInch)
  6677. {
  6678. RestoreParams();
  6679. }
  6680. FixControlsPlacement();
  6681. if (Position == poDefaultPosOnly)
  6682. {
  6683. CutFormToDesktop(this);
  6684. }
  6685. TForm::DoShow();
  6686. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6687. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6688. if (Terminal == NULL)
  6689. {
  6690. StartingDisconnected();
  6691. }
  6692. UpdatePixelsPerInchMainWindowCounter();
  6693. FShowing = true;
  6694. }
  6695. //---------------------------------------------------------------------------
  6696. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6697. {
  6698. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6699. }
  6700. //---------------------------------------------------------------------------
  6701. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6702. {
  6703. DoTerminalListChanged();
  6704. }
  6705. //---------------------------------------------------------------------------
  6706. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6707. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6708. TNotifyEvent OnBalloonClick, TObject * UserData)
  6709. {
  6710. bool Do;
  6711. UnicodeString Message;
  6712. if (E == NULL)
  6713. {
  6714. Message = Str;
  6715. Do = true;
  6716. }
  6717. else
  6718. {
  6719. Do = ExceptionMessage(E, Message);
  6720. }
  6721. if (Do && WinConfiguration->BalloonNotifications)
  6722. {
  6723. UnicodeString Title;
  6724. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6725. {
  6726. Message = TrimLeft(Message);
  6727. }
  6728. else
  6729. {
  6730. Message = UnformatMessage(Message);
  6731. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6732. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6733. Title = LoadResourceString(Captions[Type]);
  6734. if (Terminal != NULL)
  6735. {
  6736. Title = FORMAT(L"%s - %s",
  6737. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6738. }
  6739. }
  6740. if (Seconds == 0)
  6741. {
  6742. Seconds = WinConfiguration->NotificationsTimeout;
  6743. }
  6744. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6745. }
  6746. }
  6747. //---------------------------------------------------------------------------
  6748. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6749. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6750. UnicodeString HelpKeyword, const TMessageParams * Params,
  6751. TTerminal * Terminal)
  6752. {
  6753. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6754. (FOnFeedSynchronizeError != NULL)) &&
  6755. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6756. {
  6757. if (FOnFeedSynchronizeError != NULL)
  6758. {
  6759. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6760. }
  6761. else
  6762. {
  6763. DebugAssert(FErrorList != NULL);
  6764. TStringList * MoreMessagesCopy = NULL;
  6765. if (MoreMessages)
  6766. {
  6767. MoreMessagesCopy = new TStringList();
  6768. MoreMessagesCopy->Assign(MoreMessages);
  6769. }
  6770. FErrorList->AddObject(Message, MoreMessagesCopy);
  6771. }
  6772. PopupTrayBalloon(Terminal, Message, Type);
  6773. return ContinueAnswer(Answers);
  6774. }
  6775. else
  6776. {
  6777. bool UseBalloon = FTrayIcon->Visible;
  6778. if (UseBalloon)
  6779. {
  6780. PopupTrayBalloon(Terminal, Message, Type);
  6781. }
  6782. unsigned int Result;
  6783. try
  6784. {
  6785. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6786. }
  6787. __finally
  6788. {
  6789. // cancel only balloon we popped up, otherwise we may cancel notification
  6790. // balloon that was there before the message dialog
  6791. // (such as "dd confirmation opt in balloon)
  6792. if (UseBalloon)
  6793. {
  6794. FTrayIcon->CancelBalloon();
  6795. }
  6796. }
  6797. return Result;
  6798. }
  6799. }
  6800. //---------------------------------------------------------------------------
  6801. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6802. TTerminal * Terminal, Exception * E)
  6803. {
  6804. if (FTrayIcon->Visible)
  6805. {
  6806. PopupTrayBalloon(Terminal, L"", qtError, E);
  6807. }
  6808. // particularly prevent opening new session from jump list,
  6809. // while exception is shown
  6810. NonVisualDataModule->StartBusy();
  6811. try
  6812. {
  6813. ShowExtendedExceptionEx(Terminal, E);
  6814. }
  6815. __finally
  6816. {
  6817. NonVisualDataModule->EndBusy();
  6818. FTrayIcon->CancelBalloon();
  6819. }
  6820. }
  6821. //---------------------------------------------------------------------------
  6822. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6823. {
  6824. if (Terminal->Active)
  6825. {
  6826. InitStatusBar();
  6827. }
  6828. // cannot rely on active page being page for active terminal,
  6829. // as it can happen that active page is the "new session" page
  6830. // (e.g. when reconnecting active terminal, while login dialog
  6831. // invoked from "new session" page is modal)
  6832. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6833. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6834. }
  6835. //---------------------------------------------------------------------------
  6836. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6837. TTerminal * Terminal, Exception * E)
  6838. {
  6839. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6840. if (!Terminal->Active)
  6841. {
  6842. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6843. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6844. {
  6845. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6846. }
  6847. }
  6848. }
  6849. //---------------------------------------------------------------------------
  6850. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6851. UnicodeString Message, TQueryType Type,
  6852. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6853. {
  6854. if ((E == NULL) ||
  6855. ExceptionMessage(E, Message))
  6856. {
  6857. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6858. if (Important)
  6859. {
  6860. Seconds *= 5;
  6861. }
  6862. UnicodeString NoteMessage(UnformatMessage(Message));
  6863. if (Terminal != NULL)
  6864. {
  6865. NoteMessage = FORMAT(L"%s: %s",
  6866. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6867. }
  6868. if (WinConfiguration->BalloonNotifications)
  6869. {
  6870. AddNote(NoteMessage);
  6871. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6872. }
  6873. else
  6874. {
  6875. FlashOnBackground();
  6876. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6877. }
  6878. }
  6879. }
  6880. //---------------------------------------------------------------------------
  6881. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6882. {
  6883. RestoreApp();
  6884. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6885. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6886. !NonVisualDataModule->Busy)
  6887. {
  6888. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6889. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6890. if (!ComponentVisible[fcQueueView])
  6891. {
  6892. ToggleQueueVisibility();
  6893. GoToQueue();
  6894. }
  6895. }
  6896. }
  6897. //---------------------------------------------------------------------------
  6898. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6899. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6900. {
  6901. UnicodeString Message;
  6902. TNotifyEvent OnClick = NULL;
  6903. TObject * UserData = NULL;
  6904. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6905. switch (Event)
  6906. {
  6907. case qeEmptyButMonitored:
  6908. if ((ATerminal != Terminal) || QueueInvisible)
  6909. {
  6910. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6911. OnClick = QueueEmptyNoteClicked;
  6912. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6913. TerminalNoteData->Terminal = ATerminal;
  6914. UserData = TerminalNoteData;
  6915. }
  6916. break;
  6917. case qeEmpty:
  6918. OperationComplete(ATerminal->QueueOperationStart);
  6919. break;
  6920. case qePendingUserAction:
  6921. if ((ATerminal != Terminal) ||
  6922. (QueueInvisible && !IsQueueAutoPopup()))
  6923. {
  6924. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6925. }
  6926. break;
  6927. default:
  6928. DebugFail();
  6929. }
  6930. if (!Message.IsEmpty())
  6931. {
  6932. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6933. }
  6934. }
  6935. //---------------------------------------------------------------------------
  6936. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6937. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6938. {
  6939. if (FDDExtMapFile != NULL)
  6940. {
  6941. CloseHandle(FDDExtMapFile);
  6942. FDDExtMapFile = NULL;
  6943. }
  6944. if (WinConfiguration->DDFakeFile)
  6945. {
  6946. DDFakeFileInitDrag(FileList, Created);
  6947. }
  6948. }
  6949. //---------------------------------------------------------------------------
  6950. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6951. {
  6952. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6953. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6954. {
  6955. FFakeFileDropTarget = FileName;
  6956. }
  6957. }
  6958. //---------------------------------------------------------------------------
  6959. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6960. {
  6961. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6962. if (!ForceDirectories(ApiPath(Result)))
  6963. {
  6964. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6965. }
  6966. FileSetAttr(ApiPath(Result), faHidden);
  6967. return Result;
  6968. }
  6969. //---------------------------------------------------------------------------
  6970. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6971. bool & Created)
  6972. {
  6973. FFakeFileDropTarget = UnicodeString();
  6974. FDragFakeDirectory = CreateFakeTransferDirectory();
  6975. FileList->AddItem(NULL, FDragFakeDirectory);
  6976. Created = true;
  6977. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6978. {
  6979. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6980. }
  6981. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6982. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6983. {
  6984. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6985. TDragExtCommStruct* CommStruct;
  6986. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6987. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6988. DebugAssert(CommStruct != NULL);
  6989. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6990. CommStruct->Dragging = true;
  6991. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6992. LENOF(CommStruct->DropDest));
  6993. NULL_TERMINATE(CommStruct->DropDest);
  6994. UnmapViewOfFile(CommStruct);
  6995. }
  6996. }
  6997. //---------------------------------------------------------------------------
  6998. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6999. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7000. {
  7001. bool Result;
  7002. DebugAssert(!IsLocalBrowserMode());
  7003. if (Sender == RemoteDirView)
  7004. {
  7005. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7006. }
  7007. else
  7008. {
  7009. DebugAssert(Sender == RemoteDriveView);
  7010. TStrings * FileList = RemoteDriveView->DragFileList();
  7011. try
  7012. {
  7013. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7014. }
  7015. __finally
  7016. {
  7017. delete FileList;
  7018. }
  7019. }
  7020. if (FDDTargetControl == RemoteDriveView)
  7021. {
  7022. RemoteDriveView->UpdateDropTarget();
  7023. }
  7024. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7025. // or from tree view to dir view.
  7026. // foMove happens when file/dir is dragged from remote tree view outside of
  7027. // application via dragex
  7028. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7029. (DropSourceControl == RemoteDriveView))
  7030. {
  7031. RemoteDriveView->UpdateDropSource();
  7032. }
  7033. return Result;
  7034. }
  7035. //---------------------------------------------------------------------------
  7036. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7037. {
  7038. DebugAssert(!IsLocalBrowserMode());
  7039. // This also handles drops of remote files to queue.
  7040. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7041. SAFE_DESTROY(FDDFileList);
  7042. if (!FFakeFileDropTarget.IsEmpty())
  7043. {
  7044. RemoveDir(ApiPath(FFakeFileDropTarget));
  7045. }
  7046. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7047. {
  7048. try
  7049. {
  7050. TDragResult DDResult = (Sender == RemoteDirView) ?
  7051. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7052. // Focus is moved to the target application,
  7053. // but as we are going to present the UI, we need to steal the focus back.
  7054. // This most likely won't work though (windows does not allow application
  7055. // to steal focus most of the time)
  7056. Application->BringToFront();
  7057. // On older version of Windows we never got drMove here, see also comment below.
  7058. // On Windows 10, we get the "move".
  7059. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7060. {
  7061. UnicodeString TargetDirectory;
  7062. TFileOperation Operation;
  7063. // drInvalid may mean drMove, see comment below
  7064. switch (DDResult)
  7065. {
  7066. case drCopy:
  7067. Operation = foCopy;
  7068. break;
  7069. case drMove:
  7070. Operation = foMove;
  7071. break;
  7072. default:
  7073. DebugFail();
  7074. case drInvalid:
  7075. // prefer "copy" for safety
  7076. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7077. break;
  7078. }
  7079. TTransferOperationParam Param;
  7080. UnicodeString CounterName;
  7081. bool ForceQueue;
  7082. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7083. {
  7084. // we get drInvalid both if d&d was intercepted by ddext,
  7085. // and when users drops on no-drop location.
  7086. // we tell the difference by existence of response from ddext,
  7087. // so we ignore absence of response in this case
  7088. if (DDResult != drInvalid)
  7089. {
  7090. // here we know that the extension is installed,
  7091. // as it is checked as soon as drag&drop starts from
  7092. // RemoteFileControlDDCreateDragFileList
  7093. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7094. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7095. {
  7096. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7097. }
  7098. else
  7099. {
  7100. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7101. }
  7102. }
  7103. }
  7104. else
  7105. {
  7106. // download using fake file
  7107. Param.Temp = false;
  7108. Param.DragDrop = true;
  7109. if (ForceQueue)
  7110. {
  7111. Param.Queue = asOn;
  7112. }
  7113. if (Sender == RemoteDirView)
  7114. {
  7115. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7116. }
  7117. if (RemoteFileControlFileOperation(Sender, Operation,
  7118. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7119. {
  7120. Configuration->Usage->Inc(CounterName);
  7121. }
  7122. }
  7123. }
  7124. }
  7125. __finally
  7126. {
  7127. CloseHandle(FDDExtMapFile);
  7128. FDDExtMapFile = NULL;
  7129. delete FDragFakeMonitors;
  7130. FDragFakeMonitors = NULL;
  7131. RemoveDir(ApiPath(FDragFakeDirectory));
  7132. FDragFakeDirectory = L"";
  7133. FFakeFileDropTarget = UnicodeString();
  7134. }
  7135. }
  7136. if (FDragDropSshTerminate.get() != NULL)
  7137. {
  7138. FDragDropSshTerminate->Rethrow();
  7139. }
  7140. }
  7141. //---------------------------------------------------------------------------
  7142. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7143. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7144. {
  7145. // Remember drop effect so we know (when user drops files), if we copy or move
  7146. FLastDropEffect = dwEffect;
  7147. }
  7148. //---------------------------------------------------------------------------
  7149. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7150. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7151. {
  7152. bool Result;
  7153. if (FDDTargetControl == QueueView3)
  7154. {
  7155. Directory = DefaultDownloadTargetDirectory();
  7156. Result = true;
  7157. CounterName = L"DownloadsDragDropQueue";
  7158. ForceQueue = true;
  7159. }
  7160. else if (!FFakeFileDropTarget.IsEmpty())
  7161. {
  7162. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7163. Result = true;
  7164. ForceQueue = false;
  7165. CounterName = L"DownloadsDragDropFakeFile";
  7166. }
  7167. else
  7168. {
  7169. ForceQueue = false;
  7170. Enabled = false;
  7171. try
  7172. {
  7173. int Timer = 0;
  7174. Result = false;
  7175. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7176. {
  7177. {
  7178. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7179. TDragExtCommStruct* CommStruct;
  7180. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7181. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7182. DebugAssert(CommStruct != NULL);
  7183. Result = !CommStruct->Dragging;
  7184. if (Result)
  7185. {
  7186. Directory = ExtractFilePath(CommStruct->DropDest);
  7187. CounterName = L"DownloadsDragDropExternalExt";
  7188. }
  7189. UnmapViewOfFile(CommStruct);
  7190. }
  7191. if (!Result)
  7192. {
  7193. Sleep(50);
  7194. Timer += 50;
  7195. Application->ProcessMessages();
  7196. }
  7197. }
  7198. }
  7199. __finally
  7200. {
  7201. Enabled = true;
  7202. }
  7203. }
  7204. return Result;
  7205. }
  7206. //---------------------------------------------------------------------------
  7207. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7208. const UnicodeString TempDir, int SecDelay)
  7209. {
  7210. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7211. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7212. if (FDelayedDeletionTimer == NULL)
  7213. {
  7214. DebugAssert(HandleAllocated());
  7215. FDelayedDeletionTimer = new TTimer(this);
  7216. FDelayedDeletionTimer->Interval = 10000;
  7217. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7218. }
  7219. else
  7220. {
  7221. FDelayedDeletionTimer->Enabled = true;
  7222. }
  7223. }
  7224. //---------------------------------------------------------------------------
  7225. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7226. {
  7227. DebugAssert(FDelayedDeletionList != NULL);
  7228. TDateTime N = Now();
  7229. TDateTime Alert;
  7230. UnicodeString Directory;
  7231. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7232. {
  7233. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7234. if ((N >= Alert) || (Sender == NULL))
  7235. {
  7236. Directory = FDelayedDeletionList->Strings[Index];
  7237. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7238. {
  7239. FDelayedDeletionList->Delete(Index);
  7240. }
  7241. }
  7242. }
  7243. if (FDelayedDeletionList->Count == 0)
  7244. {
  7245. FDelayedDeletionTimer->Enabled = false;
  7246. }
  7247. }
  7248. //---------------------------------------------------------------------------
  7249. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7250. {
  7251. DebugAssert(!IsLocalBrowserMode());
  7252. if (IsFileControl(FDDTargetControl, osRemote) ||
  7253. (FDDTargetControl == SessionsPageControl))
  7254. {
  7255. TTerminal * TargetTerminal = NULL;
  7256. TFileOperation Operation = foNone;
  7257. if (FDDTargetControl == SessionsPageControl)
  7258. {
  7259. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7260. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7261. // do not allow dropping on the "+" and disconnected tabs
  7262. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7263. if (IsActiveTerminal(TargetTerminal))
  7264. {
  7265. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7266. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7267. {
  7268. Operation = foRemoteMove;
  7269. }
  7270. else
  7271. {
  7272. Operation = foRemoteCopy;
  7273. }
  7274. }
  7275. }
  7276. else
  7277. {
  7278. if (FLastDropEffect == DROPEFFECT_MOVE)
  7279. {
  7280. Operation = foRemoteMove;
  7281. }
  7282. else if (FLastDropEffect == DROPEFFECT_COPY)
  7283. {
  7284. Operation = foRemoteCopy;
  7285. }
  7286. }
  7287. if (Operation != foNone)
  7288. {
  7289. RemoteFileControlFileOperation(DropSourceControl,
  7290. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7291. }
  7292. // abort drag&drop
  7293. Abort();
  7294. }
  7295. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7296. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7297. (FDDTargetControl != QueueView3))
  7298. {
  7299. DebugAssert(!FDragTempDir.IsEmpty());
  7300. TTransferType Type;
  7301. UnicodeString TempDir = FDragTempDir;
  7302. // We clear FDragTempDir before calling
  7303. // just in case it fail (raises exception)
  7304. FDragTempDir = L"";
  7305. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7306. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7307. // empty directory parameter means temp directory -> don't display it!
  7308. UnicodeString TargetDir = L"";
  7309. int Options = 0;
  7310. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7311. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7312. {
  7313. Abort();
  7314. }
  7315. // TargetDir is set when dropped on local file control
  7316. // (this was workaround for legacy dirview event handling, now it should be
  7317. // made prettier)
  7318. if (TargetDir.IsEmpty())
  7319. {
  7320. TargetDir = TempDir;
  7321. if (ForceDirectories(ApiPath(TargetDir)))
  7322. {
  7323. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7324. FPendingTempSpaceWarn = true;
  7325. try
  7326. {
  7327. FDragDropOperation = true;
  7328. int Params = cpTemporary |
  7329. (Type == ttMove ? cpDelete : 0);
  7330. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7331. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7332. }
  7333. __finally
  7334. {
  7335. FDragDropOperation = false;
  7336. FPendingTempSpaceWarn = false;
  7337. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7338. }
  7339. }
  7340. else
  7341. {
  7342. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7343. }
  7344. }
  7345. }
  7346. }
  7347. //---------------------------------------------------------------------------
  7348. void __fastcall TCustomScpExplorerForm::DDDownload(
  7349. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7350. {
  7351. DebugAssert(!IsLocalBrowserMode());
  7352. TAutoBatch AutoBatch(this);
  7353. UpdateCopyParamCounters(*CopyParam);
  7354. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7355. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7356. {
  7357. RemoteDriveView->UpdateDropSource();
  7358. }
  7359. }
  7360. //---------------------------------------------------------------------------
  7361. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7362. {
  7363. public:
  7364. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7365. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7366. {
  7367. }
  7368. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7369. {
  7370. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7371. TDataObjectFilesEx::AllowData(FormatEtc);
  7372. }
  7373. };
  7374. //---------------------------------------------------------------------------
  7375. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7376. TObject * Sender, TDataObject *& DataObject)
  7377. {
  7378. if (FDDExtMapFile != NULL)
  7379. {
  7380. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7381. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7382. {
  7383. Abort();
  7384. }
  7385. if (FileList->Count > 0)
  7386. {
  7387. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7388. if (!FilesObject->IsValid(true, true))
  7389. {
  7390. FilesObject->_Release();
  7391. }
  7392. else
  7393. {
  7394. DataObject = FilesObject;
  7395. }
  7396. }
  7397. }
  7398. }
  7399. //---------------------------------------------------------------------------
  7400. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7401. {
  7402. DebugFail();
  7403. }
  7404. //---------------------------------------------------------------------------
  7405. void __fastcall TCustomScpExplorerForm::GoToTree()
  7406. {
  7407. ComponentVisible[fcRemoteTree] = true;
  7408. DriveView(osRemote)->SetFocus();
  7409. }
  7410. //---------------------------------------------------------------------------
  7411. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7412. TPanelExport Export)
  7413. {
  7414. TCustomDirView * DirView = this->DirView(Side);
  7415. std::unique_ptr<TStrings> ExportData(new TStringList());
  7416. switch (Export)
  7417. {
  7418. case pePath:
  7419. ExportData->Add(DirView->PathName);
  7420. break;
  7421. case peFileList:
  7422. case peFullFileList:
  7423. {
  7424. bool FullPath = (Export == peFullFileList);
  7425. DirView->CreateFileList(false, FullPath, ExportData.get());
  7426. for (int Index = 0; Index < ExportData->Count; Index++)
  7427. {
  7428. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7429. {
  7430. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7431. }
  7432. }
  7433. }
  7434. break;
  7435. default:
  7436. DebugFail();
  7437. }
  7438. return ExportData.release();
  7439. }
  7440. //---------------------------------------------------------------------------
  7441. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7442. TPanelExport Export, TPanelExportDestination Destination)
  7443. {
  7444. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7445. PanelExportStore(Side, Export, Destination, ExportData.get());
  7446. }
  7447. //---------------------------------------------------------------------------
  7448. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7449. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7450. TStrings * ExportData)
  7451. {
  7452. if (Destination == pedClipboard)
  7453. {
  7454. TInstantOperationVisualizer Visualizer;
  7455. CopyToClipboard(ExportData);
  7456. }
  7457. else
  7458. {
  7459. DebugFail();
  7460. }
  7461. }
  7462. //---------------------------------------------------------------------------
  7463. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7464. {
  7465. TCustomDirView * DirView = this->DirView(Side);
  7466. UnicodeString Mask = DirView->Mask;
  7467. if (DoFilterMaskDialog(DirView, Mask))
  7468. {
  7469. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7470. Configuration->Usage->Inc(L"Filters");
  7471. }
  7472. }
  7473. //---------------------------------------------------------------------------
  7474. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7475. {
  7476. return FQueueController->DefaultOperation();
  7477. }
  7478. //---------------------------------------------------------------------------
  7479. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7480. TQueueOperation Operation, void ** Param)
  7481. {
  7482. switch (Operation)
  7483. {
  7484. case qoPreferences:
  7485. return true;
  7486. case qoGoTo:
  7487. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7488. case qoOnceEmpty:
  7489. return IsAnythingQueued();
  7490. default:
  7491. return FQueueController->AllowOperation(Operation, Param);
  7492. }
  7493. }
  7494. //---------------------------------------------------------------------------
  7495. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7496. {
  7497. if (DebugAlwaysTrue(QueueView3->Visible))
  7498. {
  7499. QueueView3->SetFocus();
  7500. }
  7501. }
  7502. //---------------------------------------------------------------------------
  7503. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7504. TQueueOperation Operation, void * Param)
  7505. {
  7506. if (Operation == qoGoTo)
  7507. {
  7508. GoToQueue();
  7509. }
  7510. else if (Operation == qoPreferences)
  7511. {
  7512. PreferencesDialog(pmQueue);
  7513. }
  7514. else
  7515. {
  7516. FQueueController->ExecuteOperation(Operation, Param);
  7517. }
  7518. }
  7519. //---------------------------------------------------------------------------
  7520. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7521. {
  7522. return (Queue != NULL) && Queue->Enabled;
  7523. }
  7524. //---------------------------------------------------------------------------
  7525. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7526. {
  7527. DebugAssert(Queue != NULL);
  7528. if (Queue != NULL)
  7529. {
  7530. Queue->Enabled = !Queue->Enabled;
  7531. }
  7532. }
  7533. //---------------------------------------------------------------------------
  7534. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7535. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7536. {
  7537. FQueueActedItem = QueueView3->ItemFocused;
  7538. }
  7539. //---------------------------------------------------------------------------
  7540. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7541. {
  7542. return TopDock->Height + QueueSplitter->Height;
  7543. }
  7544. //---------------------------------------------------------------------------
  7545. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7546. {
  7547. return
  7548. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7549. (QueueDock->Visible ? QueueDock->Height : 0) +
  7550. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7551. }
  7552. //---------------------------------------------------------------------------
  7553. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7554. {
  7555. return ScaleByTextHeight(this, 48);
  7556. }
  7557. //---------------------------------------------------------------------------
  7558. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7559. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7560. {
  7561. // when queue is hidden by double-clicking splitter, stray attempt to
  7562. // resize the panel with strange value arrives, make sure it is ignored
  7563. if (ComponentVisible[fcQueueView])
  7564. {
  7565. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7566. if (NewSize > MaxHeightLimit)
  7567. {
  7568. NewSize = MaxHeightLimit;
  7569. }
  7570. int MinHeightLimit =
  7571. GetStaticQueuePanelComponentsHeight() +
  7572. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7573. GetMinQueueViewHeight();
  7574. if (NewSize < MinHeightLimit)
  7575. {
  7576. NewSize = MinHeightLimit;
  7577. }
  7578. }
  7579. else
  7580. {
  7581. Accept = false;
  7582. }
  7583. }
  7584. //---------------------------------------------------------------------------
  7585. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7586. {
  7587. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7588. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7589. if (QueueViewHeight < GetMinQueueViewHeight())
  7590. {
  7591. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7592. }
  7593. }
  7594. //---------------------------------------------------------------------------
  7595. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7596. TDragObject *& /*DragObject*/)
  7597. {
  7598. FQueueActedItem = QueueView3->ItemFocused;
  7599. }
  7600. //---------------------------------------------------------------------------
  7601. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7602. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7603. {
  7604. Accept = true;
  7605. if (Source == QueueView3)
  7606. {
  7607. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7608. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7609. if (Accept)
  7610. {
  7611. TQueueItemProxy * QueueItem;
  7612. TQueueItemProxy * DestQueueItem;
  7613. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7614. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7615. Accept = (QueueItem != DestQueueItem) &&
  7616. (QueueItem->Status == TQueueItem::qsPending) &&
  7617. (DestQueueItem->Status == TQueueItem::qsPending);
  7618. }
  7619. }
  7620. }
  7621. //---------------------------------------------------------------------------
  7622. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7623. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7624. {
  7625. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7626. {
  7627. TQueueItemProxy * QueueItem;
  7628. TQueueItemProxy * DestQueueItem;
  7629. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7630. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7631. QueueItem->Move(DestQueueItem);
  7632. }
  7633. }
  7634. //---------------------------------------------------------------------------
  7635. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7636. {
  7637. if ((QueueView3->ItemFocused == NULL) &&
  7638. (QueueView3->Items->Count > 0))
  7639. {
  7640. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7641. }
  7642. QueueLabelUpdateStatus();
  7643. }
  7644. //---------------------------------------------------------------------------
  7645. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7646. {
  7647. QueueLabelUpdateStatus();
  7648. }
  7649. //---------------------------------------------------------------------------
  7650. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7651. {
  7652. QueueLabel->UpdateStatus();
  7653. }
  7654. //---------------------------------------------------------------------------
  7655. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7656. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7657. {
  7658. if (Selected)
  7659. {
  7660. NonVisualDataModule->UpdateNonVisibleActions();
  7661. }
  7662. }
  7663. //---------------------------------------------------------------------------
  7664. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7665. {
  7666. TDragDropFilesEx * Result = NULL;
  7667. if (Sender == SessionsPageControl)
  7668. {
  7669. Result = FSessionsDragDropFilesEx;
  7670. }
  7671. else if (Sender == QueueView3)
  7672. {
  7673. Result = FQueueDragDropFilesEx;
  7674. }
  7675. else
  7676. {
  7677. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7678. if (DirView != NULL)
  7679. {
  7680. Result = DirView->DragDropFilesEx;
  7681. }
  7682. else
  7683. {
  7684. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7685. if (DriveView != NULL)
  7686. {
  7687. Result = DriveView->DragDropFilesEx;
  7688. }
  7689. }
  7690. }
  7691. DebugAssert(Result != NULL);
  7692. return Result;
  7693. }
  7694. //---------------------------------------------------------------------------
  7695. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7696. {
  7697. return (DView->SelCount == DView->FilesCount);
  7698. }
  7699. //---------------------------------------------------------------------------
  7700. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7701. {
  7702. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7703. }
  7704. //---------------------------------------------------------------------------
  7705. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7706. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7707. {
  7708. DebugAssert(!IsLocalBrowserMode());
  7709. TFileOperation Operation;
  7710. switch (Effect)
  7711. {
  7712. case DROPEFFECT_MOVE:
  7713. Operation = foMove;
  7714. break;
  7715. case DROPEFFECT_COPY:
  7716. // occures on WinXP (reported by user)
  7717. default:
  7718. Operation = foCopy;
  7719. break;
  7720. };
  7721. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7722. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7723. if (DragDropFilesEx->FileList->Count > 0)
  7724. {
  7725. TStrings * FileList = new TStringList();
  7726. try
  7727. {
  7728. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7729. {
  7730. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7731. }
  7732. FDragDropOperation = true;
  7733. TTransferOperationParam Param;
  7734. Param.TargetDirectory = TargetPath;
  7735. // upload, no temp dirs
  7736. Param.Temp = false;
  7737. Param.DragDrop = DragDrop;
  7738. Param.Options =
  7739. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7740. if (ForceQueue)
  7741. {
  7742. Param.Queue = asOn;
  7743. }
  7744. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7745. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7746. {
  7747. if (IsFileControl(DropSourceControl, osLocal))
  7748. {
  7749. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7750. }
  7751. else
  7752. {
  7753. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7754. }
  7755. }
  7756. }
  7757. __finally
  7758. {
  7759. FDragDropOperation = false;
  7760. delete FileList;
  7761. }
  7762. }
  7763. }
  7764. //---------------------------------------------------------------------------
  7765. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7766. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7767. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7768. {
  7769. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7770. }
  7771. //---------------------------------------------------------------------------
  7772. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7773. TObject * Sender, int grfKeyState, int & dwEffect)
  7774. {
  7775. DebugAssert(!IsLocalBrowserMode());
  7776. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7777. if ((dwEffect != DROPEFFECT_NONE) &&
  7778. IsFileControl(DropSourceControl, osRemote))
  7779. {
  7780. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7781. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7782. (RemoteDirView->DropTarget == NULL))
  7783. {
  7784. dwEffect = DROPEFFECT_NONE;
  7785. }
  7786. else
  7787. {
  7788. if (dwEffect == DROPEFFECT_COPY)
  7789. {
  7790. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7791. // currently we support copying always (at least via temporary directory);
  7792. // remove associated checks once this all proves stable and working
  7793. bool CopyCapable = true;
  7794. // if we do not support neither of operations, there's no discussion
  7795. if (!MoveCapable && !CopyCapable)
  7796. {
  7797. dwEffect = DROPEFFECT_NONE;
  7798. }
  7799. else
  7800. {
  7801. // The default effect inside remote panel is move,
  7802. // unless we do not support it, but support copy
  7803. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7804. {
  7805. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7806. }
  7807. else
  7808. {
  7809. // with ctrl-down, we want copy unless it is not supported
  7810. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7811. }
  7812. }
  7813. }
  7814. }
  7815. }
  7816. }
  7817. //---------------------------------------------------------------------------
  7818. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7819. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7820. {
  7821. DebugAssert(!IsLocalBrowserMode());
  7822. if (FDDTotalSize >= 0)
  7823. {
  7824. if (File->IsDirectory)
  7825. {
  7826. FDDTotalSize = -1;
  7827. }
  7828. else
  7829. {
  7830. FDDTotalSize += File->Size;
  7831. }
  7832. }
  7833. DebugAssert(!FDragTempDir.IsEmpty());
  7834. // TODO: this is quite ineffective
  7835. // TODO: what if invalid character replacement is disabled?
  7836. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7837. UnicodeString TransferFileName;
  7838. if (Sender == RemoteDriveView)
  7839. {
  7840. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7841. }
  7842. else
  7843. {
  7844. TransferFileName = File->FileName;
  7845. }
  7846. FDDFileList->AddObject(TransferFileName, File);
  7847. }
  7848. //---------------------------------------------------------------------------
  7849. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7850. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7851. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7852. {
  7853. // sometimes we do not get DDEnd so the list is not released
  7854. SAFE_DESTROY(FDDFileList);
  7855. FDDFileList = new TStringList();
  7856. FDragTempDir = WinConfiguration->TemporaryDir();
  7857. FDDTotalSize = 0;
  7858. FDragDropSshTerminate.reset(NULL);
  7859. }
  7860. //---------------------------------------------------------------------------
  7861. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7862. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7863. HRESULT & Result)
  7864. {
  7865. if (Result == DRAGDROP_S_DROP)
  7866. {
  7867. try
  7868. {
  7869. GlobalDragImageList->HideDragImage();
  7870. try
  7871. {
  7872. RemoteFileControlDDTargetDrop();
  7873. }
  7874. catch(ESshTerminate & E)
  7875. {
  7876. FDragDropSshTerminate.reset(E.Clone());
  7877. }
  7878. }
  7879. catch (Exception &E)
  7880. {
  7881. // If downloading fails we need to cancel drag&drop, otherwise
  7882. // Explorer shows error
  7883. // But by the way exception probably never reach this point as
  7884. // it's catched on way.
  7885. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7886. Result = DRAGDROP_S_CANCEL;
  7887. ShowExtendedException(Terminal, &E);
  7888. }
  7889. }
  7890. }
  7891. //---------------------------------------------------------------------------
  7892. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7893. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7894. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7895. {
  7896. TFileMasks::TParams MaskParams;
  7897. MaskParams.Size = Size;
  7898. MaskParams.Modification = Modification;
  7899. // this does not re-parse the mask if it is the same as the last time
  7900. FDirViewMatchMask = Masks;
  7901. bool ImplicitMatch;
  7902. Matches =
  7903. // RecurseInclude parameter has no effect as the Path is empty
  7904. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7905. (AllowImplicitMatches || !ImplicitMatch);
  7906. }
  7907. //---------------------------------------------------------------------------
  7908. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7909. TObject * Sender, TListItem * Item, WORD & Indexes)
  7910. {
  7911. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7912. UnicodeString Ext;
  7913. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7914. {
  7915. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7916. }
  7917. else
  7918. {
  7919. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7920. }
  7921. if (SameText(Ext, Configuration->PartialExt))
  7922. {
  7923. Indexes |= oiPartial;
  7924. }
  7925. }
  7926. //---------------------------------------------------------------------------
  7927. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7928. {
  7929. return
  7930. DirViewEnabled(osCurrent) &&
  7931. DirView(osCurrent)->CanPasteFromClipBoard();
  7932. }
  7933. //---------------------------------------------------------------------------
  7934. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7935. {
  7936. bool Result = false;
  7937. if (CanPasteToDirViewFromClipBoard())
  7938. {
  7939. Result = true;
  7940. }
  7941. else
  7942. {
  7943. UnicodeString ClipboardText;
  7944. if (NonEmptyTextFromClipboard(ClipboardText) &&
  7945. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  7946. {
  7947. if (StoredSessions->IsUrl(ClipboardText))
  7948. {
  7949. Result = true;
  7950. }
  7951. else
  7952. {
  7953. Result = DirViewEnabled(osCurrent);
  7954. }
  7955. }
  7956. }
  7957. return Result;
  7958. }
  7959. //---------------------------------------------------------------------------
  7960. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7961. {
  7962. if (DoesClipboardContainOurFiles())
  7963. {
  7964. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7965. {
  7966. DebugAssert(!IsLocalBrowserMode());
  7967. TTerminalManager * Manager = TTerminalManager::Instance();
  7968. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  7969. Manager->ActiveTerminal = FClipboardTerminal;
  7970. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  7971. }
  7972. }
  7973. else if (CanPasteToDirViewFromClipBoard())
  7974. {
  7975. DirView(osCurrent)->PasteFromClipBoard();
  7976. }
  7977. else
  7978. {
  7979. UnicodeString ClipboardText;
  7980. if (NonEmptyTextFromClipboard(ClipboardText))
  7981. {
  7982. if (StoredSessions->IsUrl(ClipboardText))
  7983. {
  7984. NewSession(ClipboardText);
  7985. }
  7986. else
  7987. {
  7988. DirView(osCurrent)->Path = ClipboardText;
  7989. }
  7990. }
  7991. }
  7992. }
  7993. //---------------------------------------------------------------------------
  7994. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7995. {
  7996. // TBD
  7997. }
  7998. //---------------------------------------------------------------------------
  7999. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8000. {
  8001. TCustomDirView * ADirView = DirView(Side);
  8002. ADirView->SelectAll(Mode);
  8003. ADirView->SetFocus();
  8004. }
  8005. //---------------------------------------------------------------------------
  8006. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8007. {
  8008. TCustomDirView * ADirView = DirView(Side);
  8009. TFileFilter Filter;
  8010. DefaultFileFilter(Filter);
  8011. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8012. {
  8013. Configuration->Usage->Inc(L"MaskSelections");
  8014. ADirView->SelectFiles(Filter, Select);
  8015. ADirView->SetFocus();
  8016. }
  8017. }
  8018. //---------------------------------------------------------------------------
  8019. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8020. {
  8021. TCustomDirView * ADirView = DirView(Side);
  8022. ADirView->RestoreSelectedNames();
  8023. ADirView->SetFocus();
  8024. }
  8025. //---------------------------------------------------------------------------
  8026. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8027. {
  8028. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8029. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8030. {
  8031. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8032. UnicodeString Ext;
  8033. if (!IsSideLocalBrowser(osCurrent))
  8034. {
  8035. Ext = UnixExtractFileExt(FileName);
  8036. }
  8037. else
  8038. {
  8039. Ext = ExtractFileExt(FileName);
  8040. }
  8041. if (Ext.IsEmpty())
  8042. {
  8043. Ext = L".";
  8044. }
  8045. TFileFilter Filter;
  8046. Filter.Masks = FORMAT(L"*%s", (Ext));
  8047. Filter.Directories = false;
  8048. CurrentDirView->SelectFiles(Filter, Select);
  8049. }
  8050. }
  8051. //---------------------------------------------------------------------------
  8052. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8053. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8054. {
  8055. UnicodeString Result;
  8056. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8057. {
  8058. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8059. }
  8060. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8061. {
  8062. // noop
  8063. }
  8064. else
  8065. {
  8066. Result =
  8067. FMTLOAD(FILE_INFO_FORMAT,
  8068. (FormatBytes(FileInfo.SelectedSize),
  8069. FormatBytes(FileInfo.FilesSize),
  8070. FormatNumber(FileInfo.SelectedCount),
  8071. FormatNumber(FileInfo.FilesCount)));
  8072. }
  8073. return Result;
  8074. }
  8075. //---------------------------------------------------------------------------
  8076. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8077. TTBXStatusPanel * Panel, TOperationSide Side)
  8078. {
  8079. if (Panel->Index == 1)
  8080. {
  8081. ToggleShowHiddenFiles();
  8082. }
  8083. else if (Panel->Index == 2)
  8084. {
  8085. Filter(Side);
  8086. }
  8087. }
  8088. //---------------------------------------------------------------------------
  8089. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8090. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8091. {
  8092. DebugAssert(!StatusBar->SimplePanel);
  8093. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8094. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8095. }
  8096. //---------------------------------------------------------------------------
  8097. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8098. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8099. {
  8100. DebugAssert(StatusBar->Panels->Count >= 3);
  8101. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8102. if (FileInfo.HiddenCount > 0)
  8103. {
  8104. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8105. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8106. }
  8107. else
  8108. {
  8109. HiddenFilesPanel->ViewPriority = 0;
  8110. // not really necessary, just to cleanup no-longer-valid data
  8111. HiddenFilesPanel->Caption = L"";
  8112. }
  8113. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8114. if (FileInfo.FilteredCount > 0)
  8115. {
  8116. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8117. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8118. }
  8119. else
  8120. {
  8121. FilteredFilesPanel->ViewPriority = 0;
  8122. // not really necessary, just to cleanup no-longer-valid data
  8123. FilteredFilesPanel->Caption = L"";
  8124. }
  8125. }
  8126. //---------------------------------------------------------------------------
  8127. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8128. TObject * /*Sender*/)
  8129. {
  8130. if (DirView(osRemote)->Enabled)
  8131. {
  8132. DirView(osRemote)->SetFocus();
  8133. }
  8134. }
  8135. //---------------------------------------------------------------------------
  8136. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8137. {
  8138. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8139. switch (Config.Show)
  8140. {
  8141. case qvShow:
  8142. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8143. {
  8144. Config.Show = qvHide;
  8145. }
  8146. else
  8147. {
  8148. Config.Show = Config.LastHideShow;
  8149. }
  8150. break;
  8151. case qvHideWhenEmpty:
  8152. if (ComponentVisible[fcQueueView])
  8153. {
  8154. Config.Show = qvHide;
  8155. }
  8156. else
  8157. {
  8158. Config.LastHideShow = Config.Show;
  8159. Config.Show = qvShow;
  8160. }
  8161. break;
  8162. case qvHide:
  8163. Config.LastHideShow = Config.Show;
  8164. Config.Show = qvShow;
  8165. break;
  8166. }
  8167. WinConfiguration->QueueView = Config;
  8168. }
  8169. //---------------------------------------------------------------------------
  8170. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8171. {
  8172. UnicodeString Result;
  8173. if (FTerminal != NULL)
  8174. {
  8175. switch (WinConfiguration->PathInCaption)
  8176. {
  8177. case picShort:
  8178. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  8179. break;
  8180. case picFull:
  8181. Result = FTerminal->CurrentDirectory;
  8182. break;
  8183. }
  8184. }
  8185. return Result;
  8186. }
  8187. //---------------------------------------------------------------------------
  8188. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8189. {
  8190. TColor Result = GetWindowColor(FSessionColor);
  8191. return Result;
  8192. }
  8193. //---------------------------------------------------------------------------
  8194. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8195. {
  8196. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8197. return Result;
  8198. }
  8199. //---------------------------------------------------------------------------
  8200. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8201. {
  8202. TTerminalManager::Instance()->UpdateAppTitle();
  8203. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8204. // background to gray on Windows 7 (works on Windows 10).
  8205. // See also EnableControl
  8206. if (Showing)
  8207. {
  8208. if (Terminal != NULL)
  8209. {
  8210. // Update path when it changes
  8211. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  8212. {
  8213. UpdateSessionTab(SessionsPageControl->ActivePage);
  8214. }
  8215. }
  8216. NonVisualDataModule->ReconnectSessionAction->Update();
  8217. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8218. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8219. CenterReconnectToolbar();
  8220. bool HasTerminal = HasActiveTerminal();
  8221. if (HasTerminal)
  8222. {
  8223. if (!RemoteDirView->Enabled)
  8224. {
  8225. RemoteDirView->Enabled = true;
  8226. if (FRemoteDirViewWasFocused)
  8227. {
  8228. ActiveControl = RemoteDirView;
  8229. }
  8230. }
  8231. RemoteDirView->Color = PanelColor();
  8232. }
  8233. else
  8234. {
  8235. if (RemoteDirView->Enabled)
  8236. {
  8237. // This is first called when the form is being constructed
  8238. // (not anymore due to Showing test above)
  8239. // but the false is overriden in the constructor later.
  8240. // An even later in TScpCommanderForm::DoShow()
  8241. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8242. RemoteDirView->Enabled = false;
  8243. }
  8244. RemoteDirView->Color = DisabledPanelColor();
  8245. }
  8246. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8247. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8248. RemoteDriveView->Color = RemoteDirView->Color;
  8249. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8250. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8251. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8252. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8253. QueueFileList->Enabled = QueueView3->Enabled;
  8254. QueueFileList->Color = QueueView3->Color;
  8255. QueueFileList->Font->Color = QueueView3->Font->Color;
  8256. QueueLabelUpdateStatus();
  8257. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8258. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8259. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8260. {
  8261. UpdateDarkMode();
  8262. }
  8263. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8264. }
  8265. }
  8266. //---------------------------------------------------------------------------
  8267. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8268. {
  8269. UpdateControls();
  8270. }
  8271. //---------------------------------------------------------------------------
  8272. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8273. TObject * Sender)
  8274. {
  8275. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8276. DebugAssert(DirView != NULL);
  8277. DoDirViewLoaded(DirView);
  8278. TransferPresetAutoSelect();
  8279. }
  8280. //---------------------------------------------------------------------------
  8281. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8282. {
  8283. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8284. // first run after installation
  8285. if (double(Updates.LastCheck) == 0)
  8286. {
  8287. // make sure next time there will be an update (if enabled)
  8288. Updates.LastCheck = TDateTime(1);
  8289. WinConfiguration->Updates = Updates;
  8290. }
  8291. else if ((double(Updates.Period) > 0) &&
  8292. (Now() - Updates.LastCheck >= Updates.Period))
  8293. {
  8294. TThreadMethod OnUpdatesChecked = NULL;
  8295. if (!IsUWP())
  8296. {
  8297. OnUpdatesChecked = UpdatesChecked;
  8298. }
  8299. StartUpdateThread(OnUpdatesChecked);
  8300. }
  8301. }
  8302. //---------------------------------------------------------------------------
  8303. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8304. {
  8305. UnicodeString Message;
  8306. bool New;
  8307. TQueryType Type;
  8308. GetUpdatesMessage(Message, New, Type, false);
  8309. if (!Message.IsEmpty())
  8310. {
  8311. if (New)
  8312. {
  8313. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8314. }
  8315. if (!New && (Type != qtWarning))
  8316. {
  8317. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8318. }
  8319. else
  8320. {
  8321. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8322. }
  8323. if (New)
  8324. {
  8325. Configuration->Usage->Inc(L"UpdateNotifications");
  8326. }
  8327. }
  8328. }
  8329. //---------------------------------------------------------------------------
  8330. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8331. {
  8332. RestoreApp();
  8333. bool CanDisplay = !NonVisualDataModule->Busy;
  8334. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8335. {
  8336. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8337. }
  8338. if (CanDisplay)
  8339. {
  8340. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8341. CheckForUpdates(true);
  8342. }
  8343. else
  8344. {
  8345. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8346. }
  8347. }
  8348. //---------------------------------------------------------------------------
  8349. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8350. TCopyParamRuleData & Data)
  8351. {
  8352. DebugAssert(Terminal != NULL);
  8353. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8354. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8355. Data.RemoteDirectory = RemoteDirView->PathName;
  8356. }
  8357. //---------------------------------------------------------------------------
  8358. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8359. {
  8360. // Terminal can be null when we are changing local directory implicitly,
  8361. // because it has been deleted, while no session is connected
  8362. // (Login dialog is open)
  8363. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8364. {
  8365. DebugAssert(!IsLocalBrowserMode());
  8366. TCopyParamRuleData Data;
  8367. GetTransferPresetAutoSelectData(Data);
  8368. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8369. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8370. if (CopyParamIndex < 0)
  8371. {
  8372. // there is no preset that matches autoselection
  8373. // set preset that we consider "default"
  8374. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8375. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8376. }
  8377. else
  8378. {
  8379. // there is preset matching autoselection
  8380. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8381. if (CopyParamName == FCopyParamAutoSelected)
  8382. {
  8383. // autoselected the same preset as the last time
  8384. // make no change (i.e. preserve custom user preset, if any)
  8385. }
  8386. else
  8387. {
  8388. // autoselected the different preset then the last time (or there
  8389. // was default preset set)
  8390. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8391. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8392. }
  8393. }
  8394. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8395. {
  8396. if (CopyParamIndex >= 0)
  8397. {
  8398. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8399. }
  8400. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8401. try
  8402. {
  8403. int Fmt =
  8404. (CopyParamIndex < 0) ?
  8405. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8406. COPY_PARAM_AUTOSELECTED;
  8407. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8408. Data->Message = MainInstructions(Message);
  8409. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8410. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8411. CopyParamAttrs);
  8412. if (CopyParamIndex >= 0)
  8413. {
  8414. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8415. Info = FORMAT(L"%s\n \n%s", (Info,
  8416. FMTLOAD(COPY_PARAM_RULE,
  8417. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8418. }
  8419. Data->Message += L"\n\n" + Info;
  8420. if (WinConfiguration->CopyParamAutoSelectNotice)
  8421. {
  8422. TransferPresetNoteMessage(Data, true);
  8423. }
  8424. else
  8425. {
  8426. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8427. Data = NULL; // ownership passed
  8428. }
  8429. }
  8430. __finally
  8431. {
  8432. delete Data;
  8433. }
  8434. }
  8435. }
  8436. }
  8437. //---------------------------------------------------------------------------
  8438. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8439. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8440. {
  8441. DebugAssert(NoteData != NULL);
  8442. DebugAssert(!IsLocalBrowserMode());
  8443. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8444. TQueryButtonAlias Aliases[1];
  8445. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8446. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8447. Params.Aliases = Aliases;
  8448. Params.AliasesCount = LENOF(Aliases);
  8449. unsigned int Result =
  8450. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8451. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8452. switch (Result)
  8453. {
  8454. case qaNeverAskAgain:
  8455. DebugAssert(AllowNeverAskAgain);
  8456. WinConfiguration->CopyParamAutoSelectNotice = false;
  8457. break;
  8458. case qaIgnore:
  8459. PreferencesDialog(pmPresets);
  8460. break;
  8461. }
  8462. }
  8463. //---------------------------------------------------------------------------
  8464. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8465. {
  8466. // as of now useless, as this is used for notes only, never for balloons, ...
  8467. RestoreApp();
  8468. // .. and we should never be busy here
  8469. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8470. {
  8471. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8472. }
  8473. }
  8474. //---------------------------------------------------------------------------
  8475. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8476. TPreferencesMode APreferencesMode)
  8477. {
  8478. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8479. TCopyParamRuleData Data;
  8480. if (Terminal != NULL)
  8481. {
  8482. PreferencesData.reset(new TPreferencesDialogData());
  8483. GetTransferPresetAutoSelectData(Data);
  8484. PreferencesData->CopyParamRuleData = &Data;
  8485. }
  8486. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8487. }
  8488. //---------------------------------------------------------------------------
  8489. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8490. const TCustomCommandType & Command)
  8491. {
  8492. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8493. {
  8494. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8495. }
  8496. }
  8497. //---------------------------------------------------------------------------
  8498. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8499. {
  8500. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8501. TCustomCommandType Command;
  8502. // make sure we use local custom command when remote are not supported
  8503. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8504. {
  8505. Command = FLastCustomCommand;
  8506. }
  8507. else
  8508. {
  8509. Command.Params = Command.Params | ccLocal;
  8510. }
  8511. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8512. FEditingFocusedAdHocCommand = OnFocused;
  8513. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8514. int Options =
  8515. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8516. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8517. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8518. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8519. {
  8520. FLastCustomCommand = Command;
  8521. UpdateCustomCommandsToolbar();
  8522. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8523. }
  8524. }
  8525. //---------------------------------------------------------------------------
  8526. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8527. {
  8528. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8529. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8530. DebugAssert(State > 0);
  8531. if (State <= 0)
  8532. {
  8533. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8534. }
  8535. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8536. }
  8537. //---------------------------------------------------------------------------
  8538. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8539. TCustomCommandType & Command, int & State)
  8540. {
  8541. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8542. if (Result)
  8543. {
  8544. Command = FLastCustomCommand;
  8545. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8546. }
  8547. return Result;
  8548. }
  8549. //---------------------------------------------------------------------------
  8550. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8551. {
  8552. ResetIncrementalSearch();
  8553. }
  8554. //---------------------------------------------------------------------------
  8555. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8556. {
  8557. DebugAssert(ComponentVisible[Component]);
  8558. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8559. }
  8560. //---------------------------------------------------------------------------
  8561. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8562. {
  8563. // when queue panel is resized here directly, the status bar is stretched
  8564. // over whole space the panel occupied
  8565. PostComponentHide(fcQueueView);
  8566. }
  8567. //---------------------------------------------------------------------------
  8568. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8569. {
  8570. ComponentVisible[fcQueueFileList] = false;
  8571. }
  8572. //---------------------------------------------------------------------------
  8573. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8574. {
  8575. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8576. RefreshColorMode();
  8577. ResetSysDarkTheme();
  8578. ConfigurationChanged();
  8579. ConfigureInterface();
  8580. // Should be called for all controls
  8581. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8582. }
  8583. //---------------------------------------------------------------------------
  8584. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8585. {
  8586. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8587. if (!FInvalid &&
  8588. (Message.LParam != 0) &&
  8589. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8590. {
  8591. ThemeChanged();
  8592. }
  8593. TForm::Dispatch(&Message);
  8594. }
  8595. //---------------------------------------------------------------------------
  8596. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8597. {
  8598. TMessage * M = static_cast<TMessage*>(Message);
  8599. switch (M->Msg)
  8600. {
  8601. case CM_DIALOGCHAR:
  8602. CMDialogChar(*M);
  8603. break;
  8604. case WM_APPCOMMAND:
  8605. WMAppCommand(*M);
  8606. break;
  8607. case WM_SYSCOMMAND:
  8608. WMSysCommand(*M);
  8609. break;
  8610. case WM_QUERYENDSESSION:
  8611. WMQueryEndSession(*M);
  8612. break;
  8613. case WM_ENDSESSION:
  8614. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8615. break;
  8616. #ifdef _DEBUG
  8617. case WM_WINDOWPOSCHANGED:
  8618. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8619. break;
  8620. case CM_CUSTOMSTYLECHANGED:
  8621. TForm::Dispatch(Message);
  8622. break;
  8623. #endif
  8624. case WM_COMPONENT_HIDE:
  8625. {
  8626. Byte Component = static_cast<Byte>(M->WParam);
  8627. // sanity check
  8628. if (ComponentVisible[Component])
  8629. {
  8630. // special treatment
  8631. if (Component == fcQueueView)
  8632. {
  8633. ToggleQueueVisibility();
  8634. DebugAssert(!ComponentVisible[fcQueueView]);
  8635. }
  8636. else
  8637. {
  8638. ComponentVisible[Component] = false;
  8639. }
  8640. }
  8641. }
  8642. break;
  8643. case WM_COPYDATA:
  8644. WMCopyData(*M);
  8645. break;
  8646. case WM_MANAGES_CAPTION:
  8647. // caption managed in TTerminalManager::UpdateAppTitle()
  8648. M->Result = 1;
  8649. break;
  8650. case WM_WANTS_MOUSEWHEEL:
  8651. M->Result = 1;
  8652. break;
  8653. case CM_SHOWINGCHANGED:
  8654. CMShowingChanged(*M);
  8655. break;
  8656. case WM_CLOSE:
  8657. WMClose(*M);
  8658. break;
  8659. case WM_DPICHANGED:
  8660. WMDpiChanged(*M);
  8661. break;
  8662. case CM_DPICHANGED:
  8663. CMDpiChanged(*M);
  8664. break;
  8665. case WM_SETTINGCHANGE:
  8666. WMSettingChange(*M);
  8667. break;
  8668. case CM_DIALOGKEY:
  8669. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8670. break;
  8671. default:
  8672. TForm::Dispatch(Message);
  8673. break;
  8674. }
  8675. }
  8676. //---------------------------------------------------------------------------
  8677. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8678. {
  8679. // noop
  8680. }
  8681. //---------------------------------------------------------------------------
  8682. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8683. {
  8684. TForm::Dispatch(&Message);
  8685. FSessionColors->Clear();
  8686. AddFixedSessionImages();
  8687. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8688. UpdateImages();
  8689. }
  8690. //---------------------------------------------------------------------------
  8691. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8692. {
  8693. TForm::Dispatch(&Message);
  8694. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8695. UpdatePixelsPerInchMainWindowCounter();
  8696. }
  8697. //---------------------------------------------------------------------------
  8698. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8699. {
  8700. // Cannot close window while we are busy.
  8701. // We cannot test this in FormCloseQuery as that is called also from
  8702. // Close(), which is called by CloseApplicationAction. So we can be busy
  8703. // there already even, when it is legitimate to close the application.
  8704. // Possibly a better place to handle this would be WMSysCommand.
  8705. if (NonVisualDataModule->Busy)
  8706. {
  8707. Message.Result = 1;
  8708. }
  8709. else
  8710. {
  8711. TForm::Dispatch(&Message);
  8712. }
  8713. }
  8714. //---------------------------------------------------------------------------
  8715. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8716. {
  8717. TForm::Dispatch(&Message);
  8718. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8719. if (Showing && !FStarted)
  8720. {
  8721. FStarted = true;
  8722. InterfaceStarted();
  8723. }
  8724. if (Showing && (Terminal == NULL))
  8725. {
  8726. // When we are starting minimized (i.e. from an installer),
  8727. // postpone showing Login dialog until we get restored.
  8728. // Otherwise the Login dialog (and Authentication window) show restored
  8729. // over invisible (minimized) main window.
  8730. if (WindowState == wsMinimized)
  8731. {
  8732. FNeedSession = true;
  8733. }
  8734. else
  8735. {
  8736. // This happens before application ever goes idle, so the toolbars would
  8737. // stay enabled (initial state) until the Login dialog is dismissed.
  8738. UpdateActions();
  8739. NonVisualDataModule->StartBusy();
  8740. try
  8741. {
  8742. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8743. // otherwise the dialog does not receive focus.
  8744. // With Commander interface the ProcessMessages is called already
  8745. // by TDriveView, but with Explorer interface, we need to call it explicily
  8746. Application->ProcessMessages();
  8747. // do not reload sessions, they have been loaded just now (optimization)
  8748. NeedSession(true);
  8749. }
  8750. __finally
  8751. {
  8752. NonVisualDataModule->EndBusy();
  8753. }
  8754. }
  8755. }
  8756. }
  8757. //---------------------------------------------------------------------------
  8758. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8759. {
  8760. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8761. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8762. }
  8763. //---------------------------------------------------------------------------
  8764. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8765. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8766. int & MaxHeight)
  8767. {
  8768. // workaround for bug in TWinControl.CalcConstraints
  8769. // Check for empty rect (restore from iconic state) is done there only after
  8770. // call to AdjustClientRect, which enlarges the rect (for forms).
  8771. TRect R = GetClientRect();
  8772. // when restoring from iconic state, place no restrictions
  8773. if (IsRectEmpty(R))
  8774. {
  8775. MinWidth = 0;
  8776. MinHeight = 0;
  8777. MaxWidth = 32000;
  8778. MaxHeight = 32000;
  8779. }
  8780. }
  8781. //---------------------------------------------------------------------------
  8782. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8783. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8784. {
  8785. // terminal can be already closed (e.g. dropped connection)
  8786. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8787. {
  8788. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8789. try
  8790. {
  8791. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8792. }
  8793. catch(...)
  8794. {
  8795. if (!Terminal->Active)
  8796. {
  8797. Close = true;
  8798. }
  8799. throw;
  8800. }
  8801. }
  8802. }
  8803. //---------------------------------------------------------------------------
  8804. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8805. {
  8806. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8807. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8808. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8809. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8810. {
  8811. OnGetSpaceAvailable = GetSpaceAvailable;
  8812. }
  8813. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8814. OnGetSpaceAvailable);
  8815. }
  8816. //---------------------------------------------------------------------------
  8817. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8818. {
  8819. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8820. Terminal->FillSessionDataForCode(Data.get());
  8821. return Data.release();
  8822. }
  8823. //---------------------------------------------------------------------------
  8824. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8825. {
  8826. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8827. DoGenerateUrlDialog(Data.get(), Paths);
  8828. }
  8829. //---------------------------------------------------------------------------
  8830. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8831. {
  8832. GenerateUrl(NULL);
  8833. }
  8834. //---------------------------------------------------------------------------
  8835. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8836. {
  8837. std::unique_ptr<TStrings> Paths(new TStringList());
  8838. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8839. GenerateUrl(Paths.get());
  8840. }
  8841. //---------------------------------------------------------------------------
  8842. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8843. {
  8844. FSessionColor = value;
  8845. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8846. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8847. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8848. DebugAssert(ColorItem != NULL);
  8849. ColorItem->Color = C;
  8850. NonVisualDataModule->ColorMenuItem->Color = C;
  8851. UpdateControls();
  8852. }
  8853. //---------------------------------------------------------------------------
  8854. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8855. {
  8856. if (value != FSessionColor)
  8857. {
  8858. UpdateSessionColor(value);
  8859. // Is null when called from LastTerminalClosed
  8860. if (Terminal != NULL)
  8861. {
  8862. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8863. }
  8864. }
  8865. }
  8866. //---------------------------------------------------------------------------
  8867. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8868. {
  8869. bool Result = FNoteTimer->Enabled;
  8870. if (Result)
  8871. {
  8872. // cannot cancel note too early
  8873. bool NotEarly =
  8874. (Now() - FNoteShown >
  8875. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8876. if (Force || NotEarly)
  8877. {
  8878. FNoteTimer->Enabled = false;
  8879. FNote = L"";
  8880. SAFE_DESTROY(FNoteData);
  8881. FOnNoteClick = NULL;
  8882. FNoteHints = FNotes->Text;
  8883. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8884. UpdateStatusBar();
  8885. }
  8886. }
  8887. return Result;
  8888. }
  8889. //---------------------------------------------------------------------------
  8890. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8891. {
  8892. DebugAssert(FNoteTimer->Enabled);
  8893. CancelNote(true);
  8894. }
  8895. //---------------------------------------------------------------------------
  8896. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8897. {
  8898. FNoteHints = FNotes->Text.TrimRight();
  8899. }
  8900. //---------------------------------------------------------------------------
  8901. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8902. {
  8903. int P = Note.Pos(L"\n");
  8904. if (P > 0)
  8905. {
  8906. Note.SetLength(P - 1);
  8907. }
  8908. FNotes->Add(FORMAT(L"[%s] %s",
  8909. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8910. while (FNotes->Count > 10)
  8911. {
  8912. FNotes->Delete(0);
  8913. }
  8914. if (UpdateNow)
  8915. {
  8916. UpdateNoteHints();
  8917. UpdateStatusBar();
  8918. }
  8919. }
  8920. //---------------------------------------------------------------------------
  8921. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8922. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8923. {
  8924. int P = Note.Pos(L"\n");
  8925. if (P > 0)
  8926. {
  8927. Note.SetLength(P - 1);
  8928. }
  8929. UpdateNoteHints();
  8930. FNote = Note;
  8931. SAFE_DESTROY(FNoteData);
  8932. FNoteData = NoteData;
  8933. FOnNoteClick = OnNoteClick;
  8934. AddNote(Note, false);
  8935. UpdateStatusBar();
  8936. FNoteShown = Now();
  8937. FNoteTimer->Enabled = false;
  8938. if (Seconds == 0)
  8939. {
  8940. Seconds = WinConfiguration->NotificationsTimeout;
  8941. }
  8942. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8943. FNoteTimer->Enabled = true;
  8944. }
  8945. //---------------------------------------------------------------------------
  8946. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8947. {
  8948. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8949. }
  8950. //---------------------------------------------------------------------------
  8951. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8952. {
  8953. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8954. {
  8955. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8956. }
  8957. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8958. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8959. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8960. 0, NULL, NULL);
  8961. }
  8962. //---------------------------------------------------------------------------
  8963. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8964. {
  8965. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8966. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8967. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8968. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  8969. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8970. }
  8971. //---------------------------------------------------------------------------
  8972. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8973. {
  8974. WinConfiguration->FormatSizeBytes = Style;
  8975. }
  8976. //---------------------------------------------------------------------------
  8977. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8978. {
  8979. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8980. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8981. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8982. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8983. }
  8984. //---------------------------------------------------------------------------
  8985. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8986. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8987. {
  8988. if (Panel->Index == 0)
  8989. {
  8990. if (FOnNoteClick != NULL)
  8991. {
  8992. // prevent the user data from being freed by possible call
  8993. // to CancelNote or PostNote during call to OnNoteClick
  8994. std::unique_ptr<TObject> NoteData(FNoteData);
  8995. TNotifyEvent OnNoteClick = FOnNoteClick;
  8996. FNoteData = NULL;
  8997. // need to cancel the note as we are going to delete its user data
  8998. CancelNote(true);
  8999. OnNoteClick(NoteData.get());
  9000. }
  9001. }
  9002. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  9003. {
  9004. FileSystemInfo();
  9005. }
  9006. }
  9007. //---------------------------------------------------------------------------
  9008. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9009. {
  9010. // workaround:
  9011. // 1) for unknown reason, disabling window, while minimized,
  9012. // prevents it from restoring, even if it was enabled again meanwhile
  9013. // 2) when disabling the main window, while another has focus
  9014. // minimize is no longer possible
  9015. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9016. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9017. // Shouldn't we use IsApplicationMinimized() here?
  9018. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9019. {
  9020. Enabled = false;
  9021. }
  9022. FLockLevel++;
  9023. }
  9024. //---------------------------------------------------------------------------
  9025. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9026. {
  9027. DebugAssert(FLockLevel > 0);
  9028. FLockLevel--;
  9029. if (FLockLevel == 0)
  9030. {
  9031. DebugAssert(FLockSuspendLevel == 0);
  9032. Enabled = true;
  9033. }
  9034. }
  9035. //---------------------------------------------------------------------------
  9036. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9037. {
  9038. // We need to make sure that window is enabled when the last modal window closes
  9039. // otherwise focus is not restored correctly.
  9040. // So we re-enable the window when modal window opens and
  9041. // disable it back after is closes
  9042. if (FLockLevel > 0)
  9043. {
  9044. // while we have nesting counter, we know that we never be called
  9045. // recursivelly as Application->OnModalBegin is called only
  9046. // for the top-level modal window
  9047. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9048. {
  9049. // won't be disabled when conditions in LockWindow() were not satisfied
  9050. FDisabledOnLockSuspend = !Enabled;
  9051. // When minimized to tray (or actually when set to SW_HIDE),
  9052. // setting Enabled makes the window focusable even when there's
  9053. // modal window over it
  9054. if (!FTrayIcon->Visible)
  9055. {
  9056. Enabled = true;
  9057. }
  9058. }
  9059. FLockSuspendLevel++;
  9060. }
  9061. }
  9062. //---------------------------------------------------------------------------
  9063. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9064. {
  9065. if (FLockSuspendLevel > 0)
  9066. {
  9067. DebugAssert(FLockLevel > 0);
  9068. FLockSuspendLevel--;
  9069. // see comment in SuspendWindowLock
  9070. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9071. {
  9072. // Note that window can be enabled here, when we were minized to tray when
  9073. // was SuspendWindowLock() called.
  9074. // We should possibly do the same check as in LockWindow(),
  9075. // if it is ever possible that the consitions change between
  9076. // SuspendWindowLock() and ResumeWindowLock()
  9077. Enabled = !FDisabledOnLockSuspend;
  9078. }
  9079. }
  9080. }
  9081. //---------------------------------------------------------------------------
  9082. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9083. {
  9084. TTBXComboBoxItem * RemotePathComboBox =
  9085. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9086. if (!TextOnly)
  9087. {
  9088. TStrings * Items = RemotePathComboBox->Strings;
  9089. Items->BeginUpdate();
  9090. try
  9091. {
  9092. Items->Clear();
  9093. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9094. {
  9095. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9096. while (!IsUnixRootPath(APath))
  9097. {
  9098. int P = APath.LastDelimiter(L'/');
  9099. DebugAssert(P >= 0);
  9100. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9101. APath.SetLength(P - 1);
  9102. }
  9103. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9104. }
  9105. }
  9106. __finally
  9107. {
  9108. Items->EndUpdate();
  9109. }
  9110. }
  9111. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9112. // Setting ItemIndex to -1 does not reset its text
  9113. if (RemotePathComboBox->Strings->Count == 0)
  9114. {
  9115. RemotePathComboBox->Text = L"";
  9116. }
  9117. }
  9118. //---------------------------------------------------------------------------
  9119. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9120. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  9121. int & ImageIndex)
  9122. {
  9123. if (AIndex < 0)
  9124. {
  9125. AIndex = Sender->ItemIndex;
  9126. }
  9127. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9128. }
  9129. //---------------------------------------------------------------------------
  9130. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9131. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9132. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9133. {
  9134. ARect.Left += (10 * AIndex);
  9135. }
  9136. //---------------------------------------------------------------------------
  9137. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9138. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9139. {
  9140. AWidth += (10 * AIndex);
  9141. }
  9142. //---------------------------------------------------------------------------
  9143. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  9144. TObject * Sender)
  9145. {
  9146. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9147. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9148. int Index = RemotePathComboBox->ItemIndex;
  9149. while (Index < RemotePathComboBox->Strings->Count - 1)
  9150. {
  9151. APath = UnixExtractFileDir(APath);
  9152. Index++;
  9153. }
  9154. // VanDyke style paths
  9155. if (APath.IsEmpty())
  9156. {
  9157. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9158. APath = ROOTDIRECTORY;
  9159. }
  9160. if (RemoteDirView->Path != APath)
  9161. {
  9162. RemoteDirView->Path = APath;
  9163. }
  9164. }
  9165. //---------------------------------------------------------------------------
  9166. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9167. {
  9168. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9169. DebugUsedParam(Sender);
  9170. UpdateRemotePathComboBox(true);
  9171. }
  9172. //---------------------------------------------------------------------------
  9173. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9174. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9175. {
  9176. ResetIncrementalSearch();
  9177. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9178. DebugAssert(DirView != NULL);
  9179. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9180. {
  9181. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9182. // OnEditing is called also from TCustomListView::CanEdit
  9183. if (Edit != NULL)
  9184. {
  9185. EditSelectBaseName(Edit);
  9186. }
  9187. }
  9188. }
  9189. //---------------------------------------------------------------------------
  9190. //---------------------------------------------------------------------------
  9191. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9192. {
  9193. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9194. Result->AutoDetectDnD = false;
  9195. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9196. Result->RenderDataOn = rdoEnterAndDropSync;
  9197. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9198. return Result;
  9199. }
  9200. //---------------------------------------------------------------------------
  9201. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9202. {
  9203. if (IsWin10Build(2004))
  9204. {
  9205. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9206. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9207. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9208. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9209. }
  9210. }
  9211. //---------------------------------------------------------------------------
  9212. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9213. {
  9214. TForm::CreateWnd();
  9215. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9216. UpdateDarkMode();
  9217. if (FSessionsDragDropFilesEx == NULL)
  9218. {
  9219. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9220. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9221. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9222. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9223. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9224. }
  9225. if (FQueueDragDropFilesEx == NULL)
  9226. {
  9227. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9228. // No need to set OnDragOver as we do not have any restrictions
  9229. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9230. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9231. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9232. }
  9233. }
  9234. //---------------------------------------------------------------------------
  9235. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9236. {
  9237. TForm::DestroyWnd();
  9238. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9239. FQueueDragDropFilesEx->DragDropControl = NULL;
  9240. }
  9241. //---------------------------------------------------------------------------
  9242. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9243. {
  9244. SideEnter(FCurrentSide);
  9245. FEverShown = true;
  9246. }
  9247. //---------------------------------------------------------------------------
  9248. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9249. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9250. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9251. {
  9252. if (!NonVisualDataModule->Busy)
  9253. {
  9254. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9255. Configuration->Usage->Inc(L"FileFinds");
  9256. LockWindow(true);
  9257. NonVisualDataModule->StartBusy();
  9258. WinConfiguration->LockedInterface = true;
  9259. try
  9260. {
  9261. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9262. }
  9263. __finally
  9264. {
  9265. WinConfiguration->LockedInterface = false;
  9266. NonVisualDataModule->EndBusy();
  9267. UnlockWindow();
  9268. }
  9269. }
  9270. }
  9271. //---------------------------------------------------------------------------
  9272. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9273. {
  9274. if (!NonVisualDataModule->Busy)
  9275. {
  9276. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9277. SetFocus();
  9278. RemoteDirView->Path = UnixExtractFilePath(Path);
  9279. UnicodeString FileName = UnixExtractFileName(Path);
  9280. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9281. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9282. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9283. if (Item == NULL)
  9284. {
  9285. ReloadDirectory(osRemote);
  9286. Item = RemoteDirView->FindFileItem(FileName);
  9287. }
  9288. if (Item != NULL)
  9289. {
  9290. RemoteDirView->ItemFocused = Item;
  9291. RemoteDirView->ItemFocused->MakeVisible(false);
  9292. RemoteDirView->SetFocus();
  9293. }
  9294. }
  9295. }
  9296. //---------------------------------------------------------------------------
  9297. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9298. {
  9299. bool Result = !NonVisualDataModule->Busy;
  9300. if (Result)
  9301. {
  9302. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9303. }
  9304. return Result;
  9305. }
  9306. //---------------------------------------------------------------------------
  9307. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9308. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9309. {
  9310. if (CanOperateOnFoundFiles(ATerminal))
  9311. {
  9312. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9313. FOnFileOperationFinished = OnFileOperationFinished;
  9314. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9315. }
  9316. }
  9317. //---------------------------------------------------------------------------
  9318. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9319. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9320. {
  9321. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9322. }
  9323. //---------------------------------------------------------------------------
  9324. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9325. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9326. {
  9327. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9328. }
  9329. //---------------------------------------------------------------------------
  9330. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9331. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9332. {
  9333. if (CanOperateOnFoundFiles(ATerminal))
  9334. {
  9335. for (int Index = 0; Index < FileList->Count; Index++)
  9336. {
  9337. UnicodeString FileName = FileList->Strings[Index];
  9338. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9339. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9340. OnFileOperationFinished(FileName, true);
  9341. }
  9342. }
  9343. }
  9344. //---------------------------------------------------------------------------
  9345. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9346. {
  9347. FFileFindTerminal = Terminal;
  9348. ShowFileFindDialog(
  9349. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9350. DoDownloadFoundFiles, DoEditFoundFiles);
  9351. }
  9352. //---------------------------------------------------------------------------
  9353. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9354. {
  9355. FTaskbarList = TaskbarList;
  9356. }
  9357. //---------------------------------------------------------------------------
  9358. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9359. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9360. {
  9361. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9362. if (Index >= 0)
  9363. {
  9364. if (Button == mbLeft)
  9365. {
  9366. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9367. // If switching tab (switching session) takes long enough for user
  9368. // to actually release the button, "mouse down" is still raised,
  9369. // but we do not get "mouse up" event, so dragging is not cancelled,
  9370. // prevent that by not beginning dragging in the first place.
  9371. if (IsKeyPressed(VK_LBUTTON))
  9372. {
  9373. // when user clicks the "+", we get mouse down only after the session
  9374. // is closed, when new session tab is already on X:Y, so dragging
  9375. // starts, prevent that
  9376. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9377. {
  9378. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9379. if (Terminal != NULL)
  9380. {
  9381. SessionsPageControl->BeginDrag(false);
  9382. }
  9383. }
  9384. }
  9385. }
  9386. else if (Button == mbMiddle)
  9387. {
  9388. // ignore middle-click for "New session tab"
  9389. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9390. if (Terminal != NULL)
  9391. {
  9392. CloseSessionTab(Index);
  9393. }
  9394. }
  9395. }
  9396. }
  9397. //---------------------------------------------------------------------------
  9398. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9399. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9400. {
  9401. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9402. // do not allow dropping on the "+" tab
  9403. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9404. if ((TargetTerminal != NULL) &&
  9405. (SessionsPageControl->ActivePage->PageIndex != Index))
  9406. {
  9407. Configuration->Usage->Inc(L"SessionTabMoves");
  9408. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9409. // but we want to actually prevent that to avoid flicker
  9410. SessionsPageControl->ActivePage->PageIndex = Index;
  9411. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9412. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9413. }
  9414. }
  9415. //---------------------------------------------------------------------------
  9416. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9417. TObject * Sender, TObject * Source, int X, int Y,
  9418. TDragState /*State*/, bool & Accept)
  9419. {
  9420. Accept = (Sender == Source);
  9421. if (Accept)
  9422. {
  9423. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9424. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9425. // do not allow dragging to the "+" tab
  9426. Accept = (Terminal != NULL);
  9427. }
  9428. }
  9429. //---------------------------------------------------------------------------
  9430. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9431. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9432. {
  9433. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9434. if (Index < 0)
  9435. {
  9436. Effect = DROPEFFECT_NONE;
  9437. }
  9438. else
  9439. {
  9440. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9441. // do not allow dropping on the "+" and disconnected tabs
  9442. if (!IsActiveTerminal(TargetTerminal))
  9443. {
  9444. Effect = DROPEFFECT_NONE;
  9445. }
  9446. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9447. {
  9448. Effect = DROPEFFECT_COPY;
  9449. }
  9450. }
  9451. }
  9452. //---------------------------------------------------------------------------
  9453. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9454. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9455. {
  9456. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9457. // do not allow dropping on the "+" and disconnected tabs
  9458. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9459. if (IsActiveTerminal(TargetTerminal))
  9460. {
  9461. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9462. if (!IsFileControl(DropSourceControl, osRemote))
  9463. {
  9464. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9465. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9466. // Why don't we use Terminal->CurrentDirectory directly?
  9467. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9468. // do not force queue, drag drop
  9469. false, true);
  9470. }
  9471. }
  9472. }
  9473. //---------------------------------------------------------------------------
  9474. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9475. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9476. {
  9477. // Downloads are handled in RemoteFileControlDDEnd
  9478. if (!IsFileControl(DropSourceControl, osRemote))
  9479. {
  9480. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9481. Terminal->CurrentDirectory,
  9482. // force queue, drag drop
  9483. true, true);
  9484. }
  9485. }
  9486. //---------------------------------------------------------------------------
  9487. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9488. {
  9489. FShowing = false;
  9490. // Do not save empty workspace
  9491. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9492. {
  9493. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9494. UnicodeString Name = WorkspaceName();
  9495. DoSaveWorkspace(
  9496. Name, DataList.get(),
  9497. !Configuration->DisablePasswordStoring &&
  9498. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9499. WinConfiguration->LastStoredSession = Name;
  9500. }
  9501. }
  9502. //---------------------------------------------------------------------------
  9503. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9504. {
  9505. TManagedTerminal * ManagedTerminal =
  9506. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9507. if (ManagedTerminal != NULL)
  9508. {
  9509. ManagedTerminal->DirectoryLoaded = Now();
  9510. }
  9511. }
  9512. //---------------------------------------------------------------------------
  9513. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9514. TListItem * /*Item*/, bool /*Selected*/)
  9515. {
  9516. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9517. switch (DirView->LastSelectMethod)
  9518. {
  9519. case smKeyboard:
  9520. Configuration->Usage->Inc(L"KeyboardSelections");
  9521. break;
  9522. case smMouse:
  9523. Configuration->Usage->Inc(L"MouseSelections");
  9524. break;
  9525. }
  9526. }
  9527. //---------------------------------------------------------------------------
  9528. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9529. {
  9530. if (Color != 0)
  9531. {
  9532. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9533. }
  9534. else
  9535. {
  9536. return FSessionTabImageIndex;
  9537. }
  9538. }
  9539. //---------------------------------------------------------------------------
  9540. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9541. {
  9542. TPngImageCollectionItem * Item =
  9543. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9544. FSessionColors->AddPng(Item->PngImage);
  9545. return FSessionColors->Count - 1;
  9546. }
  9547. //---------------------------------------------------------------------------
  9548. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9549. {
  9550. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9551. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9552. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9553. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9554. }
  9555. //---------------------------------------------------------------------------
  9556. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9557. {
  9558. for (int Index = 0; Index < Control->ControlCount; Index++)
  9559. {
  9560. TControl * AControl = Control->Controls[Index];
  9561. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9562. if (WinControl != NULL)
  9563. {
  9564. CollectItemsWithTextDisplayMode(WinControl);
  9565. }
  9566. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9567. if (Toolbar != NULL)
  9568. {
  9569. // we care for top-level items only
  9570. TTBCustomItem * Items = Toolbar->Items;
  9571. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9572. {
  9573. TTBCustomItem * Item = Items->Items[ItemIndex];
  9574. if (((Item->DisplayMode == nbdmImageAndText) ||
  9575. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9576. EligibleForImageDisplayMode(Item))
  9577. {
  9578. FItemsWithTextDisplayMode.insert(Item);
  9579. }
  9580. }
  9581. }
  9582. }
  9583. }
  9584. //---------------------------------------------------------------------------
  9585. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9586. {
  9587. return true;
  9588. }
  9589. //---------------------------------------------------------------------------
  9590. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9591. {
  9592. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9593. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9594. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9595. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9596. bool Result = true;
  9597. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9598. {
  9599. TTBCustomItem * Item = *i;
  9600. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9601. if (Label != NULL)
  9602. {
  9603. // optimization
  9604. if (Label->Visible == SelectiveToolbarText)
  9605. {
  9606. Result = false;
  9607. }
  9608. else
  9609. {
  9610. Label->Visible = SelectiveToolbarText;
  9611. }
  9612. }
  9613. else
  9614. {
  9615. // optimization
  9616. if (Item->DisplayMode == DisplayMode)
  9617. {
  9618. Result = false;
  9619. }
  9620. else
  9621. {
  9622. Item->DisplayMode = DisplayMode;
  9623. }
  9624. }
  9625. ++i;
  9626. }
  9627. if (Result)
  9628. {
  9629. UpdateNewSessionTab();
  9630. }
  9631. return Result;
  9632. }
  9633. //---------------------------------------------------------------------------
  9634. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9635. {
  9636. DebugFail();
  9637. }
  9638. //---------------------------------------------------------------------------
  9639. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9640. {
  9641. // Among other, a lock level is non zero, while directory is being loaded,
  9642. // even when it happens as a result of dirview navigation,
  9643. // i.e. when TNonVisualDataModule is NOT FBusy.
  9644. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9645. // Among other this prevents a panel auto update to occur while
  9646. // directory is changing.
  9647. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9648. }
  9649. //---------------------------------------------------------------------------
  9650. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9651. {
  9652. // While the window is disabled, we still seem to process menu shortcuts at least,
  9653. // so stop it at least here.
  9654. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9655. return
  9656. (Allowed == aaUpdate) ||
  9657. !NonVisualDataModule->Busy;
  9658. }
  9659. //---------------------------------------------------------------------------
  9660. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9661. {
  9662. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9663. }
  9664. //---------------------------------------------------------------------------
  9665. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9666. {
  9667. // This is somewhat redundant to LockWindow() call from
  9668. // TTerminalManager::TerminalReadDirectoryProgress.
  9669. // But disabling window is known not to block keyboard shorcuts
  9670. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9671. if (Busy > 0)
  9672. {
  9673. if (NonVisualDataModule->Busy)
  9674. {
  9675. State = false;
  9676. }
  9677. else
  9678. {
  9679. NonVisualDataModule->StartBusy();
  9680. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9681. LockWindow();
  9682. }
  9683. }
  9684. else if (Busy < 0)
  9685. {
  9686. UnlockWindow();
  9687. NonVisualDataModule->EndBusy();
  9688. }
  9689. else
  9690. {
  9691. State = NonVisualDataModule->Busy;
  9692. }
  9693. }
  9694. //---------------------------------------------------------------------------
  9695. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9696. {
  9697. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9698. // no context menu for "New session tab"
  9699. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9700. {
  9701. SessionsPageControl->ActivePageIndex = Index;
  9702. if (DebugAlwaysTrue(SessionTabSwitched()))
  9703. {
  9704. // copied from TControl.WMContextMenu
  9705. SendCancelMode(SessionsPageControl);
  9706. // explicit popup instead of using PopupMenu property
  9707. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9708. // while connecting yet not-connected session and hence
  9709. // allowing an access to commands over not-completelly connected session
  9710. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9711. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9712. PopupMenu->PopupComponent = SessionsPageControl;
  9713. PopupMenu->Popup(Point.x, Point.y);
  9714. }
  9715. }
  9716. Handled = true;
  9717. }
  9718. //---------------------------------------------------------------------------
  9719. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9720. {
  9721. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9722. }
  9723. //---------------------------------------------------------------------------
  9724. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9725. {
  9726. Configuration->Usage->Inc(L"PasswordChanges");
  9727. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9728. ChangePasswordData->DisableAuthentationsExceptPassword();
  9729. ChangePasswordData->ChangePassword = true;
  9730. std::unique_ptr<TTerminal> ChangePasswordSession(
  9731. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9732. ChangePasswordSession->AutoReadDirectory = false;
  9733. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9734. {
  9735. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9736. }
  9737. else
  9738. {
  9739. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9740. }
  9741. }
  9742. //---------------------------------------------------------------------------
  9743. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9744. {
  9745. // No nice way to assert SSH2
  9746. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9747. }
  9748. //---------------------------------------------------------------------------
  9749. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9750. {
  9751. TOperationVisualizer OperationVisualizer;
  9752. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9753. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9754. }
  9755. //---------------------------------------------------------------------------
  9756. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9757. {
  9758. TForm::ChangeScale(M, D);
  9759. int APixelsPerInch = GetControlPixelsPerInch(this);
  9760. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9761. try
  9762. {
  9763. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9764. }
  9765. __finally
  9766. {
  9767. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9768. }
  9769. }
  9770. //---------------------------------------------------------------------------
  9771. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9772. {
  9773. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9774. }
  9775. //---------------------------------------------------------------------
  9776. class TPublicControl : public TControl
  9777. {
  9778. friend class TCustomScpExplorerForm;
  9779. };
  9780. //---------------------------------------------------------------------------
  9781. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9782. {
  9783. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9784. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9785. }
  9786. //---------------------------------------------------------------------------
  9787. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9788. {
  9789. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9790. OpenBookmark(Side, Bookmark);
  9791. }
  9792. //---------------------------------------------------------------------------
  9793. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9794. {
  9795. DoBookmarkClick(osLocal, Sender);
  9796. }
  9797. //---------------------------------------------------------------------------
  9798. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9799. {
  9800. DoBookmarkClick(osRemote, Sender);
  9801. }
  9802. //---------------------------------------------------------------------------
  9803. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9804. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9805. {
  9806. if (BookmarkList != NULL)
  9807. {
  9808. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9809. if (!WinConfiguration->UseLocationProfiles)
  9810. {
  9811. std::unique_ptr<TStringList> Directories(new TStringList());
  9812. Directories->CaseSensitive = (Side == osRemote);
  9813. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9814. {
  9815. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9816. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9817. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9818. {
  9819. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9820. Item->Caption = EscapeHotkey(Directory);
  9821. Item->ShortCut = Bookmark->ShortCut;
  9822. Item->OnClick = OnBookmarkClick;
  9823. Item->Tag = reinterpret_cast<int>(Bookmark);
  9824. Directories->Add(Directory);
  9825. Menu->Add(Item.release());
  9826. }
  9827. }
  9828. }
  9829. else
  9830. {
  9831. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9832. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9833. {
  9834. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9835. if (!Bookmark->Node.IsEmpty())
  9836. {
  9837. Folders->Add(Bookmark->Node);
  9838. }
  9839. }
  9840. for (int Index = 0; Index < Folders->Count; Index++)
  9841. {
  9842. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9843. Item->Caption = Folders->Strings[Index];
  9844. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9845. Folders->Objects[Index] = Item.get();
  9846. Menu->Add(Item.release());
  9847. }
  9848. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9849. {
  9850. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9851. TTBCustomItem * Parent;
  9852. if (!Bookmark->Node.IsEmpty())
  9853. {
  9854. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9855. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9856. }
  9857. else
  9858. {
  9859. Parent = Menu;
  9860. }
  9861. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9862. Item->Caption = Bookmark->Name;
  9863. Item->ShortCut = Bookmark->ShortCut;
  9864. Item->OnClick = OnBookmarkClick;
  9865. Item->Tag = reinterpret_cast<int>(Bookmark);
  9866. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9867. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9868. {
  9869. // No hint for location profiles that are actually mere "bookmarks"
  9870. }
  9871. else
  9872. {
  9873. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9874. UnicodeString LongHint;
  9875. if (!Bookmark->Local.IsEmpty())
  9876. {
  9877. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9878. }
  9879. if (!Bookmark->Remote.IsEmpty())
  9880. {
  9881. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9882. }
  9883. AddToList(Hint, LongHint, L"|");
  9884. Item->Hint = Hint;
  9885. }
  9886. Parent->Add(Item.release());
  9887. }
  9888. }
  9889. AddMenuSeparator(Menu);
  9890. }
  9891. }
  9892. //---------------------------------------------------------------------------
  9893. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9894. {
  9895. Menu->Clear();
  9896. std::unique_ptr<TTBCustomItem> Item;
  9897. Item.reset(new TTBXItem(Owner));
  9898. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9899. Item->Options = Item->Options << tboDefault;
  9900. Menu->Add(Item.release());
  9901. AddMenuSeparator(Menu);
  9902. if (Terminal != NULL)
  9903. {
  9904. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9905. }
  9906. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9907. Item.reset(new TTBXItem(Owner));
  9908. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9909. Menu->Add(Item.release());
  9910. }
  9911. //---------------------------------------------------------------------------
  9912. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9913. {
  9914. bool Result = true;
  9915. try
  9916. {
  9917. if (Side == osRemote)
  9918. {
  9919. Terminal->ExceptionOnFail = true;
  9920. }
  9921. try
  9922. {
  9923. DirView(Side)->Path = Path;
  9924. }
  9925. __finally
  9926. {
  9927. if (Side == osRemote)
  9928. {
  9929. Terminal->ExceptionOnFail = false;
  9930. }
  9931. }
  9932. }
  9933. catch (Exception & E)
  9934. {
  9935. if ((Side != osRemote) || Terminal->Active)
  9936. {
  9937. ShowExtendedExceptionEx(Terminal, &E);
  9938. Result = false;
  9939. }
  9940. else
  9941. {
  9942. throw;
  9943. }
  9944. }
  9945. return Result;
  9946. }
  9947. //---------------------------------------------------------------------------
  9948. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9949. {
  9950. // Make sure there some feedback even when loading tiny local folders
  9951. TInstantOperationVisualizer Visualizer;
  9952. DirView(Side)->ReloadDirectory();
  9953. }
  9954. //---------------------------------------------------------------------------
  9955. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9956. {
  9957. if (Index == SessionsPageControl->TabIndex)
  9958. {
  9959. CloseSession();
  9960. }
  9961. else
  9962. {
  9963. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9964. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9965. TTerminalManager * Manager = TTerminalManager::Instance();
  9966. if (!Terminal->Active)
  9967. {
  9968. Manager->FreeTerminal(Terminal);
  9969. }
  9970. else
  9971. {
  9972. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9973. if (CanCloseQueue(Queue))
  9974. {
  9975. Manager->FreeTerminal(Terminal);
  9976. }
  9977. }
  9978. }
  9979. }
  9980. //---------------------------------------------------------------------------
  9981. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9982. {
  9983. CloseSessionTab(Index);
  9984. }
  9985. //---------------------------------------------------------------------------
  9986. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9987. {
  9988. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9989. {
  9990. TInstantOperationVisualizer Visualizer;
  9991. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9992. // before we create a new data object
  9993. ClipboardClear();
  9994. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9995. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9996. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9997. // Intermediate variable to WORKAROUND a crash in compiler
  9998. TObjectList * ClipboardFakeMonitors =
  9999. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10000. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10001. if (FClipboardDragDropFilesEx.get() == NULL)
  10002. {
  10003. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10004. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10005. }
  10006. FClipboardDragDropFilesEx->FileList->Clear();
  10007. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10008. FClipboardDragDropFilesEx->CopyToClipboard();
  10009. FClipboardTerminal = FTerminal;
  10010. // Need full paths, as cwd can be different once files are pasted
  10011. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10012. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10013. {
  10014. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10015. }
  10016. }
  10017. }
  10018. //---------------------------------------------------------------------------
  10019. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10020. {
  10021. if (OpenClipboard(0))
  10022. {
  10023. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10024. CloseClipboard();
  10025. }
  10026. }
  10027. //---------------------------------------------------------------------------
  10028. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10029. {
  10030. return !FClipboardFakeDirectory.IsEmpty();
  10031. }
  10032. //---------------------------------------------------------------------------
  10033. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10034. {
  10035. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10036. FClipboardFakeDirectory = UnicodeString();
  10037. FClipboardTerminal = NULL;
  10038. if (FDownloadingFromClipboard)
  10039. {
  10040. // We are called by the monitor, so attempt to release it would deadlonk
  10041. FClipboardFakeMonitorsPendingReset = true;
  10042. }
  10043. else
  10044. {
  10045. FClipboardFakeMonitors.reset(NULL);
  10046. }
  10047. }
  10048. //---------------------------------------------------------------------------
  10049. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10050. {
  10051. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10052. {
  10053. ClipboardStop();
  10054. }
  10055. }
  10056. //---------------------------------------------------------------------------
  10057. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10058. {
  10059. TTerminalManager * Manager = TTerminalManager::Instance();
  10060. Manager->ActiveTerminal = FClipboardTerminal;
  10061. TTransferOperationParam Params;
  10062. Params.TargetDirectory = TargetDirectory;
  10063. Params.DragDrop = DragDrop;
  10064. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10065. }
  10066. //---------------------------------------------------------------------------
  10067. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10068. {
  10069. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10070. // (as it pumps a Windows message queue while being shutted down)
  10071. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10072. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10073. {
  10074. // Can fail as it can be e.g. locked by AV, so we retry that later
  10075. bool Removed = RemoveDir(ApiPath(FileName));
  10076. try
  10077. {
  10078. if (!NonVisualDataModule->Busy)
  10079. {
  10080. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10081. FClipboardFakeMonitorsPendingReset = false;
  10082. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10083. TAutoFlag Flag(FDownloadingFromClipboard);
  10084. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10085. }
  10086. }
  10087. __finally
  10088. {
  10089. if (!Removed)
  10090. {
  10091. RemoveDir(ApiPath(FileName));
  10092. }
  10093. }
  10094. }
  10095. }
  10096. //---------------------------------------------------------------------------
  10097. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10098. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10099. {
  10100. TFileMasks::TParams MaskParams;
  10101. MaskParams.Size = Size;
  10102. MaskParams.Modification = Modification;
  10103. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10104. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10105. {
  10106. bool ImplicitMatch;
  10107. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10108. !ImplicitMatch)
  10109. {
  10110. Color = Iter->Color;
  10111. break;
  10112. }
  10113. }
  10114. }
  10115. //---------------------------------------------------------------------------
  10116. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10117. {
  10118. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10119. if (!DirView->IsEditing())
  10120. {
  10121. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10122. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10123. // while searching
  10124. if (Key == VK_BACK)
  10125. {
  10126. if (!FIncrementalSearch.IsEmpty())
  10127. {
  10128. if (FIncrementalSearch.Length() == 1)
  10129. {
  10130. ResetIncrementalSearch();
  10131. }
  10132. else
  10133. {
  10134. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10135. IncrementalSearch(NewText, false, false);
  10136. }
  10137. Key = 0;
  10138. }
  10139. }
  10140. }
  10141. }
  10142. //---------------------------------------------------------------------------
  10143. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10144. {
  10145. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10146. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10147. {
  10148. // Filter control sequences.
  10149. // When not searching yet, prefer use of the space for toggling file selection
  10150. // (so we cannot incrementally search for a string starting with a space).
  10151. if ((Key > VK_SPACE) ||
  10152. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10153. {
  10154. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10155. Key = 0;
  10156. }
  10157. }
  10158. }
  10159. //---------------------------------------------------------------------------
  10160. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10161. {
  10162. if (!FIncrementalSearch.IsEmpty())
  10163. {
  10164. FIncrementalSearch = L"";
  10165. DirView(osCurrent)->UpdateStatusBar();
  10166. }
  10167. }
  10168. //---------------------------------------------------------------------------
  10169. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10170. {
  10171. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10172. if (Item == NULL)
  10173. {
  10174. MessageBeep(MB_ICONHAND);
  10175. }
  10176. else
  10177. {
  10178. TCustomDirView * ADirView = DirView(osCurrent);
  10179. {
  10180. TAutoNestingCounter Guard(FIncrementalSearching);
  10181. ADirView->FocusItem(Item);
  10182. if (ADirView->NortonLike == nlOff)
  10183. {
  10184. if ((ADirView->Selected != Item) ||
  10185. (ADirView->SelCount > 1))
  10186. {
  10187. ADirView->ClearSelection();
  10188. ADirView->Selected = Item;
  10189. }
  10190. }
  10191. }
  10192. FIncrementalSearch = Text;
  10193. Item->MakeVisible(false);
  10194. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10195. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10196. ADirView->UpdateStatusBar();
  10197. }
  10198. }
  10199. //---------------------------------------------------------------------------
  10200. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10201. {
  10202. int Index = Item->Index;
  10203. if (!Reverse)
  10204. {
  10205. Index++;
  10206. if (Index >= Item->Owner->Count)
  10207. {
  10208. Index = 0;
  10209. }
  10210. }
  10211. else
  10212. {
  10213. Index--;
  10214. if (Index < 0)
  10215. {
  10216. Index = Item->Owner->Count - 1;
  10217. }
  10218. }
  10219. return Item->Owner->Item[Index];
  10220. }
  10221. //---------------------------------------------------------------------------
  10222. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10223. {
  10224. TCustomDirView * ADirView = DirView(osCurrent);
  10225. if (ADirView->Items->Count == 0)
  10226. {
  10227. return NULL;
  10228. }
  10229. else
  10230. {
  10231. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10232. TListItem * Item = CurrentItem;
  10233. if (SkipCurrent)
  10234. {
  10235. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10236. }
  10237. while (true)
  10238. {
  10239. bool Matches = false;
  10240. switch (WinConfiguration->PanelSearch)
  10241. {
  10242. case isNameStartOnly:
  10243. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10244. break;
  10245. case isName:
  10246. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10247. break;
  10248. case isAll:
  10249. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10250. int Index = 0;
  10251. while ((Index < ColCount) && !Matches)
  10252. {
  10253. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10254. if (Visible)
  10255. {
  10256. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10257. }
  10258. Index++;
  10259. }
  10260. break;
  10261. }
  10262. if (Matches)
  10263. {
  10264. return Item;
  10265. }
  10266. Item = GetNextFile(Item, Reverse);
  10267. if (Item == CurrentItem)
  10268. {
  10269. return NULL;
  10270. }
  10271. }
  10272. }
  10273. }
  10274. //---------------------------------------------------------------------------
  10275. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10276. {
  10277. ResetIncrementalSearch();
  10278. }
  10279. //---------------------------------------------------------------------------
  10280. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10281. {
  10282. if (Message.CharCode == VK_TAB)
  10283. {
  10284. if (!FIncrementalSearch.IsEmpty())
  10285. {
  10286. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10287. bool Reverse = Shift.Contains(ssShift);
  10288. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10289. Message.Result = 1;
  10290. return;
  10291. }
  10292. }
  10293. else if (Message.CharCode == VK_ESCAPE)
  10294. {
  10295. if (!FIncrementalSearch.IsEmpty())
  10296. {
  10297. ResetIncrementalSearch();
  10298. Message.Result = 1;
  10299. return;
  10300. }
  10301. }
  10302. TForm::Dispatch(&Message);
  10303. }
  10304. //---------------------------------------------------------------------------
  10305. void __fastcall TCustomScpExplorerForm::Deactivate()
  10306. {
  10307. TForm::Deactivate();
  10308. // E.g. when switching to an internal editor window
  10309. ResetIncrementalSearch();
  10310. }
  10311. //---------------------------------------------------------------------------
  10312. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10313. {
  10314. // When switching to another application
  10315. ResetIncrementalSearch();
  10316. }
  10317. //---------------------------------------------------------------------------
  10318. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10319. {
  10320. ResetIncrementalSearch();
  10321. }
  10322. //---------------------------------------------------------------------------
  10323. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10324. {
  10325. if (FIncrementalSearching <= 0)
  10326. {
  10327. ResetIncrementalSearch();
  10328. }
  10329. }
  10330. //---------------------------------------------------------------------------
  10331. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10332. {
  10333. CountClicksForWindowPrint(this);
  10334. }
  10335. //---------------------------------------------------------------------------
  10336. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10337. {
  10338. CenterReconnectToolbar();
  10339. }
  10340. //---------------------------------------------------------------------------
  10341. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10342. {
  10343. if (RemoteDirView->ItemFocused != NULL)
  10344. {
  10345. RemoteDirView->ItemFocused->Selected = true;
  10346. }
  10347. }
  10348. //---------------------------------------------------------------------------
  10349. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10350. {
  10351. if (!FQueueStatusUpdating)
  10352. {
  10353. bool Refresh;
  10354. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10355. {
  10356. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10357. }
  10358. else
  10359. {
  10360. if (FQueueFileList->GetCount() > 0)
  10361. {
  10362. FQueueFileList->Clear();
  10363. Refresh = true;
  10364. }
  10365. }
  10366. int Count = FQueueFileList->GetCount();
  10367. if (QueueFileList->Items->Count != Count)
  10368. {
  10369. DebugAssert(Refresh);
  10370. QueueFileList->Items->Count = Count;
  10371. }
  10372. if (Refresh)
  10373. {
  10374. QueueFileList->Invalidate();
  10375. }
  10376. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10377. }
  10378. }
  10379. //---------------------------------------------------------------------------
  10380. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10381. {
  10382. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10383. }
  10384. //---------------------------------------------------------------------------
  10385. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10386. {
  10387. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10388. // See TSynchronizeChecklistDialog::UpdateTimer
  10389. if (QueueFileList->Items->Count > 0)
  10390. {
  10391. QueueFileList->Items->Item[0]->MakeVisible(false);
  10392. }
  10393. UpdateQueueFileList();
  10394. }
  10395. //---------------------------------------------------------------------------
  10396. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10397. {
  10398. Active = QueueView3->Focused() || QueueFileList->Focused();
  10399. }
  10400. //---------------------------------------------------------------------------
  10401. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10402. {
  10403. QueueLabelUpdateStatus();
  10404. }
  10405. //---------------------------------------------------------------------------
  10406. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10407. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10408. {
  10409. int State = FQueueFileList->GetState(Item->Index);
  10410. if (State == qfsProcessed)
  10411. {
  10412. Sender->Canvas->Font->Color = clGrayText;
  10413. }
  10414. }
  10415. //---------------------------------------------------------------------------
  10416. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10417. {
  10418. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10419. QueueFileList->Column[0]->Width =
  10420. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10421. }
  10422. //---------------------------------------------------------------------------
  10423. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10424. {
  10425. QueueFileListColumnAutoSize();
  10426. }
  10427. //---------------------------------------------------------------------------
  10428. void __fastcall TCustomScpExplorerForm::CloseApp()
  10429. {
  10430. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10431. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10432. // So we clear the flag here.
  10433. DebugAssert(NonVisualDataModule->Busy);
  10434. NonVisualDataModule->EndBusy();
  10435. try
  10436. {
  10437. Close();
  10438. }
  10439. __finally
  10440. {
  10441. NonVisualDataModule->StartBusy();
  10442. }
  10443. }
  10444. //---------------------------------------------------------------------------
  10445. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10446. {
  10447. return (Terminal != NULL) && Terminal->Active;
  10448. }
  10449. //---------------------------------------------------------------------------
  10450. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10451. {
  10452. return IsActiveTerminal(Terminal);
  10453. }