CustomScpExplorer.cpp 347 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622
  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->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(false);
  4364. }
  4365. __finally
  4366. {
  4367. NonVisualDataModule->EndBusy();
  4368. }
  4369. }
  4370. }
  4371. //---------------------------------------------------------------------------
  4372. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4373. {
  4374. FTrayIcon->Hint = GetMainForm()->Caption;
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4378. {
  4379. UpdateTrayIcon();
  4380. }
  4381. //---------------------------------------------------------------------------
  4382. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4383. {
  4384. // workaround
  4385. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4386. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4387. // But WM_ACTIVATEAPP message can be received even when
  4388. // the main window is minimized to the tray and internal editor window is focused
  4389. // (after another application was previously active)
  4390. if (::IsIconic(Handle))
  4391. {
  4392. if (!IsAppIconic())
  4393. {
  4394. SetAppIconic(true);
  4395. }
  4396. }
  4397. ::ApplicationRestore();
  4398. Application->BringToFront();
  4399. }
  4400. //---------------------------------------------------------------------------
  4401. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4402. {
  4403. RestoreApp();
  4404. }
  4405. //---------------------------------------------------------------------------
  4406. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4407. {
  4408. if (OpenInNewWindow())
  4409. {
  4410. // todo: Pass FromSite
  4411. ExecuteNewInstance(SessionUrl);
  4412. }
  4413. else
  4414. {
  4415. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4416. }
  4417. }
  4418. //---------------------------------------------------------------------------
  4419. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4420. {
  4421. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4422. // current working directories become defaults here, what is not right
  4423. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4424. StoredSessions->NewSession(SessionName, SessionData.get());
  4425. // modified only, explicit
  4426. StoredSessions->Save(false, true);
  4427. // encode session name because of slashes in hierarchical sessions
  4428. return EncodeUrlString(SessionName);
  4429. }
  4430. //---------------------------------------------------------------------------
  4431. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4432. {
  4433. if (OpenInNewWindow())
  4434. {
  4435. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4436. }
  4437. else
  4438. {
  4439. // current working directories become defaults here, what is not right
  4440. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4441. TTerminalManager * Manager = TTerminalManager::Instance();
  4442. 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::OpenFolderOrWorkspace(const UnicodeString & Name)
  4526. {
  4527. if (OpenInNewWindow())
  4528. {
  4529. ExecuteNewInstance(Name);
  4530. }
  4531. else
  4532. {
  4533. TTerminalManager * Manager = TTerminalManager::Instance();
  4534. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4535. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4536. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4537. }
  4538. }
  4539. //---------------------------------------------------------------------------
  4540. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4541. bool &CanClose)
  4542. {
  4543. if (Terminal != NULL)
  4544. {
  4545. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4546. {
  4547. unsigned int Result;
  4548. TMessageParams Params(mpNeverAskAgainCheck);
  4549. UnicodeString Message;
  4550. int Answers = qaOK | qaCancel;
  4551. if (TTerminalManager::Instance()->Count > 1)
  4552. {
  4553. if (!WinConfiguration->AutoSaveWorkspace)
  4554. {
  4555. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4556. Answers = qaYes | qaNo | qaCancel;
  4557. }
  4558. else
  4559. {
  4560. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4561. }
  4562. }
  4563. else
  4564. {
  4565. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4566. }
  4567. if (WinConfiguration->AutoSaveWorkspace)
  4568. {
  4569. Message =
  4570. FORMAT("%s\n\n%s", (Message,
  4571. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4572. }
  4573. SetFocus();
  4574. Result = MessageDialog(Message, qtConfirmation,
  4575. Answers, HELP_NONE, &Params);
  4576. if (Result == qaNeverAskAgain)
  4577. {
  4578. WinConfiguration->ConfirmClosingSession = false;
  4579. }
  4580. if (Result == qaNo)
  4581. {
  4582. CanClose = SaveWorkspace(true);
  4583. // note that the workspace will be saved redundatly again from FormClose
  4584. }
  4585. else
  4586. {
  4587. CanClose =
  4588. (Result == qaOK) ||
  4589. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4590. (Result == qaNeverAskAgain);
  4591. }
  4592. }
  4593. if (CanClose)
  4594. {
  4595. CanClose = CanCloseQueue();
  4596. }
  4597. }
  4598. if (CanClose)
  4599. {
  4600. CanClose =
  4601. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4602. FEditorManager->CloseExternalFilesWithoutProcess();
  4603. if (CanClose)
  4604. {
  4605. while (CanClose && (FLocalEditors->Count > 0))
  4606. {
  4607. int PrevCount = FLocalEditors->Count;
  4608. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4609. CanClose = (FLocalEditors->Count < PrevCount);
  4610. }
  4611. if (CanClose)
  4612. {
  4613. CanClose = FEditorManager->Empty(true);
  4614. if (!CanClose)
  4615. {
  4616. SetFocus();
  4617. CanClose =
  4618. (MessageDialog(
  4619. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4620. }
  4621. }
  4622. }
  4623. }
  4624. }
  4625. //---------------------------------------------------------------------------
  4626. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4627. {
  4628. TForm * Form = dynamic_cast<TForm *>(Sender);
  4629. DebugAssert(Form != NULL);
  4630. Form->Close();
  4631. }
  4632. //---------------------------------------------------------------------------
  4633. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4634. {
  4635. TForm * Form = dynamic_cast<TForm *>(Sender);
  4636. delete Form;
  4637. }
  4638. //---------------------------------------------------------------------------
  4639. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4640. {
  4641. while (FLocalEditors->Count > 0)
  4642. {
  4643. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4644. }
  4645. }
  4646. //---------------------------------------------------------------------------
  4647. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4648. TObject *Sender)
  4649. {
  4650. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4651. try
  4652. {
  4653. SetProperties(osRemote, FileList);
  4654. }
  4655. __finally
  4656. {
  4657. delete FileList;
  4658. }
  4659. }
  4660. //---------------------------------------------------------------------------
  4661. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4662. {
  4663. if (value)
  4664. {
  4665. if (Component == fcCustomCommandsBand)
  4666. {
  4667. UpdateCustomCommandsToolbar();
  4668. }
  4669. }
  4670. }
  4671. //---------------------------------------------------------------------------
  4672. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4673. {
  4674. TControl * Control = GetComponent(Component);
  4675. DebugAssert(Control);
  4676. bool Changed = (Control->Visible != value);
  4677. if (Changed)
  4678. {
  4679. ComponentShowing(Component, value);
  4680. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4681. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4682. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4683. if (value)
  4684. {
  4685. int RemainingHeight = Control->Parent->ClientHeight;
  4686. int RemainingWidth = Control->Parent->ClientWidth;
  4687. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4688. {
  4689. TControl * ChildControl = Control->Parent->Controls[i];
  4690. if (ChildControl->Visible)
  4691. {
  4692. switch (ChildControl->Align)
  4693. {
  4694. case alTop:
  4695. case alBottom:
  4696. RemainingHeight -= ChildControl->Height;
  4697. break;
  4698. case alLeft:
  4699. case alRight:
  4700. RemainingWidth -= ChildControl->Width;
  4701. break;
  4702. }
  4703. }
  4704. }
  4705. int Reserve = ScaleByTextHeight(this, 32);
  4706. // queue in explorer, trees in commander
  4707. if (Control->Height > RemainingHeight - Reserve)
  4708. {
  4709. Control->Height = RemainingHeight / 2;
  4710. }
  4711. if (Control->Width > RemainingWidth - Reserve)
  4712. {
  4713. Control->Width = RemainingWidth / 2;
  4714. }
  4715. }
  4716. Control->Visible = value;
  4717. if (WasFocused && Visible)
  4718. {
  4719. DirView(osCurrent)->SetFocus();
  4720. }
  4721. FixControlsPlacement();
  4722. }
  4723. }
  4724. //---------------------------------------------------------------------------
  4725. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4726. {
  4727. TControl * Control = GetComponent(Component);
  4728. if (Control == NULL)
  4729. {
  4730. return false;
  4731. }
  4732. else
  4733. {
  4734. return Control->Visible;
  4735. }
  4736. }
  4737. //---------------------------------------------------------------------------
  4738. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4739. {
  4740. bool Result;
  4741. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4742. {
  4743. Result = !IsUWP();
  4744. }
  4745. else
  4746. {
  4747. Result = true;
  4748. }
  4749. return Result;
  4750. }
  4751. //---------------------------------------------------------------------------
  4752. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4753. {
  4754. if (DirView(osOther)->ItemFocused != NULL)
  4755. {
  4756. DirView(osOther)->ItemFocused->MakeVisible(false);
  4757. }
  4758. QueueSplitter->Visible = QueuePanel->Visible;
  4759. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4760. }
  4761. //---------------------------------------------------------------------------
  4762. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4763. {
  4764. switch (Component) {
  4765. case fcStatusBar: return RemoteStatusBar;
  4766. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4767. case fcQueueView: return QueuePanel;
  4768. case fcQueueToolbar: return QueueDock;
  4769. case fcRemoteTree: return RemoteDrivePanel;
  4770. case fcSessionsTabs: return SessionsPageControl;
  4771. default: return NULL;
  4772. }
  4773. }
  4774. //---------------------------------------------------------------------------
  4775. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4776. TObject *Sender, TListColumn *Column, TPoint &Point)
  4777. {
  4778. DebugAssert(NonVisualDataModule && Column && Sender);
  4779. NonVisualDataModule->ListColumn = Column;
  4780. TPopupMenu * DirViewColumnMenu;
  4781. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4782. {
  4783. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4784. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4785. (Column->Index == uvName);
  4786. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4787. (Column->Index == uvSize);
  4788. }
  4789. else
  4790. {
  4791. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4792. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4793. (Column->Index == dvName);
  4794. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4795. (Column->Index == dvSize);
  4796. }
  4797. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4798. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4799. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4800. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4801. }
  4802. //---------------------------------------------------------------------------
  4803. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4804. TObject *Sender, TListItem *Item, bool &AllowExec)
  4805. {
  4806. DoDirViewExecFile(Sender, Item, AllowExec);
  4807. }
  4808. //---------------------------------------------------------------------------
  4809. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4810. TListItem * Item, bool & AllowExec)
  4811. {
  4812. DebugAssert(Sender && Item && Configuration);
  4813. DebugAssert(AllowExec);
  4814. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4815. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4816. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4817. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4818. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4819. // on files only (not directories)
  4820. // and only when symlinks are resolved (apply to remote panel only)
  4821. if (!ADirView->ItemIsDirectory(Item) &&
  4822. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4823. {
  4824. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4825. !FForceExecution &&
  4826. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4827. {
  4828. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4829. {
  4830. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4831. UnlockWindow();
  4832. try
  4833. {
  4834. ExecuteFileOperation(
  4835. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4836. }
  4837. __finally
  4838. {
  4839. LockWindow();
  4840. }
  4841. AllowExec = false;
  4842. }
  4843. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4844. {
  4845. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4846. {
  4847. ExecuteFile(Side, efDefaultEditor);
  4848. AllowExec = false;
  4849. }
  4850. }
  4851. else
  4852. {
  4853. DebugFail();
  4854. }
  4855. }
  4856. // if we have not done anything special, fall back to default behavior
  4857. if (AllowExec)
  4858. {
  4859. if (Remote && !WinConfiguration->DisableOpenEdit)
  4860. {
  4861. ExecuteFile(osRemote, efShell);
  4862. AllowExec = false;
  4863. }
  4864. }
  4865. }
  4866. }
  4867. //---------------------------------------------------------------------------
  4868. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4869. {
  4870. return ((Side == osRemote) || (Side == osCurrent));
  4871. }
  4872. //---------------------------------------------------------------------------
  4873. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4874. {
  4875. DebugFail();
  4876. }
  4877. //---------------------------------------------------------------------------
  4878. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4879. {
  4880. DebugFail();
  4881. }
  4882. //---------------------------------------------------------------------------
  4883. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4884. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4885. {
  4886. TSynchronizeParamType Params;
  4887. Params.LocalDirectory = LocalDirectory;
  4888. Params.RemoteDirectory = RemoteDirectory;
  4889. int UnusedParams =
  4890. (GUIConfiguration->SynchronizeParams &
  4891. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4892. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4893. Params.Options = GUIConfiguration->SynchronizeOptions;
  4894. bool SaveSettings = false;
  4895. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4896. &DoSynchronizeTooManyDirectories);
  4897. DebugAssert(FSynchronizeController == NULL);
  4898. FSynchronizeController = &Controller;
  4899. bool Result;
  4900. try
  4901. {
  4902. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4903. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4904. int Options =
  4905. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4906. DebugAssert(FOnFeedSynchronizeError == NULL);
  4907. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4908. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4909. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4910. if (Result)
  4911. {
  4912. if (SaveSettings)
  4913. {
  4914. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4915. GUIConfiguration->SynchronizeOptions = Params.Options;
  4916. }
  4917. else
  4918. {
  4919. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4920. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4921. FLAGSET(Params.Options, soSynchronize))
  4922. {
  4923. GUIConfiguration->SynchronizeOptions =
  4924. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4925. soSynchronize;
  4926. }
  4927. }
  4928. LocalDirectory = Params.LocalDirectory;
  4929. RemoteDirectory = Params.RemoteDirectory;
  4930. }
  4931. }
  4932. __finally
  4933. {
  4934. FSynchronizeController = NULL;
  4935. DebugAssert(FOnFeedSynchronizeError == NULL);
  4936. FOnFeedSynchronizeError = NULL;
  4937. }
  4938. return Result;
  4939. }
  4940. //---------------------------------------------------------------------------
  4941. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4942. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4943. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4944. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4945. TSynchronizeOptions * Options, bool Full)
  4946. {
  4947. // Keep-up-to-date mode
  4948. if (Terminal->Status == ssOpened)
  4949. {
  4950. try
  4951. {
  4952. int PParams = Params.Params;
  4953. if (!Full)
  4954. {
  4955. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4956. if (FLAGSET(Params.Options, soRecurse))
  4957. {
  4958. PParams |= TTerminal::spSubDirs;
  4959. }
  4960. }
  4961. else
  4962. {
  4963. // if keepuptodate is non-recursive,
  4964. // full sync before has to be non-recursive as well
  4965. if (FLAGCLEAR(Params.Options, soRecurse))
  4966. {
  4967. PParams |= TTerminal::spNoRecurse;
  4968. }
  4969. }
  4970. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4971. PParams, Checklist, Options);
  4972. }
  4973. catch (Exception & E)
  4974. {
  4975. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4976. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4977. {
  4978. // noop, already logged in MoreMessageDialog
  4979. }
  4980. else
  4981. {
  4982. ShowExtendedExceptionEx(Terminal, &E);
  4983. throw;
  4984. }
  4985. }
  4986. }
  4987. }
  4988. //---------------------------------------------------------------------------
  4989. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4990. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4991. const UnicodeString ErrorStr)
  4992. {
  4993. if (!Directory.IsEmpty())
  4994. {
  4995. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4996. }
  4997. else
  4998. {
  4999. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5000. }
  5001. }
  5002. //---------------------------------------------------------------------------
  5003. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5004. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5005. {
  5006. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5007. {
  5008. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5009. }
  5010. else
  5011. {
  5012. TMessageParams Params(mpNeverAskAgainCheck);
  5013. unsigned int Result = MessageDialog(
  5014. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5015. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5016. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5017. {
  5018. MaxDirectories *= 2;
  5019. if (Result == qaNeverAskAgain)
  5020. {
  5021. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5022. }
  5023. }
  5024. else
  5025. {
  5026. Abort();
  5027. }
  5028. }
  5029. }
  5030. //---------------------------------------------------------------------------
  5031. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5032. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5033. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5034. TSynchronizeOptions * Options)
  5035. {
  5036. // Keep-up-to-date mode
  5037. DebugAssert(!FAutoOperation);
  5038. void * BatchStorage;
  5039. BatchStart(BatchStorage);
  5040. FAutoOperation = true;
  5041. bool AnyOperation = false;
  5042. TDateTime StartTime = Now();
  5043. TSynchronizeChecklist * AChecklist = NULL;
  5044. try
  5045. {
  5046. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5047. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5048. {
  5049. FSynchronizeProgressForm->Start();
  5050. }
  5051. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5052. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5053. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5054. Options);
  5055. SAFE_DESTROY(FSynchronizeProgressForm);
  5056. AnyOperation = (AChecklist->CheckedCount > 0);
  5057. if (AnyOperation)
  5058. {
  5059. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5060. CreateProgressForm(&SynchronizeProgress);
  5061. try
  5062. {
  5063. Terminal->SynchronizeApply(
  5064. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5065. SynchronizeProcessedItem, NULL, NULL, NULL);
  5066. }
  5067. __finally
  5068. {
  5069. DestroyProgressForm();
  5070. }
  5071. }
  5072. }
  5073. __finally
  5074. {
  5075. if (Checklist == NULL)
  5076. {
  5077. delete AChecklist;
  5078. }
  5079. else
  5080. {
  5081. *Checklist = AChecklist;
  5082. }
  5083. FAutoOperation = false;
  5084. SAFE_DESTROY(FSynchronizeProgressForm);
  5085. BatchEnd(BatchStorage);
  5086. ReloadLocalDirectory();
  5087. if (AnyOperation)
  5088. {
  5089. OperationComplete(StartTime);
  5090. }
  5091. }
  5092. }
  5093. //---------------------------------------------------------------------------
  5094. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5095. {
  5096. // can be called from command line
  5097. return Visible &&
  5098. ((DirView(osOther)->SelCount > 0) ||
  5099. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5100. }
  5101. //---------------------------------------------------------------------------
  5102. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5103. {
  5104. LogSynchronizeEvent(Terminal, Message);
  5105. }
  5106. //---------------------------------------------------------------------------
  5107. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5108. int Params, TSynchronizeOptions & Options)
  5109. {
  5110. DebugAssert(!IsLocalBrowserMode());
  5111. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5112. {
  5113. Options.Filter = new TStringList();
  5114. Options.Filter->CaseSensitive = false;
  5115. Options.Filter->Duplicates = Types::dupAccept;
  5116. if (DirView(osRemote)->SelCount > 0)
  5117. {
  5118. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5119. }
  5120. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5121. {
  5122. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5123. }
  5124. Options.Filter->Sort();
  5125. }
  5126. }
  5127. //---------------------------------------------------------------------------
  5128. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5129. {
  5130. TCopyParamType Defaults;
  5131. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5132. ConfigStorage->AccessMode = smRead;
  5133. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5134. {
  5135. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5136. }
  5137. ConfigStorage.reset(NULL);
  5138. std::unique_ptr<TStringList> Options(new TStringList());
  5139. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5140. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5141. UnicodeString Result;
  5142. if (Options->Count > 0)
  5143. {
  5144. Result =
  5145. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5146. }
  5147. return Result;
  5148. }
  5149. //---------------------------------------------------------------------------
  5150. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5151. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5152. {
  5153. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5154. UnicodeString AdditionalParams =
  5155. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5156. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5157. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5158. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5159. SerializeCopyParamForCommandLine(CopyParams)));
  5160. ExecuteNewInstance(SessionName, AdditionalParams);
  5161. }
  5162. //---------------------------------------------------------------------------
  5163. struct TSynchronizeParams
  5164. {
  5165. UnicodeString LocalDirectory;
  5166. UnicodeString RemoteDirectory;
  5167. TSynchronizeMode Mode;
  5168. TSynchronizeChecklist * Checklist;
  5169. int Params;
  5170. TCopyParamType * CopyParam;
  5171. TDateTime CollectElapsed;
  5172. TDateTime * StartTime;
  5173. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5174. };
  5175. //---------------------------------------------------------------------------
  5176. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5177. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5178. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5179. {
  5180. DebugAssert(!FAutoOperation);
  5181. void * BatchStorage;
  5182. BatchStart(BatchStorage);
  5183. FAutoOperation = true;
  5184. TFileOperationStatistics Statistics;
  5185. TDateTime Start = Now();
  5186. try
  5187. {
  5188. Params.OnProcessedItem = OnProcessedItem;
  5189. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5190. CreateProgressForm(&SynchronizeProgress);
  5191. Terminal->SynchronizeApply(
  5192. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5193. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5194. &Statistics);
  5195. }
  5196. __finally
  5197. {
  5198. Params.OnProcessedItem = NULL;
  5199. FAutoOperation = false;
  5200. DestroyProgressForm();
  5201. BatchEnd(BatchStorage);
  5202. ReloadLocalDirectory();
  5203. }
  5204. if (WinConfiguration->SynchronizeSummary)
  5205. {
  5206. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5207. // The statistics should be 0 anyway in this case
  5208. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5209. {
  5210. Message += L"\n";
  5211. if (Statistics.FilesUploaded > 0)
  5212. {
  5213. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5214. }
  5215. if (Statistics.FilesDownloaded > 0)
  5216. {
  5217. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5218. }
  5219. if (Statistics.FilesDeletedLocal > 0)
  5220. {
  5221. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5222. }
  5223. if (Statistics.FilesDeletedRemote > 0)
  5224. {
  5225. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5226. }
  5227. }
  5228. TDateTime Elapsed = (Now() - Start);
  5229. Message +=
  5230. L"\n" +
  5231. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5232. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5233. TMessageParams Params(mpNeverAskAgainCheck);
  5234. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5235. if (Result == qaNeverAskAgain)
  5236. {
  5237. WinConfiguration->SynchronizeSummary = false;
  5238. }
  5239. }
  5240. }
  5241. //---------------------------------------------------------------------------
  5242. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5243. {
  5244. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5245. {
  5246. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5247. }
  5248. // Not set in keep-up-to-date mode - Synchronize() method
  5249. if (Token != NULL)
  5250. {
  5251. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5252. if (Params.OnProcessedItem != NULL)
  5253. {
  5254. Params.OnProcessedItem(NULL, ChecklistItem);
  5255. }
  5256. }
  5257. }
  5258. //---------------------------------------------------------------------------
  5259. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5260. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5261. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5262. {
  5263. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5264. *Params.StartTime = Now();
  5265. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5266. }
  5267. //---------------------------------------------------------------------------
  5268. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5269. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5270. {
  5271. // terminal can be already closed (e.g. dropped connection)
  5272. if (Terminal != NULL)
  5273. {
  5274. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5275. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5276. }
  5277. }
  5278. //---------------------------------------------------------------------------
  5279. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5280. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5281. {
  5282. DebugAssert(!IsLocalBrowserMode());
  5283. TAutoBatch AutoBatch(this);
  5284. TAutoFlag AutoOperationFlag(FAutoOperation);
  5285. if (Side == osRemote)
  5286. {
  5287. std::unique_ptr<TStrings> FileList(new TStringList());
  5288. FileList->AddObject(FileName, RemoteFile);
  5289. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5290. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5291. RemoteDirView->SaveSelection();
  5292. RemoteDirView->SaveSelectedNames();
  5293. try
  5294. {
  5295. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5296. }
  5297. catch(...)
  5298. {
  5299. RemoteDirView->DiscardSavedSelection();
  5300. throw;
  5301. }
  5302. RemoteDirView->RestoreSelection();
  5303. }
  5304. else if (DebugAlwaysTrue(Side == osLocal))
  5305. {
  5306. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5307. {
  5308. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5309. }
  5310. UnicodeString Directory = ExtractFileDir(FileName);
  5311. ReloadLocalDirectory(Directory);
  5312. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5313. if (!SamePaths(Directory, NewDirectory))
  5314. {
  5315. ReloadLocalDirectory(NewDirectory);
  5316. }
  5317. }
  5318. }
  5319. //---------------------------------------------------------------------------
  5320. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5321. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5322. const TCopyParamType * CopyParams)
  5323. {
  5324. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5325. UnicodeString AdditionalParams =
  5326. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5327. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5328. LocalDirectory, RemoteDirectory, Mode, Params,
  5329. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5330. SerializeCopyParamForCommandLine(CopyParams)));
  5331. ExecuteNewInstance(SessionName, AdditionalParams);
  5332. }
  5333. //---------------------------------------------------------------------------
  5334. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5335. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5336. TSynchronizeMode & Mode, int Params, bool & SaveMode, bool UseDefaults)
  5337. {
  5338. int Result;
  5339. bool SaveSettings = false;
  5340. int Options =
  5341. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5342. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5343. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5344. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5345. bool Continue =
  5346. UseDefaults ||
  5347. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5348. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow);
  5349. if (Continue)
  5350. {
  5351. Configuration->Usage->Inc(L"Synchronizations");
  5352. UpdateCopyParamCounters(CopyParam);
  5353. TSynchronizeOptions SynchronizeOptions;
  5354. GetSynchronizeOptions(Params, SynchronizeOptions);
  5355. if (SaveSettings)
  5356. {
  5357. GUIConfiguration->SynchronizeParams = Params;
  5358. }
  5359. else
  5360. {
  5361. SaveMode = false;
  5362. }
  5363. TDateTime StartTime = Now();
  5364. TSynchronizeChecklist * Checklist = NULL;
  5365. try
  5366. {
  5367. DebugAssert(!FAutoOperation);
  5368. FAutoOperation = true;
  5369. try
  5370. {
  5371. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5372. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5373. int Files = -1;
  5374. if (DataList->Count >= 1)
  5375. {
  5376. Files = StrToIntDef(DataList->Strings[0], Files);
  5377. }
  5378. else
  5379. {
  5380. DataList->Add(UnicodeString());
  5381. }
  5382. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5383. FSynchronizeProgressForm->Start();
  5384. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5385. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5386. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5387. &SynchronizeOptions);
  5388. if (Terminal->SessionData->CacheDirectories)
  5389. {
  5390. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5391. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5392. }
  5393. }
  5394. __finally
  5395. {
  5396. FAutoOperation = false;
  5397. SAFE_DESTROY(FSynchronizeProgressForm);
  5398. }
  5399. TSynchronizeParams SynchronizeParams;
  5400. SynchronizeParams.LocalDirectory = LocalDirectory;
  5401. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5402. SynchronizeParams.Mode = Mode;
  5403. SynchronizeParams.CopyParam = &CopyParam;
  5404. SynchronizeParams.Params = Params;
  5405. SynchronizeParams.Checklist = Checklist;
  5406. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5407. SynchronizeParams.StartTime = &StartTime;
  5408. SynchronizeParams.OnProcessedItem = NULL;
  5409. Result = Checklist->Count;
  5410. if (Checklist->Count > 0)
  5411. {
  5412. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5413. {
  5414. if (!DoSynchronizeChecklistDialog(
  5415. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5416. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5417. {
  5418. Result = -1;
  5419. }
  5420. }
  5421. else
  5422. {
  5423. FullSynchronize(SynchronizeParams, NULL, NULL);
  5424. }
  5425. }
  5426. else
  5427. {
  5428. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5429. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5430. }
  5431. }
  5432. __finally
  5433. {
  5434. delete Checklist;
  5435. }
  5436. OperationComplete(StartTime);
  5437. }
  5438. else
  5439. {
  5440. Result = -1;
  5441. }
  5442. return Result;
  5443. }
  5444. //---------------------------------------------------------------------------
  5445. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5446. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5447. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5448. {
  5449. if (Collect)
  5450. {
  5451. DebugAssert(FSynchronizeProgressForm != NULL);
  5452. if (!FSynchronizeProgressForm->Started)
  5453. {
  5454. FSynchronizeProgressForm->Start();
  5455. }
  5456. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5457. SetTaskbarListProgressValue(CompareProgress);
  5458. }
  5459. else
  5460. {
  5461. DebugAssert(FSynchronizeProgressForm == NULL);
  5462. DebugAssert(FProgressForm != NULL);
  5463. }
  5464. }
  5465. //---------------------------------------------------------------------------
  5466. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5467. {
  5468. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5469. TRemoteFile * File = NULL;
  5470. Terminal->ReadFile(FullFileName, File);
  5471. if (File != NULL)
  5472. {
  5473. std::unique_ptr<TRemoteFile> FileOwner(File);
  5474. TAutoFlag Flag(FStandaloneEditing);
  5475. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5476. Application->ShowMainForm = false;
  5477. Application->Run();
  5478. }
  5479. }
  5480. //---------------------------------------------------------------------------
  5481. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5482. {
  5483. DebugFail();
  5484. }
  5485. //---------------------------------------------------------------------------
  5486. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5487. {
  5488. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5489. SessionData->Assign(Terminal->SessionData);
  5490. UpdateSessionData(SessionData.get());
  5491. Terminal->UpdateSessionCredentials(SessionData.get());
  5492. if (!Terminal->SessionData->HasSessionName())
  5493. {
  5494. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5495. // once IsWorkspace is cleared
  5496. SessionData->Name = UnicodeString();
  5497. SessionData->IsWorkspace = false;
  5498. }
  5499. return SessionData.release();
  5500. }
  5501. //---------------------------------------------------------------------------
  5502. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5503. {
  5504. TSessionData * SessionData = CloneCurrentSessionData();
  5505. try
  5506. {
  5507. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5508. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5509. }
  5510. __finally
  5511. {
  5512. delete SessionData;
  5513. }
  5514. }
  5515. //---------------------------------------------------------------------------
  5516. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5517. {
  5518. TTerminalManager * Manager = TTerminalManager::Instance();
  5519. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5520. if (DebugAlwaysTrue(Terminal != NULL))
  5521. {
  5522. // Update (Managed)Terminal->StateData
  5523. UpdateTerminal(Terminal);
  5524. }
  5525. Manager->SaveWorkspace(DataList.get());
  5526. return DataList.release();
  5527. }
  5528. //---------------------------------------------------------------------------
  5529. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5530. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5531. {
  5532. WinConfiguration->LastWorkspace = Name;
  5533. if (!SavePasswords)
  5534. {
  5535. for (int Index = 0; Index < DataList->Count; Index++)
  5536. {
  5537. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5538. if (SessionData->Link.IsEmpty())
  5539. {
  5540. SessionData->ClearSessionPasswords();
  5541. }
  5542. }
  5543. }
  5544. StoredSessions->NewWorkspace(Name, DataList);
  5545. // modified only
  5546. StoredSessions->Save(false, Explicit);
  5547. }
  5548. //---------------------------------------------------------------------------
  5549. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5550. {
  5551. return
  5552. DefaultStr(WinConfiguration->LastWorkspace,
  5553. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5554. }
  5555. //---------------------------------------------------------------------------
  5556. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5557. {
  5558. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5559. bool AnyNonStoredSessionWithPassword = false;
  5560. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5561. bool AllNonStoredSessionsAnonymous = true;
  5562. TTerminalManager * Manager = TTerminalManager::Instance();
  5563. for (int Index = 0; Index < DataList->Count; Index++)
  5564. {
  5565. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5566. if (SessionData->Link.IsEmpty())
  5567. {
  5568. if (SessionData->HasAnySessionPassword())
  5569. {
  5570. AnyNonStoredSessionWithPassword = true;
  5571. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5572. {
  5573. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5574. }
  5575. }
  5576. if (!SameText(SessionData->UserName, AnonymousUserName))
  5577. {
  5578. AllNonStoredSessionsAnonymous = false;
  5579. }
  5580. }
  5581. }
  5582. bool SavePasswords;
  5583. bool * PSavePasswords;
  5584. bool NotRecommendedSavingPasswords =
  5585. !CustomWinConfiguration->UseMasterPassword &&
  5586. !AllNonStoredSessionsAnonymous;
  5587. if (Configuration->DisablePasswordStoring ||
  5588. !AnyNonStoredSessionWithPassword)
  5589. {
  5590. PSavePasswords = NULL;
  5591. SavePasswords = false;
  5592. }
  5593. else
  5594. {
  5595. PSavePasswords = &SavePasswords;
  5596. SavePasswords =
  5597. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5598. !NotRecommendedSavingPasswords;
  5599. }
  5600. UnicodeString Name = WorkspaceName();
  5601. bool CreateShortcut = false;
  5602. bool Result =
  5603. DoSaveWorkspaceDialog(
  5604. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5605. EnableAutoSave);
  5606. if (Result)
  5607. {
  5608. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5609. if (CreateShortcut)
  5610. {
  5611. TOperationVisualizer Visualizer;
  5612. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5613. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5614. }
  5615. if (EnableAutoSave)
  5616. {
  5617. WinConfiguration->AutoSaveWorkspace = true;
  5618. WinConfiguration->AutoWorkspace = Name;
  5619. if (PSavePasswords != NULL)
  5620. {
  5621. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5622. }
  5623. }
  5624. }
  5625. return Result;
  5626. }
  5627. //---------------------------------------------------------------------------
  5628. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5629. {
  5630. DebugAssert(!IsLocalBrowserMode());
  5631. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5632. if (WinConfiguration->PreservePanelState)
  5633. {
  5634. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5635. }
  5636. UpdateSessionData(Terminal->StateData);
  5637. }
  5638. //---------------------------------------------------------------------------
  5639. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5640. {
  5641. // Keep in sync with TSessionData::CopyStateData
  5642. DebugAssert(!IsLocalBrowserMode());
  5643. DebugAssert(Data != NULL);
  5644. // This is inconsistent with how color (for example) is handled.
  5645. Data->Name = Terminal->SessionData->Name;
  5646. // cannot use RemoteDirView->Path, because it is empty if connection
  5647. // was already closed
  5648. // also only peek, we may not be connected at all atm
  5649. // so make sure we do not try retrieving current directory from the server
  5650. // (particularly with FTP)
  5651. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5652. if (!ACurrentDirectory.IsEmpty())
  5653. {
  5654. Data->RemoteDirectory = ACurrentDirectory;
  5655. }
  5656. Data->Color = SessionColor;
  5657. }
  5658. //---------------------------------------------------------------------------
  5659. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5660. {
  5661. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5662. DebugAssert(Toolbar != NULL);
  5663. for (int i = 0; i < Toolbar->Items->Count; i++)
  5664. {
  5665. TTBXCustomDropDownItem * DropDownItem;
  5666. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5667. if (DropDownItem != NULL)
  5668. {
  5669. ToolbarItemResize(DropDownItem,
  5670. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5671. Toolbar->NonClientWidth);
  5672. break;
  5673. }
  5674. }
  5675. }
  5676. //---------------------------------------------------------------------------
  5677. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5678. {
  5679. Item->EditWidth = Width;
  5680. }
  5681. //---------------------------------------------------------------------------
  5682. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5683. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5684. {
  5685. for (int i = 0; i < Toolbar->Items->Count; i++)
  5686. {
  5687. TTBXCustomDropDownItem * DropDownItem;
  5688. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5689. if (DropDownItem != NULL)
  5690. {
  5691. ASize.x -= DropDownItem->EditWidth;
  5692. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5693. }
  5694. }
  5695. }
  5696. //---------------------------------------------------------------------------
  5697. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5698. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5699. {
  5700. if (WinConfiguration->DDWarnLackOfTempSpace)
  5701. {
  5702. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5703. if (!ADrive.IsEmpty())
  5704. {
  5705. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5706. DebugAssert(RequiredSpace >= 0);
  5707. __int64 RequiredWithReserve;
  5708. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5709. if (FreeSpace < RequiredWithReserve)
  5710. {
  5711. unsigned int Result;
  5712. TMessageParams Params(mpNeverAskAgainCheck);
  5713. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5714. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5715. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5716. if (Result == qaNeverAskAgain)
  5717. {
  5718. WinConfiguration->DDWarnLackOfTempSpace = false;
  5719. }
  5720. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5721. }
  5722. }
  5723. }
  5724. }
  5725. //---------------------------------------------------------------------------
  5726. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5727. {
  5728. DoOpenDirectoryDialog(odAddBookmark, Side);
  5729. }
  5730. //---------------------------------------------------------------------------
  5731. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5732. {
  5733. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5734. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5735. try
  5736. {
  5737. TCustomDirView * DView = DirView(Side);
  5738. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5739. {
  5740. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5741. }
  5742. }
  5743. catch (...)
  5744. {
  5745. delete VisitedDirectories;
  5746. throw;
  5747. }
  5748. return VisitedDirectories;
  5749. }
  5750. //---------------------------------------------------------------------------
  5751. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5752. TOpenDirectoryMode Mode, TOperationSide Side)
  5753. {
  5754. bool Continue = true;
  5755. if (Mode == odAddBookmark)
  5756. {
  5757. TMessageParams Params(mpNeverAskAgainCheck);
  5758. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5759. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5760. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5761. unsigned int Answer =
  5762. MessageDialog(Message,
  5763. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5764. if (Answer == qaNeverAskAgain)
  5765. {
  5766. Continue = true;
  5767. WinConfiguration->UseSharedBookmarks = true;
  5768. }
  5769. else if (Answer == qaOK)
  5770. {
  5771. Continue = true;
  5772. WinConfiguration->UseSharedBookmarks = false;
  5773. }
  5774. else
  5775. {
  5776. Continue = false;
  5777. }
  5778. }
  5779. if (Continue)
  5780. {
  5781. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5782. try
  5783. {
  5784. UnicodeString Name = DirView(Side)->PathName;
  5785. bool Repeat;
  5786. do
  5787. {
  5788. Repeat = false;
  5789. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5790. // do not allow switching to location profiles,
  5791. // if we are not connected
  5792. HasDirView[osLocal] && (Terminal != NULL)))
  5793. {
  5794. TWindowLock Lock(this);
  5795. if (!TryOpenDirectory(Side, Name))
  5796. {
  5797. Repeat = true;
  5798. Mode = odBrowse;
  5799. }
  5800. }
  5801. }
  5802. while (Repeat);
  5803. }
  5804. __finally
  5805. {
  5806. delete VisitedDirectories;
  5807. }
  5808. }
  5809. }
  5810. //---------------------------------------------------------------------------
  5811. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5812. {
  5813. DebugAssert(!FTerminal->CommandSessionOpened);
  5814. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5815. }
  5816. //---------------------------------------------------------------------------
  5817. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5818. {
  5819. bool Result = FTerminal->IsCapable[Capability] ||
  5820. FTerminal->CommandSessionOpened;
  5821. if (!Result)
  5822. {
  5823. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5824. if (!GUIConfiguration->ConfirmCommandSession)
  5825. {
  5826. Result = true;
  5827. }
  5828. else
  5829. {
  5830. TMessageParams Params(mpNeverAskAgainCheck);
  5831. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5832. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5833. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5834. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5835. if (Answer == qaNeverAskAgain)
  5836. {
  5837. GUIConfiguration->ConfirmCommandSession = false;
  5838. Result = true;
  5839. }
  5840. else if (Answer == qaOK)
  5841. {
  5842. Result = true;
  5843. }
  5844. }
  5845. if (Result)
  5846. {
  5847. Result = CommandSessionFallback();
  5848. }
  5849. }
  5850. return Result;
  5851. }
  5852. //---------------------------------------------------------------------------
  5853. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5854. {
  5855. if (EnsureCommandSessionFallback(fcAnyCommand))
  5856. {
  5857. DoConsoleDialog(Terminal, Command);
  5858. }
  5859. }
  5860. //---------------------------------------------------------------------------
  5861. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5862. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5863. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5864. {
  5865. if (!IsSideLocalBrowser(osRemote) &&
  5866. IsFileControl(DropSourceControl, osRemote) &&
  5867. (FDDExtMapFile != NULL))
  5868. {
  5869. Accept = true;
  5870. }
  5871. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5872. DebugAssert(FDDTargetControl != NULL);
  5873. }
  5874. //---------------------------------------------------------------------------
  5875. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5876. _di_IDataObject DataObj, int KeyState,
  5877. const TPoint & Point, int & Effect, bool & Accept)
  5878. {
  5879. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5880. }
  5881. //---------------------------------------------------------------------------
  5882. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5883. _di_IDataObject DataObj, int KeyState,
  5884. const TPoint & Point, int & Effect, bool & Accept)
  5885. {
  5886. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5887. }
  5888. //---------------------------------------------------------------------------
  5889. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5890. TObject * Sender)
  5891. {
  5892. DebugUsedParam(Sender);
  5893. DebugAssert(FDDTargetControl == Sender);
  5894. FDDTargetControl = NULL;
  5895. }
  5896. //---------------------------------------------------------------------------
  5897. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5898. {
  5899. FileControlDDDragLeave(SessionsPageControl);
  5900. }
  5901. //---------------------------------------------------------------------------
  5902. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5903. {
  5904. FileControlDDDragLeave(QueueView3);
  5905. }
  5906. //---------------------------------------------------------------------------
  5907. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5908. {
  5909. DebugAssert(!IsSideLocalBrowser(Side));
  5910. DebugUsedParam(Side);
  5911. bool Edit = false;
  5912. TRemoteFile * File = NULL;
  5913. UnicodeString FileName;
  5914. UnicodeString PointTo;
  5915. bool SymbolicLink = true;
  5916. if (RemoteDirView->ItemFocused)
  5917. {
  5918. DebugAssert(RemoteDirView->ItemFocused->Data);
  5919. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5920. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5921. if (Edit)
  5922. {
  5923. FileName = File->FileName;
  5924. PointTo = File->LinkTo;
  5925. }
  5926. else
  5927. {
  5928. PointTo = File->FileName;
  5929. }
  5930. }
  5931. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5932. Terminal->IsCapable[fcHardLink]))
  5933. {
  5934. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5935. if (Edit)
  5936. {
  5937. DebugAssert(File->FileName == FileName);
  5938. int Params = dfNoRecursive;
  5939. Terminal->ExceptionOnFail = true;
  5940. try
  5941. {
  5942. Terminal->DeleteFile(L"", File, &Params);
  5943. }
  5944. __finally
  5945. {
  5946. Terminal->ExceptionOnFail = false;
  5947. }
  5948. }
  5949. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5950. }
  5951. }
  5952. //---------------------------------------------------------------------------
  5953. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5954. {
  5955. return
  5956. (IsSideLocalBrowser(Side) ||
  5957. ((Terminal != NULL) &&
  5958. Terminal->ResolvingSymlinks &&
  5959. Terminal->IsCapable[fcSymbolicLink]));
  5960. }
  5961. //---------------------------------------------------------------------------
  5962. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5963. {
  5964. return
  5965. !IsSideLocalBrowser(FCurrentSide) &&
  5966. (RemoteDirView->ItemFocused != NULL) &&
  5967. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5968. Terminal->SessionData->ResolveSymlinks;
  5969. }
  5970. //---------------------------------------------------------------------------
  5971. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5972. {
  5973. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5974. FForceExecution = true;
  5975. try
  5976. {
  5977. DirView(osCurrent)->ExecuteCurrentFile();
  5978. }
  5979. __finally
  5980. {
  5981. FForceExecution = false;
  5982. }
  5983. }
  5984. //---------------------------------------------------------------------------
  5985. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5986. {
  5987. TEditorData ExternalEditor;
  5988. ExternalEditor.Editor = edExternal;
  5989. bool Remember = false;
  5990. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  5991. {
  5992. if (Remember)
  5993. {
  5994. TEditorList * EditorList = new TEditorList;
  5995. try
  5996. {
  5997. *EditorList = *WinConfiguration->EditorList;
  5998. bool Found = false;
  5999. int i = 0;
  6000. while (!Found && (i < EditorList->Count))
  6001. {
  6002. const TEditorPreferences * Editor = EditorList->Editors[i];
  6003. if ((Editor->Data->Editor == edExternal) &&
  6004. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6005. {
  6006. Found = true;
  6007. }
  6008. i++;
  6009. }
  6010. if (!Found)
  6011. {
  6012. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6013. WinConfiguration->EditorList = EditorList;
  6014. }
  6015. }
  6016. __finally
  6017. {
  6018. delete EditorList;
  6019. }
  6020. }
  6021. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6022. }
  6023. }
  6024. //---------------------------------------------------------------------------
  6025. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6026. {
  6027. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6028. if (FFileFindTerminal == Sender)
  6029. {
  6030. FFileFindTerminal = NULL;
  6031. HideFileFindDialog();
  6032. }
  6033. if (FClipboardTerminal == Sender)
  6034. {
  6035. ClipboardClear(); // implies ClipboardStop
  6036. }
  6037. }
  6038. //---------------------------------------------------------------------------
  6039. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6040. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6041. {
  6042. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6043. DebugAssert(Terminal != NULL);
  6044. if (Data->Terminal == Terminal)
  6045. {
  6046. Data->Terminal = NULL;
  6047. }
  6048. }
  6049. //---------------------------------------------------------------------------
  6050. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6051. {
  6052. UpdateControls();
  6053. SessionColor = TColor(0);
  6054. UpdateRemotePathComboBox(false);
  6055. NeedSession(true);
  6056. }
  6057. //---------------------------------------------------------------------------
  6058. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  6059. {
  6060. try
  6061. {
  6062. try
  6063. {
  6064. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6065. }
  6066. __finally
  6067. {
  6068. if (!WinConfiguration->KeepOpenWhenNoSession &&
  6069. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6070. {
  6071. TerminateApplication();
  6072. }
  6073. }
  6074. }
  6075. catch (EAbort &)
  6076. {
  6077. // swallow
  6078. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6079. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6080. }
  6081. }
  6082. //---------------------------------------------------------------------------
  6083. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6084. {
  6085. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6086. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6087. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6088. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6089. try
  6090. {
  6091. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6092. // Clear the design time unthemed tab
  6093. ((SessionsPageControl->PageCount > 0) &&
  6094. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6095. {
  6096. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6097. }
  6098. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6099. {
  6100. TThemeTabSheet * TabSheet;
  6101. if (Index >= SessionsPageControl->PageCount)
  6102. {
  6103. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6104. TabSheet->PageControl = SessionsPageControl;
  6105. }
  6106. else
  6107. {
  6108. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6109. }
  6110. bool IsSessionTab = (Index < TerminalList->Count);
  6111. if (IsSessionTab)
  6112. {
  6113. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6114. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6115. UpdateSessionTab(TabSheet);
  6116. }
  6117. else
  6118. {
  6119. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6120. TabSheet->Tag = 0; // not really needed
  6121. TabSheet->Shadowed = false;
  6122. // 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)
  6123. UpdateNewSessionTab();
  6124. }
  6125. TabSheet->ShowCloseButton = IsSessionTab;
  6126. }
  6127. }
  6128. __finally
  6129. {
  6130. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6131. }
  6132. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6133. }
  6134. //---------------------------------------------------------------------------
  6135. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6136. {
  6137. DoTerminalListChanged();
  6138. }
  6139. //---------------------------------------------------------------------------
  6140. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6141. {
  6142. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6143. DebugAssert(TabSheet->ImageIndex == 0);
  6144. TabSheet->Caption =
  6145. WinConfiguration->SelectiveToolbarText ?
  6146. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6147. UnicodeString();
  6148. }
  6149. //---------------------------------------------------------------------------
  6150. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6151. {
  6152. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6153. }
  6154. //---------------------------------------------------------------------------
  6155. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6156. {
  6157. if (DebugAlwaysTrue(TabSheet != NULL))
  6158. {
  6159. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6160. if (DebugAlwaysTrue(ATerminal != NULL))
  6161. {
  6162. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6163. TabSheet->ImageIndex = AddSessionColor(Color);
  6164. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6165. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6166. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6167. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6168. {
  6169. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6170. }
  6171. }
  6172. }
  6173. }
  6174. //---------------------------------------------------------------------------
  6175. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6176. {
  6177. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6178. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6179. bool Result = (Terminal != NULL);
  6180. if (Result)
  6181. {
  6182. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6183. }
  6184. else
  6185. {
  6186. try
  6187. {
  6188. NewSession(false);
  6189. }
  6190. __finally
  6191. {
  6192. DoTerminalListChanged();
  6193. }
  6194. FSessionsPageControlNewSessionTime = Now();
  6195. }
  6196. return Result;
  6197. }
  6198. //---------------------------------------------------------------------------
  6199. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6200. {
  6201. SessionTabSwitched();
  6202. }
  6203. //---------------------------------------------------------------------------
  6204. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6205. {
  6206. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6207. DebugAssert(TransferList != NULL);
  6208. UnicodeString Name;
  6209. if (TransferList->ItemIndex <= 0)
  6210. {
  6211. Name = L"";
  6212. }
  6213. else
  6214. {
  6215. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6216. }
  6217. if (FCopyParamAutoSelected.IsEmpty())
  6218. {
  6219. // if previous preset was not autoselected, make new preset the "default"
  6220. FCopyParamDefault = Name;
  6221. }
  6222. GUIConfiguration->CopyParamCurrent = Name;
  6223. }
  6224. //---------------------------------------------------------------------------
  6225. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6226. {
  6227. // sanity check
  6228. bool ExistingPreset =
  6229. (FTransferListHoverIndex >= 0) &&
  6230. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6231. DebugAssert(ExistingPreset);
  6232. if (ExistingPreset)
  6233. {
  6234. HDC DC = GetDC(0);
  6235. TCanvas * Canvas = new TCanvas();
  6236. try
  6237. {
  6238. Canvas->Handle = DC;
  6239. Canvas->Font = GetToolbarFont(this);
  6240. UnicodeString Name;
  6241. if (FTransferListHoverIndex == 0)
  6242. {
  6243. Name = L"";
  6244. }
  6245. else
  6246. {
  6247. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6248. }
  6249. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6250. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6251. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6252. static_cast<TObject*>(GetComponent(fcTransferList)));
  6253. UnicodeString InfoStr =
  6254. GUIConfiguration->CopyParamPreset[Name].
  6255. GetInfoStr(L"; ",
  6256. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6257. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6258. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6259. {
  6260. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6261. {
  6262. InfoStr.SetLength(InfoStr.Length() - 1);
  6263. }
  6264. InfoStr += Ellipsis;
  6265. }
  6266. // UpdateCaption does not cause invalidation of whole submenu, while
  6267. // setting Caption property does.
  6268. // also it probably does not resize the label, even if necessary
  6269. // (we do not want that anyway)
  6270. TransferLabel->UpdateCaption(InfoStr);
  6271. }
  6272. __finally
  6273. {
  6274. Canvas->Handle = NULL;
  6275. ReleaseDC(0, DC);
  6276. delete Canvas;
  6277. }
  6278. }
  6279. }
  6280. //---------------------------------------------------------------------------
  6281. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6282. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6283. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6284. {
  6285. if (FTransferListHoverIndex != AHoverIndex)
  6286. {
  6287. FTransferListHoverIndex = AHoverIndex;
  6288. UpdateTransferLabel();
  6289. }
  6290. }
  6291. //---------------------------------------------------------------------------
  6292. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6293. {
  6294. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6295. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6296. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6297. {
  6298. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6299. {
  6300. OpenDirectory(GetSide(osCurrent));
  6301. Message.Result = 1;
  6302. }
  6303. else
  6304. {
  6305. TForm::Dispatch(&Message);
  6306. }
  6307. }
  6308. else
  6309. {
  6310. TForm::Dispatch(&Message);
  6311. }
  6312. }
  6313. //---------------------------------------------------------------------------
  6314. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6315. {
  6316. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6317. if ((FIgnoreNextDialogChar != 0) &&
  6318. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6319. {
  6320. Message.Result = 1;
  6321. }
  6322. else
  6323. {
  6324. TForm::Dispatch(&Message);
  6325. }
  6326. FIgnoreNextDialogChar = 0;
  6327. }
  6328. //---------------------------------------------------------------------------
  6329. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6330. {
  6331. // The four low-order bits are used internally by Windows
  6332. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6333. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6334. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6335. // 61730 - restore thru double click - undocumented
  6336. // 61490 - maximize thru double click - undocumented
  6337. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6338. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6339. {
  6340. SysResizing(Cmd);
  6341. }
  6342. TForm::Dispatch(&Message);
  6343. }
  6344. //---------------------------------------------------------------------------
  6345. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6346. {
  6347. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6348. // Also there no point returning TRUE as we are not able to
  6349. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6350. // Hence the process termination might be safer :)
  6351. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6352. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6353. {
  6354. Message.Result = FALSE;
  6355. }
  6356. else
  6357. {
  6358. Message.Result = TRUE;
  6359. }
  6360. // Do not call default handling as that triggers OnCloseQuery,
  6361. // where our implementation will popup confirmation dialogs, what we do not want,
  6362. // as per Vista guidelines:
  6363. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6364. }
  6365. //---------------------------------------------------------------------------
  6366. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6367. {
  6368. if (Message.EndSession && IsApplicationMinimized())
  6369. {
  6370. // WORKAROUND
  6371. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6372. // when it receives WM_ENDSESSION,
  6373. // but that sometimes (particularly when application is minimized) cause crashes.
  6374. // Crash popups message that blocks log off.
  6375. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6376. // so we call ExitProcess() immediately, not even trying to cleanup.
  6377. // It still causes beep, so there's likely some popup, but it does not block
  6378. // log off.
  6379. ExitProcess(0);
  6380. }
  6381. TForm::Dispatch(&Message);
  6382. }
  6383. #ifdef _DEBUG
  6384. //---------------------------------------------------------------------------
  6385. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6386. {
  6387. TForm::Dispatch(&Message);
  6388. }
  6389. #endif
  6390. //---------------------------------------------------------------------------
  6391. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6392. {
  6393. }
  6394. //---------------------------------------------------------------------------
  6395. void __fastcall TCustomScpExplorerForm::DoShow()
  6396. {
  6397. // only now are the controls resized finally, so the size constraints
  6398. // will not conflict with possibly very small window size
  6399. RestoreFormParams();
  6400. // This indicates that the form was rescaled after parameters were loaded and
  6401. // as most probably the parameters were actually saved with the current form scaling
  6402. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6403. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6404. // and no rounding problems will occur.
  6405. // See also RestoreForm.
  6406. // (we should be safe to call RestoreParams unconditionally)
  6407. if (PixelsPerInch != Screen->PixelsPerInch)
  6408. {
  6409. RestoreParams();
  6410. }
  6411. FixControlsPlacement();
  6412. if (Position == poDefaultPosOnly)
  6413. {
  6414. CutFormToDesktop(this);
  6415. }
  6416. TForm::DoShow();
  6417. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6418. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6419. if (Terminal == NULL)
  6420. {
  6421. StartingDisconnected();
  6422. }
  6423. UpdatePixelsPerInchMainWindowCounter();
  6424. FShowing = true;
  6425. }
  6426. //---------------------------------------------------------------------------
  6427. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6428. {
  6429. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6430. }
  6431. //---------------------------------------------------------------------------
  6432. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6433. {
  6434. DoTerminalListChanged();
  6435. }
  6436. //---------------------------------------------------------------------------
  6437. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6438. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6439. TNotifyEvent OnBalloonClick, TObject * UserData)
  6440. {
  6441. bool Do;
  6442. UnicodeString Message;
  6443. if (E == NULL)
  6444. {
  6445. Message = Str;
  6446. Do = true;
  6447. }
  6448. else
  6449. {
  6450. Do = ExceptionMessage(E, Message);
  6451. }
  6452. if (Do && WinConfiguration->BalloonNotifications)
  6453. {
  6454. UnicodeString Title;
  6455. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6456. {
  6457. Message = TrimLeft(Message);
  6458. }
  6459. else
  6460. {
  6461. Message = UnformatMessage(Message);
  6462. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6463. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6464. Title = LoadResourceString(Captions[Type]);
  6465. if (Terminal != NULL)
  6466. {
  6467. Title = FORMAT(L"%s - %s",
  6468. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6469. }
  6470. }
  6471. if (Seconds == 0)
  6472. {
  6473. Seconds = WinConfiguration->NotificationsTimeout;
  6474. }
  6475. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6476. }
  6477. }
  6478. //---------------------------------------------------------------------------
  6479. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6480. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6481. UnicodeString HelpKeyword, const TMessageParams * Params,
  6482. TTerminal * Terminal)
  6483. {
  6484. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6485. (FOnFeedSynchronizeError != NULL)) &&
  6486. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6487. {
  6488. if (FOnFeedSynchronizeError != NULL)
  6489. {
  6490. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6491. }
  6492. else
  6493. {
  6494. DebugAssert(FErrorList != NULL);
  6495. TStringList * MoreMessagesCopy = NULL;
  6496. if (MoreMessages)
  6497. {
  6498. MoreMessagesCopy = new TStringList();
  6499. MoreMessagesCopy->Assign(MoreMessages);
  6500. }
  6501. FErrorList->AddObject(Message, MoreMessagesCopy);
  6502. }
  6503. PopupTrayBalloon(Terminal, Message, Type);
  6504. return ContinueAnswer(Answers);
  6505. }
  6506. else
  6507. {
  6508. bool UseBalloon = FTrayIcon->Visible;
  6509. if (UseBalloon)
  6510. {
  6511. PopupTrayBalloon(Terminal, Message, Type);
  6512. }
  6513. unsigned int Result;
  6514. try
  6515. {
  6516. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6517. }
  6518. __finally
  6519. {
  6520. // cancel only balloon we popped up, otherwise we may cancel notification
  6521. // balloon that was there before the message dialog
  6522. // (such as "dd confirmation opt in balloon)
  6523. if (UseBalloon)
  6524. {
  6525. FTrayIcon->CancelBalloon();
  6526. }
  6527. }
  6528. return Result;
  6529. }
  6530. }
  6531. //---------------------------------------------------------------------------
  6532. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6533. TTerminal * Terminal, Exception * E)
  6534. {
  6535. if (FTrayIcon->Visible)
  6536. {
  6537. PopupTrayBalloon(Terminal, L"", qtError, E);
  6538. }
  6539. // particularly prevent opening new session from jump list,
  6540. // while exception is shown
  6541. NonVisualDataModule->StartBusy();
  6542. try
  6543. {
  6544. ShowExtendedExceptionEx(Terminal, E);
  6545. }
  6546. __finally
  6547. {
  6548. NonVisualDataModule->EndBusy();
  6549. FTrayIcon->CancelBalloon();
  6550. }
  6551. }
  6552. //---------------------------------------------------------------------------
  6553. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6554. {
  6555. if (Terminal->Active)
  6556. {
  6557. InitStatusBar();
  6558. }
  6559. // cannot rely on active page being page for active terminal,
  6560. // as it can happen that active page is the "new session" page
  6561. // (e.g. when reconnecting active terminal, while login dialog
  6562. // invoked from "new session" page is modal)
  6563. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6564. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6565. }
  6566. //---------------------------------------------------------------------------
  6567. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6568. TTerminal * Terminal, Exception * E)
  6569. {
  6570. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6571. if (!Terminal->Active)
  6572. {
  6573. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6574. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6575. {
  6576. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6577. }
  6578. }
  6579. }
  6580. //---------------------------------------------------------------------------
  6581. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6582. UnicodeString Message, TQueryType Type,
  6583. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6584. {
  6585. if ((E == NULL) ||
  6586. ExceptionMessage(E, Message))
  6587. {
  6588. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6589. if (Important)
  6590. {
  6591. Seconds *= 5;
  6592. }
  6593. UnicodeString NoteMessage(UnformatMessage(Message));
  6594. if (Terminal != NULL)
  6595. {
  6596. NoteMessage = FORMAT(L"%s: %s",
  6597. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6598. }
  6599. if (WinConfiguration->BalloonNotifications)
  6600. {
  6601. AddNote(NoteMessage);
  6602. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6603. }
  6604. else
  6605. {
  6606. FlashOnBackground();
  6607. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6608. }
  6609. }
  6610. }
  6611. //---------------------------------------------------------------------------
  6612. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6613. {
  6614. RestoreApp();
  6615. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6616. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6617. !NonVisualDataModule->Busy)
  6618. {
  6619. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6620. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6621. if (!ComponentVisible[fcQueueView])
  6622. {
  6623. ToggleQueueVisibility();
  6624. GoToQueue();
  6625. }
  6626. }
  6627. }
  6628. //---------------------------------------------------------------------------
  6629. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6630. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6631. {
  6632. UnicodeString Message;
  6633. TNotifyEvent OnClick = NULL;
  6634. TObject * UserData = NULL;
  6635. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6636. switch (Event)
  6637. {
  6638. case qeEmptyButMonitored:
  6639. if ((ATerminal != Terminal) || QueueInvisible)
  6640. {
  6641. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6642. OnClick = QueueEmptyNoteClicked;
  6643. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6644. TerminalNoteData->Terminal = ATerminal;
  6645. UserData = TerminalNoteData;
  6646. }
  6647. break;
  6648. case qeEmpty:
  6649. OperationComplete(ATerminal->QueueOperationStart);
  6650. break;
  6651. case qePendingUserAction:
  6652. if ((ATerminal != Terminal) ||
  6653. (QueueInvisible && !IsQueueAutoPopup()))
  6654. {
  6655. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6656. }
  6657. break;
  6658. default:
  6659. DebugFail();
  6660. }
  6661. if (!Message.IsEmpty())
  6662. {
  6663. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6664. }
  6665. }
  6666. //---------------------------------------------------------------------------
  6667. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6668. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6669. {
  6670. if (FDDExtMapFile != NULL)
  6671. {
  6672. CloseHandle(FDDExtMapFile);
  6673. FDDExtMapFile = NULL;
  6674. }
  6675. if (WinConfiguration->DDFakeFile)
  6676. {
  6677. DDFakeFileInitDrag(FileList, Created);
  6678. }
  6679. }
  6680. //---------------------------------------------------------------------------
  6681. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6682. {
  6683. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6684. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6685. {
  6686. FFakeFileDropTarget = FileName;
  6687. }
  6688. }
  6689. //---------------------------------------------------------------------------
  6690. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6691. {
  6692. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6693. if (!ForceDirectories(ApiPath(Result)))
  6694. {
  6695. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6696. }
  6697. FileSetAttr(ApiPath(Result), faHidden);
  6698. return Result;
  6699. }
  6700. //---------------------------------------------------------------------------
  6701. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6702. bool & Created)
  6703. {
  6704. FFakeFileDropTarget = UnicodeString();
  6705. FDragFakeDirectory = CreateFakeTransferDirectory();
  6706. FileList->AddItem(NULL, FDragFakeDirectory);
  6707. Created = true;
  6708. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6709. {
  6710. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6711. }
  6712. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6713. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6714. {
  6715. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6716. TDragExtCommStruct* CommStruct;
  6717. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6718. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6719. DebugAssert(CommStruct != NULL);
  6720. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6721. CommStruct->Dragging = true;
  6722. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6723. LENOF(CommStruct->DropDest));
  6724. NULL_TERMINATE(CommStruct->DropDest);
  6725. UnmapViewOfFile(CommStruct);
  6726. }
  6727. }
  6728. //---------------------------------------------------------------------------
  6729. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6730. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6731. {
  6732. bool Result;
  6733. DebugAssert(!IsLocalBrowserMode());
  6734. if (Sender == RemoteDirView)
  6735. {
  6736. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6737. }
  6738. else
  6739. {
  6740. DebugAssert(Sender == RemoteDriveView);
  6741. TStrings * FileList = RemoteDriveView->DragFileList();
  6742. try
  6743. {
  6744. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6745. }
  6746. __finally
  6747. {
  6748. delete FileList;
  6749. }
  6750. }
  6751. if (FDDTargetControl == RemoteDriveView)
  6752. {
  6753. RemoteDriveView->UpdateDropTarget();
  6754. }
  6755. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6756. // or from tree view to dir view.
  6757. // foMove happens when file/dir is dragged from remote tree view outside of
  6758. // application via dragex
  6759. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6760. (DropSourceControl == RemoteDriveView))
  6761. {
  6762. RemoteDriveView->UpdateDropSource();
  6763. }
  6764. return Result;
  6765. }
  6766. //---------------------------------------------------------------------------
  6767. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6768. {
  6769. DebugAssert(!IsLocalBrowserMode());
  6770. // This also handles drops of remote files to queue.
  6771. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6772. SAFE_DESTROY(FDDFileList);
  6773. if (!FFakeFileDropTarget.IsEmpty())
  6774. {
  6775. RemoveDir(ApiPath(FFakeFileDropTarget));
  6776. }
  6777. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6778. {
  6779. try
  6780. {
  6781. TDragResult DDResult = (Sender == RemoteDirView) ?
  6782. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6783. // Focus is moved to the target application,
  6784. // but as we are going to present the UI, we need to steal the focus back.
  6785. // This most likely won't work though (windows does not allow application
  6786. // to steal focus most of the time)
  6787. Application->BringToFront();
  6788. // On older version of Windows we never got drMove here, see also comment below.
  6789. // On Windows 10, we get the "move".
  6790. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6791. {
  6792. UnicodeString TargetDirectory;
  6793. TFileOperation Operation;
  6794. // drInvalid may mean drMove, see comment below
  6795. switch (DDResult)
  6796. {
  6797. case drCopy:
  6798. Operation = foCopy;
  6799. break;
  6800. case drMove:
  6801. Operation = foMove;
  6802. break;
  6803. default:
  6804. DebugFail();
  6805. case drInvalid:
  6806. // prefer "copy" for safety
  6807. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6808. break;
  6809. }
  6810. TTransferOperationParam Param;
  6811. UnicodeString CounterName;
  6812. bool ForceQueue;
  6813. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6814. {
  6815. // we get drInvalid both if d&d was intercepted by ddext,
  6816. // and when users drops on no-drop location.
  6817. // we tell the difference by existence of response from ddext,
  6818. // so we ignore absence of response in this case
  6819. if (DDResult != drInvalid)
  6820. {
  6821. // here we know that the extension is installed,
  6822. // as it is checked as soon as drag&drop starts from
  6823. // RemoteFileControlDDCreateDragFileList
  6824. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6825. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6826. {
  6827. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6828. }
  6829. else
  6830. {
  6831. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6832. }
  6833. }
  6834. }
  6835. else
  6836. {
  6837. // download using fake file
  6838. Param.Temp = false;
  6839. Param.DragDrop = true;
  6840. if (ForceQueue)
  6841. {
  6842. Param.Queue = asOn;
  6843. }
  6844. if (Sender == RemoteDirView)
  6845. {
  6846. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6847. }
  6848. if (RemoteFileControlFileOperation(Sender, Operation,
  6849. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6850. {
  6851. Configuration->Usage->Inc(CounterName);
  6852. }
  6853. }
  6854. }
  6855. }
  6856. __finally
  6857. {
  6858. CloseHandle(FDDExtMapFile);
  6859. FDDExtMapFile = NULL;
  6860. delete FDragFakeMonitors;
  6861. FDragFakeMonitors = NULL;
  6862. RemoveDir(ApiPath(FDragFakeDirectory));
  6863. FDragFakeDirectory = L"";
  6864. FFakeFileDropTarget = UnicodeString();
  6865. }
  6866. }
  6867. if (FDragDropSshTerminate.get() != NULL)
  6868. {
  6869. FDragDropSshTerminate->Rethrow();
  6870. }
  6871. }
  6872. //---------------------------------------------------------------------------
  6873. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6874. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6875. {
  6876. // Remember drop effect so we know (when user drops files), if we copy or move
  6877. FLastDropEffect = dwEffect;
  6878. }
  6879. //---------------------------------------------------------------------------
  6880. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6881. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6882. {
  6883. bool Result;
  6884. if (FDDTargetControl == QueueView3)
  6885. {
  6886. Directory = DefaultDownloadTargetDirectory();
  6887. Result = true;
  6888. CounterName = L"DownloadsDragDropQueue";
  6889. ForceQueue = true;
  6890. }
  6891. else if (!FFakeFileDropTarget.IsEmpty())
  6892. {
  6893. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6894. Result = true;
  6895. ForceQueue = false;
  6896. CounterName = L"DownloadsDragDropFakeFile";
  6897. }
  6898. else
  6899. {
  6900. ForceQueue = false;
  6901. Enabled = false;
  6902. try
  6903. {
  6904. int Timer = 0;
  6905. Result = false;
  6906. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6907. {
  6908. {
  6909. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6910. TDragExtCommStruct* CommStruct;
  6911. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6912. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6913. DebugAssert(CommStruct != NULL);
  6914. Result = !CommStruct->Dragging;
  6915. if (Result)
  6916. {
  6917. Directory = ExtractFilePath(CommStruct->DropDest);
  6918. CounterName = L"DownloadsDragDropExternalExt";
  6919. }
  6920. UnmapViewOfFile(CommStruct);
  6921. }
  6922. if (!Result)
  6923. {
  6924. Sleep(50);
  6925. Timer += 50;
  6926. Application->ProcessMessages();
  6927. }
  6928. }
  6929. }
  6930. __finally
  6931. {
  6932. Enabled = true;
  6933. }
  6934. }
  6935. return Result;
  6936. }
  6937. //---------------------------------------------------------------------------
  6938. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6939. const UnicodeString TempDir, int SecDelay)
  6940. {
  6941. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6942. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6943. if (FDelayedDeletionTimer == NULL)
  6944. {
  6945. DebugAssert(HandleAllocated());
  6946. FDelayedDeletionTimer = new TTimer(this);
  6947. FDelayedDeletionTimer->Interval = 10000;
  6948. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6949. }
  6950. else
  6951. {
  6952. FDelayedDeletionTimer->Enabled = true;
  6953. }
  6954. }
  6955. //---------------------------------------------------------------------------
  6956. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6957. {
  6958. DebugAssert(FDelayedDeletionList != NULL);
  6959. TDateTime N = Now();
  6960. TDateTime Alert;
  6961. UnicodeString Directory;
  6962. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6963. {
  6964. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6965. if ((N >= Alert) || (Sender == NULL))
  6966. {
  6967. Directory = FDelayedDeletionList->Strings[Index];
  6968. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6969. {
  6970. FDelayedDeletionList->Delete(Index);
  6971. }
  6972. }
  6973. }
  6974. if (FDelayedDeletionList->Count == 0)
  6975. {
  6976. FDelayedDeletionTimer->Enabled = false;
  6977. }
  6978. }
  6979. //---------------------------------------------------------------------------
  6980. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6981. {
  6982. DebugAssert(!IsLocalBrowserMode());
  6983. if (IsFileControl(FDDTargetControl, osRemote) ||
  6984. (FDDTargetControl == SessionsPageControl))
  6985. {
  6986. TTerminal * TargetTerminal = NULL;
  6987. TFileOperation Operation = foNone;
  6988. if (FDDTargetControl == SessionsPageControl)
  6989. {
  6990. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6991. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6992. // do not allow dropping on the "+" tab
  6993. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6994. if (TargetTerminal != NULL)
  6995. {
  6996. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6997. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6998. {
  6999. Operation = foRemoteMove;
  7000. }
  7001. else
  7002. {
  7003. Operation = foRemoteCopy;
  7004. }
  7005. }
  7006. }
  7007. else
  7008. {
  7009. if (FLastDropEffect == DROPEFFECT_MOVE)
  7010. {
  7011. Operation = foRemoteMove;
  7012. }
  7013. else if (FLastDropEffect == DROPEFFECT_COPY)
  7014. {
  7015. Operation = foRemoteCopy;
  7016. }
  7017. }
  7018. if (Operation != foNone)
  7019. {
  7020. RemoteFileControlFileOperation(DropSourceControl,
  7021. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7022. }
  7023. // abort drag&drop
  7024. Abort();
  7025. }
  7026. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7027. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7028. (FDDTargetControl != QueueView3))
  7029. {
  7030. DebugAssert(!FDragTempDir.IsEmpty());
  7031. TTransferType Type;
  7032. UnicodeString TempDir = FDragTempDir;
  7033. // We clear FDragTempDir before calling
  7034. // just in case it fail (raises exception)
  7035. FDragTempDir = L"";
  7036. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7037. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7038. // empty directory parameter means temp directory -> don't display it!
  7039. UnicodeString TargetDir = L"";
  7040. int Options = 0;
  7041. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7042. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7043. {
  7044. Abort();
  7045. }
  7046. // TargetDir is set when dropped on local file control
  7047. // (this was workaround for legacy dirview event handling, now it should be
  7048. // made prettier)
  7049. if (TargetDir.IsEmpty())
  7050. {
  7051. TargetDir = TempDir;
  7052. if (ForceDirectories(ApiPath(TargetDir)))
  7053. {
  7054. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7055. FPendingTempSpaceWarn = true;
  7056. try
  7057. {
  7058. FDragDropOperation = true;
  7059. int Params = cpTemporary |
  7060. (Type == ttMove ? cpDelete : 0);
  7061. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7062. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7063. }
  7064. __finally
  7065. {
  7066. FDragDropOperation = false;
  7067. FPendingTempSpaceWarn = false;
  7068. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7069. }
  7070. }
  7071. else
  7072. {
  7073. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7074. }
  7075. }
  7076. }
  7077. }
  7078. //---------------------------------------------------------------------------
  7079. void __fastcall TCustomScpExplorerForm::DDDownload(
  7080. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7081. {
  7082. DebugAssert(!IsLocalBrowserMode());
  7083. TAutoBatch AutoBatch(this);
  7084. UpdateCopyParamCounters(*CopyParam);
  7085. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7086. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7087. {
  7088. RemoteDriveView->UpdateDropSource();
  7089. }
  7090. }
  7091. //---------------------------------------------------------------------------
  7092. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7093. {
  7094. public:
  7095. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7096. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7097. {
  7098. }
  7099. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7100. {
  7101. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7102. TDataObjectFilesEx::AllowData(FormatEtc);
  7103. }
  7104. };
  7105. //---------------------------------------------------------------------------
  7106. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7107. TObject * Sender, TDataObject *& DataObject)
  7108. {
  7109. if (FDDExtMapFile != NULL)
  7110. {
  7111. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7112. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7113. {
  7114. Abort();
  7115. }
  7116. if (FileList->Count > 0)
  7117. {
  7118. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7119. if (!FilesObject->IsValid(true, true))
  7120. {
  7121. FilesObject->_Release();
  7122. }
  7123. else
  7124. {
  7125. DataObject = FilesObject;
  7126. }
  7127. }
  7128. }
  7129. }
  7130. //---------------------------------------------------------------------------
  7131. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7132. {
  7133. DebugFail();
  7134. }
  7135. //---------------------------------------------------------------------------
  7136. void __fastcall TCustomScpExplorerForm::GoToTree()
  7137. {
  7138. ComponentVisible[fcRemoteTree] = true;
  7139. DriveView(osRemote)->SetFocus();
  7140. }
  7141. //---------------------------------------------------------------------------
  7142. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7143. TPanelExport Export)
  7144. {
  7145. TCustomDirView * DirView = this->DirView(Side);
  7146. std::unique_ptr<TStrings> ExportData(new TStringList());
  7147. switch (Export)
  7148. {
  7149. case pePath:
  7150. ExportData->Add(DirView->PathName);
  7151. break;
  7152. case peFileList:
  7153. case peFullFileList:
  7154. {
  7155. bool FullPath = (Export == peFullFileList);
  7156. DirView->CreateFileList(false, FullPath, ExportData.get());
  7157. for (int Index = 0; Index < ExportData->Count; Index++)
  7158. {
  7159. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7160. {
  7161. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7162. }
  7163. }
  7164. }
  7165. break;
  7166. default:
  7167. DebugFail();
  7168. }
  7169. return ExportData.release();
  7170. }
  7171. //---------------------------------------------------------------------------
  7172. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7173. TPanelExport Export, TPanelExportDestination Destination)
  7174. {
  7175. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7176. PanelExportStore(Side, Export, Destination, ExportData.get());
  7177. }
  7178. //---------------------------------------------------------------------------
  7179. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7180. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7181. TStrings * ExportData)
  7182. {
  7183. if (Destination == pedClipboard)
  7184. {
  7185. TInstantOperationVisualizer Visualizer;
  7186. CopyToClipboard(ExportData);
  7187. }
  7188. else
  7189. {
  7190. DebugFail();
  7191. }
  7192. }
  7193. //---------------------------------------------------------------------------
  7194. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7195. {
  7196. TCustomDirView * DirView = this->DirView(Side);
  7197. UnicodeString Mask = DirView->Mask;
  7198. if (DoFilterMaskDialog(DirView, Mask))
  7199. {
  7200. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7201. Configuration->Usage->Inc(L"Filters");
  7202. }
  7203. }
  7204. //---------------------------------------------------------------------------
  7205. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7206. {
  7207. return FQueueController->DefaultOperation();
  7208. }
  7209. //---------------------------------------------------------------------------
  7210. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7211. TQueueOperation Operation, void ** Param)
  7212. {
  7213. switch (Operation)
  7214. {
  7215. case qoPreferences:
  7216. return true;
  7217. case qoGoTo:
  7218. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7219. case qoOnceEmpty:
  7220. return !FQueueController->Empty;
  7221. default:
  7222. return FQueueController->AllowOperation(Operation, Param);
  7223. }
  7224. }
  7225. //---------------------------------------------------------------------------
  7226. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7227. {
  7228. if (DebugAlwaysTrue(QueueView3->Visible))
  7229. {
  7230. QueueView3->SetFocus();
  7231. }
  7232. }
  7233. //---------------------------------------------------------------------------
  7234. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7235. TQueueOperation Operation, void * Param)
  7236. {
  7237. if (Operation == qoGoTo)
  7238. {
  7239. GoToQueue();
  7240. }
  7241. else if (Operation == qoPreferences)
  7242. {
  7243. PreferencesDialog(pmQueue);
  7244. }
  7245. else
  7246. {
  7247. FQueueController->ExecuteOperation(Operation, Param);
  7248. }
  7249. }
  7250. //---------------------------------------------------------------------------
  7251. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7252. {
  7253. return (Queue != NULL) && Queue->Enabled;
  7254. }
  7255. //---------------------------------------------------------------------------
  7256. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7257. {
  7258. DebugAssert(Queue != NULL);
  7259. if (Queue != NULL)
  7260. {
  7261. Queue->Enabled = !Queue->Enabled;
  7262. }
  7263. }
  7264. //---------------------------------------------------------------------------
  7265. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7266. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7267. {
  7268. FQueueActedItem = QueueView3->ItemFocused;
  7269. }
  7270. //---------------------------------------------------------------------------
  7271. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7272. {
  7273. return TopDock->Height + QueueSplitter->Height;
  7274. }
  7275. //---------------------------------------------------------------------------
  7276. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7277. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7278. {
  7279. // when queue is hidden by double-clicking splitter, stray attempt to
  7280. // resize the panel with strange value arrives, make sure it is ignored
  7281. if (ComponentVisible[fcQueueView])
  7282. {
  7283. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7284. RemotePanel->Constraints->MinHeight;
  7285. if (NewSize > HeightLimit)
  7286. {
  7287. NewSize = HeightLimit;
  7288. }
  7289. }
  7290. else
  7291. {
  7292. Accept = false;
  7293. }
  7294. }
  7295. //---------------------------------------------------------------------------
  7296. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7297. TDragObject *& /*DragObject*/)
  7298. {
  7299. FQueueActedItem = QueueView3->ItemFocused;
  7300. }
  7301. //---------------------------------------------------------------------------
  7302. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7303. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7304. {
  7305. Accept = true;
  7306. if (Source == QueueView3)
  7307. {
  7308. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7309. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7310. if (Accept)
  7311. {
  7312. TQueueItemProxy * QueueItem;
  7313. TQueueItemProxy * DestQueueItem;
  7314. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7315. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7316. Accept = (QueueItem != DestQueueItem) &&
  7317. (QueueItem->Status == TQueueItem::qsPending) &&
  7318. (DestQueueItem->Status == TQueueItem::qsPending);
  7319. }
  7320. }
  7321. }
  7322. //---------------------------------------------------------------------------
  7323. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7324. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7325. {
  7326. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7327. {
  7328. TQueueItemProxy * QueueItem;
  7329. TQueueItemProxy * DestQueueItem;
  7330. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7331. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7332. QueueItem->Move(DestQueueItem);
  7333. }
  7334. }
  7335. //---------------------------------------------------------------------------
  7336. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7337. {
  7338. if ((QueueView3->ItemFocused == NULL) &&
  7339. (QueueView3->Items->Count > 0))
  7340. {
  7341. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7342. }
  7343. QueueLabelUpdateStatus();
  7344. }
  7345. //---------------------------------------------------------------------------
  7346. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7347. {
  7348. QueueLabelUpdateStatus();
  7349. }
  7350. //---------------------------------------------------------------------------
  7351. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7352. {
  7353. QueueLabel->UpdateStatus();
  7354. }
  7355. //---------------------------------------------------------------------------
  7356. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7357. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7358. {
  7359. if (Selected)
  7360. {
  7361. NonVisualDataModule->UpdateNonVisibleActions();
  7362. }
  7363. }
  7364. //---------------------------------------------------------------------------
  7365. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7366. {
  7367. TDragDropFilesEx * Result = NULL;
  7368. if (Sender == SessionsPageControl)
  7369. {
  7370. Result = FSessionsDragDropFilesEx;
  7371. }
  7372. else if (Sender == QueueView3)
  7373. {
  7374. Result = FQueueDragDropFilesEx;
  7375. }
  7376. else
  7377. {
  7378. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7379. if (DirView != NULL)
  7380. {
  7381. Result = DirView->DragDropFilesEx;
  7382. }
  7383. else
  7384. {
  7385. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7386. if (DriveView != NULL)
  7387. {
  7388. Result = DriveView->DragDropFilesEx;
  7389. }
  7390. }
  7391. }
  7392. DebugAssert(Result != NULL);
  7393. return Result;
  7394. }
  7395. //---------------------------------------------------------------------------
  7396. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7397. {
  7398. return (DView->SelCount == DView->FilesCount);
  7399. }
  7400. //---------------------------------------------------------------------------
  7401. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7402. {
  7403. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7404. }
  7405. //---------------------------------------------------------------------------
  7406. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7407. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7408. {
  7409. DebugAssert(!IsLocalBrowserMode());
  7410. TFileOperation Operation;
  7411. switch (Effect)
  7412. {
  7413. case DROPEFFECT_MOVE:
  7414. Operation = foMove;
  7415. break;
  7416. case DROPEFFECT_COPY:
  7417. // occures on WinXP (reported by user)
  7418. default:
  7419. Operation = foCopy;
  7420. break;
  7421. };
  7422. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7423. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7424. if (DragDropFilesEx->FileList->Count > 0)
  7425. {
  7426. TStrings * FileList = new TStringList();
  7427. try
  7428. {
  7429. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7430. {
  7431. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7432. }
  7433. FDragDropOperation = true;
  7434. TTransferOperationParam Param;
  7435. Param.TargetDirectory = TargetPath;
  7436. // upload, no temp dirs
  7437. Param.Temp = false;
  7438. Param.DragDrop = DragDrop;
  7439. Param.Options =
  7440. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7441. if (ForceQueue)
  7442. {
  7443. Param.Queue = asOn;
  7444. }
  7445. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7446. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7447. {
  7448. if (IsFileControl(DropSourceControl, osLocal))
  7449. {
  7450. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7451. }
  7452. else
  7453. {
  7454. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7455. }
  7456. }
  7457. }
  7458. __finally
  7459. {
  7460. FDragDropOperation = false;
  7461. delete FileList;
  7462. }
  7463. }
  7464. }
  7465. //---------------------------------------------------------------------------
  7466. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7467. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7468. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7469. {
  7470. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7471. }
  7472. //---------------------------------------------------------------------------
  7473. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7474. TObject * Sender, int grfKeyState, int & dwEffect)
  7475. {
  7476. DebugAssert(!IsLocalBrowserMode());
  7477. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7478. if ((dwEffect != DROPEFFECT_NONE) &&
  7479. IsFileControl(DropSourceControl, osRemote))
  7480. {
  7481. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7482. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7483. (RemoteDirView->DropTarget == NULL))
  7484. {
  7485. dwEffect = DROPEFFECT_NONE;
  7486. }
  7487. else
  7488. {
  7489. if (dwEffect == DROPEFFECT_COPY)
  7490. {
  7491. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7492. // currently we support copying always (at least via temporary directory);
  7493. // remove associated checks once this all proves stable and working
  7494. bool CopyCapable = true;
  7495. // if we do not support neither of operations, there's no discussion
  7496. if (!MoveCapable && !CopyCapable)
  7497. {
  7498. dwEffect = DROPEFFECT_NONE;
  7499. }
  7500. else
  7501. {
  7502. // The default effect inside remote panel is move,
  7503. // unless we do not support it, but support copy
  7504. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7505. {
  7506. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7507. }
  7508. else
  7509. {
  7510. // with ctrl-down, we want copy unless it is not supported
  7511. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7512. }
  7513. }
  7514. }
  7515. }
  7516. }
  7517. }
  7518. //---------------------------------------------------------------------------
  7519. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7520. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7521. {
  7522. DebugAssert(!IsLocalBrowserMode());
  7523. if (FDDTotalSize >= 0)
  7524. {
  7525. if (File->IsDirectory)
  7526. {
  7527. FDDTotalSize = -1;
  7528. }
  7529. else
  7530. {
  7531. FDDTotalSize += File->Size;
  7532. }
  7533. }
  7534. DebugAssert(!FDragTempDir.IsEmpty());
  7535. // TODO: this is quite ineffective
  7536. // TODO: what if invalid character replacement is disabled?
  7537. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7538. UnicodeString TransferFileName;
  7539. if (Sender == RemoteDriveView)
  7540. {
  7541. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7542. }
  7543. else
  7544. {
  7545. TransferFileName = File->FileName;
  7546. }
  7547. FDDFileList->AddObject(TransferFileName, File);
  7548. }
  7549. //---------------------------------------------------------------------------
  7550. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7551. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7552. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7553. {
  7554. // sometimes we do not get DDEnd so the list is not released
  7555. SAFE_DESTROY(FDDFileList);
  7556. FDDFileList = new TStringList();
  7557. FDragTempDir = WinConfiguration->TemporaryDir();
  7558. FDDTotalSize = 0;
  7559. FDragDropSshTerminate.reset(NULL);
  7560. }
  7561. //---------------------------------------------------------------------------
  7562. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7563. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7564. HRESULT & Result)
  7565. {
  7566. if (Result == DRAGDROP_S_DROP)
  7567. {
  7568. try
  7569. {
  7570. GlobalDragImageList->HideDragImage();
  7571. try
  7572. {
  7573. RemoteFileControlDDTargetDrop();
  7574. }
  7575. catch(ESshTerminate & E)
  7576. {
  7577. FDragDropSshTerminate.reset(E.Clone());
  7578. }
  7579. }
  7580. catch (Exception &E)
  7581. {
  7582. // If downloading fails we need to cancel drag&drop, otherwise
  7583. // Explorer shows error
  7584. // But by the way exception probably never reach this point as
  7585. // it's catched on way.
  7586. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7587. Result = DRAGDROP_S_CANCEL;
  7588. ShowExtendedException(Terminal, &E);
  7589. }
  7590. }
  7591. }
  7592. //---------------------------------------------------------------------------
  7593. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7594. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7595. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7596. {
  7597. TFileMasks::TParams MaskParams;
  7598. MaskParams.Size = Size;
  7599. MaskParams.Modification = Modification;
  7600. // this does not re-parse the mask if it is the same as the last time
  7601. FDirViewMatchMask = Masks;
  7602. bool ImplicitMatch;
  7603. Matches =
  7604. // RecurseInclude parameter has no effect as the Path is empty
  7605. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7606. (AllowImplicitMatches || !ImplicitMatch);
  7607. }
  7608. //---------------------------------------------------------------------------
  7609. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7610. TObject * Sender, TListItem * Item, WORD & Indexes)
  7611. {
  7612. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7613. UnicodeString Ext;
  7614. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7615. {
  7616. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7617. }
  7618. else
  7619. {
  7620. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7621. }
  7622. if (SameText(Ext, Configuration->PartialExt))
  7623. {
  7624. Indexes |= oiPartial;
  7625. }
  7626. }
  7627. //---------------------------------------------------------------------------
  7628. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7629. {
  7630. return
  7631. DirViewEnabled(osCurrent) &&
  7632. DirView(osCurrent)->CanPasteFromClipBoard();
  7633. }
  7634. //---------------------------------------------------------------------------
  7635. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7636. {
  7637. bool Result = false;
  7638. if (CanPasteToDirViewFromClipBoard())
  7639. {
  7640. Result = true;
  7641. }
  7642. else
  7643. {
  7644. UnicodeString ClipboardText;
  7645. if (NonEmptyTextFromClipboard(ClipboardText))
  7646. {
  7647. if (StoredSessions->IsUrl(ClipboardText))
  7648. {
  7649. Result = true;
  7650. }
  7651. else
  7652. {
  7653. Result = DirViewEnabled(osCurrent);
  7654. }
  7655. }
  7656. }
  7657. return Result;
  7658. }
  7659. //---------------------------------------------------------------------------
  7660. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7661. {
  7662. if (DoesClipboardContainOurFiles())
  7663. {
  7664. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7665. {
  7666. DebugAssert(!IsLocalBrowserMode());
  7667. TTerminalManager * Manager = TTerminalManager::Instance();
  7668. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7669. Manager->ActiveTerminal = FClipboardTerminal;
  7670. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7671. }
  7672. }
  7673. else if (CanPasteToDirViewFromClipBoard())
  7674. {
  7675. DirView(osCurrent)->PasteFromClipBoard();
  7676. }
  7677. else
  7678. {
  7679. UnicodeString ClipboardText;
  7680. if (NonEmptyTextFromClipboard(ClipboardText))
  7681. {
  7682. if (StoredSessions->IsUrl(ClipboardText))
  7683. {
  7684. NewSession(false, ClipboardText);
  7685. }
  7686. else
  7687. {
  7688. DirView(osCurrent)->Path = ClipboardText;
  7689. }
  7690. }
  7691. }
  7692. }
  7693. //---------------------------------------------------------------------------
  7694. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7695. {
  7696. // TBD
  7697. }
  7698. //---------------------------------------------------------------------------
  7699. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7700. {
  7701. TCustomDirView * ADirView = DirView(Side);
  7702. ADirView->SelectAll(Mode);
  7703. ADirView->SetFocus();
  7704. }
  7705. //---------------------------------------------------------------------------
  7706. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7707. {
  7708. TCustomDirView * ADirView = DirView(Side);
  7709. TFileFilter Filter;
  7710. DefaultFileFilter(Filter);
  7711. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7712. {
  7713. Configuration->Usage->Inc(L"MaskSelections");
  7714. ADirView->SelectFiles(Filter, Select);
  7715. ADirView->SetFocus();
  7716. }
  7717. }
  7718. //---------------------------------------------------------------------------
  7719. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7720. {
  7721. TCustomDirView * ADirView = DirView(Side);
  7722. ADirView->RestoreSelectedNames();
  7723. ADirView->SetFocus();
  7724. }
  7725. //---------------------------------------------------------------------------
  7726. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7727. {
  7728. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7729. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7730. {
  7731. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7732. UnicodeString Ext;
  7733. if (!IsSideLocalBrowser(osCurrent))
  7734. {
  7735. Ext = UnixExtractFileExt(FileName);
  7736. }
  7737. else
  7738. {
  7739. Ext = ExtractFileExt(FileName);
  7740. }
  7741. if (Ext.IsEmpty())
  7742. {
  7743. Ext = L".";
  7744. }
  7745. TFileFilter Filter;
  7746. Filter.Masks = FORMAT(L"*%s", (Ext));
  7747. Filter.Directories = false;
  7748. CurrentDirView->SelectFiles(Filter, Select);
  7749. }
  7750. }
  7751. //---------------------------------------------------------------------------
  7752. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7753. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7754. {
  7755. UnicodeString Result;
  7756. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7757. {
  7758. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7759. }
  7760. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7761. {
  7762. // noop
  7763. }
  7764. else
  7765. {
  7766. Result =
  7767. FMTLOAD(FILE_INFO_FORMAT,
  7768. (FormatBytes(FileInfo.SelectedSize),
  7769. FormatBytes(FileInfo.FilesSize),
  7770. FormatNumber(FileInfo.SelectedCount),
  7771. FormatNumber(FileInfo.FilesCount)));
  7772. }
  7773. return Result;
  7774. }
  7775. //---------------------------------------------------------------------------
  7776. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7777. TTBXStatusPanel * Panel, TOperationSide Side)
  7778. {
  7779. if (Panel->Index == 1)
  7780. {
  7781. ToggleShowHiddenFiles();
  7782. }
  7783. else if (Panel->Index == 2)
  7784. {
  7785. Filter(Side);
  7786. }
  7787. }
  7788. //---------------------------------------------------------------------------
  7789. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7790. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7791. {
  7792. DebugAssert(!StatusBar->SimplePanel);
  7793. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7794. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7795. }
  7796. //---------------------------------------------------------------------------
  7797. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7798. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7799. {
  7800. DebugAssert(StatusBar->Panels->Count >= 3);
  7801. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7802. if (FileInfo.HiddenCount > 0)
  7803. {
  7804. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7805. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7806. }
  7807. else
  7808. {
  7809. HiddenFilesPanel->ViewPriority = 0;
  7810. // not really necessary, just to cleanup no-longer-valid data
  7811. HiddenFilesPanel->Caption = L"";
  7812. }
  7813. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7814. if (FileInfo.FilteredCount > 0)
  7815. {
  7816. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7817. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7818. }
  7819. else
  7820. {
  7821. FilteredFilesPanel->ViewPriority = 0;
  7822. // not really necessary, just to cleanup no-longer-valid data
  7823. FilteredFilesPanel->Caption = L"";
  7824. }
  7825. }
  7826. //---------------------------------------------------------------------------
  7827. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7828. TObject * /*Sender*/)
  7829. {
  7830. if (DirView(osRemote)->Enabled)
  7831. {
  7832. DirView(osRemote)->SetFocus();
  7833. }
  7834. }
  7835. //---------------------------------------------------------------------------
  7836. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7837. {
  7838. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7839. switch (Config.Show)
  7840. {
  7841. case qvShow:
  7842. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7843. {
  7844. Config.Show = qvHide;
  7845. }
  7846. else
  7847. {
  7848. Config.Show = Config.LastHideShow;
  7849. }
  7850. break;
  7851. case qvHideWhenEmpty:
  7852. if (ComponentVisible[fcQueueView])
  7853. {
  7854. Config.Show = qvHide;
  7855. }
  7856. else
  7857. {
  7858. Config.LastHideShow = Config.Show;
  7859. Config.Show = qvShow;
  7860. }
  7861. break;
  7862. case qvHide:
  7863. Config.LastHideShow = Config.Show;
  7864. Config.Show = qvShow;
  7865. break;
  7866. }
  7867. WinConfiguration->QueueView = Config;
  7868. }
  7869. //---------------------------------------------------------------------------
  7870. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7871. {
  7872. UnicodeString Result;
  7873. if (FTerminal != NULL)
  7874. {
  7875. switch (WinConfiguration->PathInCaption)
  7876. {
  7877. case picShort:
  7878. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7879. break;
  7880. case picFull:
  7881. Result = FTerminal->CurrentDirectory;
  7882. break;
  7883. }
  7884. }
  7885. return Result;
  7886. }
  7887. //---------------------------------------------------------------------------
  7888. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7889. {
  7890. TColor Result = GetWindowColor(FSessionColor);
  7891. return Result;
  7892. }
  7893. //---------------------------------------------------------------------------
  7894. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7895. {
  7896. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7897. return Result;
  7898. }
  7899. //---------------------------------------------------------------------------
  7900. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7901. {
  7902. TTerminalManager::Instance()->UpdateAppTitle();
  7903. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7904. // background to gray on Windows 7 (works on Windows 10).
  7905. // See also EnableControl
  7906. if (Showing)
  7907. {
  7908. if (Terminal != NULL)
  7909. {
  7910. // Update path when it changes
  7911. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7912. {
  7913. UpdateSessionTab(SessionsPageControl->ActivePage);
  7914. }
  7915. }
  7916. NonVisualDataModule->ReconnectSessionAction->Update();
  7917. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  7918. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  7919. CenterReconnectToolbar();
  7920. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7921. if (HasTerminal)
  7922. {
  7923. // TODO needed yet with second local browser?
  7924. if (!RemoteDirView->Enabled)
  7925. {
  7926. RemoteDirView->Enabled = true;
  7927. if (FRemoteDirViewWasFocused)
  7928. {
  7929. ActiveControl = RemoteDirView;
  7930. }
  7931. }
  7932. RemoteDirView->Color = PanelColor();
  7933. }
  7934. else
  7935. {
  7936. if (RemoteDirView->Enabled)
  7937. {
  7938. // This is first called when the form is being constructed
  7939. // (not anymore due to Showing test above)
  7940. // but the false is overriden in the constructor later.
  7941. // An even later in TScpCommanderForm::DoShow()
  7942. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7943. RemoteDirView->Enabled = false;
  7944. RemoteDirView->Color = DisabledPanelColor();
  7945. }
  7946. }
  7947. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  7948. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  7949. RemoteDriveView->Color = RemoteDirView->Color;
  7950. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  7951. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  7952. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  7953. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  7954. QueueLabelUpdateStatus();
  7955. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  7956. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  7957. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7958. }
  7959. }
  7960. //---------------------------------------------------------------------------
  7961. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7962. {
  7963. UpdateControls();
  7964. }
  7965. //---------------------------------------------------------------------------
  7966. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7967. TObject * Sender)
  7968. {
  7969. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7970. DebugAssert(DirView != NULL);
  7971. DoDirViewLoaded(DirView);
  7972. TransferPresetAutoSelect();
  7973. }
  7974. //---------------------------------------------------------------------------
  7975. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7976. {
  7977. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7978. // first run after installation
  7979. if (double(Updates.LastCheck) == 0)
  7980. {
  7981. // make sure next time there will be an update (if enabled)
  7982. Updates.LastCheck = TDateTime(1);
  7983. WinConfiguration->Updates = Updates;
  7984. }
  7985. else if ((double(Updates.Period) > 0) &&
  7986. (Now() - Updates.LastCheck >= Updates.Period))
  7987. {
  7988. TThreadMethod OnUpdatesChecked = NULL;
  7989. if (!IsUWP())
  7990. {
  7991. OnUpdatesChecked = UpdatesChecked;
  7992. }
  7993. StartUpdateThread(OnUpdatesChecked);
  7994. }
  7995. }
  7996. //---------------------------------------------------------------------------
  7997. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7998. {
  7999. UnicodeString Message;
  8000. bool New;
  8001. TQueryType Type;
  8002. GetUpdatesMessage(Message, New, Type, false);
  8003. if (!Message.IsEmpty())
  8004. {
  8005. if (New)
  8006. {
  8007. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8008. }
  8009. if (!New && (Type != qtWarning))
  8010. {
  8011. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8012. }
  8013. else
  8014. {
  8015. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8016. }
  8017. if (New)
  8018. {
  8019. Configuration->Usage->Inc(L"UpdateNotifications");
  8020. }
  8021. }
  8022. }
  8023. //---------------------------------------------------------------------------
  8024. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8025. {
  8026. RestoreApp();
  8027. bool CanDisplay = !NonVisualDataModule->Busy;
  8028. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8029. {
  8030. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8031. }
  8032. if (CanDisplay)
  8033. {
  8034. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8035. CheckForUpdates(true);
  8036. }
  8037. else
  8038. {
  8039. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8040. }
  8041. }
  8042. //---------------------------------------------------------------------------
  8043. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8044. TCopyParamRuleData & Data)
  8045. {
  8046. DebugAssert(Terminal != NULL);
  8047. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8048. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8049. Data.RemoteDirectory = RemoteDirView->PathName;
  8050. }
  8051. //---------------------------------------------------------------------------
  8052. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8053. {
  8054. // Terminal can be null when we are changing local directory implicitly,
  8055. // because it has been deleted, while no session is connected
  8056. // (Login dialog is open)
  8057. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8058. {
  8059. DebugAssert(!IsLocalBrowserMode());
  8060. TCopyParamRuleData Data;
  8061. GetTransferPresetAutoSelectData(Data);
  8062. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8063. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8064. if (CopyParamIndex < 0)
  8065. {
  8066. // there is no preset that matches autoselection
  8067. // set preset that we consider "default"
  8068. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8069. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8070. }
  8071. else
  8072. {
  8073. // there is preset matching autoselection
  8074. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8075. if (CopyParamName == FCopyParamAutoSelected)
  8076. {
  8077. // autoselected the same preset as the last time
  8078. // make no change (i.e. preserve custom user preset, if any)
  8079. }
  8080. else
  8081. {
  8082. // autoselected the different preset then the last time (or there
  8083. // was default preset set)
  8084. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8085. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8086. }
  8087. }
  8088. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8089. {
  8090. if (CopyParamIndex >= 0)
  8091. {
  8092. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8093. }
  8094. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8095. try
  8096. {
  8097. int Fmt =
  8098. (CopyParamIndex < 0) ?
  8099. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8100. COPY_PARAM_AUTOSELECTED;
  8101. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8102. Data->Message = MainInstructions(Message);
  8103. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8104. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8105. CopyParamAttrs);
  8106. if (CopyParamIndex >= 0)
  8107. {
  8108. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8109. Info = FORMAT(L"%s\n \n%s", (Info,
  8110. FMTLOAD(COPY_PARAM_RULE,
  8111. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8112. }
  8113. Data->Message += L"\n\n" + Info;
  8114. if (WinConfiguration->CopyParamAutoSelectNotice)
  8115. {
  8116. TransferPresetNoteMessage(Data, true);
  8117. }
  8118. else
  8119. {
  8120. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8121. Data = NULL; // ownership passed
  8122. }
  8123. }
  8124. __finally
  8125. {
  8126. delete Data;
  8127. }
  8128. }
  8129. }
  8130. }
  8131. //---------------------------------------------------------------------------
  8132. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8133. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8134. {
  8135. DebugAssert(NoteData != NULL);
  8136. DebugAssert(!IsLocalBrowserMode());
  8137. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8138. TQueryButtonAlias Aliases[1];
  8139. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8140. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8141. Params.Aliases = Aliases;
  8142. Params.AliasesCount = LENOF(Aliases);
  8143. unsigned int Result =
  8144. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8145. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8146. switch (Result)
  8147. {
  8148. case qaNeverAskAgain:
  8149. DebugAssert(AllowNeverAskAgain);
  8150. WinConfiguration->CopyParamAutoSelectNotice = false;
  8151. break;
  8152. case qaIgnore:
  8153. PreferencesDialog(pmPresets);
  8154. break;
  8155. }
  8156. }
  8157. //---------------------------------------------------------------------------
  8158. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8159. {
  8160. // as of now useless, as this is used for notes only, never for balloons, ...
  8161. RestoreApp();
  8162. // .. and we should never be busy here
  8163. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8164. {
  8165. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8166. }
  8167. }
  8168. //---------------------------------------------------------------------------
  8169. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8170. TPreferencesMode APreferencesMode)
  8171. {
  8172. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8173. TCopyParamRuleData Data;
  8174. if (Terminal != NULL)
  8175. {
  8176. PreferencesData.reset(new TPreferencesDialogData());
  8177. GetTransferPresetAutoSelectData(Data);
  8178. PreferencesData->CopyParamRuleData = &Data;
  8179. }
  8180. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8181. }
  8182. //---------------------------------------------------------------------------
  8183. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8184. const TCustomCommandType & Command)
  8185. {
  8186. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8187. {
  8188. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8189. }
  8190. }
  8191. //---------------------------------------------------------------------------
  8192. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8193. {
  8194. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8195. TCustomCommandType Command;
  8196. // make sure we use local custom command when remote are not supported
  8197. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8198. {
  8199. Command = FLastCustomCommand;
  8200. }
  8201. else
  8202. {
  8203. Command.Params = Command.Params | ccLocal;
  8204. }
  8205. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8206. FEditingFocusedAdHocCommand = OnFocused;
  8207. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8208. int Options =
  8209. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8210. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8211. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8212. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8213. {
  8214. FLastCustomCommand = Command;
  8215. UpdateCustomCommandsToolbar();
  8216. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8217. }
  8218. }
  8219. //---------------------------------------------------------------------------
  8220. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8221. {
  8222. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8223. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8224. DebugAssert(State > 0);
  8225. if (State <= 0)
  8226. {
  8227. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8228. }
  8229. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8230. }
  8231. //---------------------------------------------------------------------------
  8232. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8233. TCustomCommandType & Command, int & State)
  8234. {
  8235. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8236. if (Result)
  8237. {
  8238. Command = FLastCustomCommand;
  8239. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8240. }
  8241. return Result;
  8242. }
  8243. //---------------------------------------------------------------------------
  8244. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8245. {
  8246. ResetIncrementalSearch();
  8247. }
  8248. //---------------------------------------------------------------------------
  8249. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8250. {
  8251. DebugAssert(ComponentVisible[Component]);
  8252. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8253. }
  8254. //---------------------------------------------------------------------------
  8255. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8256. {
  8257. // when queue panel is resized here directly, the status bar is stretched
  8258. // over whole space the panel occupied
  8259. PostComponentHide(fcQueueView);
  8260. }
  8261. //---------------------------------------------------------------------------
  8262. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8263. {
  8264. WinConfiguration->ResetSysDarkTheme();
  8265. ConfigurationChanged();
  8266. ConfigureInterface();
  8267. TForm::Dispatch(&Message);
  8268. }
  8269. //---------------------------------------------------------------------------
  8270. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8271. {
  8272. TMessage * M = static_cast<TMessage*>(Message);
  8273. switch (M->Msg)
  8274. {
  8275. case CM_DIALOGCHAR:
  8276. CMDialogChar(*M);
  8277. break;
  8278. case WM_APPCOMMAND:
  8279. WMAppCommand(*M);
  8280. break;
  8281. case WM_SYSCOMMAND:
  8282. WMSysCommand(*M);
  8283. break;
  8284. case WM_QUERYENDSESSION:
  8285. WMQueryEndSession(*M);
  8286. break;
  8287. case WM_ENDSESSION:
  8288. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8289. break;
  8290. #ifdef _DEBUG
  8291. case WM_WINDOWPOSCHANGED:
  8292. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8293. break;
  8294. case CM_CUSTOMSTYLECHANGED:
  8295. TForm::Dispatch(Message);
  8296. break;
  8297. #endif
  8298. case WM_COMPONENT_HIDE:
  8299. {
  8300. Byte Component = static_cast<Byte>(M->WParam);
  8301. // sanity check
  8302. if (ComponentVisible[Component])
  8303. {
  8304. // special treatment
  8305. if (Component == fcQueueView)
  8306. {
  8307. ToggleQueueVisibility();
  8308. DebugAssert(!ComponentVisible[fcQueueView]);
  8309. }
  8310. else
  8311. {
  8312. ComponentVisible[Component] = false;
  8313. }
  8314. }
  8315. }
  8316. break;
  8317. case WM_COPYDATA:
  8318. WMCopyData(*M);
  8319. break;
  8320. case WM_MANAGES_CAPTION:
  8321. // caption managed in TTerminalManager::UpdateAppTitle()
  8322. M->Result = 1;
  8323. break;
  8324. case WM_WANTS_MOUSEWHEEL:
  8325. M->Result = 1;
  8326. break;
  8327. case CM_SHOWINGCHANGED:
  8328. CMShowingChanged(*M);
  8329. break;
  8330. case WM_CLOSE:
  8331. WMClose(*M);
  8332. break;
  8333. case WM_DPICHANGED:
  8334. WMDpiChanged(*M);
  8335. break;
  8336. case CM_DPICHANGED:
  8337. CMDpiChanged(*M);
  8338. break;
  8339. case WM_WININICHANGE:
  8340. WMWinIniChange(*M);
  8341. break;
  8342. case CM_DIALOGKEY:
  8343. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8344. break;
  8345. default:
  8346. TForm::Dispatch(Message);
  8347. break;
  8348. }
  8349. }
  8350. //---------------------------------------------------------------------------
  8351. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8352. {
  8353. // noop
  8354. }
  8355. //---------------------------------------------------------------------------
  8356. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8357. {
  8358. TForm::Dispatch(&Message);
  8359. FSessionColors->Clear();
  8360. AddFixedSessionImages();
  8361. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8362. UpdateImages();
  8363. }
  8364. //---------------------------------------------------------------------------
  8365. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8366. {
  8367. TForm::Dispatch(&Message);
  8368. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8369. UpdatePixelsPerInchMainWindowCounter();
  8370. }
  8371. //---------------------------------------------------------------------------
  8372. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8373. {
  8374. // Cannot close window while we are busy.
  8375. // We cannot test this in FormCloseQuery as that is called also from
  8376. // Close(), which is called by CloseApplicationAction. So we can be busy
  8377. // there already even, when it is legitimate to close the application.
  8378. // Possibly a better place to handle this would be WMSysCommand.
  8379. if (NonVisualDataModule->Busy)
  8380. {
  8381. Message.Result = 1;
  8382. }
  8383. else
  8384. {
  8385. TForm::Dispatch(&Message);
  8386. }
  8387. }
  8388. //---------------------------------------------------------------------------
  8389. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8390. {
  8391. TForm::Dispatch(&Message);
  8392. if (Showing && (Terminal == NULL))
  8393. {
  8394. // When we are starting minimized (i.e. from an installer),
  8395. // postpone showing Login dialog until we get restored.
  8396. // Otherwise the Login dialog (and Authentication window) show restored
  8397. // over invidible (minimized) main window.
  8398. if (WindowState == wsMinimized)
  8399. {
  8400. FNeedSession = true;
  8401. }
  8402. else
  8403. {
  8404. // This happens before application ever goes idle, so the toolbars would
  8405. // stay enabled (initial state) until the Login dialog is dismissed.
  8406. UpdateActions();
  8407. NonVisualDataModule->StartBusy();
  8408. try
  8409. {
  8410. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8411. // otherwise the dialog does not receive focus.
  8412. // With Commander interface the ProcessMessages is called already
  8413. // by TDriveView, but with Explorer interface, we need to call it explicily
  8414. Application->ProcessMessages();
  8415. // do not reload sessions, they have been loaded just now (optimization)
  8416. NeedSession(false);
  8417. }
  8418. __finally
  8419. {
  8420. NonVisualDataModule->EndBusy();
  8421. }
  8422. }
  8423. }
  8424. }
  8425. //---------------------------------------------------------------------------
  8426. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8427. {
  8428. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8429. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8430. }
  8431. //---------------------------------------------------------------------------
  8432. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8433. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8434. int & MaxHeight)
  8435. {
  8436. // workaround for bug in TWinControl.CalcConstraints
  8437. // Check for empty rect (restore from iconic state) is done there only after
  8438. // call to AdjustClientRect, which enlarges the rect (for forms).
  8439. TRect R = GetClientRect();
  8440. // when restoring from iconic state, place no restrictions
  8441. if (IsRectEmpty(R))
  8442. {
  8443. MinWidth = 0;
  8444. MinHeight = 0;
  8445. MaxWidth = 32000;
  8446. MaxHeight = 32000;
  8447. }
  8448. }
  8449. //---------------------------------------------------------------------------
  8450. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8451. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8452. {
  8453. // terminal can be already closed (e.g. dropped connection)
  8454. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8455. {
  8456. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8457. try
  8458. {
  8459. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8460. }
  8461. catch(...)
  8462. {
  8463. if (!Terminal->Active)
  8464. {
  8465. Close = true;
  8466. }
  8467. throw;
  8468. }
  8469. }
  8470. }
  8471. //---------------------------------------------------------------------------
  8472. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8473. {
  8474. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8475. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8476. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8477. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8478. {
  8479. OnGetSpaceAvailable = GetSpaceAvailable;
  8480. }
  8481. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8482. OnGetSpaceAvailable);
  8483. }
  8484. //---------------------------------------------------------------------------
  8485. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8486. {
  8487. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8488. Terminal->FillSessionDataForCode(Data.get());
  8489. return Data.release();
  8490. }
  8491. //---------------------------------------------------------------------------
  8492. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8493. {
  8494. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8495. DoGenerateUrlDialog(Data.get(), Paths);
  8496. }
  8497. //---------------------------------------------------------------------------
  8498. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8499. {
  8500. GenerateUrl(NULL);
  8501. }
  8502. //---------------------------------------------------------------------------
  8503. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8504. {
  8505. std::unique_ptr<TStrings> Paths(new TStringList());
  8506. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8507. GenerateUrl(Paths.get());
  8508. }
  8509. //---------------------------------------------------------------------------
  8510. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8511. {
  8512. FSessionColor = value;
  8513. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8514. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8515. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8516. DebugAssert(ColorItem != NULL);
  8517. ColorItem->Color = C;
  8518. NonVisualDataModule->ColorMenuItem->Color = C;
  8519. UpdateControls();
  8520. }
  8521. //---------------------------------------------------------------------------
  8522. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8523. {
  8524. if (value != FSessionColor)
  8525. {
  8526. UpdateSessionColor(value);
  8527. // Is null when called from LastTerminalClosed
  8528. if (Terminal != NULL)
  8529. {
  8530. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8531. }
  8532. }
  8533. }
  8534. //---------------------------------------------------------------------------
  8535. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8536. {
  8537. bool Result = FNoteTimer->Enabled;
  8538. if (Result)
  8539. {
  8540. // cannot cancel note too early
  8541. bool NotEarly =
  8542. (Now() - FNoteShown >
  8543. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8544. if (Force || NotEarly)
  8545. {
  8546. FNoteTimer->Enabled = false;
  8547. FNote = L"";
  8548. SAFE_DESTROY(FNoteData);
  8549. FOnNoteClick = NULL;
  8550. FNoteHints = FNotes->Text;
  8551. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8552. UpdateStatusBar();
  8553. }
  8554. }
  8555. return Result;
  8556. }
  8557. //---------------------------------------------------------------------------
  8558. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8559. {
  8560. DebugAssert(FNoteTimer->Enabled);
  8561. CancelNote(true);
  8562. }
  8563. //---------------------------------------------------------------------------
  8564. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8565. {
  8566. int P = Note.Pos(L"\n");
  8567. if (P > 0)
  8568. {
  8569. Note.SetLength(P - 1);
  8570. }
  8571. FNotes->Add(FORMAT(L"[%s] %s",
  8572. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8573. while (FNotes->Count > 10)
  8574. {
  8575. FNotes->Delete(0);
  8576. }
  8577. if (UpdateNow)
  8578. {
  8579. FNoteHints = FNotes->Text;
  8580. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8581. UpdateStatusBar();
  8582. }
  8583. }
  8584. //---------------------------------------------------------------------------
  8585. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8586. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8587. {
  8588. int P = Note.Pos(L"\n");
  8589. if (P > 0)
  8590. {
  8591. Note.SetLength(P - 1);
  8592. }
  8593. FNoteHints = FNotes->Text;
  8594. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8595. FNote = Note;
  8596. SAFE_DESTROY(FNoteData);
  8597. FNoteData = NoteData;
  8598. FOnNoteClick = OnNoteClick;
  8599. AddNote(Note, false);
  8600. UpdateStatusBar();
  8601. FNoteShown = Now();
  8602. FNoteTimer->Enabled = false;
  8603. if (Seconds == 0)
  8604. {
  8605. Seconds = WinConfiguration->NotificationsTimeout;
  8606. }
  8607. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8608. FNoteTimer->Enabled = true;
  8609. }
  8610. //---------------------------------------------------------------------------
  8611. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8612. {
  8613. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8614. }
  8615. //---------------------------------------------------------------------------
  8616. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8617. {
  8618. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8619. {
  8620. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8621. }
  8622. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8623. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8624. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8625. 0, NULL, NULL);
  8626. }
  8627. //---------------------------------------------------------------------------
  8628. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8629. {
  8630. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8631. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8632. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8633. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8634. }
  8635. //---------------------------------------------------------------------------
  8636. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8637. {
  8638. WinConfiguration->FormatSizeBytes = Style;
  8639. }
  8640. //---------------------------------------------------------------------------
  8641. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8642. {
  8643. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8644. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8645. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8646. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8647. }
  8648. //---------------------------------------------------------------------------
  8649. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8650. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8651. {
  8652. if (Panel->Index == 0)
  8653. {
  8654. if (FOnNoteClick != NULL)
  8655. {
  8656. // prevent the user data from being freed by possible call
  8657. // to CancelNote or PostNote during call to OnNoteClick
  8658. std::unique_ptr<TObject> NoteData(FNoteData);
  8659. TNotifyEvent OnNoteClick = FOnNoteClick;
  8660. FNoteData = NULL;
  8661. // need to cancel the note as we are going to delete its user data
  8662. CancelNote(true);
  8663. OnNoteClick(NoteData.get());
  8664. }
  8665. }
  8666. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8667. {
  8668. FileSystemInfo();
  8669. }
  8670. }
  8671. //---------------------------------------------------------------------------
  8672. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8673. {
  8674. // workaround:
  8675. // 1) for unknown reason, disabling window, while minimized,
  8676. // prevents it from restoring, even if it was enabled again meanwhile
  8677. // 2) when disabling the main window, while another has focus
  8678. // minimize is no longer possible
  8679. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8680. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8681. // Shouldn't we use IsApplicationMinimized() here?
  8682. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8683. {
  8684. Enabled = false;
  8685. }
  8686. FLockLevel++;
  8687. }
  8688. //---------------------------------------------------------------------------
  8689. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8690. {
  8691. DebugAssert(FLockLevel > 0);
  8692. FLockLevel--;
  8693. if (FLockLevel == 0)
  8694. {
  8695. DebugAssert(FLockSuspendLevel == 0);
  8696. Enabled = true;
  8697. }
  8698. }
  8699. //---------------------------------------------------------------------------
  8700. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8701. {
  8702. // We need to make sure that window is enabled when the last modal window closes
  8703. // otherwise focus is not restored correctly.
  8704. // So we re-enable the window when modal window opens and
  8705. // disable it back after is closes
  8706. if (FLockLevel > 0)
  8707. {
  8708. // while we have nesting counter, we know that we never be called
  8709. // recursivelly as Application->OnModalBegin is called only
  8710. // for the top-level modal window
  8711. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8712. {
  8713. // won't be disabled when conditions in LockWindow() were not satisfied
  8714. FDisabledOnLockSuspend = !Enabled;
  8715. // When minimized to tray (or actually when set to SW_HIDE),
  8716. // setting Enabled makes the window focusable even when there's
  8717. // modal window over it
  8718. if (!FTrayIcon->Visible)
  8719. {
  8720. Enabled = true;
  8721. }
  8722. }
  8723. FLockSuspendLevel++;
  8724. }
  8725. }
  8726. //---------------------------------------------------------------------------
  8727. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8728. {
  8729. if (FLockSuspendLevel > 0)
  8730. {
  8731. DebugAssert(FLockLevel > 0);
  8732. FLockSuspendLevel--;
  8733. // see comment in SuspendWindowLock
  8734. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8735. {
  8736. // Note that window can be enabled here, when we were minized to tray when
  8737. // was SuspendWindowLock() called.
  8738. // We should possibly do the same check as in LockWindow(),
  8739. // if it is ever possible that the consitions change between
  8740. // SuspendWindowLock() and ResumeWindowLock()
  8741. Enabled = !FDisabledOnLockSuspend;
  8742. }
  8743. }
  8744. }
  8745. //---------------------------------------------------------------------------
  8746. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8747. {
  8748. if (!TextOnly)
  8749. {
  8750. TTBXComboBoxItem * RemotePathComboBox =
  8751. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8752. TStrings * Items = RemotePathComboBox->Strings;
  8753. Items->BeginUpdate();
  8754. try
  8755. {
  8756. Items->Clear();
  8757. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8758. {
  8759. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8760. while (!IsUnixRootPath(APath))
  8761. {
  8762. int P = APath.LastDelimiter(L'/');
  8763. DebugAssert(P >= 0);
  8764. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8765. APath.SetLength(P - 1);
  8766. }
  8767. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8768. }
  8769. }
  8770. __finally
  8771. {
  8772. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8773. // Setting ItemIndex to -1 does not reset its text
  8774. if (Items->Count == 0)
  8775. {
  8776. RemotePathComboBox->Text = L"";
  8777. }
  8778. Items->EndUpdate();
  8779. }
  8780. }
  8781. }
  8782. //---------------------------------------------------------------------------
  8783. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8784. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8785. int & ImageIndex)
  8786. {
  8787. if (AIndex < 0)
  8788. {
  8789. AIndex = Sender->ItemIndex;
  8790. }
  8791. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8792. }
  8793. //---------------------------------------------------------------------------
  8794. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8795. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8796. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8797. {
  8798. ARect.Left += (10 * AIndex);
  8799. }
  8800. //---------------------------------------------------------------------------
  8801. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8802. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8803. {
  8804. AWidth += (10 * AIndex);
  8805. }
  8806. //---------------------------------------------------------------------------
  8807. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8808. TObject * Sender)
  8809. {
  8810. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8811. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8812. int Index = RemotePathComboBox->ItemIndex;
  8813. while (Index < RemotePathComboBox->Strings->Count - 1)
  8814. {
  8815. APath = UnixExtractFileDir(APath);
  8816. Index++;
  8817. }
  8818. // VanDyke style paths
  8819. if (APath.IsEmpty())
  8820. {
  8821. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8822. APath = ROOTDIRECTORY;
  8823. }
  8824. if (RemoteDirView->Path != APath)
  8825. {
  8826. RemoteDirView->Path = APath;
  8827. }
  8828. }
  8829. //---------------------------------------------------------------------------
  8830. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8831. {
  8832. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8833. DebugUsedParam(Sender);
  8834. UpdateRemotePathComboBox(true);
  8835. }
  8836. //---------------------------------------------------------------------------
  8837. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8838. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8839. {
  8840. ResetIncrementalSearch();
  8841. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8842. DebugAssert(DirView != NULL);
  8843. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8844. {
  8845. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8846. // OnEditing is called also from TCustomListView::CanEdit
  8847. if (Edit != NULL)
  8848. {
  8849. EditSelectBaseName(Edit);
  8850. }
  8851. }
  8852. }
  8853. //---------------------------------------------------------------------------
  8854. //---------------------------------------------------------------------------
  8855. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8856. {
  8857. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8858. Result->AutoDetectDnD = false;
  8859. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8860. Result->RenderDataOn = rdoEnterAndDropSync;
  8861. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8862. return Result;
  8863. }
  8864. //---------------------------------------------------------------------------
  8865. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8866. {
  8867. TForm::CreateWnd();
  8868. if (FSessionsDragDropFilesEx == NULL)
  8869. {
  8870. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8871. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8872. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8873. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8874. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8875. }
  8876. if (FQueueDragDropFilesEx == NULL)
  8877. {
  8878. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8879. // No need to set OnDragOver as we do not have any restrictions
  8880. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8881. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8882. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8883. }
  8884. }
  8885. //---------------------------------------------------------------------------
  8886. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8887. {
  8888. TForm::DestroyWnd();
  8889. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8890. FQueueDragDropFilesEx->DragDropControl = NULL;
  8891. }
  8892. //---------------------------------------------------------------------------
  8893. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8894. {
  8895. SideEnter(FCurrentSide);
  8896. FEverShown = true;
  8897. }
  8898. //---------------------------------------------------------------------------
  8899. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8900. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8901. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8902. {
  8903. if (!NonVisualDataModule->Busy)
  8904. {
  8905. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8906. Configuration->Usage->Inc(L"FileFinds");
  8907. LockWindow(true);
  8908. NonVisualDataModule->StartBusy();
  8909. WinConfiguration->LockedInterface = true;
  8910. try
  8911. {
  8912. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8913. }
  8914. __finally
  8915. {
  8916. WinConfiguration->LockedInterface = false;
  8917. NonVisualDataModule->EndBusy();
  8918. UnlockWindow();
  8919. }
  8920. }
  8921. }
  8922. //---------------------------------------------------------------------------
  8923. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8924. {
  8925. if (!NonVisualDataModule->Busy)
  8926. {
  8927. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8928. SetFocus();
  8929. RemoteDirView->Path = UnixExtractFilePath(Path);
  8930. UnicodeString FileName = UnixExtractFileName(Path);
  8931. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  8932. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  8933. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  8934. if (Item == NULL)
  8935. {
  8936. ReloadDirectory(osRemote);
  8937. Item = RemoteDirView->FindFileItem(FileName);
  8938. }
  8939. if (Item != NULL)
  8940. {
  8941. RemoteDirView->ItemFocused = Item;
  8942. RemoteDirView->ItemFocused->MakeVisible(false);
  8943. RemoteDirView->SetFocus();
  8944. }
  8945. }
  8946. }
  8947. //---------------------------------------------------------------------------
  8948. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8949. {
  8950. bool Result = !NonVisualDataModule->Busy;
  8951. if (Result)
  8952. {
  8953. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8954. }
  8955. return Result;
  8956. }
  8957. //---------------------------------------------------------------------------
  8958. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8959. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8960. {
  8961. if (CanOperateOnFoundFiles(ATerminal))
  8962. {
  8963. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8964. FOnFileOperationFinished = OnFileOperationFinished;
  8965. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8966. }
  8967. }
  8968. //---------------------------------------------------------------------------
  8969. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8970. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8971. {
  8972. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8973. }
  8974. //---------------------------------------------------------------------------
  8975. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8976. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8977. {
  8978. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8979. }
  8980. //---------------------------------------------------------------------------
  8981. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8982. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8983. {
  8984. if (CanOperateOnFoundFiles(ATerminal))
  8985. {
  8986. for (int Index = 0; Index < FileList->Count; Index++)
  8987. {
  8988. UnicodeString FileName = FileList->Strings[Index];
  8989. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8990. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8991. OnFileOperationFinished(FileName, true);
  8992. }
  8993. }
  8994. }
  8995. //---------------------------------------------------------------------------
  8996. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8997. {
  8998. FFileFindTerminal = Terminal;
  8999. ShowFileFindDialog(
  9000. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9001. DoDownloadFoundFiles, DoEditFoundFiles);
  9002. }
  9003. //---------------------------------------------------------------------------
  9004. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9005. {
  9006. FTaskbarList = TaskbarList;
  9007. }
  9008. //---------------------------------------------------------------------------
  9009. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9010. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9011. {
  9012. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9013. if (Index >= 0)
  9014. {
  9015. if (Button == mbLeft)
  9016. {
  9017. // "Mouse down" is raised only after tab is switched.
  9018. // If switching tab (switching session) takes long enough for user
  9019. // to actually release the button, "mouse down" is still raised,
  9020. // but we do not get "mouse up" event, so dragging is not cancelled,
  9021. // prevent that by not beginning dragging in the first place.
  9022. if (IsKeyPressed(VK_LBUTTON))
  9023. {
  9024. // when user clicks the "+", we get mouse down only after the session
  9025. // is closed, when new session tab is already on X:Y, so dragging
  9026. // starts, prevent that
  9027. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9028. {
  9029. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9030. if (Terminal != NULL)
  9031. {
  9032. SessionsPageControl->BeginDrag(false);
  9033. }
  9034. }
  9035. }
  9036. }
  9037. else if (Button == mbMiddle)
  9038. {
  9039. // ignore middle-click for "New session tab"
  9040. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9041. if (Terminal != NULL)
  9042. {
  9043. CloseSessionTab(Index);
  9044. }
  9045. }
  9046. }
  9047. }
  9048. //---------------------------------------------------------------------------
  9049. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9050. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9051. {
  9052. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9053. // do not allow dropping on the "+" tab
  9054. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9055. if ((TargetTerminal != NULL) &&
  9056. (SessionsPageControl->ActivePage->PageIndex != Index))
  9057. {
  9058. Configuration->Usage->Inc(L"SessionTabMoves");
  9059. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9060. // but we want to actually prevent that to avoid flicker
  9061. SessionsPageControl->ActivePage->PageIndex = Index;
  9062. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9063. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9064. }
  9065. }
  9066. //---------------------------------------------------------------------------
  9067. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9068. TObject * Sender, TObject * Source, int X, int Y,
  9069. TDragState /*State*/, bool & Accept)
  9070. {
  9071. Accept = (Sender == Source);
  9072. if (Accept)
  9073. {
  9074. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9075. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9076. // do not allow dragging to the "+" tab
  9077. Accept = (Terminal != NULL);
  9078. }
  9079. }
  9080. //---------------------------------------------------------------------------
  9081. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9082. const TPoint & Point, int & Effect)
  9083. {
  9084. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9085. if (Index < 0)
  9086. {
  9087. Effect = DROPEFFECT_NONE;
  9088. }
  9089. else
  9090. {
  9091. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9092. // do not allow dropping on the "+" tab
  9093. if (TargetTerminal == NULL)
  9094. {
  9095. Effect = DROPEFFECT_NONE;
  9096. }
  9097. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9098. {
  9099. Effect = DROPEFFECT_COPY;
  9100. }
  9101. }
  9102. }
  9103. //---------------------------------------------------------------------------
  9104. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9105. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9106. {
  9107. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9108. // do not allow dropping on the "+" tab
  9109. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9110. if (TargetTerminal != NULL)
  9111. {
  9112. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9113. if (!IsFileControl(DropSourceControl, osRemote))
  9114. {
  9115. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9116. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9117. // Why don't we use Terminal->CurrentDirectory directly?
  9118. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9119. // do not force queue, drag drop
  9120. false, true);
  9121. }
  9122. }
  9123. }
  9124. //---------------------------------------------------------------------------
  9125. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9126. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9127. {
  9128. // Downloads are handled in RemoteFileControlDDEnd
  9129. if (!IsFileControl(DropSourceControl, osRemote))
  9130. {
  9131. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9132. Terminal->CurrentDirectory,
  9133. // force queue, drag drop
  9134. true, true);
  9135. }
  9136. }
  9137. //---------------------------------------------------------------------------
  9138. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9139. {
  9140. FShowing = false;
  9141. // Do not save empty workspace
  9142. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9143. {
  9144. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9145. UnicodeString Name = WorkspaceName();
  9146. DoSaveWorkspace(
  9147. Name, DataList.get(),
  9148. !Configuration->DisablePasswordStoring &&
  9149. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9150. WinConfiguration->LastStoredSession = Name;
  9151. }
  9152. }
  9153. //---------------------------------------------------------------------------
  9154. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9155. {
  9156. TManagedTerminal * ManagedTerminal =
  9157. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9158. if (ManagedTerminal != NULL)
  9159. {
  9160. ManagedTerminal->DirectoryLoaded = Now();
  9161. }
  9162. }
  9163. //---------------------------------------------------------------------------
  9164. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9165. TListItem * /*Item*/, bool /*Selected*/)
  9166. {
  9167. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9168. switch (DirView->LastSelectMethod)
  9169. {
  9170. case smKeyboard:
  9171. Configuration->Usage->Inc(L"KeyboardSelections");
  9172. break;
  9173. case smMouse:
  9174. Configuration->Usage->Inc(L"MouseSelections");
  9175. break;
  9176. }
  9177. }
  9178. //---------------------------------------------------------------------------
  9179. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9180. {
  9181. if (Color != 0)
  9182. {
  9183. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9184. }
  9185. else
  9186. {
  9187. return FSessionTabImageIndex;
  9188. }
  9189. }
  9190. //---------------------------------------------------------------------------
  9191. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9192. {
  9193. TPngImageCollectionItem * Item =
  9194. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9195. FSessionColors->AddPng(Item->PngImage);
  9196. return FSessionColors->Count - 1;
  9197. }
  9198. //---------------------------------------------------------------------------
  9199. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9200. {
  9201. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9202. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9203. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9204. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9205. }
  9206. //---------------------------------------------------------------------------
  9207. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9208. {
  9209. for (int Index = 0; Index < Control->ControlCount; Index++)
  9210. {
  9211. TControl * AControl = Control->Controls[Index];
  9212. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9213. if (WinControl != NULL)
  9214. {
  9215. CollectItemsWithTextDisplayMode(WinControl);
  9216. }
  9217. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9218. if (Toolbar != NULL)
  9219. {
  9220. // we care for top-level items only
  9221. TTBCustomItem * Items = Toolbar->Items;
  9222. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9223. {
  9224. TTBCustomItem * Item = Items->Items[ItemIndex];
  9225. if (((Item->DisplayMode == nbdmImageAndText) ||
  9226. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9227. EligibleForImageDisplayMode(Item))
  9228. {
  9229. FItemsWithTextDisplayMode.insert(Item);
  9230. }
  9231. }
  9232. }
  9233. }
  9234. }
  9235. //---------------------------------------------------------------------------
  9236. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9237. {
  9238. return true;
  9239. }
  9240. //---------------------------------------------------------------------------
  9241. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9242. {
  9243. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9244. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9245. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9246. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9247. bool Result = true;
  9248. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9249. {
  9250. TTBCustomItem * Item = *i;
  9251. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9252. if (Label != NULL)
  9253. {
  9254. // optimization
  9255. if (Label->Visible == SelectiveToolbarText)
  9256. {
  9257. Result = false;
  9258. }
  9259. else
  9260. {
  9261. Label->Visible = SelectiveToolbarText;
  9262. }
  9263. }
  9264. else
  9265. {
  9266. // optimization
  9267. if (Item->DisplayMode == DisplayMode)
  9268. {
  9269. Result = false;
  9270. }
  9271. else
  9272. {
  9273. Item->DisplayMode = DisplayMode;
  9274. }
  9275. }
  9276. ++i;
  9277. }
  9278. if (Result)
  9279. {
  9280. UpdateNewSessionTab();
  9281. }
  9282. return Result;
  9283. }
  9284. //---------------------------------------------------------------------------
  9285. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9286. {
  9287. DebugFail();
  9288. }
  9289. //---------------------------------------------------------------------------
  9290. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9291. {
  9292. // Among other, a lock level is non zero, while directory is being loaded,
  9293. // even when it happens as a result of dirview navigation,
  9294. // i.e. when TNonVisualDataModule is NOT FBusy.
  9295. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9296. // Among other this prevents a panel auto update to occur while
  9297. // directory is changing.
  9298. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9299. }
  9300. //---------------------------------------------------------------------------
  9301. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9302. {
  9303. // While the window is disabled, we still seem to process menu shortcuts at least,
  9304. // so stop it at least here.
  9305. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9306. return
  9307. (Allowed == aaUpdate) ||
  9308. !NonVisualDataModule->Busy;
  9309. }
  9310. //---------------------------------------------------------------------------
  9311. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9312. {
  9313. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9314. }
  9315. //---------------------------------------------------------------------------
  9316. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9317. {
  9318. // This is somewhat redundant to LockWindow() call from
  9319. // TTerminalManager::TerminalReadDirectoryProgress.
  9320. // But disabling window is known not to block keyboard shorcuts
  9321. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9322. if (Busy > 0)
  9323. {
  9324. if (NonVisualDataModule->Busy)
  9325. {
  9326. State = false;
  9327. }
  9328. else
  9329. {
  9330. NonVisualDataModule->StartBusy();
  9331. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9332. LockWindow();
  9333. }
  9334. }
  9335. else if (Busy < 0)
  9336. {
  9337. UnlockWindow();
  9338. NonVisualDataModule->EndBusy();
  9339. }
  9340. else
  9341. {
  9342. State = NonVisualDataModule->Busy;
  9343. }
  9344. }
  9345. //---------------------------------------------------------------------------
  9346. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9347. {
  9348. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9349. // no context menu for "New session tab"
  9350. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9351. {
  9352. SessionsPageControl->ActivePageIndex = Index;
  9353. if (DebugAlwaysTrue(SessionTabSwitched()))
  9354. {
  9355. // copied from TControl.WMContextMenu
  9356. SendCancelMode(SessionsPageControl);
  9357. // explicit popup instead of using PopupMenu property
  9358. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9359. // while connecting yet not-connected session and hence
  9360. // allowing an access to commands over not-completelly connected session
  9361. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9362. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9363. PopupMenu->PopupComponent = SessionsPageControl;
  9364. PopupMenu->Popup(Point.x, Point.y);
  9365. }
  9366. }
  9367. Handled = true;
  9368. }
  9369. //---------------------------------------------------------------------------
  9370. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9371. {
  9372. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9373. }
  9374. //---------------------------------------------------------------------------
  9375. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9376. {
  9377. Configuration->Usage->Inc(L"PasswordChanges");
  9378. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9379. ChangePasswordData->DisableAuthentationsExceptPassword();
  9380. ChangePasswordData->ChangePassword = true;
  9381. std::unique_ptr<TTerminal> ChangePasswordSession(
  9382. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9383. ChangePasswordSession->AutoReadDirectory = false;
  9384. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9385. {
  9386. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9387. }
  9388. else
  9389. {
  9390. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9391. }
  9392. }
  9393. //---------------------------------------------------------------------------
  9394. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9395. {
  9396. // No nice way to assert SSH2
  9397. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9398. }
  9399. //---------------------------------------------------------------------------
  9400. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9401. {
  9402. TOperationVisualizer OperationVisualizer;
  9403. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9404. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9405. }
  9406. //---------------------------------------------------------------------------
  9407. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9408. {
  9409. TForm::ChangeScale(M, D);
  9410. int APixelsPerInch = GetControlPixelsPerInch(this);
  9411. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9412. try
  9413. {
  9414. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9415. }
  9416. __finally
  9417. {
  9418. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9419. }
  9420. }
  9421. //---------------------------------------------------------------------------
  9422. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9423. {
  9424. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9425. }
  9426. //---------------------------------------------------------------------
  9427. class TPublicControl : public TControl
  9428. {
  9429. friend class TCustomScpExplorerForm;
  9430. };
  9431. //---------------------------------------------------------------------------
  9432. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9433. {
  9434. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9435. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9436. }
  9437. //---------------------------------------------------------------------------
  9438. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9439. {
  9440. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9441. OpenBookmark(Side, Bookmark);
  9442. }
  9443. //---------------------------------------------------------------------------
  9444. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9445. {
  9446. DoBookmarkClick(osLocal, Sender);
  9447. }
  9448. //---------------------------------------------------------------------------
  9449. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9450. {
  9451. DoBookmarkClick(osRemote, Sender);
  9452. }
  9453. //---------------------------------------------------------------------------
  9454. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9455. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9456. {
  9457. // TODO
  9458. if (BookmarkList != NULL)
  9459. {
  9460. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9461. if (!WinConfiguration->UseLocationProfiles)
  9462. {
  9463. std::unique_ptr<TStringList> Directories(new TStringList());
  9464. Directories->CaseSensitive = (Side == osRemote);
  9465. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9466. {
  9467. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9468. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9469. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9470. {
  9471. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9472. Item->Caption = EscapeHotkey(Directory);
  9473. Item->ShortCut = Bookmark->ShortCut;
  9474. Item->OnClick = OnBookmarkClick;
  9475. Item->Tag = reinterpret_cast<int>(Bookmark);
  9476. Directories->Add(Directory);
  9477. Menu->Add(Item.release());
  9478. }
  9479. }
  9480. }
  9481. else
  9482. {
  9483. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9484. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9485. {
  9486. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9487. if (!Bookmark->Node.IsEmpty())
  9488. {
  9489. Folders->Add(Bookmark->Node);
  9490. }
  9491. }
  9492. for (int Index = 0; Index < Folders->Count; Index++)
  9493. {
  9494. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9495. Item->Caption = Folders->Strings[Index];
  9496. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9497. Folders->Objects[Index] = Item.get();
  9498. Menu->Add(Item.release());
  9499. }
  9500. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9501. {
  9502. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9503. TTBCustomItem * Parent;
  9504. if (!Bookmark->Node.IsEmpty())
  9505. {
  9506. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9507. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9508. }
  9509. else
  9510. {
  9511. Parent = Menu;
  9512. }
  9513. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9514. Item->Caption = Bookmark->Name;
  9515. Item->ShortCut = Bookmark->ShortCut;
  9516. Item->OnClick = OnBookmarkClick;
  9517. Item->Tag = reinterpret_cast<int>(Bookmark);
  9518. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9519. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9520. {
  9521. // No hint for location profiles that are actually mere "bookmarks"
  9522. }
  9523. else
  9524. {
  9525. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9526. UnicodeString LongHint;
  9527. if (!Bookmark->Local.IsEmpty())
  9528. {
  9529. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9530. }
  9531. if (!Bookmark->Remote.IsEmpty())
  9532. {
  9533. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9534. }
  9535. AddToList(Hint, LongHint, L"|");
  9536. Item->Hint = Hint;
  9537. }
  9538. Parent->Add(Item.release());
  9539. }
  9540. }
  9541. AddMenuSeparator(Menu);
  9542. }
  9543. }
  9544. //---------------------------------------------------------------------------
  9545. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9546. {
  9547. Menu->Clear();
  9548. std::unique_ptr<TTBCustomItem> Item;
  9549. Item.reset(new TTBXItem(Owner));
  9550. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9551. Item->Options = Item->Options << tboDefault;
  9552. Menu->Add(Item.release());
  9553. AddMenuSeparator(Menu);
  9554. if (Terminal != NULL)
  9555. {
  9556. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9557. }
  9558. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9559. Item.reset(new TTBXItem(Owner));
  9560. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9561. Menu->Add(Item.release());
  9562. }
  9563. //---------------------------------------------------------------------------
  9564. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9565. {
  9566. bool Result = true;
  9567. try
  9568. {
  9569. if (Side == osRemote)
  9570. {
  9571. Terminal->ExceptionOnFail = true;
  9572. }
  9573. try
  9574. {
  9575. DirView(Side)->Path = Path;
  9576. }
  9577. __finally
  9578. {
  9579. if (Side == osRemote)
  9580. {
  9581. Terminal->ExceptionOnFail = false;
  9582. }
  9583. }
  9584. }
  9585. catch (Exception & E)
  9586. {
  9587. if ((Side != osRemote) || Terminal->Active)
  9588. {
  9589. ShowExtendedExceptionEx(Terminal, &E);
  9590. Result = false;
  9591. }
  9592. else
  9593. {
  9594. throw;
  9595. }
  9596. }
  9597. return Result;
  9598. }
  9599. //---------------------------------------------------------------------------
  9600. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9601. {
  9602. // Make sure there some feedback even when loading tiny local folders
  9603. TInstantOperationVisualizer Visualizer;
  9604. DirView(Side)->ReloadDirectory();
  9605. }
  9606. //---------------------------------------------------------------------------
  9607. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9608. {
  9609. if (Index == SessionsPageControl->TabIndex)
  9610. {
  9611. CloseSession();
  9612. }
  9613. else
  9614. {
  9615. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9616. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9617. TTerminalManager * Manager = TTerminalManager::Instance();
  9618. if (!Terminal->Active)
  9619. {
  9620. Manager->FreeTerminal(Terminal);
  9621. }
  9622. else
  9623. {
  9624. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9625. if (CanCloseQueue(Queue))
  9626. {
  9627. Manager->FreeTerminal(Terminal);
  9628. }
  9629. }
  9630. }
  9631. }
  9632. //---------------------------------------------------------------------------
  9633. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9634. {
  9635. CloseSessionTab(Index);
  9636. }
  9637. //---------------------------------------------------------------------------
  9638. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9639. {
  9640. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9641. {
  9642. TInstantOperationVisualizer Visualizer;
  9643. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9644. // before we create a new data object
  9645. ClipboardClear();
  9646. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9647. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9648. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9649. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9650. if (FClipboardDragDropFilesEx.get() == NULL)
  9651. {
  9652. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9653. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9654. }
  9655. FClipboardDragDropFilesEx->FileList->Clear();
  9656. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9657. FClipboardDragDropFilesEx->CopyToClipboard();
  9658. FClipboardTerminal = FTerminal;
  9659. // Need full paths, as cwd can be different once files are pasted
  9660. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9661. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9662. {
  9663. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9664. }
  9665. }
  9666. }
  9667. //---------------------------------------------------------------------------
  9668. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9669. {
  9670. if (OpenClipboard(0))
  9671. {
  9672. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9673. CloseClipboard();
  9674. }
  9675. }
  9676. //---------------------------------------------------------------------------
  9677. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9678. {
  9679. return !FClipboardFakeDirectory.IsEmpty();
  9680. }
  9681. //---------------------------------------------------------------------------
  9682. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9683. {
  9684. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9685. FClipboardFakeDirectory = UnicodeString();
  9686. FClipboardTerminal = NULL;
  9687. // Also called in Idle()
  9688. FClipboardFakeMonitors.reset(NULL);
  9689. }
  9690. //---------------------------------------------------------------------------
  9691. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9692. {
  9693. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9694. {
  9695. ClipboardStop();
  9696. }
  9697. }
  9698. //---------------------------------------------------------------------------
  9699. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9700. {
  9701. TTerminalManager * Manager = TTerminalManager::Instance();
  9702. Manager->ActiveTerminal = FClipboardTerminal;
  9703. TTransferOperationParam Params;
  9704. Params.TargetDirectory = TargetDirectory;
  9705. Params.DragDrop = DragDrop;
  9706. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9707. }
  9708. //---------------------------------------------------------------------------
  9709. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9710. {
  9711. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9712. // (as it pumps a Windows message queue while beign shutted down)
  9713. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9714. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9715. {
  9716. // Can fail as it can be e.g. locked by AV, so we retry that later
  9717. bool Removed = RemoveDir(ApiPath(FileName));
  9718. try
  9719. {
  9720. if (!NonVisualDataModule->Busy)
  9721. {
  9722. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9723. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9724. }
  9725. }
  9726. __finally
  9727. {
  9728. if (!Removed)
  9729. {
  9730. RemoveDir(ApiPath(FileName));
  9731. }
  9732. }
  9733. }
  9734. }
  9735. //---------------------------------------------------------------------------
  9736. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9737. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9738. {
  9739. TFileMasks::TParams MaskParams;
  9740. MaskParams.Size = Size;
  9741. MaskParams.Modification = Modification;
  9742. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9743. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9744. {
  9745. bool ImplicitMatch;
  9746. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9747. !ImplicitMatch)
  9748. {
  9749. Color = Iter->Color;
  9750. break;
  9751. }
  9752. }
  9753. }
  9754. //---------------------------------------------------------------------------
  9755. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9756. {
  9757. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9758. if (!DirView->IsEditing())
  9759. {
  9760. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9761. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9762. // while searching
  9763. if (Key == VK_BACK)
  9764. {
  9765. if (!FIncrementalSearch.IsEmpty())
  9766. {
  9767. if (FIncrementalSearch.Length() == 1)
  9768. {
  9769. ResetIncrementalSearch();
  9770. }
  9771. else
  9772. {
  9773. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9774. IncrementalSearch(NewText, false, false);
  9775. }
  9776. Key = 0;
  9777. }
  9778. }
  9779. }
  9780. }
  9781. //---------------------------------------------------------------------------
  9782. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9783. {
  9784. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9785. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9786. {
  9787. // Filter control sequences.
  9788. // When not searching yet, prefer use of the space for toggling file selection
  9789. // (so we cannot incrementally search for a string starting with a space).
  9790. if ((Key > VK_SPACE) ||
  9791. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9792. {
  9793. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9794. Key = 0;
  9795. }
  9796. }
  9797. }
  9798. //---------------------------------------------------------------------------
  9799. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9800. {
  9801. if (!FIncrementalSearch.IsEmpty())
  9802. {
  9803. FIncrementalSearch = L"";
  9804. DirView(osCurrent)->UpdateStatusBar();
  9805. }
  9806. }
  9807. //---------------------------------------------------------------------------
  9808. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9809. {
  9810. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9811. if (Item == NULL)
  9812. {
  9813. MessageBeep(MB_ICONHAND);
  9814. }
  9815. else
  9816. {
  9817. TCustomDirView * ADirView = DirView(osCurrent);
  9818. {
  9819. TAutoNestingCounter Guard(FIncrementalSearching);
  9820. ADirView->FocusItem(Item);
  9821. if (ADirView->NortonLike == nlOff)
  9822. {
  9823. if ((ADirView->Selected != Item) ||
  9824. (ADirView->SelCount > 1))
  9825. {
  9826. ADirView->ClearSelection();
  9827. ADirView->Selected = Item;
  9828. }
  9829. }
  9830. }
  9831. FIncrementalSearch = Text;
  9832. Item->MakeVisible(false);
  9833. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9834. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9835. ADirView->UpdateStatusBar();
  9836. }
  9837. }
  9838. //---------------------------------------------------------------------------
  9839. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9840. {
  9841. int Index = Item->Index;
  9842. if (!Reverse)
  9843. {
  9844. Index++;
  9845. if (Index >= Item->Owner->Count)
  9846. {
  9847. Index = 0;
  9848. }
  9849. }
  9850. else
  9851. {
  9852. Index--;
  9853. if (Index < 0)
  9854. {
  9855. Index = Item->Owner->Count - 1;
  9856. }
  9857. }
  9858. return Item->Owner->Item[Index];
  9859. }
  9860. //---------------------------------------------------------------------------
  9861. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9862. {
  9863. TCustomDirView * ADirView = DirView(osCurrent);
  9864. if (ADirView->Items->Count == 0)
  9865. {
  9866. return NULL;
  9867. }
  9868. else
  9869. {
  9870. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9871. TListItem * Item = CurrentItem;
  9872. if (SkipCurrent)
  9873. {
  9874. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9875. }
  9876. while (true)
  9877. {
  9878. bool Matches = false;
  9879. switch (WinConfiguration->PanelSearch)
  9880. {
  9881. case isNameStartOnly:
  9882. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9883. break;
  9884. case isName:
  9885. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9886. break;
  9887. case isAll:
  9888. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9889. int Index = 0;
  9890. while ((Index < ColCount) && !Matches)
  9891. {
  9892. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9893. if (Visible)
  9894. {
  9895. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9896. }
  9897. Index++;
  9898. }
  9899. break;
  9900. }
  9901. if (Matches)
  9902. {
  9903. return Item;
  9904. }
  9905. Item = GetNextFile(Item, Reverse);
  9906. if (Item == CurrentItem)
  9907. {
  9908. return NULL;
  9909. }
  9910. }
  9911. }
  9912. }
  9913. //---------------------------------------------------------------------------
  9914. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9915. {
  9916. ResetIncrementalSearch();
  9917. }
  9918. //---------------------------------------------------------------------------
  9919. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9920. {
  9921. if (Message.CharCode == VK_TAB)
  9922. {
  9923. if (!FIncrementalSearch.IsEmpty())
  9924. {
  9925. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  9926. bool Reverse = Shift.Contains(ssShift);
  9927. IncrementalSearch(FIncrementalSearch, true, Reverse);
  9928. Message.Result = 1;
  9929. return;
  9930. }
  9931. }
  9932. else if (Message.CharCode == VK_ESCAPE)
  9933. {
  9934. if (!FIncrementalSearch.IsEmpty())
  9935. {
  9936. ResetIncrementalSearch();
  9937. Message.Result = 1;
  9938. return;
  9939. }
  9940. }
  9941. TForm::Dispatch(&Message);
  9942. }
  9943. //---------------------------------------------------------------------------
  9944. void __fastcall TCustomScpExplorerForm::Deactivate()
  9945. {
  9946. TForm::Deactivate();
  9947. // E.g. when switching to an internal editor window
  9948. ResetIncrementalSearch();
  9949. }
  9950. //---------------------------------------------------------------------------
  9951. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  9952. {
  9953. // When switching to another application
  9954. ResetIncrementalSearch();
  9955. }
  9956. //---------------------------------------------------------------------------
  9957. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  9958. {
  9959. ResetIncrementalSearch();
  9960. }
  9961. //---------------------------------------------------------------------------
  9962. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  9963. {
  9964. if (FIncrementalSearching <= 0)
  9965. {
  9966. ResetIncrementalSearch();
  9967. }
  9968. }
  9969. //---------------------------------------------------------------------------
  9970. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  9971. {
  9972. CountClicksForWindowPrint(this);
  9973. }
  9974. //---------------------------------------------------------------------------
  9975. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  9976. {
  9977. CenterReconnectToolbar();
  9978. }
  9979. //---------------------------------------------------------------------------