CustomScpExplorer.cpp 349 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 4;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. class TAutoBatch
  101. {
  102. public:
  103. TAutoBatch(TCustomScpExplorerForm * Form) :
  104. FForm(Form)
  105. {
  106. FForm->BatchStart(FBatchStorage);
  107. }
  108. ~TAutoBatch()
  109. {
  110. FForm->BatchEnd(FBatchStorage);
  111. }
  112. private:
  113. TCustomScpExplorerForm * FForm;
  114. void * FBatchStorage;
  115. };
  116. //---------------------------------------------------------------------------
  117. struct TTransferOperationParam
  118. {
  119. TTransferOperationParam();
  120. UnicodeString TargetDirectory;
  121. bool Temp;
  122. bool DragDrop;
  123. int Options;
  124. TAutoSwitch Queue;
  125. };
  126. //---------------------------------------------------------------------------
  127. TTransferOperationParam::TTransferOperationParam()
  128. {
  129. Temp = false;
  130. DragDrop = false;
  131. Options = 0;
  132. Queue = asAuto;
  133. }
  134. //---------------------------------------------------------------------------
  135. class TTransferPresetNoteData : public TObject
  136. {
  137. public:
  138. UnicodeString Message;
  139. };
  140. //---------------------------------------------------------------------------
  141. class TTerminalNoteData : public TObject
  142. {
  143. public:
  144. TManagedTerminal * Terminal;
  145. };
  146. //---------------------------------------------------------------------------
  147. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  148. FFormRestored(false),
  149. TForm(Owner)
  150. {
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. InitControls();
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  159. NonVisualDataModule->ScpExplorer = this;
  160. FAutoOperation = false;
  161. FOnFileOperationFinished = NULL;
  162. FForceExecution = false;
  163. FIgnoreNextDialogChar = 0;
  164. FErrorList = NULL;
  165. FSynchronizeProgressForm = NULL;
  166. FProgressForm = NULL;
  167. FRefreshLocalDirectory = false;
  168. FRefreshRemoteDirectory = false;
  169. FDDExtMapFile = NULL;
  170. // CreateMutexW keeps failing with ERROR_NOACCESS
  171. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  172. DebugAssert(FDDExtMutex != NULL);
  173. FDDTargetControl = NULL;
  174. FDelayedDeletionTimer = NULL;
  175. FDelayedDeletionList = new TStringList();
  176. FDDFileList = NULL;
  177. FPendingTempSpaceWarn = false;
  178. FCapturedLog = NULL;
  179. FDragDropOperation = false;
  180. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  181. FAllowTransferPresetAutoSelect = true;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FMoveToQueue = false;
  195. FStandaloneEditing = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FNeedSession = false;
  198. FDoNotIdleCurrentTerminal = 0;
  199. FIncrementalSearching = 0;
  200. FIncrementalSearchHaveNext = false;
  201. FEditorManager = new TEditorManager();
  202. FEditorManager->OnFileChange = ExecutedFileChanged;
  203. FEditorManager->OnFileReload = ExecutedFileReload;
  204. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  205. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  206. FLocalEditors = new TList();
  207. FQueueStatus = NULL;
  208. FQueueStatusSection = new TCriticalSection();
  209. FQueueStatusInvalidated = false;
  210. FQueueItemInvalidated = false;
  211. FQueueActedItem = NULL;
  212. FQueueController = new TQueueController(QueueView3);
  213. FUserActionTimer = new TTimer(this);
  214. FUserActionTimer->Enabled = false;
  215. FUserActionTimer->Interval = 10;
  216. FUserActionTimer->OnTimer = UserActionTimer;
  217. FNotes = new TStringList();
  218. FNoteTimer = new TTimer(this);
  219. FNoteTimer->Enabled = false;
  220. FNoteTimer->OnTimer = NoteTimer;
  221. FOnNoteClick = NULL;
  222. FOle32Library = LoadLibrary(L"Ole32.dll");
  223. FDragMoveCursor = FOle32Library != NULL ?
  224. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  225. UseSystemSettings(this);
  226. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  227. static_cast<TObject*>(GetComponent(fcTransferList)));
  228. DebugAssert(TransferList != NULL);
  229. FTransferListHoverIndex = -1;
  230. TransferList->OnChange = TransferListChange;
  231. TransferList->OnDrawItem = TransferListDrawItem;
  232. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  233. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  234. UseDesktopFont(SessionsPageControl);
  235. UpdateSessionsPageControlHeight();
  236. UseDesktopFont(RemoteDirView);
  237. UseDesktopFont(RemoteDriveView);
  238. UseDesktopFont(QueueView3);
  239. UseDesktopFont(QueueLabel);
  240. UseDesktopFont(RemoteStatusBar);
  241. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  242. QueueSplitter->ShowHint = true;
  243. RemotePanelSplitter->ShowHint = true;
  244. UpdateImages();
  245. FCustomCommandMenu = CreateTBXPopupMenu(this);
  246. FCustomCommandLocalFileList = NULL;
  247. FCustomCommandRemoteFileList = NULL;
  248. FSessionColors = new TPngImageList(this);
  249. FSessionColors->ColorDepth = cd32Bit;
  250. AddFixedSessionImages();
  251. SessionsPageControl->Images = FSessionColors;
  252. QueueLabel->FocusControl = QueueView3;
  253. UpdateQueueLabel();
  254. FRemoteDirViewWasFocused = true;
  255. CreateHiddenWindow();
  256. StartUpdates();
  257. }
  258. //---------------------------------------------------------------------------
  259. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  260. {
  261. // this has to be one of the very first things to do
  262. StopUpdateThread();
  263. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  264. TTerminalManager::Instance()->ScpExplorer = NULL;
  265. delete FCustomCommandLocalFileList;
  266. delete FCustomCommandRemoteFileList;
  267. delete FCustomCommandMenu;
  268. delete FTrayIcon;
  269. FTrayIcon = NULL;
  270. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  271. delete FEditorManager;
  272. ForceCloseLocalEditors();
  273. delete FLocalEditors;
  274. HideFileFindDialog();
  275. if (FDelayedDeletionTimer)
  276. {
  277. DoDelayedDeletion(NULL);
  278. SAFE_DESTROY(FDelayedDeletionTimer);
  279. }
  280. SAFE_DESTROY(FDelayedDeletionList);
  281. // sometimes we do not get DDEnd so the list is not released
  282. SAFE_DESTROY(FDDFileList);
  283. DebugAssert(FSynchronizeController == NULL);
  284. CloseHandle(FDDExtMutex);
  285. FDDExtMutex = NULL;
  286. FreeLibrary(FOle32Library);
  287. FOle32Library = NULL;
  288. FDragMoveCursor = NULL;
  289. DebugAssert(!FErrorList);
  290. if (FEverShown)
  291. {
  292. // when window is never shown (like when running command-line operation),
  293. // particularly window site is not restored correctly (BoundsRect value set
  294. // in RestoreForm gets lost during handle allocation), so we do not want
  295. // it to be stored
  296. StoreParams();
  297. }
  298. Terminal = NULL;
  299. Queue = NULL;
  300. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  301. NonVisualDataModule->ScpExplorer = NULL;
  302. delete FQueueController;
  303. FQueueController = NULL;
  304. delete FQueueStatusSection;
  305. FQueueStatusSection = NULL;
  306. delete FQueueStatus;
  307. FQueueStatus = NULL;
  308. delete FUserActionTimer;
  309. FUserActionTimer = NULL;
  310. delete FNoteTimer;
  311. delete FNotes;
  312. delete FNoteData;
  313. SAFE_DESTROY(FDocks);
  314. SessionsPageControl->Images = NULL;
  315. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  316. SAFE_DESTROY(FSessionColors);
  317. SAFE_DESTROY(FSessionsDragDropFilesEx);
  318. SAFE_DESTROY(FHistoryMenu[0][0]);
  319. SAFE_DESTROY(FHistoryMenu[0][1]);
  320. SAFE_DESTROY(FHistoryMenu[1][0]);
  321. SAFE_DESTROY(FHistoryMenu[1][1]);
  322. if (FHiddenWindow != NULL)
  323. {
  324. DestroyWindow(FHiddenWindow);
  325. FHiddenWindow = NULL;
  326. }
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  330. {
  331. std::unique_ptr<TSessionData> Data;
  332. if (!Session.IsEmpty())
  333. {
  334. bool DefaultsOnly;
  335. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  336. }
  337. TTerminalManager * Manager = TTerminalManager::Instance();
  338. for (int Index = 0; Index < Manager->Count; Index++)
  339. {
  340. TTerminal * Terminal = Manager->Terminals[Index];
  341. if (Session.IsEmpty() ||
  342. Terminal->SessionData->IsSameSite(Data.get()))
  343. {
  344. if (Path.IsEmpty())
  345. {
  346. Terminal->ClearCaches();
  347. }
  348. else
  349. {
  350. Terminal->DirectoryModified(Path, true);
  351. }
  352. }
  353. }
  354. // We should flag a pending refresh for the background terminals or busy foreground terminals
  355. if ((Terminal != NULL) && Terminal->Active &&
  356. CanCommandLineFromAnotherInstance() &&
  357. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  358. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  359. {
  360. Terminal->ReloadDirectory();
  361. }
  362. }
  363. //---------------------------------------------------------------------------
  364. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  365. {
  366. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  367. size_t MessageSize = sizeof(TCopyDataMessage);
  368. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  369. if (Result)
  370. {
  371. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  372. Result = (Message.Version == TCopyDataMessage::Version1);
  373. if (Result)
  374. {
  375. switch (Message.Command)
  376. {
  377. case TCopyDataMessage::CommandCanCommandLine:
  378. Result = CanCommandLineFromAnotherInstance();
  379. break;
  380. case TCopyDataMessage::CommandCommandLine:
  381. {
  382. UnicodeString CommandLine(Message.CommandLine);
  383. Result = CommandLineFromAnotherInstance(CommandLine);
  384. }
  385. break;
  386. case TCopyDataMessage::RefreshPanel:
  387. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  388. break;
  389. case TCopyDataMessage::MainWindowCheck:
  390. Result = true;
  391. break;
  392. default:
  393. Result = false;
  394. break;
  395. }
  396. }
  397. }
  398. Message.Result = Result ? 1 : 0;
  399. }
  400. //---------------------------------------------------------------------------
  401. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  402. {
  403. WNDCLASS WindowClass = {0};
  404. WindowClass.lpfnWndProc = DefWindowProc;
  405. WindowClass.hInstance = HInstance;
  406. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  407. FHiddenWindow = NULL;
  408. if (RegisterClass(&WindowClass))
  409. {
  410. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  411. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  412. }
  413. }
  414. //---------------------------------------------------------------------------
  415. bool __fastcall TCustomScpExplorerForm::CanConsole()
  416. {
  417. return
  418. (Terminal != NULL) &&
  419. Terminal->Active &&
  420. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  421. }
  422. //---------------------------------------------------------------------------
  423. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  424. {
  425. bool Result = Visible && !NonVisualDataModule->Busy;
  426. return Result;
  427. }
  428. //---------------------------------------------------------------------------
  429. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  430. const UnicodeString & CommandLine)
  431. {
  432. TProgramParams Params(CommandLine);
  433. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  434. if (Result)
  435. {
  436. NonVisualDataModule->StartBusy();
  437. try
  438. {
  439. // this action is initiated from another process,
  440. // so it's likely that our window is not visible,
  441. // and user won't see what is going on
  442. Application->BringToFront();
  443. // reload sessions as we may be asked to open a session
  444. // just stored by another instance
  445. StoredSessions->Reload();
  446. UnicodeString SessionName = Params.Param[1];
  447. std::unique_ptr<TObjectList> DataList(new TObjectList());
  448. UnicodeString DownloadFile; // unused
  449. try
  450. {
  451. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  452. }
  453. catch (EAbort &)
  454. {
  455. // Happens when opening session in PuTTY and in other situations,
  456. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  457. // to process the command-line on its own (or to pass it to yet another instance)
  458. }
  459. if (DataList->Count > 0)
  460. {
  461. TTerminalManager * Manager = TTerminalManager::Instance();
  462. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  463. }
  464. }
  465. __finally
  466. {
  467. NonVisualDataModule->EndBusy();
  468. }
  469. }
  470. return Result;
  471. }
  472. //---------------------------------------------------------------------------
  473. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  474. {
  475. if (FTerminal != value)
  476. {
  477. TerminalChanging();
  478. DoSetTerminal(value, false);
  479. }
  480. }
  481. //---------------------------------------------------------------------------
  482. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  483. {
  484. FTerminal = value;
  485. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  486. FAllowTransferPresetAutoSelect = false;
  487. try
  488. {
  489. TerminalChanged(Replace);
  490. }
  491. __finally
  492. {
  493. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  494. }
  495. if (Terminal != NULL)
  496. {
  497. TransferPresetAutoSelect();
  498. }
  499. }
  500. //---------------------------------------------------------------------------
  501. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  502. {
  503. DoSetTerminal(value, true);
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  507. {
  508. if (FTerminal != NULL)
  509. {
  510. UpdateTerminal(Terminal);
  511. }
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  515. {
  516. if (Terminal != NULL)
  517. {
  518. UpdateSessionColor((TColor)Terminal->StateData->Color);
  519. }
  520. DoTerminalListChanged();
  521. DebugAssert(!IsLocalBrowserMode()); // TODO
  522. if (Replaced)
  523. {
  524. RemoteDirView->ReplaceTerminal(Terminal);
  525. }
  526. else
  527. {
  528. RemoteDirView->Terminal = Terminal;
  529. }
  530. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  531. if (Terminal != NULL)
  532. {
  533. if (Terminal->Active)
  534. {
  535. Terminal->RefreshDirectory();
  536. }
  537. if (WinConfiguration->PreservePanelState)
  538. {
  539. if (Terminal->RemoteExplorerState != NULL)
  540. {
  541. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  542. }
  543. else
  544. {
  545. DirView(osRemote)->ClearState();
  546. }
  547. }
  548. if (!Replaced && Terminal->Active)
  549. {
  550. InitStatusBar();
  551. }
  552. }
  553. UpdateTransferList();
  554. // Update panels Enable state before refreshing the labels
  555. UpdateControls();
  556. }
  557. //---------------------------------------------------------------------------
  558. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  559. {
  560. if (Queue != value)
  561. {
  562. if (FQueue != NULL)
  563. {
  564. FQueue->OnListUpdate = NULL;
  565. FQueue->OnQueueItemUpdate = NULL;
  566. }
  567. FQueue = value;
  568. if (FQueue != NULL)
  569. {
  570. DebugAssert(FQueue->OnListUpdate == NULL);
  571. FQueue->OnListUpdate = QueueListUpdate;
  572. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  573. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  574. }
  575. QueueChanged();
  576. }
  577. }
  578. //---------------------------------------------------------------------------
  579. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  580. TListItem * Item)
  581. {
  582. if (FQueueActedItem == Item)
  583. {
  584. FQueueActedItem = NULL;
  585. if ((QueueView3->PopupMenu != NULL) &&
  586. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  587. {
  588. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  589. ReleaseCapture();
  590. }
  591. }
  592. if (Item->Data == FPendingQueueActionItem)
  593. {
  594. FPendingQueueActionItem = NULL;
  595. }
  596. }
  597. //---------------------------------------------------------------------------
  598. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  599. {
  600. {
  601. TGuard Guard(FQueueStatusSection);
  602. FQueueStatusInvalidated = false;
  603. if (FQueue != NULL)
  604. {
  605. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  606. }
  607. }
  608. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  609. {
  610. FMaxQueueLength = FQueueStatus->Count;
  611. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  612. }
  613. FQueueController->UpdateQueueStatus(FQueueStatus);
  614. SetQueueProgress();
  615. UpdateQueueView();
  616. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  617. if (IsEmpty && (Terminal != NULL))
  618. {
  619. TOnceDoneOperation OnceDoneOperation =
  620. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  621. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  622. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  623. {
  624. FQueue->Enabled = false;
  625. }
  626. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  627. {
  628. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  629. }
  630. }
  631. }
  632. //---------------------------------------------------------------------------
  633. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  634. {
  635. UnicodeString Result;
  636. if (FQueueStatus != NULL)
  637. {
  638. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  639. {
  640. TFileOperationProgressType * ProgressData =
  641. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  642. if ((ProgressData != NULL) && ProgressData->InProgress)
  643. {
  644. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  645. }
  646. }
  647. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  648. {
  649. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  650. }
  651. }
  652. return Result;
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  656. {
  657. ComponentVisible[fcQueueView] =
  658. (WinConfiguration->QueueView.Show == qvShow) ||
  659. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  660. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  661. }
  662. //---------------------------------------------------------------------------
  663. void __fastcall TCustomScpExplorerForm::QueueChanged()
  664. {
  665. if (FQueueStatus != NULL)
  666. {
  667. delete FQueueStatus;
  668. FQueueStatus = NULL;
  669. }
  670. UpdateQueueStatus(true);
  671. }
  672. //---------------------------------------------------------------------------
  673. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  674. {
  675. if (FQueue == Queue)
  676. {
  677. FQueueStatusInvalidated = true;
  678. }
  679. }
  680. //---------------------------------------------------------------------------
  681. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  682. TQueueItem * Item)
  683. {
  684. if (FQueue == Queue)
  685. {
  686. TGuard Guard(FQueueStatusSection);
  687. // this may be running in parallel with QueueChanged
  688. if (FQueueStatus != NULL)
  689. {
  690. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  691. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  692. {
  693. FRefreshLocalDirectory = (QueueItem == NULL) ||
  694. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  695. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  696. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  697. }
  698. if (QueueItem != NULL)
  699. {
  700. QueueItem->UserData = (void*)true;
  701. FQueueItemInvalidated = true;
  702. }
  703. }
  704. }
  705. }
  706. //---------------------------------------------------------------------------
  707. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  708. {
  709. // during standalone editing, we have no way to see/control queue,
  710. // so we have to always popup prompts automatically
  711. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  712. }
  713. //---------------------------------------------------------------------------
  714. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  715. {
  716. if (FQueueStatus != NULL)
  717. {
  718. bool QueueAutoPopup = IsQueueAutoPopup();
  719. bool NeedRefresh = FQueueController->NeedRefresh();
  720. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  721. FQueueItemInvalidated = false;
  722. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  723. bool Updated = false;
  724. TQueueItemProxy * QueueItem;
  725. bool WasUserAction;
  726. for (int Index = 0; Index < Limit; Index++)
  727. {
  728. bool Update = false;
  729. QueueItem = FQueueStatus->Items[Index];
  730. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  731. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  732. (FPendingQueueActionItem == NULL))
  733. {
  734. FPendingQueueActionItem = QueueItem;
  735. FUserActionTimer->Enabled = true;
  736. }
  737. if ((bool)QueueItem->UserData)
  738. {
  739. QueueItem->UserData = (void*)false;
  740. QueueItem->Update();
  741. Updated = true;
  742. Update = true;
  743. }
  744. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  745. {
  746. Update = true;
  747. }
  748. if (Update)
  749. {
  750. FQueueController->RefreshQueueItem(QueueItem);
  751. }
  752. }
  753. if (Updated)
  754. {
  755. NonVisualDataModule->UpdateNonVisibleActions();
  756. SetQueueProgress();
  757. }
  758. }
  759. }
  760. //---------------------------------------------------------------------------
  761. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  762. {
  763. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  764. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  765. }
  766. //---------------------------------------------------------------------------
  767. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  768. {
  769. if (Progress >= 0)
  770. {
  771. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  772. }
  773. else
  774. {
  775. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  776. }
  777. }
  778. //---------------------------------------------------------------------------
  779. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  780. {
  781. int OverallProgress;
  782. // FProgressForm is null when this is called from SetQueueProgress
  783. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  784. {
  785. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  786. }
  787. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  788. {
  789. OverallProgress = ProgressData->OverallProgress();
  790. }
  791. else
  792. {
  793. OverallProgress = -1;
  794. }
  795. SetTaskbarListProgressValue(OverallProgress);
  796. }
  797. //---------------------------------------------------------------------------
  798. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  799. {
  800. TTerminalManager::Instance()->QueueStatusUpdated();
  801. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  802. {
  803. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  804. {
  805. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  806. {
  807. TFileOperationProgressType * ProgressData = NULL;
  808. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  809. {
  810. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  811. }
  812. if ((ProgressData != NULL) &&
  813. ProgressData->InProgress)
  814. {
  815. SetTaskbarListProgressValue(ProgressData);
  816. }
  817. else
  818. {
  819. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  820. }
  821. }
  822. else
  823. {
  824. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  825. }
  826. }
  827. else
  828. {
  829. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  830. }
  831. }
  832. UpdateQueueLabel();
  833. }
  834. //---------------------------------------------------------------------------
  835. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  836. {
  837. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  838. if (FQueueStatus != NULL)
  839. {
  840. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  841. if (ActiveAndPendingPrimaryCount > 0)
  842. {
  843. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  844. }
  845. }
  846. QueueLabel->Caption = Caption;
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  850. {
  851. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  852. static_cast<TComponent*>(GetComponent(fcTransferList)));
  853. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  854. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  855. TransferList->Strings->BeginUpdate();
  856. try
  857. {
  858. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  859. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  860. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  861. if (FTransferDropDownHint.IsEmpty())
  862. {
  863. FTransferDropDownHint = TransferDropDown->Hint;
  864. }
  865. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  866. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  867. TransferDropDown->Text = StripHotkey(Name);
  868. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  869. (FTransferDropDownHint, StripHotkey(Name),
  870. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  871. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  872. // update the label, otherwise when it is updated only on the first draw
  873. // of the list, it is drawn "bold" for some reason
  874. FTransferListHoverIndex = TransferList->ItemIndex;
  875. UpdateTransferLabel();
  876. }
  877. __finally
  878. {
  879. TransferList->Strings->EndUpdate();
  880. }
  881. }
  882. //---------------------------------------------------------------------------
  883. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  884. {
  885. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  886. DebugAssert(Toolbar != NULL);
  887. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  888. }
  889. //---------------------------------------------------------------------------
  890. void __fastcall TCustomScpExplorerForm::UpdateActions()
  891. {
  892. TForm::UpdateActions();
  893. if (ComponentVisible[fcCustomCommandsBand])
  894. {
  895. UpdateCustomCommandsToolbar();
  896. }
  897. }
  898. //---------------------------------------------------------------------------
  899. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  900. {
  901. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  902. }
  903. //---------------------------------------------------------------------------
  904. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  905. {
  906. Color = GetBtnFaceColor();
  907. DebugAssert(Configuration && RemoteDirView);
  908. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  909. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  910. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  911. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  912. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  913. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  914. {
  915. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  916. // selection is not redrawn automatically when RowSelect changes
  917. RemoteDirView->Invalidate();
  918. }
  919. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  920. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  921. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  922. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  923. UpdateSessionsPageControlHeight();
  924. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  925. UpdateToolbarDisplayMode();
  926. UpdateTransferList();
  927. if (Terminal != NULL)
  928. {
  929. TransferPresetAutoSelect();
  930. }
  931. UpdateQueueView();
  932. UpdateControls();
  933. // this can be called even before constuctor finishes.
  934. if (FEditorManager != NULL)
  935. {
  936. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  937. }
  938. // this can be called even before constuctor finishes.
  939. if (FLocalEditors != NULL)
  940. {
  941. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  942. {
  943. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  944. }
  945. }
  946. if (ComponentVisible[fcCustomCommandsBand])
  947. {
  948. UpdateCustomCommandsToolbar();
  949. }
  950. // show only when keeping queue items forever,
  951. // otherwise, its enough to have in in the context menu
  952. QueueDeleteAllDoneQueueToolbarItem->Visible =
  953. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  954. if (FFileColorsCurrent != WinConfiguration->FileColors)
  955. {
  956. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  957. FFileColorsCurrent = WinConfiguration->FileColors;
  958. FileColorsChanged();
  959. }
  960. }
  961. //---------------------------------------------------------------------------
  962. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  963. {
  964. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  965. DirView->Invalidate();
  966. }
  967. //---------------------------------------------------------------------------
  968. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  969. {
  970. DoFileColorsChanged(RemoteDirView);
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  974. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  975. void * /*Arg*/)
  976. {
  977. if (Token != NULL)
  978. {
  979. TForm * Editor = dynamic_cast<TForm*>(Token);
  980. DebugAssert(Editor != NULL);
  981. ReconfigureEditorForm(Editor);
  982. }
  983. }
  984. //---------------------------------------------------------------------------
  985. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  986. {
  987. WinConfiguration->DDTransferConfirmation = asOn;
  988. }
  989. //---------------------------------------------------------------------------
  990. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  991. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  992. {
  993. // noop
  994. }
  995. //---------------------------------------------------------------------------
  996. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  997. TTransferDirection Direction, TTransferType Type, bool Temp,
  998. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  999. bool Confirm, bool DragDrop, int Options)
  1000. {
  1001. bool Result = true;
  1002. DebugAssert(Terminal && Terminal->Active);
  1003. // these parameters are known in advance
  1004. int Params =
  1005. FLAGMASK(Type == ttMove, cpDelete);
  1006. bool ToTemp = (Temp && (Direction == tdToLocal));
  1007. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1008. {
  1009. bool DisableNewerOnly =
  1010. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1011. ToTemp;
  1012. Options |=
  1013. FLAGMASK(ToTemp, coTemp) |
  1014. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1015. coDoNotShowAgain;
  1016. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1017. int CopyParamAttrs = (Direction == tdToRemote ?
  1018. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1019. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1020. int OutputOptions =
  1021. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1022. cooDoNotShowAgain);
  1023. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1024. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1025. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1026. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1027. if (Result)
  1028. {
  1029. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1030. {
  1031. if (DragDrop)
  1032. {
  1033. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1034. {
  1035. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1036. NULL, 0, EnableDDTransferConfirmation, NULL);
  1037. }
  1038. WinConfiguration->DDTransferConfirmation = asOff;
  1039. }
  1040. else
  1041. {
  1042. WinConfiguration->ConfirmTransferring = false;
  1043. }
  1044. }
  1045. else
  1046. {
  1047. // User explicitly unchecked "do not show again",
  1048. // so show him the dialog the next time
  1049. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1050. {
  1051. WinConfiguration->DDTransferConfirmation = asOn;
  1052. }
  1053. }
  1054. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1055. }
  1056. }
  1057. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1058. {
  1059. Configuration->Usage->Inc(L"TransfersOnBackground");
  1060. // these parameter are known only after transfer dialog
  1061. Params |=
  1062. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1063. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1064. Result = false;
  1065. ClearTransferSourceSelection(Direction);
  1066. }
  1067. return Result;
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1071. {
  1072. if (FOnFileOperationFinished != NULL)
  1073. {
  1074. FOnFileOperationFinished(UnicodeString(), true);
  1075. }
  1076. else
  1077. {
  1078. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1079. if (HasDirView[Side])
  1080. {
  1081. DirView(Side)->SelectAll(smNone);
  1082. }
  1083. }
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1087. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1088. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1089. int Params)
  1090. {
  1091. DebugAssert(Queue != NULL);
  1092. bool SingleFile = false;
  1093. if (FileList->Count == 1)
  1094. {
  1095. if (Direction == tdToRemote)
  1096. {
  1097. UnicodeString FileName = FileList->Strings[0];
  1098. SingleFile = FileExists(ApiPath(FileName));
  1099. }
  1100. else
  1101. {
  1102. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1103. SingleFile = !File->IsDirectory;
  1104. }
  1105. }
  1106. TQueueItem * QueueItem;
  1107. if (Direction == tdToRemote)
  1108. {
  1109. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1110. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1111. }
  1112. else
  1113. {
  1114. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1115. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1116. }
  1117. AddQueueItem(Queue, QueueItem, Terminal);
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1121. {
  1122. if (Queue->IsEmpty)
  1123. {
  1124. Terminal->QueueOperationStart = Now();
  1125. }
  1126. Queue->AddItem(QueueItem);
  1127. }
  1128. //---------------------------------------------------------------------------
  1129. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1130. {
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::InitControls()
  1134. {
  1135. DebugAssert(FDocks != NULL);
  1136. for (int Index = 0; Index < ComponentCount; Index++)
  1137. {
  1138. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1139. if ((Dock != NULL) && (Dock->Tag == 0))
  1140. {
  1141. FDocks->Add(Dock);
  1142. }
  1143. }
  1144. CollectItemsWithTextDisplayMode(this);
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1148. {
  1149. ConfigurationChanged();
  1150. QueuePanel->Height =
  1151. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1152. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1153. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1154. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1155. }
  1156. //---------------------------------------------------------------------------
  1157. void __fastcall TCustomScpExplorerForm::StoreParams()
  1158. {
  1159. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1160. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1161. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1162. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1163. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1164. }
  1165. //---------------------------------------------------------------------------
  1166. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1167. {
  1168. if (!FFormRestored)
  1169. {
  1170. FFormRestored = true;
  1171. RestoreFormParams();
  1172. }
  1173. TForm::CreateParams(Params);
  1174. }
  1175. //---------------------------------------------------------------------------
  1176. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1177. {
  1178. DebugAssert(FDocks != NULL);
  1179. for (int Index = 0; Index < FDocks->Count; Index++)
  1180. {
  1181. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1182. }
  1183. }
  1184. //---------------------------------------------------------------------------
  1185. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1186. {
  1187. SetDockAllowDrag(true);
  1188. while (!ButtonsStr.IsEmpty())
  1189. {
  1190. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1191. UnicodeString Name = CutToChar(S, L'=', true);
  1192. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1193. {
  1194. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1195. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1196. {
  1197. while (!S.IsEmpty())
  1198. {
  1199. UnicodeString S2 = CutToChar(S, L',', true);
  1200. UnicodeString Name = CutToChar(S2, L':', true);
  1201. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1202. {
  1203. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1204. if (SameText(GetToolbarItemName(Item), Name))
  1205. {
  1206. // forward compatibility
  1207. UnicodeString S3 = CutToChar(S2, L':', true);
  1208. bool Visible = StrToIntDef(S3, 0);
  1209. Item->Visible = Visible;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. }
  1215. }
  1216. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1217. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1218. }
  1219. //---------------------------------------------------------------------------
  1220. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1221. {
  1222. UnicodeString Result;
  1223. SetDockAllowDrag(true);
  1224. Result = ::GetToolbarsLayoutStr(this);
  1225. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1226. return Result;
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1230. {
  1231. UnicodeString Result;
  1232. if (Item->Action != NULL)
  1233. {
  1234. Result = Item->Action->Name;
  1235. Result = RemoveSuffix(Result, L"Action");
  1236. }
  1237. else
  1238. {
  1239. Result = Item->Name;
  1240. Result = RemoveSuffix(Result, L"SubmenuItem");
  1241. Result = RemoveSuffix(Result, L"Item");
  1242. }
  1243. return Result;
  1244. }
  1245. //---------------------------------------------------------------------------
  1246. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1247. {
  1248. UnicodeString Result;
  1249. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1250. {
  1251. UnicodeString ToolbarStr;
  1252. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1253. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1254. {
  1255. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1256. {
  1257. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1258. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1259. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1260. // and save the changes here only.
  1261. if (!Item->Visible)
  1262. {
  1263. UnicodeString Name = GetToolbarItemName(Item);
  1264. DebugAssert(Name.Pos(L"TBX") == 0);
  1265. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1266. }
  1267. }
  1268. }
  1269. if (!ToolbarStr.IsEmpty())
  1270. {
  1271. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1272. }
  1273. }
  1274. return Result;
  1275. }
  1276. //---------------------------------------------------------------------------
  1277. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1278. {
  1279. DebugAssert(FProgressForm == NULL);
  1280. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1281. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1282. FProgressForm->DeleteLocalToRecycleBin =
  1283. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1284. FProgressForm->DeleteRemoteToRecycleBin =
  1285. (Terminal != NULL) &&
  1286. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1287. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1288. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1289. LockWindow();
  1290. if (FTaskbarList != NULL)
  1291. {
  1292. // Actually, do not know what hides the progress once the operation finishes
  1293. SetTaskbarListProgressState(TBPF_NORMAL);
  1294. }
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1298. {
  1299. UnlockWindow();
  1300. SAFE_DESTROY(FProgressForm);
  1301. SetQueueProgress();
  1302. }
  1303. //---------------------------------------------------------------------------
  1304. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1305. TFileOperationProgressType & ProgressData)
  1306. {
  1307. // Download to temporary local directory
  1308. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1309. {
  1310. bool Continue = true;
  1311. FPendingTempSpaceWarn = false;
  1312. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1313. if (!Continue)
  1314. {
  1315. Abort();
  1316. }
  1317. }
  1318. // operation is being executed and we still didn't show up progress form
  1319. if (ProgressData.InProgress && !FProgressForm)
  1320. {
  1321. CreateProgressForm(NULL);
  1322. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1323. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1324. }
  1325. // operation is finished (or terminated), so we hide progress form
  1326. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1327. {
  1328. DestroyProgressForm();
  1329. if (ProgressData.Operation == foCalculateSize)
  1330. {
  1331. // When calculating size before transfer, the abort caused by
  1332. // cancel flag set due to "MoveToQueue" below,
  1333. // is not propagated back to us in ExecuteFileOperation,
  1334. // as it is silently swallowed.
  1335. // So we explicitly (re)throw it here.
  1336. if (FMoveToQueue)
  1337. {
  1338. Abort();
  1339. }
  1340. }
  1341. else
  1342. {
  1343. if ((ProgressData.Cancel == csContinue) &&
  1344. !FAutoOperation)
  1345. {
  1346. OperationComplete(ProgressData.StartTime);
  1347. }
  1348. }
  1349. }
  1350. if (FProgressForm &&
  1351. // Can happen only during synchronization, when ending one of the operations
  1352. ProgressData.InProgress)
  1353. {
  1354. FProgressForm->SetProgressData(ProgressData);
  1355. if (FTaskbarList != NULL)
  1356. {
  1357. DebugAssert(ProgressData.InProgress);
  1358. SetTaskbarListProgressValue(&ProgressData);
  1359. }
  1360. if (FProgressForm->Cancel > csContinue)
  1361. {
  1362. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1363. if (FProgressForm->Cancel == csCancelFile)
  1364. {
  1365. FProgressForm->ClearCancel();
  1366. }
  1367. // cancel cancels even the move
  1368. FMoveToQueue = false;
  1369. }
  1370. else if (FProgressForm->MoveToQueue)
  1371. {
  1372. FMoveToQueue = true;
  1373. ProgressData.SetCancelAtLeast(csCancel);
  1374. }
  1375. if ((FTransferResumeList != NULL) &&
  1376. ProgressData.InProgress &&
  1377. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1378. !ProgressData.FullFileName.IsEmpty())
  1379. {
  1380. if ((FTransferResumeList->Count == 0) ||
  1381. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1382. {
  1383. // note that we do not recognize directories from files here
  1384. FTransferResumeList->Add(ProgressData.FullFileName);
  1385. }
  1386. }
  1387. }
  1388. }
  1389. //---------------------------------------------------------------------------
  1390. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1391. const TDateTime & StartTime)
  1392. {
  1393. if (GUIConfiguration->BeepOnFinish &&
  1394. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1395. {
  1396. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1397. DWORD Sound;
  1398. if (!ExtractFileExt(BeepSound).IsEmpty())
  1399. {
  1400. Sound = SND_FILENAME;
  1401. }
  1402. else
  1403. {
  1404. Sound = SND_ALIAS;
  1405. }
  1406. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1407. }
  1408. }
  1409. //---------------------------------------------------------------------------
  1410. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1411. TFileOperationProgressType & ProgressData)
  1412. {
  1413. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1414. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1415. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1416. FileOperationProgress(ProgressData);
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1420. {
  1421. UnicodeString Result;
  1422. if (FProgressForm != NULL)
  1423. {
  1424. Result = FProgressForm->ProgressStr();
  1425. }
  1426. return Result;
  1427. }
  1428. //---------------------------------------------------------------------------
  1429. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1430. bool DragDrop)
  1431. {
  1432. return (!DragDrop && (DropSourceControl == NULL)) ||
  1433. (DropSourceControl == DirView(osOther));
  1434. }
  1435. //---------------------------------------------------------------------------
  1436. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1437. TFileOperation Operation, TOperationSide Side,
  1438. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1439. TOnceDoneOperation & OnceDoneOperation)
  1440. {
  1441. if (!FAutoOperation)
  1442. {
  1443. // no selection on "/upload", form servers only as event handler
  1444. // (it is not displayed)
  1445. if (PanelOperation(Side, FDragDropOperation) &&
  1446. Visible && (Operation != foCalculateSize) &&
  1447. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1448. {
  1449. if (FOnFileOperationFinished != NULL)
  1450. {
  1451. FOnFileOperationFinished(FileName, Success);
  1452. }
  1453. else
  1454. {
  1455. TCustomDirView * DView = DirView(Side);
  1456. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1457. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1458. // this can happen when local drive is unplugged in the middle of the operation
  1459. if (Item != NULL)
  1460. {
  1461. if (Success) Item->Selected = false;
  1462. DView->MakeProgressVisible(Item);
  1463. }
  1464. }
  1465. }
  1466. if ((Operation == foCopy) || (Operation == foMove))
  1467. {
  1468. DebugAssert(!IsLocalBrowserMode()); // TODO
  1469. if (Side == osLocal)
  1470. {
  1471. Configuration->Usage->Inc(L"UploadedFiles");
  1472. }
  1473. else
  1474. {
  1475. Configuration->Usage->Inc(L"DownloadedFiles");
  1476. }
  1477. }
  1478. }
  1479. if (Success && (FSynchronizeController != NULL))
  1480. {
  1481. DebugAssert(!IsSideLocalBrowser(Side));
  1482. if (Operation == foCopy)
  1483. {
  1484. DebugAssert(Side == osLocal);
  1485. FSynchronizeController->LogOperation(soUpload, FileName);
  1486. }
  1487. else if (Operation == foDelete)
  1488. {
  1489. DebugAssert(Side == osRemote);
  1490. FSynchronizeController->LogOperation(soDelete, FileName);
  1491. }
  1492. }
  1493. if (FProgressForm)
  1494. {
  1495. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1496. }
  1497. }
  1498. //---------------------------------------------------------------------------
  1499. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1500. TFileOperation Operation, TOperationSide Side,
  1501. bool Temp, const UnicodeString & FileName, Boolean Success,
  1502. TOnceDoneOperation & OnceDoneOperation)
  1503. {
  1504. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1505. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1506. OnceDoneOperation);
  1507. }
  1508. //---------------------------------------------------------------------------
  1509. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1510. {
  1511. return false;
  1512. }
  1513. //---------------------------------------------------------------------------
  1514. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1515. {
  1516. return false;
  1517. }
  1518. //---------------------------------------------------------------------------
  1519. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1520. {
  1521. return NULL;
  1522. }
  1523. //---------------------------------------------------------------------------
  1524. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1525. {
  1526. DebugAssert(GetSide(Side) == osRemote);
  1527. DebugUsedParam(Side);
  1528. return RemoteDirView;
  1529. }
  1530. //---------------------------------------------------------------------------
  1531. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1532. {
  1533. DebugAssert(GetSide(Side) == osRemote);
  1534. DebugUsedParam(Side);
  1535. return RemoteDriveView;
  1536. }
  1537. //---------------------------------------------------------------------------
  1538. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1539. {
  1540. DebugAssert(GetSide(Side) == osRemote);
  1541. DebugUsedParam(Side);
  1542. return (Terminal != NULL) && Terminal->Active;
  1543. }
  1544. //---------------------------------------------------------------------------
  1545. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1546. TOperationSide Side, int FilesOnly)
  1547. {
  1548. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1549. }
  1550. //---------------------------------------------------------------------------
  1551. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1552. TOperationSide Side, int FilesOnly)
  1553. {
  1554. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1555. }
  1556. //---------------------------------------------------------------------------
  1557. struct THistoryItemData
  1558. {
  1559. short int Side;
  1560. short int Index;
  1561. };
  1562. //---------------------------------------------------------------------------
  1563. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1564. {
  1565. DirView(Side)->HistoryGo(Index);
  1566. }
  1567. //---------------------------------------------------------------------------
  1568. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1569. {
  1570. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1571. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1572. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1573. }
  1574. //---------------------------------------------------------------------------
  1575. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1576. bool Back)
  1577. {
  1578. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1579. {
  1580. TCustomDirView * DView = DirView(Side);
  1581. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1582. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1583. if (ICount > 10)
  1584. {
  1585. ICount = 10;
  1586. }
  1587. Menu->Items->Clear();
  1588. THistoryItemData Data;
  1589. Data.Side = Side;
  1590. for (short int i = 1; i <= ICount; i++)
  1591. {
  1592. TTBCustomItem * Item = new TTBXItem(Menu);
  1593. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1594. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1595. Item->Hint = DView->HistoryPath[Data.Index];
  1596. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1597. Item->Tag = *reinterpret_cast<int*>(&Data);
  1598. Item->OnClick = HistoryItemClick;
  1599. Menu->Items->Add(Item);
  1600. }
  1601. }
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1605. TOperationSide Side, bool Back)
  1606. {
  1607. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1608. {
  1609. // workaround
  1610. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1611. // operator new allocates memory in Pascal code, but calls inline
  1612. // contructor in C++, leading in problems, the function does
  1613. // both in Pascal code
  1614. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1615. UpdateHistoryMenu(Side, Back);
  1616. }
  1617. return FHistoryMenu[Side == osLocal][Back];
  1618. }
  1619. //---------------------------------------------------------------------------
  1620. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1621. TCustomDirView *Sender)
  1622. {
  1623. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1624. UpdateHistoryMenu(Side, true);
  1625. UpdateHistoryMenu(Side, false);
  1626. }
  1627. //---------------------------------------------------------------------------
  1628. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1629. {
  1630. // remote custom commands can be executed only if the server supports shell commands
  1631. // or have secondary shell
  1632. return (FTerminal != NULL) && FTerminal->Active && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1633. }
  1634. //---------------------------------------------------------------------------
  1635. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1636. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1637. {
  1638. int Result;
  1639. TFileCustomCommand RemoteCustomCommand;
  1640. TLocalCustomCommand LocalCustomCommand;
  1641. TFileCustomCommand * NonInteractiveCustomCommand =
  1642. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1643. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1644. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1645. if (FLAGCLEAR(Command.Params, ccLocal))
  1646. {
  1647. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1648. // custom command that does not operate with files can be executed anytime ...
  1649. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1650. {
  1651. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1652. {
  1653. // ... but do not show such command in remote file menu (TODO)
  1654. Result = AllowedState;
  1655. }
  1656. else
  1657. {
  1658. Result = -1;
  1659. }
  1660. }
  1661. else
  1662. {
  1663. if ((ListType == ccltAll) || (ListType == ccltFile))
  1664. {
  1665. Result = (!IsSideLocalBrowser(FCurrentSide) && DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1666. }
  1667. else
  1668. {
  1669. Result = -1;
  1670. }
  1671. }
  1672. }
  1673. else
  1674. {
  1675. // custom command that does not operate with files can be executed anytime
  1676. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1677. {
  1678. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1679. }
  1680. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1681. {
  1682. if ((ListType == ccltAll) || (ListType == ccltFile))
  1683. {
  1684. // special case is "diff"-style command that can be executed over any side,
  1685. // if we have both sides
  1686. Result =
  1687. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1688. // directly (instead of EnableSelectedOperation) to pass
  1689. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1690. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1691. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1692. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1693. }
  1694. else if (ListType == ccltBoth)
  1695. {
  1696. DebugAssert(!IsLocalBrowserMode());
  1697. Result = 1;
  1698. }
  1699. else
  1700. {
  1701. Result = -1;
  1702. }
  1703. }
  1704. else
  1705. {
  1706. if ((ListType == ccltAll) || (ListType == ccltFile))
  1707. {
  1708. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1709. }
  1710. else
  1711. {
  1712. Result = -1;
  1713. }
  1714. }
  1715. }
  1716. return Result;
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1720. TStrings * FileList, const TCustomCommandType & ACommand,
  1721. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1722. {
  1723. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1724. {
  1725. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1726. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1727. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1728. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1729. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1730. bool Capture =
  1731. FLAGSET(ACommand.Params, ccShowResults) ||
  1732. FLAGSET(ACommand.Params, ccCopyResults) ||
  1733. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1734. TCaptureOutputEvent OutputEvent = NULL;
  1735. DebugAssert(FCapturedLog == NULL);
  1736. if (Capture)
  1737. {
  1738. FCapturedLog = new TStringList();
  1739. OutputEvent = TerminalCaptureLog;
  1740. }
  1741. try
  1742. {
  1743. if (!RemoteCustomCommand.IsFileCommand(Command))
  1744. {
  1745. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1746. OutputEvent);
  1747. }
  1748. else
  1749. {
  1750. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1751. }
  1752. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1753. {
  1754. if (FLAGSET(ACommand.Params, ccCopyResults))
  1755. {
  1756. CopyToClipboard(FCapturedLog);
  1757. }
  1758. if (FLAGSET(ACommand.Params, ccShowResults))
  1759. {
  1760. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1761. }
  1762. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1763. {
  1764. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1765. }
  1766. }
  1767. }
  1768. __finally
  1769. {
  1770. SAFE_DESTROY(FCapturedLog);
  1771. }
  1772. }
  1773. }
  1774. //---------------------------------------------------------------------------
  1775. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1776. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1777. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1778. {
  1779. DebugAssert(!IsLocalBrowserMode());
  1780. TStrings * LocalFileList = NULL;
  1781. TStrings * RemoteFileList = NULL;
  1782. TStrings * RemoteFileListFull = NULL;
  1783. try
  1784. {
  1785. if (LocalFileCommand)
  1786. {
  1787. if (ALocalFileList == NULL)
  1788. {
  1789. DebugAssert(HasDirView[osLocal]);
  1790. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1791. // directly (instead of EnableSelectedOperation) to pass
  1792. // false to FocusedFileOnlyWhenFocused
  1793. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1794. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1795. }
  1796. else
  1797. {
  1798. LocalFileList = ALocalFileList;
  1799. }
  1800. if (FileListCommand)
  1801. {
  1802. if (LocalFileList->Count != 1)
  1803. {
  1804. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1805. }
  1806. }
  1807. else
  1808. {
  1809. if ((LocalFileList->Count != 1) &&
  1810. (FileList->Count != 1) &&
  1811. (LocalFileList->Count != FileList->Count))
  1812. {
  1813. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1814. }
  1815. }
  1816. }
  1817. UnicodeString RootTempDir;
  1818. UnicodeString TempDir;
  1819. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1820. if (!RemoteFiles)
  1821. {
  1822. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1823. }
  1824. try
  1825. {
  1826. TDateTimes RemoteFileTimes;
  1827. if (RemoteFiles)
  1828. {
  1829. RemoteFileList = FileList;
  1830. }
  1831. else
  1832. {
  1833. RemoteFileList = new TStringList();
  1834. TMakeLocalFileListParams MakeFileListParam;
  1835. MakeFileListParam.FileList = RemoteFileList;
  1836. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1837. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1838. MakeFileListParam.Recursive =
  1839. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1840. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1841. if (!MakeFileListParam.Recursive)
  1842. {
  1843. RemoteFileTimes.clear();
  1844. RemoteFileListFull = new TStringList();
  1845. MakeFileListParam.FileList = RemoteFileListFull;
  1846. MakeFileListParam.Recursive = true;
  1847. MakeFileListParam.IncludeDirs = false;
  1848. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1849. }
  1850. else
  1851. {
  1852. RemoteFileListFull = RemoteFileList;
  1853. }
  1854. }
  1855. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1856. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1857. if (!NonBlocking)
  1858. {
  1859. int Count;
  1860. if (FileListCommand)
  1861. {
  1862. Count = 1;
  1863. }
  1864. else if (LocalFileCommand)
  1865. {
  1866. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1867. }
  1868. else
  1869. {
  1870. Count = RemoteFileList->Count;
  1871. }
  1872. Progress.Start(foCustomCommand, osRemote, Count);
  1873. DebugAssert(FProgressForm != NULL);
  1874. FProgressForm->ReadOnly = true;
  1875. }
  1876. try
  1877. {
  1878. if (FileListCommand)
  1879. {
  1880. UnicodeString LocalFile;
  1881. // MakeFileList does not delimit filenames
  1882. UnicodeString FileList = MakeFileList(RemoteFileList);
  1883. if (LocalFileCommand)
  1884. {
  1885. DebugAssert(LocalFileList->Count == 1);
  1886. LocalFile = LocalFileList->Strings[0];
  1887. }
  1888. TLocalCustomCommand CustomCommand(Data,
  1889. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1890. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1891. if (NonBlocking)
  1892. {
  1893. DebugAssert(!POutput);
  1894. ExecuteShellChecked(ShellCommand);
  1895. }
  1896. else
  1897. {
  1898. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1899. }
  1900. }
  1901. else if (LocalFileCommand)
  1902. {
  1903. if (LocalFileList->Count == 1)
  1904. {
  1905. UnicodeString LocalFile = LocalFileList->Strings[0];
  1906. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1907. {
  1908. UnicodeString FileName = RemoteFileList->Strings[Index];
  1909. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1910. Progress.SetFile(FileNameForProgress);
  1911. TLocalCustomCommand CustomCommand(Data,
  1912. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1913. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1914. TOnceDoneOperation OnceDoneOperation;
  1915. // Do not unselect anything, as it won't work with two panels anyway
  1916. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1917. }
  1918. }
  1919. else if (RemoteFileList->Count == 1)
  1920. {
  1921. UnicodeString FileName = RemoteFileList->Strings[0];
  1922. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1923. {
  1924. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1925. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1926. Progress.SetFile(LocalFileNameOnly);
  1927. TLocalCustomCommand CustomCommand(
  1928. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1929. FileName, LocalFileName, L"");
  1930. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1931. TOnceDoneOperation OnceDoneOperation;
  1932. // Do not unselect anything, as it won't work with two panels anyway
  1933. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1934. }
  1935. }
  1936. else
  1937. {
  1938. if (LocalFileList->Count != RemoteFileList->Count)
  1939. {
  1940. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1941. }
  1942. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1943. {
  1944. UnicodeString FileName = RemoteFileList->Strings[Index];
  1945. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1946. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1947. Progress.SetFile(LocalFileNameOnly); // sic
  1948. TLocalCustomCommand CustomCommand(
  1949. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1950. FileName, LocalFileName, L"");
  1951. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1952. TOnceDoneOperation OnceDoneOperation;
  1953. // Do not unselect anything, as it won't work with two panels anyway
  1954. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1955. }
  1956. }
  1957. }
  1958. else
  1959. {
  1960. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1961. {
  1962. UnicodeString FileName = RemoteFileList->Strings[Index];
  1963. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1964. Progress.SetFile(FileNameForProgress);
  1965. TLocalCustomCommand CustomCommand(Data,
  1966. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1967. FileName, L"", L"");
  1968. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1969. TOnceDoneOperation OnceDoneOperation;
  1970. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1971. }
  1972. }
  1973. }
  1974. __finally
  1975. {
  1976. if (!NonBlocking)
  1977. {
  1978. Progress.Stop();
  1979. }
  1980. }
  1981. DebugAssert(!FAutoOperation);
  1982. if (!RemoteFiles)
  1983. {
  1984. TempDir = IncludeTrailingBackslash(TempDir);
  1985. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1986. {
  1987. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1988. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1989. // Skip directories, process only nested files.
  1990. // The check is redundant as FileAge fails for directories anyway.
  1991. !DirectoryExists(FileName))
  1992. {
  1993. UnicodeString RemoteDir =
  1994. UnixExtractFileDir(
  1995. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1996. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1997. TDateTime NewTime;
  1998. if (FileAge(FileName, NewTime) &&
  1999. (NewTime != RemoteFileTimes[Index]))
  2000. {
  2001. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2002. TemporaryFileCopyParam(CopyParam);
  2003. CopyParam.FileMask = L"";
  2004. FAutoOperation = true;
  2005. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2006. TemporaryFilesList->Add(FileName);
  2007. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2008. }
  2009. }
  2010. }
  2011. }
  2012. }
  2013. __finally
  2014. {
  2015. FAutoOperation = false;
  2016. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2017. {
  2018. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2019. }
  2020. }
  2021. }
  2022. __finally
  2023. {
  2024. if (RemoteFileList != FileList)
  2025. {
  2026. delete RemoteFileList;
  2027. }
  2028. if (LocalFileList != ALocalFileList)
  2029. {
  2030. delete LocalFileList;
  2031. }
  2032. if (RemoteFileListFull != RemoteFileList)
  2033. {
  2034. delete RemoteFileListFull;
  2035. }
  2036. }
  2037. }
  2038. //---------------------------------------------------------------------------
  2039. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2040. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2041. bool FileListCommand, UnicodeString * POutput)
  2042. {
  2043. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2044. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2045. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2046. {
  2047. UnicodeString FileName = SelectedFileList->Strings[Index];
  2048. if (DirectoryExists(FileName))
  2049. {
  2050. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2051. {
  2052. LocalFileList->Add(FileName);
  2053. }
  2054. if (FLAGSET(ACommand.Params, ccRecursive))
  2055. {
  2056. TMakeLocalFileListParams MakeFileListParam;
  2057. MakeFileListParam.FileList = LocalFileList.get();
  2058. MakeFileListParam.FileTimes = NULL;
  2059. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2060. MakeFileListParam.Recursive = true;
  2061. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2062. }
  2063. }
  2064. else
  2065. {
  2066. LocalFileList->Add(FileName);
  2067. }
  2068. }
  2069. UnicodeString RemotePath;
  2070. if (Terminal != NULL)
  2071. {
  2072. RemotePath = Terminal->CurrentDirectory;
  2073. }
  2074. if (FileListCommand)
  2075. {
  2076. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2077. TLocalCustomCommand CustomCommand(
  2078. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2079. L"", L"", FileList);
  2080. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2081. }
  2082. else
  2083. {
  2084. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2085. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2086. DebugAssert(FProgressForm != NULL);
  2087. FProgressForm->ReadOnly = true;
  2088. try
  2089. {
  2090. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2091. {
  2092. UnicodeString FileName = LocalFileList->Strings[Index];
  2093. Progress.SetFile(FileName);
  2094. TLocalCustomCommand CustomCommand(
  2095. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2096. FileName, L"", L"");
  2097. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2098. TOnceDoneOperation OnceDoneOperation;
  2099. Progress.Finish(FileName, true, OnceDoneOperation);
  2100. }
  2101. }
  2102. __finally
  2103. {
  2104. Progress.Stop();
  2105. }
  2106. }
  2107. }
  2108. //---------------------------------------------------------------------------
  2109. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2110. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2111. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2112. {
  2113. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2114. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2115. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2116. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2117. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2118. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2119. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2120. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2121. std::unique_ptr<UnicodeString> POutput;
  2122. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2123. FLAGSET(ACommand.Params, ccCopyResults))
  2124. {
  2125. POutput.reset(new UnicodeString());
  2126. }
  2127. if (!LocalCustomCommand.IsFileCommand(Command))
  2128. {
  2129. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2130. }
  2131. // remote files?
  2132. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2133. {
  2134. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2135. }
  2136. // local files
  2137. else
  2138. {
  2139. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2140. }
  2141. if (POutput.get() != NULL)
  2142. {
  2143. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2144. int P = POutput->Pos(sLineBreak);
  2145. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2146. {
  2147. POutput->SetLength(P - 1);
  2148. }
  2149. // Copy even empty output to clipboard
  2150. if (FLAGSET(ACommand.Params, ccCopyResults))
  2151. {
  2152. CopyToClipboard(*POutput);
  2153. }
  2154. // But do not show an empty message box.
  2155. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2156. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2157. !POutput->IsEmpty())
  2158. {
  2159. TClipboardHandler ClipboardHandler;
  2160. ClipboardHandler.Text = *POutput;
  2161. TMessageParams Params;
  2162. TQueryButtonAlias Aliases[1];
  2163. Aliases[0].Button = qaRetry;
  2164. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2165. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2166. Params.Aliases = Aliases;
  2167. Params.AliasesCount = LENOF(Aliases);
  2168. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2169. }
  2170. }
  2171. }
  2172. //---------------------------------------------------------------------------
  2173. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2174. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2175. {
  2176. TCustomCommandData Data;
  2177. UnicodeString Site;
  2178. UnicodeString RemotePath;
  2179. if (Terminal != NULL)
  2180. {
  2181. Data = TCustomCommandData(Terminal);
  2182. Site = Terminal->SessionData->SessionKey;
  2183. RemotePath = Terminal->CurrentDirectory;
  2184. }
  2185. UnicodeString HelpKeyword = ACommand.HomePage;
  2186. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2187. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2188. {
  2189. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2190. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2191. {
  2192. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2193. }
  2194. else
  2195. {
  2196. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2197. }
  2198. if (!DoCustomCommandOptionsDialog(
  2199. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2200. {
  2201. Abort();
  2202. }
  2203. }
  2204. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2205. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2206. {
  2207. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2208. }
  2209. else
  2210. {
  2211. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2212. }
  2213. }
  2214. //---------------------------------------------------------------------------
  2215. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2216. const TCustomCommandType & Command)
  2217. {
  2218. DebugAssert(FCustomCommandLocalFileList != NULL);
  2219. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2220. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2221. TStrings * LocalFileList = new TStringList();
  2222. TStrings * RemoteFileList = new TStringList();
  2223. try
  2224. {
  2225. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2226. {
  2227. LocalFileList->Clear();
  2228. LocalFileList->AddObject(
  2229. FCustomCommandLocalFileList->Strings[Index],
  2230. FCustomCommandLocalFileList->Objects[Index]);
  2231. RemoteFileList->Clear();
  2232. RemoteFileList->AddObject(
  2233. FCustomCommandRemoteFileList->Strings[Index],
  2234. FCustomCommandRemoteFileList->Objects[Index]);
  2235. CustomCommand(RemoteFileList, Command, LocalFileList);
  2236. }
  2237. }
  2238. __finally
  2239. {
  2240. delete LocalFileList;
  2241. delete RemoteFileList;
  2242. }
  2243. }
  2244. //---------------------------------------------------------------------------
  2245. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2246. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2247. {
  2248. delete FCustomCommandLocalFileList;
  2249. delete FCustomCommandRemoteFileList;
  2250. // takeover ownership,
  2251. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2252. FCustomCommandLocalFileList = LocalFileList;
  2253. FCustomCommandRemoteFileList = RemoteFileList;
  2254. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2255. if (Button != NULL)
  2256. {
  2257. FCustomCommandMenu->Items->Clear();
  2258. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2259. MenuPopup(FCustomCommandMenu, Button);
  2260. }
  2261. else
  2262. {
  2263. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2264. }
  2265. }
  2266. //---------------------------------------------------------------------------
  2267. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2268. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2269. {
  2270. DebugAssert(FCapturedLog != NULL);
  2271. if ((OutputType == cotOutput) || (OutputType == cotError))
  2272. {
  2273. FCapturedLog->Add(AddedLine);
  2274. }
  2275. }
  2276. //---------------------------------------------------------------------------
  2277. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2278. TOperationSide Side)
  2279. {
  2280. return
  2281. (Side == osOther) &&
  2282. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2283. }
  2284. //---------------------------------------------------------------------------
  2285. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2286. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2287. {
  2288. TTBItemOptions O;
  2289. O = Item->Options;
  2290. TCustomDirView * DView = DirView(Side);
  2291. if ((DView->ItemFocused != NULL) &&
  2292. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2293. // when resolving links is disabled, default action is to enter the directory,
  2294. // no matter what DoubleClickAction is configured to
  2295. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2296. // Can only Edit files, but can Open/Copy even directories
  2297. ((DoubleClickAction != dcaEdit) ||
  2298. !DView->ItemIsDirectory(DView->ItemFocused)))
  2299. {
  2300. Item->Options = O << tboDefault;
  2301. }
  2302. else
  2303. {
  2304. Item->Options = O >> tboDefault;
  2305. }
  2306. }
  2307. //---------------------------------------------------------------------------
  2308. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2309. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2310. {
  2311. TCustomDirView * DView = DirView(Side);
  2312. TListItem * Item = DView->ItemFocused;
  2313. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2314. EnableFocusedOperation[Side])
  2315. {
  2316. TPoint ClientPoint;
  2317. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2318. TPoint(0, 0) : MousePos;
  2319. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2320. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2321. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2322. }
  2323. }
  2324. //---------------------------------------------------------------------------
  2325. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2326. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2327. {
  2328. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2329. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2330. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2331. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2332. Handled = true;
  2333. }
  2334. //---------------------------------------------------------------------------
  2335. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2336. {
  2337. }
  2338. //---------------------------------------------------------------------------
  2339. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2340. {
  2341. DebugAssert(FErrorList == NULL);
  2342. if (WinConfiguration->ContinueOnError)
  2343. {
  2344. FErrorList = new TStringList();
  2345. Configuration->Usage->Inc(L"ContinuationsOnError");
  2346. }
  2347. NonVisualDataModule->StartBusy();
  2348. }
  2349. //---------------------------------------------------------------------------
  2350. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2351. {
  2352. NonVisualDataModule->EndBusy();
  2353. if (FErrorList)
  2354. {
  2355. HandleErrorList(FErrorList);
  2356. }
  2357. }
  2358. //---------------------------------------------------------------------------
  2359. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2360. const TCopyParamType & CopyParam)
  2361. {
  2362. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2363. {
  2364. Configuration->Usage->Inc(L"FileMaskUses");
  2365. }
  2366. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2367. {
  2368. Configuration->Usage->Inc(L"OperationMaskUses");
  2369. }
  2370. }
  2371. //---------------------------------------------------------------------------
  2372. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2373. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2374. {
  2375. DebugAssert(!IsLocalBrowserMode());
  2376. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2377. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2378. TTransferOperationParam DefaultParam;
  2379. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2380. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2381. switch (Param.Queue)
  2382. {
  2383. case asOn:
  2384. CopyParam.Queue = true;
  2385. break;
  2386. case asOff:
  2387. CopyParam.Queue = false;
  2388. break;
  2389. case asAuto:
  2390. default:
  2391. // keep default
  2392. break;
  2393. }
  2394. bool Result =
  2395. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2396. if (Result)
  2397. {
  2398. DebugAssert(Terminal);
  2399. bool SelectionRestored = false;
  2400. TCustomDirView * DView = NULL;
  2401. if (HasDirView[Side])
  2402. {
  2403. DView = DirView(Side);
  2404. DView->SaveSelection();
  2405. DView->SaveSelectedNames();
  2406. }
  2407. UpdateCopyParamCounters(CopyParam);
  2408. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2409. DebugAssert(FTransferResumeList == NULL);
  2410. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2411. FMoveToQueue = false;
  2412. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2413. try
  2414. {
  2415. TStrings * PermanentFileList;
  2416. std::unique_ptr<TStrings> PermanentFileListOwner;
  2417. try
  2418. {
  2419. if (Side == osLocal)
  2420. {
  2421. PermanentFileList = FileList;
  2422. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2423. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2424. if (Operation == foMove)
  2425. {
  2426. ReloadLocalDirectory();
  2427. if (DView != NULL)
  2428. {
  2429. DView->RestoreSelection();
  2430. }
  2431. SelectionRestored = true;
  2432. }
  2433. }
  2434. else
  2435. {
  2436. // Clone the file list as it may refer to current directory files,
  2437. // which get destroyed, when the source directory is reloaded after foMove operation.
  2438. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2439. // But for a hotfix, we are not going to do such a big change.
  2440. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2441. PermanentFileList = PermanentFileListOwner.get();
  2442. try
  2443. {
  2444. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2445. }
  2446. __finally
  2447. {
  2448. if (Operation == foMove)
  2449. {
  2450. if (DView != NULL)
  2451. {
  2452. DView->RestoreSelection();
  2453. }
  2454. SelectionRestored = true;
  2455. }
  2456. ReloadLocalDirectory(Param.TargetDirectory);
  2457. }
  2458. }
  2459. }
  2460. catch (EAbort &)
  2461. {
  2462. if (FMoveToQueue)
  2463. {
  2464. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2465. DebugAssert(CopyParam.TransferSkipList == NULL);
  2466. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2467. if (TransferResumeList->Count > 0)
  2468. {
  2469. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2470. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2471. }
  2472. CopyParam.TransferSkipList = TransferResumeList.release();
  2473. // not really needed, just to keep it consistent with TransferResumeList
  2474. FTransferResumeList = NULL;
  2475. FMoveToQueue = false;
  2476. Configuration->Usage->Inc("MovesToBackground");
  2477. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2478. ClearTransferSourceSelection(Direction);
  2479. }
  2480. throw;
  2481. }
  2482. }
  2483. __finally
  2484. {
  2485. if (!SelectionRestored && (DView != NULL))
  2486. {
  2487. DView->DiscardSavedSelection();
  2488. }
  2489. FTransferResumeList = NULL;
  2490. }
  2491. }
  2492. return Result;
  2493. }
  2494. //---------------------------------------------------------------------------
  2495. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2496. TOperationSide Side, TStrings * FileList, void * Param)
  2497. {
  2498. DebugAssert(FileList->Count);
  2499. // We deliberately do not toggle alternative flag (Param), but use OR,
  2500. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2501. // shortcut of CurrentDeleteAlternativeAction
  2502. bool Alternative =
  2503. bool(Param) || UseAlternativeFunction();
  2504. bool Recycle;
  2505. if (IsSideLocalBrowser(Side))
  2506. {
  2507. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2508. }
  2509. else
  2510. {
  2511. Recycle =
  2512. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2513. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2514. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2515. }
  2516. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2517. if (!Result)
  2518. {
  2519. UnicodeString Query;
  2520. if (FileList->Count == 1)
  2521. {
  2522. if (IsSideLocalBrowser(Side))
  2523. {
  2524. Query = ExtractFileName(FileList->Strings[0]);
  2525. }
  2526. else
  2527. {
  2528. Query = UnixExtractFileName(FileList->Strings[0]);
  2529. }
  2530. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2531. }
  2532. else
  2533. {
  2534. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2535. }
  2536. TMessageParams Params(mpNeverAskAgainCheck);
  2537. Params.ImageName = L"Delete file";
  2538. unsigned int Answer =
  2539. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2540. if (Answer == qaNeverAskAgain)
  2541. {
  2542. Result = true;
  2543. if (Recycle)
  2544. {
  2545. WinConfiguration->ConfirmRecycling = false;
  2546. }
  2547. else
  2548. {
  2549. WinConfiguration->ConfirmDeleting = false;
  2550. }
  2551. }
  2552. else
  2553. {
  2554. Result = (Answer == qaOK);
  2555. }
  2556. }
  2557. if (Result)
  2558. {
  2559. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2560. }
  2561. return Result;
  2562. }
  2563. //---------------------------------------------------------------------------
  2564. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2565. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2566. {
  2567. TAutoBatch AutoBatch(this);
  2568. bool Result;
  2569. if ((Operation == foCopy) || (Operation == foMove))
  2570. {
  2571. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2572. }
  2573. else if (Operation == foRename)
  2574. {
  2575. DebugAssert(DirView(Side)->ItemFocused);
  2576. DirView(Side)->ItemFocused->EditCaption();
  2577. Result = true;
  2578. }
  2579. else if (Operation == foDelete)
  2580. {
  2581. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2582. }
  2583. else if (Operation == foSetProperties)
  2584. {
  2585. DirView(osRemote)->SaveSelectedNames();
  2586. Result = SetProperties(Side, FileList);
  2587. }
  2588. else if (Operation == foCustomCommand)
  2589. {
  2590. DebugAssert(Param);
  2591. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2592. DirView(osRemote)->SaveSelectedNames();
  2593. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2594. CustomCommand(FileList, *Command, NULL);
  2595. Result = true;
  2596. }
  2597. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2598. {
  2599. DebugAssert(!IsSideLocalBrowser(Side));
  2600. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2601. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2602. }
  2603. else if (Operation == foLock)
  2604. {
  2605. DebugAssert(!IsSideLocalBrowser(Side));
  2606. LockFiles(FileList, true);
  2607. Result = true;
  2608. }
  2609. else if (Operation == foUnlock)
  2610. {
  2611. DebugAssert(!IsSideLocalBrowser(Side));
  2612. LockFiles(FileList, false);
  2613. Result = true;
  2614. }
  2615. else
  2616. {
  2617. DebugFail();
  2618. }
  2619. return Result;
  2620. }
  2621. //---------------------------------------------------------------------------
  2622. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2623. {
  2624. if (Side == osCurrent)
  2625. {
  2626. Side = FCurrentSide;
  2627. }
  2628. return Side;
  2629. }
  2630. //---------------------------------------------------------------------------
  2631. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2632. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2633. {
  2634. Side = GetSide(Side);
  2635. bool Result;
  2636. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2637. try
  2638. {
  2639. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2640. }
  2641. __finally
  2642. {
  2643. delete FileList;
  2644. }
  2645. return Result;
  2646. }
  2647. //---------------------------------------------------------------------------
  2648. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2649. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2650. bool NoConfirmation, void * Param)
  2651. {
  2652. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2653. {
  2654. if ((Operation == foCopy) || (Operation == foMove))
  2655. {
  2656. DebugAssert(!IsLocalBrowserMode());
  2657. if (GetSide(Side) == osLocal)
  2658. {
  2659. Configuration->Usage->Inc(L"UploadsCommand");
  2660. }
  2661. else
  2662. {
  2663. Configuration->Usage->Inc(L"DownloadsCommand");
  2664. }
  2665. }
  2666. }
  2667. }
  2668. //---------------------------------------------------------------------------
  2669. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2670. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2671. {
  2672. TTransferOperationParam Param;
  2673. if ((WinConfiguration->Interface != ifCommander) ||
  2674. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2675. {
  2676. Flags &= ~cocShortCutHint;
  2677. }
  2678. TCustomDirView * DView = DirView(Side);
  2679. Param.Options =
  2680. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2681. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2682. if (FLAGSET(Flags, cocQueue))
  2683. {
  2684. Param.Queue = asOn;
  2685. }
  2686. else if (FLAGSET(Flags, cocNonQueue))
  2687. {
  2688. Param.Queue = asOff;
  2689. }
  2690. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2691. }
  2692. //---------------------------------------------------------------------------
  2693. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2694. {
  2695. try
  2696. {
  2697. if (ErrorList->Count)
  2698. {
  2699. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2700. if (MessageDialog(Message, qtError,
  2701. qaOK | qaCancel, HELP_NONE) == qaOK)
  2702. {
  2703. unsigned int Answer;
  2704. int Index = 0;
  2705. do
  2706. {
  2707. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2708. TQueryButtonAlias Aliases[2];
  2709. Aliases[0].Button = qaYes;
  2710. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2711. Aliases[1].Button = qaNo;
  2712. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2713. TMessageParams Params;
  2714. Params.Aliases = Aliases;
  2715. Params.AliasesCount = LENOF(Aliases);
  2716. Answer = MoreMessageDialog(
  2717. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2718. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2719. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2720. qaOK, HELP_NONE, &Params);
  2721. if (Answer == qaNo)
  2722. {
  2723. Index++;
  2724. }
  2725. if (Answer == qaYes)
  2726. {
  2727. Index--;
  2728. }
  2729. }
  2730. while (Answer != qaOK);
  2731. }
  2732. }
  2733. }
  2734. __finally
  2735. {
  2736. TStrings * List = ErrorList;
  2737. ErrorList = NULL;
  2738. for (int i = 0; i < List->Count; i++)
  2739. {
  2740. delete List->Objects[i];
  2741. }
  2742. delete List;
  2743. }
  2744. }
  2745. //---------------------------------------------------------------------------
  2746. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2747. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2748. {
  2749. // needed for checking filemasks, as there's no directory object
  2750. // associated with the file object
  2751. File->FullFileName = FullFileName;
  2752. TFileMasks::TParams MaskParams;
  2753. MaskParams.Size = File->Size;
  2754. MaskParams.Modification = File->Modification;
  2755. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2756. }
  2757. //---------------------------------------------------------------------------
  2758. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2759. {
  2760. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2761. Side = GetSide(Side);
  2762. TCustomDirView * DView = DirView(Side);
  2763. TListItem * FocusedItem = DView->ItemFocused;
  2764. UnicodeString Name;
  2765. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2766. {
  2767. Name = DView->ItemFileName(FocusedItem);
  2768. }
  2769. else
  2770. {
  2771. Name = LoadStr(NEW_FILE);
  2772. }
  2773. UnicodeString Names = Name;
  2774. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2775. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2776. HELP_EDIT_NEW, History.get(), true))
  2777. {
  2778. while (!Names.IsEmpty())
  2779. {
  2780. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2781. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2782. UnicodeString TargetFileName;
  2783. UnicodeString LocalFileName;
  2784. UnicodeString RootTempDir;
  2785. UnicodeString TempDir;
  2786. UnicodeString RemoteDirectory;
  2787. bool ExistingFile = false;
  2788. if (!IsSideLocalBrowser(Side))
  2789. {
  2790. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2791. TRemoteFile * File = NULL;
  2792. if (FTerminal->FileExists(Name, &File) &&
  2793. !File->IsDirectory)
  2794. {
  2795. try
  2796. {
  2797. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2798. ExistingFile = true;
  2799. }
  2800. __finally
  2801. {
  2802. delete File;
  2803. }
  2804. }
  2805. if (!ExistingFile)
  2806. {
  2807. RemoteDirectory = UnixExtractFilePath(Name);
  2808. TemporaryDirectoryForRemoteFiles(
  2809. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2810. TargetFileName = UnixExtractFileName(Name);
  2811. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2812. LocalFileName = TempDir +
  2813. // We probably do not want to trim the VMS version here
  2814. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2815. }
  2816. }
  2817. else
  2818. {
  2819. if (ExtractFilePath(Name).IsEmpty())
  2820. {
  2821. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2822. }
  2823. else
  2824. {
  2825. LocalFileName = ExpandFileName(Name);
  2826. }
  2827. TargetFileName = ExtractFileName(Name);
  2828. }
  2829. if (!ExistingFile)
  2830. {
  2831. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2832. if (NewFile)
  2833. {
  2834. int File = FileCreate(ApiPath(LocalFileName));
  2835. if (File < 0)
  2836. {
  2837. if (!RootTempDir.IsEmpty())
  2838. {
  2839. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2840. }
  2841. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2842. }
  2843. else
  2844. {
  2845. FileClose(File);
  2846. }
  2847. }
  2848. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2849. const TEditorData * ExternalEditor = NULL;
  2850. TFileMasks::TParams MaskParams; // size not known
  2851. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2852. false, MaskParams);
  2853. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2854. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2855. }
  2856. }
  2857. }
  2858. }
  2859. //---------------------------------------------------------------------------
  2860. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2861. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2862. {
  2863. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2864. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2865. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2866. return
  2867. ((ExecuteFileBy == efInternalEditor)) ||
  2868. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2869. }
  2870. //---------------------------------------------------------------------------
  2871. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2872. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2873. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2874. UnicodeString RemoteDirectory, bool NewFile)
  2875. {
  2876. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2877. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2878. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2879. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2880. Side = GetSide(Side);
  2881. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2882. if (!IsSideLocalBrowser(Side))
  2883. {
  2884. Data->Terminal = Terminal;
  2885. Data->Queue = Queue;
  2886. Data->SessionData = CloneCurrentSessionData();
  2887. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2888. Data->RemoteDirectory = RemoteDirectory;
  2889. Data->SessionName = Terminal->SessionData->SessionName;
  2890. Data->LocalRootDirectory = LocalRootDirectory;
  2891. Data->OriginalFileName = OriginalFileName;
  2892. Data->Command = L""; // will be changed later for external editor
  2893. }
  2894. if (ExecuteFileBy == efInternalEditor)
  2895. {
  2896. if (!IsSideLocalBrowser(Side))
  2897. {
  2898. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2899. L" - " + Terminal->SessionData->SessionName;
  2900. TForm * Editor;
  2901. try
  2902. {
  2903. TNotifyEvent OnFileChanged = NULL;
  2904. TNotifyEvent OnSaveAll = NULL;
  2905. TAnyModifiedEvent OnAnyModified = NULL;
  2906. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2907. if (Terminal->IsCapable[fsBackgroundTransfers])
  2908. {
  2909. OnFileChanged = FEditorManager->FileChanged;
  2910. OnSaveAll = SaveAllInternalEditors;
  2911. OnAnyModified = AnyInternalEditorModified;
  2912. }
  2913. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2914. FEditorManager->FileReload, FEditorManager->FileClosed,
  2915. OnSaveAll, OnAnyModified,
  2916. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2917. }
  2918. catch(...)
  2919. {
  2920. if (!LocalRootDirectory.IsEmpty())
  2921. {
  2922. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2923. }
  2924. throw;
  2925. }
  2926. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2927. }
  2928. else
  2929. {
  2930. DebugAssert(!FStandaloneEditing);
  2931. TForm * Editor =
  2932. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2933. SaveAllInternalEditors, AnyInternalEditorModified,
  2934. L"", false, SessionColor, -1, NewFile);
  2935. FLocalEditors->Add(Editor);
  2936. }
  2937. }
  2938. else
  2939. {
  2940. HANDLE Process;
  2941. if (ExecuteFileBy == efExternalEditor)
  2942. {
  2943. UnicodeString Program, Params, Dir;
  2944. Data->Command = ExternalEditor->ExternalEditor;
  2945. ReformatFileNameCommand(Data->Command);
  2946. SplitCommand(Data->Command, Program, Params, Dir);
  2947. Params = ExpandFileNameCommand(Params, FileName);
  2948. Program = ExpandEnvironmentVariables(Program);
  2949. if (!ExecuteShell(Program, Params, Process))
  2950. {
  2951. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2952. }
  2953. }
  2954. else
  2955. {
  2956. DebugAssert(IsSideLocalBrowser(Side));
  2957. if (!ExecuteShell(FileName, L"", Process))
  2958. {
  2959. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2960. }
  2961. }
  2962. if (IsSideLocalBrowser(Side) ||
  2963. ((ExecuteFileBy == efShell) &&
  2964. !WinConfiguration->Editor.SDIShellEditor) ||
  2965. ((ExecuteFileBy == efExternalEditor) &&
  2966. !ExternalEditor->SDIExternalEditor))
  2967. {
  2968. // no need for handle
  2969. if (Process != NULL)
  2970. {
  2971. DebugCheck(CloseHandle(Process));
  2972. }
  2973. Process = INVALID_HANDLE_VALUE;
  2974. }
  2975. else
  2976. {
  2977. if (Process == NULL)
  2978. {
  2979. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2980. }
  2981. }
  2982. if (!IsSideLocalBrowser(Side))
  2983. {
  2984. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2985. }
  2986. }
  2987. }
  2988. //---------------------------------------------------------------------------
  2989. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2990. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2991. void * /*Arg*/)
  2992. {
  2993. if (Token != NULL)
  2994. {
  2995. EditorFormFileSave(static_cast<TForm *>(Token));
  2996. }
  2997. }
  2998. //---------------------------------------------------------------------------
  2999. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3000. {
  3001. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3002. {
  3003. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3004. }
  3005. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3006. }
  3007. //---------------------------------------------------------------------------
  3008. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3009. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3010. void * Arg)
  3011. {
  3012. if ((Token != NULL) &&
  3013. IsEditorFormModified(static_cast<TForm *>(Token)))
  3014. {
  3015. *static_cast<bool *>(Arg) = true;
  3016. }
  3017. }
  3018. //---------------------------------------------------------------------------
  3019. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3020. TObject * /*Sender*/, bool & Modified)
  3021. {
  3022. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3023. {
  3024. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3025. {
  3026. Modified = true;
  3027. }
  3028. }
  3029. if (!Modified)
  3030. {
  3031. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3032. }
  3033. }
  3034. //---------------------------------------------------------------------------
  3035. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3036. {
  3037. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3038. }
  3039. //---------------------------------------------------------------------------
  3040. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3041. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3042. UnicodeString & Result, UnicodeString & RootDirectory)
  3043. {
  3044. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3045. {
  3046. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3047. Result = RootDirectory;
  3048. }
  3049. else
  3050. {
  3051. RootDirectory = L"";
  3052. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3053. Result = IncludeTrailingBackslash(Result);
  3054. }
  3055. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3056. {
  3057. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3058. }
  3059. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3060. {
  3061. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3062. {
  3063. RemoteDirectory.Delete(1, 1);
  3064. }
  3065. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3066. }
  3067. if (!ForceDirectories(ApiPath(Result)))
  3068. {
  3069. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3070. }
  3071. }
  3072. //---------------------------------------------------------------------------
  3073. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3074. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3075. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3076. {
  3077. DebugAssert(!IsLocalBrowserMode());
  3078. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3079. if (ForceText)
  3080. {
  3081. CopyParam.TransferMode = tmAscii;
  3082. }
  3083. TemporaryFileCopyParam(CopyParam);
  3084. if (AllFiles)
  3085. {
  3086. CopyParam.IncludeFileMask = TFileMasks();
  3087. }
  3088. if (TempDir.IsEmpty())
  3089. {
  3090. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3091. }
  3092. DebugAssert(!FAutoOperation);
  3093. FAutoOperation = AutoOperation;
  3094. Terminal->ExceptionOnFail = true;
  3095. try
  3096. {
  3097. try
  3098. {
  3099. // turn off confirmations, as for MDI editors we may possibly download
  3100. // the same file over
  3101. Terminal->CopyToLocal(
  3102. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3103. if (GetTargetNames)
  3104. {
  3105. for (int i = 0; i < FileList->Count; i++)
  3106. {
  3107. FileList->Strings[i] =
  3108. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3109. }
  3110. }
  3111. }
  3112. catch(...)
  3113. {
  3114. if (!RootTempDir.IsEmpty())
  3115. {
  3116. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3117. }
  3118. throw;
  3119. }
  3120. }
  3121. __finally
  3122. {
  3123. FAutoOperation = false;
  3124. Terminal->ExceptionOnFail = false;
  3125. }
  3126. }
  3127. //---------------------------------------------------------------------------
  3128. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3129. {
  3130. // Do not waste time checking for default editor list the next time,
  3131. // or testing if default editor is not notepad.
  3132. bool TryNextTime = false;
  3133. UnicodeString UsageState;
  3134. if (!WinConfiguration->EditorList->IsDefaultList())
  3135. {
  3136. UsageState = "H";
  3137. }
  3138. else
  3139. {
  3140. UnicodeString Executable;
  3141. UnicodeString ExecutableDescription;
  3142. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3143. {
  3144. UnicodeString Message =
  3145. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3146. unsigned int Answer =
  3147. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3148. if (Answer != qaOK)
  3149. {
  3150. UsageState = "R";
  3151. }
  3152. else
  3153. {
  3154. UsageState = "A";
  3155. TEditorData EditorData;
  3156. EditorData.Editor = edExternal;
  3157. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3158. EditorData.DecideExternalEditorText();
  3159. TEditorList EditorList;
  3160. EditorList = *WinConfiguration->EditorList;
  3161. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3162. WinConfiguration->EditorList = &EditorList;
  3163. }
  3164. }
  3165. }
  3166. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3167. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3168. }
  3169. //---------------------------------------------------------------------------
  3170. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3171. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3172. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3173. {
  3174. if (ExecuteFileBy == efDefaultEditor)
  3175. {
  3176. if (!WinConfiguration->OfferedEditorAutoConfig)
  3177. {
  3178. EditorAutoConfig();
  3179. }
  3180. const TEditorPreferences * Editor =
  3181. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3182. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3183. {
  3184. ExecuteFileBy = efInternalEditor;
  3185. ExternalEditor = NULL;
  3186. }
  3187. else if (Editor->Data->Editor == edOpen)
  3188. {
  3189. ExecuteFileBy = efShell;
  3190. ExternalEditor = NULL;
  3191. }
  3192. else
  3193. {
  3194. ExecuteFileBy = efExternalEditor;
  3195. ExternalEditor = Editor->Data;
  3196. }
  3197. }
  3198. }
  3199. //---------------------------------------------------------------------------
  3200. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3201. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3202. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3203. {
  3204. UnicodeString OriginalFileName;
  3205. UnicodeString LocalRootDirectory;
  3206. UnicodeString RemoteDirectory;
  3207. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3208. IsSideLocalBrowser(Side), MaskParams);
  3209. UnicodeString Counter;
  3210. UnicodeString LocalFileName;
  3211. bool Handled = false;
  3212. if (!IsSideLocalBrowser(Side))
  3213. {
  3214. // We need to trim VMS version here, so that we use name without version
  3215. // when uploading back to create a new version of the file
  3216. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3217. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3218. TObject * Token = NULL;
  3219. UnicodeString LocalDirectory;
  3220. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3221. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3222. LocalDirectory))
  3223. {
  3224. if (Token != NULL)
  3225. {
  3226. TForm * Form = dynamic_cast<TForm *>(Token);
  3227. if (Form->WindowState == wsMinimized)
  3228. {
  3229. ShowWindow(Form->Handle, SW_RESTORE);
  3230. }
  3231. else
  3232. {
  3233. Form->SetFocus();
  3234. }
  3235. Handled = true;
  3236. }
  3237. else
  3238. {
  3239. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3240. }
  3241. }
  3242. if (!Handled)
  3243. {
  3244. TStringList * FileList1 = new TStringList();
  3245. try
  3246. {
  3247. FileList1->AddObject(FullFileName, Object);
  3248. TemporarilyDownloadFiles(FileList1,
  3249. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3250. LocalRootDirectory, LocalDirectory, true, true, true);
  3251. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3252. }
  3253. __finally
  3254. {
  3255. delete FileList1;
  3256. }
  3257. switch (ExecuteFileBy)
  3258. {
  3259. case efShell:
  3260. Counter = "RemoteFilesExecuted";
  3261. break;
  3262. case efInternalEditor:
  3263. Counter = "RemoteFilesOpenedInInternalEditor";
  3264. break;
  3265. case efExternalEditor:
  3266. Counter = "RemoteFilesOpenedInExternalEditor";
  3267. break;
  3268. default:
  3269. DebugFail();
  3270. }
  3271. }
  3272. }
  3273. else
  3274. {
  3275. LocalFileName = FullFileName;
  3276. OriginalFileName = ExtractFileName(FullFileName);
  3277. switch (ExecuteFileBy)
  3278. {
  3279. case efShell:
  3280. Counter = "LocalFilesExecuted";
  3281. break;
  3282. case efInternalEditor:
  3283. Counter = "LocalFilesOpenedInInternalEditor";
  3284. break;
  3285. case efExternalEditor:
  3286. Counter = "LocalFilesOpenedInExternalEditor";
  3287. break;
  3288. default:
  3289. DebugFail();
  3290. }
  3291. }
  3292. if (!Handled)
  3293. {
  3294. Configuration->Usage->Inc(Counter);
  3295. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3296. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3297. }
  3298. }
  3299. //---------------------------------------------------------------------------
  3300. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3301. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3302. bool AllSelected, bool OnFocused)
  3303. {
  3304. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3305. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3306. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3307. Side = GetSide(Side);
  3308. TCustomDirView * DView = DirView(Side);
  3309. TStrings * FileList =
  3310. AllSelected ?
  3311. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3312. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3313. try
  3314. {
  3315. DebugAssert(AllSelected || (FileList->Count == 1));
  3316. for (int i = 0; i < FileList->Count; i++)
  3317. {
  3318. UnicodeString ListFileName = FileList->Strings[i];
  3319. UnicodeString FileNameOnly =
  3320. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3321. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3322. if (!DView->ItemIsDirectory(Item))
  3323. {
  3324. UnicodeString FullFileName;
  3325. if (!IsSideLocalBrowser(Side))
  3326. {
  3327. FullFileName = DirView(Side)->Path + ListFileName;
  3328. }
  3329. else
  3330. {
  3331. FullFileName = ListFileName;
  3332. }
  3333. TFileMasks::TParams MaskParams;
  3334. MaskParams.Size = DView->ItemFileSize(Item);
  3335. TDateTimePrecision Precision;
  3336. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3337. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3338. FileList->Objects[i], MaskParams);
  3339. }
  3340. }
  3341. }
  3342. __finally
  3343. {
  3344. delete FileList;
  3345. }
  3346. }
  3347. //---------------------------------------------------------------------------
  3348. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3349. {
  3350. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3351. CopyParam.FileNameCase = ncNoChange;
  3352. CopyParam.PreserveRights = false;
  3353. CopyParam.PreserveReadOnly = false;
  3354. CopyParam.ReplaceInvalidChars = true;
  3355. CopyParam.IncludeFileMask = TFileMasks();
  3356. CopyParam.NewerOnly = false;
  3357. CopyParam.FileMask = L"";
  3358. }
  3359. //---------------------------------------------------------------------------
  3360. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3361. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3362. {
  3363. TTerminalManager * Manager = TTerminalManager::Instance();
  3364. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3365. {
  3366. if (!NonVisualDataModule->Busy)
  3367. {
  3368. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3369. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3370. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3371. if (Data->Terminal == NULL)
  3372. {
  3373. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3374. if (SameSiteTerminal != NULL)
  3375. {
  3376. UnicodeString Message =
  3377. FMTLOAD(EDIT_SESSION_REATTACH,
  3378. (FileNameOnly, Data->SessionName, FileNameOnly));
  3379. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3380. {
  3381. Data->Terminal = Terminal;
  3382. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3383. Data->SessionName = Terminal->SessionData->SessionName;
  3384. // We might also overwrite session data
  3385. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3386. }
  3387. else
  3388. {
  3389. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3390. Abort();
  3391. }
  3392. }
  3393. }
  3394. // foreground session should reconnect itself
  3395. else if (Terminal != Data->Terminal)
  3396. {
  3397. UnicodeString Message =
  3398. MainInstructions(
  3399. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3400. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3401. {
  3402. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3403. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3404. }
  3405. else
  3406. {
  3407. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3408. Abort();
  3409. }
  3410. }
  3411. }
  3412. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3413. {
  3414. Configuration->Usage->Inc(L"EditInactiveSession");
  3415. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3416. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3417. (ExtractFileName(FileName), Data->SessionName)));
  3418. }
  3419. }
  3420. TStrings * FileList = new TStringList();
  3421. try
  3422. {
  3423. FileList->Add(FileName);
  3424. // consider using the same settings (preset) as when the file was downloaded
  3425. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3426. TemporaryFileCopyParam(CopyParam);
  3427. if (Data->ForceText)
  3428. {
  3429. CopyParam.TransferMode = tmAscii;
  3430. }
  3431. // so i do not need to worry if masking algorithm works in all cases
  3432. // ("" means "copy file name", no masking is actually done)
  3433. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3434. {
  3435. CopyParam.FileMask = L"";
  3436. }
  3437. else
  3438. {
  3439. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3440. }
  3441. DebugAssert(Data->Queue != NULL);
  3442. int Params = cpNoConfirmation | cpTemporary;
  3443. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3444. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3445. QueueItem->CompleteEvent = UploadCompleteEvent;
  3446. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3447. }
  3448. __finally
  3449. {
  3450. delete FileList;
  3451. }
  3452. }
  3453. //---------------------------------------------------------------------------
  3454. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3455. const UnicodeString FileName, const TEditedFileData * Data)
  3456. {
  3457. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3458. DebugAssert(!NonVisualDataModule->Busy);
  3459. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3460. {
  3461. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3462. (ExtractFileName(FileName), Data->SessionName)));
  3463. }
  3464. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3465. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3466. NonVisualDataModule->StartBusy();
  3467. try
  3468. {
  3469. std::unique_ptr<TRemoteFile> File;
  3470. UnicodeString RemoteFileName =
  3471. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3472. FTerminal->ExceptionOnFail = true;
  3473. try
  3474. {
  3475. TRemoteFile * AFile = NULL;
  3476. FTerminal->ReadFile(RemoteFileName, AFile);
  3477. File.reset(AFile);
  3478. if (!File->HaveFullFileName)
  3479. {
  3480. File->FullFileName = RemoteFileName;
  3481. }
  3482. }
  3483. __finally
  3484. {
  3485. FTerminal->ExceptionOnFail = false;
  3486. }
  3487. std::unique_ptr<TStrings> FileList(new TStringList());
  3488. FileList->AddObject(RemoteFileName, File.get());
  3489. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3490. UnicodeString TempDir = ExtractFilePath(FileName);
  3491. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3492. TempDir, true, true, true);
  3493. // sanity check, the target file name should be still the same
  3494. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3495. }
  3496. __finally
  3497. {
  3498. NonVisualDataModule->EndBusy();
  3499. // it actually may not exist anymore...
  3500. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3501. }
  3502. }
  3503. //---------------------------------------------------------------------------
  3504. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3505. const TEditedFileData * Data, bool & KeepOpen)
  3506. {
  3507. // Command is set for external editors only (not for "shell" open).
  3508. if (!Data->Command.IsEmpty())
  3509. {
  3510. bool AnyFound = false;
  3511. bool AnyMDI = false;
  3512. bool AnyNonMDI = false;
  3513. bool AnyDetect = false;
  3514. TEditorList * EditorList = new TEditorList();
  3515. try
  3516. {
  3517. *EditorList = *WinConfiguration->EditorList;
  3518. for (int i = 0; i < EditorList->Count; i++)
  3519. {
  3520. const TEditorPreferences * Editor = EditorList->Editors[i];
  3521. if ((Editor->Data->Editor == edExternal) &&
  3522. (Editor->Data->ExternalEditor == Data->Command))
  3523. {
  3524. AnyFound = true;
  3525. if (Editor->Data->SDIExternalEditor)
  3526. {
  3527. AnyNonMDI = true;
  3528. if (Editor->Data->DetectMDIExternalEditor)
  3529. {
  3530. AnyDetect = true;
  3531. }
  3532. }
  3533. else
  3534. {
  3535. AnyMDI = true;
  3536. }
  3537. }
  3538. }
  3539. bool EnableMDI = false;
  3540. bool DisableDetect = false;
  3541. if (AnyMDI)
  3542. {
  3543. KeepOpen = true;
  3544. if (AnyNonMDI)
  3545. {
  3546. // there is at least one instance of the editor with MDI support enabled,
  3547. // and one with disabled, enable it for all instances
  3548. EnableMDI = true;
  3549. }
  3550. }
  3551. else if (AnyFound && !AnyDetect)
  3552. {
  3553. // at least once instance found but all have MDI autodetection disabled
  3554. // => close the file (default action)
  3555. }
  3556. else
  3557. {
  3558. // no instance of the editor has MDI support enabled
  3559. TMessageParams Params;
  3560. if (AnyFound)
  3561. {
  3562. // there is at least one instance of the editor
  3563. Params.Params |= mpNeverAskAgainCheck;
  3564. }
  3565. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3566. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3567. switch (Answer)
  3568. {
  3569. case qaNeverAskAgain:
  3570. DisableDetect = true;
  3571. break;
  3572. case qaNo:
  3573. EnableMDI = true;
  3574. KeepOpen = true;
  3575. break;
  3576. }
  3577. }
  3578. if (AnyFound && (EnableMDI || DisableDetect))
  3579. {
  3580. bool Changed = false;
  3581. for (int i = 0; i < EditorList->Count; i++)
  3582. {
  3583. const TEditorPreferences * Editor = EditorList->Editors[i];
  3584. if ((Editor->Data->Editor == edExternal) &&
  3585. (Editor->Data->ExternalEditor == Data->Command) &&
  3586. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3587. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3588. {
  3589. Changed = true;
  3590. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3591. if (EnableMDI)
  3592. {
  3593. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3594. }
  3595. if (DisableDetect)
  3596. {
  3597. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3598. }
  3599. EditorList->Change(i, UpdatedEditor);
  3600. }
  3601. }
  3602. if (Changed)
  3603. {
  3604. WinConfiguration->EditorList = EditorList;
  3605. }
  3606. }
  3607. }
  3608. __finally
  3609. {
  3610. delete EditorList;
  3611. }
  3612. }
  3613. else
  3614. {
  3615. // "open" case
  3616. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3617. qaOK, HELP_APP_EARLY_CLOSED);
  3618. }
  3619. }
  3620. //---------------------------------------------------------------------------
  3621. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3622. {
  3623. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3624. }
  3625. //---------------------------------------------------------------------------
  3626. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3627. {
  3628. SideEnter(osRemote);
  3629. }
  3630. //---------------------------------------------------------------------------
  3631. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3632. {
  3633. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3634. SideEnter(osRemote);
  3635. }
  3636. //---------------------------------------------------------------------------
  3637. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3638. {
  3639. FCurrentSide = Side;
  3640. if (Visible)
  3641. {
  3642. UpdateControls();
  3643. }
  3644. }
  3645. //---------------------------------------------------------------------------
  3646. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3647. TStrings * FileList, bool Alternative)
  3648. {
  3649. TCustomDirView * DView = DirView(Side);
  3650. DView->SaveSelection();
  3651. DView->SaveSelectedNames();
  3652. DebugAssert(!FAlternativeDelete);
  3653. FAlternativeDelete = Alternative;
  3654. try
  3655. {
  3656. if (!IsSideLocalBrowser(Side))
  3657. {
  3658. DebugAssert(Terminal != NULL);
  3659. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3660. }
  3661. else
  3662. {
  3663. try
  3664. {
  3665. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3666. }
  3667. __finally
  3668. {
  3669. ReloadLocalDirectory();
  3670. }
  3671. }
  3672. FAlternativeDelete = false;
  3673. }
  3674. catch(...)
  3675. {
  3676. FAlternativeDelete = false;
  3677. DView->DiscardSavedSelection();
  3678. throw;
  3679. }
  3680. DView->RestoreSelection();
  3681. }
  3682. //---------------------------------------------------------------------------
  3683. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3684. {
  3685. DebugAssert(Terminal);
  3686. DebugAssert(!IsLocalBrowserMode());
  3687. RemoteDirView->SaveSelection();
  3688. RemoteDirView->SaveSelectedNames();
  3689. try
  3690. {
  3691. if (Lock)
  3692. {
  3693. Terminal->LockFiles(FileList);
  3694. }
  3695. else
  3696. {
  3697. Terminal->UnlockFiles(FileList);
  3698. }
  3699. }
  3700. catch(...)
  3701. {
  3702. RemoteDirView->DiscardSavedSelection();
  3703. throw;
  3704. }
  3705. RemoteDirView->RestoreSelection();
  3706. }
  3707. //---------------------------------------------------------------------------
  3708. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3709. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3710. bool NoConfirmation, bool Move)
  3711. {
  3712. DebugAssert(Terminal != NULL);
  3713. DebugAssert(!IsLocalBrowserMode());
  3714. // update Terminal->StateData->RemoteDirectory
  3715. UpdateTerminal(Terminal);
  3716. if (Session == NULL)
  3717. {
  3718. Session = TTerminalManager::Instance()->ActiveTerminal;
  3719. }
  3720. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3721. {
  3722. if (RemoteDriveView->DropTarget != NULL)
  3723. {
  3724. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3725. }
  3726. else if (RemoteDirView->DropTarget != NULL)
  3727. {
  3728. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3729. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3730. }
  3731. else
  3732. {
  3733. Target = RemoteDirView->Path;
  3734. }
  3735. }
  3736. else
  3737. {
  3738. Target = Session->StateData->RemoteDirectory;
  3739. }
  3740. Target = UnixIncludeTrailingBackslash(Target);
  3741. if (FileList->Count == 1)
  3742. {
  3743. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3744. }
  3745. else
  3746. {
  3747. FileMask = AnyMask;
  3748. }
  3749. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3750. bool Result = true;
  3751. if (!NoConfirmation)
  3752. {
  3753. bool Multi = (FileList->Count > 1);
  3754. if (Move)
  3755. {
  3756. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3757. }
  3758. else
  3759. {
  3760. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3761. TStrings * Directories = new TStringList;
  3762. try
  3763. {
  3764. for (int Index = 0; Index < Sessions->Count; Index++)
  3765. {
  3766. TManagedTerminal * Terminal =
  3767. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3768. Directories->Add(Terminal->StateData->RemoteDirectory);
  3769. }
  3770. TDirectRemoteCopy AllowDirectCopy;
  3771. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3772. {
  3773. DebugAssert(DirectCopy);
  3774. AllowDirectCopy = drcAllow;
  3775. }
  3776. else if (FTerminal->IsCapable[fcSecondaryShell])
  3777. {
  3778. DebugAssert(DirectCopy);
  3779. AllowDirectCopy = drcConfirmCommandSession;
  3780. }
  3781. else
  3782. {
  3783. DebugAssert(!DirectCopy);
  3784. AllowDirectCopy = drcDisallow;
  3785. }
  3786. void * ASession = Session;
  3787. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3788. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3789. Session = static_cast<TManagedTerminal *>(ASession);
  3790. }
  3791. __finally
  3792. {
  3793. delete Directories;
  3794. }
  3795. }
  3796. }
  3797. return Result;
  3798. }
  3799. //---------------------------------------------------------------------------
  3800. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3801. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3802. {
  3803. DebugAssert(!IsLocalBrowserMode());
  3804. bool DirectCopy;
  3805. UnicodeString Target, FileMask;
  3806. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3807. if (Result)
  3808. {
  3809. if (!Move && !DirectCopy)
  3810. {
  3811. Configuration->Usage->Inc("RemoteCopyTemp");
  3812. UnicodeString RootTempDir;
  3813. UnicodeString TempDir;
  3814. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3815. TStrings * TemporaryFilesList = new TStringList();
  3816. try
  3817. {
  3818. TMakeLocalFileListParams MakeFileListParam;
  3819. MakeFileListParam.FileList = TemporaryFilesList;
  3820. MakeFileListParam.FileTimes = NULL;
  3821. MakeFileListParam.IncludeDirs = true;
  3822. MakeFileListParam.Recursive = false;
  3823. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3824. TTerminalManager::Instance()->ActiveTerminal = Session;
  3825. if (TemporaryFilesList->Count > 0)
  3826. {
  3827. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3828. CopyParam.FileMask = FileMask;
  3829. DebugAssert(!FAutoOperation);
  3830. FAutoOperation = true;
  3831. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3832. }
  3833. }
  3834. __finally
  3835. {
  3836. delete TemporaryFilesList;
  3837. FAutoOperation = false;
  3838. if (!RootTempDir.IsEmpty())
  3839. {
  3840. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3841. }
  3842. }
  3843. }
  3844. else
  3845. {
  3846. RemoteDirView->SaveSelection();
  3847. RemoteDirView->SaveSelectedNames();
  3848. try
  3849. {
  3850. if (Move)
  3851. {
  3852. Configuration->Usage->Inc("RemoteMove");
  3853. Terminal->MoveFiles(FileList, Target, FileMask);
  3854. }
  3855. else
  3856. {
  3857. Configuration->Usage->Inc("RemoteCopyDirect");
  3858. DebugAssert(DirectCopy);
  3859. DebugAssert(Session == FTerminal);
  3860. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3861. FTerminal->CommandSessionOpened ||
  3862. CommandSessionFallback())
  3863. {
  3864. Terminal->CopyFiles(FileList, Target, FileMask);
  3865. }
  3866. }
  3867. }
  3868. catch(...)
  3869. {
  3870. RemoteDirView->DiscardSavedSelection();
  3871. throw;
  3872. }
  3873. RemoteDirView->RestoreSelection();
  3874. }
  3875. }
  3876. return Result;
  3877. }
  3878. //---------------------------------------------------------------------------
  3879. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3880. const UnicodeString & Path, TRemoteProperties & Properties)
  3881. {
  3882. DebugAssert(!IsLocalBrowserMode());
  3883. Properties.Valid = Properties.Valid << vpEncrypt;
  3884. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3885. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3886. }
  3887. //---------------------------------------------------------------------------
  3888. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3889. {
  3890. Side = GetSide(Side);
  3891. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3892. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3893. UnicodeString Name = LoadStr(NEW_FOLDER);
  3894. int AllowedChanges =
  3895. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3896. bool SaveSettings = false;
  3897. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3898. {
  3899. TWindowLock Lock(this);
  3900. if (!IsSideLocalBrowser(Side))
  3901. {
  3902. if (SaveSettings)
  3903. {
  3904. GUIConfiguration->NewDirectoryProperties = Properties;
  3905. }
  3906. CreateRemoteDirectory(Name, Properties);
  3907. }
  3908. else
  3909. {
  3910. DirView(Side)->CreateDirectory(Name);
  3911. }
  3912. }
  3913. }
  3914. //---------------------------------------------------------------------------
  3915. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3916. {
  3917. TWindowLock Lock(this);
  3918. DirView(Side)->ExecuteHomeDirectory();
  3919. }
  3920. //---------------------------------------------------------------------------
  3921. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3922. {
  3923. DoOpenDirectoryDialog(odBrowse, Side);
  3924. }
  3925. //---------------------------------------------------------------------------
  3926. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3927. {
  3928. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3929. bool Result = !Path.IsEmpty();
  3930. if (Result)
  3931. {
  3932. // While we might get here when the session is closed (from location profiles),
  3933. // it's not a problem as the Path setter is noop then
  3934. DirView(Side)->Path = Path;
  3935. }
  3936. return Result;
  3937. }
  3938. //---------------------------------------------------------------------------
  3939. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3940. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3941. bool & Close)
  3942. {
  3943. // terminal can be already closed (e.g. dropped connection)
  3944. if (Terminal != NULL)
  3945. {
  3946. try
  3947. {
  3948. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3949. }
  3950. catch(...)
  3951. {
  3952. if (!Terminal->Active)
  3953. {
  3954. Close = true;
  3955. }
  3956. throw;
  3957. }
  3958. }
  3959. }
  3960. //---------------------------------------------------------------------------
  3961. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3962. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3963. bool & Close)
  3964. {
  3965. // terminal can be already closed (e.g. dropped connection)
  3966. if (Terminal != NULL)
  3967. {
  3968. Configuration->Usage->Inc(L"ChecksumCalculated");
  3969. try
  3970. {
  3971. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3972. }
  3973. catch(...)
  3974. {
  3975. if (!Terminal->Active)
  3976. {
  3977. Close = true;
  3978. }
  3979. throw;
  3980. }
  3981. }
  3982. }
  3983. //---------------------------------------------------------------------------
  3984. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3985. {
  3986. bool Result;
  3987. if (!IsSideLocalBrowser(Side))
  3988. {
  3989. TRemoteTokenList * GroupList = NULL;
  3990. TRemoteTokenList * UserList = NULL;
  3991. try
  3992. {
  3993. TRemoteProperties CurrentProperties;
  3994. if (Terminal->LoadFilesProperties(FileList))
  3995. {
  3996. RemoteDirView->Invalidate();
  3997. }
  3998. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3999. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4000. if (CapableGroupChanging || CapableOwnerChanging)
  4001. {
  4002. if (CapableGroupChanging)
  4003. {
  4004. GroupList = Terminal->Groups->Duplicate();
  4005. }
  4006. if (CapableOwnerChanging)
  4007. {
  4008. UserList = Terminal->Users->Duplicate();
  4009. }
  4010. TRemoteDirectory * Files = Terminal->Files;
  4011. int Count = Files->Count;
  4012. if (Count > 100)
  4013. {
  4014. Count = 100;
  4015. }
  4016. for (int Index = 0; Index < Count; Index++)
  4017. {
  4018. TRemoteFile * File = Files->Files[Index];
  4019. if (CapableGroupChanging)
  4020. {
  4021. GroupList->AddUnique(File->Group);
  4022. }
  4023. if (CapableOwnerChanging)
  4024. {
  4025. UserList->AddUnique(File->Owner);
  4026. }
  4027. }
  4028. // if we haven't collected tokens for all files in current directory,
  4029. // make sure we collect them at least for all selected files.
  4030. // (note that so far the files in FileList has to be from current direcotry)
  4031. if (Count < Files->Count)
  4032. {
  4033. for (int Index = 0; Index < FileList->Count; Index++)
  4034. {
  4035. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4036. if (CapableGroupChanging)
  4037. {
  4038. GroupList->AddUnique(File->Group);
  4039. }
  4040. if (CapableOwnerChanging)
  4041. {
  4042. UserList->AddUnique(File->Owner);
  4043. }
  4044. }
  4045. }
  4046. }
  4047. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4048. int Flags = 0;
  4049. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4050. if (CapableOwnerChanging) Flags |= cpOwner;
  4051. if (CapableGroupChanging) Flags |= cpGroup;
  4052. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4053. if (Terminal->IsCapable[fcCalculatingChecksum])
  4054. {
  4055. CalculateChecksumEvent = CalculateChecksum;
  4056. }
  4057. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4058. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4059. TRemoteProperties NewProperties = CurrentProperties;
  4060. Result =
  4061. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4062. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4063. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4064. CalculateSize, CalculateChecksumEvent);
  4065. if (Result)
  4066. {
  4067. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4068. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4069. }
  4070. }
  4071. __finally
  4072. {
  4073. delete GroupList;
  4074. delete UserList;
  4075. }
  4076. }
  4077. else
  4078. {
  4079. DirView(Side)->DisplayPropertiesMenu();
  4080. Result = true;
  4081. }
  4082. return Result;
  4083. }
  4084. //---------------------------------------------------------------------------
  4085. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4086. {
  4087. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4088. {
  4089. FIgnoreNextDialogChar = Key;
  4090. }
  4091. Key = 0;
  4092. }
  4093. //---------------------------------------------------------------------------
  4094. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4095. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4096. {
  4097. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4098. if (Command != NULL)
  4099. {
  4100. KeyProcessed(Key, Shift);
  4101. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4102. {
  4103. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4104. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4105. false, false, const_cast<TCustomCommandType *>(Command));
  4106. }
  4107. }
  4108. }
  4109. //---------------------------------------------------------------------------
  4110. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4111. {
  4112. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4113. {
  4114. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4115. }
  4116. if (!DirView(osCurrent)->IsEditing())
  4117. {
  4118. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4119. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4120. {
  4121. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4122. if (((Action->ShortCut == KeyShortCut) ||
  4123. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4124. AllowedAction(Action, aaShortCut))
  4125. {
  4126. // Has to be called before the action as the dialog char is already in queue.
  4127. // So when the action consumes message queue, we already need to have the
  4128. // FIgnoreNextDialogChar set
  4129. KeyProcessed(Key, Shift);
  4130. // Reset reference to previous component (when menu/toolbar was clicked).
  4131. // Needed to detect that action was invoked by keyboard shortcut
  4132. // in TNonVisualDataModule::ExplorerActionsExecute
  4133. Action->ActionComponent = NULL;
  4134. Action->Execute();
  4135. return;
  4136. }
  4137. }
  4138. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4139. {
  4140. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4141. {
  4142. KeyProcessed(Key, Shift);
  4143. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4144. return;
  4145. }
  4146. }
  4147. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4148. {
  4149. KeyProcessed(Key, Shift);
  4150. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4151. }
  4152. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4153. if (IsCustomShortCut(CustomShortCut))
  4154. {
  4155. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4156. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4157. if (WinConfiguration->SharedBookmarks != NULL)
  4158. {
  4159. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4160. if ((Bookmark != NULL) &&
  4161. OpenBookmark(FCurrentSide, Bookmark))
  4162. {
  4163. KeyProcessed(Key, Shift);
  4164. }
  4165. }
  4166. }
  4167. }
  4168. TForm::KeyDown(Key, Shift);
  4169. }
  4170. //---------------------------------------------------------------------------
  4171. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4172. {
  4173. DebugAssert(Terminal != NULL);
  4174. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4175. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4176. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4177. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4178. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4179. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4180. // expanded from ?: to avoid memory leaks
  4181. if (SecurityEnabled)
  4182. {
  4183. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4184. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4185. }
  4186. else
  4187. {
  4188. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4189. }
  4190. if (FileSystemInfo.ProtocolName.IsEmpty())
  4191. {
  4192. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4193. }
  4194. else
  4195. {
  4196. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4197. }
  4198. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4199. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4200. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4201. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4202. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4203. UnicodeString CompressionStr = CSCompressionStr;
  4204. if (CSCompressionStr != SCCompressionStr)
  4205. {
  4206. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4207. }
  4208. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4209. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4210. UpdateStatusBar();
  4211. }
  4212. //---------------------------------------------------------------------------
  4213. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4214. {
  4215. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4216. DebugAssert(SessionStatusBar != NULL);
  4217. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4218. {
  4219. // note: (Terminal->Status < sshReady) currently never happens here,
  4220. // so STATUS_CONNECTING is never used
  4221. SessionStatusBar->SimplePanel = true;
  4222. SessionStatusBar->SimpleText = LoadStr(
  4223. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4224. }
  4225. else
  4226. {
  4227. DebugAssert(Terminal);
  4228. SessionStatusBar->SimplePanel = false;
  4229. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4230. if (!FNote.IsEmpty())
  4231. {
  4232. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4233. }
  4234. else
  4235. {
  4236. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4237. }
  4238. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4239. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4240. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4241. }
  4242. }
  4243. //---------------------------------------------------------------------------
  4244. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4245. {
  4246. Panel->Caption = L"";
  4247. }
  4248. //---------------------------------------------------------------------------
  4249. void __fastcall TCustomScpExplorerForm::Idle()
  4250. {
  4251. if (FShowing || FStandaloneEditing)
  4252. {
  4253. FEditorManager->Check();
  4254. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4255. // that needs to know if queue view is visible (and it may be closed after queue update)
  4256. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4257. }
  4258. if (FShowing)
  4259. {
  4260. if (!NonVisualDataModule->Busy &&
  4261. // Menu is opened or drag&drop is going on
  4262. (Mouse->Capture == NULL))
  4263. {
  4264. if (FRefreshRemoteDirectory)
  4265. {
  4266. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4267. {
  4268. Terminal->RefreshDirectory();
  4269. }
  4270. FRefreshRemoteDirectory = false;
  4271. }
  4272. if (FRefreshLocalDirectory)
  4273. {
  4274. ReloadLocalDirectory();
  4275. FRefreshLocalDirectory = false;
  4276. }
  4277. if (WinConfiguration->RefreshRemotePanel)
  4278. {
  4279. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4280. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4281. {
  4282. RemoteDirView->ReloadDirectory();
  4283. }
  4284. }
  4285. }
  4286. }
  4287. if (FShowing || FStandaloneEditing)
  4288. {
  4289. if (FQueueStatusInvalidated)
  4290. {
  4291. UpdateQueueStatus(false);
  4292. }
  4293. RefreshQueueItems();
  4294. }
  4295. if (FShowing)
  4296. {
  4297. UpdateStatusBar();
  4298. }
  4299. FIgnoreNextDialogChar = 0;
  4300. }
  4301. //---------------------------------------------------------------------------
  4302. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4303. {
  4304. try
  4305. {
  4306. FUserActionTimer->Enabled = false;
  4307. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4308. {
  4309. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4310. {
  4311. FPendingQueueActionItem->ProcessUserAction();
  4312. }
  4313. }
  4314. }
  4315. __finally
  4316. {
  4317. FPendingQueueActionItem = NULL;
  4318. }
  4319. }
  4320. //---------------------------------------------------------------------------
  4321. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4322. {
  4323. if (WinConfiguration->MinimizeToTray)
  4324. {
  4325. UpdateTrayIcon();
  4326. FTrayIcon->Visible = true;
  4327. if (Visible)
  4328. {
  4329. ShowWindow(Handle, SW_HIDE);
  4330. }
  4331. }
  4332. }
  4333. //---------------------------------------------------------------------------
  4334. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4335. {
  4336. // WORKAROUND
  4337. // When restoring maximized window from minimization,
  4338. // rarely some controls do not align properly.
  4339. // Two instances seen (both for Commander):
  4340. // - When restoring, window is temporarily narrower (not maximized),
  4341. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4342. // Once maximized already, top dock shinks back, but the session PageControl,
  4343. // do not align up, leaving space between TopDock and PageControl.
  4344. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4345. for (int Index = 0; Index < ControlCount; Index++)
  4346. {
  4347. RealignControl(Controls[Index]);
  4348. }
  4349. if (FTrayIcon->Visible)
  4350. {
  4351. FTrayIcon->Visible = false;
  4352. if (Visible)
  4353. {
  4354. ShowWindow(Handle, SW_SHOW);
  4355. }
  4356. }
  4357. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4358. {
  4359. FNeedSession = false;
  4360. NonVisualDataModule->StartBusy();
  4361. try
  4362. {
  4363. NeedSession(true);
  4364. }
  4365. __finally
  4366. {
  4367. NonVisualDataModule->EndBusy();
  4368. }
  4369. }
  4370. }
  4371. //---------------------------------------------------------------------------
  4372. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4373. {
  4374. FTrayIcon->Hint = GetMainForm()->Caption;
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4378. {
  4379. UpdateTrayIcon();
  4380. }
  4381. //---------------------------------------------------------------------------
  4382. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4383. {
  4384. // workaround
  4385. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4386. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4387. // But WM_ACTIVATEAPP message can be received even when
  4388. // the main window is minimized to the tray and internal editor window is focused
  4389. // (after another application was previously active)
  4390. if (::IsIconic(Handle))
  4391. {
  4392. if (!IsAppIconic())
  4393. {
  4394. SetAppIconic(true);
  4395. }
  4396. }
  4397. ::ApplicationRestore();
  4398. Application->BringToFront();
  4399. }
  4400. //---------------------------------------------------------------------------
  4401. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4402. {
  4403. RestoreApp();
  4404. }
  4405. //---------------------------------------------------------------------------
  4406. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4407. {
  4408. if (OpenInNewWindow())
  4409. {
  4410. // todo: Pass FromSite
  4411. ExecuteNewInstance(SessionUrl);
  4412. }
  4413. else
  4414. {
  4415. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4416. }
  4417. }
  4418. //---------------------------------------------------------------------------
  4419. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4420. {
  4421. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4422. // current working directories become defaults here, what is not right
  4423. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4424. StoredSessions->NewSession(SessionName, SessionData.get());
  4425. // modified only, explicit
  4426. StoredSessions->Save(false, true);
  4427. // encode session name because of slashes in hierarchical sessions
  4428. return EncodeUrlString(SessionName);
  4429. }
  4430. //---------------------------------------------------------------------------
  4431. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4432. {
  4433. if (OpenInNewWindow())
  4434. {
  4435. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4436. }
  4437. else
  4438. {
  4439. // current working directories become defaults here, what is not right
  4440. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4441. TTerminalManager * Manager = TTerminalManager::Instance();
  4442. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4443. Manager->ActiveTerminal = Terminal;
  4444. }
  4445. }
  4446. //---------------------------------------------------------------------------
  4447. void __fastcall TCustomScpExplorerForm::RenameSession()
  4448. {
  4449. UnicodeString Name = Terminal->SessionData->SessionName;
  4450. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4451. {
  4452. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4453. TSessionData::ValidateName(Name);
  4454. // This is inconsistent with how color (for example) is handled.
  4455. Terminal->SessionData->Name = Name;
  4456. UpdateControls();
  4457. // Add/Remove distinguishing paths from sessions of the same name.
  4458. DoTerminalListChanged();
  4459. }
  4460. }
  4461. //---------------------------------------------------------------------------
  4462. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4463. {
  4464. DebugAssert(Queue != NULL);
  4465. bool Result = Queue->IsEmpty;
  4466. if (!Result)
  4467. {
  4468. SetFocus();
  4469. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4470. }
  4471. return Result;
  4472. }
  4473. //---------------------------------------------------------------------------
  4474. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4475. {
  4476. return CanCloseQueue(FQueue);
  4477. }
  4478. //---------------------------------------------------------------------------
  4479. void __fastcall TCustomScpExplorerForm::CloseSession()
  4480. {
  4481. if (CanCloseQueue())
  4482. {
  4483. TTerminalManager::Instance()->FreeActiveTerminal();
  4484. }
  4485. }
  4486. //---------------------------------------------------------------------------
  4487. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4488. {
  4489. if (CanCloseQueue())
  4490. {
  4491. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4492. }
  4493. }
  4494. //---------------------------------------------------------------------------
  4495. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4496. {
  4497. RemoteDirView->Terminal = Terminal;
  4498. UpdateRemotePathComboBox(false);
  4499. UpdateControls();
  4500. }
  4501. //---------------------------------------------------------------------------
  4502. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4503. {
  4504. if (DebugAlwaysTrue(!Terminal->Active))
  4505. {
  4506. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4507. }
  4508. }
  4509. //---------------------------------------------------------------------------
  4510. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4511. {
  4512. if (OpenInNewWindow())
  4513. {
  4514. // encode session name because of slashes in hierarchical sessions
  4515. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4516. }
  4517. else
  4518. {
  4519. TTerminalManager * Manager = TTerminalManager::Instance();
  4520. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4521. Manager->ActiveTerminal = Terminal;
  4522. }
  4523. }
  4524. //---------------------------------------------------------------------------
  4525. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4526. {
  4527. TTerminalManager * Manager = TTerminalManager::Instance();
  4528. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4529. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4530. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4531. if (!ConnectFirstTerminal)
  4532. {
  4533. FirstTerminal->Disconnected = true;
  4534. FirstTerminal->DisconnectedTemporarily = true;
  4535. }
  4536. Manager->ActiveTerminal = FirstTerminal;
  4537. }
  4538. //---------------------------------------------------------------------------
  4539. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4540. {
  4541. if (OpenInNewWindow())
  4542. {
  4543. ExecuteNewInstance(Name);
  4544. }
  4545. else
  4546. {
  4547. DoOpenFolderOrWorkspace(Name, true);
  4548. }
  4549. }
  4550. //---------------------------------------------------------------------------
  4551. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4552. bool &CanClose)
  4553. {
  4554. if (Terminal != NULL)
  4555. {
  4556. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4557. {
  4558. unsigned int Result;
  4559. TMessageParams Params(mpNeverAskAgainCheck);
  4560. UnicodeString Message;
  4561. int Answers = qaOK | qaCancel;
  4562. if (TTerminalManager::Instance()->Count > 1)
  4563. {
  4564. if (!WinConfiguration->AutoSaveWorkspace)
  4565. {
  4566. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4567. }
  4568. else
  4569. {
  4570. Message = LoadStr(CLOSE_SESSIONS);
  4571. }
  4572. }
  4573. else
  4574. {
  4575. if (!WinConfiguration->AutoSaveWorkspace)
  4576. {
  4577. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4578. }
  4579. else
  4580. {
  4581. Message = LoadStr(CLOSE_SESSION);
  4582. }
  4583. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4584. }
  4585. UnicodeString Note;
  4586. if (WinConfiguration->AutoSaveWorkspace)
  4587. {
  4588. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4589. }
  4590. else
  4591. {
  4592. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4593. Answers = qaYes | qaNo | qaCancel;
  4594. }
  4595. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4596. SetFocus();
  4597. Result = MessageDialog(Message, qtConfirmation,
  4598. Answers, HELP_NONE, &Params);
  4599. if (Result == qaNeverAskAgain)
  4600. {
  4601. WinConfiguration->ConfirmClosingSession = false;
  4602. }
  4603. if (Result == qaNo)
  4604. {
  4605. CanClose = SaveWorkspace(true);
  4606. // note that the workspace will be saved redundatly again from FormClose
  4607. }
  4608. else
  4609. {
  4610. CanClose =
  4611. (Result == qaOK) ||
  4612. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4613. (Result == qaNeverAskAgain);
  4614. }
  4615. }
  4616. if (CanClose)
  4617. {
  4618. CanClose = CanCloseQueue();
  4619. }
  4620. }
  4621. if (CanClose)
  4622. {
  4623. CanClose =
  4624. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4625. FEditorManager->CloseExternalFilesWithoutProcess();
  4626. if (CanClose)
  4627. {
  4628. while (CanClose && (FLocalEditors->Count > 0))
  4629. {
  4630. int PrevCount = FLocalEditors->Count;
  4631. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4632. CanClose = (FLocalEditors->Count < PrevCount);
  4633. }
  4634. if (CanClose)
  4635. {
  4636. CanClose = FEditorManager->Empty(true);
  4637. if (!CanClose)
  4638. {
  4639. SetFocus();
  4640. CanClose =
  4641. (MessageDialog(
  4642. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4643. }
  4644. }
  4645. }
  4646. }
  4647. }
  4648. //---------------------------------------------------------------------------
  4649. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4650. {
  4651. TForm * Form = dynamic_cast<TForm *>(Sender);
  4652. DebugAssert(Form != NULL);
  4653. Form->Close();
  4654. }
  4655. //---------------------------------------------------------------------------
  4656. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4657. {
  4658. TForm * Form = dynamic_cast<TForm *>(Sender);
  4659. delete Form;
  4660. }
  4661. //---------------------------------------------------------------------------
  4662. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4663. {
  4664. while (FLocalEditors->Count > 0)
  4665. {
  4666. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4667. }
  4668. }
  4669. //---------------------------------------------------------------------------
  4670. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4671. TObject *Sender)
  4672. {
  4673. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4674. try
  4675. {
  4676. SetProperties(osRemote, FileList);
  4677. }
  4678. __finally
  4679. {
  4680. delete FileList;
  4681. }
  4682. }
  4683. //---------------------------------------------------------------------------
  4684. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4685. {
  4686. if (value)
  4687. {
  4688. if (Component == fcCustomCommandsBand)
  4689. {
  4690. UpdateCustomCommandsToolbar();
  4691. }
  4692. }
  4693. }
  4694. //---------------------------------------------------------------------------
  4695. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4696. {
  4697. TControl * Control = GetComponent(Component);
  4698. DebugAssert(Control);
  4699. bool Changed = (Control->Visible != value);
  4700. if (Changed)
  4701. {
  4702. ComponentShowing(Component, value);
  4703. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4704. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4705. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4706. if (value)
  4707. {
  4708. int RemainingHeight = Control->Parent->ClientHeight;
  4709. int RemainingWidth = Control->Parent->ClientWidth;
  4710. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4711. {
  4712. TControl * ChildControl = Control->Parent->Controls[i];
  4713. if (ChildControl->Visible)
  4714. {
  4715. switch (ChildControl->Align)
  4716. {
  4717. case alTop:
  4718. case alBottom:
  4719. RemainingHeight -= ChildControl->Height;
  4720. break;
  4721. case alLeft:
  4722. case alRight:
  4723. RemainingWidth -= ChildControl->Width;
  4724. break;
  4725. }
  4726. }
  4727. }
  4728. int Reserve = ScaleByTextHeight(this, 32);
  4729. // queue in explorer, trees in commander
  4730. if (Control->Height > RemainingHeight - Reserve)
  4731. {
  4732. Control->Height = RemainingHeight / 2;
  4733. }
  4734. if (Control->Width > RemainingWidth - Reserve)
  4735. {
  4736. Control->Width = RemainingWidth / 2;
  4737. }
  4738. }
  4739. Control->Visible = value;
  4740. if (WasFocused && Visible)
  4741. {
  4742. DirView(osCurrent)->SetFocus();
  4743. }
  4744. FixControlsPlacement();
  4745. }
  4746. }
  4747. //---------------------------------------------------------------------------
  4748. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4749. {
  4750. TControl * Control = GetComponent(Component);
  4751. if (Control == NULL)
  4752. {
  4753. return false;
  4754. }
  4755. else
  4756. {
  4757. return Control->Visible;
  4758. }
  4759. }
  4760. //---------------------------------------------------------------------------
  4761. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4762. {
  4763. bool Result;
  4764. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4765. {
  4766. Result = !IsUWP();
  4767. }
  4768. else
  4769. {
  4770. Result = true;
  4771. }
  4772. return Result;
  4773. }
  4774. //---------------------------------------------------------------------------
  4775. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4776. {
  4777. if (DirView(osOther)->ItemFocused != NULL)
  4778. {
  4779. DirView(osOther)->ItemFocused->MakeVisible(false);
  4780. }
  4781. QueueSplitter->Visible = QueuePanel->Visible;
  4782. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4783. }
  4784. //---------------------------------------------------------------------------
  4785. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4786. {
  4787. switch (Component) {
  4788. case fcStatusBar: return RemoteStatusBar;
  4789. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4790. case fcQueueView: return QueuePanel;
  4791. case fcQueueToolbar: return QueueDock;
  4792. case fcRemoteTree: return RemoteDrivePanel;
  4793. case fcSessionsTabs: return SessionsPageControl;
  4794. default: return NULL;
  4795. }
  4796. }
  4797. //---------------------------------------------------------------------------
  4798. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4799. TObject *Sender, TListColumn *Column, TPoint &Point)
  4800. {
  4801. DebugAssert(NonVisualDataModule && Column && Sender);
  4802. NonVisualDataModule->ListColumn = Column;
  4803. TPopupMenu * DirViewColumnMenu;
  4804. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4805. {
  4806. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4807. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4808. (Column->Index == uvName);
  4809. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4810. (Column->Index == uvSize);
  4811. }
  4812. else
  4813. {
  4814. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4815. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4816. (Column->Index == dvName);
  4817. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4818. (Column->Index == dvSize);
  4819. }
  4820. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4821. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4822. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4823. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4824. }
  4825. //---------------------------------------------------------------------------
  4826. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4827. TObject *Sender, TListItem *Item, bool &AllowExec)
  4828. {
  4829. DoDirViewExecFile(Sender, Item, AllowExec);
  4830. }
  4831. //---------------------------------------------------------------------------
  4832. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4833. TListItem * Item, bool & AllowExec)
  4834. {
  4835. DebugAssert(Sender && Item && Configuration);
  4836. DebugAssert(AllowExec);
  4837. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4838. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4839. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4840. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4841. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4842. // on files only (not directories)
  4843. // and only when symlinks are resolved (apply to remote panel only)
  4844. if (!ADirView->ItemIsDirectory(Item) &&
  4845. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4846. {
  4847. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4848. !FForceExecution &&
  4849. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4850. {
  4851. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4852. {
  4853. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4854. UnlockWindow();
  4855. try
  4856. {
  4857. ExecuteFileOperation(
  4858. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4859. }
  4860. __finally
  4861. {
  4862. LockWindow();
  4863. }
  4864. AllowExec = false;
  4865. }
  4866. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4867. {
  4868. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4869. {
  4870. ExecuteFile(Side, efDefaultEditor);
  4871. AllowExec = false;
  4872. }
  4873. }
  4874. else
  4875. {
  4876. DebugFail();
  4877. }
  4878. }
  4879. // if we have not done anything special, fall back to default behavior
  4880. if (AllowExec)
  4881. {
  4882. if (Remote && !WinConfiguration->DisableOpenEdit)
  4883. {
  4884. ExecuteFile(osRemote, efShell);
  4885. AllowExec = false;
  4886. }
  4887. }
  4888. }
  4889. }
  4890. //---------------------------------------------------------------------------
  4891. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4892. {
  4893. return ((Side == osRemote) || (Side == osCurrent));
  4894. }
  4895. //---------------------------------------------------------------------------
  4896. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4897. {
  4898. DebugFail();
  4899. }
  4900. //---------------------------------------------------------------------------
  4901. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4902. {
  4903. DebugFail();
  4904. }
  4905. //---------------------------------------------------------------------------
  4906. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4907. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4908. {
  4909. TSynchronizeParamType Params;
  4910. Params.LocalDirectory = LocalDirectory;
  4911. Params.RemoteDirectory = RemoteDirectory;
  4912. int UnusedParams =
  4913. (GUIConfiguration->SynchronizeParams &
  4914. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4915. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4916. Params.Options = GUIConfiguration->SynchronizeOptions;
  4917. bool SaveSettings = false;
  4918. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4919. &DoSynchronizeTooManyDirectories);
  4920. DebugAssert(FSynchronizeController == NULL);
  4921. FSynchronizeController = &Controller;
  4922. bool Result;
  4923. try
  4924. {
  4925. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4926. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4927. int Options =
  4928. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4929. DebugAssert(FOnFeedSynchronizeError == NULL);
  4930. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4931. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4932. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4933. if (Result)
  4934. {
  4935. if (SaveSettings)
  4936. {
  4937. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4938. GUIConfiguration->SynchronizeOptions = Params.Options;
  4939. }
  4940. else
  4941. {
  4942. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4943. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4944. FLAGSET(Params.Options, soSynchronize))
  4945. {
  4946. GUIConfiguration->SynchronizeOptions =
  4947. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4948. soSynchronize;
  4949. }
  4950. }
  4951. LocalDirectory = Params.LocalDirectory;
  4952. RemoteDirectory = Params.RemoteDirectory;
  4953. }
  4954. }
  4955. __finally
  4956. {
  4957. FSynchronizeController = NULL;
  4958. DebugAssert(FOnFeedSynchronizeError == NULL);
  4959. FOnFeedSynchronizeError = NULL;
  4960. }
  4961. return Result;
  4962. }
  4963. //---------------------------------------------------------------------------
  4964. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4965. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4966. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4967. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4968. TSynchronizeOptions * Options, bool Full)
  4969. {
  4970. // Keep-up-to-date mode
  4971. if (Terminal->Status == ssOpened)
  4972. {
  4973. try
  4974. {
  4975. int PParams = Params.Params;
  4976. if (!Full)
  4977. {
  4978. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4979. if (FLAGSET(Params.Options, soRecurse))
  4980. {
  4981. PParams |= TTerminal::spSubDirs;
  4982. }
  4983. }
  4984. else
  4985. {
  4986. // if keepuptodate is non-recursive,
  4987. // full sync before has to be non-recursive as well
  4988. if (FLAGCLEAR(Params.Options, soRecurse))
  4989. {
  4990. PParams |= TTerminal::spNoRecurse;
  4991. }
  4992. }
  4993. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4994. PParams, Checklist, Options);
  4995. }
  4996. catch (Exception & E)
  4997. {
  4998. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4999. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5000. {
  5001. // noop, already logged in MoreMessageDialog
  5002. }
  5003. else
  5004. {
  5005. ShowExtendedExceptionEx(Terminal, &E);
  5006. throw;
  5007. }
  5008. }
  5009. }
  5010. }
  5011. //---------------------------------------------------------------------------
  5012. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5013. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5014. const UnicodeString ErrorStr)
  5015. {
  5016. if (!Directory.IsEmpty())
  5017. {
  5018. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5019. }
  5020. else
  5021. {
  5022. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5023. }
  5024. }
  5025. //---------------------------------------------------------------------------
  5026. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5027. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5028. {
  5029. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5030. {
  5031. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5032. }
  5033. else
  5034. {
  5035. TMessageParams Params(mpNeverAskAgainCheck);
  5036. unsigned int Result = MessageDialog(
  5037. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5038. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5039. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5040. {
  5041. MaxDirectories *= 2;
  5042. if (Result == qaNeverAskAgain)
  5043. {
  5044. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5045. }
  5046. }
  5047. else
  5048. {
  5049. Abort();
  5050. }
  5051. }
  5052. }
  5053. //---------------------------------------------------------------------------
  5054. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5055. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5056. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5057. TSynchronizeOptions * Options)
  5058. {
  5059. // Keep-up-to-date mode
  5060. DebugAssert(!FAutoOperation);
  5061. void * BatchStorage;
  5062. BatchStart(BatchStorage);
  5063. FAutoOperation = true;
  5064. bool AnyOperation = false;
  5065. TDateTime StartTime = Now();
  5066. TSynchronizeChecklist * AChecklist = NULL;
  5067. try
  5068. {
  5069. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5070. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5071. {
  5072. FSynchronizeProgressForm->Start();
  5073. }
  5074. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5075. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5076. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5077. Options);
  5078. SAFE_DESTROY(FSynchronizeProgressForm);
  5079. AnyOperation = (AChecklist->CheckedCount > 0);
  5080. if (AnyOperation)
  5081. {
  5082. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5083. CreateProgressForm(&SynchronizeProgress);
  5084. try
  5085. {
  5086. Terminal->SynchronizeApply(
  5087. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5088. SynchronizeProcessedItem, NULL, NULL, NULL);
  5089. }
  5090. __finally
  5091. {
  5092. DestroyProgressForm();
  5093. }
  5094. }
  5095. }
  5096. __finally
  5097. {
  5098. if (Checklist == NULL)
  5099. {
  5100. delete AChecklist;
  5101. }
  5102. else
  5103. {
  5104. *Checklist = AChecklist;
  5105. }
  5106. FAutoOperation = false;
  5107. SAFE_DESTROY(FSynchronizeProgressForm);
  5108. BatchEnd(BatchStorage);
  5109. ReloadLocalDirectory();
  5110. if (AnyOperation)
  5111. {
  5112. OperationComplete(StartTime);
  5113. }
  5114. }
  5115. }
  5116. //---------------------------------------------------------------------------
  5117. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5118. {
  5119. // can be called from command line
  5120. return Visible &&
  5121. ((DirView(osOther)->SelCount > 0) ||
  5122. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5123. }
  5124. //---------------------------------------------------------------------------
  5125. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5126. {
  5127. LogSynchronizeEvent(Terminal, Message);
  5128. }
  5129. //---------------------------------------------------------------------------
  5130. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5131. int Params, TSynchronizeOptions & Options)
  5132. {
  5133. DebugAssert(!IsLocalBrowserMode());
  5134. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5135. {
  5136. Options.Filter = new TStringList();
  5137. Options.Filter->CaseSensitive = false;
  5138. Options.Filter->Duplicates = Types::dupAccept;
  5139. if (DirView(osRemote)->SelCount > 0)
  5140. {
  5141. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5142. }
  5143. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5144. {
  5145. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5146. }
  5147. Options.Filter->Sort();
  5148. }
  5149. }
  5150. //---------------------------------------------------------------------------
  5151. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5152. {
  5153. TCopyParamType Defaults;
  5154. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5155. ConfigStorage->AccessMode = smRead;
  5156. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5157. {
  5158. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5159. }
  5160. ConfigStorage.reset(NULL);
  5161. std::unique_ptr<TStringList> Options(new TStringList());
  5162. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5163. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5164. UnicodeString Result;
  5165. if (Options->Count > 0)
  5166. {
  5167. Result =
  5168. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5169. }
  5170. return Result;
  5171. }
  5172. //---------------------------------------------------------------------------
  5173. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5174. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5175. {
  5176. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5177. UnicodeString AdditionalParams =
  5178. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5179. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5180. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5181. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5182. SerializeCopyParamForCommandLine(CopyParams)));
  5183. ExecuteNewInstance(SessionName, AdditionalParams);
  5184. }
  5185. //---------------------------------------------------------------------------
  5186. struct TSynchronizeParams
  5187. {
  5188. UnicodeString LocalDirectory;
  5189. UnicodeString RemoteDirectory;
  5190. TSynchronizeMode Mode;
  5191. TSynchronizeChecklist * Checklist;
  5192. int Params;
  5193. TCopyParamType * CopyParam;
  5194. TDateTime CollectElapsed;
  5195. TDateTime * StartTime;
  5196. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5197. };
  5198. //---------------------------------------------------------------------------
  5199. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5200. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5201. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5202. {
  5203. DebugAssert(!FAutoOperation);
  5204. void * BatchStorage;
  5205. BatchStart(BatchStorage);
  5206. FAutoOperation = true;
  5207. TFileOperationStatistics Statistics;
  5208. TDateTime Start = Now();
  5209. try
  5210. {
  5211. Params.OnProcessedItem = OnProcessedItem;
  5212. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5213. CreateProgressForm(&SynchronizeProgress);
  5214. Terminal->SynchronizeApply(
  5215. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5216. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5217. &Statistics);
  5218. }
  5219. __finally
  5220. {
  5221. Params.OnProcessedItem = NULL;
  5222. FAutoOperation = false;
  5223. DestroyProgressForm();
  5224. BatchEnd(BatchStorage);
  5225. ReloadLocalDirectory();
  5226. }
  5227. if (WinConfiguration->SynchronizeSummary)
  5228. {
  5229. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5230. // The statistics should be 0 anyway in this case
  5231. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5232. {
  5233. Message += L"\n";
  5234. if (Statistics.FilesUploaded > 0)
  5235. {
  5236. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5237. }
  5238. if (Statistics.FilesDownloaded > 0)
  5239. {
  5240. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5241. }
  5242. if (Statistics.FilesDeletedLocal > 0)
  5243. {
  5244. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5245. }
  5246. if (Statistics.FilesDeletedRemote > 0)
  5247. {
  5248. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5249. }
  5250. }
  5251. TDateTime Elapsed = (Now() - Start);
  5252. Message +=
  5253. L"\n" +
  5254. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5255. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5256. TMessageParams Params(mpNeverAskAgainCheck);
  5257. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5258. if (Result == qaNeverAskAgain)
  5259. {
  5260. WinConfiguration->SynchronizeSummary = false;
  5261. }
  5262. }
  5263. }
  5264. //---------------------------------------------------------------------------
  5265. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5266. {
  5267. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5268. {
  5269. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5270. }
  5271. // Not set in keep-up-to-date mode - Synchronize() method
  5272. if (Token != NULL)
  5273. {
  5274. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5275. if (Params.OnProcessedItem != NULL)
  5276. {
  5277. Params.OnProcessedItem(NULL, ChecklistItem);
  5278. }
  5279. }
  5280. }
  5281. //---------------------------------------------------------------------------
  5282. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5283. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5284. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5285. {
  5286. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5287. *Params.StartTime = Now();
  5288. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5289. }
  5290. //---------------------------------------------------------------------------
  5291. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5292. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5293. {
  5294. // terminal can be already closed (e.g. dropped connection)
  5295. if (Terminal != NULL)
  5296. {
  5297. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5298. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5299. }
  5300. }
  5301. //---------------------------------------------------------------------------
  5302. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5303. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5304. {
  5305. DebugAssert(!IsLocalBrowserMode());
  5306. TAutoBatch AutoBatch(this);
  5307. TAutoFlag AutoOperationFlag(FAutoOperation);
  5308. if (Side == osRemote)
  5309. {
  5310. std::unique_ptr<TStrings> FileList(new TStringList());
  5311. FileList->AddObject(FileName, RemoteFile);
  5312. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5313. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5314. RemoteDirView->SaveSelection();
  5315. RemoteDirView->SaveSelectedNames();
  5316. try
  5317. {
  5318. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5319. }
  5320. catch(...)
  5321. {
  5322. RemoteDirView->DiscardSavedSelection();
  5323. throw;
  5324. }
  5325. RemoteDirView->RestoreSelection();
  5326. }
  5327. else if (DebugAlwaysTrue(Side == osLocal))
  5328. {
  5329. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5330. {
  5331. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5332. }
  5333. UnicodeString Directory = ExtractFileDir(FileName);
  5334. ReloadLocalDirectory(Directory);
  5335. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5336. if (!SamePaths(Directory, NewDirectory))
  5337. {
  5338. ReloadLocalDirectory(NewDirectory);
  5339. }
  5340. }
  5341. }
  5342. //---------------------------------------------------------------------------
  5343. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5344. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5345. const TCopyParamType * CopyParams)
  5346. {
  5347. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5348. UnicodeString AdditionalParams =
  5349. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5350. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5351. LocalDirectory, RemoteDirectory, Mode, Params,
  5352. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5353. SerializeCopyParamForCommandLine(CopyParams)));
  5354. ExecuteNewInstance(SessionName, AdditionalParams);
  5355. }
  5356. //---------------------------------------------------------------------------
  5357. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5358. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5359. TSynchronizeMode & Mode, int Params, bool & SaveMode, bool UseDefaults)
  5360. {
  5361. int Result;
  5362. bool SaveSettings = false;
  5363. int Options =
  5364. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5365. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5366. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5367. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5368. bool Continue =
  5369. UseDefaults ||
  5370. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5371. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow);
  5372. if (Continue)
  5373. {
  5374. Configuration->Usage->Inc(L"Synchronizations");
  5375. UpdateCopyParamCounters(CopyParam);
  5376. TSynchronizeOptions SynchronizeOptions;
  5377. GetSynchronizeOptions(Params, SynchronizeOptions);
  5378. if (SaveSettings)
  5379. {
  5380. GUIConfiguration->SynchronizeParams = Params;
  5381. }
  5382. else
  5383. {
  5384. SaveMode = false;
  5385. }
  5386. TDateTime StartTime = Now();
  5387. TSynchronizeChecklist * Checklist = NULL;
  5388. try
  5389. {
  5390. DebugAssert(!FAutoOperation);
  5391. FAutoOperation = true;
  5392. try
  5393. {
  5394. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5395. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5396. int Files = -1;
  5397. if (DataList->Count >= 1)
  5398. {
  5399. Files = StrToIntDef(DataList->Strings[0], Files);
  5400. }
  5401. else
  5402. {
  5403. DataList->Add(UnicodeString());
  5404. }
  5405. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5406. FSynchronizeProgressForm->Start();
  5407. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5408. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5409. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5410. &SynchronizeOptions);
  5411. if (Terminal->SessionData->CacheDirectories)
  5412. {
  5413. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5414. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5415. }
  5416. }
  5417. __finally
  5418. {
  5419. FAutoOperation = false;
  5420. SAFE_DESTROY(FSynchronizeProgressForm);
  5421. }
  5422. TSynchronizeParams SynchronizeParams;
  5423. SynchronizeParams.LocalDirectory = LocalDirectory;
  5424. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5425. SynchronizeParams.Mode = Mode;
  5426. SynchronizeParams.CopyParam = &CopyParam;
  5427. SynchronizeParams.Params = Params;
  5428. SynchronizeParams.Checklist = Checklist;
  5429. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5430. SynchronizeParams.StartTime = &StartTime;
  5431. SynchronizeParams.OnProcessedItem = NULL;
  5432. Result = Checklist->Count;
  5433. if (Checklist->Count > 0)
  5434. {
  5435. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5436. {
  5437. if (!DoSynchronizeChecklistDialog(
  5438. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5439. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5440. {
  5441. Result = -1;
  5442. }
  5443. }
  5444. else
  5445. {
  5446. FullSynchronize(SynchronizeParams, NULL, NULL);
  5447. }
  5448. }
  5449. else
  5450. {
  5451. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5452. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5453. }
  5454. }
  5455. __finally
  5456. {
  5457. delete Checklist;
  5458. }
  5459. OperationComplete(StartTime);
  5460. }
  5461. else
  5462. {
  5463. Result = -1;
  5464. }
  5465. return Result;
  5466. }
  5467. //---------------------------------------------------------------------------
  5468. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5469. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5470. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5471. {
  5472. if (Collect)
  5473. {
  5474. DebugAssert(FSynchronizeProgressForm != NULL);
  5475. if (!FSynchronizeProgressForm->Started)
  5476. {
  5477. FSynchronizeProgressForm->Start();
  5478. }
  5479. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5480. SetTaskbarListProgressValue(CompareProgress);
  5481. }
  5482. else
  5483. {
  5484. DebugAssert(FSynchronizeProgressForm == NULL);
  5485. DebugAssert(FProgressForm != NULL);
  5486. }
  5487. }
  5488. //---------------------------------------------------------------------------
  5489. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5490. {
  5491. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5492. TRemoteFile * File = NULL;
  5493. Terminal->ReadFile(FullFileName, File);
  5494. if (File != NULL)
  5495. {
  5496. std::unique_ptr<TRemoteFile> FileOwner(File);
  5497. TAutoFlag Flag(FStandaloneEditing);
  5498. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5499. Application->ShowMainForm = false;
  5500. Application->Run();
  5501. }
  5502. }
  5503. //---------------------------------------------------------------------------
  5504. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5505. {
  5506. DebugFail();
  5507. }
  5508. //---------------------------------------------------------------------------
  5509. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5510. {
  5511. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5512. SessionData->Assign(Terminal->SessionData);
  5513. UpdateSessionData(SessionData.get());
  5514. Terminal->UpdateSessionCredentials(SessionData.get());
  5515. if (!Terminal->SessionData->HasSessionName())
  5516. {
  5517. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5518. // once IsWorkspace is cleared
  5519. SessionData->Name = UnicodeString();
  5520. SessionData->IsWorkspace = false;
  5521. }
  5522. return SessionData.release();
  5523. }
  5524. //---------------------------------------------------------------------------
  5525. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5526. {
  5527. TSessionData * SessionData = CloneCurrentSessionData();
  5528. try
  5529. {
  5530. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5531. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5532. }
  5533. __finally
  5534. {
  5535. delete SessionData;
  5536. }
  5537. }
  5538. //---------------------------------------------------------------------------
  5539. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5540. {
  5541. TTerminalManager * Manager = TTerminalManager::Instance();
  5542. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5543. if (DebugAlwaysTrue(Terminal != NULL))
  5544. {
  5545. // Update (Managed)Terminal->StateData
  5546. UpdateTerminal(Terminal);
  5547. }
  5548. Manager->SaveWorkspace(DataList.get());
  5549. return DataList.release();
  5550. }
  5551. //---------------------------------------------------------------------------
  5552. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5553. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5554. {
  5555. WinConfiguration->LastWorkspace = Name;
  5556. if (!SavePasswords)
  5557. {
  5558. for (int Index = 0; Index < DataList->Count; Index++)
  5559. {
  5560. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5561. if (SessionData->Link.IsEmpty())
  5562. {
  5563. SessionData->ClearSessionPasswords();
  5564. }
  5565. }
  5566. }
  5567. StoredSessions->NewWorkspace(Name, DataList);
  5568. // modified only
  5569. StoredSessions->Save(false, Explicit);
  5570. }
  5571. //---------------------------------------------------------------------------
  5572. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5573. {
  5574. return
  5575. DefaultStr(WinConfiguration->LastWorkspace,
  5576. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5577. }
  5578. //---------------------------------------------------------------------------
  5579. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5580. {
  5581. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5582. bool AnyNonStoredSessionWithPassword = false;
  5583. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5584. bool AllNonStoredSessionsAnonymous = true;
  5585. TTerminalManager * Manager = TTerminalManager::Instance();
  5586. for (int Index = 0; Index < DataList->Count; Index++)
  5587. {
  5588. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5589. if (SessionData->Link.IsEmpty())
  5590. {
  5591. if (SessionData->HasAnySessionPassword())
  5592. {
  5593. AnyNonStoredSessionWithPassword = true;
  5594. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5595. {
  5596. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5597. }
  5598. }
  5599. if (!SameText(SessionData->UserName, AnonymousUserName))
  5600. {
  5601. AllNonStoredSessionsAnonymous = false;
  5602. }
  5603. }
  5604. }
  5605. bool SavePasswords;
  5606. bool * PSavePasswords;
  5607. bool NotRecommendedSavingPasswords =
  5608. !CustomWinConfiguration->UseMasterPassword &&
  5609. !AllNonStoredSessionsAnonymous;
  5610. if (Configuration->DisablePasswordStoring ||
  5611. !AnyNonStoredSessionWithPassword)
  5612. {
  5613. PSavePasswords = NULL;
  5614. SavePasswords = false;
  5615. }
  5616. else
  5617. {
  5618. PSavePasswords = &SavePasswords;
  5619. SavePasswords =
  5620. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5621. !NotRecommendedSavingPasswords;
  5622. }
  5623. UnicodeString Name = WorkspaceName();
  5624. bool CreateShortcut = false;
  5625. bool Result =
  5626. DoSaveWorkspaceDialog(
  5627. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5628. EnableAutoSave);
  5629. if (Result)
  5630. {
  5631. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5632. if (CreateShortcut)
  5633. {
  5634. TOperationVisualizer Visualizer;
  5635. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5636. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5637. }
  5638. if (EnableAutoSave)
  5639. {
  5640. WinConfiguration->AutoSaveWorkspace = true;
  5641. WinConfiguration->AutoWorkspace = Name;
  5642. if (PSavePasswords != NULL)
  5643. {
  5644. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5645. }
  5646. }
  5647. }
  5648. return Result;
  5649. }
  5650. //---------------------------------------------------------------------------
  5651. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5652. {
  5653. DebugAssert(!IsLocalBrowserMode());
  5654. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5655. if (WinConfiguration->PreservePanelState)
  5656. {
  5657. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5658. }
  5659. UpdateSessionData(Terminal->StateData);
  5660. }
  5661. //---------------------------------------------------------------------------
  5662. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5663. {
  5664. // Keep in sync with TSessionData::CopyStateData
  5665. DebugAssert(!IsLocalBrowserMode());
  5666. DebugAssert(Data != NULL);
  5667. // This is inconsistent with how color (for example) is handled.
  5668. Data->Name = Terminal->SessionData->Name;
  5669. // cannot use RemoteDirView->Path, because it is empty if connection
  5670. // was already closed
  5671. // also only peek, we may not be connected at all atm
  5672. // so make sure we do not try retrieving current directory from the server
  5673. // (particularly with FTP)
  5674. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5675. if (!ACurrentDirectory.IsEmpty())
  5676. {
  5677. Data->RemoteDirectory = ACurrentDirectory;
  5678. }
  5679. Data->Color = SessionColor;
  5680. }
  5681. //---------------------------------------------------------------------------
  5682. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5683. {
  5684. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5685. DebugAssert(Toolbar != NULL);
  5686. for (int i = 0; i < Toolbar->Items->Count; i++)
  5687. {
  5688. TTBXCustomDropDownItem * DropDownItem;
  5689. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5690. if (DropDownItem != NULL)
  5691. {
  5692. ToolbarItemResize(DropDownItem,
  5693. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5694. Toolbar->NonClientWidth);
  5695. break;
  5696. }
  5697. }
  5698. }
  5699. //---------------------------------------------------------------------------
  5700. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5701. {
  5702. Item->EditWidth = Width;
  5703. }
  5704. //---------------------------------------------------------------------------
  5705. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5706. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5707. {
  5708. for (int i = 0; i < Toolbar->Items->Count; i++)
  5709. {
  5710. TTBXCustomDropDownItem * DropDownItem;
  5711. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5712. if (DropDownItem != NULL)
  5713. {
  5714. ASize.x -= DropDownItem->EditWidth;
  5715. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5716. }
  5717. }
  5718. }
  5719. //---------------------------------------------------------------------------
  5720. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5721. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5722. {
  5723. if (WinConfiguration->DDWarnLackOfTempSpace)
  5724. {
  5725. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5726. if (!ADrive.IsEmpty())
  5727. {
  5728. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5729. DebugAssert(RequiredSpace >= 0);
  5730. __int64 RequiredWithReserve;
  5731. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5732. if (FreeSpace < RequiredWithReserve)
  5733. {
  5734. unsigned int Result;
  5735. TMessageParams Params(mpNeverAskAgainCheck);
  5736. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5737. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5738. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5739. if (Result == qaNeverAskAgain)
  5740. {
  5741. WinConfiguration->DDWarnLackOfTempSpace = false;
  5742. }
  5743. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5744. }
  5745. }
  5746. }
  5747. }
  5748. //---------------------------------------------------------------------------
  5749. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5750. {
  5751. DoOpenDirectoryDialog(odAddBookmark, Side);
  5752. }
  5753. //---------------------------------------------------------------------------
  5754. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5755. {
  5756. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5757. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5758. try
  5759. {
  5760. TCustomDirView * DView = DirView(Side);
  5761. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5762. {
  5763. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5764. }
  5765. }
  5766. catch (...)
  5767. {
  5768. delete VisitedDirectories;
  5769. throw;
  5770. }
  5771. return VisitedDirectories;
  5772. }
  5773. //---------------------------------------------------------------------------
  5774. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5775. TOpenDirectoryMode Mode, TOperationSide Side)
  5776. {
  5777. bool Continue = true;
  5778. if (Mode == odAddBookmark)
  5779. {
  5780. TMessageParams Params(mpNeverAskAgainCheck);
  5781. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5782. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5783. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5784. unsigned int Answer =
  5785. MessageDialog(Message,
  5786. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5787. if (Answer == qaNeverAskAgain)
  5788. {
  5789. Continue = true;
  5790. WinConfiguration->UseSharedBookmarks = true;
  5791. }
  5792. else if (Answer == qaOK)
  5793. {
  5794. Continue = true;
  5795. WinConfiguration->UseSharedBookmarks = false;
  5796. }
  5797. else
  5798. {
  5799. Continue = false;
  5800. }
  5801. }
  5802. if (Continue)
  5803. {
  5804. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5805. try
  5806. {
  5807. UnicodeString Name = DirView(Side)->PathName;
  5808. bool Repeat;
  5809. do
  5810. {
  5811. Repeat = false;
  5812. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5813. // do not allow switching to location profiles,
  5814. // if we are not connected
  5815. HasDirView[osLocal] && (Terminal != NULL)))
  5816. {
  5817. TWindowLock Lock(this);
  5818. if (!TryOpenDirectory(Side, Name))
  5819. {
  5820. Repeat = true;
  5821. Mode = odBrowse;
  5822. }
  5823. }
  5824. }
  5825. while (Repeat);
  5826. }
  5827. __finally
  5828. {
  5829. delete VisitedDirectories;
  5830. }
  5831. }
  5832. }
  5833. //---------------------------------------------------------------------------
  5834. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5835. {
  5836. DebugAssert(!FTerminal->CommandSessionOpened);
  5837. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5838. }
  5839. //---------------------------------------------------------------------------
  5840. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5841. {
  5842. bool Result = FTerminal->IsCapable[Capability] ||
  5843. FTerminal->CommandSessionOpened;
  5844. if (!Result)
  5845. {
  5846. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5847. if (!GUIConfiguration->ConfirmCommandSession)
  5848. {
  5849. Result = true;
  5850. }
  5851. else
  5852. {
  5853. TMessageParams Params(mpNeverAskAgainCheck);
  5854. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5855. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5856. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5857. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5858. if (Answer == qaNeverAskAgain)
  5859. {
  5860. GUIConfiguration->ConfirmCommandSession = false;
  5861. Result = true;
  5862. }
  5863. else if (Answer == qaOK)
  5864. {
  5865. Result = true;
  5866. }
  5867. }
  5868. if (Result)
  5869. {
  5870. Result = CommandSessionFallback();
  5871. }
  5872. }
  5873. return Result;
  5874. }
  5875. //---------------------------------------------------------------------------
  5876. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5877. {
  5878. if (EnsureCommandSessionFallback(fcAnyCommand))
  5879. {
  5880. DoConsoleDialog(Terminal, Command);
  5881. }
  5882. }
  5883. //---------------------------------------------------------------------------
  5884. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5885. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5886. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5887. {
  5888. if (!IsSideLocalBrowser(osRemote) &&
  5889. IsFileControl(DropSourceControl, osRemote) &&
  5890. (FDDExtMapFile != NULL))
  5891. {
  5892. Accept = true;
  5893. }
  5894. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5895. DebugAssert(FDDTargetControl != NULL);
  5896. }
  5897. //---------------------------------------------------------------------------
  5898. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5899. _di_IDataObject DataObj, int KeyState,
  5900. const TPoint & Point, int & Effect, bool & Accept)
  5901. {
  5902. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5903. }
  5904. //---------------------------------------------------------------------------
  5905. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5906. _di_IDataObject DataObj, int KeyState,
  5907. const TPoint & Point, int & Effect, bool & Accept)
  5908. {
  5909. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5910. }
  5911. //---------------------------------------------------------------------------
  5912. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5913. TObject * Sender)
  5914. {
  5915. DebugUsedParam(Sender);
  5916. DebugAssert(FDDTargetControl == Sender);
  5917. FDDTargetControl = NULL;
  5918. }
  5919. //---------------------------------------------------------------------------
  5920. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5921. {
  5922. FileControlDDDragLeave(SessionsPageControl);
  5923. }
  5924. //---------------------------------------------------------------------------
  5925. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5926. {
  5927. FileControlDDDragLeave(QueueView3);
  5928. }
  5929. //---------------------------------------------------------------------------
  5930. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5931. {
  5932. DebugAssert(!IsSideLocalBrowser(Side));
  5933. DebugUsedParam(Side);
  5934. bool Edit = false;
  5935. TRemoteFile * File = NULL;
  5936. UnicodeString FileName;
  5937. UnicodeString PointTo;
  5938. bool SymbolicLink = true;
  5939. if (RemoteDirView->ItemFocused)
  5940. {
  5941. DebugAssert(RemoteDirView->ItemFocused->Data);
  5942. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5943. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5944. if (Edit)
  5945. {
  5946. FileName = File->FileName;
  5947. PointTo = File->LinkTo;
  5948. }
  5949. else
  5950. {
  5951. PointTo = File->FileName;
  5952. }
  5953. }
  5954. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5955. Terminal->IsCapable[fcHardLink]))
  5956. {
  5957. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5958. if (Edit)
  5959. {
  5960. DebugAssert(File->FileName == FileName);
  5961. int Params = dfNoRecursive;
  5962. Terminal->ExceptionOnFail = true;
  5963. try
  5964. {
  5965. Terminal->DeleteFile(L"", File, &Params);
  5966. }
  5967. __finally
  5968. {
  5969. Terminal->ExceptionOnFail = false;
  5970. }
  5971. }
  5972. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5973. }
  5974. }
  5975. //---------------------------------------------------------------------------
  5976. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5977. {
  5978. return
  5979. (IsSideLocalBrowser(Side) ||
  5980. ((Terminal != NULL) &&
  5981. Terminal->ResolvingSymlinks &&
  5982. Terminal->IsCapable[fcSymbolicLink]));
  5983. }
  5984. //---------------------------------------------------------------------------
  5985. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5986. {
  5987. return
  5988. !IsSideLocalBrowser(FCurrentSide) &&
  5989. (RemoteDirView->ItemFocused != NULL) &&
  5990. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5991. Terminal->SessionData->ResolveSymlinks;
  5992. }
  5993. //---------------------------------------------------------------------------
  5994. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5995. {
  5996. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5997. FForceExecution = true;
  5998. try
  5999. {
  6000. DirView(osCurrent)->ExecuteCurrentFile();
  6001. }
  6002. __finally
  6003. {
  6004. FForceExecution = false;
  6005. }
  6006. }
  6007. //---------------------------------------------------------------------------
  6008. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6009. {
  6010. TEditorData ExternalEditor;
  6011. ExternalEditor.Editor = edExternal;
  6012. bool Remember = false;
  6013. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6014. {
  6015. if (Remember)
  6016. {
  6017. TEditorList * EditorList = new TEditorList;
  6018. try
  6019. {
  6020. *EditorList = *WinConfiguration->EditorList;
  6021. bool Found = false;
  6022. int i = 0;
  6023. while (!Found && (i < EditorList->Count))
  6024. {
  6025. const TEditorPreferences * Editor = EditorList->Editors[i];
  6026. if ((Editor->Data->Editor == edExternal) &&
  6027. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6028. {
  6029. Found = true;
  6030. }
  6031. i++;
  6032. }
  6033. if (!Found)
  6034. {
  6035. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6036. WinConfiguration->EditorList = EditorList;
  6037. }
  6038. }
  6039. __finally
  6040. {
  6041. delete EditorList;
  6042. }
  6043. }
  6044. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6045. }
  6046. }
  6047. //---------------------------------------------------------------------------
  6048. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6049. {
  6050. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6051. if (FFileFindTerminal == Sender)
  6052. {
  6053. FFileFindTerminal = NULL;
  6054. HideFileFindDialog();
  6055. }
  6056. if (FClipboardTerminal == Sender)
  6057. {
  6058. ClipboardClear(); // implies ClipboardStop
  6059. }
  6060. }
  6061. //---------------------------------------------------------------------------
  6062. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6063. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6064. {
  6065. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6066. DebugAssert(Terminal != NULL);
  6067. if (Data->Terminal == Terminal)
  6068. {
  6069. Data->Terminal = NULL;
  6070. }
  6071. }
  6072. //---------------------------------------------------------------------------
  6073. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6074. {
  6075. UpdateControls();
  6076. SessionColor = TColor(0);
  6077. UpdateRemotePathComboBox(false);
  6078. NeedSession(false);
  6079. }
  6080. //---------------------------------------------------------------------------
  6081. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6082. {
  6083. try
  6084. {
  6085. // Cache, as the login dialog can change its value
  6086. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6087. try
  6088. {
  6089. if (ShowLogin)
  6090. {
  6091. bool ReloadSessions = !Startup;
  6092. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6093. }
  6094. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6095. // This detects if workspace was saved the last time the main widow was closed
  6096. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6097. {
  6098. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6099. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6100. }
  6101. }
  6102. __finally
  6103. {
  6104. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6105. // (so there was no chance for the user to open any session yet)
  6106. if ((ShowLogin || !Startup) &&
  6107. !WinConfiguration->KeepOpenWhenNoSession &&
  6108. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6109. {
  6110. TerminateApplication();
  6111. }
  6112. }
  6113. }
  6114. catch (EAbort &)
  6115. {
  6116. // swallow
  6117. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6118. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6119. }
  6120. }
  6121. //---------------------------------------------------------------------------
  6122. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6123. {
  6124. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6125. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6126. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6127. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6128. try
  6129. {
  6130. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6131. // Clear the design time unthemed tab
  6132. ((SessionsPageControl->PageCount > 0) &&
  6133. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6134. {
  6135. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6136. }
  6137. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6138. {
  6139. TThemeTabSheet * TabSheet;
  6140. if (Index >= SessionsPageControl->PageCount)
  6141. {
  6142. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6143. TabSheet->PageControl = SessionsPageControl;
  6144. }
  6145. else
  6146. {
  6147. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6148. }
  6149. bool IsSessionTab = (Index < TerminalList->Count);
  6150. if (IsSessionTab)
  6151. {
  6152. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6153. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6154. UpdateSessionTab(TabSheet);
  6155. }
  6156. else
  6157. {
  6158. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6159. TabSheet->Tag = 0; // not really needed
  6160. TabSheet->Shadowed = false;
  6161. // 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)
  6162. UpdateNewSessionTab();
  6163. }
  6164. TabSheet->ShowCloseButton = IsSessionTab;
  6165. }
  6166. }
  6167. __finally
  6168. {
  6169. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6170. }
  6171. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6172. }
  6173. //---------------------------------------------------------------------------
  6174. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6175. {
  6176. DoTerminalListChanged();
  6177. }
  6178. //---------------------------------------------------------------------------
  6179. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6180. {
  6181. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6182. DebugAssert(TabSheet->ImageIndex == 0);
  6183. TabSheet->Caption =
  6184. WinConfiguration->SelectiveToolbarText ?
  6185. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6186. UnicodeString();
  6187. }
  6188. //---------------------------------------------------------------------------
  6189. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6190. {
  6191. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6192. }
  6193. //---------------------------------------------------------------------------
  6194. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6195. {
  6196. if (DebugAlwaysTrue(TabSheet != NULL))
  6197. {
  6198. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6199. if (DebugAlwaysTrue(ATerminal != NULL))
  6200. {
  6201. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6202. TabSheet->ImageIndex = AddSessionColor(Color);
  6203. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6204. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6205. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6206. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6207. {
  6208. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6209. }
  6210. }
  6211. }
  6212. }
  6213. //---------------------------------------------------------------------------
  6214. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6215. {
  6216. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6217. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6218. bool Result = (Terminal != NULL);
  6219. if (Result)
  6220. {
  6221. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6222. }
  6223. else
  6224. {
  6225. try
  6226. {
  6227. NewSession(false);
  6228. }
  6229. __finally
  6230. {
  6231. DoTerminalListChanged();
  6232. }
  6233. FSessionsPageControlNewSessionTime = Now();
  6234. }
  6235. return Result;
  6236. }
  6237. //---------------------------------------------------------------------------
  6238. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6239. {
  6240. SessionTabSwitched();
  6241. }
  6242. //---------------------------------------------------------------------------
  6243. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6244. {
  6245. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6246. DebugAssert(TransferList != NULL);
  6247. UnicodeString Name;
  6248. if (TransferList->ItemIndex <= 0)
  6249. {
  6250. Name = L"";
  6251. }
  6252. else
  6253. {
  6254. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6255. }
  6256. if (FCopyParamAutoSelected.IsEmpty())
  6257. {
  6258. // if previous preset was not autoselected, make new preset the "default"
  6259. FCopyParamDefault = Name;
  6260. }
  6261. GUIConfiguration->CopyParamCurrent = Name;
  6262. }
  6263. //---------------------------------------------------------------------------
  6264. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6265. {
  6266. // sanity check
  6267. bool ExistingPreset =
  6268. (FTransferListHoverIndex >= 0) &&
  6269. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6270. DebugAssert(ExistingPreset);
  6271. if (ExistingPreset)
  6272. {
  6273. HDC DC = GetDC(0);
  6274. TCanvas * Canvas = new TCanvas();
  6275. try
  6276. {
  6277. Canvas->Handle = DC;
  6278. Canvas->Font = GetToolbarFont(this);
  6279. UnicodeString Name;
  6280. if (FTransferListHoverIndex == 0)
  6281. {
  6282. Name = L"";
  6283. }
  6284. else
  6285. {
  6286. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6287. }
  6288. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6289. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6290. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6291. static_cast<TObject*>(GetComponent(fcTransferList)));
  6292. UnicodeString InfoStr =
  6293. GUIConfiguration->CopyParamPreset[Name].
  6294. GetInfoStr(L"; ",
  6295. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6296. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6297. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6298. {
  6299. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6300. {
  6301. InfoStr.SetLength(InfoStr.Length() - 1);
  6302. }
  6303. InfoStr += Ellipsis;
  6304. }
  6305. // UpdateCaption does not cause invalidation of whole submenu, while
  6306. // setting Caption property does.
  6307. // also it probably does not resize the label, even if necessary
  6308. // (we do not want that anyway)
  6309. TransferLabel->UpdateCaption(InfoStr);
  6310. }
  6311. __finally
  6312. {
  6313. Canvas->Handle = NULL;
  6314. ReleaseDC(0, DC);
  6315. delete Canvas;
  6316. }
  6317. }
  6318. }
  6319. //---------------------------------------------------------------------------
  6320. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6321. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6322. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6323. {
  6324. if (FTransferListHoverIndex != AHoverIndex)
  6325. {
  6326. FTransferListHoverIndex = AHoverIndex;
  6327. UpdateTransferLabel();
  6328. }
  6329. }
  6330. //---------------------------------------------------------------------------
  6331. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6332. {
  6333. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6334. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6335. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6336. {
  6337. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6338. {
  6339. OpenDirectory(GetSide(osCurrent));
  6340. Message.Result = 1;
  6341. }
  6342. else
  6343. {
  6344. TForm::Dispatch(&Message);
  6345. }
  6346. }
  6347. else
  6348. {
  6349. TForm::Dispatch(&Message);
  6350. }
  6351. }
  6352. //---------------------------------------------------------------------------
  6353. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6354. {
  6355. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6356. if ((FIgnoreNextDialogChar != 0) &&
  6357. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6358. {
  6359. Message.Result = 1;
  6360. }
  6361. else
  6362. {
  6363. TForm::Dispatch(&Message);
  6364. }
  6365. FIgnoreNextDialogChar = 0;
  6366. }
  6367. //---------------------------------------------------------------------------
  6368. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6369. {
  6370. // The four low-order bits are used internally by Windows
  6371. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6372. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6373. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6374. // 61730 - restore thru double click - undocumented
  6375. // 61490 - maximize thru double click - undocumented
  6376. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6377. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6378. {
  6379. SysResizing(Cmd);
  6380. }
  6381. TForm::Dispatch(&Message);
  6382. }
  6383. //---------------------------------------------------------------------------
  6384. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6385. {
  6386. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6387. // Also there no point returning TRUE as we are not able to
  6388. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6389. // Hence the process termination might be safer :)
  6390. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6391. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6392. {
  6393. Message.Result = FALSE;
  6394. }
  6395. else
  6396. {
  6397. Message.Result = TRUE;
  6398. }
  6399. // Do not call default handling as that triggers OnCloseQuery,
  6400. // where our implementation will popup confirmation dialogs, what we do not want,
  6401. // as per Vista guidelines:
  6402. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6403. }
  6404. //---------------------------------------------------------------------------
  6405. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6406. {
  6407. if (Message.EndSession && IsApplicationMinimized())
  6408. {
  6409. // WORKAROUND
  6410. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6411. // when it receives WM_ENDSESSION,
  6412. // but that sometimes (particularly when application is minimized) cause crashes.
  6413. // Crash popups message that blocks log off.
  6414. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6415. // so we call ExitProcess() immediately, not even trying to cleanup.
  6416. // It still causes beep, so there's likely some popup, but it does not block
  6417. // log off.
  6418. ExitProcess(0);
  6419. }
  6420. TForm::Dispatch(&Message);
  6421. }
  6422. #ifdef _DEBUG
  6423. //---------------------------------------------------------------------------
  6424. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6425. {
  6426. TForm::Dispatch(&Message);
  6427. }
  6428. #endif
  6429. //---------------------------------------------------------------------------
  6430. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6431. {
  6432. }
  6433. //---------------------------------------------------------------------------
  6434. void __fastcall TCustomScpExplorerForm::DoShow()
  6435. {
  6436. // only now are the controls resized finally, so the size constraints
  6437. // will not conflict with possibly very small window size
  6438. RestoreFormParams();
  6439. // This indicates that the form was rescaled after parameters were loaded and
  6440. // as most probably the parameters were actually saved with the current form scaling
  6441. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6442. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6443. // and no rounding problems will occur.
  6444. // See also RestoreForm.
  6445. // (we should be safe to call RestoreParams unconditionally)
  6446. if (PixelsPerInch != Screen->PixelsPerInch)
  6447. {
  6448. RestoreParams();
  6449. }
  6450. FixControlsPlacement();
  6451. if (Position == poDefaultPosOnly)
  6452. {
  6453. CutFormToDesktop(this);
  6454. }
  6455. TForm::DoShow();
  6456. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6457. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6458. if (Terminal == NULL)
  6459. {
  6460. StartingDisconnected();
  6461. }
  6462. UpdatePixelsPerInchMainWindowCounter();
  6463. FShowing = true;
  6464. }
  6465. //---------------------------------------------------------------------------
  6466. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6467. {
  6468. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6469. }
  6470. //---------------------------------------------------------------------------
  6471. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6472. {
  6473. DoTerminalListChanged();
  6474. }
  6475. //---------------------------------------------------------------------------
  6476. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6477. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6478. TNotifyEvent OnBalloonClick, TObject * UserData)
  6479. {
  6480. bool Do;
  6481. UnicodeString Message;
  6482. if (E == NULL)
  6483. {
  6484. Message = Str;
  6485. Do = true;
  6486. }
  6487. else
  6488. {
  6489. Do = ExceptionMessage(E, Message);
  6490. }
  6491. if (Do && WinConfiguration->BalloonNotifications)
  6492. {
  6493. UnicodeString Title;
  6494. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6495. {
  6496. Message = TrimLeft(Message);
  6497. }
  6498. else
  6499. {
  6500. Message = UnformatMessage(Message);
  6501. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6502. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6503. Title = LoadResourceString(Captions[Type]);
  6504. if (Terminal != NULL)
  6505. {
  6506. Title = FORMAT(L"%s - %s",
  6507. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6508. }
  6509. }
  6510. if (Seconds == 0)
  6511. {
  6512. Seconds = WinConfiguration->NotificationsTimeout;
  6513. }
  6514. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6515. }
  6516. }
  6517. //---------------------------------------------------------------------------
  6518. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6519. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6520. UnicodeString HelpKeyword, const TMessageParams * Params,
  6521. TTerminal * Terminal)
  6522. {
  6523. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6524. (FOnFeedSynchronizeError != NULL)) &&
  6525. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6526. {
  6527. if (FOnFeedSynchronizeError != NULL)
  6528. {
  6529. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6530. }
  6531. else
  6532. {
  6533. DebugAssert(FErrorList != NULL);
  6534. TStringList * MoreMessagesCopy = NULL;
  6535. if (MoreMessages)
  6536. {
  6537. MoreMessagesCopy = new TStringList();
  6538. MoreMessagesCopy->Assign(MoreMessages);
  6539. }
  6540. FErrorList->AddObject(Message, MoreMessagesCopy);
  6541. }
  6542. PopupTrayBalloon(Terminal, Message, Type);
  6543. return ContinueAnswer(Answers);
  6544. }
  6545. else
  6546. {
  6547. bool UseBalloon = FTrayIcon->Visible;
  6548. if (UseBalloon)
  6549. {
  6550. PopupTrayBalloon(Terminal, Message, Type);
  6551. }
  6552. unsigned int Result;
  6553. try
  6554. {
  6555. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6556. }
  6557. __finally
  6558. {
  6559. // cancel only balloon we popped up, otherwise we may cancel notification
  6560. // balloon that was there before the message dialog
  6561. // (such as "dd confirmation opt in balloon)
  6562. if (UseBalloon)
  6563. {
  6564. FTrayIcon->CancelBalloon();
  6565. }
  6566. }
  6567. return Result;
  6568. }
  6569. }
  6570. //---------------------------------------------------------------------------
  6571. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6572. TTerminal * Terminal, Exception * E)
  6573. {
  6574. if (FTrayIcon->Visible)
  6575. {
  6576. PopupTrayBalloon(Terminal, L"", qtError, E);
  6577. }
  6578. // particularly prevent opening new session from jump list,
  6579. // while exception is shown
  6580. NonVisualDataModule->StartBusy();
  6581. try
  6582. {
  6583. ShowExtendedExceptionEx(Terminal, E);
  6584. }
  6585. __finally
  6586. {
  6587. NonVisualDataModule->EndBusy();
  6588. FTrayIcon->CancelBalloon();
  6589. }
  6590. }
  6591. //---------------------------------------------------------------------------
  6592. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6593. {
  6594. if (Terminal->Active)
  6595. {
  6596. InitStatusBar();
  6597. }
  6598. // cannot rely on active page being page for active terminal,
  6599. // as it can happen that active page is the "new session" page
  6600. // (e.g. when reconnecting active terminal, while login dialog
  6601. // invoked from "new session" page is modal)
  6602. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6603. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6604. }
  6605. //---------------------------------------------------------------------------
  6606. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6607. TTerminal * Terminal, Exception * E)
  6608. {
  6609. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6610. if (!Terminal->Active)
  6611. {
  6612. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6613. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6614. {
  6615. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6616. }
  6617. }
  6618. }
  6619. //---------------------------------------------------------------------------
  6620. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6621. UnicodeString Message, TQueryType Type,
  6622. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6623. {
  6624. if ((E == NULL) ||
  6625. ExceptionMessage(E, Message))
  6626. {
  6627. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6628. if (Important)
  6629. {
  6630. Seconds *= 5;
  6631. }
  6632. UnicodeString NoteMessage(UnformatMessage(Message));
  6633. if (Terminal != NULL)
  6634. {
  6635. NoteMessage = FORMAT(L"%s: %s",
  6636. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6637. }
  6638. if (WinConfiguration->BalloonNotifications)
  6639. {
  6640. AddNote(NoteMessage);
  6641. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6642. }
  6643. else
  6644. {
  6645. FlashOnBackground();
  6646. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6647. }
  6648. }
  6649. }
  6650. //---------------------------------------------------------------------------
  6651. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6652. {
  6653. RestoreApp();
  6654. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6655. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6656. !NonVisualDataModule->Busy)
  6657. {
  6658. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6659. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6660. if (!ComponentVisible[fcQueueView])
  6661. {
  6662. ToggleQueueVisibility();
  6663. GoToQueue();
  6664. }
  6665. }
  6666. }
  6667. //---------------------------------------------------------------------------
  6668. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6669. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6670. {
  6671. UnicodeString Message;
  6672. TNotifyEvent OnClick = NULL;
  6673. TObject * UserData = NULL;
  6674. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6675. switch (Event)
  6676. {
  6677. case qeEmptyButMonitored:
  6678. if ((ATerminal != Terminal) || QueueInvisible)
  6679. {
  6680. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6681. OnClick = QueueEmptyNoteClicked;
  6682. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6683. TerminalNoteData->Terminal = ATerminal;
  6684. UserData = TerminalNoteData;
  6685. }
  6686. break;
  6687. case qeEmpty:
  6688. OperationComplete(ATerminal->QueueOperationStart);
  6689. break;
  6690. case qePendingUserAction:
  6691. if ((ATerminal != Terminal) ||
  6692. (QueueInvisible && !IsQueueAutoPopup()))
  6693. {
  6694. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6695. }
  6696. break;
  6697. default:
  6698. DebugFail();
  6699. }
  6700. if (!Message.IsEmpty())
  6701. {
  6702. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6703. }
  6704. }
  6705. //---------------------------------------------------------------------------
  6706. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6707. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6708. {
  6709. if (FDDExtMapFile != NULL)
  6710. {
  6711. CloseHandle(FDDExtMapFile);
  6712. FDDExtMapFile = NULL;
  6713. }
  6714. if (WinConfiguration->DDFakeFile)
  6715. {
  6716. DDFakeFileInitDrag(FileList, Created);
  6717. }
  6718. }
  6719. //---------------------------------------------------------------------------
  6720. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6721. {
  6722. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6723. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6724. {
  6725. FFakeFileDropTarget = FileName;
  6726. }
  6727. }
  6728. //---------------------------------------------------------------------------
  6729. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6730. {
  6731. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6732. if (!ForceDirectories(ApiPath(Result)))
  6733. {
  6734. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6735. }
  6736. FileSetAttr(ApiPath(Result), faHidden);
  6737. return Result;
  6738. }
  6739. //---------------------------------------------------------------------------
  6740. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6741. bool & Created)
  6742. {
  6743. FFakeFileDropTarget = UnicodeString();
  6744. FDragFakeDirectory = CreateFakeTransferDirectory();
  6745. FileList->AddItem(NULL, FDragFakeDirectory);
  6746. Created = true;
  6747. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6748. {
  6749. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6750. }
  6751. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6752. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6753. {
  6754. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6755. TDragExtCommStruct* CommStruct;
  6756. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6757. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6758. DebugAssert(CommStruct != NULL);
  6759. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6760. CommStruct->Dragging = true;
  6761. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6762. LENOF(CommStruct->DropDest));
  6763. NULL_TERMINATE(CommStruct->DropDest);
  6764. UnmapViewOfFile(CommStruct);
  6765. }
  6766. }
  6767. //---------------------------------------------------------------------------
  6768. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6769. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6770. {
  6771. bool Result;
  6772. DebugAssert(!IsLocalBrowserMode());
  6773. if (Sender == RemoteDirView)
  6774. {
  6775. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6776. }
  6777. else
  6778. {
  6779. DebugAssert(Sender == RemoteDriveView);
  6780. TStrings * FileList = RemoteDriveView->DragFileList();
  6781. try
  6782. {
  6783. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6784. }
  6785. __finally
  6786. {
  6787. delete FileList;
  6788. }
  6789. }
  6790. if (FDDTargetControl == RemoteDriveView)
  6791. {
  6792. RemoteDriveView->UpdateDropTarget();
  6793. }
  6794. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6795. // or from tree view to dir view.
  6796. // foMove happens when file/dir is dragged from remote tree view outside of
  6797. // application via dragex
  6798. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6799. (DropSourceControl == RemoteDriveView))
  6800. {
  6801. RemoteDriveView->UpdateDropSource();
  6802. }
  6803. return Result;
  6804. }
  6805. //---------------------------------------------------------------------------
  6806. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6807. {
  6808. DebugAssert(!IsLocalBrowserMode());
  6809. // This also handles drops of remote files to queue.
  6810. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6811. SAFE_DESTROY(FDDFileList);
  6812. if (!FFakeFileDropTarget.IsEmpty())
  6813. {
  6814. RemoveDir(ApiPath(FFakeFileDropTarget));
  6815. }
  6816. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6817. {
  6818. try
  6819. {
  6820. TDragResult DDResult = (Sender == RemoteDirView) ?
  6821. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6822. // Focus is moved to the target application,
  6823. // but as we are going to present the UI, we need to steal the focus back.
  6824. // This most likely won't work though (windows does not allow application
  6825. // to steal focus most of the time)
  6826. Application->BringToFront();
  6827. // On older version of Windows we never got drMove here, see also comment below.
  6828. // On Windows 10, we get the "move".
  6829. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6830. {
  6831. UnicodeString TargetDirectory;
  6832. TFileOperation Operation;
  6833. // drInvalid may mean drMove, see comment below
  6834. switch (DDResult)
  6835. {
  6836. case drCopy:
  6837. Operation = foCopy;
  6838. break;
  6839. case drMove:
  6840. Operation = foMove;
  6841. break;
  6842. default:
  6843. DebugFail();
  6844. case drInvalid:
  6845. // prefer "copy" for safety
  6846. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6847. break;
  6848. }
  6849. TTransferOperationParam Param;
  6850. UnicodeString CounterName;
  6851. bool ForceQueue;
  6852. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6853. {
  6854. // we get drInvalid both if d&d was intercepted by ddext,
  6855. // and when users drops on no-drop location.
  6856. // we tell the difference by existence of response from ddext,
  6857. // so we ignore absence of response in this case
  6858. if (DDResult != drInvalid)
  6859. {
  6860. // here we know that the extension is installed,
  6861. // as it is checked as soon as drag&drop starts from
  6862. // RemoteFileControlDDCreateDragFileList
  6863. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6864. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6865. {
  6866. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6867. }
  6868. else
  6869. {
  6870. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6871. }
  6872. }
  6873. }
  6874. else
  6875. {
  6876. // download using fake file
  6877. Param.Temp = false;
  6878. Param.DragDrop = true;
  6879. if (ForceQueue)
  6880. {
  6881. Param.Queue = asOn;
  6882. }
  6883. if (Sender == RemoteDirView)
  6884. {
  6885. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6886. }
  6887. if (RemoteFileControlFileOperation(Sender, Operation,
  6888. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6889. {
  6890. Configuration->Usage->Inc(CounterName);
  6891. }
  6892. }
  6893. }
  6894. }
  6895. __finally
  6896. {
  6897. CloseHandle(FDDExtMapFile);
  6898. FDDExtMapFile = NULL;
  6899. delete FDragFakeMonitors;
  6900. FDragFakeMonitors = NULL;
  6901. RemoveDir(ApiPath(FDragFakeDirectory));
  6902. FDragFakeDirectory = L"";
  6903. FFakeFileDropTarget = UnicodeString();
  6904. }
  6905. }
  6906. if (FDragDropSshTerminate.get() != NULL)
  6907. {
  6908. FDragDropSshTerminate->Rethrow();
  6909. }
  6910. }
  6911. //---------------------------------------------------------------------------
  6912. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6913. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6914. {
  6915. // Remember drop effect so we know (when user drops files), if we copy or move
  6916. FLastDropEffect = dwEffect;
  6917. }
  6918. //---------------------------------------------------------------------------
  6919. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6920. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6921. {
  6922. bool Result;
  6923. if (FDDTargetControl == QueueView3)
  6924. {
  6925. Directory = DefaultDownloadTargetDirectory();
  6926. Result = true;
  6927. CounterName = L"DownloadsDragDropQueue";
  6928. ForceQueue = true;
  6929. }
  6930. else if (!FFakeFileDropTarget.IsEmpty())
  6931. {
  6932. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6933. Result = true;
  6934. ForceQueue = false;
  6935. CounterName = L"DownloadsDragDropFakeFile";
  6936. }
  6937. else
  6938. {
  6939. ForceQueue = false;
  6940. Enabled = false;
  6941. try
  6942. {
  6943. int Timer = 0;
  6944. Result = false;
  6945. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6946. {
  6947. {
  6948. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6949. TDragExtCommStruct* CommStruct;
  6950. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6951. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6952. DebugAssert(CommStruct != NULL);
  6953. Result = !CommStruct->Dragging;
  6954. if (Result)
  6955. {
  6956. Directory = ExtractFilePath(CommStruct->DropDest);
  6957. CounterName = L"DownloadsDragDropExternalExt";
  6958. }
  6959. UnmapViewOfFile(CommStruct);
  6960. }
  6961. if (!Result)
  6962. {
  6963. Sleep(50);
  6964. Timer += 50;
  6965. Application->ProcessMessages();
  6966. }
  6967. }
  6968. }
  6969. __finally
  6970. {
  6971. Enabled = true;
  6972. }
  6973. }
  6974. return Result;
  6975. }
  6976. //---------------------------------------------------------------------------
  6977. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6978. const UnicodeString TempDir, int SecDelay)
  6979. {
  6980. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6981. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6982. if (FDelayedDeletionTimer == NULL)
  6983. {
  6984. DebugAssert(HandleAllocated());
  6985. FDelayedDeletionTimer = new TTimer(this);
  6986. FDelayedDeletionTimer->Interval = 10000;
  6987. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6988. }
  6989. else
  6990. {
  6991. FDelayedDeletionTimer->Enabled = true;
  6992. }
  6993. }
  6994. //---------------------------------------------------------------------------
  6995. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6996. {
  6997. DebugAssert(FDelayedDeletionList != NULL);
  6998. TDateTime N = Now();
  6999. TDateTime Alert;
  7000. UnicodeString Directory;
  7001. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7002. {
  7003. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7004. if ((N >= Alert) || (Sender == NULL))
  7005. {
  7006. Directory = FDelayedDeletionList->Strings[Index];
  7007. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7008. {
  7009. FDelayedDeletionList->Delete(Index);
  7010. }
  7011. }
  7012. }
  7013. if (FDelayedDeletionList->Count == 0)
  7014. {
  7015. FDelayedDeletionTimer->Enabled = false;
  7016. }
  7017. }
  7018. //---------------------------------------------------------------------------
  7019. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7020. {
  7021. DebugAssert(!IsLocalBrowserMode());
  7022. if (IsFileControl(FDDTargetControl, osRemote) ||
  7023. (FDDTargetControl == SessionsPageControl))
  7024. {
  7025. TTerminal * TargetTerminal = NULL;
  7026. TFileOperation Operation = foNone;
  7027. if (FDDTargetControl == SessionsPageControl)
  7028. {
  7029. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7030. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7031. // do not allow dropping on the "+" tab
  7032. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7033. if (TargetTerminal != NULL)
  7034. {
  7035. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7036. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7037. {
  7038. Operation = foRemoteMove;
  7039. }
  7040. else
  7041. {
  7042. Operation = foRemoteCopy;
  7043. }
  7044. }
  7045. }
  7046. else
  7047. {
  7048. if (FLastDropEffect == DROPEFFECT_MOVE)
  7049. {
  7050. Operation = foRemoteMove;
  7051. }
  7052. else if (FLastDropEffect == DROPEFFECT_COPY)
  7053. {
  7054. Operation = foRemoteCopy;
  7055. }
  7056. }
  7057. if (Operation != foNone)
  7058. {
  7059. RemoteFileControlFileOperation(DropSourceControl,
  7060. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7061. }
  7062. // abort drag&drop
  7063. Abort();
  7064. }
  7065. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7066. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7067. (FDDTargetControl != QueueView3))
  7068. {
  7069. DebugAssert(!FDragTempDir.IsEmpty());
  7070. TTransferType Type;
  7071. UnicodeString TempDir = FDragTempDir;
  7072. // We clear FDragTempDir before calling
  7073. // just in case it fail (raises exception)
  7074. FDragTempDir = L"";
  7075. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7076. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7077. // empty directory parameter means temp directory -> don't display it!
  7078. UnicodeString TargetDir = L"";
  7079. int Options = 0;
  7080. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7081. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7082. {
  7083. Abort();
  7084. }
  7085. // TargetDir is set when dropped on local file control
  7086. // (this was workaround for legacy dirview event handling, now it should be
  7087. // made prettier)
  7088. if (TargetDir.IsEmpty())
  7089. {
  7090. TargetDir = TempDir;
  7091. if (ForceDirectories(ApiPath(TargetDir)))
  7092. {
  7093. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7094. FPendingTempSpaceWarn = true;
  7095. try
  7096. {
  7097. FDragDropOperation = true;
  7098. int Params = cpTemporary |
  7099. (Type == ttMove ? cpDelete : 0);
  7100. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7101. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7102. }
  7103. __finally
  7104. {
  7105. FDragDropOperation = false;
  7106. FPendingTempSpaceWarn = false;
  7107. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7108. }
  7109. }
  7110. else
  7111. {
  7112. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7113. }
  7114. }
  7115. }
  7116. }
  7117. //---------------------------------------------------------------------------
  7118. void __fastcall TCustomScpExplorerForm::DDDownload(
  7119. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7120. {
  7121. DebugAssert(!IsLocalBrowserMode());
  7122. TAutoBatch AutoBatch(this);
  7123. UpdateCopyParamCounters(*CopyParam);
  7124. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7125. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7126. {
  7127. RemoteDriveView->UpdateDropSource();
  7128. }
  7129. }
  7130. //---------------------------------------------------------------------------
  7131. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7132. {
  7133. public:
  7134. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7135. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7136. {
  7137. }
  7138. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7139. {
  7140. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7141. TDataObjectFilesEx::AllowData(FormatEtc);
  7142. }
  7143. };
  7144. //---------------------------------------------------------------------------
  7145. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7146. TObject * Sender, TDataObject *& DataObject)
  7147. {
  7148. if (FDDExtMapFile != NULL)
  7149. {
  7150. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7151. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7152. {
  7153. Abort();
  7154. }
  7155. if (FileList->Count > 0)
  7156. {
  7157. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7158. if (!FilesObject->IsValid(true, true))
  7159. {
  7160. FilesObject->_Release();
  7161. }
  7162. else
  7163. {
  7164. DataObject = FilesObject;
  7165. }
  7166. }
  7167. }
  7168. }
  7169. //---------------------------------------------------------------------------
  7170. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7171. {
  7172. DebugFail();
  7173. }
  7174. //---------------------------------------------------------------------------
  7175. void __fastcall TCustomScpExplorerForm::GoToTree()
  7176. {
  7177. ComponentVisible[fcRemoteTree] = true;
  7178. DriveView(osRemote)->SetFocus();
  7179. }
  7180. //---------------------------------------------------------------------------
  7181. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7182. TPanelExport Export)
  7183. {
  7184. TCustomDirView * DirView = this->DirView(Side);
  7185. std::unique_ptr<TStrings> ExportData(new TStringList());
  7186. switch (Export)
  7187. {
  7188. case pePath:
  7189. ExportData->Add(DirView->PathName);
  7190. break;
  7191. case peFileList:
  7192. case peFullFileList:
  7193. {
  7194. bool FullPath = (Export == peFullFileList);
  7195. DirView->CreateFileList(false, FullPath, ExportData.get());
  7196. for (int Index = 0; Index < ExportData->Count; Index++)
  7197. {
  7198. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7199. {
  7200. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7201. }
  7202. }
  7203. }
  7204. break;
  7205. default:
  7206. DebugFail();
  7207. }
  7208. return ExportData.release();
  7209. }
  7210. //---------------------------------------------------------------------------
  7211. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7212. TPanelExport Export, TPanelExportDestination Destination)
  7213. {
  7214. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7215. PanelExportStore(Side, Export, Destination, ExportData.get());
  7216. }
  7217. //---------------------------------------------------------------------------
  7218. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7219. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7220. TStrings * ExportData)
  7221. {
  7222. if (Destination == pedClipboard)
  7223. {
  7224. TInstantOperationVisualizer Visualizer;
  7225. CopyToClipboard(ExportData);
  7226. }
  7227. else
  7228. {
  7229. DebugFail();
  7230. }
  7231. }
  7232. //---------------------------------------------------------------------------
  7233. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7234. {
  7235. TCustomDirView * DirView = this->DirView(Side);
  7236. UnicodeString Mask = DirView->Mask;
  7237. if (DoFilterMaskDialog(DirView, Mask))
  7238. {
  7239. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7240. Configuration->Usage->Inc(L"Filters");
  7241. }
  7242. }
  7243. //---------------------------------------------------------------------------
  7244. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7245. {
  7246. return FQueueController->DefaultOperation();
  7247. }
  7248. //---------------------------------------------------------------------------
  7249. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7250. TQueueOperation Operation, void ** Param)
  7251. {
  7252. switch (Operation)
  7253. {
  7254. case qoPreferences:
  7255. return true;
  7256. case qoGoTo:
  7257. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7258. case qoOnceEmpty:
  7259. return !FQueueController->Empty;
  7260. default:
  7261. return FQueueController->AllowOperation(Operation, Param);
  7262. }
  7263. }
  7264. //---------------------------------------------------------------------------
  7265. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7266. {
  7267. if (DebugAlwaysTrue(QueueView3->Visible))
  7268. {
  7269. QueueView3->SetFocus();
  7270. }
  7271. }
  7272. //---------------------------------------------------------------------------
  7273. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7274. TQueueOperation Operation, void * Param)
  7275. {
  7276. if (Operation == qoGoTo)
  7277. {
  7278. GoToQueue();
  7279. }
  7280. else if (Operation == qoPreferences)
  7281. {
  7282. PreferencesDialog(pmQueue);
  7283. }
  7284. else
  7285. {
  7286. FQueueController->ExecuteOperation(Operation, Param);
  7287. }
  7288. }
  7289. //---------------------------------------------------------------------------
  7290. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7291. {
  7292. return (Queue != NULL) && Queue->Enabled;
  7293. }
  7294. //---------------------------------------------------------------------------
  7295. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7296. {
  7297. DebugAssert(Queue != NULL);
  7298. if (Queue != NULL)
  7299. {
  7300. Queue->Enabled = !Queue->Enabled;
  7301. }
  7302. }
  7303. //---------------------------------------------------------------------------
  7304. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7305. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7306. {
  7307. FQueueActedItem = QueueView3->ItemFocused;
  7308. }
  7309. //---------------------------------------------------------------------------
  7310. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7311. {
  7312. return TopDock->Height + QueueSplitter->Height;
  7313. }
  7314. //---------------------------------------------------------------------------
  7315. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7316. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7317. {
  7318. // when queue is hidden by double-clicking splitter, stray attempt to
  7319. // resize the panel with strange value arrives, make sure it is ignored
  7320. if (ComponentVisible[fcQueueView])
  7321. {
  7322. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7323. RemotePanel->Constraints->MinHeight;
  7324. if (NewSize > HeightLimit)
  7325. {
  7326. NewSize = HeightLimit;
  7327. }
  7328. }
  7329. else
  7330. {
  7331. Accept = false;
  7332. }
  7333. }
  7334. //---------------------------------------------------------------------------
  7335. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7336. TDragObject *& /*DragObject*/)
  7337. {
  7338. FQueueActedItem = QueueView3->ItemFocused;
  7339. }
  7340. //---------------------------------------------------------------------------
  7341. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7342. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7343. {
  7344. Accept = true;
  7345. if (Source == QueueView3)
  7346. {
  7347. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7348. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7349. if (Accept)
  7350. {
  7351. TQueueItemProxy * QueueItem;
  7352. TQueueItemProxy * DestQueueItem;
  7353. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7354. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7355. Accept = (QueueItem != DestQueueItem) &&
  7356. (QueueItem->Status == TQueueItem::qsPending) &&
  7357. (DestQueueItem->Status == TQueueItem::qsPending);
  7358. }
  7359. }
  7360. }
  7361. //---------------------------------------------------------------------------
  7362. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7363. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7364. {
  7365. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7366. {
  7367. TQueueItemProxy * QueueItem;
  7368. TQueueItemProxy * DestQueueItem;
  7369. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7370. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7371. QueueItem->Move(DestQueueItem);
  7372. }
  7373. }
  7374. //---------------------------------------------------------------------------
  7375. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7376. {
  7377. if ((QueueView3->ItemFocused == NULL) &&
  7378. (QueueView3->Items->Count > 0))
  7379. {
  7380. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7381. }
  7382. QueueLabelUpdateStatus();
  7383. }
  7384. //---------------------------------------------------------------------------
  7385. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7386. {
  7387. QueueLabelUpdateStatus();
  7388. }
  7389. //---------------------------------------------------------------------------
  7390. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7391. {
  7392. QueueLabel->UpdateStatus();
  7393. }
  7394. //---------------------------------------------------------------------------
  7395. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7396. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7397. {
  7398. if (Selected)
  7399. {
  7400. NonVisualDataModule->UpdateNonVisibleActions();
  7401. }
  7402. }
  7403. //---------------------------------------------------------------------------
  7404. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7405. {
  7406. TDragDropFilesEx * Result = NULL;
  7407. if (Sender == SessionsPageControl)
  7408. {
  7409. Result = FSessionsDragDropFilesEx;
  7410. }
  7411. else if (Sender == QueueView3)
  7412. {
  7413. Result = FQueueDragDropFilesEx;
  7414. }
  7415. else
  7416. {
  7417. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7418. if (DirView != NULL)
  7419. {
  7420. Result = DirView->DragDropFilesEx;
  7421. }
  7422. else
  7423. {
  7424. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7425. if (DriveView != NULL)
  7426. {
  7427. Result = DriveView->DragDropFilesEx;
  7428. }
  7429. }
  7430. }
  7431. DebugAssert(Result != NULL);
  7432. return Result;
  7433. }
  7434. //---------------------------------------------------------------------------
  7435. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7436. {
  7437. return (DView->SelCount == DView->FilesCount);
  7438. }
  7439. //---------------------------------------------------------------------------
  7440. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7441. {
  7442. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7443. }
  7444. //---------------------------------------------------------------------------
  7445. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7446. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7447. {
  7448. DebugAssert(!IsLocalBrowserMode());
  7449. TFileOperation Operation;
  7450. switch (Effect)
  7451. {
  7452. case DROPEFFECT_MOVE:
  7453. Operation = foMove;
  7454. break;
  7455. case DROPEFFECT_COPY:
  7456. // occures on WinXP (reported by user)
  7457. default:
  7458. Operation = foCopy;
  7459. break;
  7460. };
  7461. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7462. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7463. if (DragDropFilesEx->FileList->Count > 0)
  7464. {
  7465. TStrings * FileList = new TStringList();
  7466. try
  7467. {
  7468. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7469. {
  7470. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7471. }
  7472. FDragDropOperation = true;
  7473. TTransferOperationParam Param;
  7474. Param.TargetDirectory = TargetPath;
  7475. // upload, no temp dirs
  7476. Param.Temp = false;
  7477. Param.DragDrop = DragDrop;
  7478. Param.Options =
  7479. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7480. if (ForceQueue)
  7481. {
  7482. Param.Queue = asOn;
  7483. }
  7484. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7485. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7486. {
  7487. if (IsFileControl(DropSourceControl, osLocal))
  7488. {
  7489. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7490. }
  7491. else
  7492. {
  7493. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7494. }
  7495. }
  7496. }
  7497. __finally
  7498. {
  7499. FDragDropOperation = false;
  7500. delete FileList;
  7501. }
  7502. }
  7503. }
  7504. //---------------------------------------------------------------------------
  7505. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7506. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7507. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7508. {
  7509. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7510. }
  7511. //---------------------------------------------------------------------------
  7512. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7513. TObject * Sender, int grfKeyState, int & dwEffect)
  7514. {
  7515. DebugAssert(!IsLocalBrowserMode());
  7516. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7517. if ((dwEffect != DROPEFFECT_NONE) &&
  7518. IsFileControl(DropSourceControl, osRemote))
  7519. {
  7520. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7521. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7522. (RemoteDirView->DropTarget == NULL))
  7523. {
  7524. dwEffect = DROPEFFECT_NONE;
  7525. }
  7526. else
  7527. {
  7528. if (dwEffect == DROPEFFECT_COPY)
  7529. {
  7530. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7531. // currently we support copying always (at least via temporary directory);
  7532. // remove associated checks once this all proves stable and working
  7533. bool CopyCapable = true;
  7534. // if we do not support neither of operations, there's no discussion
  7535. if (!MoveCapable && !CopyCapable)
  7536. {
  7537. dwEffect = DROPEFFECT_NONE;
  7538. }
  7539. else
  7540. {
  7541. // The default effect inside remote panel is move,
  7542. // unless we do not support it, but support copy
  7543. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7544. {
  7545. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7546. }
  7547. else
  7548. {
  7549. // with ctrl-down, we want copy unless it is not supported
  7550. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7551. }
  7552. }
  7553. }
  7554. }
  7555. }
  7556. }
  7557. //---------------------------------------------------------------------------
  7558. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7559. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7560. {
  7561. DebugAssert(!IsLocalBrowserMode());
  7562. if (FDDTotalSize >= 0)
  7563. {
  7564. if (File->IsDirectory)
  7565. {
  7566. FDDTotalSize = -1;
  7567. }
  7568. else
  7569. {
  7570. FDDTotalSize += File->Size;
  7571. }
  7572. }
  7573. DebugAssert(!FDragTempDir.IsEmpty());
  7574. // TODO: this is quite ineffective
  7575. // TODO: what if invalid character replacement is disabled?
  7576. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7577. UnicodeString TransferFileName;
  7578. if (Sender == RemoteDriveView)
  7579. {
  7580. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7581. }
  7582. else
  7583. {
  7584. TransferFileName = File->FileName;
  7585. }
  7586. FDDFileList->AddObject(TransferFileName, File);
  7587. }
  7588. //---------------------------------------------------------------------------
  7589. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7590. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7591. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7592. {
  7593. // sometimes we do not get DDEnd so the list is not released
  7594. SAFE_DESTROY(FDDFileList);
  7595. FDDFileList = new TStringList();
  7596. FDragTempDir = WinConfiguration->TemporaryDir();
  7597. FDDTotalSize = 0;
  7598. FDragDropSshTerminate.reset(NULL);
  7599. }
  7600. //---------------------------------------------------------------------------
  7601. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7602. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7603. HRESULT & Result)
  7604. {
  7605. if (Result == DRAGDROP_S_DROP)
  7606. {
  7607. try
  7608. {
  7609. GlobalDragImageList->HideDragImage();
  7610. try
  7611. {
  7612. RemoteFileControlDDTargetDrop();
  7613. }
  7614. catch(ESshTerminate & E)
  7615. {
  7616. FDragDropSshTerminate.reset(E.Clone());
  7617. }
  7618. }
  7619. catch (Exception &E)
  7620. {
  7621. // If downloading fails we need to cancel drag&drop, otherwise
  7622. // Explorer shows error
  7623. // But by the way exception probably never reach this point as
  7624. // it's catched on way.
  7625. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7626. Result = DRAGDROP_S_CANCEL;
  7627. ShowExtendedException(Terminal, &E);
  7628. }
  7629. }
  7630. }
  7631. //---------------------------------------------------------------------------
  7632. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7633. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7634. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7635. {
  7636. TFileMasks::TParams MaskParams;
  7637. MaskParams.Size = Size;
  7638. MaskParams.Modification = Modification;
  7639. // this does not re-parse the mask if it is the same as the last time
  7640. FDirViewMatchMask = Masks;
  7641. bool ImplicitMatch;
  7642. Matches =
  7643. // RecurseInclude parameter has no effect as the Path is empty
  7644. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7645. (AllowImplicitMatches || !ImplicitMatch);
  7646. }
  7647. //---------------------------------------------------------------------------
  7648. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7649. TObject * Sender, TListItem * Item, WORD & Indexes)
  7650. {
  7651. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7652. UnicodeString Ext;
  7653. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7654. {
  7655. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7656. }
  7657. else
  7658. {
  7659. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7660. }
  7661. if (SameText(Ext, Configuration->PartialExt))
  7662. {
  7663. Indexes |= oiPartial;
  7664. }
  7665. }
  7666. //---------------------------------------------------------------------------
  7667. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7668. {
  7669. return
  7670. DirViewEnabled(osCurrent) &&
  7671. DirView(osCurrent)->CanPasteFromClipBoard();
  7672. }
  7673. //---------------------------------------------------------------------------
  7674. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7675. {
  7676. bool Result = false;
  7677. if (CanPasteToDirViewFromClipBoard())
  7678. {
  7679. Result = true;
  7680. }
  7681. else
  7682. {
  7683. UnicodeString ClipboardText;
  7684. if (NonEmptyTextFromClipboard(ClipboardText))
  7685. {
  7686. if (StoredSessions->IsUrl(ClipboardText))
  7687. {
  7688. Result = true;
  7689. }
  7690. else
  7691. {
  7692. Result = DirViewEnabled(osCurrent);
  7693. }
  7694. }
  7695. }
  7696. return Result;
  7697. }
  7698. //---------------------------------------------------------------------------
  7699. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7700. {
  7701. if (DoesClipboardContainOurFiles())
  7702. {
  7703. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7704. {
  7705. DebugAssert(!IsLocalBrowserMode());
  7706. TTerminalManager * Manager = TTerminalManager::Instance();
  7707. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7708. Manager->ActiveTerminal = FClipboardTerminal;
  7709. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7710. }
  7711. }
  7712. else if (CanPasteToDirViewFromClipBoard())
  7713. {
  7714. DirView(osCurrent)->PasteFromClipBoard();
  7715. }
  7716. else
  7717. {
  7718. UnicodeString ClipboardText;
  7719. if (NonEmptyTextFromClipboard(ClipboardText))
  7720. {
  7721. if (StoredSessions->IsUrl(ClipboardText))
  7722. {
  7723. NewSession(false, ClipboardText);
  7724. }
  7725. else
  7726. {
  7727. DirView(osCurrent)->Path = ClipboardText;
  7728. }
  7729. }
  7730. }
  7731. }
  7732. //---------------------------------------------------------------------------
  7733. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7734. {
  7735. // TBD
  7736. }
  7737. //---------------------------------------------------------------------------
  7738. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7739. {
  7740. TCustomDirView * ADirView = DirView(Side);
  7741. ADirView->SelectAll(Mode);
  7742. ADirView->SetFocus();
  7743. }
  7744. //---------------------------------------------------------------------------
  7745. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7746. {
  7747. TCustomDirView * ADirView = DirView(Side);
  7748. TFileFilter Filter;
  7749. DefaultFileFilter(Filter);
  7750. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7751. {
  7752. Configuration->Usage->Inc(L"MaskSelections");
  7753. ADirView->SelectFiles(Filter, Select);
  7754. ADirView->SetFocus();
  7755. }
  7756. }
  7757. //---------------------------------------------------------------------------
  7758. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7759. {
  7760. TCustomDirView * ADirView = DirView(Side);
  7761. ADirView->RestoreSelectedNames();
  7762. ADirView->SetFocus();
  7763. }
  7764. //---------------------------------------------------------------------------
  7765. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7766. {
  7767. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7768. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7769. {
  7770. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7771. UnicodeString Ext;
  7772. if (!IsSideLocalBrowser(osCurrent))
  7773. {
  7774. Ext = UnixExtractFileExt(FileName);
  7775. }
  7776. else
  7777. {
  7778. Ext = ExtractFileExt(FileName);
  7779. }
  7780. if (Ext.IsEmpty())
  7781. {
  7782. Ext = L".";
  7783. }
  7784. TFileFilter Filter;
  7785. Filter.Masks = FORMAT(L"*%s", (Ext));
  7786. Filter.Directories = false;
  7787. CurrentDirView->SelectFiles(Filter, Select);
  7788. }
  7789. }
  7790. //---------------------------------------------------------------------------
  7791. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7792. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7793. {
  7794. UnicodeString Result;
  7795. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7796. {
  7797. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7798. }
  7799. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7800. {
  7801. // noop
  7802. }
  7803. else
  7804. {
  7805. Result =
  7806. FMTLOAD(FILE_INFO_FORMAT,
  7807. (FormatBytes(FileInfo.SelectedSize),
  7808. FormatBytes(FileInfo.FilesSize),
  7809. FormatNumber(FileInfo.SelectedCount),
  7810. FormatNumber(FileInfo.FilesCount)));
  7811. }
  7812. return Result;
  7813. }
  7814. //---------------------------------------------------------------------------
  7815. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7816. TTBXStatusPanel * Panel, TOperationSide Side)
  7817. {
  7818. if (Panel->Index == 1)
  7819. {
  7820. ToggleShowHiddenFiles();
  7821. }
  7822. else if (Panel->Index == 2)
  7823. {
  7824. Filter(Side);
  7825. }
  7826. }
  7827. //---------------------------------------------------------------------------
  7828. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7829. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7830. {
  7831. DebugAssert(!StatusBar->SimplePanel);
  7832. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7833. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7834. }
  7835. //---------------------------------------------------------------------------
  7836. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7837. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7838. {
  7839. DebugAssert(StatusBar->Panels->Count >= 3);
  7840. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7841. if (FileInfo.HiddenCount > 0)
  7842. {
  7843. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7844. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7845. }
  7846. else
  7847. {
  7848. HiddenFilesPanel->ViewPriority = 0;
  7849. // not really necessary, just to cleanup no-longer-valid data
  7850. HiddenFilesPanel->Caption = L"";
  7851. }
  7852. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7853. if (FileInfo.FilteredCount > 0)
  7854. {
  7855. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7856. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7857. }
  7858. else
  7859. {
  7860. FilteredFilesPanel->ViewPriority = 0;
  7861. // not really necessary, just to cleanup no-longer-valid data
  7862. FilteredFilesPanel->Caption = L"";
  7863. }
  7864. }
  7865. //---------------------------------------------------------------------------
  7866. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7867. TObject * /*Sender*/)
  7868. {
  7869. if (DirView(osRemote)->Enabled)
  7870. {
  7871. DirView(osRemote)->SetFocus();
  7872. }
  7873. }
  7874. //---------------------------------------------------------------------------
  7875. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7876. {
  7877. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7878. switch (Config.Show)
  7879. {
  7880. case qvShow:
  7881. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7882. {
  7883. Config.Show = qvHide;
  7884. }
  7885. else
  7886. {
  7887. Config.Show = Config.LastHideShow;
  7888. }
  7889. break;
  7890. case qvHideWhenEmpty:
  7891. if (ComponentVisible[fcQueueView])
  7892. {
  7893. Config.Show = qvHide;
  7894. }
  7895. else
  7896. {
  7897. Config.LastHideShow = Config.Show;
  7898. Config.Show = qvShow;
  7899. }
  7900. break;
  7901. case qvHide:
  7902. Config.LastHideShow = Config.Show;
  7903. Config.Show = qvShow;
  7904. break;
  7905. }
  7906. WinConfiguration->QueueView = Config;
  7907. }
  7908. //---------------------------------------------------------------------------
  7909. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7910. {
  7911. UnicodeString Result;
  7912. if (FTerminal != NULL)
  7913. {
  7914. switch (WinConfiguration->PathInCaption)
  7915. {
  7916. case picShort:
  7917. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7918. break;
  7919. case picFull:
  7920. Result = FTerminal->CurrentDirectory;
  7921. break;
  7922. }
  7923. }
  7924. return Result;
  7925. }
  7926. //---------------------------------------------------------------------------
  7927. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7928. {
  7929. TColor Result = GetWindowColor(FSessionColor);
  7930. return Result;
  7931. }
  7932. //---------------------------------------------------------------------------
  7933. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7934. {
  7935. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7936. return Result;
  7937. }
  7938. //---------------------------------------------------------------------------
  7939. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7940. {
  7941. TTerminalManager::Instance()->UpdateAppTitle();
  7942. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7943. // background to gray on Windows 7 (works on Windows 10).
  7944. // See also EnableControl
  7945. if (Showing)
  7946. {
  7947. if (Terminal != NULL)
  7948. {
  7949. // Update path when it changes
  7950. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7951. {
  7952. UpdateSessionTab(SessionsPageControl->ActivePage);
  7953. }
  7954. }
  7955. NonVisualDataModule->ReconnectSessionAction->Update();
  7956. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  7957. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  7958. CenterReconnectToolbar();
  7959. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7960. if (HasTerminal)
  7961. {
  7962. // TODO needed yet with second local browser?
  7963. if (!RemoteDirView->Enabled)
  7964. {
  7965. RemoteDirView->Enabled = true;
  7966. if (FRemoteDirViewWasFocused)
  7967. {
  7968. ActiveControl = RemoteDirView;
  7969. }
  7970. }
  7971. RemoteDirView->Color = PanelColor();
  7972. }
  7973. else
  7974. {
  7975. if (RemoteDirView->Enabled)
  7976. {
  7977. // This is first called when the form is being constructed
  7978. // (not anymore due to Showing test above)
  7979. // but the false is overriden in the constructor later.
  7980. // An even later in TScpCommanderForm::DoShow()
  7981. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7982. RemoteDirView->Enabled = false;
  7983. RemoteDirView->Color = DisabledPanelColor();
  7984. }
  7985. }
  7986. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  7987. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  7988. RemoteDriveView->Color = RemoteDirView->Color;
  7989. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  7990. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  7991. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  7992. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  7993. QueueLabelUpdateStatus();
  7994. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  7995. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  7996. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7997. }
  7998. }
  7999. //---------------------------------------------------------------------------
  8000. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8001. {
  8002. UpdateControls();
  8003. }
  8004. //---------------------------------------------------------------------------
  8005. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8006. TObject * Sender)
  8007. {
  8008. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8009. DebugAssert(DirView != NULL);
  8010. DoDirViewLoaded(DirView);
  8011. TransferPresetAutoSelect();
  8012. }
  8013. //---------------------------------------------------------------------------
  8014. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8015. {
  8016. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8017. // first run after installation
  8018. if (double(Updates.LastCheck) == 0)
  8019. {
  8020. // make sure next time there will be an update (if enabled)
  8021. Updates.LastCheck = TDateTime(1);
  8022. WinConfiguration->Updates = Updates;
  8023. }
  8024. else if ((double(Updates.Period) > 0) &&
  8025. (Now() - Updates.LastCheck >= Updates.Period))
  8026. {
  8027. TThreadMethod OnUpdatesChecked = NULL;
  8028. if (!IsUWP())
  8029. {
  8030. OnUpdatesChecked = UpdatesChecked;
  8031. }
  8032. StartUpdateThread(OnUpdatesChecked);
  8033. }
  8034. }
  8035. //---------------------------------------------------------------------------
  8036. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8037. {
  8038. UnicodeString Message;
  8039. bool New;
  8040. TQueryType Type;
  8041. GetUpdatesMessage(Message, New, Type, false);
  8042. if (!Message.IsEmpty())
  8043. {
  8044. if (New)
  8045. {
  8046. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8047. }
  8048. if (!New && (Type != qtWarning))
  8049. {
  8050. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8051. }
  8052. else
  8053. {
  8054. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8055. }
  8056. if (New)
  8057. {
  8058. Configuration->Usage->Inc(L"UpdateNotifications");
  8059. }
  8060. }
  8061. }
  8062. //---------------------------------------------------------------------------
  8063. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8064. {
  8065. RestoreApp();
  8066. bool CanDisplay = !NonVisualDataModule->Busy;
  8067. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8068. {
  8069. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8070. }
  8071. if (CanDisplay)
  8072. {
  8073. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8074. CheckForUpdates(true);
  8075. }
  8076. else
  8077. {
  8078. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8079. }
  8080. }
  8081. //---------------------------------------------------------------------------
  8082. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8083. TCopyParamRuleData & Data)
  8084. {
  8085. DebugAssert(Terminal != NULL);
  8086. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8087. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8088. Data.RemoteDirectory = RemoteDirView->PathName;
  8089. }
  8090. //---------------------------------------------------------------------------
  8091. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8092. {
  8093. // Terminal can be null when we are changing local directory implicitly,
  8094. // because it has been deleted, while no session is connected
  8095. // (Login dialog is open)
  8096. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8097. {
  8098. DebugAssert(!IsLocalBrowserMode());
  8099. TCopyParamRuleData Data;
  8100. GetTransferPresetAutoSelectData(Data);
  8101. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8102. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8103. if (CopyParamIndex < 0)
  8104. {
  8105. // there is no preset that matches autoselection
  8106. // set preset that we consider "default"
  8107. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8108. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8109. }
  8110. else
  8111. {
  8112. // there is preset matching autoselection
  8113. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8114. if (CopyParamName == FCopyParamAutoSelected)
  8115. {
  8116. // autoselected the same preset as the last time
  8117. // make no change (i.e. preserve custom user preset, if any)
  8118. }
  8119. else
  8120. {
  8121. // autoselected the different preset then the last time (or there
  8122. // was default preset set)
  8123. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8124. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8125. }
  8126. }
  8127. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8128. {
  8129. if (CopyParamIndex >= 0)
  8130. {
  8131. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8132. }
  8133. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8134. try
  8135. {
  8136. int Fmt =
  8137. (CopyParamIndex < 0) ?
  8138. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8139. COPY_PARAM_AUTOSELECTED;
  8140. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8141. Data->Message = MainInstructions(Message);
  8142. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8143. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8144. CopyParamAttrs);
  8145. if (CopyParamIndex >= 0)
  8146. {
  8147. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8148. Info = FORMAT(L"%s\n \n%s", (Info,
  8149. FMTLOAD(COPY_PARAM_RULE,
  8150. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8151. }
  8152. Data->Message += L"\n\n" + Info;
  8153. if (WinConfiguration->CopyParamAutoSelectNotice)
  8154. {
  8155. TransferPresetNoteMessage(Data, true);
  8156. }
  8157. else
  8158. {
  8159. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8160. Data = NULL; // ownership passed
  8161. }
  8162. }
  8163. __finally
  8164. {
  8165. delete Data;
  8166. }
  8167. }
  8168. }
  8169. }
  8170. //---------------------------------------------------------------------------
  8171. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8172. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8173. {
  8174. DebugAssert(NoteData != NULL);
  8175. DebugAssert(!IsLocalBrowserMode());
  8176. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8177. TQueryButtonAlias Aliases[1];
  8178. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8179. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8180. Params.Aliases = Aliases;
  8181. Params.AliasesCount = LENOF(Aliases);
  8182. unsigned int Result =
  8183. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8184. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8185. switch (Result)
  8186. {
  8187. case qaNeverAskAgain:
  8188. DebugAssert(AllowNeverAskAgain);
  8189. WinConfiguration->CopyParamAutoSelectNotice = false;
  8190. break;
  8191. case qaIgnore:
  8192. PreferencesDialog(pmPresets);
  8193. break;
  8194. }
  8195. }
  8196. //---------------------------------------------------------------------------
  8197. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8198. {
  8199. // as of now useless, as this is used for notes only, never for balloons, ...
  8200. RestoreApp();
  8201. // .. and we should never be busy here
  8202. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8203. {
  8204. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8205. }
  8206. }
  8207. //---------------------------------------------------------------------------
  8208. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8209. TPreferencesMode APreferencesMode)
  8210. {
  8211. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8212. TCopyParamRuleData Data;
  8213. if (Terminal != NULL)
  8214. {
  8215. PreferencesData.reset(new TPreferencesDialogData());
  8216. GetTransferPresetAutoSelectData(Data);
  8217. PreferencesData->CopyParamRuleData = &Data;
  8218. }
  8219. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8220. }
  8221. //---------------------------------------------------------------------------
  8222. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8223. const TCustomCommandType & Command)
  8224. {
  8225. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8226. {
  8227. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8228. }
  8229. }
  8230. //---------------------------------------------------------------------------
  8231. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8232. {
  8233. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8234. TCustomCommandType Command;
  8235. // make sure we use local custom command when remote are not supported
  8236. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8237. {
  8238. Command = FLastCustomCommand;
  8239. }
  8240. else
  8241. {
  8242. Command.Params = Command.Params | ccLocal;
  8243. }
  8244. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8245. FEditingFocusedAdHocCommand = OnFocused;
  8246. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8247. int Options =
  8248. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8249. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8250. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8251. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8252. {
  8253. FLastCustomCommand = Command;
  8254. UpdateCustomCommandsToolbar();
  8255. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8256. }
  8257. }
  8258. //---------------------------------------------------------------------------
  8259. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8260. {
  8261. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8262. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8263. DebugAssert(State > 0);
  8264. if (State <= 0)
  8265. {
  8266. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8267. }
  8268. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8269. }
  8270. //---------------------------------------------------------------------------
  8271. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8272. TCustomCommandType & Command, int & State)
  8273. {
  8274. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8275. if (Result)
  8276. {
  8277. Command = FLastCustomCommand;
  8278. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8279. }
  8280. return Result;
  8281. }
  8282. //---------------------------------------------------------------------------
  8283. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8284. {
  8285. ResetIncrementalSearch();
  8286. }
  8287. //---------------------------------------------------------------------------
  8288. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8289. {
  8290. DebugAssert(ComponentVisible[Component]);
  8291. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8292. }
  8293. //---------------------------------------------------------------------------
  8294. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8295. {
  8296. // when queue panel is resized here directly, the status bar is stretched
  8297. // over whole space the panel occupied
  8298. PostComponentHide(fcQueueView);
  8299. }
  8300. //---------------------------------------------------------------------------
  8301. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8302. {
  8303. WinConfiguration->ResetSysDarkTheme();
  8304. ConfigurationChanged();
  8305. ConfigureInterface();
  8306. TForm::Dispatch(&Message);
  8307. }
  8308. //---------------------------------------------------------------------------
  8309. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8310. {
  8311. TMessage * M = static_cast<TMessage*>(Message);
  8312. switch (M->Msg)
  8313. {
  8314. case CM_DIALOGCHAR:
  8315. CMDialogChar(*M);
  8316. break;
  8317. case WM_APPCOMMAND:
  8318. WMAppCommand(*M);
  8319. break;
  8320. case WM_SYSCOMMAND:
  8321. WMSysCommand(*M);
  8322. break;
  8323. case WM_QUERYENDSESSION:
  8324. WMQueryEndSession(*M);
  8325. break;
  8326. case WM_ENDSESSION:
  8327. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8328. break;
  8329. #ifdef _DEBUG
  8330. case WM_WINDOWPOSCHANGED:
  8331. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8332. break;
  8333. case CM_CUSTOMSTYLECHANGED:
  8334. TForm::Dispatch(Message);
  8335. break;
  8336. #endif
  8337. case WM_COMPONENT_HIDE:
  8338. {
  8339. Byte Component = static_cast<Byte>(M->WParam);
  8340. // sanity check
  8341. if (ComponentVisible[Component])
  8342. {
  8343. // special treatment
  8344. if (Component == fcQueueView)
  8345. {
  8346. ToggleQueueVisibility();
  8347. DebugAssert(!ComponentVisible[fcQueueView]);
  8348. }
  8349. else
  8350. {
  8351. ComponentVisible[Component] = false;
  8352. }
  8353. }
  8354. }
  8355. break;
  8356. case WM_COPYDATA:
  8357. WMCopyData(*M);
  8358. break;
  8359. case WM_MANAGES_CAPTION:
  8360. // caption managed in TTerminalManager::UpdateAppTitle()
  8361. M->Result = 1;
  8362. break;
  8363. case WM_WANTS_MOUSEWHEEL:
  8364. M->Result = 1;
  8365. break;
  8366. case CM_SHOWINGCHANGED:
  8367. CMShowingChanged(*M);
  8368. break;
  8369. case WM_CLOSE:
  8370. WMClose(*M);
  8371. break;
  8372. case WM_DPICHANGED:
  8373. WMDpiChanged(*M);
  8374. break;
  8375. case CM_DPICHANGED:
  8376. CMDpiChanged(*M);
  8377. break;
  8378. case WM_WININICHANGE:
  8379. WMWinIniChange(*M);
  8380. break;
  8381. case CM_DIALOGKEY:
  8382. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8383. break;
  8384. default:
  8385. TForm::Dispatch(Message);
  8386. break;
  8387. }
  8388. }
  8389. //---------------------------------------------------------------------------
  8390. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8391. {
  8392. // noop
  8393. }
  8394. //---------------------------------------------------------------------------
  8395. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8396. {
  8397. TForm::Dispatch(&Message);
  8398. FSessionColors->Clear();
  8399. AddFixedSessionImages();
  8400. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8401. UpdateImages();
  8402. }
  8403. //---------------------------------------------------------------------------
  8404. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8405. {
  8406. TForm::Dispatch(&Message);
  8407. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8408. UpdatePixelsPerInchMainWindowCounter();
  8409. }
  8410. //---------------------------------------------------------------------------
  8411. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8412. {
  8413. // Cannot close window while we are busy.
  8414. // We cannot test this in FormCloseQuery as that is called also from
  8415. // Close(), which is called by CloseApplicationAction. So we can be busy
  8416. // there already even, when it is legitimate to close the application.
  8417. // Possibly a better place to handle this would be WMSysCommand.
  8418. if (NonVisualDataModule->Busy)
  8419. {
  8420. Message.Result = 1;
  8421. }
  8422. else
  8423. {
  8424. TForm::Dispatch(&Message);
  8425. }
  8426. }
  8427. //---------------------------------------------------------------------------
  8428. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8429. {
  8430. TForm::Dispatch(&Message);
  8431. if (Showing && (Terminal == NULL))
  8432. {
  8433. // When we are starting minimized (i.e. from an installer),
  8434. // postpone showing Login dialog until we get restored.
  8435. // Otherwise the Login dialog (and Authentication window) show restored
  8436. // over invisible (minimized) main window.
  8437. if (WindowState == wsMinimized)
  8438. {
  8439. FNeedSession = true;
  8440. }
  8441. else
  8442. {
  8443. // This happens before application ever goes idle, so the toolbars would
  8444. // stay enabled (initial state) until the Login dialog is dismissed.
  8445. UpdateActions();
  8446. NonVisualDataModule->StartBusy();
  8447. try
  8448. {
  8449. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8450. // otherwise the dialog does not receive focus.
  8451. // With Commander interface the ProcessMessages is called already
  8452. // by TDriveView, but with Explorer interface, we need to call it explicily
  8453. Application->ProcessMessages();
  8454. // do not reload sessions, they have been loaded just now (optimization)
  8455. NeedSession(true);
  8456. }
  8457. __finally
  8458. {
  8459. NonVisualDataModule->EndBusy();
  8460. }
  8461. }
  8462. }
  8463. }
  8464. //---------------------------------------------------------------------------
  8465. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8466. {
  8467. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8468. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8469. }
  8470. //---------------------------------------------------------------------------
  8471. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8472. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8473. int & MaxHeight)
  8474. {
  8475. // workaround for bug in TWinControl.CalcConstraints
  8476. // Check for empty rect (restore from iconic state) is done there only after
  8477. // call to AdjustClientRect, which enlarges the rect (for forms).
  8478. TRect R = GetClientRect();
  8479. // when restoring from iconic state, place no restrictions
  8480. if (IsRectEmpty(R))
  8481. {
  8482. MinWidth = 0;
  8483. MinHeight = 0;
  8484. MaxWidth = 32000;
  8485. MaxHeight = 32000;
  8486. }
  8487. }
  8488. //---------------------------------------------------------------------------
  8489. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8490. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8491. {
  8492. // terminal can be already closed (e.g. dropped connection)
  8493. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8494. {
  8495. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8496. try
  8497. {
  8498. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8499. }
  8500. catch(...)
  8501. {
  8502. if (!Terminal->Active)
  8503. {
  8504. Close = true;
  8505. }
  8506. throw;
  8507. }
  8508. }
  8509. }
  8510. //---------------------------------------------------------------------------
  8511. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8512. {
  8513. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8514. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8515. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8516. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8517. {
  8518. OnGetSpaceAvailable = GetSpaceAvailable;
  8519. }
  8520. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8521. OnGetSpaceAvailable);
  8522. }
  8523. //---------------------------------------------------------------------------
  8524. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8525. {
  8526. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8527. Terminal->FillSessionDataForCode(Data.get());
  8528. return Data.release();
  8529. }
  8530. //---------------------------------------------------------------------------
  8531. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8532. {
  8533. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8534. DoGenerateUrlDialog(Data.get(), Paths);
  8535. }
  8536. //---------------------------------------------------------------------------
  8537. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8538. {
  8539. GenerateUrl(NULL);
  8540. }
  8541. //---------------------------------------------------------------------------
  8542. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8543. {
  8544. std::unique_ptr<TStrings> Paths(new TStringList());
  8545. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8546. GenerateUrl(Paths.get());
  8547. }
  8548. //---------------------------------------------------------------------------
  8549. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8550. {
  8551. FSessionColor = value;
  8552. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8553. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8554. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8555. DebugAssert(ColorItem != NULL);
  8556. ColorItem->Color = C;
  8557. NonVisualDataModule->ColorMenuItem->Color = C;
  8558. UpdateControls();
  8559. }
  8560. //---------------------------------------------------------------------------
  8561. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8562. {
  8563. if (value != FSessionColor)
  8564. {
  8565. UpdateSessionColor(value);
  8566. // Is null when called from LastTerminalClosed
  8567. if (Terminal != NULL)
  8568. {
  8569. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8570. }
  8571. }
  8572. }
  8573. //---------------------------------------------------------------------------
  8574. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8575. {
  8576. bool Result = FNoteTimer->Enabled;
  8577. if (Result)
  8578. {
  8579. // cannot cancel note too early
  8580. bool NotEarly =
  8581. (Now() - FNoteShown >
  8582. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8583. if (Force || NotEarly)
  8584. {
  8585. FNoteTimer->Enabled = false;
  8586. FNote = L"";
  8587. SAFE_DESTROY(FNoteData);
  8588. FOnNoteClick = NULL;
  8589. FNoteHints = FNotes->Text;
  8590. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8591. UpdateStatusBar();
  8592. }
  8593. }
  8594. return Result;
  8595. }
  8596. //---------------------------------------------------------------------------
  8597. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8598. {
  8599. DebugAssert(FNoteTimer->Enabled);
  8600. CancelNote(true);
  8601. }
  8602. //---------------------------------------------------------------------------
  8603. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8604. {
  8605. int P = Note.Pos(L"\n");
  8606. if (P > 0)
  8607. {
  8608. Note.SetLength(P - 1);
  8609. }
  8610. FNotes->Add(FORMAT(L"[%s] %s",
  8611. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8612. while (FNotes->Count > 10)
  8613. {
  8614. FNotes->Delete(0);
  8615. }
  8616. if (UpdateNow)
  8617. {
  8618. FNoteHints = FNotes->Text;
  8619. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8620. UpdateStatusBar();
  8621. }
  8622. }
  8623. //---------------------------------------------------------------------------
  8624. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8625. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8626. {
  8627. int P = Note.Pos(L"\n");
  8628. if (P > 0)
  8629. {
  8630. Note.SetLength(P - 1);
  8631. }
  8632. FNoteHints = FNotes->Text;
  8633. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8634. FNote = Note;
  8635. SAFE_DESTROY(FNoteData);
  8636. FNoteData = NoteData;
  8637. FOnNoteClick = OnNoteClick;
  8638. AddNote(Note, false);
  8639. UpdateStatusBar();
  8640. FNoteShown = Now();
  8641. FNoteTimer->Enabled = false;
  8642. if (Seconds == 0)
  8643. {
  8644. Seconds = WinConfiguration->NotificationsTimeout;
  8645. }
  8646. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8647. FNoteTimer->Enabled = true;
  8648. }
  8649. //---------------------------------------------------------------------------
  8650. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8651. {
  8652. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8653. }
  8654. //---------------------------------------------------------------------------
  8655. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8656. {
  8657. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8658. {
  8659. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8660. }
  8661. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8662. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8663. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8664. 0, NULL, NULL);
  8665. }
  8666. //---------------------------------------------------------------------------
  8667. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8668. {
  8669. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8670. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8671. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8672. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8673. }
  8674. //---------------------------------------------------------------------------
  8675. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8676. {
  8677. WinConfiguration->FormatSizeBytes = Style;
  8678. }
  8679. //---------------------------------------------------------------------------
  8680. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8681. {
  8682. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8683. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8684. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8685. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8686. }
  8687. //---------------------------------------------------------------------------
  8688. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8689. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8690. {
  8691. if (Panel->Index == 0)
  8692. {
  8693. if (FOnNoteClick != NULL)
  8694. {
  8695. // prevent the user data from being freed by possible call
  8696. // to CancelNote or PostNote during call to OnNoteClick
  8697. std::unique_ptr<TObject> NoteData(FNoteData);
  8698. TNotifyEvent OnNoteClick = FOnNoteClick;
  8699. FNoteData = NULL;
  8700. // need to cancel the note as we are going to delete its user data
  8701. CancelNote(true);
  8702. OnNoteClick(NoteData.get());
  8703. }
  8704. }
  8705. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8706. {
  8707. FileSystemInfo();
  8708. }
  8709. }
  8710. //---------------------------------------------------------------------------
  8711. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8712. {
  8713. // workaround:
  8714. // 1) for unknown reason, disabling window, while minimized,
  8715. // prevents it from restoring, even if it was enabled again meanwhile
  8716. // 2) when disabling the main window, while another has focus
  8717. // minimize is no longer possible
  8718. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8719. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8720. // Shouldn't we use IsApplicationMinimized() here?
  8721. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8722. {
  8723. Enabled = false;
  8724. }
  8725. FLockLevel++;
  8726. }
  8727. //---------------------------------------------------------------------------
  8728. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8729. {
  8730. DebugAssert(FLockLevel > 0);
  8731. FLockLevel--;
  8732. if (FLockLevel == 0)
  8733. {
  8734. DebugAssert(FLockSuspendLevel == 0);
  8735. Enabled = true;
  8736. }
  8737. }
  8738. //---------------------------------------------------------------------------
  8739. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8740. {
  8741. // We need to make sure that window is enabled when the last modal window closes
  8742. // otherwise focus is not restored correctly.
  8743. // So we re-enable the window when modal window opens and
  8744. // disable it back after is closes
  8745. if (FLockLevel > 0)
  8746. {
  8747. // while we have nesting counter, we know that we never be called
  8748. // recursivelly as Application->OnModalBegin is called only
  8749. // for the top-level modal window
  8750. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8751. {
  8752. // won't be disabled when conditions in LockWindow() were not satisfied
  8753. FDisabledOnLockSuspend = !Enabled;
  8754. // When minimized to tray (or actually when set to SW_HIDE),
  8755. // setting Enabled makes the window focusable even when there's
  8756. // modal window over it
  8757. if (!FTrayIcon->Visible)
  8758. {
  8759. Enabled = true;
  8760. }
  8761. }
  8762. FLockSuspendLevel++;
  8763. }
  8764. }
  8765. //---------------------------------------------------------------------------
  8766. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8767. {
  8768. if (FLockSuspendLevel > 0)
  8769. {
  8770. DebugAssert(FLockLevel > 0);
  8771. FLockSuspendLevel--;
  8772. // see comment in SuspendWindowLock
  8773. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8774. {
  8775. // Note that window can be enabled here, when we were minized to tray when
  8776. // was SuspendWindowLock() called.
  8777. // We should possibly do the same check as in LockWindow(),
  8778. // if it is ever possible that the consitions change between
  8779. // SuspendWindowLock() and ResumeWindowLock()
  8780. Enabled = !FDisabledOnLockSuspend;
  8781. }
  8782. }
  8783. }
  8784. //---------------------------------------------------------------------------
  8785. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8786. {
  8787. if (!TextOnly)
  8788. {
  8789. TTBXComboBoxItem * RemotePathComboBox =
  8790. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8791. TStrings * Items = RemotePathComboBox->Strings;
  8792. Items->BeginUpdate();
  8793. try
  8794. {
  8795. Items->Clear();
  8796. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8797. {
  8798. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8799. while (!IsUnixRootPath(APath))
  8800. {
  8801. int P = APath.LastDelimiter(L'/');
  8802. DebugAssert(P >= 0);
  8803. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8804. APath.SetLength(P - 1);
  8805. }
  8806. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8807. }
  8808. }
  8809. __finally
  8810. {
  8811. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8812. // Setting ItemIndex to -1 does not reset its text
  8813. if (Items->Count == 0)
  8814. {
  8815. RemotePathComboBox->Text = L"";
  8816. }
  8817. Items->EndUpdate();
  8818. }
  8819. }
  8820. }
  8821. //---------------------------------------------------------------------------
  8822. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8823. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8824. int & ImageIndex)
  8825. {
  8826. if (AIndex < 0)
  8827. {
  8828. AIndex = Sender->ItemIndex;
  8829. }
  8830. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8831. }
  8832. //---------------------------------------------------------------------------
  8833. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8834. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8835. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8836. {
  8837. ARect.Left += (10 * AIndex);
  8838. }
  8839. //---------------------------------------------------------------------------
  8840. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8841. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8842. {
  8843. AWidth += (10 * AIndex);
  8844. }
  8845. //---------------------------------------------------------------------------
  8846. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8847. TObject * Sender)
  8848. {
  8849. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8850. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8851. int Index = RemotePathComboBox->ItemIndex;
  8852. while (Index < RemotePathComboBox->Strings->Count - 1)
  8853. {
  8854. APath = UnixExtractFileDir(APath);
  8855. Index++;
  8856. }
  8857. // VanDyke style paths
  8858. if (APath.IsEmpty())
  8859. {
  8860. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8861. APath = ROOTDIRECTORY;
  8862. }
  8863. if (RemoteDirView->Path != APath)
  8864. {
  8865. RemoteDirView->Path = APath;
  8866. }
  8867. }
  8868. //---------------------------------------------------------------------------
  8869. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8870. {
  8871. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8872. DebugUsedParam(Sender);
  8873. UpdateRemotePathComboBox(true);
  8874. }
  8875. //---------------------------------------------------------------------------
  8876. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8877. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8878. {
  8879. ResetIncrementalSearch();
  8880. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8881. DebugAssert(DirView != NULL);
  8882. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8883. {
  8884. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8885. // OnEditing is called also from TCustomListView::CanEdit
  8886. if (Edit != NULL)
  8887. {
  8888. EditSelectBaseName(Edit);
  8889. }
  8890. }
  8891. }
  8892. //---------------------------------------------------------------------------
  8893. //---------------------------------------------------------------------------
  8894. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8895. {
  8896. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8897. Result->AutoDetectDnD = false;
  8898. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8899. Result->RenderDataOn = rdoEnterAndDropSync;
  8900. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8901. return Result;
  8902. }
  8903. //---------------------------------------------------------------------------
  8904. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8905. {
  8906. TForm::CreateWnd();
  8907. if (FSessionsDragDropFilesEx == NULL)
  8908. {
  8909. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8910. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8911. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8912. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8913. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8914. }
  8915. if (FQueueDragDropFilesEx == NULL)
  8916. {
  8917. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8918. // No need to set OnDragOver as we do not have any restrictions
  8919. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8920. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8921. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8922. }
  8923. }
  8924. //---------------------------------------------------------------------------
  8925. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8926. {
  8927. TForm::DestroyWnd();
  8928. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8929. FQueueDragDropFilesEx->DragDropControl = NULL;
  8930. }
  8931. //---------------------------------------------------------------------------
  8932. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8933. {
  8934. SideEnter(FCurrentSide);
  8935. FEverShown = true;
  8936. }
  8937. //---------------------------------------------------------------------------
  8938. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8939. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8940. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8941. {
  8942. if (!NonVisualDataModule->Busy)
  8943. {
  8944. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8945. Configuration->Usage->Inc(L"FileFinds");
  8946. LockWindow(true);
  8947. NonVisualDataModule->StartBusy();
  8948. WinConfiguration->LockedInterface = true;
  8949. try
  8950. {
  8951. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8952. }
  8953. __finally
  8954. {
  8955. WinConfiguration->LockedInterface = false;
  8956. NonVisualDataModule->EndBusy();
  8957. UnlockWindow();
  8958. }
  8959. }
  8960. }
  8961. //---------------------------------------------------------------------------
  8962. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8963. {
  8964. if (!NonVisualDataModule->Busy)
  8965. {
  8966. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8967. SetFocus();
  8968. RemoteDirView->Path = UnixExtractFilePath(Path);
  8969. UnicodeString FileName = UnixExtractFileName(Path);
  8970. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  8971. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  8972. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  8973. if (Item == NULL)
  8974. {
  8975. ReloadDirectory(osRemote);
  8976. Item = RemoteDirView->FindFileItem(FileName);
  8977. }
  8978. if (Item != NULL)
  8979. {
  8980. RemoteDirView->ItemFocused = Item;
  8981. RemoteDirView->ItemFocused->MakeVisible(false);
  8982. RemoteDirView->SetFocus();
  8983. }
  8984. }
  8985. }
  8986. //---------------------------------------------------------------------------
  8987. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8988. {
  8989. bool Result = !NonVisualDataModule->Busy;
  8990. if (Result)
  8991. {
  8992. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8993. }
  8994. return Result;
  8995. }
  8996. //---------------------------------------------------------------------------
  8997. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8998. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8999. {
  9000. if (CanOperateOnFoundFiles(ATerminal))
  9001. {
  9002. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9003. FOnFileOperationFinished = OnFileOperationFinished;
  9004. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9005. }
  9006. }
  9007. //---------------------------------------------------------------------------
  9008. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9009. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9010. {
  9011. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9012. }
  9013. //---------------------------------------------------------------------------
  9014. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9015. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9016. {
  9017. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9018. }
  9019. //---------------------------------------------------------------------------
  9020. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9021. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9022. {
  9023. if (CanOperateOnFoundFiles(ATerminal))
  9024. {
  9025. for (int Index = 0; Index < FileList->Count; Index++)
  9026. {
  9027. UnicodeString FileName = FileList->Strings[Index];
  9028. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9029. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9030. OnFileOperationFinished(FileName, true);
  9031. }
  9032. }
  9033. }
  9034. //---------------------------------------------------------------------------
  9035. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9036. {
  9037. FFileFindTerminal = Terminal;
  9038. ShowFileFindDialog(
  9039. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9040. DoDownloadFoundFiles, DoEditFoundFiles);
  9041. }
  9042. //---------------------------------------------------------------------------
  9043. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9044. {
  9045. FTaskbarList = TaskbarList;
  9046. }
  9047. //---------------------------------------------------------------------------
  9048. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9049. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9050. {
  9051. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9052. if (Index >= 0)
  9053. {
  9054. if (Button == mbLeft)
  9055. {
  9056. // "Mouse down" is raised only after tab is switched.
  9057. // If switching tab (switching session) takes long enough for user
  9058. // to actually release the button, "mouse down" is still raised,
  9059. // but we do not get "mouse up" event, so dragging is not cancelled,
  9060. // prevent that by not beginning dragging in the first place.
  9061. if (IsKeyPressed(VK_LBUTTON))
  9062. {
  9063. // when user clicks the "+", we get mouse down only after the session
  9064. // is closed, when new session tab is already on X:Y, so dragging
  9065. // starts, prevent that
  9066. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9067. {
  9068. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9069. if (Terminal != NULL)
  9070. {
  9071. SessionsPageControl->BeginDrag(false);
  9072. }
  9073. }
  9074. }
  9075. }
  9076. else if (Button == mbMiddle)
  9077. {
  9078. // ignore middle-click for "New session tab"
  9079. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9080. if (Terminal != NULL)
  9081. {
  9082. CloseSessionTab(Index);
  9083. }
  9084. }
  9085. }
  9086. }
  9087. //---------------------------------------------------------------------------
  9088. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9089. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9090. {
  9091. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9092. // do not allow dropping on the "+" tab
  9093. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9094. if ((TargetTerminal != NULL) &&
  9095. (SessionsPageControl->ActivePage->PageIndex != Index))
  9096. {
  9097. Configuration->Usage->Inc(L"SessionTabMoves");
  9098. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9099. // but we want to actually prevent that to avoid flicker
  9100. SessionsPageControl->ActivePage->PageIndex = Index;
  9101. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9102. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9103. }
  9104. }
  9105. //---------------------------------------------------------------------------
  9106. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9107. TObject * Sender, TObject * Source, int X, int Y,
  9108. TDragState /*State*/, bool & Accept)
  9109. {
  9110. Accept = (Sender == Source);
  9111. if (Accept)
  9112. {
  9113. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9114. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9115. // do not allow dragging to the "+" tab
  9116. Accept = (Terminal != NULL);
  9117. }
  9118. }
  9119. //---------------------------------------------------------------------------
  9120. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9121. const TPoint & Point, int & Effect)
  9122. {
  9123. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9124. if (Index < 0)
  9125. {
  9126. Effect = DROPEFFECT_NONE;
  9127. }
  9128. else
  9129. {
  9130. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9131. // do not allow dropping on the "+" tab
  9132. if (TargetTerminal == NULL)
  9133. {
  9134. Effect = DROPEFFECT_NONE;
  9135. }
  9136. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9137. {
  9138. Effect = DROPEFFECT_COPY;
  9139. }
  9140. }
  9141. }
  9142. //---------------------------------------------------------------------------
  9143. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9144. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9145. {
  9146. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9147. // do not allow dropping on the "+" tab
  9148. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9149. if (TargetTerminal != NULL)
  9150. {
  9151. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9152. if (!IsFileControl(DropSourceControl, osRemote))
  9153. {
  9154. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9155. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9156. // Why don't we use Terminal->CurrentDirectory directly?
  9157. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9158. // do not force queue, drag drop
  9159. false, true);
  9160. }
  9161. }
  9162. }
  9163. //---------------------------------------------------------------------------
  9164. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9165. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9166. {
  9167. // Downloads are handled in RemoteFileControlDDEnd
  9168. if (!IsFileControl(DropSourceControl, osRemote))
  9169. {
  9170. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9171. Terminal->CurrentDirectory,
  9172. // force queue, drag drop
  9173. true, true);
  9174. }
  9175. }
  9176. //---------------------------------------------------------------------------
  9177. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9178. {
  9179. FShowing = false;
  9180. // Do not save empty workspace
  9181. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9182. {
  9183. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9184. UnicodeString Name = WorkspaceName();
  9185. DoSaveWorkspace(
  9186. Name, DataList.get(),
  9187. !Configuration->DisablePasswordStoring &&
  9188. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9189. WinConfiguration->LastStoredSession = Name;
  9190. }
  9191. }
  9192. //---------------------------------------------------------------------------
  9193. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9194. {
  9195. TManagedTerminal * ManagedTerminal =
  9196. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9197. if (ManagedTerminal != NULL)
  9198. {
  9199. ManagedTerminal->DirectoryLoaded = Now();
  9200. }
  9201. }
  9202. //---------------------------------------------------------------------------
  9203. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9204. TListItem * /*Item*/, bool /*Selected*/)
  9205. {
  9206. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9207. switch (DirView->LastSelectMethod)
  9208. {
  9209. case smKeyboard:
  9210. Configuration->Usage->Inc(L"KeyboardSelections");
  9211. break;
  9212. case smMouse:
  9213. Configuration->Usage->Inc(L"MouseSelections");
  9214. break;
  9215. }
  9216. }
  9217. //---------------------------------------------------------------------------
  9218. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9219. {
  9220. if (Color != 0)
  9221. {
  9222. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9223. }
  9224. else
  9225. {
  9226. return FSessionTabImageIndex;
  9227. }
  9228. }
  9229. //---------------------------------------------------------------------------
  9230. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9231. {
  9232. TPngImageCollectionItem * Item =
  9233. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9234. FSessionColors->AddPng(Item->PngImage);
  9235. return FSessionColors->Count - 1;
  9236. }
  9237. //---------------------------------------------------------------------------
  9238. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9239. {
  9240. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9241. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9242. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9243. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9244. }
  9245. //---------------------------------------------------------------------------
  9246. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9247. {
  9248. for (int Index = 0; Index < Control->ControlCount; Index++)
  9249. {
  9250. TControl * AControl = Control->Controls[Index];
  9251. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9252. if (WinControl != NULL)
  9253. {
  9254. CollectItemsWithTextDisplayMode(WinControl);
  9255. }
  9256. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9257. if (Toolbar != NULL)
  9258. {
  9259. // we care for top-level items only
  9260. TTBCustomItem * Items = Toolbar->Items;
  9261. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9262. {
  9263. TTBCustomItem * Item = Items->Items[ItemIndex];
  9264. if (((Item->DisplayMode == nbdmImageAndText) ||
  9265. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9266. EligibleForImageDisplayMode(Item))
  9267. {
  9268. FItemsWithTextDisplayMode.insert(Item);
  9269. }
  9270. }
  9271. }
  9272. }
  9273. }
  9274. //---------------------------------------------------------------------------
  9275. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9276. {
  9277. return true;
  9278. }
  9279. //---------------------------------------------------------------------------
  9280. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9281. {
  9282. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9283. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9284. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9285. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9286. bool Result = true;
  9287. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9288. {
  9289. TTBCustomItem * Item = *i;
  9290. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9291. if (Label != NULL)
  9292. {
  9293. // optimization
  9294. if (Label->Visible == SelectiveToolbarText)
  9295. {
  9296. Result = false;
  9297. }
  9298. else
  9299. {
  9300. Label->Visible = SelectiveToolbarText;
  9301. }
  9302. }
  9303. else
  9304. {
  9305. // optimization
  9306. if (Item->DisplayMode == DisplayMode)
  9307. {
  9308. Result = false;
  9309. }
  9310. else
  9311. {
  9312. Item->DisplayMode = DisplayMode;
  9313. }
  9314. }
  9315. ++i;
  9316. }
  9317. if (Result)
  9318. {
  9319. UpdateNewSessionTab();
  9320. }
  9321. return Result;
  9322. }
  9323. //---------------------------------------------------------------------------
  9324. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9325. {
  9326. DebugFail();
  9327. }
  9328. //---------------------------------------------------------------------------
  9329. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9330. {
  9331. // Among other, a lock level is non zero, while directory is being loaded,
  9332. // even when it happens as a result of dirview navigation,
  9333. // i.e. when TNonVisualDataModule is NOT FBusy.
  9334. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9335. // Among other this prevents a panel auto update to occur while
  9336. // directory is changing.
  9337. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9338. }
  9339. //---------------------------------------------------------------------------
  9340. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9341. {
  9342. // While the window is disabled, we still seem to process menu shortcuts at least,
  9343. // so stop it at least here.
  9344. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9345. return
  9346. (Allowed == aaUpdate) ||
  9347. !NonVisualDataModule->Busy;
  9348. }
  9349. //---------------------------------------------------------------------------
  9350. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9351. {
  9352. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9353. }
  9354. //---------------------------------------------------------------------------
  9355. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9356. {
  9357. // This is somewhat redundant to LockWindow() call from
  9358. // TTerminalManager::TerminalReadDirectoryProgress.
  9359. // But disabling window is known not to block keyboard shorcuts
  9360. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9361. if (Busy > 0)
  9362. {
  9363. if (NonVisualDataModule->Busy)
  9364. {
  9365. State = false;
  9366. }
  9367. else
  9368. {
  9369. NonVisualDataModule->StartBusy();
  9370. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9371. LockWindow();
  9372. }
  9373. }
  9374. else if (Busy < 0)
  9375. {
  9376. UnlockWindow();
  9377. NonVisualDataModule->EndBusy();
  9378. }
  9379. else
  9380. {
  9381. State = NonVisualDataModule->Busy;
  9382. }
  9383. }
  9384. //---------------------------------------------------------------------------
  9385. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9386. {
  9387. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9388. // no context menu for "New session tab"
  9389. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9390. {
  9391. SessionsPageControl->ActivePageIndex = Index;
  9392. if (DebugAlwaysTrue(SessionTabSwitched()))
  9393. {
  9394. // copied from TControl.WMContextMenu
  9395. SendCancelMode(SessionsPageControl);
  9396. // explicit popup instead of using PopupMenu property
  9397. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9398. // while connecting yet not-connected session and hence
  9399. // allowing an access to commands over not-completelly connected session
  9400. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9401. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9402. PopupMenu->PopupComponent = SessionsPageControl;
  9403. PopupMenu->Popup(Point.x, Point.y);
  9404. }
  9405. }
  9406. Handled = true;
  9407. }
  9408. //---------------------------------------------------------------------------
  9409. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9410. {
  9411. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9412. }
  9413. //---------------------------------------------------------------------------
  9414. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9415. {
  9416. Configuration->Usage->Inc(L"PasswordChanges");
  9417. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9418. ChangePasswordData->DisableAuthentationsExceptPassword();
  9419. ChangePasswordData->ChangePassword = true;
  9420. std::unique_ptr<TTerminal> ChangePasswordSession(
  9421. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9422. ChangePasswordSession->AutoReadDirectory = false;
  9423. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9424. {
  9425. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9426. }
  9427. else
  9428. {
  9429. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9430. }
  9431. }
  9432. //---------------------------------------------------------------------------
  9433. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9434. {
  9435. // No nice way to assert SSH2
  9436. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9437. }
  9438. //---------------------------------------------------------------------------
  9439. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9440. {
  9441. TOperationVisualizer OperationVisualizer;
  9442. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9443. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9444. }
  9445. //---------------------------------------------------------------------------
  9446. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9447. {
  9448. TForm::ChangeScale(M, D);
  9449. int APixelsPerInch = GetControlPixelsPerInch(this);
  9450. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9451. try
  9452. {
  9453. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9454. }
  9455. __finally
  9456. {
  9457. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9458. }
  9459. }
  9460. //---------------------------------------------------------------------------
  9461. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9462. {
  9463. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9464. }
  9465. //---------------------------------------------------------------------
  9466. class TPublicControl : public TControl
  9467. {
  9468. friend class TCustomScpExplorerForm;
  9469. };
  9470. //---------------------------------------------------------------------------
  9471. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9472. {
  9473. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9474. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9475. }
  9476. //---------------------------------------------------------------------------
  9477. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9478. {
  9479. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9480. OpenBookmark(Side, Bookmark);
  9481. }
  9482. //---------------------------------------------------------------------------
  9483. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9484. {
  9485. DoBookmarkClick(osLocal, Sender);
  9486. }
  9487. //---------------------------------------------------------------------------
  9488. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9489. {
  9490. DoBookmarkClick(osRemote, Sender);
  9491. }
  9492. //---------------------------------------------------------------------------
  9493. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9494. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9495. {
  9496. // TODO
  9497. if (BookmarkList != NULL)
  9498. {
  9499. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9500. if (!WinConfiguration->UseLocationProfiles)
  9501. {
  9502. std::unique_ptr<TStringList> Directories(new TStringList());
  9503. Directories->CaseSensitive = (Side == osRemote);
  9504. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9505. {
  9506. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9507. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9508. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9509. {
  9510. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9511. Item->Caption = EscapeHotkey(Directory);
  9512. Item->ShortCut = Bookmark->ShortCut;
  9513. Item->OnClick = OnBookmarkClick;
  9514. Item->Tag = reinterpret_cast<int>(Bookmark);
  9515. Directories->Add(Directory);
  9516. Menu->Add(Item.release());
  9517. }
  9518. }
  9519. }
  9520. else
  9521. {
  9522. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9523. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9524. {
  9525. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9526. if (!Bookmark->Node.IsEmpty())
  9527. {
  9528. Folders->Add(Bookmark->Node);
  9529. }
  9530. }
  9531. for (int Index = 0; Index < Folders->Count; Index++)
  9532. {
  9533. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9534. Item->Caption = Folders->Strings[Index];
  9535. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9536. Folders->Objects[Index] = Item.get();
  9537. Menu->Add(Item.release());
  9538. }
  9539. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9540. {
  9541. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9542. TTBCustomItem * Parent;
  9543. if (!Bookmark->Node.IsEmpty())
  9544. {
  9545. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9546. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9547. }
  9548. else
  9549. {
  9550. Parent = Menu;
  9551. }
  9552. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9553. Item->Caption = Bookmark->Name;
  9554. Item->ShortCut = Bookmark->ShortCut;
  9555. Item->OnClick = OnBookmarkClick;
  9556. Item->Tag = reinterpret_cast<int>(Bookmark);
  9557. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9558. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9559. {
  9560. // No hint for location profiles that are actually mere "bookmarks"
  9561. }
  9562. else
  9563. {
  9564. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9565. UnicodeString LongHint;
  9566. if (!Bookmark->Local.IsEmpty())
  9567. {
  9568. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9569. }
  9570. if (!Bookmark->Remote.IsEmpty())
  9571. {
  9572. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9573. }
  9574. AddToList(Hint, LongHint, L"|");
  9575. Item->Hint = Hint;
  9576. }
  9577. Parent->Add(Item.release());
  9578. }
  9579. }
  9580. AddMenuSeparator(Menu);
  9581. }
  9582. }
  9583. //---------------------------------------------------------------------------
  9584. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9585. {
  9586. Menu->Clear();
  9587. std::unique_ptr<TTBCustomItem> Item;
  9588. Item.reset(new TTBXItem(Owner));
  9589. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9590. Item->Options = Item->Options << tboDefault;
  9591. Menu->Add(Item.release());
  9592. AddMenuSeparator(Menu);
  9593. if (Terminal != NULL)
  9594. {
  9595. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9596. }
  9597. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9598. Item.reset(new TTBXItem(Owner));
  9599. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9600. Menu->Add(Item.release());
  9601. }
  9602. //---------------------------------------------------------------------------
  9603. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9604. {
  9605. bool Result = true;
  9606. try
  9607. {
  9608. if (Side == osRemote)
  9609. {
  9610. Terminal->ExceptionOnFail = true;
  9611. }
  9612. try
  9613. {
  9614. DirView(Side)->Path = Path;
  9615. }
  9616. __finally
  9617. {
  9618. if (Side == osRemote)
  9619. {
  9620. Terminal->ExceptionOnFail = false;
  9621. }
  9622. }
  9623. }
  9624. catch (Exception & E)
  9625. {
  9626. if ((Side != osRemote) || Terminal->Active)
  9627. {
  9628. ShowExtendedExceptionEx(Terminal, &E);
  9629. Result = false;
  9630. }
  9631. else
  9632. {
  9633. throw;
  9634. }
  9635. }
  9636. return Result;
  9637. }
  9638. //---------------------------------------------------------------------------
  9639. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9640. {
  9641. // Make sure there some feedback even when loading tiny local folders
  9642. TInstantOperationVisualizer Visualizer;
  9643. DirView(Side)->ReloadDirectory();
  9644. }
  9645. //---------------------------------------------------------------------------
  9646. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9647. {
  9648. if (Index == SessionsPageControl->TabIndex)
  9649. {
  9650. CloseSession();
  9651. }
  9652. else
  9653. {
  9654. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9655. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9656. TTerminalManager * Manager = TTerminalManager::Instance();
  9657. if (!Terminal->Active)
  9658. {
  9659. Manager->FreeTerminal(Terminal);
  9660. }
  9661. else
  9662. {
  9663. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9664. if (CanCloseQueue(Queue))
  9665. {
  9666. Manager->FreeTerminal(Terminal);
  9667. }
  9668. }
  9669. }
  9670. }
  9671. //---------------------------------------------------------------------------
  9672. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9673. {
  9674. CloseSessionTab(Index);
  9675. }
  9676. //---------------------------------------------------------------------------
  9677. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9678. {
  9679. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9680. {
  9681. TInstantOperationVisualizer Visualizer;
  9682. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9683. // before we create a new data object
  9684. ClipboardClear();
  9685. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9686. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9687. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9688. // Intermediate variable to WORKAROUND a crash in compiler
  9689. TObjectList * ClipboardFakeMonitors =
  9690. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9691. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9692. if (FClipboardDragDropFilesEx.get() == NULL)
  9693. {
  9694. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9695. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9696. }
  9697. FClipboardDragDropFilesEx->FileList->Clear();
  9698. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9699. FClipboardDragDropFilesEx->CopyToClipboard();
  9700. FClipboardTerminal = FTerminal;
  9701. // Need full paths, as cwd can be different once files are pasted
  9702. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9703. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9704. {
  9705. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9706. }
  9707. }
  9708. }
  9709. //---------------------------------------------------------------------------
  9710. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9711. {
  9712. if (OpenClipboard(0))
  9713. {
  9714. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9715. CloseClipboard();
  9716. }
  9717. }
  9718. //---------------------------------------------------------------------------
  9719. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9720. {
  9721. return !FClipboardFakeDirectory.IsEmpty();
  9722. }
  9723. //---------------------------------------------------------------------------
  9724. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9725. {
  9726. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9727. FClipboardFakeDirectory = UnicodeString();
  9728. FClipboardTerminal = NULL;
  9729. // Also called in Idle()
  9730. FClipboardFakeMonitors.reset(NULL);
  9731. }
  9732. //---------------------------------------------------------------------------
  9733. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9734. {
  9735. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9736. {
  9737. ClipboardStop();
  9738. }
  9739. }
  9740. //---------------------------------------------------------------------------
  9741. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9742. {
  9743. TTerminalManager * Manager = TTerminalManager::Instance();
  9744. Manager->ActiveTerminal = FClipboardTerminal;
  9745. TTransferOperationParam Params;
  9746. Params.TargetDirectory = TargetDirectory;
  9747. Params.DragDrop = DragDrop;
  9748. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9749. }
  9750. //---------------------------------------------------------------------------
  9751. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9752. {
  9753. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9754. // (as it pumps a Windows message queue while being shutted down)
  9755. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9756. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9757. {
  9758. // Can fail as it can be e.g. locked by AV, so we retry that later
  9759. bool Removed = RemoveDir(ApiPath(FileName));
  9760. try
  9761. {
  9762. if (!NonVisualDataModule->Busy)
  9763. {
  9764. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9765. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9766. }
  9767. }
  9768. __finally
  9769. {
  9770. if (!Removed)
  9771. {
  9772. RemoveDir(ApiPath(FileName));
  9773. }
  9774. }
  9775. }
  9776. }
  9777. //---------------------------------------------------------------------------
  9778. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9779. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9780. {
  9781. TFileMasks::TParams MaskParams;
  9782. MaskParams.Size = Size;
  9783. MaskParams.Modification = Modification;
  9784. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9785. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9786. {
  9787. bool ImplicitMatch;
  9788. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9789. !ImplicitMatch)
  9790. {
  9791. Color = Iter->Color;
  9792. break;
  9793. }
  9794. }
  9795. }
  9796. //---------------------------------------------------------------------------
  9797. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9798. {
  9799. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9800. if (!DirView->IsEditing())
  9801. {
  9802. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9803. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9804. // while searching
  9805. if (Key == VK_BACK)
  9806. {
  9807. if (!FIncrementalSearch.IsEmpty())
  9808. {
  9809. if (FIncrementalSearch.Length() == 1)
  9810. {
  9811. ResetIncrementalSearch();
  9812. }
  9813. else
  9814. {
  9815. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9816. IncrementalSearch(NewText, false, false);
  9817. }
  9818. Key = 0;
  9819. }
  9820. }
  9821. }
  9822. }
  9823. //---------------------------------------------------------------------------
  9824. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9825. {
  9826. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9827. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9828. {
  9829. // Filter control sequences.
  9830. // When not searching yet, prefer use of the space for toggling file selection
  9831. // (so we cannot incrementally search for a string starting with a space).
  9832. if ((Key > VK_SPACE) ||
  9833. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9834. {
  9835. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9836. Key = 0;
  9837. }
  9838. }
  9839. }
  9840. //---------------------------------------------------------------------------
  9841. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9842. {
  9843. if (!FIncrementalSearch.IsEmpty())
  9844. {
  9845. FIncrementalSearch = L"";
  9846. DirView(osCurrent)->UpdateStatusBar();
  9847. }
  9848. }
  9849. //---------------------------------------------------------------------------
  9850. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9851. {
  9852. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9853. if (Item == NULL)
  9854. {
  9855. MessageBeep(MB_ICONHAND);
  9856. }
  9857. else
  9858. {
  9859. TCustomDirView * ADirView = DirView(osCurrent);
  9860. {
  9861. TAutoNestingCounter Guard(FIncrementalSearching);
  9862. ADirView->FocusItem(Item);
  9863. if (ADirView->NortonLike == nlOff)
  9864. {
  9865. if ((ADirView->Selected != Item) ||
  9866. (ADirView->SelCount > 1))
  9867. {
  9868. ADirView->ClearSelection();
  9869. ADirView->Selected = Item;
  9870. }
  9871. }
  9872. }
  9873. FIncrementalSearch = Text;
  9874. Item->MakeVisible(false);
  9875. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9876. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9877. ADirView->UpdateStatusBar();
  9878. }
  9879. }
  9880. //---------------------------------------------------------------------------
  9881. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9882. {
  9883. int Index = Item->Index;
  9884. if (!Reverse)
  9885. {
  9886. Index++;
  9887. if (Index >= Item->Owner->Count)
  9888. {
  9889. Index = 0;
  9890. }
  9891. }
  9892. else
  9893. {
  9894. Index--;
  9895. if (Index < 0)
  9896. {
  9897. Index = Item->Owner->Count - 1;
  9898. }
  9899. }
  9900. return Item->Owner->Item[Index];
  9901. }
  9902. //---------------------------------------------------------------------------
  9903. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9904. {
  9905. TCustomDirView * ADirView = DirView(osCurrent);
  9906. if (ADirView->Items->Count == 0)
  9907. {
  9908. return NULL;
  9909. }
  9910. else
  9911. {
  9912. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9913. TListItem * Item = CurrentItem;
  9914. if (SkipCurrent)
  9915. {
  9916. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9917. }
  9918. while (true)
  9919. {
  9920. bool Matches = false;
  9921. switch (WinConfiguration->PanelSearch)
  9922. {
  9923. case isNameStartOnly:
  9924. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9925. break;
  9926. case isName:
  9927. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9928. break;
  9929. case isAll:
  9930. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9931. int Index = 0;
  9932. while ((Index < ColCount) && !Matches)
  9933. {
  9934. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9935. if (Visible)
  9936. {
  9937. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9938. }
  9939. Index++;
  9940. }
  9941. break;
  9942. }
  9943. if (Matches)
  9944. {
  9945. return Item;
  9946. }
  9947. Item = GetNextFile(Item, Reverse);
  9948. if (Item == CurrentItem)
  9949. {
  9950. return NULL;
  9951. }
  9952. }
  9953. }
  9954. }
  9955. //---------------------------------------------------------------------------
  9956. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9957. {
  9958. ResetIncrementalSearch();
  9959. }
  9960. //---------------------------------------------------------------------------
  9961. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9962. {
  9963. if (Message.CharCode == VK_TAB)
  9964. {
  9965. if (!FIncrementalSearch.IsEmpty())
  9966. {
  9967. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  9968. bool Reverse = Shift.Contains(ssShift);
  9969. IncrementalSearch(FIncrementalSearch, true, Reverse);
  9970. Message.Result = 1;
  9971. return;
  9972. }
  9973. }
  9974. else if (Message.CharCode == VK_ESCAPE)
  9975. {
  9976. if (!FIncrementalSearch.IsEmpty())
  9977. {
  9978. ResetIncrementalSearch();
  9979. Message.Result = 1;
  9980. return;
  9981. }
  9982. }
  9983. TForm::Dispatch(&Message);
  9984. }
  9985. //---------------------------------------------------------------------------
  9986. void __fastcall TCustomScpExplorerForm::Deactivate()
  9987. {
  9988. TForm::Deactivate();
  9989. // E.g. when switching to an internal editor window
  9990. ResetIncrementalSearch();
  9991. }
  9992. //---------------------------------------------------------------------------
  9993. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  9994. {
  9995. // When switching to another application
  9996. ResetIncrementalSearch();
  9997. }
  9998. //---------------------------------------------------------------------------
  9999. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10000. {
  10001. ResetIncrementalSearch();
  10002. }
  10003. //---------------------------------------------------------------------------
  10004. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10005. {
  10006. if (FIncrementalSearching <= 0)
  10007. {
  10008. ResetIncrementalSearch();
  10009. }
  10010. }
  10011. //---------------------------------------------------------------------------
  10012. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10013. {
  10014. CountClicksForWindowPrint(this);
  10015. }
  10016. //---------------------------------------------------------------------------
  10017. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10018. {
  10019. CenterReconnectToolbar();
  10020. }
  10021. //---------------------------------------------------------------------------